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/LLVMContext.h" 14 #include "llvm/IR/Module.h" 15 #include "llvm/Support/SourceMgr.h" 16 #include "gtest/gtest.h" 17 18 using namespace llvm; 19 20 namespace { 21 22 TEST(AsmParserTest, NullTerminatedInput) { 23 LLVMContext &Ctx = getGlobalContext(); 24 StringRef Source = "; Empty module \n"; 25 SMDiagnostic Error; 26 auto Mod = parseAssemblyString(Source, Error, Ctx); 27 28 EXPECT_TRUE(Mod != nullptr); 29 EXPECT_TRUE(Error.getMessage().empty()); 30 } 31 32 #ifdef GTEST_HAS_DEATH_TEST 33 #ifndef NDEBUG 34 35 TEST(AsmParserTest, NonNullTerminatedInput) { 36 LLVMContext &Ctx = getGlobalContext(); 37 StringRef Source = "; Empty module \n\1\2"; 38 SMDiagnostic Error; 39 std::unique_ptr<Module> Mod; 40 EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2), 41 Error, Ctx), 42 "Buffer is not null terminated!"); 43 } 44 45 #endif 46 #endif 47 48 TEST(AsmParserTest, SlotMappingTest) { 49 LLVMContext &Ctx = getGlobalContext(); 50 StringRef Source = "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}"; 51 SMDiagnostic Error; 52 SlotMapping Mapping; 53 auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping); 54 55 EXPECT_TRUE(Mod != nullptr); 56 EXPECT_TRUE(Error.getMessage().empty()); 57 58 ASSERT_EQ(Mapping.GlobalValues.size(), 1u); 59 EXPECT_TRUE(isa<GlobalVariable>(Mapping.GlobalValues[0])); 60 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); 65 } 66 67 } // end anonymous namespace 68