[PATCH] D150187: [tidy][IdentifierNaming] Fix crashes on non-identifiers

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 9 02:30:35 PDT 2023


kadircet created this revision.
kadircet added a reviewer: VitaNuo.
Herald added subscribers: PiotrZSL, carlosgalvezp.
Herald added a reviewer: njames93.
Herald added a project: All.
kadircet requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150187

Files:
  clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-nocrash.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-nocrash.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-nocrash.cpp
@@ -0,0 +1,11 @@
+// RUN: %check_clang_tidy %s readability-identifier-naming %t
+
+struct Foo {
+  operator bool();
+};
+
+void foo() {
+  // Make sure we don't crash on non-identifier names (e.g. conversion
+  // operators).
+  if (Foo()) {}
+}
Index: clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
+++ clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp
@@ -453,9 +453,12 @@
       Decl = Overridden;
   }
   Decl = cast<NamedDecl>(Decl->getCanonicalDecl());
-  return addUsage(RenamerClangTidyCheck::NamingCheckId(Decl->getLocation(),
-                                                       Decl->getName()),
-                  Range, SourceMgr);
+  // Don't keep track for non-identifier names.
+  if (auto *II = Decl->getIdentifier()) {
+    return addUsage(RenamerClangTidyCheck::NamingCheckId(Decl->getLocation(),
+                                                         II->getName()),
+                    Range, SourceMgr);
+  }
 }
 
 void RenamerClangTidyCheck::checkNamedDecl(const NamedDecl *Decl,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150187.520633.patch
Type: text/x-patch
Size: 1431 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230509/ea4a0466/attachment.bin>


More information about the cfe-commits mailing list