blob: ae054b363b5bbc0662f2fc147a63ebdea67f98e6 [file] [log] [blame]
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +00001//===- unittests/AST/APValueTest.cpp - APValue tests ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/AST/APValue.h"
11
12#include "clang/Basic/Diagnostic.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/SmallString.h"
15
16#include "gtest/gtest.h"
17
18using namespace llvm;
19using namespace clang;
20
21namespace {
22
23class DiagnosticOutputGetter {
24 class LastDiagnosticString : public DiagnosticClient {
25 SmallString<64> LastDiagnostic;
26 public:
27 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
28 const DiagnosticInfo &Info) {
29 LastDiagnostic.clear();
30 Info.FormatDiagnostic(LastDiagnostic);
31 }
32
33 StringRef get() const { return LastDiagnostic; }
34 };
35
36 const IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs;
37 const unsigned diag_just_format;
38 LastDiagnosticString LastDiagnostic;
39 Diagnostic Diag;
40
41public:
42 DiagnosticOutputGetter()
43 : DiagIDs(new DiagnosticIDs),
44 diag_just_format(DiagIDs->getCustomDiagID(DiagnosticIDs::Error, "%0")),
45 Diag(DiagIDs, &LastDiagnostic, false) {
46 }
47
48 template<typename T>
49 std::string operator()(const T& value) {
50 Diag.Report(diag_just_format) << value;
51 return LastDiagnostic.get().str();
52 }
53};
54
55TEST(APValue, Diagnostics) {
56 DiagnosticOutputGetter GetDiagnosticOutput;
57
58 EXPECT_EQ("Uninitialized", GetDiagnosticOutput(APValue()));
59 EXPECT_EQ("5", GetDiagnosticOutput(APValue(APSInt(APInt(16, 5)))));
60 EXPECT_EQ("3.141590e+00",
61 GetDiagnosticOutput(APValue(APFloat(APFloat::IEEEdouble,
62 "3.14159"))));
63 EXPECT_EQ("3+4i",
64 GetDiagnosticOutput(APValue(APSInt(APInt(16, 3)),
65 APSInt(APInt(16, 4)))));
66 EXPECT_EQ("3.200000e+00+5.700000e+00i",
67 GetDiagnosticOutput(APValue(
68 APFloat(APFloat::IEEEdouble, "3.2"),
69 APFloat(APFloat::IEEEdouble, "5.7"))));
70 APValue V[] = {
71 APValue(APSInt(APInt(16, 3))),
72 APValue(APSInt(APInt(16, 4))),
73 APValue(APSInt(APInt(16, 5)))
74 };
75 EXPECT_EQ("[3, 4, 5]",
76 GetDiagnosticOutput(APValue(V, array_lengthof(V))));
77}
78
79} // anonymous namespace