10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Lex/MacroInfo.h"
14#include "clang/Lex/Preprocessor.h"
22static constexpr bool DefaultIgnoreMacros =
false;
29class MacroExpansionsWithFileAndLine :
public PPCallbacks {
31 explicit MacroExpansionsWithFileAndLine(
33 : SuppressMacroExpansions(SME) {}
35 void MacroExpands(
const Token &MacroNameTok,
36 const MacroDefinition &MD, SourceRange Range,
37 const MacroArgs *
Args)
override {
40 for (
const auto& T : MD.getMacroInfo()->tokens()) {
41 if (T.is(tok::identifier)) {
42 StringRef IdentName = T.getIdentifierInfo()->getName();
43 if (IdentName ==
"__FILE__") {
45 }
else if (IdentName ==
"__LINE__") {
50 if (HasFile && HasLine) {
51 SuppressMacroExpansions->insert(Range);
65 Options.getLocalOrGlobal(
"IgnoreMacros", DefaultIgnoreMacros)) {}
73 Finder->addMatcher(predefinedExpr(hasAncestor(lambdaExpr())).bind(
"E"),
78 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
79 PP->addPPCallbacks(std::make_unique<MacroExpansionsWithFileAndLine>(
80 &SuppressMacroExpansions));
84 const auto *
E = Result.Nodes.getNodeAs<PredefinedExpr>(
"E");
85 if (
E->getIdentKind() != PredefinedExpr::Func &&
86 E->getIdentKind() != PredefinedExpr::Function) {
90 if (
E->getLocation().isMacroID()) {
95 Result.SourceManager->getImmediateExpansionRange(
E->getLocation());
96 if (SuppressMacroExpansions.find(ER.getAsRange()) !=
97 SuppressMacroExpansions.end()) {
103 diag(
E->getLocation(),
104 "inside a lambda, '%0' expands to the name of the function call "
105 "operator; consider capturing the name of the enclosing function "
107 << PredefinedExpr::getIdentKindName(
E->getIdentKind());
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)
llvm::StringMap< ClangTidyValue > OptionMap