Jonas Toth | f22f348 | 2018-01-17 10:27:41 +0000 | [diff] [blame] | 1 | //===--- AvoidGotoCheck.cpp - clang-tidy-----------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jonas Toth | f22f348 | 2018-01-17 10:27:41 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "AvoidGotoCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | |
| 13 | using namespace clang::ast_matchers; |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| 17 | namespace cppcoreguidelines { |
| 18 | |
Benjamin Kramer | f8c9929 | 2018-02-18 19:02:35 +0000 | [diff] [blame] | 19 | namespace { |
Jonas Toth | f22f348 | 2018-01-17 10:27:41 +0000 | [diff] [blame] | 20 | AST_MATCHER(GotoStmt, isForwardJumping) { |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 21 | return Node.getBeginLoc() < Node.getLabel()->getBeginLoc(); |
Jonas Toth | f22f348 | 2018-01-17 10:27:41 +0000 | [diff] [blame] | 22 | } |
Benjamin Kramer | f8c9929 | 2018-02-18 19:02:35 +0000 | [diff] [blame] | 23 | } // namespace |
Jonas Toth | f22f348 | 2018-01-17 10:27:41 +0000 | [diff] [blame] | 24 | |
| 25 | void AvoidGotoCheck::registerMatchers(MatchFinder *Finder) { |
| 26 | if (!getLangOpts().CPlusPlus) |
| 27 | return; |
| 28 | |
| 29 | // TODO: This check does not recognize `IndirectGotoStmt` which is a |
| 30 | // GNU extension. These must be matched separately and an AST matcher |
| 31 | // is currently missing for them. |
| 32 | |
| 33 | // Check if the 'goto' is used for control flow other than jumping |
| 34 | // out of a nested loop. |
| 35 | auto Loop = stmt(anyOf(forStmt(), cxxForRangeStmt(), whileStmt(), doStmt())); |
| 36 | auto NestedLoop = |
| 37 | stmt(anyOf(forStmt(hasAncestor(Loop)), cxxForRangeStmt(hasAncestor(Loop)), |
| 38 | whileStmt(hasAncestor(Loop)), doStmt(hasAncestor(Loop)))); |
| 39 | |
| 40 | Finder->addMatcher(gotoStmt(anyOf(unless(hasAncestor(NestedLoop)), |
| 41 | unless(isForwardJumping()))) |
| 42 | .bind("goto"), |
| 43 | this); |
| 44 | } |
| 45 | |
| 46 | void AvoidGotoCheck::check(const MatchFinder::MatchResult &Result) { |
| 47 | const auto *Goto = Result.Nodes.getNodeAs<GotoStmt>("goto"); |
| 48 | |
| 49 | diag(Goto->getGotoLoc(), "avoid using 'goto' for flow control") |
| 50 | << Goto->getSourceRange(); |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 51 | diag(Goto->getLabel()->getBeginLoc(), "label defined here", |
Jonas Toth | f22f348 | 2018-01-17 10:27:41 +0000 | [diff] [blame] | 52 | DiagnosticIDs::Note); |
| 53 | } |
| 54 | } // namespace cppcoreguidelines |
| 55 | } // namespace tidy |
| 56 | } // namespace clang |