aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCongcong Cai <congcongcai0907@163.com>2024-04-20 07:45:20 +0800
committerGitHub <noreply@github.com>2024-04-20 07:45:20 +0800
commit7fcca112034f55c26e943c13fd0d57adfbe96705 (patch)
tree4674605d1f264aaa847e18af4bbcc7b4dae8da81
parent6a35ee8077647b32626c3c41f9d9da4dae6670fc (diff)
downloadllvm-7fcca112034f55c26e943c13fd0d57adfbe96705.zip
llvm-7fcca112034f55c26e943c13fd0d57adfbe96705.tar.gz
llvm-7fcca112034f55c26e943c13fd0d57adfbe96705.tar.bz2
[clang-tidy] bugprone-lambda-function-name ignore macro in captures (#89076)
-rw-r--r--clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp14
-rw-r--r--clang-tools-extra/docs/ReleaseNotes.rst4
-rw-r--r--clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp19
3 files changed, 34 insertions, 3 deletions
diff --git a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
index 5260a8b..32f5edd 100644
--- a/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp
@@ -8,7 +8,9 @@
#include "LambdaFunctionNameCheck.h"
#include "clang/AST/ASTContext.h"
+#include "clang/AST/DeclCXX.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
@@ -56,6 +58,8 @@ private:
LambdaFunctionNameCheck::SourceRangeSet* SuppressMacroExpansions;
};
+AST_MATCHER(CXXMethodDecl, isInLambda) { return Node.getParent()->isLambda(); }
+
} // namespace
LambdaFunctionNameCheck::LambdaFunctionNameCheck(StringRef Name,
@@ -69,9 +73,13 @@ void LambdaFunctionNameCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
}
void LambdaFunctionNameCheck::registerMatchers(MatchFinder *Finder) {
- // Match on PredefinedExprs inside a lambda.
- Finder->addMatcher(predefinedExpr(hasAncestor(lambdaExpr())).bind("E"),
- this);
+ Finder->addMatcher(
+ cxxMethodDecl(isInLambda(),
+ hasBody(forEachDescendant(
+ predefinedExpr(hasAncestor(cxxMethodDecl().bind("fn")))
+ .bind("E"))),
+ equalsBoundNode("fn")),
+ this);
}
void LambdaFunctionNameCheck::registerPPCallbacks(
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a457e6f..9ef1d38 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -155,6 +155,10 @@ Changes in existing checks
<clang-tidy/checks/bugprone/inc-dec-in-conditions>` check to ignore code
within unevaluated contexts, such as ``decltype``.
+- Improved :doc:`bugprone-lambda-function-name<clang-tidy/checks/bugprone/lambda-function-name>`
+ check by ignoring ``__func__`` macro in lambda captures, initializers of
+ default parameters and nested function declarations.
+
- Improved :doc:`bugprone-non-zero-enum-to-bool-conversion
<clang-tidy/checks/bugprone/non-zero-enum-to-bool-conversion>` check by
eliminating false positives resulting from direct usage of bitwise operators
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp
index 936ee87..5c2bb57 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp
@@ -19,6 +19,22 @@ void Positives() {
// CHECK-MESSAGES-NO-CONFIG: :[[@LINE-1]]:8: warning: inside a lambda, '__FUNCTION__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
[] { EMBED_IN_ANOTHER_MACRO1; }();
// CHECK-MESSAGES-NO-CONFIG: :[[@LINE-1]]:8: warning: inside a lambda, '__func__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
+ [] {
+ __func__;
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: inside a lambda, '__func__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
+ struct S {
+ void f() {
+ __func__;
+ [] {
+ __func__;
+ // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: inside a lambda, '__func__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
+ }();
+ __func__;
+ }
+ };
+ __func__;
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: inside a lambda, '__func__' expands to the name of the function call operator; consider capturing the name of the enclosing function explicitly [bugprone-lambda-function-name]
+ }();
}
#define FUNC_MACRO_WITH_FILE_AND_LINE Foo(__func__, __FILE__, __LINE__)
@@ -40,4 +56,7 @@ void Negatives() {
[] { FUNC_MACRO_WITH_FILE_AND_LINE; }();
[] { FUNCTION_MACRO_WITH_FILE_AND_LINE; }();
[] { EMBED_IN_ANOTHER_MACRO2; }();
+
+ [] (const char* func = __func__) { func; }();
+ [func=__func__] { func; }();
}