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/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "gtest/gtest.h"
22 TEST(AsmParserTest, NullTerminatedInput) {
23 LLVMContext &Ctx = getGlobalContext();
24 StringRef Source = "; Empty module \n";
26 auto Mod = parseAssemblyString(Source, Error, Ctx);
28 EXPECT_TRUE(Mod != nullptr);
29 EXPECT_TRUE(Error.getMessage().empty());
32 #ifdef GTEST_HAS_DEATH_TEST
35 TEST(AsmParserTest, NonNullTerminatedInput) {
36 LLVMContext &Ctx = getGlobalContext();
37 StringRef Source = "; Empty module \n\1\2";
39 std::unique_ptr<Module> Mod;
40 EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2),
42 "Buffer is not null terminated!");
48 TEST(AsmParserTest, SlotMappingTest) {
49 LLVMContext &Ctx = getGlobalContext();
50 StringRef Source = "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}";
53 auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
55 EXPECT_TRUE(Mod != nullptr);
56 EXPECT_TRUE(Error.getMessage().empty());
58 ASSERT_EQ(Mapping.GlobalValues.size(), 1u);
59 EXPECT_TRUE(isa<GlobalVariable>(Mapping.GlobalValues[0]));
61 EXPECT_EQ(Mapping.MetadataNodes.size(), 2u);
62 EXPECT_EQ(Mapping.MetadataNodes.count(0), 1u);
63 EXPECT_EQ(Mapping.MetadataNodes.count(42), 1u);
64 EXPECT_EQ(Mapping.MetadataNodes.count(1), 0u);
67 } // end anonymous namespace