diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp --- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp +++ b/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()) { + 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().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 diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp --- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp @@ -198,6 +198,24 @@ "Derived& foo(); void fun() { foo().^a; }"); } +TEST(WalkAST, CXXDependentScopeMemberExprs) { + testWalk("template struct $explicit^Base { void method(); };", + "template void k(Base t) { t.^method(); }"); + testWalk("template struct $explicit^Base { void method(); };", + "template void k(Base& t) { t.^method(); }"); + testWalk("template struct $explicit^Base { void method(); };", + "template void k(Base* t) { t->^method(); }"); + testWalk("template struct $explicit^Base { void method(); };", + "template void k() { Base t; t.^method(); }"); + testWalk("template struct $explicit^Base { void method(); };", + "template void k() { Base* t; t->^method(); }"); + testWalk("template struct $explicit^Base { void method(); };", + "template void k() { Base().^method(); }"); + testWalk("template struct Base { void method(); }; " + "template struct $explicit^Derived: Base {};", + "template void k(Derived t) { t.^method(); }"); +} + TEST(WalkAST, ConstructExprs) { testWalk("struct $implicit^S {};", "S ^t;"); testWalk("struct S { $implicit^S(); };", "S ^t;");