blob: 3e800cd59e1a5f2d7da8fb21f44a2531efde5ee7 [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
Benjamin Kramerf8c99292018-02-18 19:02:35 +000020namespace {
Jonas Tothf22f3482018-01-17 10:27:41 +000021AST_MATCHER(GotoStmt, isForwardJumping) {
22 return Node.getLocStart() < Node.getLabel()->getLocStart();
23}
Benjamin Kramerf8c99292018-02-18 19:02:35 +000024} // namespace
Jonas Tothf22f3482018-01-17 10:27:41 +000025
26void AvoidGotoCheck::registerMatchers(MatchFinder *Finder) {
27 if (!getLangOpts().CPlusPlus)
28 return;
29
30 // TODO: This check does not recognize `IndirectGotoStmt` which is a
31 // GNU extension. These must be matched separately and an AST matcher
32 // is currently missing for them.
33
34 // Check if the 'goto' is used for control flow other than jumping
35 // out of a nested loop.
36 auto Loop = stmt(anyOf(forStmt(), cxxForRangeStmt(), whileStmt(), doStmt()));
37 auto NestedLoop =
38 stmt(anyOf(forStmt(hasAncestor(Loop)), cxxForRangeStmt(hasAncestor(Loop)),
39 whileStmt(hasAncestor(Loop)), doStmt(hasAncestor(Loop))));
40
41 Finder->addMatcher(gotoStmt(anyOf(unless(hasAncestor(NestedLoop)),
42 unless(isForwardJumping())))
43 .bind("goto"),
44 this);
45}
46
47void AvoidGotoCheck::check(const MatchFinder::MatchResult &Result) {
48 const auto *Goto = Result.Nodes.getNodeAs<GotoStmt>("goto");
49
50 diag(Goto->getGotoLoc(), "avoid using 'goto' for flow control")
51 << Goto->getSourceRange();
52 diag(Goto->getLabel()->getLocStart(), "label defined here",
53 DiagnosticIDs::Note);
54}
55} // namespace cppcoreguidelines
56} // namespace tidy
57} // namespace clang