xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/bugprone/lambda-function-name.cpp (revision 7fcca112034f55c26e943c13fd0d57adfbe96705)
1 // RUN: %check_clang_tidy -check-suffixes=,NO-CONFIG %s bugprone-lambda-function-name %t
2 // RUN: %check_clang_tidy %s bugprone-lambda-function-name %t -- -config="{CheckOptions: [{key: bugprone-lambda-function-name.IgnoreMacros, value: true}]}" --
3 
4 
Foo(const char * a,const char * b,int c)5 void Foo(const char* a, const char* b, int c) {}
6 
7 #define FUNC_MACRO Foo(__func__, "", 0)
8 #define FUNCTION_MACRO Foo(__FUNCTION__, "", 0)
9 #define EMBED_IN_ANOTHER_MACRO1 FUNC_MACRO
10 
Positives()11 void Positives() {
12   [] { __func__; }();
13   // 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]
14   [] { __FUNCTION__; }();
15   // 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]
16   [] { FUNC_MACRO; }();
17   // 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]
18   [] { FUNCTION_MACRO; }();
19   // 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]
20   [] { EMBED_IN_ANOTHER_MACRO1; }();
21   // 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]
22   [] {
23     __func__;
24   // 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]
25     struct S {
26       void f() {
27         __func__;
28         [] {
29           __func__;
30   // 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]
31         }();
32         __func__;
33       }
34     };
35     __func__;
36   // 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]
37   }();
38 }
39 
40 #define FUNC_MACRO_WITH_FILE_AND_LINE Foo(__func__, __FILE__, __LINE__)
41 #define FUNCTION_MACRO_WITH_FILE_AND_LINE Foo(__FUNCTION__, __FILE__, __LINE__)
42 #define EMBED_IN_ANOTHER_MACRO2 FUNC_MACRO_WITH_FILE_AND_LINE
43 
Negatives()44 void Negatives() {
45   __func__;
46   __FUNCTION__;
47 
48   // __PRETTY_FUNCTION__ should not trigger a warning because its value is
49   // actually potentially useful.
50   __PRETTY_FUNCTION__;
51   [] { __PRETTY_FUNCTION__; }();
52 
53   // Don't warn if __func__/__FUNCTION is used inside a macro that also uses
54   // __FILE__ and __LINE__, on the assumption that __FILE__ and __LINE__ will
55   // be useful even if __func__/__FUNCTION__ is not.
56   [] { FUNC_MACRO_WITH_FILE_AND_LINE; }();
57   [] { FUNCTION_MACRO_WITH_FILE_AND_LINE; }();
58   [] { EMBED_IN_ANOTHER_MACRO2; }();
59 
60   [] (const char* func = __func__) { func; }();
61   [func=__func__] { func; }();
62 }
63