[clang-tools-extra] ac616fb - [Clang-tidy] Check the existence of ElaboratedType's qualifiers

Jun Zhang via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 1 07:54:39 PST 2022


Author: Jun Zhang
Date: 2022-03-01T23:52:44+08:00
New Revision: ac616fbb05b8c0e8f85144d54d5295d2d663c5b7

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

LOG: [Clang-tidy] Check the existence of ElaboratedType's qualifiers

The ElaboratedType can have no qualifiers, so we should check it before
use.

Fix #issue53874(https://github.com/llvm/llvm-project/issues/53874)

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
    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 f2c1b0f5ec49b..0f73b1320de5a 100644
--- a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
@@ -19,14 +19,15 @@ namespace readability {
 
 static unsigned getNameSpecifierNestingLevel(const QualType &QType) {
   if (const ElaboratedType *ElType = QType->getAs<ElaboratedType>()) {
-    const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier();
-    unsigned NameSpecifierNestingLevel = 1;
-    do {
-      NameSpecifierNestingLevel++;
-      NestedSpecifiers = NestedSpecifiers->getPrefix();
-    } while (NestedSpecifiers);
-
-    return NameSpecifierNestingLevel;
+    if (const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier()) {
+      unsigned NameSpecifierNestingLevel = 1;
+      do {
+        NameSpecifierNestingLevel++;
+        NestedSpecifiers = NestedSpecifiers->getPrefix();
+      } while (NestedSpecifiers);
+
+      return NameSpecifierNestingLevel;
+    }
   }
   return 0;
 }
@@ -68,6 +69,10 @@ void StaticAccessedThroughInstanceCheck::check(
   PrintingPolicy PrintingPolicyWithSupressedTag(AstContext->getLangOpts());
   PrintingPolicyWithSupressedTag.SuppressTagKeyword = true;
   PrintingPolicyWithSupressedTag.SuppressUnwrittenScope = true;
+
+  PrintingPolicyWithSupressedTag.PrintCanonicalTypes =
+      !BaseExpr->getType()->isTypedefNameType();
+
   std::string BaseTypeName =
       BaseType.getAsString(PrintingPolicyWithSupressedTag);
 

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 cd8d198c3d47d..debf3b9222164 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
@@ -198,6 +198,28 @@ void static_through_instance() {
   h<4>();
 }
 
+struct SP {
+  static int I;
+} P;
+
+void usep() {
+  P.I;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
+  // CHECK-FIXES: {{^}}  SP::I;{{$}}
+}
+
+namespace NSP {
+struct SP {
+  static int I;
+} P;
+} // namespace NSP
+
+void usensp() {
+  NSP::P.I;
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member
+  // CHECK-FIXES: {{^}}  NSP::SP::I;{{$}}
+}
+
 // Overloaded member access operator
 struct Q {
   static int K;
@@ -237,9 +259,9 @@ void use_anonymous() {
 
 namespace Outer {
   inline namespace Inline {
-    struct S {
-      static int I;
-    };
+  struct S {
+    static int I;
+  };
   }
 }
 


        


More information about the cfe-commits mailing list