blob: 5ac454de5f13ff9b03ce499a829fc517349368b7 [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 {
David Blaikie78ad0b92011-09-25 23:39:51 +000024 class LastDiagnosticString : public DiagnosticConsumer {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +000025 SmallString<64> LastDiagnostic;
26 public:
David Blaikied6471f72011-09-25 23:23:43 +000027 virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
David Blaikie40847cf2011-09-26 01:18:08 +000028 const Diagnostic &Info) {
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +000029 LastDiagnostic.clear();
30 Info.FormatDiagnostic(LastDiagnostic);
31 }
32
33 StringRef get() const { return LastDiagnostic; }
Douglas Gregor2587ede2011-09-29 00:53:49 +000034
35 virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
36 return new LastDiagnosticString();
37 }
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +000038 };
39
40 const IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs;
41 const unsigned diag_just_format;
42 LastDiagnosticString LastDiagnostic;
David Blaikied6471f72011-09-25 23:23:43 +000043 DiagnosticsEngine Diag;
Jeffrey Yasskin5b106a82011-07-18 16:43:53 +000044
45public:
46 DiagnosticOutputGetter()
47 : DiagIDs(new DiagnosticIDs),
48 diag_just_format(DiagIDs->getCustomDiagID(DiagnosticIDs::Error, "%0")),
49 Diag(DiagIDs, &LastDiagnostic, false) {
50 }
51
52 template<typename T>
53 std::string operator()(const T& value) {
54 Diag.Report(diag_just_format) << value;
55 return LastDiagnostic.get().str();
56 }
57};
58
59TEST(APValue, Diagnostics) {
60 DiagnosticOutputGetter GetDiagnosticOutput;
61
62 EXPECT_EQ("Uninitialized", GetDiagnosticOutput(APValue()));
63 EXPECT_EQ("5", GetDiagnosticOutput(APValue(APSInt(APInt(16, 5)))));
64 EXPECT_EQ("3.141590e+00",
65 GetDiagnosticOutput(APValue(APFloat(APFloat::IEEEdouble,
66 "3.14159"))));
67 EXPECT_EQ("3+4i",
68 GetDiagnosticOutput(APValue(APSInt(APInt(16, 3)),
69 APSInt(APInt(16, 4)))));
70 EXPECT_EQ("3.200000e+00+5.700000e+00i",
71 GetDiagnosticOutput(APValue(
72 APFloat(APFloat::IEEEdouble, "3.2"),
73 APFloat(APFloat::IEEEdouble, "5.7"))));
74 APValue V[] = {
75 APValue(APSInt(APInt(16, 3))),
76 APValue(APSInt(APInt(16, 4))),
77 APValue(APSInt(APInt(16, 5)))
78 };
79 EXPECT_EQ("[3, 4, 5]",
80 GetDiagnosticOutput(APValue(V, array_lengthof(V))));
81}
82
83} // anonymous namespace