annotate clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidGotoCheck.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- AvoidGotoCheck.cpp - clang-tidy-----------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "AvoidGotoCheck.h"
anatofuz
parents:
diff changeset
10 #include "clang/AST/ASTContext.h"
anatofuz
parents:
diff changeset
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 using namespace clang::ast_matchers;
anatofuz
parents:
diff changeset
14
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
15 namespace clang::tidy::cppcoreguidelines {
150
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 namespace {
anatofuz
parents:
diff changeset
18 AST_MATCHER(GotoStmt, isForwardJumping) {
anatofuz
parents:
diff changeset
19 return Node.getBeginLoc() < Node.getLabel()->getBeginLoc();
anatofuz
parents:
diff changeset
20 }
anatofuz
parents:
diff changeset
21 } // namespace
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 void AvoidGotoCheck::registerMatchers(MatchFinder *Finder) {
anatofuz
parents:
diff changeset
24 // TODO: This check does not recognize `IndirectGotoStmt` which is a
anatofuz
parents:
diff changeset
25 // GNU extension. These must be matched separately and an AST matcher
anatofuz
parents:
diff changeset
26 // is currently missing for them.
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 // Check if the 'goto' is used for control flow other than jumping
anatofuz
parents:
diff changeset
29 // out of a nested loop.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
30 auto Loop = mapAnyOf(forStmt, cxxForRangeStmt, whileStmt, doStmt);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
31 auto NestedLoop = Loop.with(hasAncestor(Loop));
150
anatofuz
parents:
diff changeset
32
anatofuz
parents:
diff changeset
33 Finder->addMatcher(gotoStmt(anyOf(unless(hasAncestor(NestedLoop)),
anatofuz
parents:
diff changeset
34 unless(isForwardJumping())))
anatofuz
parents:
diff changeset
35 .bind("goto"),
anatofuz
parents:
diff changeset
36 this);
anatofuz
parents:
diff changeset
37 }
anatofuz
parents:
diff changeset
38
anatofuz
parents:
diff changeset
39 void AvoidGotoCheck::check(const MatchFinder::MatchResult &Result) {
anatofuz
parents:
diff changeset
40 const auto *Goto = Result.Nodes.getNodeAs<GotoStmt>("goto");
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 diag(Goto->getGotoLoc(), "avoid using 'goto' for flow control")
anatofuz
parents:
diff changeset
43 << Goto->getSourceRange();
anatofuz
parents:
diff changeset
44 diag(Goto->getLabel()->getBeginLoc(), "label defined here",
anatofuz
parents:
diff changeset
45 DiagnosticIDs::Note);
anatofuz
parents:
diff changeset
46 }
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
47 } // namespace clang::tidy::cppcoreguidelines