[cfe-commits] r135398 - in /cfe/trunk: include/clang/AST/APValue.h lib/AST/APValue.cpp unittests/AST/ unittests/AST/APValueTest.cpp unittests/AST/Makefile unittests/CMakeLists.txt unittests/Makefile

Jeffrey Yasskin jyasskin at google.com
Mon Jul 18 09:43:53 PDT 2011


Author: jyasskin
Date: Mon Jul 18 11:43:53 2011
New Revision: 135398

URL: http://llvm.org/viewvc/llvm-project?rev=135398&view=rev
Log:
Define DiagnosticBuilder<<APValue so it's easy to include APValues in
diagnostics.

Added:
    cfe/trunk/unittests/AST/
    cfe/trunk/unittests/AST/APValueTest.cpp
    cfe/trunk/unittests/AST/Makefile
Modified:
    cfe/trunk/include/clang/AST/APValue.h
    cfe/trunk/lib/AST/APValue.cpp
    cfe/trunk/unittests/CMakeLists.txt
    cfe/trunk/unittests/Makefile

Modified: cfe/trunk/include/clang/AST/APValue.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/APValue.h?rev=135398&r1=135397&r2=135398&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/APValue.h (original)
+++ cfe/trunk/include/clang/AST/APValue.h Mon Jul 18 11:43:53 2011
@@ -19,6 +19,7 @@
 
 namespace clang {
   class CharUnits;
+  class DiagnosticBuilder;
   class Expr;
 
 /// APValue - This class implements a discriminated union of [uninitialized]
@@ -238,6 +239,10 @@
   return OS;
 }
 
+// Writes a concise representation of V to DB, in a single << operation.
+const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
+                                    const APValue &V);
+
 } // end namespace clang.
 
 #endif

Modified: cfe/trunk/lib/AST/APValue.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/APValue.cpp?rev=135398&r1=135397&r2=135398&view=diff
==============================================================================
--- cfe/trunk/lib/AST/APValue.cpp (original)
+++ cfe/trunk/lib/AST/APValue.cpp Mon Jul 18 11:43:53 2011
@@ -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;

Added: cfe/trunk/unittests/AST/APValueTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/AST/APValueTest.cpp?rev=135398&view=auto
==============================================================================
--- cfe/trunk/unittests/AST/APValueTest.cpp (added)
+++ cfe/trunk/unittests/AST/APValueTest.cpp Mon Jul 18 11:43:53 2011
@@ -0,0 +1,79 @@
+//===- unittests/AST/APValueTest.cpp - APValue tests ---===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/APValue.h"
+
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.h"
+
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace clang;
+
+namespace {
+
+class DiagnosticOutputGetter {
+  class LastDiagnosticString : public DiagnosticClient {
+    SmallString<64> LastDiagnostic;
+  public:
+    virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
+                                  const DiagnosticInfo &Info) {
+      LastDiagnostic.clear();
+      Info.FormatDiagnostic(LastDiagnostic);
+    }
+
+    StringRef get() const { return LastDiagnostic; }
+  };
+
+  const IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs;
+  const unsigned diag_just_format;
+  LastDiagnosticString LastDiagnostic;
+  Diagnostic Diag;
+
+public:
+  DiagnosticOutputGetter()
+    : DiagIDs(new DiagnosticIDs),
+      diag_just_format(DiagIDs->getCustomDiagID(DiagnosticIDs::Error, "%0")),
+      Diag(DiagIDs, &LastDiagnostic, false) {
+  }
+
+  template<typename T>
+  std::string operator()(const T& value) {
+    Diag.Report(diag_just_format) << value;
+    return LastDiagnostic.get().str();
+  }
+};
+
+TEST(APValue, Diagnostics) {
+  DiagnosticOutputGetter GetDiagnosticOutput;
+
+  EXPECT_EQ("Uninitialized", GetDiagnosticOutput(APValue()));
+  EXPECT_EQ("5", GetDiagnosticOutput(APValue(APSInt(APInt(16, 5)))));
+  EXPECT_EQ("3.141590e+00",
+            GetDiagnosticOutput(APValue(APFloat(APFloat::IEEEdouble,
+                                                "3.14159"))));
+  EXPECT_EQ("3+4i",
+            GetDiagnosticOutput(APValue(APSInt(APInt(16, 3)),
+                                        APSInt(APInt(16, 4)))));
+  EXPECT_EQ("3.200000e+00+5.700000e+00i",
+            GetDiagnosticOutput(APValue(
+                                  APFloat(APFloat::IEEEdouble, "3.2"),
+                                  APFloat(APFloat::IEEEdouble, "5.7"))));
+  APValue V[] = {
+    APValue(APSInt(APInt(16, 3))),
+    APValue(APSInt(APInt(16, 4))),
+    APValue(APSInt(APInt(16, 5)))
+  };
+  EXPECT_EQ("[3, 4, 5]",
+            GetDiagnosticOutput(APValue(V, array_lengthof(V))));
+}
+
+} // anonymous namespace

Added: cfe/trunk/unittests/AST/Makefile
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/AST/Makefile?rev=135398&view=auto
==============================================================================
--- cfe/trunk/unittests/AST/Makefile (added)
+++ cfe/trunk/unittests/AST/Makefile Mon Jul 18 11:43:53 2011
@@ -0,0 +1,15 @@
+##===- unittests/Frontend/Makefile -------------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL = ../..
+TESTNAME = AST
+LINK_COMPONENTS := support mc
+USEDLIBS = clangAST.a clangBasic.a
+
+include $(CLANG_LEVEL)/unittests/Makefile

Modified: cfe/trunk/unittests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/CMakeLists.txt?rev=135398&r1=135397&r2=135398&view=diff
==============================================================================
--- cfe/trunk/unittests/CMakeLists.txt (original)
+++ cfe/trunk/unittests/CMakeLists.txt Mon Jul 18 11:43:53 2011
@@ -50,6 +50,11 @@
   add_definitions("-Wno-variadic-macros")
 endif()
 
+add_clang_unittest(AST
+  AST/APValueTest.cpp
+  USED_LIBS gtest gtest_main clangAST
+ )
+
 add_clang_unittest(Basic
   Basic/FileManagerTest.cpp
   USED_LIBS gtest gtest_main clangBasic

Modified: cfe/trunk/unittests/Makefile
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Makefile?rev=135398&r1=135397&r2=135398&view=diff
==============================================================================
--- cfe/trunk/unittests/Makefile (original)
+++ cfe/trunk/unittests/Makefile Mon Jul 18 11:43:53 2011
@@ -14,7 +14,7 @@
 
 IS_UNITTEST_LEVEL := 1
 CLANG_LEVEL := ..
-PARALLEL_DIRS = Basic Frontend
+PARALLEL_DIRS = AST Basic Frontend
 
 endif  # CLANG_LEVEL
 





More information about the cfe-commits mailing list