clang-tools 20.0.0git
LambdaFunctionNameCheck.cpp
Go to the documentation of this file.
1//===--- LambdaFunctionNameCheck.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
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(
35 : SuppressMacroExpansions(SME) {}
36
37 void MacroExpands(const Token &MacroNameTok,
38 const MacroDefinition &MD, SourceRange Range,
39 const MacroArgs *Args) override {
40 bool HasFile = false;
41 bool HasLine = false;
42 for (const auto& T : MD.getMacroInfo()->tokens()) {
43 if (T.is(tok::identifier)) {
44 StringRef IdentName = T.getIdentifierInfo()->getName();
45 if (IdentName == "__FILE__") {
46 HasFile = true;
47 } else if (IdentName == "__LINE__") {
48 HasLine = true;
49 }
50 }
51 }
52 if (HasFile && HasLine) {
53 SuppressMacroExpansions->insert(Range);
54 }
55 }
56
57private:
58 LambdaFunctionNameCheck::SourceRangeSet* SuppressMacroExpansions;
59};
60
61AST_MATCHER(CXXMethodDecl, isInLambda) { return Node.getParent()->isLambda(); }
62
63} // namespace
64
66 ClangTidyContext *Context)
67 : ClangTidyCheck(Name, Context),
68 IgnoreMacros(
69 Options.getLocalOrGlobal("IgnoreMacros", DefaultIgnoreMacros)) {}
70
72 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
73}
74
76 Finder->addMatcher(
77 cxxMethodDecl(isInLambda(),
78 hasBody(forEachDescendant(
79 predefinedExpr(hasAncestor(cxxMethodDecl().bind("fn")))
80 .bind("E"))),
81 equalsBoundNode("fn")),
82 this);
83}
84
86 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
87 PP->addPPCallbacks(std::make_unique<MacroExpansionsWithFileAndLine>(
88 &SuppressMacroExpansions));
89}
90
91void LambdaFunctionNameCheck::check(const MatchFinder::MatchResult &Result) {
92 const auto *E = Result.Nodes.getNodeAs<PredefinedExpr>("E");
93 if (E->getIdentKind() != PredefinedIdentKind::Func &&
94 E->getIdentKind() != PredefinedIdentKind::Function) {
95 // We don't care about other PredefinedExprs.
96 return;
97 }
98 if (E->getLocation().isMacroID()) {
99 if (IgnoreMacros)
100 return;
101
102 auto ER =
103 Result.SourceManager->getImmediateExpansionRange(E->getLocation());
104 if (SuppressMacroExpansions.find(ER.getAsRange()) !=
105 SuppressMacroExpansions.end()) {
106 // This is a macro expansion for which we should not warn.
107 return;
108 }
109 }
110
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 "
114 "explicitly")
115 << PredefinedExpr::getIdentKindName(E->getIdentKind());
116}
117
118} // namespace clang::tidy::bugprone
const Expr * E
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
llvm::json::Object Args
Definition: Trace.cpp:138
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