Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp

raw
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
9 #include "LambdaFunctionNameCheck.h"
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
16 using namespace clang::ast_matchers;
17
18 namespace clang::tidy::bugprone {
19
20 namespace {
21
22 // Keep track of macro expansions that contain both __FILE__ and __LINE__. If
23 // such a macro also uses __func__ or __FUNCTION__, we don't want to issue a
24 // warning because __FILE__ and __LINE__ may be useful even if __func__ or
25 // __FUNCTION__ is not, especially if the macro could be used in the context of
26 // either a function body or a lambda body.
27 class MacroExpansionsWithFileAndLine : public PPCallbacks {
28 public:
29 explicit MacroExpansionsWithFileAndLine(
30 LambdaFunctionNameCheck::SourceRangeSet *SME)
31 : SuppressMacroExpansions(SME) {}
32
33 void MacroExpands(const Token &MacroNameTok,
34 const MacroDefinition &MD, SourceRange Range,
35 const MacroArgs *Args) override {
36 bool HasFile = false;
37 bool HasLine = false;
38 for (const auto& T : MD.getMacroInfo()->tokens()) {
39 if (T.is(tok::identifier)) {
40 StringRef IdentName = T.getIdentifierInfo()->getName();
41 if (IdentName == "__FILE__") {
42 HasFile = true;
43 } else if (IdentName == "__LINE__") {
44 HasLine = true;
45 }
46 }
47 }
48 if (HasFile && HasLine) {
49 SuppressMacroExpansions->insert(Range);
50 }
51 }
52
53 private:
54 LambdaFunctionNameCheck::SourceRangeSet* SuppressMacroExpansions;
55 };
56
57 } // namespace
58
59 void LambdaFunctionNameCheck::registerMatchers(MatchFinder *Finder) {
60 // Match on PredefinedExprs inside a lambda.
61 Finder->addMatcher(predefinedExpr(hasAncestor(lambdaExpr())).bind("E"),
62 this);
63 }
64
65 void LambdaFunctionNameCheck::registerPPCallbacks(
66 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
67 PP->addPPCallbacks(std::make_unique<MacroExpansionsWithFileAndLine>(
68 &SuppressMacroExpansions));
69 }
70
71 void LambdaFunctionNameCheck::check(const MatchFinder::MatchResult &Result) {
72 const auto *E = Result.Nodes.getNodeAs<PredefinedExpr>("E");
73 if (E->getIdentKind() != PredefinedExpr::Func &&
74 E->getIdentKind() != PredefinedExpr::Function) {
75 // We don't care about other PredefinedExprs.
76 return;
77 }
78 if (E->getLocation().isMacroID()) {
79 auto ER =
80 Result.SourceManager->getImmediateExpansionRange(E->getLocation());
81 if (SuppressMacroExpansions.find(ER.getAsRange()) !=
82 SuppressMacroExpansions.end()) {
83 // This is a macro expansion for which we should not warn.
84 return;
85 }
86 }
87 diag(E->getLocation(),
88 "inside a lambda, '%0' expands to the name of the function call "
89 "operator; consider capturing the name of the enclosing function "
90 "explicitly")
91 << PredefinedExpr::getIdentKindName(E->getIdentKind());
92 }
93
94 } // namespace clang::tidy::bugprone