1 //===- llvm/unittest/AsmParser/AsmParserTest.cpp - asm parser unittests ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
23 TEST(AsmParserTest
, NullTerminatedInput
) {
25 StringRef Source
= "; Empty module \n";
27 auto Mod
= parseAssemblyString(Source
, Error
, Ctx
);
29 EXPECT_TRUE(Mod
!= nullptr);
30 EXPECT_TRUE(Error
.getMessage().empty());
33 #ifdef GTEST_HAS_DEATH_TEST
36 TEST(AsmParserTest
, NonNullTerminatedInput
) {
38 StringRef Source
= "; Empty module \n\1\2";
40 std::unique_ptr
<Module
> Mod
;
41 EXPECT_DEATH(Mod
= parseAssemblyString(Source
.substr(0, Source
.size() - 2),
43 "Buffer is not null terminated!");
49 TEST(AsmParserTest
, SlotMappingTest
) {
51 StringRef Source
= "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}";
54 auto Mod
= parseAssemblyString(Source
, Error
, Ctx
, &Mapping
);
56 EXPECT_TRUE(Mod
!= nullptr);
57 EXPECT_TRUE(Error
.getMessage().empty());
59 ASSERT_EQ(Mapping
.GlobalValues
.size(), 1u);
60 EXPECT_TRUE(isa
<GlobalVariable
>(Mapping
.GlobalValues
[0]));
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);
68 TEST(AsmParserTest
, TypeAndConstantValueParsing
) {
71 StringRef Source
= "define void @test() {\n entry:\n ret void\n}";
72 auto Mod
= parseAssemblyString(Source
, Error
, Ctx
);
73 ASSERT_TRUE(Mod
!= nullptr);
77 V
= parseConstantValue("double 3.5", Error
, M
);
79 EXPECT_TRUE(V
->getType()->isDoubleTy());
80 ASSERT_TRUE(isa
<ConstantFP
>(V
));
81 EXPECT_TRUE(cast
<ConstantFP
>(V
)->isExactlyValue(3.5));
83 V
= parseConstantValue("i32 42", Error
, M
);
85 EXPECT_TRUE(V
->getType()->isIntegerTy());
86 ASSERT_TRUE(isa
<ConstantInt
>(V
));
87 EXPECT_TRUE(cast
<ConstantInt
>(V
)->equalsInt(42));
89 V
= parseConstantValue("<4 x i32> <i32 0, i32 1, i32 2, i32 3>", Error
, M
);
91 EXPECT_TRUE(V
->getType()->isVectorTy());
92 ASSERT_TRUE(isa
<ConstantDataVector
>(V
));
94 V
= parseConstantValue("i32 add (i32 1, i32 2)", Error
, M
);
96 ASSERT_TRUE(isa
<ConstantInt
>(V
));
98 V
= parseConstantValue("i8* blockaddress(@test, %entry)", Error
, M
);
100 ASSERT_TRUE(isa
<BlockAddress
>(V
));
102 V
= parseConstantValue("i8** undef", Error
, M
);
104 ASSERT_TRUE(isa
<UndefValue
>(V
));
106 EXPECT_FALSE(parseConstantValue("duble 3.25", Error
, M
));
107 EXPECT_EQ(Error
.getMessage(), "expected type");
109 EXPECT_FALSE(parseConstantValue("i32 3.25", Error
, M
));
110 EXPECT_EQ(Error
.getMessage(), "floating point constant invalid for type");
112 EXPECT_FALSE(parseConstantValue("i32* @foo", Error
, M
));
113 EXPECT_EQ(Error
.getMessage(), "expected a constant value");
115 EXPECT_FALSE(parseConstantValue("i32 3, ", Error
, M
));
116 EXPECT_EQ(Error
.getMessage(), "expected end of string");
119 TEST(AsmParserTest
, TypeAndConstantValueWithSlotMappingParsing
) {
123 "%st = type { i32, i32 }\n"
124 "@v = common global [50 x %st] zeroinitializer, align 16\n"
125 "%0 = type { i32, i32, i32, i32 }\n"
126 "@g = common global [50 x %0] zeroinitializer, align 16\n"
127 "define void @marker4(i64 %d) {\n"
129 " %conv = trunc i64 %d to i32\n"
130 " store i32 %conv, i32* getelementptr inbounds "
131 " ([50 x %st], [50 x %st]* @v, i64 0, i64 0, i32 0), align 16\n"
132 " store i32 %conv, i32* getelementptr inbounds "
133 " ([50 x %0], [50 x %0]* @g, i64 0, i64 0, i32 0), align 16\n"
137 auto Mod
= parseAssemblyString(Source
, Error
, Ctx
, &Mapping
);
138 ASSERT_TRUE(Mod
!= nullptr);
142 V
= parseConstantValue("i32* getelementptr inbounds ([50 x %st], [50 x %st]* "
143 "@v, i64 0, i64 0, i32 0)",
146 ASSERT_TRUE(isa
<ConstantExpr
>(V
));
148 V
= parseConstantValue("i32* getelementptr inbounds ([50 x %0], [50 x %0]* "
149 "@g, i64 0, i64 0, i32 0)",
152 ASSERT_TRUE(isa
<ConstantExpr
>(V
));
155 TEST(AsmParserTest
, TypeWithSlotMappingParsing
) {
159 "%st = type { i32, i32 }\n"
160 "@v = common global [50 x %st] zeroinitializer, align 16\n"
161 "%0 = type { i32, i32, i32, i32 }\n"
162 "@g = common global [50 x %0] zeroinitializer, align 16\n"
163 "define void @marker4(i64 %d) {\n"
165 " %conv = trunc i64 %d to i32\n"
166 " store i32 %conv, i32* getelementptr inbounds "
167 " ([50 x %st], [50 x %st]* @v, i64 0, i64 0, i32 0), align 16\n"
168 " store i32 %conv, i32* getelementptr inbounds "
169 " ([50 x %0], [50 x %0]* @g, i64 0, i64 0, i32 0), align 16\n"
173 auto Mod
= parseAssemblyString(Source
, Error
, Ctx
, &Mapping
);
174 ASSERT_TRUE(Mod
!= nullptr);
177 // Check we properly parse integer types.
179 Ty
= parseType("i32", Error
, M
, &Mapping
);
181 ASSERT_TRUE(Ty
->isIntegerTy());
182 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
184 // Check we properly parse integer types with exotic size.
185 Ty
= parseType("i13", Error
, M
, &Mapping
);
187 ASSERT_TRUE(Ty
->isIntegerTy());
188 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 13);
190 // Check we properly parse floating point types.
191 Ty
= parseType("float", Error
, M
, &Mapping
);
193 ASSERT_TRUE(Ty
->isFloatTy());
195 Ty
= parseType("double", Error
, M
, &Mapping
);
197 ASSERT_TRUE(Ty
->isDoubleTy());
199 // Check we properly parse struct types.
201 Ty
= parseType("%st", Error
, M
, &Mapping
);
203 ASSERT_TRUE(Ty
->isStructTy());
205 // Check the details of the struct.
206 StructType
*ST
= cast
<StructType
>(Ty
);
207 ASSERT_TRUE(ST
->getNumElements() == 2);
208 for (unsigned i
= 0, e
= ST
->getNumElements(); i
!= e
; ++i
) {
209 Ty
= ST
->getElementType(i
);
210 ASSERT_TRUE(Ty
->isIntegerTy());
211 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
215 Ty
= parseType("%0", Error
, M
, &Mapping
);
217 ASSERT_TRUE(Ty
->isStructTy());
219 // Check the details of the struct.
220 ST
= cast
<StructType
>(Ty
);
221 ASSERT_TRUE(ST
->getNumElements() == 4);
222 for (unsigned i
= 0, e
= ST
->getNumElements(); i
!= e
; ++i
) {
223 Ty
= ST
->getElementType(i
);
224 ASSERT_TRUE(Ty
->isIntegerTy());
225 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
228 // Check we properly parse vector types.
229 Ty
= parseType("<5 x i32>", Error
, M
, &Mapping
);
231 ASSERT_TRUE(Ty
->isVectorTy());
233 // Check the details of the vector.
234 VectorType
*VT
= cast
<VectorType
>(Ty
);
235 ASSERT_TRUE(VT
->getNumElements() == 5);
236 ASSERT_TRUE(VT
->getBitWidth() == 160);
237 Ty
= VT
->getElementType();
238 ASSERT_TRUE(Ty
->isIntegerTy());
239 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
242 Ty
= parseType("%opaque", Error
, M
, &Mapping
);
244 ASSERT_TRUE(Ty
->isStructTy());
246 ST
= cast
<StructType
>(Ty
);
247 ASSERT_TRUE(ST
->isOpaque());
249 // Check we properly parse pointer types.
251 Ty
= parseType("i32*", Error
, M
, &Mapping
);
253 ASSERT_TRUE(Ty
->isPointerTy());
255 PointerType
*PT
= cast
<PointerType
>(Ty
);
256 Ty
= PT
->getElementType();
257 ASSERT_TRUE(Ty
->isIntegerTy());
258 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
261 Ty
= parseType("i32**", Error
, M
, &Mapping
);
263 ASSERT_TRUE(Ty
->isPointerTy());
265 PT
= cast
<PointerType
>(Ty
);
266 Ty
= PT
->getElementType();
267 ASSERT_TRUE(Ty
->isPointerTy());
269 PT
= cast
<PointerType
>(Ty
);
270 Ty
= PT
->getElementType();
271 ASSERT_TRUE(Ty
->isIntegerTy());
272 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
274 // Check that we reject types with garbage.
275 Ty
= parseType("i32 garbage", Error
, M
, &Mapping
);
279 TEST(AsmParserTest
, TypeAtBeginningWithSlotMappingParsing
) {
283 "%st = type { i32, i32 }\n"
284 "@v = common global [50 x %st] zeroinitializer, align 16\n"
285 "%0 = type { i32, i32, i32, i32 }\n"
286 "@g = common global [50 x %0] zeroinitializer, align 16\n"
287 "define void @marker4(i64 %d) {\n"
289 " %conv = trunc i64 %d to i32\n"
290 " store i32 %conv, i32* getelementptr inbounds "
291 " ([50 x %st], [50 x %st]* @v, i64 0, i64 0, i32 0), align 16\n"
292 " store i32 %conv, i32* getelementptr inbounds "
293 " ([50 x %0], [50 x %0]* @g, i64 0, i64 0, i32 0), align 16\n"
297 auto Mod
= parseAssemblyString(Source
, Error
, Ctx
, &Mapping
);
298 ASSERT_TRUE(Mod
!= nullptr);
302 // Check we properly parse integer types.
304 Ty
= parseTypeAtBeginning("i32", Read
, Error
, M
, &Mapping
);
306 ASSERT_TRUE(Ty
->isIntegerTy());
307 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
308 ASSERT_TRUE(Read
== 3);
310 // Check we properly parse integer types with exotic size.
311 Ty
= parseTypeAtBeginning("i13", Read
, Error
, M
, &Mapping
);
313 ASSERT_TRUE(Ty
->isIntegerTy());
314 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 13);
315 ASSERT_TRUE(Read
== 3);
317 // Check we properly parse floating point types.
318 Ty
= parseTypeAtBeginning("float", Read
, Error
, M
, &Mapping
);
320 ASSERT_TRUE(Ty
->isFloatTy());
321 ASSERT_TRUE(Read
== 5);
323 Ty
= parseTypeAtBeginning("double", Read
, Error
, M
, &Mapping
);
325 ASSERT_TRUE(Ty
->isDoubleTy());
326 ASSERT_TRUE(Read
== 6);
328 // Check we properly parse struct types.
330 Ty
= parseTypeAtBeginning("%st", Read
, Error
, M
, &Mapping
);
332 ASSERT_TRUE(Ty
->isStructTy());
333 ASSERT_TRUE(Read
== 3);
335 // Check the details of the struct.
336 StructType
*ST
= cast
<StructType
>(Ty
);
337 ASSERT_TRUE(ST
->getNumElements() == 2);
338 for (unsigned i
= 0, e
= ST
->getNumElements(); i
!= e
; ++i
) {
339 Ty
= ST
->getElementType(i
);
340 ASSERT_TRUE(Ty
->isIntegerTy());
341 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
345 Ty
= parseTypeAtBeginning("%0", Read
, Error
, M
, &Mapping
);
347 ASSERT_TRUE(Ty
->isStructTy());
348 ASSERT_TRUE(Read
== 2);
350 // Check the details of the struct.
351 ST
= cast
<StructType
>(Ty
);
352 ASSERT_TRUE(ST
->getNumElements() == 4);
353 for (unsigned i
= 0, e
= ST
->getNumElements(); i
!= e
; ++i
) {
354 Ty
= ST
->getElementType(i
);
355 ASSERT_TRUE(Ty
->isIntegerTy());
356 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
359 // Check we properly parse vector types.
360 Ty
= parseTypeAtBeginning("<5 x i32>", Read
, Error
, M
, &Mapping
);
362 ASSERT_TRUE(Ty
->isVectorTy());
363 ASSERT_TRUE(Read
== 9);
365 // Check the details of the vector.
366 VectorType
*VT
= cast
<VectorType
>(Ty
);
367 ASSERT_TRUE(VT
->getNumElements() == 5);
368 ASSERT_TRUE(VT
->getBitWidth() == 160);
369 Ty
= VT
->getElementType();
370 ASSERT_TRUE(Ty
->isIntegerTy());
371 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
374 Ty
= parseTypeAtBeginning("%opaque", Read
, Error
, M
, &Mapping
);
376 ASSERT_TRUE(Ty
->isStructTy());
377 ASSERT_TRUE(Read
== 7);
379 ST
= cast
<StructType
>(Ty
);
380 ASSERT_TRUE(ST
->isOpaque());
382 // Check we properly parse pointer types.
384 Ty
= parseTypeAtBeginning("i32*", Read
, Error
, M
, &Mapping
);
386 ASSERT_TRUE(Ty
->isPointerTy());
387 ASSERT_TRUE(Read
== 4);
389 PointerType
*PT
= cast
<PointerType
>(Ty
);
390 Ty
= PT
->getElementType();
391 ASSERT_TRUE(Ty
->isIntegerTy());
392 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
395 Ty
= parseTypeAtBeginning("i32**", Read
, Error
, M
, &Mapping
);
397 ASSERT_TRUE(Ty
->isPointerTy());
398 ASSERT_TRUE(Read
== 5);
400 PT
= cast
<PointerType
>(Ty
);
401 Ty
= PT
->getElementType();
402 ASSERT_TRUE(Ty
->isPointerTy());
404 PT
= cast
<PointerType
>(Ty
);
405 Ty
= PT
->getElementType();
406 ASSERT_TRUE(Ty
->isIntegerTy());
407 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
409 // Check that we reject types with garbage.
410 Ty
= parseTypeAtBeginning("i32 garbage", Read
, Error
, M
, &Mapping
);
412 ASSERT_TRUE(Ty
->isIntegerTy());
413 ASSERT_TRUE(Ty
->getPrimitiveSizeInBits() == 32);
414 // We go to the next token, i.e., we read "i32" + ' '.
415 ASSERT_TRUE(Read
== 4);
418 } // end anonymous namespace