[PATCH] D149948: [include-cleaner] Treat references to nested types implicit

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 23 07:41:30 PDT 2023


This revision was automatically updated to reflect the committed changes.
kadircet marked an inline comment as done.
Closed by commit rGcce7b816a1ee: [include-cleaner] Treat references to nested types implicit (authored by kadircet).

Changed prior to commit:
  https://reviews.llvm.org/D149948?vs=519794&id=524711#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149948/new/

https://reviews.llvm.org/D149948

Files:
  clang-tools-extra/include-cleaner/lib/WalkAST.cpp
  clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp


Index: clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -341,6 +341,34 @@
   testWalk("template<typename T> struct $explicit^S { S(T); };", "^S s(42);");
 }
 
+TEST(WalkAST, NestedTypes) {
+  testWalk(R"cpp(
+      struct Base { typedef int $implicit^a; };
+      struct Derived : public Base {};)cpp",
+           "void fun() { Derived::^a x; }");
+  testWalk(R"cpp(
+      struct Base { using $implicit^a = int; };
+      struct Derived : public Base {};)cpp",
+           "void fun() { Derived::^a x; }");
+  testWalk(R"cpp(
+      struct ns { struct a {}; };
+      struct Base : public ns { using ns::$implicit^a; };
+      struct Derived : public Base {};)cpp",
+           "void fun() { Derived::^a x; }");
+  testWalk(R"cpp(
+      struct Base { struct $implicit^a {}; };
+      struct Derived : public Base {};)cpp",
+           "void fun() { Derived::^a x; }");
+  testWalk("struct Base { struct $implicit^a {}; };",
+           "struct Derived : public Base { ^a x; };");
+  testWalk(R"cpp(
+      struct Base { struct $implicit^a {}; };
+      struct Derived : public Base {};
+      struct SoDerived : public Derived {};
+      )cpp",
+           "void fun() { SoDerived::Derived::^a x; }");
+}
+
 TEST(WalkAST, MemberExprs) {
   testWalk("struct $implicit^S { static int f; };", "void foo() { S::^f; }");
   testWalk("struct B { static int f; }; struct $implicit^S : B {};",
Index: clang-tools-extra/include-cleaner/lib/WalkAST.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -218,31 +218,45 @@
   }
 
   // TypeLoc visitors.
+  void reportType(SourceLocation RefLoc, NamedDecl *ND) {
+    // Reporting explicit references to types nested inside classes can cause
+    // issues, e.g. a type accessed through a derived class shouldn't require
+    // inclusion of the base.
+    // Hence we report all such references as implicit. The code must spell the
+    // outer type-location somewhere, which will trigger an explicit reference
+    // and per IWYS, it's that spelling's responsibility to bring in necessary
+    // declarations.
+    RefType RT = llvm::isa<RecordDecl>(ND->getDeclContext())
+                     ? RefType::Implicit
+                     : RefType::Explicit;
+    return report(RefLoc, ND, RT);
+  }
+
   bool VisitUsingTypeLoc(UsingTypeLoc TL) {
-    report(TL.getNameLoc(), TL.getFoundDecl());
+    reportType(TL.getNameLoc(), TL.getFoundDecl());
     return true;
   }
 
   bool VisitTagTypeLoc(TagTypeLoc TTL) {
-    report(TTL.getNameLoc(), TTL.getDecl());
+    reportType(TTL.getNameLoc(), TTL.getDecl());
     return true;
   }
 
   bool VisitTypedefTypeLoc(TypedefTypeLoc TTL) {
-    report(TTL.getNameLoc(), TTL.getTypedefNameDecl());
+    reportType(TTL.getNameLoc(), TTL.getTypedefNameDecl());
     return true;
   }
 
   bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
-    report(TL.getTemplateNameLoc(),
-           getMostRelevantTemplatePattern(TL.getTypePtr()));
+    reportType(TL.getTemplateNameLoc(),
+               getMostRelevantTemplatePattern(TL.getTypePtr()));
     return true;
   }
 
   bool VisitDeducedTemplateSpecializationTypeLoc(
       DeducedTemplateSpecializationTypeLoc TL) {
-    report(TL.getTemplateNameLoc(),
-           getMostRelevantTemplatePattern(TL.getTypePtr()));
+    reportType(TL.getTemplateNameLoc(),
+               getMostRelevantTemplatePattern(TL.getTypePtr()));
     return true;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149948.524711.patch
Type: text/x-patch
Size: 3756 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230523/c2afb27c/attachment.bin>


More information about the cfe-commits mailing list