clang-tools 23.0.0git
LambdaFunctionNameCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
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"
17
18using namespace clang::ast_matchers;
19
20namespace clang::tidy::bugprone {
21
22namespace {
23
24static constexpr bool DefaultIgnoreMacros = false;
25
26// Keep track of macro expansions that contain both __FILE__ and __LINE__. If
27// such a macro also uses __func__ or __FUNCTION__, we don't want to issue a
28// warning because __FILE__ and __LINE__ may be useful even if __func__ or
29// __FUNCTION__ is not, especially if the macro could be used in the context of
30// either a function body or a lambda body.
31class MacroExpansionsWithFileAndLine : public PPCallbacks {
32public:
33 explicit MacroExpansionsWithFileAndLine(llvm::DenseSet<SourceRange> *SME)
34 : SuppressMacroExpansions(SME) {}
35
36 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
37 SourceRange Range, const MacroArgs *Args) override {
38 bool HasFile = false;
39 bool HasLine = false;
40 for (const Token &T : MD.getMacroInfo()->tokens()) {
41 if (T.is(tok::identifier)) {
42 const StringRef IdentName = T.getIdentifierInfo()->getName();
43 if (IdentName == "__FILE__")
44 HasFile = true;
45 else if (IdentName == "__LINE__")
46 HasLine = true;
47 }
48 }
49 if (HasFile && HasLine)
50 SuppressMacroExpansions->insert(Range);
51 }
52
53private:
54 llvm::DenseSet<SourceRange> *SuppressMacroExpansions;
55};
56
57AST_MATCHER(CXXMethodDecl, isInLambda) { return Node.getParent()->isLambda(); }
58
59} // namespace
60
62 ClangTidyContext *Context)
63 : ClangTidyCheck(Name, Context),
64 IgnoreMacros(Options.get("IgnoreMacros", DefaultIgnoreMacros)) {}
65
67 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
68}
69
71 Finder->addMatcher(
72 cxxMethodDecl(isInLambda(),
73 hasBody(forEachDescendant(
74 predefinedExpr(hasAncestor(cxxMethodDecl().bind("fn")))
75 .bind("E"))),
76 equalsBoundNode("fn")),
77 this);
78}
79
81 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
82 PP->addPPCallbacks(std::make_unique<MacroExpansionsWithFileAndLine>(
83 &SuppressMacroExpansions));
84}
85
86void LambdaFunctionNameCheck::check(const MatchFinder::MatchResult &Result) {
87 const auto *E = Result.Nodes.getNodeAs<PredefinedExpr>("E");
88 if (E->getIdentKind() != PredefinedIdentKind::Func &&
89 E->getIdentKind() != PredefinedIdentKind::Function) {
90 // We don't care about other PredefinedExprs.
91 return;
92 }
93 if (E->getLocation().isMacroID()) {
94 if (IgnoreMacros)
95 return;
96
97 auto ER =
98 Result.SourceManager->getImmediateExpansionRange(E->getLocation());
99 if (SuppressMacroExpansions.contains(ER.getAsRange())) {
100 // This is a macro expansion for which we should not warn.
101 return;
102 }
103 }
104
105 diag(E->getLocation(),
106 "inside a lambda, '%0' expands to the name of the function call "
107 "operator; consider capturing the name of the enclosing function "
108 "explicitly")
109 << PredefinedExpr::getIdentKindName(E->getIdentKind());
110}
111
112} // namespace clang::tidy::bugprone
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
LambdaFunctionNameCheck(StringRef Name, ClangTidyContext *Context)
AST_MATCHER(BinaryOperator, isRelationalOperator)
llvm::StringMap< ClangTidyValue > OptionMap