AsmParser: Require a terminating null character when creating memory buffer.
[oota-llvm.git] / unittests / AsmParser / AsmParserTest.cpp
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/IR/LLVMContext.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18
19 namespace {
20
21 TEST(AsmParserTest, NullTerminatedInput) {
22   LLVMContext &Ctx = getGlobalContext();
23   StringRef Source = "; Empty module \n";
24   SMDiagnostic Error;
25   auto Mod = parseAssemblyString(Source, Error, Ctx);
26
27   EXPECT_TRUE(Mod != nullptr);
28   EXPECT_TRUE(Error.getMessage().empty());
29 }
30
31 #ifdef GTEST_HAS_DEATH_TEST
32 #ifndef NDEBUG
33
34 TEST(AsmParserTest, NonNullTerminatedInput) {
35   LLVMContext &Ctx = getGlobalContext();
36   StringRef Source = "; Empty module \n\1\2";
37   SMDiagnostic Error;
38   std::unique_ptr<Module> Mod;
39   EXPECT_DEATH(Mod = parseAssemblyString(Source.substr(0, Source.size() - 2),
40                                          Error, Ctx),
41                "Buffer is not null terminated!");
42 }
43
44 #endif
45 #endif
46
47 } // end anonymous namespace