diff options
Diffstat (limited to 'lib/AST/DeclPrinter.cpp')
-rw-r--r-- | lib/AST/DeclPrinter.cpp | 57 |
1 files changed, 40 insertions, 17 deletions
diff --git a/lib/AST/DeclPrinter.cpp b/lib/AST/DeclPrinter.cpp index e5e5130..c0f3e17 100644 --- a/lib/AST/DeclPrinter.cpp +++ b/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<PackExpansionType>()) { + Pack = true; + T = PET->getPattern(); + } + T.print(Out, Policy, (Pack ? "..." : "") + DeclName); +} + void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) { this->Indent(); Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); @@ -365,6 +377,9 @@ void DeclPrinter::VisitRecordDecl(RecordDecl *D) { if (!Policy.SuppressSpecifiers && D->isModulePrivate()) Out << "__module_private__ "; Out << D->getKindName(); + + prettyPrintAttributes(D); + if (D->getIdentifier()) Out << ' ' << *D; @@ -647,7 +662,6 @@ void DeclPrinter::VisitLabelDecl(LabelDecl *D) { Out << *D << ":"; } - void DeclPrinter::VisitVarDecl(VarDecl *D) { if (!Policy.SuppressSpecifiers) { StorageClass SC = D->getStorageClass(); @@ -675,7 +689,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; @@ -757,6 +771,9 @@ void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { if (!Policy.SuppressSpecifiers && D->isModulePrivate()) Out << "__module_private__ "; Out << D->getKindName(); + + prettyPrintAttributes(D); + if (D->getIdentifier()) Out << ' ' << *D; @@ -773,9 +790,11 @@ void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { Out << "virtual "; AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); - if (AS != AS_none) + if (AS != AS_none) { Print(AS); - Out << " " << Base->getType().getAsString(Policy); + Out << " "; + } + Out << Base->getType().getAsString(Policy); if (Base->isPackExpansion()) Out << "..."; @@ -830,7 +849,7 @@ void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params, Out << "class "; if (TTP->isParameterPack()) - Out << "... "; + Out << "..."; Out << *TTP; @@ -843,15 +862,10 @@ void DeclPrinter::PrintTemplateParameters(const TemplateParameterList *Params, }; } else if (const NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { - Out << NTTP->getType().getAsString(Policy); - - if (NTTP->isParameterPack() && !isa<PackExpansionType>(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 << " = "; @@ -940,11 +954,12 @@ void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { if (OMD->isVariadic()) Out << ", ..."; + + prettyPrintAttributes(OMD); if (OMD->getBody() && !Policy.TerseOutput) { Out << ' '; OMD->getBody()->printPretty(Out, nullptr, Policy); - Out << '\n'; } else if (Policy.PolishForDeclaration) Out << ';'; @@ -954,6 +969,7 @@ void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { std::string I = OID->getNameAsString(); ObjCInterfaceDecl *SID = OID->getSuperClass(); + bool eolnOut = false; if (SID) Out << "@implementation " << I << " : " << *SID; else @@ -961,6 +977,7 @@ void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { if (OID->ivar_size() > 0) { Out << "{\n"; + eolnOut = true; Indentation += Policy.Indentation; for (const auto *I : OID->ivars()) { Indent() << I->getASTContext().getUnqualifiedObjCPointerType(I->getType()). @@ -969,7 +986,13 @@ void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { Indentation -= Policy.Indentation; Out << "}\n"; } + else if (SID || (OID->decls_begin() != OID->decls_end())) { + Out << "\n"; + eolnOut = true; + } VisitDeclContext(OID, false); + if (!eolnOut) + Out << "\n"; Out << "@end"; } @@ -1008,14 +1031,14 @@ void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { Indentation -= Policy.Indentation; Out << "}\n"; } - else if (SID) { + else if (SID || (OID->decls_begin() != OID->decls_end())) { Out << "\n"; eolnOut = true; } VisitDeclContext(OID, false); if (!eolnOut) - Out << ' '; + Out << "\n"; Out << "@end"; // FIXME: implement the rest... } |