[clang-tools-extra] 35bee35 - [clang-tidy] prevent generated checks from triggering assertions on anonymous functions

David Truby via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 12 04:44:49 PDT 2020


Author: Bogdan Serea
Date: 2020-08-12T12:43:40+01:00
New Revision: 35bee3503f4c33d92434a314e49e3e6f4f7419bc

URL: https://github.com/llvm/llvm-project/commit/35bee3503f4c33d92434a314e49e3e6f4f7419bc
DIFF: https://github.com/llvm/llvm-project/commit/35bee3503f4c33d92434a314e49e3e6f4f7419bc.diff

LOG: [clang-tidy] prevent generated checks from triggering assertions on anonymous functions

Skeleton checks generated by clang-tidy add_check.py cause assertions to fail when run over anonymous functions(lambda functions). This patch introduces an additional check to verify that the target function is not anonymous before calling getName().
The code snippet from the [[ https://clang.llvm.org/extra/clang-tidy/Contributing.html | clang-tidy tutorial  ]]is also updated.

Reviewed By: alexfh, DavidTruby

Differential Revision: https://reviews.llvm.org/D85218

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/add_new_check.py
    clang-tools-extra/docs/clang-tidy/Contributing.rst

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py
index 231f43c0b8f3..14fcfe8d49ff 100755
--- a/clang-tools-extra/clang-tidy/add_new_check.py
+++ b/clang-tools-extra/clang-tidy/add_new_check.py
@@ -136,7 +136,7 @@ def write_implementation(module_path, module, namespace, check_name_camel):
 void %(check_name)s::check(const MatchFinder::MatchResult &Result) {
   // FIXME: Add callback implementation.
   const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
-  if (MatchedDecl->getName().startswith("awesome_"))
+  if (!MatchedDecl->getIdentifier() || MatchedDecl->getName().startswith("awesome_"))
     return;
   diag(MatchedDecl->getLocation(), "function %%0 is insufficiently awesome")
       << MatchedDecl;

diff  --git a/clang-tools-extra/docs/clang-tidy/Contributing.rst b/clang-tools-extra/docs/clang-tidy/Contributing.rst
index 3ed6dadb5e8b..6b7af479804d 100644
--- a/clang-tools-extra/docs/clang-tidy/Contributing.rst
+++ b/clang-tools-extra/docs/clang-tidy/Contributing.rst
@@ -202,7 +202,7 @@ can further inspect them and report diagnostics.
 
   void AwesomeFunctionNamesCheck::check(const MatchFinder::MatchResult &Result) {
     const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
-    if (MatchedDecl->getName().startswith("awesome_"))
+    if (!MatchedDecl->getIdentifier() || MatchedDecl->getName().startswith("awesome_"))
       return;
     diag(MatchedDecl->getLocation(), "function %0 is insufficiently awesome")
         << MatchedDecl


        


More information about the cfe-commits mailing list