felix642 created this revision. Herald added subscribers: PiotrZSL, carlosgalvezp, xazax.hun. Herald added a reviewer: njames93. Herald added a project: All. felix642 requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
Improved check lambda-function-name with option IgnoreMacros to ignore warnings in macro expansion. Relates to #62857 (https://github.com/llvm/llvm-project/issues/62857) Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D157829 Files: clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/bugprone/lambda-function-name.rst clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp +++ clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp @@ -1,4 +1,6 @@ -// RUN: %check_clang_tidy %s bugprone-lambda-function-name %t +// RUN: %check_clang_tidy -check-suffixes=,NO-CONFIG %s bugprone-lambda-function-name %t +// RUN: %check_clang_tidy %s bugprone-lambda-function-name %t -- -config="{CheckOptions: [{key: bugprone-lambda-function-name.IgnoreMacros, value: true}]}" -- + void Foo(const char* a, const char* b, int c) {} @@ -12,11 +14,11 @@ [] { __FUNCTION__; }(); // CHECK-MESSAGES: :[[@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] [] { FUNC_MACRO; }(); - // CHECK-MESSAGES: :[[@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] + // 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] [] { FUNCTION_MACRO; }(); - // CHECK-MESSAGES: :[[@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] + // 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: :[[@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] + // 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] } #define FUNC_MACRO_WITH_FILE_AND_LINE Foo(__func__, __FILE__, __LINE__) Index: clang-tools-extra/docs/clang-tidy/checks/bugprone/lambda-function-name.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/bugprone/lambda-function-name.rst +++ clang-tools-extra/docs/clang-tidy/checks/bugprone/lambda-function-name.rst @@ -25,3 +25,11 @@ Called from FancyFunction Now called from FancyFunction + +Options +------- + +.. option:: IgnoreMacros + + The value `true` specifies that attempting to get the name of a function from + within a macro should not be diagnosed. The default value is `false`. Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -173,6 +173,10 @@ <clang-tidy/checks/bugprone/reserved-identifier>`, so that it does not warn on macros starting with underscore and lowercase letter. +- Improved :doc:`bugprone-lambda-function-name + <clang-tidy/checks/bugprone/lambda-function-name>` check by adding option + `IgnoreMacros` to ignore warnings in macros. + - Improved :doc:`cppcoreguidelines-avoid-non-const-global-variables <clang-tidy/checks/cppcoreguidelines/avoid-non-const-global-variables>` check to ignore ``static`` variables declared within the scope of Index: clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h =================================================================== --- clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h +++ clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.h @@ -13,6 +13,8 @@ namespace clang::tidy::bugprone { +inline constexpr bool DefaultIgnoreMacros = false; + /// Detect when __func__ or __FUNCTION__ is being used from within a lambda. In /// that context, those expressions expand to the name of the call operator /// (i.e., `operator()`). @@ -32,7 +34,8 @@ using SourceRangeSet = std::set<SourceRange, SourceRangeLessThan>; LambdaFunctionNameCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), + IgnoreMacros(Options.get("IgnoreMacros", DefaultIgnoreMacros)) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override; @@ -40,6 +43,7 @@ private: SourceRangeSet SuppressMacroExpansions; + bool IgnoreMacros; }; } // namespace clang::tidy::bugprone Index: clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp +++ clang-tools-extra/clang-tidy/bugprone/LambdaFunctionNameCheck.cpp @@ -78,8 +78,8 @@ if (E->getLocation().isMacroID()) { auto ER = Result.SourceManager->getImmediateExpansionRange(E->getLocation()); - if (SuppressMacroExpansions.find(ER.getAsRange()) != - SuppressMacroExpansions.end()) { + if (IgnoreMacros || SuppressMacroExpansions.find(ER.getAsRange()) != + SuppressMacroExpansions.end()) { // This is a macro expansion for which we should not warn. return; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits