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) {
24 LLVMContext &Ctx = getGlobalContext();
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) {
37 LLVMContext &Ctx = getGlobalContext();
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) {
50 LLVMContext &Ctx = getGlobalContext();
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) {
69 LLVMContext &Ctx = getGlobalContext();
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) {
120 LLVMContext &Ctx = getGlobalContext();
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 } // end anonymous namespace