Provide operator<< for stream output of DeclarationNames

ASTDumper was already trying to do this & instead got an implicit bool
conversion by surprise (thus printing out 0 or 1 instead of the name of
the declaration). To avoid that issue & simplify call sites, simply make
it the normal/expected operator<<(raw_ostream&, ...) overload & simplify
all the existing call sites. (bonus: this function doesn't need to be a
member or friend, it's just using public API in DeclarationName)

llvm-svn: 181832
This commit is contained in:
David Blaikie 2013-05-14 21:04:00 +00:00
parent 9fbc230de2
commit d4da8728ba
9 changed files with 86 additions and 93 deletions

View File

@ -142,7 +142,7 @@ public:
// FIXME: Deprecated, move clients to getName().
std::string getNameAsString() const { return Name.getAsString(); }
void printName(raw_ostream &os) const { return Name.printName(os); }
void printName(raw_ostream &os) const { os << Name; }
/// getDeclName - Get the actual, stored name of the declaration,
/// which may be a special name.

View File

@ -210,9 +210,6 @@ public:
/// getNameAsString - Retrieve the human-readable string for this name.
std::string getAsString() const;
/// printName - Print the human-readable name to a stream.
void printName(raw_ostream &OS) const;
/// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
/// this declaration name, or NULL if this declaration name isn't a
/// simple identifier.
@ -302,6 +299,8 @@ public:
void dump() const;
};
raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
/// Ordering on two declaration names. If both names are identifiers,
/// this provides a lexicographical ordering.
inline bool operator<(DeclarationName LHS, DeclarationName RHS) {

View File

@ -300,8 +300,7 @@ void clang::FormatASTNodeDiagnosticArgument(
assert(ModLen == 0 && ArgLen == 0 &&
"Invalid modifier for DeclarationName argument");
DeclarationName N = DeclarationName::getFromOpaqueInteger(Val);
N.printName(OS);
OS << DeclarationName::getFromOpaqueInteger(Val);
break;
}
case DiagnosticsEngine::ak_nameddecl: {

View File

@ -449,9 +449,7 @@ void ASTDumper::dumpBareDeclRef(const Decl *D) {
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
ColorScope Color(*this, DeclNameColor);
OS << " '";
ND->getDeclName().printName(OS);
OS << "'";
OS << " '" << ND->getDeclName() << '\'';
}
if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))

View File

@ -133,6 +133,66 @@ int DeclarationName::compare(DeclarationName LHS, DeclarationName RHS) {
llvm_unreachable("Invalid DeclarationName Kind!");
}
raw_ostream &operator<<(raw_ostream &OS, DeclarationName N) {
switch (N.getNameKind()) {
case DeclarationName::Identifier:
if (const IdentifierInfo *II = N.getAsIdentifierInfo())
OS << II->getName();
return OS;
case DeclarationName::ObjCZeroArgSelector:
case DeclarationName::ObjCOneArgSelector:
case DeclarationName::ObjCMultiArgSelector:
return OS << N.getObjCSelector().getAsString();
case DeclarationName::CXXConstructorName: {
QualType ClassType = N.getCXXNameType();
if (const RecordType *ClassRec = ClassType->getAs<RecordType>())
return OS << *ClassRec->getDecl();
return OS << ClassType.getAsString();
}
case DeclarationName::CXXDestructorName: {
OS << '~';
QualType Type = N.getCXXNameType();
if (const RecordType *Rec = Type->getAs<RecordType>())
return OS << *Rec->getDecl();
return OS << Type.getAsString();
}
case DeclarationName::CXXOperatorName: {
static const char* const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
0,
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Spelling,
#include "clang/Basic/OperatorKinds.def"
};
const char *OpName = OperatorNames[N.getCXXOverloadedOperator()];
assert(OpName && "not an overloaded operator");
OS << "operator";
if (OpName[0] >= 'a' && OpName[0] <= 'z')
OS << ' ';
return OS << OpName;
}
case DeclarationName::CXXLiteralOperatorName:
return OS << "operator \"\" " << N.getCXXLiteralIdentifier()->getName();
case DeclarationName::CXXConversionFunctionName: {
OS << "operator ";
QualType Type = N.getCXXNameType();
if (const RecordType *Rec = Type->getAs<RecordType>())
return OS << *Rec->getDecl();
return OS << Type.getAsString();
}
case DeclarationName::CXXUsingDirective:
return OS << "<using-directive>";
}
llvm_unreachable("Unexpected declaration name kind");
}
} // end namespace clang
DeclarationName::NameKind DeclarationName::getNameKind() const {
@ -180,80 +240,10 @@ bool DeclarationName::isDependentName() const {
std::string DeclarationName::getAsString() const {
std::string Result;
llvm::raw_string_ostream OS(Result);
printName(OS);
OS << *this;
return OS.str();
}
void DeclarationName::printName(raw_ostream &OS) const {
switch (getNameKind()) {
case Identifier:
if (const IdentifierInfo *II = getAsIdentifierInfo())
OS << II->getName();
return;
case ObjCZeroArgSelector:
case ObjCOneArgSelector:
case ObjCMultiArgSelector:
OS << getObjCSelector().getAsString();
return;
case CXXConstructorName: {
QualType ClassType = getCXXNameType();
if (const RecordType *ClassRec = ClassType->getAs<RecordType>())
OS << *ClassRec->getDecl();
else
OS << ClassType.getAsString();
return;
}
case CXXDestructorName: {
OS << '~';
QualType Type = getCXXNameType();
if (const RecordType *Rec = Type->getAs<RecordType>())
OS << *Rec->getDecl();
else
OS << Type.getAsString();
return;
}
case CXXOperatorName: {
static const char* const OperatorNames[NUM_OVERLOADED_OPERATORS] = {
0,
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
Spelling,
#include "clang/Basic/OperatorKinds.def"
};
const char *OpName = OperatorNames[getCXXOverloadedOperator()];
assert(OpName && "not an overloaded operator");
OS << "operator";
if (OpName[0] >= 'a' && OpName[0] <= 'z')
OS << ' ';
OS << OpName;
return;
}
case CXXLiteralOperatorName:
OS << "operator \"\" " << getCXXLiteralIdentifier()->getName();
return;
case CXXConversionFunctionName: {
OS << "operator ";
QualType Type = getCXXNameType();
if (const RecordType *Rec = Type->getAs<RecordType>())
OS << *Rec->getDecl();
else
OS << Type.getAsString();
return;
}
case CXXUsingDirective:
OS << "<using-directive>";
return;
}
llvm_unreachable("Unexpected declaration name kind");
}
QualType DeclarationName::getCXXNameType() const {
if (CXXSpecialName *CXXName = getAsCXXSpecialName())
return CXXName->Type;
@ -336,8 +326,7 @@ DeclarationName DeclarationName::getUsingDirectiveName() {
}
void DeclarationName::dump() const {
printName(llvm::errs());
llvm::errs() << '\n';
llvm::errs() << *this << '\n';
}
DeclarationNameTable::DeclarationNameTable(const ASTContext &C) : Ctx(C) {
@ -537,7 +526,7 @@ void DeclarationNameInfo::printName(raw_ostream &OS) const {
case DeclarationName::CXXOperatorName:
case DeclarationName::CXXLiteralOperatorName:
case DeclarationName::CXXUsingDirective:
Name.printName(OS);
OS << Name;
return;
case DeclarationName::CXXConstructorName:
@ -549,9 +538,8 @@ void DeclarationNameInfo::printName(raw_ostream &OS) const {
else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
OS << "operator ";
OS << TInfo->getType().getAsString();
}
else
Name.printName(OS);
} else
OS << Name;
return;
}
llvm_unreachable("Unexpected declaration name kind");

View File

@ -4166,7 +4166,7 @@ std::string TypoCorrection::getAsString(const LangOptions &LO) const {
std::string tmpBuffer;
llvm::raw_string_ostream PrefixOStream(tmpBuffer);
CorrectionNameSpec->print(PrefixOStream, PrintingPolicy(LO));
CorrectionName.printName(PrefixOStream);
PrefixOStream << CorrectionName;
return PrefixOStream.str();
}

View File

@ -1465,9 +1465,7 @@ ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
SmallString<256> Buf;
llvm::raw_svector_ostream Out(Buf);
Out << "Assuming '";
VD->getDeclName().printName(Out);
Out << "' is ";
Out << "Assuming '" << VD->getDeclName() << "' is ";
QualType VDTy = VD->getType();

View File

@ -1,6 +1,7 @@
// RUN: %clang_cc1 -ast-print %s > %t
// RUN: FileCheck < %t %s -check-prefix=CHECK1
// RUN: FileCheck < %t %s -check-prefix=CHECK2
// RUN: %clang_cc1 -ast-dump %s | FileCheck --check-prefix=DUMP %s
template <int X, typename Y, int Z = 5>
struct foo {
@ -37,3 +38,14 @@ void baz() {
// Template definition - bar
// CHECK1: template <int A, typename B> B bar()
// CHECK2: template <int A, typename B> B bar()
namespace test2 {
void func(int);
void func(float);
template<typename T>
void tmpl() {
func(T());
}
// DUMP: UnresolvedLookupExpr {{.*}} <col:3> '<overloaded function type>' lvalue (ADL) = 'func'
}

View File

@ -309,9 +309,8 @@ void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
// Ideally we would use 'GenObjCMethod', but this is such a hot path
// for Objective-C code that we don't want to use
// DeclarationName::getAsString().
Out << (D->isInstanceMethod() ? "(im)" : "(cm)");
DeclarationName N(D->getSelector());
N.printName(Out);
Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
<< DeclarationName(D->getSelector());
}
void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) {