Add streamed versions of getQualifiedNameAsString.

Move the cold virtual method getNameForDiagnostic out of line.

llvm-svn: 175966
This commit is contained in:
Benjamin Kramer 2013-02-23 13:53:57 +00:00
parent f2ea19ea2e
commit 24ebf7c92c
6 changed files with 45 additions and 26 deletions

View File

@ -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

View File

@ -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<const DeclContext *, 8> ContextsTy;
ContextsTy Contexts;
@ -1185,10 +1199,7 @@ std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
while (Ctx && isa<NamedDecl>(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 << "<anonymous>";
}
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 {

View File

@ -190,8 +190,11 @@ void PrettyStackTraceDecl::print(raw_ostream &OS) const {
OS << Message;
if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
OS << " '" << DN->getQualifiedNameAsString() << '\'';
if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
OS << " '";
DN->printQualifiedName(OS);
OS << '\'';
}
OS << '\n';
}

View File

@ -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())

View File

@ -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

View File

@ -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;
}