blob: 923555b90d84d5d6944fd460981b86bc66534a10 [file] [log] [blame]
Jonas Tothf22f3482018-01-17 10:27:41 +00001//===--- AvoidGotoCheck.cpp - clang-tidy-----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "AvoidGotoCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace cppcoreguidelines {
19
20AST_MATCHER(GotoStmt, isForwardJumping) {
21 return Node.getLocStart() < Node.getLabel()->getLocStart();
22}
23
24void AvoidGotoCheck::registerMatchers(MatchFinder *Finder) {
25 if (!getLangOpts().CPlusPlus)
26 return;
27
28 // TODO: This check does not recognize `IndirectGotoStmt` which is a
29 // GNU extension. These must be matched separately and an AST matcher
30 // is currently missing for them.
31
32 // Check if the 'goto' is used for control flow other than jumping
33 // out of a nested loop.
34 auto Loop = stmt(anyOf(forStmt(), cxxForRangeStmt(), whileStmt(), doStmt()));
35 auto NestedLoop =
36 stmt(anyOf(forStmt(hasAncestor(Loop)), cxxForRangeStmt(hasAncestor(Loop)),
37 whileStmt(hasAncestor(Loop)), doStmt(hasAncestor(Loop))));
38
39 Finder->addMatcher(gotoStmt(anyOf(unless(hasAncestor(NestedLoop)),
40 unless(isForwardJumping())))
41 .bind("goto"),
42 this);
43}
44
45void AvoidGotoCheck::check(const MatchFinder::MatchResult &Result) {
46 const auto *Goto = Result.Nodes.getNodeAs<GotoStmt>("goto");
47
48 diag(Goto->getGotoLoc(), "avoid using 'goto' for flow control")
49 << Goto->getSourceRange();
50 diag(Goto->getLabel()->getLocStart(), "label defined here",
51 DiagnosticIDs::Note);
52}
53} // namespace cppcoreguidelines
54} // namespace tidy
55} // namespace clang