From: Benjamin Kramer Date: Sat, 23 Feb 2013 13:53:57 +0000 (+0000) Subject: Add streamed versions of getQualifiedNameAsString. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b063ef0222a99ee168631afa7b5a882d494b8fde;p=clang Add streamed versions of getQualifiedNameAsString. Move the cold virtual method getNameForDiagnostic out of line. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175966 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index 3a34b051f8..de392d5194 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -24,7 +24,6 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/Optional.h" #include "llvm/Support/Compiler.h" -#include "llvm/Support/raw_ostream.h" namespace clang { struct ASTTemplateArgumentListInfo; @@ -152,32 +151,29 @@ public: /// \brief Set the name of this declaration. void setDeclName(DeclarationName N) { Name = N; } - /// getQualifiedNameAsString - Returns human-readable qualified name for + /// printQualifiedName - Returns human-readable qualified name for /// declaration, like A::B::i, for i being member of namespace A::B. /// If declaration is not member of context which can be named (record, - /// namespace), it will return same result as getNameAsString(). + /// namespace), it will return same result as printName(). /// Creating this name is expensive, so it should be called only when /// performance doesn't matter. + void printQualifiedName(raw_ostream &OS) const; + void printQualifiedName(raw_ostream &OS, const PrintingPolicy &Policy) const; + + // FIXME: Remove string versions. std::string getQualifiedNameAsString() const; std::string getQualifiedNameAsString(const PrintingPolicy &Policy) const; /// getNameForDiagnostic - Appends a human-readable name for this - /// declaration into the given string. + /// declaration into the given stream. /// /// This is the method invoked by Sema when displaying a NamedDecl /// in a diagnostic. It does not necessarily produce the same - /// result as getNameAsString(); for example, class template + /// result as printName(); for example, class template /// specializations are printed with their template arguments. - /// - /// TODO: use an API that doesn't require so many temporary strings virtual void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, - bool Qualified) const { - if (Qualified) - OS << getQualifiedNameAsString(Policy); - else - printName(OS); - } + bool Qualified) const; /// declarationReplaces - Determine whether this declaration, if /// known to be well-formed within its context, will replace the diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index d2a9f84cd4..5e8d130789 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -1173,10 +1173,24 @@ std::string NamedDecl::getQualifiedNameAsString() const { } std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { + std::string QualName; + llvm::raw_string_ostream OS(QualName); + printQualifiedName(OS, P); + return OS.str(); +} + +void NamedDecl::printQualifiedName(raw_ostream &OS) const { + printQualifiedName(OS, getASTContext().getPrintingPolicy()); +} + +void NamedDecl::printQualifiedName(raw_ostream &OS, + const PrintingPolicy &P) const { const DeclContext *Ctx = getDeclContext(); - if (Ctx->isFunctionOrMethod()) - return getNameAsString(); + if (Ctx->isFunctionOrMethod()) { + printName(OS); + return; + } typedef SmallVector ContextsTy; ContextsTy Contexts; @@ -1185,10 +1199,7 @@ std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { while (Ctx && isa(Ctx)) { Contexts.push_back(Ctx); Ctx = Ctx->getParent(); - }; - - std::string QualName; - llvm::raw_string_ostream OS(QualName); + } for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); I != E; ++I) { @@ -1241,8 +1252,15 @@ std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { OS << *this; else OS << ""; +} - return OS.str(); +void NamedDecl::getNameForDiagnostic(raw_ostream &OS, + const PrintingPolicy &Policy, + bool Qualified) const { + if (Qualified) + printQualifiedName(OS, Policy); + else + printName(OS); } bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index cdf150595c..0db520e7d6 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -190,8 +190,11 @@ void PrettyStackTraceDecl::print(raw_ostream &OS) const { OS << Message; - if (const NamedDecl *DN = dyn_cast_or_null(TheDecl)) - OS << " '" << DN->getQualifiedNameAsString() << '\''; + if (const NamedDecl *DN = dyn_cast_or_null(TheDecl)) { + OS << " '"; + DN->printQualifiedName(OS); + OS << '\''; + } OS << '\n'; } diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 66980a9cd1..b97f4d1d3a 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -477,8 +477,9 @@ std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) { } PrintingPolicy Policy(Context.getLangOpts()); - std::string Proto = FD->getQualifiedNameAsString(Policy); + std::string Proto; llvm::raw_string_ostream POut(Proto); + FD->printQualifiedName(POut, Policy); const FunctionDecl *Decl = FD; if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern()) diff --git a/lib/CodeGen/CodeGenTypes.cpp b/lib/CodeGen/CodeGenTypes.cpp index c82e1d829d..259d10673d 100644 --- a/lib/CodeGen/CodeGenTypes.cpp +++ b/lib/CodeGen/CodeGenTypes.cpp @@ -61,14 +61,14 @@ void CodeGenTypes::addRecordTypeName(const RecordDecl *RD, // FIXME: We should not have to check for a null decl context here. // Right now we do it because the implicit Obj-C decls don't have one. if (RD->getDeclContext()) - OS << RD->getQualifiedNameAsString(); + RD->printQualifiedName(OS); else RD->printName(OS); } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) { // FIXME: We should not have to check for a null decl context here. // Right now we do it because the implicit Obj-C decls don't have one. if (TDD->getDeclContext()) - OS << TDD->getQualifiedNameAsString(); + TDD->printQualifiedName(OS); else TDD->printName(OS); } else diff --git a/lib/Frontend/ASTConsumers.cpp b/lib/Frontend/ASTConsumers.cpp index 687fc00c6c..936bd2f8a4 100644 --- a/lib/Frontend/ASTConsumers.cpp +++ b/lib/Frontend/ASTConsumers.cpp @@ -104,7 +104,8 @@ namespace { bool shouldWalkTypesOfTypeLocs() const { return false; } virtual bool VisitNamedDecl(NamedDecl *D) { - Out << D->getQualifiedNameAsString() << "\n"; + D->printQualifiedName(Out); + Out << '\n'; return true; }