| 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/ASTMatchers/ASTMatchFinder.h" |
| 11 | |
| 12 | using namespace clang::ast_matchers; |
| 13 | |
| 14 | namespace clang::tidy::cppcoreguidelines { |
| 15 | |
| 16 | AvoidDoWhileCheck::AvoidDoWhileCheck(StringRef Name, ClangTidyContext *Context) |
| 17 | : ClangTidyCheck(Name, Context), |
| 18 | IgnoreMacros(Options.getLocalOrGlobal(LocalName: "IgnoreMacros", Default: false)) {} |
| 19 | |
| 20 | void AvoidDoWhileCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 21 | Options.store(Options&: Opts, LocalName: "IgnoreMacros", Value: IgnoreMacros); |
| 22 | } |
| 23 | |
| 24 | void AvoidDoWhileCheck::registerMatchers(MatchFinder *Finder) { |
| 25 | Finder->addMatcher(NodeMatch: doStmt().bind(ID: "x"), Action: this); |
| 26 | } |
| 27 | |
| 28 | void AvoidDoWhileCheck::check(const MatchFinder::MatchResult &Result) { |
| 29 | if (const auto *MatchedDecl = Result.Nodes.getNodeAs<DoStmt>(ID: "x")) { |
| 30 | if (IgnoreMacros && MatchedDecl->getBeginLoc().isMacroID()) |
| 31 | return; |
| 32 | diag(Loc: MatchedDecl->getBeginLoc(), Description: "avoid do-while loops"); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | } // namespace clang::tidy::cppcoreguidelines |
| 37 |
