hokein created this revision. hokein added a reviewer: sammccall. Herald added a subscriber: kadircet. Herald added a project: All. hokein requested review of this revision. Herald added projects: clang, clang-tools-extra.
Fixes https://github.com/llvm/llvm-project/issues/59533 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D140095 Files: clang-tools-extra/include-cleaner/lib/WalkAST.cpp clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp clang/include/clang/AST/Type.h clang/lib/AST/Type.cpp
Index: clang/lib/AST/Type.cpp =================================================================== --- clang/lib/AST/Type.cpp +++ clang/lib/AST/Type.cpp @@ -524,6 +524,10 @@ return getAsSugar<TypedefType>(this); } +template <> const UsingType *Type::getAs() const { + return getAsSugar<UsingType>(this); +} + template <> const TemplateSpecializationType *Type::getAs() const { return getAsSugar<TemplateSpecializationType>(this); } Index: clang/include/clang/AST/Type.h =================================================================== --- clang/include/clang/AST/Type.h +++ clang/include/clang/AST/Type.h @@ -2592,6 +2592,7 @@ /// This will check for a TypedefType by removing any existing sugar /// until it reaches a TypedefType or a non-sugared type. template <> const TypedefType *Type::getAs() const; +template <> const UsingType *Type::getAs() const; /// This will check for a TemplateSpecializationType by removing any /// existing sugar until it reaches a TemplateSpecializationType or a 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 @@ -210,6 +210,19 @@ }; struct Foo {};)cpp", "void test(unique_ptr<Foo> &V) { V.^release(); }"); + // Respect the sugar type (typedef, using-type). + testWalk(R"cpp( + namespace ns { struct Foo { int a; }; } + using $explicit^Bar = ns::Foo;)cpp", + "void test(Bar b) { b.^a; }"); + testWalk(R"cpp( + namespace ns { struct Foo { int a; }; } + using ns::$explicit^Foo;)cpp", + "void test(Foo b) { b.^a; }"); + testWalk(R"cpp( + namespace ns { template<typename> struct Foo { int a; }; } + using ns::$explicit^Foo;)cpp", + "void k(Foo<int> b) { b.^a; }"); } TEST(WalkAST, ConstructExprs) { 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 @@ -27,16 +27,6 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> { DeclCallback Callback; - bool handleTemplateName(SourceLocation Loc, TemplateName TN) { - // For using-templates, only mark the alias. - if (auto *USD = TN.getAsUsingShadowDecl()) { - report(Loc, USD); - return true; - } - report(Loc, TN.getAsTemplateDecl()); - return true; - } - void report(SourceLocation Loc, NamedDecl *ND, RefType RT = RefType::Explicit) { if (!ND || Loc.isInvalid()) @@ -44,9 +34,22 @@ Callback(Loc, *cast<NamedDecl>(ND->getCanonicalDecl()), RT); } + NamedDecl *resolveTemplateName(TemplateName TN) { + // For using-templates, only mark the alias. + if (auto *USD = TN.getAsUsingShadowDecl()) + return USD; + return TN.getAsTemplateDecl(); + } NamedDecl *resolveType(QualType Type) { if (Type->isPointerType()) Type = Type->getPointeeType(); + if (const auto *TT = Type->getAs<TypedefType>()) + return TT->getDecl(); + if (const auto *UT = Type->getAs<UsingType>()) + return UT->getFoundDecl(); + if (const auto *TST = Type->getAs<TemplateSpecializationType>()) + return resolveTemplateName(TST->getTemplateName()); + return Type->getAsRecordDecl(); } @@ -126,15 +129,17 @@ bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { // FIXME: Handle explicit specializations. - return handleTemplateName(TL.getTemplateNameLoc(), - TL.getTypePtr()->getTemplateName()); + report(TL.getTemplateNameLoc(), + resolveTemplateName(TL.getTypePtr()->getTemplateName())); + return true; } bool VisitDeducedTemplateSpecializationTypeLoc( DeducedTemplateSpecializationTypeLoc TL) { // FIXME: Handle specializations. - return handleTemplateName(TL.getTemplateNameLoc(), - TL.getTypePtr()->getTemplateName()); + report(TL.getTemplateNameLoc(), + resolveTemplateName(TL.getTypePtr()->getTemplateName())); + return true; } bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &TL) { @@ -142,9 +147,11 @@ // Template-template parameters require special attention, as there's no // TemplateNameLoc. if (Arg.getKind() == TemplateArgument::Template || - Arg.getKind() == TemplateArgument::TemplateExpansion) - return handleTemplateName(TL.getLocation(), - Arg.getAsTemplateOrTemplatePattern()); + Arg.getKind() == TemplateArgument::TemplateExpansion) { + report(TL.getLocation(), + resolveTemplateName(Arg.getAsTemplateOrTemplatePattern())); + return true; + } return RecursiveASTVisitor::TraverseTemplateArgumentLoc(TL); } };
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits