Author: Sam McCall Date: 2019-11-15T10:40:38+01:00 New Revision: 87054ec07bd57719b9f66bf0548d7ac8019799f5
URL: https://github.com/llvm/llvm-project/commit/87054ec07bd57719b9f66bf0548d7ac8019799f5 DIFF: https://github.com/llvm/llvm-project/commit/87054ec07bd57719b9f66bf0548d7ac8019799f5.diff LOG: [AST] Use written names instead of `type-param-0-0` in more cases when printing types and decls. Summary: This doesn't cover decls in diagnostics, which use NamedDecl::getNameForDiagnostic(). (That should also be fixed later I think). This covers some cases of https://github.com/clangd/clangd/issues/76 (hover, but not outline or sighelp) Reviewers: hokein Subscribers: ilya-biryukov, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D70236 Added: Modified: clang-tools-extra/clangd/unittests/FindTargetTests.cpp clang-tools-extra/clangd/unittests/XRefsTests.cpp clang/lib/AST/DeclPrinter.cpp clang/test/Index/print-display-names.cpp clang/unittests/AST/DeclPrinterTest.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp index 8f7c7aaeaefe..f6e5fe723ec7 100644 --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -288,8 +288,7 @@ TEST_F(TargetDeclTest, ClassTemplate) { )cpp"; EXPECT_DECLS("TemplateSpecializationTypeLoc", {"template<> class Foo<int *>", Rel::TemplateInstantiation}, - {"template <typename T> class Foo<type-parameter-0-0 *>", - Rel::TemplatePattern}); + {"template <typename T> class Foo<T *>", Rel::TemplatePattern}); } TEST_F(TargetDeclTest, FunctionTemplate) { diff --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp index c20ac2936a42..0de112735566 100644 --- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp +++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp @@ -897,6 +897,17 @@ void foo())cpp"; HI.Definition = "int test"; HI.Type = "int"; }}, + // Partially-specialized class decl. (formerly type-parameter-0-0) + {R"cpp( + template <typename T> class X; + template <typename T> class [[^X]]<T*> {}; + )cpp", + [](HoverInfo &HI) { + HI.Name = "X<T *>"; + HI.NamespaceScope = ""; + HI.Kind = SymbolKind::Class; + HI.Definition = "template <typename T> class X<T *> {}"; + }}, // auto on lambda {R"cpp( diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp index 608b0b44072b..2c0077eca9d1 100644 --- a/clang/lib/AST/DeclPrinter.cpp +++ b/clang/lib/AST/DeclPrinter.cpp @@ -108,8 +108,8 @@ namespace { void printTemplateParameters(const TemplateParameterList *Params, bool OmitTemplateKW = false); - void printTemplateArguments(const TemplateArgumentList &Args, - const TemplateParameterList *Params = nullptr); + void printTemplateArguments(llvm::ArrayRef<TemplateArgument> Args); + void printTemplateArguments(llvm::ArrayRef<TemplateArgumentLoc> Args); void prettyPrintAttributes(Decl *D); void prettyPrintPragmas(Decl *D); void printDeclType(QualType T, StringRef DeclName, bool Pack = false); @@ -636,10 +636,15 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { if (GuideDecl) Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString(); - if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) { + if (D->isFunctionTemplateSpecialization()) { llvm::raw_string_ostream POut(Proto); DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation); - TArgPrinter.printTemplateArguments(*TArgs); + const auto *TArgAsWritten = D->getTemplateSpecializationArgsAsWritten(); + if (TArgAsWritten && !Policy.PrintCanonicalTypes) + TArgPrinter.printTemplateArguments(TArgAsWritten->arguments()); + else if (const TemplateArgumentList *TArgs = + D->getTemplateSpecializationArgs()) + TArgPrinter.printTemplateArguments(TArgs->asArray()); } QualType Ty = D->getType(); @@ -957,10 +962,15 @@ void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { if (D->getIdentifier()) { Out << ' ' << *D; - if (auto S = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) - printTemplateArguments(S->getTemplateArgs(), S->getTemplateParameters()); - else if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) - printTemplateArguments(S->getTemplateArgs()); + if (auto S = dyn_cast<ClassTemplateSpecializationDecl>(D)) { + ArrayRef<TemplateArgument> Args = S->getTemplateArgs().asArray(); + if (!Policy.PrintCanonicalTypes) + if (const auto* TSI = S->getTypeAsWritten()) + if (const auto *TST = + dyn_cast<TemplateSpecializationType>(TSI->getType())) + Args = TST->template_arguments(); + printTemplateArguments(Args); + } } if (D->isCompleteDefinition()) { @@ -1083,40 +1093,22 @@ void DeclPrinter::printTemplateParameters(const TemplateParameterList *Params, Out << ' '; } -void DeclPrinter::printTemplateArguments(const TemplateArgumentList &Args, - const TemplateParameterList *Params) { +void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgument> Args) { Out << "<"; for (size_t I = 0, E = Args.size(); I < E; ++I) { - const TemplateArgument &A = Args[I]; if (I) Out << ", "; - if (Params) { - if (A.getKind() == TemplateArgument::Type) - if (auto T = A.getAsType()->getAs<TemplateTypeParmType>()) { - auto P = cast<TemplateTypeParmDecl>(Params->getParam(T->getIndex())); - Out << *P; - continue; - } - if (A.getKind() == TemplateArgument::Template) { - if (auto T = A.getAsTemplate().getAsTemplateDecl()) - if (auto TD = dyn_cast<TemplateTemplateParmDecl>(T)) { - auto P = cast<TemplateTemplateParmDecl>( - Params->getParam(TD->getIndex())); - Out << *P; - continue; - } - } - if (A.getKind() == TemplateArgument::Expression) { - if (auto E = dyn_cast<DeclRefExpr>(A.getAsExpr())) - if (auto N = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { - auto P = cast<NonTypeTemplateParmDecl>( - Params->getParam(N->getIndex())); - Out << *P; - continue; - } - } - } - A.print(Policy, Out); + Args[I].print(Policy, Out); + } + Out << ">"; +} + +void DeclPrinter::printTemplateArguments(ArrayRef<TemplateArgumentLoc> Args) { + Out << "<"; + for (size_t I = 0, E = Args.size(); I < E; ++I) { + if (I) + Out << ", "; + Args[I].getArgument().print(Policy, Out); } Out << ">"; } diff --git a/clang/test/Index/print-display-names.cpp b/clang/test/Index/print-display-names.cpp index 958948df5386..ff221e1907c0 100644 --- a/clang/test/Index/print-display-names.cpp +++ b/clang/test/Index/print-display-names.cpp @@ -22,7 +22,7 @@ template<> void g<int>(ClassTmpl<int, int>); // RUN: env CINDEXTEST_PRINTINGPOLICY_TERSEOUTPUT=1 c-index-test -test-load-source all-pretty %s | FileCheck %s --check-prefix=PRETTY // PRETTY: print-display-names.cpp:2:7: ClassTemplate=template <typename T, typename> class ClassTmpl {}:2:7 (Definition) Extent=[1:1 - 2:20] // PRETTY: print-display-names.cpp:4:13: TypedefDecl=typedef int Integer:4:13 (Definition) Extent=[4:1 - 4:20] -// PRETTY: print-display-names.cpp:6:16: ClassDecl=template<> class ClassTmpl<int, int> {}:6:16 (Definition) [Specialization of ClassTmpl:2:7] Extent=[6:1 - 6:43] +// PRETTY: print-display-names.cpp:6:16: ClassDecl=template<> class ClassTmpl<Integer, Integer> {}:6:16 (Definition) [Specialization of ClassTmpl:2:7] Extent=[6:1 - 6:43] // PRETTY: print-display-names.cpp:8:6: FunctionDecl=void f(ClassTmpl<float, Integer> p):8:6 Extent=[8:1 - 8:36] // PRETTY: print-display-names.cpp:8:34: ParmDecl=ClassTmpl<float, Integer> p:8:34 (Definition) Extent=[8:8 - 8:35] // PRETTY: print-display-names.cpp:11:6: FunctionTemplate=template <typename T> void g(ClassTmpl<T, T>):11:6 Extent=[10:1 - 11:24] diff --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp index c003e361ef27..5a0a80421d04 100644 --- a/clang/unittests/AST/DeclPrinterTest.cpp +++ b/clang/unittests/AST/DeclPrinterTest.cpp @@ -1067,8 +1067,7 @@ TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl2) { "template<typename T>" "struct A<T *> { T a; };", classTemplateSpecializationDecl().bind("id"), - "template <typename T> struct A<type-parameter-0-0 *> {}")); - // WRONG; Should be: "template<typename T> struct A<T *> { ... }" + "template <typename T> struct A<T *> {}")); } TEST(DeclPrinter, TestClassTemplateSpecializationDecl1) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits