From: Richard Smith Date: Wed, 23 Jul 2014 03:17:06 +0000 (+0000) Subject: When pretty-printing a declaration of a pack, put the ellipsis before the name X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a4bb292095482300a74170e4e297055549a38736;p=platform%2Fupstream%2Fllvm.git When pretty-printing a declaration of a pack, put the ellipsis before the name being declared, not at the end. When pretty-printing a non-type template parameter, put the name of the parameter in the middle of the type, not at the end. llvm-svn: 213718 --- diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp index e5e5130..ec7ba65 100644 --- a/clang/lib/AST/DeclPrinter.cpp +++ b/clang/lib/AST/DeclPrinter.cpp @@ -87,6 +87,7 @@ namespace { void PrintTemplateParameters(const TemplateParameterList *Params, const TemplateArgumentList *Args = nullptr); void prettyPrintAttributes(Decl *D); + void printDeclType(QualType T, StringRef DeclName, bool Pack = false); }; } @@ -197,6 +198,17 @@ void DeclPrinter::prettyPrintAttributes(Decl *D) { } } +void DeclPrinter::printDeclType(QualType T, StringRef DeclName, bool Pack) { + // Normally, a PackExpansionType is written as T[3]... (for instance, as a + // template argument), but if it is the type of a declaration, the ellipsis + // is placed before the name being declared. + if (auto *PET = T->getAs()) { + Pack = true; + T = PET->getPattern(); + } + T.print(Out, Policy, (Pack ? "..." : "") + DeclName); +} + void DeclPrinter::ProcessDeclGroup(SmallVectorImpl& Decls) { this->Indent(); Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); @@ -647,7 +659,6 @@ void DeclPrinter::VisitLabelDecl(LabelDecl *D) { Out << *D << ":"; } - void DeclPrinter::VisitVarDecl(VarDecl *D) { if (!Policy.SuppressSpecifiers) { StorageClass SC = D->getStorageClass(); @@ -675,7 +686,7 @@ void DeclPrinter::VisitVarDecl(VarDecl *D) { QualType T = D->getTypeSourceInfo() ? D->getTypeSourceInfo()->getType() : D->getASTContext().getUnqualifiedObjCPointerType(D->getType()); - T.print(Out, Policy, D->getName()); + printDeclType(T, D->getName()); Expr *Init = D->getInit(); if (!Policy.SuppressInitializers && Init) { bool ImplicitInit = false; @@ -830,7 +841,7 @@ void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params, Out << "class "; if (TTP->isParameterPack()) - Out << "... "; + Out << "..."; Out << *TTP; @@ -843,15 +854,10 @@ void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params, }; } else if (const NonTypeTemplateParmDecl *NTTP = dyn_cast(Param)) { - Out << NTTP->getType().getAsString(Policy); - - if (NTTP->isParameterPack() && !isa(NTTP->getType())) - Out << "..."; - - if (IdentifierInfo *Name = NTTP->getIdentifier()) { - Out << ' '; - Out << Name->getName(); - } + StringRef Name; + if (IdentifierInfo *II = NTTP->getIdentifier()) + Name = II->getName(); + printDeclType(NTTP->getType(), Name, NTTP->isParameterPack()); if (Args) { Out << " = "; diff --git a/clang/test/Misc/ast-dump-templates.cpp b/clang/test/Misc/ast-dump-templates.cpp index b7aeca8..022d5c4 100644 --- a/clang/test/Misc/ast-dump-templates.cpp +++ b/clang/test/Misc/ast-dump-templates.cpp @@ -39,6 +39,18 @@ void baz() { // CHECK1: template B bar() // CHECK2: template B bar() +// CHECK1-LABEL: template struct A { +// CHECK1-NEXT: template struct B { +template struct A { + template struct B {}; +}; + +// CHECK1-LABEL: template void f(T ...[3]) { +// CHECK1-NEXT: A a; +template void f(T ...[3]) { + A a; +} + namespace test2 { void func(int); void func(float); diff --git a/clang/test/SemaCXX/cxx11-ast-print.cpp b/clang/test/SemaCXX/cxx11-ast-print.cpp index f7bfc11..5604374 100644 --- a/clang/test/SemaCXX/cxx11-ast-print.cpp +++ b/clang/test/SemaCXX/cxx11-ast-print.cpp @@ -15,7 +15,7 @@ decltype(4.5_baz) operator"" _baz(char); // CHECK: const char *operator "" _quux(const char *); const char *operator"" _quux(const char *); -// CHECK: template const char *operator "" _fritz(); +// CHECK: template const char *operator "" _fritz(); template const char *operator"" _fritz(); // CHECK: const char *p1 = "bar1"_foo; diff --git a/clang/unittests/AST/DeclPrinterTest.cpp b/clang/unittests/AST/DeclPrinterTest.cpp index 5340756..9ba5979 100644 --- a/clang/unittests/AST/DeclPrinterTest.cpp +++ b/clang/unittests/AST/DeclPrinterTest.cpp @@ -544,6 +544,7 @@ TEST(DeclPrinter, TestCXXConstructorDecl10) { "};", constructorDecl(ofClass(hasName("A"))).bind("id"), "A(const A &a)")); + // WRONG; Should be: "A(const A &a);" } TEST(DeclPrinter, TestCXXConstructorDecl11) { @@ -553,8 +554,8 @@ TEST(DeclPrinter, TestCXXConstructorDecl11) { " A(T&&... ts) : T(ts)... {}" "};", constructorDecl(ofClass(hasName("A"))).bind("id"), - "A(T &&ts...) : T(ts)...")); - // WRONG; Should be: "A(T&&... ts) : T(ts)..." + "A(T &&...ts) : T(ts)...")); + // WRONG; Should be: "A(T &&...ts) : T(ts)... {}" } TEST(DeclPrinter, TestCXXDestructorDecl1) { @@ -1011,8 +1012,8 @@ TEST(DeclPrinter, TestClassTemplateDecl10) { "template" "struct A { int a; };", classTemplateDecl(hasName("A")).bind("id"), - "template struct A {\n}")); - // Should be: with semicolon, with { ... }, without spaces before '...' + "template struct A {\n}")); + // Should be: with semicolon, with { ... } } TEST(DeclPrinter, TestClassTemplateDecl11) { @@ -1020,8 +1021,8 @@ TEST(DeclPrinter, TestClassTemplateDecl11) { "template" "struct A : public T... { int a; };", classTemplateDecl(hasName("A")).bind("id"), - "template struct A : public T... {\n}")); - // Should be: with semicolon, with { ... }, without spaces before '...' + "template struct A : public T... {\n}")); + // Should be: with semicolon, with { ... } } TEST(DeclPrinter, TestClassTemplatePartialSpecializationDecl1) { @@ -1080,9 +1081,7 @@ TEST(DeclPrinter, TestFunctionTemplateDecl3) { "template" "void A(T... a);", functionTemplateDecl(hasName("A")).bind("id"), - "template void A(T a...)")); - // WRONG; Should be: "template void A(T... a)" - // (not "T a...") + "template void A(T ...a)")); // Should be: with semicolon. } @@ -1239,7 +1238,7 @@ TEST(DeclPrinter, TestTemplateArgumentList13) { "};", "A", "Z A")); - // Should be: with semicolon, without extra space in "> >" + // Should be: with semicolon } TEST(DeclPrinter, TestTemplateArgumentList14) { @@ -1251,7 +1250,7 @@ TEST(DeclPrinter, TestTemplateArgumentList14) { "};", "A", "Z...> A")); - // Should be: with semicolon, without extra space in "> >" + // Should be: with semicolon } TEST(DeclPrinter, TestTemplateArgumentList15) { @@ -1262,7 +1261,7 @@ TEST(DeclPrinter, TestTemplateArgumentList15) { "};", "A", "Z A")); - // Should be: with semicolon, without extra space in "> >" + // Should be: with semicolon } TEST(DeclPrinter, TestObjCMethod1) {