clang-tools 20.0.0git
AvoidDoWhileCheck.cpp
Go to the documentation of this file.
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
13using namespace clang::ast_matchers;
14
16
18 : ClangTidyCheck(Name, Context),
19 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", false)) {}
20
22 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
23}
24
25void AvoidDoWhileCheck::registerMatchers(MatchFinder *Finder) {
26 Finder->addMatcher(doStmt().bind("x"), this);
27}
28
29void AvoidDoWhileCheck::check(const MatchFinder::MatchResult &Result) {
30 if (const auto *MatchedDecl = Result.Nodes.getNodeAs<DoStmt>("x")) {
31 if (IgnoreMacros && MatchedDecl->getBeginLoc().isMacroID())
32 return;
33 diag(MatchedDecl->getBeginLoc(), "avoid do-while loops");
34 }
35}
36
37} // namespace clang::tidy::cppcoreguidelines
llvm::SmallString< 256U > Name
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AvoidDoWhileCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
llvm::StringMap< ClangTidyValue > OptionMap