Add printName to DeclarationName which prints the human-readable name on a
raw_ostream. Use it in getAsString and NamedDecl's raw_ostream operator.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101633 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h
index e3e16d3..d409fdb 100644
--- a/include/clang/AST/Decl.h
+++ b/include/clang/AST/Decl.h
@@ -217,7 +217,11 @@
   static bool classofKind(Kind K) { return K >= NamedFirst && K <= NamedLast; }
 };
 
-llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const NamedDecl *ND);
+inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
+                                     const NamedDecl *ND) {
+  ND->getDeclName().printName(OS);
+  return OS;
+}
 
 /// NamespaceDecl - Represent a C++ namespace.
 class NamespaceDecl : public NamedDecl, public DeclContext {
diff --git a/include/clang/AST/DeclarationName.h b/include/clang/AST/DeclarationName.h
index 6225069..a02e8d9 100644
--- a/include/clang/AST/DeclarationName.h
+++ b/include/clang/AST/DeclarationName.h
@@ -198,9 +198,12 @@
   /// callee in a call expression with dependent arguments.
   bool isDependentName() const;
   
-  /// getName - Retrieve the human-readable string for this name.
+  /// getNameAsString - Retrieve the human-readable string for this name.
   std::string getAsString() const;
 
+  /// printName - Print the human-readable name to a stream.
+  void printName(llvm::raw_ostream &OS) const;
+
   /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
   /// this declaration name, or NULL if this declaration name isn't a
   /// simple identifier.
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index fdec2f5..d4cc945 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -512,12 +512,6 @@
   return false;
 }
 
-llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
-                                     const NamedDecl *ND) {
-  OS << ND->getNameAsString();
-  return OS;
-}
-
 //===----------------------------------------------------------------------===//
 // DeclaratorDecl Implementation
 //===----------------------------------------------------------------------===//
diff --git a/lib/AST/DeclarationName.cpp b/lib/AST/DeclarationName.cpp
index 19b58bc..4f85fca 100644
--- a/lib/AST/DeclarationName.cpp
+++ b/lib/AST/DeclarationName.cpp
@@ -18,7 +18,7 @@
 #include "clang/Basic/IdentifierTable.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/FoldingSet.h"
-#include <cstdio>
+#include "llvm/Support/raw_ostream.h"
 using namespace clang;
 
 namespace clang {
@@ -202,32 +202,42 @@
 }
 
 std::string DeclarationName::getAsString() const {
+  std::string Result;
+  llvm::raw_string_ostream OS(Result);
+  printName(OS);
+  return OS.str();
+}
+
+void DeclarationName::printName(llvm::raw_ostream &OS) const {
   switch (getNameKind()) {
   case Identifier:
     if (const IdentifierInfo *II = getAsIdentifierInfo())
-      return II->getName();
-    return "";
+      OS << II->getName();
+    return;
 
   case ObjCZeroArgSelector:
   case ObjCOneArgSelector:
   case ObjCMultiArgSelector:
-    return getObjCSelector().getAsString();
+    OS << getObjCSelector().getAsString();
+    return;
 
   case CXXConstructorName: {
     QualType ClassType = getCXXNameType();
     if (const RecordType *ClassRec = ClassType->getAs<RecordType>())
-      return ClassRec->getDecl()->getNameAsString();
-    return ClassType.getAsString();
+      OS << ClassRec->getDecl();
+    else
+      OS << ClassType.getAsString();
+    return;
   }
 
   case CXXDestructorName: {
-    std::string Result = "~";
+    OS << '~';
     QualType Type = getCXXNameType();
     if (const RecordType *Rec = Type->getAs<RecordType>())
-      Result += Rec->getDecl()->getNameAsString();
+      OS << Rec->getDecl();
     else
-      Result += Type.getAsString();
-    return Result;
+      OS << Type.getAsString();
+    return;
   }
 
   case CXXOperatorName: {
@@ -240,32 +250,32 @@
     const char *OpName = OperatorNames[getCXXOverloadedOperator()];
     assert(OpName && "not an overloaded operator");
 
-    std::string Result = "operator";
+    OS << "operator";
     if (OpName[0] >= 'a' && OpName[0] <= 'z')
-      Result += ' ';
-    Result += OpName;
-    return Result;
+      OS << ' ';
+    OS << OpName;
+    return;
   }
 
-  case CXXLiteralOperatorName: {
-    return "operator \"\" " + std::string(getCXXLiteralIdentifier()->getName());
-  }
+  case CXXLiteralOperatorName:
+    OS << "operator \"\" " << getCXXLiteralIdentifier()->getName();
+    return;
 
   case CXXConversionFunctionName: {
-    std::string Result = "operator ";
+    OS << "operator ";
     QualType Type = getCXXNameType();
     if (const RecordType *Rec = Type->getAs<RecordType>())
-      Result += Rec->getDecl()->getNameAsString();
+      OS << Rec->getDecl();
     else
-      Result += Type.getAsString();
-    return Result;
+      OS << Type.getAsString();
+    return;
   }
   case CXXUsingDirective:
-    return "<using-directive>";
+    OS << "<using-directive>";
+    return;
   }
 
   assert(false && "Unexpected declaration name kind");
-  return "";
 }
 
 QualType DeclarationName::getCXXNameType() const {
@@ -369,7 +379,8 @@
 }
 
 void DeclarationName::dump() const {
-  fprintf(stderr, "%s\n", getAsString().c_str());
+  printName(llvm::errs());
+  llvm::errs() << '\n';
 }
 
 DeclarationNameTable::DeclarationNameTable() {