[clang-tools-extra] 3afe3db - [clang-tidy] Fix readability-static-accessed-through-instance check for anonymous structs
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 4 00:25:40 PDT 2023
Author: AMS21
Date: 2023-04-04T07:20:25Z
New Revision: 3afe3dbfa0157608aa1d058f6be28e0060aaf9c6
URL: https://github.com/llvm/llvm-project/commit/3afe3dbfa0157608aa1d058f6be28e0060aaf9c6
DIFF: https://github.com/llvm/llvm-project/commit/3afe3dbfa0157608aa1d058f6be28e0060aaf9c6.diff
LOG: [clang-tidy] Fix readability-static-accessed-through-instance check for anonymous structs
Previously we would provide a fixit which looked like
this `unnamed struct at ...::f()` but which is obviously
not valid C/C++.
Since there is no real nice way to accesses a static function
from an anonymous struct anyways we simply ignore all
anonymous structs.
Fixes llvm#61736
Reviewed By: PiotrZSL
Differential Revision: https://reviews.llvm.org/D147411
Added:
Modified:
clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
index 4bb14850d602..619374cddbcf 100644
--- a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
@@ -59,7 +59,7 @@ void StaticAccessedThroughInstanceCheck::check(
if (isa<CXXOperatorCallExpr>(BaseExpr))
return;
- QualType BaseType =
+ const QualType BaseType =
BaseExpr->getType()->isPointerType()
? BaseExpr->getType()->getPointeeType().getUnqualifiedType()
: BaseExpr->getType().getUnqualifiedType();
@@ -75,6 +75,11 @@ void StaticAccessedThroughInstanceCheck::check(
std::string BaseTypeName =
BaseType.getAsString(PrintingPolicyWithSuppressedTag);
+ // Ignore anonymous structs/classes which will not have an identifier
+ const RecordDecl *RecDecl = BaseType->getAsCXXRecordDecl();
+ if (!RecDecl || RecDecl->getIdentifier() == nullptr)
+ return;
+
// Do not warn for CUDA built-in variables.
if (StringRef(BaseTypeName).startswith("__cuda_builtin_"))
return;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 13a06efcf563..573609be539a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -255,8 +255,9 @@ Changes in existing checks
be unnecessarily emitted for template dependent ``if constexpr``.
- Improved :doc:`readability-static-accessed-through-instance
- <clang-tidy/checks/readability/static-accessed-through-instance>` check to
- support unscoped enumerations through instances.
+ <clang-tidy/checks/readability/static-accessed-through-instance>` check to
+ support unscoped enumerations through instances and fixed usage of anonymous
+ structs or classes.
- Fixed a false positive in :doc:`cppcoreguidelines-slicing
<clang-tidy/checks/cppcoreguidelines/slicing>` check when warning would be
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp
index 6d4a03421913..9e8930084a65 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp
@@ -306,3 +306,60 @@ unsigned int x4 = gridDim.x;
// CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member
} // namespace Bugzilla_48758
+
+// https://github.com/llvm/llvm-project/issues/61736
+namespace llvm_issue_61736
+{
+
+struct {
+ static void f() {}
+} AnonStruct, *AnonStructPointer;
+
+class {
+ public:
+ static void f() {}
+} AnonClass, *AnonClassPointer;
+
+void testAnonymousStructAndClass() {
+ AnonStruct.f();
+ AnonStructPointer->f();
+
+ AnonClass.f();
+ AnonClassPointer->f();
+}
+
+struct Embedded {
+ struct {
+ static void f() {}
+ } static EmbeddedStruct, *EmbeddedStructPointer;
+
+ class {
+ public:
+ static void f() {}
+ } static EmbeddedClass, *EmbeddedClassPointer;
+};
+
+void testEmbeddedAnonymousStructAndClass() {
+ Embedded::EmbeddedStruct.f();
+ Embedded::EmbeddedStructPointer->f();
+
+ Embedded::EmbeddedClass.f();
+ Embedded::EmbeddedClassPointer->f();
+
+ Embedded E;
+ E.EmbeddedStruct.f();
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member accessed through instance [readability-static-accessed-through-instance]
+ // CHECK-FIXES: {{^}} llvm_issue_61736::Embedded::EmbeddedStruct.f();{{$}}
+ E.EmbeddedStructPointer->f();
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member accessed through instance [readability-static-accessed-through-instance]
+ // CHECK-FIXES: {{^}} llvm_issue_61736::Embedded::EmbeddedStructPointer->f();{{$}}
+
+ E.EmbeddedClass.f();
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member accessed through instance [readability-static-accessed-through-instance]
+ // CHECK-FIXES: {{^}} llvm_issue_61736::Embedded::EmbeddedClass.f();{{$}}
+ E.EmbeddedClassPointer->f();
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member accessed through instance [readability-static-accessed-through-instance]
+ // CHECK-FIXES: {{^}} llvm_issue_61736::Embedded::EmbeddedClassPointer->f();{{$}}
+}
+
+} // namespace llvm_issue_61736
More information about the cfe-commits
mailing list