summaryrefslogtreecommitdiffstats
path: root/unittests/AST/APValueTest.cpp
diff options
context:
space:
mode:
authordim <dim@FreeBSD.org>2011-10-20 21:14:49 +0000
committerdim <dim@FreeBSD.org>2011-10-20 21:14:49 +0000
commit3963a48221351c61c17fb3f382341ab04809a3d3 (patch)
treeee2483e98b09cac943dc93a6969d83ca737ff139 /unittests/AST/APValueTest.cpp
parent611ba3ea3300b71eb95dc4e45f20eee5dddd32e1 (diff)
downloadFreeBSD-src-3963a48221351c61c17fb3f382341ab04809a3d3.zip
FreeBSD-src-3963a48221351c61c17fb3f382341ab04809a3d3.tar.gz
Vendor import of clang release_30 branch r142614:
http://llvm.org/svn/llvm-project/cfe/branches/release_30@142614
Diffstat (limited to 'unittests/AST/APValueTest.cpp')
-rw-r--r--unittests/AST/APValueTest.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/unittests/AST/APValueTest.cpp b/unittests/AST/APValueTest.cpp
new file mode 100644
index 0000000..5ac454d
--- /dev/null
+++ b/unittests/AST/APValueTest.cpp
@@ -0,0 +1,83 @@
+//===- 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 DiagnosticConsumer {
+ SmallString<64> LastDiagnostic;
+ public:
+ virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
+ const Diagnostic &Info) {
+ LastDiagnostic.clear();
+ Info.FormatDiagnostic(LastDiagnostic);
+ }
+
+ StringRef get() const { return LastDiagnostic; }
+
+ virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
+ return new LastDiagnosticString();
+ }
+ };
+
+ const IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs;
+ const unsigned diag_just_format;
+ LastDiagnosticString LastDiagnostic;
+ DiagnosticsEngine 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
OpenPOWER on IntegriCloud