AsmParser: Save and restore the parsing state for types using SlotMapping.
[oota-llvm.git] / unittests / AsmParser / AsmParserTest.cpp
1 //===- llvm/unittest/AsmParser/AsmParserTest.cpp - asm parser unittests ---===//
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 "llvm/ADT/StringRef.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/AsmParser/SlotMapping.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "gtest/gtest.h"
18
19 using namespace llvm;
20
21 namespace {
22
23 TEST(AsmParserTest, NullTerminatedInput) {
24   LLVMContext &Ctx = getGlobalContext();
25   StringRef Source = "; Empty module \n";
26   SMDiagnostic Error;
27   auto Mod = parseAssemblyString(Source, Error, Ctx);
28
29   EXPECT_TRUE(Mod != nullptr);
30   EXPECT_TRUE(Error.getMessage().empty());
31 }
32
33 #ifdef GTEST_HAS_DEATH_TEST
34 #ifndef NDEBUG
35
36 TEST(AsmParserTest, NonNullTerminatedInput) {
37   LLVMContext &Ctx = getGlobalContext();
38   StringRef Source = "; Empty module \n\1\2";
39   SMDiagnostic Error;
40   std::unique_ptr<Module> Mod;
41   EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2),
42                                          Error, Ctx),
43                "Buffer is not null terminated!");
44 }
45
46 #endif
47 #endif
48
49 TEST(AsmParserTest, SlotMappingTest) {
50   LLVMContext &Ctx = getGlobalContext();
51   StringRef Source = "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}";
52   SMDiagnostic Error;
53   SlotMapping Mapping;
54   auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
55
56   EXPECT_TRUE(Mod != nullptr);
57   EXPECT_TRUE(Error.getMessage().empty());
58
59   ASSERT_EQ(Mapping.GlobalValues.size(), 1u);
60   EXPECT_TRUE(isa<GlobalVariable>(Mapping.GlobalValues[0]));
61
62   EXPECT_EQ(Mapping.MetadataNodes.size(), 2u);
63   EXPECT_EQ(Mapping.MetadataNodes.count(0), 1u);
64   EXPECT_EQ(Mapping.MetadataNodes.count(42), 1u);
65   EXPECT_EQ(Mapping.MetadataNodes.count(1), 0u);
66 }
67
68 TEST(AsmParserTest, TypeAndConstantValueParsing) {
69   LLVMContext &Ctx = getGlobalContext();
70   SMDiagnostic Error;
71   StringRef Source = "define void @test() {\n  entry:\n  ret void\n}";
72   auto Mod = parseAssemblyString(Source, Error, Ctx);
73   ASSERT_TRUE(Mod != nullptr);
74   auto &M = *Mod;
75
76   const Value *V;
77   V = parseConstantValue("double 3.5", Error, M);
78   ASSERT_TRUE(V);
79   EXPECT_TRUE(V->getType()->isDoubleTy());
80   ASSERT_TRUE(isa<ConstantFP>(V));
81   EXPECT_TRUE(cast<ConstantFP>(V)->isExactlyValue(3.5));
82
83   V = parseConstantValue("i32 42", Error, M);
84   ASSERT_TRUE(V);
85   EXPECT_TRUE(V->getType()->isIntegerTy());
86   ASSERT_TRUE(isa<ConstantInt>(V));
87   EXPECT_TRUE(cast<ConstantInt>(V)->equalsInt(42));
88
89   V = parseConstantValue("<4 x i32> <i32 0, i32 1, i32 2, i32 3>", Error, M);
90   ASSERT_TRUE(V);
91   EXPECT_TRUE(V->getType()->isVectorTy());
92   ASSERT_TRUE(isa<ConstantDataVector>(V));
93
94   V = parseConstantValue("i32 add (i32 1, i32 2)", Error, M);
95   ASSERT_TRUE(V);
96   ASSERT_TRUE(isa<ConstantInt>(V));
97
98   V = parseConstantValue("i8* blockaddress(@test, %entry)", Error, M);
99   ASSERT_TRUE(V);
100   ASSERT_TRUE(isa<BlockAddress>(V));
101
102   EXPECT_FALSE(parseConstantValue("duble 3.25", Error, M));
103   EXPECT_EQ(Error.getMessage(), "expected type");
104
105   EXPECT_FALSE(parseConstantValue("i32 3.25", Error, M));
106   EXPECT_EQ(Error.getMessage(), "floating point constant invalid for type");
107
108   EXPECT_FALSE(parseConstantValue("i32* @foo", Error, M));
109   EXPECT_EQ(Error.getMessage(), "expected a constant value");
110
111   EXPECT_FALSE(parseConstantValue("i32 3, ", Error, M));
112   EXPECT_EQ(Error.getMessage(), "expected end of string");
113 }
114
115 TEST(AsmParserTest, TypeAndConstantValueWithSlotMappingParsing) {
116   LLVMContext &Ctx = getGlobalContext();
117   SMDiagnostic Error;
118   StringRef Source =
119       "%st = type { i32, i32 }\n"
120       "@v = common global [50 x %st] zeroinitializer, align 16\n"
121       "%0 = type { i32, i32, i32, i32 }\n"
122       "@g = common global [50 x %0] zeroinitializer, align 16\n"
123       "define void @marker4(i64 %d) {\n"
124       "entry:\n"
125       "  %conv = trunc i64 %d to i32\n"
126       "  store i32 %conv, i32* getelementptr inbounds "
127       "    ([50 x %st], [50 x %st]* @v, i64 0, i64 0, i32 0), align 16\n"
128       "  store i32 %conv, i32* getelementptr inbounds "
129       "    ([50 x %0], [50 x %0]* @g, i64 0, i64 0, i32 0), align 16\n"
130       "  ret void\n"
131       "}";
132   SlotMapping Mapping;
133   auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
134   ASSERT_TRUE(Mod != nullptr);
135   auto &M = *Mod;
136
137   const Value *V;
138   V = parseConstantValue("i32* getelementptr inbounds ([50 x %st], [50 x %st]* "
139                          "@v, i64 0, i64 0, i32 0)",
140                          Error, M, &Mapping);
141   ASSERT_TRUE(V);
142   ASSERT_TRUE(isa<ConstantExpr>(V));
143
144   V = parseConstantValue("i32* getelementptr inbounds ([50 x %0], [50 x %0]* "
145                          "@g, i64 0, i64 0, i32 0)",
146                          Error, M, &Mapping);
147   ASSERT_TRUE(V);
148   ASSERT_TRUE(isa<ConstantExpr>(V));
149 }
150
151 } // end anonymous namespace