Home | History | Annotate | Download | only in AsmParser
      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/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"
     18 
     19 using namespace llvm;
     20 
     21 namespace {
     22 
     23 TEST(AsmParserTest, NullTerminatedInput) {
     24   LLVMContext &Ctx = getGlobalContext();
     25   StringRef Source = "; Empty module \n";
     26   SMDiagnostic Error;
     27   auto Mod = parseAssemblyString(Source, Error, Ctx);
     28 
     29   EXPECT_TRUE(Mod != nullptr);
     30   EXPECT_TRUE(Error.getMessage().empty());
     31 }
     32 
     33 #ifdef GTEST_HAS_DEATH_TEST
     34 #ifndef NDEBUG
     35 
     36 TEST(AsmParserTest, NonNullTerminatedInput) {
     37   LLVMContext &Ctx = getGlobalContext();
     38   StringRef Source = "; Empty module \n\1\2";
     39   SMDiagnostic Error;
     40   std::unique_ptr<Module> Mod;
     41   EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2),
     42                                          Error, Ctx),
     43                "Buffer is not null terminated!");
     44 }
     45 
     46 #endif
     47 #endif
     48 
     49 TEST(AsmParserTest, SlotMappingTest) {
     50   LLVMContext &Ctx = getGlobalContext();
     51   StringRef Source = "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}";
     52   SMDiagnostic Error;
     53   SlotMapping Mapping;
     54   auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
     55 
     56   EXPECT_TRUE(Mod != nullptr);
     57   EXPECT_TRUE(Error.getMessage().empty());
     58 
     59   ASSERT_EQ(Mapping.GlobalValues.size(), 1u);
     60   EXPECT_TRUE(isa<GlobalVariable>(Mapping.GlobalValues[0]));
     61 
     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);
     66 }
     67 
     68 TEST(AsmParserTest, TypeAndConstantValueParsing) {
     69   LLVMContext &Ctx = getGlobalContext();
     70   SMDiagnostic Error;
     71   StringRef Source = "define void @test() {\n  entry:\n  ret void\n}";
     72   auto Mod = parseAssemblyString(Source, Error, Ctx);
     73   ASSERT_TRUE(Mod != nullptr);
     74   auto &M = *Mod;
     75 
     76   const Value *V;
     77   V = parseConstantValue("double 3.5", Error, M);
     78   ASSERT_TRUE(V);
     79   EXPECT_TRUE(V->getType()->isDoubleTy());
     80   ASSERT_TRUE(isa<ConstantFP>(V));
     81   EXPECT_TRUE(cast<ConstantFP>(V)->isExactlyValue(3.5));
     82 
     83   V = parseConstantValue("i32 42", Error, M);
     84   ASSERT_TRUE(V);
     85   EXPECT_TRUE(V->getType()->isIntegerTy());
     86   ASSERT_TRUE(isa<ConstantInt>(V));
     87   EXPECT_TRUE(cast<ConstantInt>(V)->equalsInt(42));
     88 
     89   V = parseConstantValue("<4 x i32> <i32 0, i32 1, i32 2, i32 3>", Error, M);
     90   ASSERT_TRUE(V);
     91   EXPECT_TRUE(V->getType()->isVectorTy());
     92   ASSERT_TRUE(isa<ConstantDataVector>(V));
     93 
     94   V = parseConstantValue("i32 add (i32 1, i32 2)", Error, M);
     95   ASSERT_TRUE(V);
     96   ASSERT_TRUE(isa<ConstantInt>(V));
     97 
     98   V = parseConstantValue("i8* blockaddress(@test, %entry)", Error, M);
     99   ASSERT_TRUE(V);
    100   ASSERT_TRUE(isa<BlockAddress>(V));
    101 
    102   V = parseConstantValue("i8** undef", Error, M);
    103   ASSERT_TRUE(V);
    104   ASSERT_TRUE(isa<UndefValue>(V));
    105 
    106   EXPECT_FALSE(parseConstantValue("duble 3.25", Error, M));
    107   EXPECT_EQ(Error.getMessage(), "expected type");
    108 
    109   EXPECT_FALSE(parseConstantValue("i32 3.25", Error, M));
    110   EXPECT_EQ(Error.getMessage(), "floating point constant invalid for type");
    111 
    112   EXPECT_FALSE(parseConstantValue("i32* @foo", Error, M));
    113   EXPECT_EQ(Error.getMessage(), "expected a constant value");
    114 
    115   EXPECT_FALSE(parseConstantValue("i32 3, ", Error, M));
    116   EXPECT_EQ(Error.getMessage(), "expected end of string");
    117 }
    118 
    119 TEST(AsmParserTest, TypeAndConstantValueWithSlotMappingParsing) {
    120   LLVMContext &Ctx = getGlobalContext();
    121   SMDiagnostic Error;
    122   StringRef Source =
    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"
    128       "entry:\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"
    134       "  ret void\n"
    135       "}";
    136   SlotMapping Mapping;
    137   auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
    138   ASSERT_TRUE(Mod != nullptr);
    139   auto &M = *Mod;
    140 
    141   const Value *V;
    142   V = parseConstantValue("i32* getelementptr inbounds ([50 x %st], [50 x %st]* "
    143                          "@v, i64 0, i64 0, i32 0)",
    144                          Error, M, &Mapping);
    145   ASSERT_TRUE(V);
    146   ASSERT_TRUE(isa<ConstantExpr>(V));
    147 
    148   V = parseConstantValue("i32* getelementptr inbounds ([50 x %0], [50 x %0]* "
    149                          "@g, i64 0, i64 0, i32 0)",
    150                          Error, M, &Mapping);
    151   ASSERT_TRUE(V);
    152   ASSERT_TRUE(isa<ConstantExpr>(V));
    153 }
    154 
    155 } // end anonymous namespace
    156