10#include "clang/AST/ASTContext.h"
11#include "clang/AST/DeclCXX.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Lex/MacroInfo.h"
16#include "clang/Lex/Preprocessor.h"
24static constexpr bool DefaultIgnoreMacros =
false;
31class MacroExpansionsWithFileAndLine :
public PPCallbacks {
33 explicit MacroExpansionsWithFileAndLine(
35 : SuppressMacroExpansions(SME) {}
37 void MacroExpands(
const Token &MacroNameTok,
38 const MacroDefinition &MD, SourceRange Range,
39 const MacroArgs *
Args)
override {
42 for (
const auto& T : MD.getMacroInfo()->tokens()) {
43 if (
T.is(tok::identifier)) {
44 StringRef IdentName =
T.getIdentifierInfo()->getName();
45 if (IdentName ==
"__FILE__") {
47 }
else if (IdentName ==
"__LINE__") {
52 if (HasFile && HasLine) {
53 SuppressMacroExpansions->insert(Range);
61AST_MATCHER(CXXMethodDecl, isInLambda) {
return Node.getParent()->isLambda(); }
69 Options.getLocalOrGlobal(
"IgnoreMacros", DefaultIgnoreMacros)) {}
77 cxxMethodDecl(isInLambda(),
78 hasBody(forEachDescendant(
79 predefinedExpr(hasAncestor(cxxMethodDecl().bind(
"fn")))
81 equalsBoundNode(
"fn")),
86 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
87 PP->addPPCallbacks(std::make_unique<MacroExpansionsWithFileAndLine>(
88 &SuppressMacroExpansions));
92 const auto *
E = Result.Nodes.getNodeAs<PredefinedExpr>(
"E");
93 if (
E->getIdentKind() != PredefinedIdentKind::Func &&
94 E->getIdentKind() != PredefinedIdentKind::Function) {
98 if (
E->getLocation().isMacroID()) {
103 Result.SourceManager->getImmediateExpansionRange(
E->getLocation());
104 if (SuppressMacroExpansions.find(ER.getAsRange()) !=
105 SuppressMacroExpansions.end()) {
111 diag(
E->getLocation(),
112 "inside a lambda, '%0' expands to the name of the function call "
113 "operator; consider capturing the name of the enclosing function "
115 << PredefinedExpr::getIdentKindName(
E->getIdentKind());
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
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 registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
std::set< SourceRange, SourceRangeLessThan > SourceRangeSet
LambdaFunctionNameCheck(StringRef Name, ClangTidyContext *Context)
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)
llvm::StringMap< ClangTidyValue > OptionMap