diff options
| author | Alexander Kornienko <alexfh@google.com> | 2017-06-02 17:36:32 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2017-06-02 17:36:32 +0000 |
| commit | d9a4491b0a05a70733a80622d29a49d79210622a (patch) | |
| tree | f4cb0a552e8a0096874142cf045bc67ee1bf07cb /clang-tools-extra/test/clang-tidy/misc-lambda-function-name.cpp | |
| parent | cdb5dad4cc4039e357c0437d9789b0353a989565 (diff) | |
| download | bcm5719-llvm-d9a4491b0a05a70733a80622d29a49d79210622a.tar.gz bcm5719-llvm-d9a4491b0a05a70733a80622d29a49d79210622a.zip | |
[clang-tidy] check for __func__/__FUNCTION__ in lambdas
Add a clang-tidy check for using func__/FUNCTION__ inside lambdas. This
evaluates to the string operator(), which is almost never useful and almost
certainly not what the author intended.
Patch by Bryce Liu!
Differential Revision: https://reviews.llvm.org/D33497
llvm-svn: 304570
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/misc-lambda-function-name.cpp')
| -rw-r--r-- | clang-tools-extra/test/clang-tidy/misc-lambda-function-name.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/misc-lambda-function-name.cpp b/clang-tools-extra/test/clang-tidy/misc-lambda-function-name.cpp new file mode 100644 index 00000000000..cc5aa2b5a0b --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/misc-lambda-function-name.cpp @@ -0,0 +1,41 @@ +// RUN: %check_clang_tidy %s misc-lambda-function-name %t + +void Foo(const char* a, const char* b, int c) {} + +#define FUNC_MACRO Foo(__func__, "", 0) +#define FUNCTION_MACRO Foo(__FUNCTION__, "", 0) +#define EMBED_IN_ANOTHER_MACRO1 FUNC_MACRO + +void Positives() { + [] { __func__; }(); + // 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 [misc-lambda-function-name] + [] { __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 [misc-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 [misc-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 [misc-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 [misc-lambda-function-name] +} + +#define FUNC_MACRO_WITH_FILE_AND_LINE Foo(__func__, __FILE__, __LINE__) +#define FUNCTION_MACRO_WITH_FILE_AND_LINE Foo(__FUNCTION__, __FILE__, __LINE__) +#define EMBED_IN_ANOTHER_MACRO2 FUNC_MACRO_WITH_FILE_AND_LINE + +void Negatives() { + __func__; + __FUNCTION__; + + // __PRETTY_FUNCTION__ should not trigger a warning because its value is + // actually potentially useful. + __PRETTY_FUNCTION__; + [] { __PRETTY_FUNCTION__; }(); + + // Don't warn if __func__/__FUNCTION is used inside a macro that also uses + // __FILE__ and __LINE__, on the assumption that __FILE__ and __LINE__ will + // be useful even if __func__/__FUNCTION__ is not. + [] { FUNC_MACRO_WITH_FILE_AND_LINE; }(); + [] { FUNCTION_MACRO_WITH_FILE_AND_LINE; }(); + [] { EMBED_IN_ANOTHER_MACRO2; }(); +} |

