[PATCH] D33497: clang-tidy check for __func__/__FUNCTION__ in lambdas

2017-05-24 Thread Bryce Liu via Phabricator via cfe-commits
brycel created this revision.
Herald added a subscriber: mgorny.

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.


https://reviews.llvm.org/D33497

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/LambdaFunctionNameCheck.cpp
  clang-tidy/misc/LambdaFunctionNameCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-lambda-function-name.rst
  test/clang-tidy/misc-lambda-function-name.cpp

Index: test/clang-tidy/misc-lambda-function-name.cpp
===
--- test/clang-tidy/misc-lambda-function-name.cpp
+++ test/clang-tidy/misc-lambda-function-name.cpp
@@ -0,0 +1,13 @@
+// RUN: %check_clang_tidy %s misc-lambda-function-name %t
+
+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]
+}
+
+void Negatives() {
+  __func__;
+  __FUNCTION__;
+}
Index: docs/clang-tidy/checks/misc-lambda-function-name.rst
===
--- docs/clang-tidy/checks/misc-lambda-function-name.rst
+++ docs/clang-tidy/checks/misc-lambda-function-name.rst
@@ -0,0 +1,27 @@
+.. title:: clang-tidy - misc-lambda-function-name
+
+misc-lambda-function-name
+=
+
+Checks for attempts to get the name of a function from within a lambda
+expression. The name of a lambda is always something like ``operator()``, which
+is almost never what was intended.
+
+Example:
+
+.. code-block:: c++
+
+  void FancyFunction() {
+[] { printf("Called from %s\n", __func__); }();
+[] { printf("Now called from %s\n", __FUNCTION__); }();
+  }
+
+Output::
+
+  Called from operator()
+  Now called from operator()
+
+Likely intended output::
+
+  Called from FancyFunction
+  Now called from FancyFunction
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -82,6 +82,7 @@
misc-inaccurate-erase
misc-incorrect-roundings
misc-inefficient-algorithm
+   misc-lambda-function-name
misc-macro-parentheses
misc-macro-repeated-side-effects
misc-misplaced-const
Index: clang-tidy/misc/MiscTidyModule.cpp
===
--- clang-tidy/misc/MiscTidyModule.cpp
+++ clang-tidy/misc/MiscTidyModule.cpp
@@ -21,6 +21,7 @@
 #include "InaccurateEraseCheck.h"
 #include "IncorrectRoundings.h"
 #include "InefficientAlgorithmCheck.h"
+#include "LambdaFunctionNameCheck.h"
 #include "MacroParenthesesCheck.h"
 #include "MacroRepeatedSideEffectsCheck.h"
 #include "MisplacedConstCheck.h"
@@ -68,6 +69,8 @@
 "misc-assert-side-effect");
 CheckFactories.registerCheck(
 "misc-forwarding-reference-overload");
+CheckFactories.registerCheck(
+"misc-lambda-function-name");
 CheckFactories.registerCheck("misc-misplaced-const");
 CheckFactories.registerCheck(
 "misc-unconventional-assign-operator");
Index: clang-tidy/misc/LambdaFunctionNameCheck.h
===
--- clang-tidy/misc/LambdaFunctionNameCheck.h
+++ clang-tidy/misc/LambdaFunctionNameCheck.h
@@ -0,0 +1,37 @@
+//===--- LambdaFunctionNameCheck.h - clang-tidy--*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_LAMBDA_FUNCTION_NAME_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_LAMBDA_FUNCTION_NAME_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace misc {
+
+/// 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()`).
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/misc-lambda-function-name.html
+class LambdaFunctionNameCheck : public ClangTidyCheck {
+public:
+  LambdaFunctionNameCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_m

[PATCH] D33497: clang-tidy check for __func__/__FUNCTION__ in lambdas

2017-05-24 Thread Bryce Liu via Phabricator via cfe-commits
brycel added a comment.

In https://reviews.llvm.org/D33497#763307, @lebedev.ri wrote:

> 1. What about `__PRETTY_FUNCTION__` ?


`__PRETTY_FUNCTION__` is a little more useful, it at least tells you which 
function the lambda is defined in:

  #include 
  
  int main() {
auto f = [] {
  printf("__func__: %s\n", __func__);
  printf("__FUNCTION__: %s\n", __FUNCTION__);
  printf("__PRETTY_FUNCTION__: %s\n", __PRETTY_FUNCTION__);
};
f();
  }

results in the output:

  __func__: operator()
  __FUNCTION__: operator()
  __PRETTY_FUNCTION__: auto main()::(anonymous class)::operator()() const

I think `__PRETTY_FUNCTION__` inside a lambda is useful enough not to warn 
about.

> 2. Consider following generic error handling macro: (ThrowException is some 
> template function) ``` #undef STR #define STR(a) XSTR(a)
> 
>   #define ThrowExceptionHelper(CLASS, fmt, ...) 
> ThrowException(__FILE__ ":" STR(__LINE__) ": %s: " fmt, 
> __PRETTY_FUNCTION__, ##__VA_ARGS__) #endif ``` Which is called like 
> `if(somethig) ThrowException(std::exception, "%s", "bar");` Even though the 
> function name may be useless, file/line info is still there and is(?) 
> correct. Perhaps there should not be a warning in such a case?

That's a good point. I'll look into suppressing the warning if we're in a macro 
definition where `__FILE__` and `__LINE__` are both used. It may suppress some 
warnings that would be legitimate (such as if `__FILE__`/`__LINE__` are written 
but not actually output anywhere), but in those cases it's probably impossible 
to figure out the author's intention.


https://reviews.llvm.org/D33497



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D33497: [clang-tidy] check for __func__/__FUNCTION__ in lambdas

2017-05-24 Thread Bryce Liu via Phabricator via cfe-commits
brycel updated this revision to Diff 100149.
brycel added a comment.
Herald added a subscriber: xazax.hun.

Addressed the following review comments:

- Added a test to make sure we don't warn on __PRETTY_FUNCTION__
- Suppressed warnings when in a macro that also uses __FILE__ and __LINE__
- Updated docs/ReleaseNotes.rst.


https://reviews.llvm.org/D33497

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/LambdaFunctionNameCheck.cpp
  clang-tidy/misc/LambdaFunctionNameCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-lambda-function-name.rst
  test/clang-tidy/misc-lambda-function-name.cpp

Index: test/clang-tidy/misc-lambda-function-name.cpp
===
--- test/clang-tidy/misc-lambda-function-name.cpp
+++ 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; }();
+}
Index: docs/clang-tidy/checks/misc-lambda-function-name.rst
===
--- docs/clang-tidy/checks/misc-lambda-function-name.rst
+++ docs/clang-tidy/checks/misc-lambda-function-name.rst
@@ -0,0 +1,27 @@
+.. title:: clang-tidy - misc-lambda-function-name
+
+misc-lambda-function-name
+=
+
+Checks for attempts to get the name of a function from within a lambda
+expression. The name of a lambda is always something like ``operator()``, which
+is almost never what was intended.
+
+Example:
+
+.. code-block:: c++
+
+  void FancyFunction() {
+[] { printf("Called from %s\n", __func__); }();
+[] { printf("Now called from %s\n", __FUNCTION__); }();
+  }
+
+Output::
+
+  Called from operator()
+  Now called from operator()
+
+Likely intended output::
+
+  Called from FancyFunction
+  Now called from FancyFunction
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -82,6 +82,7 @@
misc-inaccurate-erase
misc-incorrect-roundings
misc-inefficient-algorithm
+   misc-lambda-function-name
misc-macro-parentheses
misc-macro-repeated-side-effects
misc-misplaced-const
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -77,6 +77,11 @@
 
   Finds perfect forwarding constructors that can unintentionally hide copy or move constructors.
 
+- New `misc-lambda-function-name
+	`_ check
+
+	Finds uses of __func__ or __FUNCTION__ inside lambdas.
+
 - N

[PATCH] D33497: [clang-tidy] check for __func__/__FUNCTION__ in lambdas

2017-05-24 Thread Bryce Liu via Phabricator via cfe-commits
brycel updated this revision to Diff 100159.
brycel marked an inline comment as done.
brycel added a comment.

Fixed ReleaseNotes.rst


https://reviews.llvm.org/D33497

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/LambdaFunctionNameCheck.cpp
  clang-tidy/misc/LambdaFunctionNameCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-lambda-function-name.rst
  test/clang-tidy/misc-lambda-function-name.cpp

Index: test/clang-tidy/misc-lambda-function-name.cpp
===
--- test/clang-tidy/misc-lambda-function-name.cpp
+++ 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; }();
+}
Index: docs/clang-tidy/checks/misc-lambda-function-name.rst
===
--- docs/clang-tidy/checks/misc-lambda-function-name.rst
+++ docs/clang-tidy/checks/misc-lambda-function-name.rst
@@ -0,0 +1,27 @@
+.. title:: clang-tidy - misc-lambda-function-name
+
+misc-lambda-function-name
+=
+
+Checks for attempts to get the name of a function from within a lambda
+expression. The name of a lambda is always something like ``operator()``, which
+is almost never what was intended.
+
+Example:
+
+.. code-block:: c++
+
+  void FancyFunction() {
+[] { printf("Called from %s\n", __func__); }();
+[] { printf("Now called from %s\n", __FUNCTION__); }();
+  }
+
+Output::
+
+  Called from operator()
+  Now called from operator()
+
+Likely intended output::
+
+  Called from FancyFunction
+  Now called from FancyFunction
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -82,6 +82,7 @@
misc-inaccurate-erase
misc-incorrect-roundings
misc-inefficient-algorithm
+   misc-lambda-function-name
misc-macro-parentheses
misc-macro-repeated-side-effects
misc-misplaced-const
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -77,6 +77,11 @@
 
   Finds perfect forwarding constructors that can unintentionally hide copy or move constructors.
 
+- New `misc-lambda-function-name
+	`_ check
+
+	Finds uses of `__func__` or `__FUNCTION__` inside lambdas.
+
 - New `modernize-replace-random-shuffle
   `_ check
 
Index: clang-tidy/misc/MiscTidyModule.cpp
===

[PATCH] D33497: [clang-tidy] check for __func__/__FUNCTION__ in lambdas

2017-05-24 Thread Bryce Liu via Phabricator via cfe-commits
brycel marked an inline comment as done.
brycel added inline comments.



Comment at: docs/ReleaseNotes.rst:83
+
+   Finds uses of __func__ or __FUNCTION__ inside lambdas.
+

Eugene.Zelenko wrote:
> Eugene.Zelenko wrote:
> > Please highlight __func__ and __FUNCTION__ with ``.
> I meant double ``, not single `.
Sorry, I had Markdown on my brain.


https://reviews.llvm.org/D33497



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D33497: [clang-tidy] check for __func__/__FUNCTION__ in lambdas

2017-05-24 Thread Bryce Liu via Phabricator via cfe-commits
brycel updated this revision to Diff 100161.
brycel added a comment.

Double backticks, not single.


https://reviews.llvm.org/D33497

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/LambdaFunctionNameCheck.cpp
  clang-tidy/misc/LambdaFunctionNameCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-lambda-function-name.rst
  test/clang-tidy/misc-lambda-function-name.cpp

Index: test/clang-tidy/misc-lambda-function-name.cpp
===
--- test/clang-tidy/misc-lambda-function-name.cpp
+++ 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; }();
+}
Index: docs/clang-tidy/checks/misc-lambda-function-name.rst
===
--- docs/clang-tidy/checks/misc-lambda-function-name.rst
+++ docs/clang-tidy/checks/misc-lambda-function-name.rst
@@ -0,0 +1,27 @@
+.. title:: clang-tidy - misc-lambda-function-name
+
+misc-lambda-function-name
+=
+
+Checks for attempts to get the name of a function from within a lambda
+expression. The name of a lambda is always something like ``operator()``, which
+is almost never what was intended.
+
+Example:
+
+.. code-block:: c++
+
+  void FancyFunction() {
+[] { printf("Called from %s\n", __func__); }();
+[] { printf("Now called from %s\n", __FUNCTION__); }();
+  }
+
+Output::
+
+  Called from operator()
+  Now called from operator()
+
+Likely intended output::
+
+  Called from FancyFunction
+  Now called from FancyFunction
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -82,6 +82,7 @@
misc-inaccurate-erase
misc-incorrect-roundings
misc-inefficient-algorithm
+   misc-lambda-function-name
misc-macro-parentheses
misc-macro-repeated-side-effects
misc-misplaced-const
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -77,6 +77,11 @@
 
   Finds perfect forwarding constructors that can unintentionally hide copy or move constructors.
 
+- New `misc-lambda-function-name
+	`_ check
+
+	Finds uses of ``__func__`` or ``__FUNCTION__`` inside lambdas.
+
 - New `modernize-replace-random-shuffle
   `_ check
 
Index: clang-tidy/misc/MiscTidyModule.cpp
=

[PATCH] D33497: [clang-tidy] check for __func__/__FUNCTION__ in lambdas

2017-06-02 Thread Bryce Liu via Phabricator via cfe-commits
brycel updated this revision to Diff 101223.
brycel marked 4 inline comments as done.
brycel added a comment.

Addressed comments from alexfh. In particular, changed to using a set from a 
vector.


https://reviews.llvm.org/D33497

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/LambdaFunctionNameCheck.cpp
  clang-tidy/misc/LambdaFunctionNameCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-lambda-function-name.rst
  test/clang-tidy/misc-lambda-function-name.cpp

Index: test/clang-tidy/misc-lambda-function-name.cpp
===
--- test/clang-tidy/misc-lambda-function-name.cpp
+++ 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; }();
+}
Index: docs/clang-tidy/checks/misc-lambda-function-name.rst
===
--- docs/clang-tidy/checks/misc-lambda-function-name.rst
+++ docs/clang-tidy/checks/misc-lambda-function-name.rst
@@ -0,0 +1,27 @@
+.. title:: clang-tidy - misc-lambda-function-name
+
+misc-lambda-function-name
+=
+
+Checks for attempts to get the name of a function from within a lambda
+expression. The name of a lambda is always something like ``operator()``, which
+is almost never what was intended.
+
+Example:
+
+.. code-block:: c++
+
+  void FancyFunction() {
+[] { printf("Called from %s\n", __func__); }();
+[] { printf("Now called from %s\n", __FUNCTION__); }();
+  }
+
+Output::
+
+  Called from operator()
+  Now called from operator()
+
+Likely intended output::
+
+  Called from FancyFunction
+  Now called from FancyFunction
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -82,6 +82,7 @@
misc-inaccurate-erase
misc-incorrect-roundings
misc-inefficient-algorithm
+   misc-lambda-function-name
misc-macro-parentheses
misc-macro-repeated-side-effects
misc-misplaced-const
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -77,6 +77,11 @@
 
   Finds perfect forwarding constructors that can unintentionally hide copy or move constructors.
 
+- New `misc-lambda-function-name
+	`_ check
+
+	Finds uses of ``__func__`` or ``__FUNCTION__`` inside lambdas.
+
 - New `modernize-replace-random-shuffle
   

[PATCH] D33497: [clang-tidy] check for __func__/__FUNCTION__ in lambdas

2017-06-02 Thread Bryce Liu via Phabricator via cfe-commits
brycel updated this revision to Diff 101224.
brycel added a comment.

Move namespace-level types to class-level to avoid potential future name 
conflicts.


https://reviews.llvm.org/D33497

Files:
  clang-tidy/misc/CMakeLists.txt
  clang-tidy/misc/LambdaFunctionNameCheck.cpp
  clang-tidy/misc/LambdaFunctionNameCheck.h
  clang-tidy/misc/MiscTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/misc-lambda-function-name.rst
  test/clang-tidy/misc-lambda-function-name.cpp

Index: test/clang-tidy/misc-lambda-function-name.cpp
===
--- test/clang-tidy/misc-lambda-function-name.cpp
+++ 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; }();
+}
Index: docs/clang-tidy/checks/misc-lambda-function-name.rst
===
--- docs/clang-tidy/checks/misc-lambda-function-name.rst
+++ docs/clang-tidy/checks/misc-lambda-function-name.rst
@@ -0,0 +1,27 @@
+.. title:: clang-tidy - misc-lambda-function-name
+
+misc-lambda-function-name
+=
+
+Checks for attempts to get the name of a function from within a lambda
+expression. The name of a lambda is always something like ``operator()``, which
+is almost never what was intended.
+
+Example:
+
+.. code-block:: c++
+
+  void FancyFunction() {
+[] { printf("Called from %s\n", __func__); }();
+[] { printf("Now called from %s\n", __FUNCTION__); }();
+  }
+
+Output::
+
+  Called from operator()
+  Now called from operator()
+
+Likely intended output::
+
+  Called from FancyFunction
+  Now called from FancyFunction
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -82,6 +82,7 @@
misc-inaccurate-erase
misc-incorrect-roundings
misc-inefficient-algorithm
+   misc-lambda-function-name
misc-macro-parentheses
misc-macro-repeated-side-effects
misc-misplaced-const
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -77,6 +77,11 @@
 
   Finds perfect forwarding constructors that can unintentionally hide copy or move constructors.
 
+- New `misc-lambda-function-name
+	`_ check
+
+	Finds uses of ``__func__`` or ``__FUNCTION__`` inside lambdas.
+
 - New `modernize-replace-random-shuffle
   `_ check
 
Index: clang-tidy/misc/M