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,
const MacroDefinition &MD,
38 SourceRange Range,
const MacroArgs *Args)
override {
41 for (
const Token &T : MD.getMacroInfo()->tokens()) {
42 if (
T.is(tok::identifier)) {
43 StringRef IdentName =
T.getIdentifierInfo()->getName();
44 if (IdentName ==
"__FILE__") {
46 }
else if (IdentName ==
"__LINE__") {
51 if (HasFile && HasLine) {
52 SuppressMacroExpansions->insert(Range);
60AST_MATCHER(CXXMethodDecl, isInLambda) {
return Node.getParent()->isLambda(); }
67 IgnoreMacros(Options.get(
"IgnoreMacros", DefaultIgnoreMacros)) {}
70 Options.store(Opts,
"IgnoreMacros", IgnoreMacros);
75 cxxMethodDecl(isInLambda(),
76 hasBody(forEachDescendant(
77 predefinedExpr(hasAncestor(cxxMethodDecl().bind(
"fn")))
79 equalsBoundNode(
"fn")),
84 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
85 PP->addPPCallbacks(std::make_unique<MacroExpansionsWithFileAndLine>(
86 &SuppressMacroExpansions));
90 const auto *E = Result.Nodes.getNodeAs<PredefinedExpr>(
"E");
91 if (E->getIdentKind() != PredefinedIdentKind::Func &&
92 E->getIdentKind() != PredefinedIdentKind::Function) {
96 if (E->getLocation().isMacroID()) {
101 Result.SourceManager->getImmediateExpansionRange(E->getLocation());
102 if (SuppressMacroExpansions.find(ER.getAsRange()) !=
103 SuppressMacroExpansions.end()) {
109 diag(E->getLocation(),
110 "inside a lambda, '%0' expands to the name of the function call "
111 "operator; consider capturing the name of the enclosing function "
113 << PredefinedExpr::getIdentKindName(E->getIdentKind());
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
std::set< SourceRange, SourceRangeLessThan > SourceRangeSet
LambdaFunctionNameCheck(StringRef Name, ClangTidyContext *Context)
AST_MATCHER(BinaryOperator, isRelationalOperator)
llvm::StringMap< ClangTidyValue > OptionMap