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 EXPECT_FALSE(parseConstantValue("duble 3.25", Error, M));
103 EXPECT_EQ(Error.getMessage(), "expected type");
105 EXPECT_FALSE(parseConstantValue("i32 3.25", Error, M));
106 EXPECT_EQ(Error.getMessage(), "floating point constant invalid for type");
108 EXPECT_FALSE(parseConstantValue("i32* @foo", Error, M));
109 EXPECT_EQ(Error.getMessage(), "expected a constant value");
111 EXPECT_FALSE(parseConstantValue("i32 3, ", Error, M));
112 EXPECT_EQ(Error.getMessage(), "expected end of string");
115 } // end anonymous namespace