clang-tools 19.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/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Lex/MacroInfo.h"
14#include "clang/Lex/Preprocessor.h"
15
16using namespace clang::ast_matchers;
17
18namespace clang::tidy::bugprone {
19
20namespace {
21
22static constexpr bool DefaultIgnoreMacros = false;
23
24// Keep track of macro expansions that contain both __FILE__ and __LINE__. If
25// such a macro also uses __func__ or __FUNCTION__, we don't want to issue a
26// warning because __FILE__ and __LINE__ may be useful even if __func__ or
27// __FUNCTION__ is not, especially if the macro could be used in the context of
28// either a function body or a lambda body.
29class MacroExpansionsWithFileAndLine : public PPCallbacks {
30public:
31 explicit MacroExpansionsWithFileAndLine(
33 : SuppressMacroExpansions(SME) {}
34
35 void MacroExpands(const Token &MacroNameTok,
36 const MacroDefinition &MD, SourceRange Range,
37 const MacroArgs *Args) override {
38 bool HasFile = false;
39 bool HasLine = false;
40 for (const auto& T : MD.getMacroInfo()->tokens()) {
41 if (T.is(tok::identifier)) {
42 StringRef IdentName = T.getIdentifierInfo()->getName();
43 if (IdentName == "__FILE__") {
44 HasFile = true;
45 } else if (IdentName == "__LINE__") {
46 HasLine = true;
47 }
48 }
49 }
50 if (HasFile && HasLine) {
51 SuppressMacroExpansions->insert(Range);
52 }
53 }
54
55private:
56 LambdaFunctionNameCheck::SourceRangeSet* SuppressMacroExpansions;
57};
58
59} // namespace
60
62 ClangTidyContext *Context)
63 : ClangTidyCheck(Name, Context),
64 IgnoreMacros(
65 Options.getLocalOrGlobal("IgnoreMacros", DefaultIgnoreMacros)) {}
66
68 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
69}
70
72 // Match on PredefinedExprs inside a lambda.
73 Finder->addMatcher(predefinedExpr(hasAncestor(lambdaExpr())).bind("E"),
74 this);
75}
76
78 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
79 PP->addPPCallbacks(std::make_unique<MacroExpansionsWithFileAndLine>(
80 &SuppressMacroExpansions));
81}
82
83void LambdaFunctionNameCheck::check(const MatchFinder::MatchResult &Result) {
84 const auto *E = Result.Nodes.getNodeAs<PredefinedExpr>("E");
85 if (E->getIdentKind() != PredefinedIdentKind::Func &&
86 E->getIdentKind() != PredefinedIdentKind::Function) {
87 // We don't care about other PredefinedExprs.
88 return;
89 }
90 if (E->getLocation().isMacroID()) {
91 if (IgnoreMacros)
92 return;
93
94 auto ER =
95 Result.SourceManager->getImmediateExpansionRange(E->getLocation());
96 if (SuppressMacroExpansions.find(ER.getAsRange()) !=
97 SuppressMacroExpansions.end()) {
98 // This is a macro expansion for which we should not warn.
99 return;
100 }
101 }
102
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 "
106 "explicitly")
107 << PredefinedExpr::getIdentKindName(E->getIdentKind());
108}
109
110} // namespace clang::tidy::bugprone
const Expr * E
llvm::SmallString< 256U > Name
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)
llvm::StringMap< ClangTidyValue > OptionMap