Define DiagnosticBuilder<<APValue so it's easy to include APValues in
diagnostics.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@135398 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/APValue.cpp b/lib/AST/APValue.cpp
index ebe99b1..86eec3b 100644
--- a/lib/AST/APValue.cpp
+++ b/lib/AST/APValue.cpp
@@ -13,6 +13,8 @@
 
 #include "clang/AST/APValue.h"
 #include "clang/AST/CharUnits.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;
 
@@ -118,6 +120,49 @@
   }
 }
 
+static void WriteShortAPValueToStream(llvm::raw_ostream& Out,
+                                      const APValue& V) {
+  switch (V.getKind()) {
+  default: assert(0 && "Unknown APValue kind!");
+  case APValue::Uninitialized:
+    Out << "Uninitialized";
+    break;
+  case APValue::Int:
+    Out << V.getInt();
+    break;
+  case APValue::Float:
+    Out << GetApproxValue(V.getFloat());
+    break;
+  case APValue::Vector:
+    Out << '[';
+    WriteShortAPValueToStream(Out, V.getVectorElt(0));
+    for (unsigned i = 1; i != V.getVectorLength(); ++i) {
+      Out << ", ";
+      WriteShortAPValueToStream(Out, V.getVectorElt(i));
+    }
+    Out << ']';
+    break;
+  case APValue::ComplexInt:
+    Out << V.getComplexIntReal() << "+" << V.getComplexIntImag() << "i";
+    break;
+  case APValue::ComplexFloat:
+    Out << GetApproxValue(V.getComplexFloatReal()) << "+"
+        << GetApproxValue(V.getComplexFloatImag()) << "i";
+    break;
+  case APValue::LValue:
+    Out << "LValue: <todo>";
+    break;
+  }
+}
+
+const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
+                                           const APValue &V) {
+  llvm::SmallString<64> Buffer;
+  llvm::raw_svector_ostream Out(Buffer);
+  WriteShortAPValueToStream(Out, V);
+  return DB << Out.str();
+}
+
 const Expr* APValue::getLValueBase() const {
   assert(isLValue() && "Invalid accessor");
   return ((const LV*)(const void*)Data)->Base;