From b36a2e7828befcf948f461b72c78a8d2386db2e7 Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Tue, 9 May 2023 11:30:07 +0200 Subject: [PATCH] [tidy][IdentifierNaming] Fix crashes on non-identifiers Differential Revision: https://reviews.llvm.org/D150187 --- clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp | 10 +++++++--- .../checkers/readability/identifier-naming-bugfix.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp index 5347dad..ee400d8 100644 --- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp +++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp @@ -448,14 +448,18 @@ void RenamerClangTidyCheck::addUsage( void RenamerClangTidyCheck::addUsage(const NamedDecl *Decl, SourceRange Range, const SourceManager *SourceMgr) { + // Don't keep track for non-identifier names. + auto *II = Decl->getIdentifier(); + if (!II) + return; if (const auto *Method = dyn_cast(Decl)) { if (const CXXMethodDecl *Overridden = getOverrideMethod(Method)) Decl = Overridden; } Decl = cast(Decl->getCanonicalDecl()); - return addUsage(RenamerClangTidyCheck::NamingCheckId(Decl->getLocation(), - Decl->getName()), - Range, SourceMgr); + return addUsage( + RenamerClangTidyCheck::NamingCheckId(Decl->getLocation(), II->getName()), + Range, SourceMgr); } void RenamerClangTidyCheck::checkNamedDecl(const NamedDecl *Decl, diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-bugfix.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-bugfix.cpp index 9f892eb..25f2c71 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-bugfix.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-bugfix.cpp @@ -3,3 +3,14 @@ // This used to cause a null pointer dereference. auto [left] = right; // CHECK-MESSAGES: :[[@LINE-1]]:15: error: use of undeclared identifier 'right' + +namespace crash_on_nonidentifiers { +struct Foo { + operator bool(); +}; +void foo() { + // Make sure we don't crash on non-identifier names (e.g. conversion + // operators). + if (Foo()) {} +} +} -- 2.7.4