1 //===- 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 18 using namespace llvm; 19 using namespace clang; 20 21 namespace { 22 23 class DiagnosticOutputGetter { 24 class LastDiagnosticString : public DiagnosticConsumer { 25 SmallString<64> LastDiagnostic; 26 public: 27 virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 28 const Diagnostic &Info) { 29 LastDiagnostic.clear(); 30 Info.FormatDiagnostic(LastDiagnostic); 31 } 32 33 StringRef get() const { return LastDiagnostic; } 34 35 virtual DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { 36 return new LastDiagnosticString(); 37 } 38 }; 39 40 const IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs; 41 const unsigned diag_just_format; 42 LastDiagnosticString LastDiagnostic; 43 DiagnosticsEngine Diag; 44 45 public: 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 59 TEST(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 84