2
0

AsmParserTest.cpp 1.9 KB

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