diff options
Diffstat (limited to 'lib/AST/DeclPrinter.cpp')
-rw-r--r-- | lib/AST/DeclPrinter.cpp | 181 |
1 files changed, 105 insertions, 76 deletions
diff --git a/lib/AST/DeclPrinter.cpp b/lib/AST/DeclPrinter.cpp index 6eeba88e4033..b792c5920a55 100644 --- a/lib/AST/DeclPrinter.cpp +++ b/lib/AST/DeclPrinter.cpp @@ -28,6 +28,7 @@ namespace { class DeclPrinter : public DeclVisitor<DeclPrinter> { raw_ostream &Out; PrintingPolicy Policy; + const ASTContext &Context; unsigned Indentation; bool PrintInstantiation; @@ -36,6 +37,8 @@ namespace { void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls); void Print(AccessSpecifier AS); + void PrintConstructorInitializers(CXXConstructorDecl *CDecl, + std::string &Proto); /// Print an Objective-C method type in parentheses. /// @@ -48,9 +51,10 @@ namespace { public: DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy, - unsigned Indentation = 0, bool PrintInstantiation = false) - : Out(Out), Policy(Policy), Indentation(Indentation), - PrintInstantiation(PrintInstantiation) { } + const ASTContext &Context, unsigned Indentation = 0, + bool PrintInstantiation = false) + : Out(Out), Policy(Policy), Context(Context), Indentation(Indentation), + PrintInstantiation(PrintInstantiation) {} void VisitDeclContext(DeclContext *DC, bool Indent = true); @@ -115,7 +119,8 @@ void Decl::print(raw_ostream &Out, unsigned Indentation, void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, unsigned Indentation, bool PrintInstantiation) const { - DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation); + DeclPrinter Printer(Out, Policy, getASTContext(), Indentation, + PrintInstantiation); Printer.Visit(const_cast<Decl*>(this)); } @@ -192,7 +197,7 @@ LLVM_DUMP_METHOD void DeclContext::dumpDeclContext() const { DC = DC->getParent(); ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext(); - DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0); + DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), Ctx, 0); Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); } @@ -271,6 +276,71 @@ void DeclPrinter::Print(AccessSpecifier AS) { } } +void DeclPrinter::PrintConstructorInitializers(CXXConstructorDecl *CDecl, + std::string &Proto) { + bool HasInitializerList = false; + for (const auto *BMInitializer : CDecl->inits()) { + if (BMInitializer->isInClassMemberInitializer()) + continue; + + if (!HasInitializerList) { + Proto += " : "; + Out << Proto; + Proto.clear(); + HasInitializerList = true; + } else + Out << ", "; + + if (BMInitializer->isAnyMemberInitializer()) { + FieldDecl *FD = BMInitializer->getAnyMember(); + Out << *FD; + } else { + Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); + } + + Out << "("; + if (!BMInitializer->getInit()) { + // Nothing to print + } else { + Expr *Init = BMInitializer->getInit(); + if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) + Init = Tmp->getSubExpr(); + + Init = Init->IgnoreParens(); + + Expr *SimpleInit = nullptr; + Expr **Args = nullptr; + unsigned NumArgs = 0; + if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { + Args = ParenList->getExprs(); + NumArgs = ParenList->getNumExprs(); + } else if (CXXConstructExpr *Construct = + dyn_cast<CXXConstructExpr>(Init)) { + Args = Construct->getArgs(); + NumArgs = Construct->getNumArgs(); + } else + SimpleInit = Init; + + if (SimpleInit) + SimpleInit->printPretty(Out, nullptr, Policy, Indentation); + else { + for (unsigned I = 0; I != NumArgs; ++I) { + assert(Args[I] != nullptr && "Expected non-null Expr"); + if (isa<CXXDefaultArgExpr>(Args[I])) + break; + + if (I) + Out << ", "; + Args[I]->printPretty(Out, nullptr, Policy, Indentation); + } + } + } + Out << ")"; + if (BMInitializer->isPackExpansion()) + Out << "..."; + } +} + //---------------------------------------------------------------------------- // Common C declarations //---------------------------------------------------------------------------- @@ -467,7 +537,7 @@ void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { prettyPrintAttributes(D); if (Expr *Init = D->getInitExpr()) { Out << " = "; - Init->printPretty(Out, nullptr, Policy, Indentation); + Init->printPretty(Out, nullptr, Policy, Indentation, &Context); } } @@ -510,18 +580,24 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { PrintingPolicy SubPolicy(Policy); SubPolicy.SuppressSpecifiers = false; std::string Proto; - if (!Policy.SuppressScope) { - if (const NestedNameSpecifier *NS = D->getQualifier()) { - llvm::raw_string_ostream OS(Proto); - NS->print(OS, Policy); + + if (Policy.FullyQualifiedName) { + Proto += D->getQualifiedNameAsString(); + } else { + if (!Policy.SuppressScope) { + if (const NestedNameSpecifier *NS = D->getQualifier()) { + llvm::raw_string_ostream OS(Proto); + NS->print(OS, Policy); + } } + Proto += D->getNameInfo().getAsString(); } - Proto += D->getNameInfo().getAsString(); + if (GuideDecl) Proto = GuideDecl->getDeducedTemplate()->getDeclName().getAsString(); if (const TemplateArgumentList *TArgs = D->getTemplateSpecializationArgs()) { llvm::raw_string_ostream POut(Proto); - DeclPrinter TArgPrinter(POut, SubPolicy, Indentation); + DeclPrinter TArgPrinter(POut, SubPolicy, Context, Indentation); TArgPrinter.printTemplateArguments(*TArgs); } @@ -539,7 +615,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { Proto += "("; if (FT) { llvm::raw_string_ostream POut(Proto); - DeclPrinter ParamPrinter(POut, SubPolicy, Indentation); + DeclPrinter ParamPrinter(POut, SubPolicy, Context, Indentation); for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { if (i) POut << ", "; ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); @@ -605,67 +681,8 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { } if (CDecl) { - bool HasInitializerList = false; - for (const auto *BMInitializer : CDecl->inits()) { - if (BMInitializer->isInClassMemberInitializer()) - continue; - - if (!HasInitializerList) { - Proto += " : "; - Out << Proto; - Proto.clear(); - HasInitializerList = true; - } else - Out << ", "; - - if (BMInitializer->isAnyMemberInitializer()) { - FieldDecl *FD = BMInitializer->getAnyMember(); - Out << *FD; - } else { - Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); - } - - Out << "("; - if (!BMInitializer->getInit()) { - // Nothing to print - } else { - Expr *Init = BMInitializer->getInit(); - if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) - Init = Tmp->getSubExpr(); - - Init = Init->IgnoreParens(); - - Expr *SimpleInit = nullptr; - Expr **Args = nullptr; - unsigned NumArgs = 0; - if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { - Args = ParenList->getExprs(); - NumArgs = ParenList->getNumExprs(); - } else if (CXXConstructExpr *Construct - = dyn_cast<CXXConstructExpr>(Init)) { - Args = Construct->getArgs(); - NumArgs = Construct->getNumArgs(); - } else - SimpleInit = Init; - - if (SimpleInit) - SimpleInit->printPretty(Out, nullptr, Policy, Indentation); - else { - for (unsigned I = 0; I != NumArgs; ++I) { - assert(Args[I] != nullptr && "Expected non-null Expr"); - if (isa<CXXDefaultArgExpr>(Args[I])) - break; - - if (I) - Out << ", "; - Args[I]->printPretty(Out, nullptr, Policy, Indentation); - } - } - } - Out << ")"; - if (BMInitializer->isPackExpansion()) - Out << "..."; - } + if (!Policy.TerseOutput) + PrintConstructorInitializers(CDecl, Proto); } else if (!ConversionDecl && !isa<CXXDestructorDecl>(D)) { if (FT && FT->hasTrailingReturn()) { if (!GuideDecl) @@ -695,7 +712,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { // This is a K&R function definition, so we need to print the // parameters. Out << '\n'; - DeclPrinter ParamPrinter(Out, SubPolicy, Indentation); + DeclPrinter ParamPrinter(Out, SubPolicy, Context, Indentation); Indentation += Policy.Indentation; for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { Indent(); @@ -709,7 +726,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { if (D->getBody()) D->getBody()->printPretty(Out, nullptr, SubPolicy, Indentation); } else { - if (isa<CXXConstructorDecl>(*D)) + if (!Policy.TerseOutput && isa<CXXConstructorDecl>(*D)) Out << " {}"; } } @@ -1541,7 +1558,19 @@ void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { Out << ")"; if (auto *Init = D->getInitializer()) { Out << " initializer("; + switch (D->getInitializerKind()) { + case OMPDeclareReductionDecl::DirectInit: + Out << "omp_priv("; + break; + case OMPDeclareReductionDecl::CopyInit: + Out << "omp_priv = "; + break; + case OMPDeclareReductionDecl::CallInit: + break; + } Init->printPretty(Out, nullptr, Policy, 0); + if (D->getInitializerKind() == OMPDeclareReductionDecl::DirectInit) + Out << ")"; Out << ")"; } } |