blob: 9c2081fa2f2d27d727af1fee04289040dc749c5c [file] [log] [blame]
Alex Lorenzc6277792015-05-20 20:41:27 +00001//===- 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"
Alex Lorenz8955f7d2015-06-23 17:10:10 +000012#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenzc6277792015-05-20 20:41:27 +000013#include "llvm/IR/LLVMContext.h"
14#include "llvm/IR/Module.h"
15#include "llvm/Support/SourceMgr.h"
16#include "gtest/gtest.h"
17
18using namespace llvm;
19
20namespace {
21
22TEST(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
35TEST(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
Alex Lorenz8955f7d2015-06-23 17:10:10 +000048TEST(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
Alex Lorenzc6277792015-05-20 20:41:27 +000067} // end anonymous namespace