From d9a4491b0a05a70733a80622d29a49d79210622a Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Fri, 2 Jun 2017 17:36:32 +0000 Subject: [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 --- .../test/clang-tidy/misc-lambda-function-name.cpp | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 clang-tools-extra/test/clang-tidy/misc-lambda-function-name.cpp (limited to 'clang-tools-extra/test/clang-tidy/misc-lambda-function-name.cpp') 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; }(); +} -- cgit v1.2.3