1 //===--- TerminatingContinueCheck.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 "TerminatingContinueCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14 #include "clang/Tooling/FixIt.h"
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace bugprone {
21 
22 void TerminatingContinueCheck::registerMatchers(MatchFinder *Finder) {
23   const auto doWithFalse =
24       doStmt(hasCondition(ignoringImpCasts(
25                  anyOf(cxxBoolLiteral(equals(false)), integerLiteral(equals(0)),
26                        cxxNullPtrLiteralExpr(), gnuNullExpr()))),
27              equalsBoundNode("closestLoop"));
28 
29   Finder->addMatcher(
30       continueStmt(hasAncestor(stmt(anyOf(forStmt(), whileStmt(),
31                                           cxxForRangeStmt(), doStmt()))
32                                    .bind("closestLoop")),
33                    hasAncestor(doWithFalse))
34           .bind("continue"),
35       this);
36 }
37 
38 void TerminatingContinueCheck::check(const MatchFinder::MatchResult &Result) {
39   const auto *ContStmt = Result.Nodes.getNodeAs<ContinueStmt>("continue");
40 
41   auto Diag =
42       diag(ContStmt->getLocStart(),
43            "'continue' in loop with false condition is equivalent to 'break'")
44       << tooling::fixit::createReplacement(*ContStmt, "break");
45 }
46 
47 } // namespace bugprone
48 } // namespace tidy
49 } // namespace clang
50