1 //===--- AvoidDoWhileCheck.cpp - clang-tidy -------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "AvoidDoWhileCheck.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 19 AvoidDoWhileCheck::AvoidDoWhileCheck(StringRef Name, ClangTidyContext *Context) 20 : ClangTidyCheck(Name, Context), 21 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", false)) {} 22 23 void AvoidDoWhileCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { 24 Options.store(Opts, "IgnoreMacros", IgnoreMacros); 25 } 26 27 void AvoidDoWhileCheck::registerMatchers(MatchFinder *Finder) { 28 Finder->addMatcher(doStmt().bind("x"), this); 29 } 30 31 void AvoidDoWhileCheck::check(const MatchFinder::MatchResult &Result) { 32 if (const auto *MatchedDecl = Result.Nodes.getNodeAs<DoStmt>("x")) { 33 if (IgnoreMacros && MatchedDecl->getBeginLoc().isMacroID()) 34 return; 35 diag(MatchedDecl->getBeginLoc(), "avoid do-while loops"); 36 } 37 } 38 39 } // namespace cppcoreguidelines 40 } // namespace tidy 41 } // namespace clang 42