[PATCH] D139409: [include-cleaner] Handle dependent type members in AST

Viktoriia Bakalova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 9 08:01:17 PST 2022


VitaNuo updated this revision to Diff 481659.
VitaNuo marked 3 inline comments as done.
VitaNuo added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D139409

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
@@ -198,6 +198,24 @@
            "Derived& foo(); void fun() { foo().^a; }");
 }
 
+TEST(WalkAST, CXXDependentScopeMemberExprs) {
+  testWalk("template<typename T> struct $explicit^Base { void method(); };",
+           "template<typename T> void k(Base<T> t) { t.^method(); }");
+  testWalk("template<typename T> struct $explicit^Base { void method(); };",
+           "template<typename T> void k(Base<T>& t) { t.^method(); }");
+  testWalk("template<typename T> struct $explicit^Base { void method(); };",
+           "template<typename T> void k(Base<T>* t) { t->^method(); }");
+  testWalk("template<typename T> struct $explicit^Base { void method(); };",
+           "template<typename T> void k() { Base<T> t; t.^method(); }");
+  testWalk("template<typename T> struct $explicit^Base { void method(); };",
+           "template<typename T> void k() { Base<T>* t; t->^method(); }");
+  testWalk("template<typename T> struct $explicit^Base { void method(); };",
+           "template<typename T> void k() { Base<T>().^method(); }");
+  testWalk("template<typename T> struct Base { void method(); }; "
+           "template<typename T> struct $explicit^Derived: Base<T> {};",
+           "template<typename T> void k(Derived<T> t) { t.^method(); }");
+}
+
 TEST(WalkAST, ConstructExprs) {
   testWalk("struct $implicit^S {};", "S ^t;");
   testWalk("struct S { $implicit^S(); };", "S ^t;");
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
@@ -11,6 +11,7 @@
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/ExprCXX.h"
+#include "clang/AST/NestedNameSpecifier.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/AST/TemplateName.h"
 #include "clang/AST/Type.h"
@@ -47,6 +48,10 @@
   NamedDecl *resolveType(QualType Type) {
     if (Type->isPointerType())
       Type = Type->getPointeeType();
+    if (const TemplateSpecializationType *TST =
+            Type->getAs<TemplateSpecializationType>()) {
+      return TST->getTemplateName().getAsTemplateDecl();
+    }
     return Type->getAsRecordDecl();
   }
 
@@ -62,12 +67,16 @@
     // A member expr implies a usage of the class type
     // (e.g., to prevent inserting a header of base class when using base
     // members from a derived object).
-    // FIXME: support dependent types, e.g., "std::vector<T>().size()".
     QualType Type = E->getBase()->IgnoreImpCasts()->getType();
     report(E->getMemberLoc(), resolveType(Type));
     return true;
   }
 
+  bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
+    report(E->getMemberLoc(), resolveType(E->getBaseType()));
+    return true;
+  }
+
   bool VisitCXXConstructExpr(CXXConstructExpr *E) {
     report(E->getLocation(), E->getConstructor(),
            E->getParenOrBraceRange().isValid() ? RefType::Explicit


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139409.481659.patch
Type: text/x-patch
Size: 3243 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221209/1e9d718e/attachment.bin>


More information about the cfe-commits mailing list