llvm.org GIT mirror llvm / 9b29ff9
Modernize the .ll parsing interface. * Use StringRef instead of std::string& * Return a std::unique_ptr<Module> instead of taking an optional module to write to (was not really used). * Use current comment style. * Use current naming convention. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215989 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
15 changed file(s) with 71 addition(s) and 82 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_ASMPARSER_PARSER_H
1414 #define LLVM_ASMPARSER_PARSER_H
1515
16 #include "llvm/ADT/StringRef.h"
1617 #include
17 #include
1818
1919 namespace llvm {
2020
2929 /// that this does not verify that the generated Module is valid, so you should
3030 /// run the verifier after parsing the file to check that it is okay.
3131 /// @brief Parse LLVM Assembly from a file
32 Module *ParseAssemblyFile(
33 const std::string &Filename, ///< The name of the file to parse
34 SMDiagnostic &Error, ///< Error result info.
35 LLVMContext &Context ///< Context in which to allocate globals info.
36 );
32 /// @param Filename The name of the file to parse
33 /// @param Error Error result info.
34 /// @param Context Context in which to allocate globals info.
35 std::unique_ptr parseAssemblyFile(StringRef Filename,
36 SMDiagnostic &Error,
37 LLVMContext &Context);
3738
3839 /// The function is a secondary interface to the LLVM Assembly Parser. It parses
3940 /// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
4142 /// that this does not verify that the generated Module is valid, so you should
4243 /// run the verifier after parsing the file to check that it is okay.
4344 /// @brief Parse LLVM Assembly from a string
44 Module *ParseAssemblyString(
45 const char *AsmString, ///< The string containing assembly
46 Module *M, ///< A module to add the assembly too.
47 SMDiagnostic &Error, ///< Error result info.
48 LLVMContext &Context
49 );
45 /// @param AsmString The string containing assembly
46 /// @param Error Error result info.
47 /// @param Context Context in which to allocate globals info.
48 std::unique_ptr parseAssemblyString(StringRef AsmString,
49 SMDiagnostic &Error,
50 LLVMContext &Context);
5051
5152 /// This function is the low-level interface to the LLVM Assembly Parser.
5253 /// ParseAssemblyFile and ParseAssemblyString are wrappers around this function.
5354 /// @brief Parse LLVM Assembly from a MemoryBuffer.
54 Module *ParseAssembly(
55 std::unique_ptr F, ///< The MemoryBuffer containing assembly
56 Module *M, ///< A module to add the assembly too.
57 SMDiagnostic &Err, ///< Error result info.
58 LLVMContext &Context);
55 /// @param F The MemoryBuffer containing assembly
56 /// @param Err Error result info.
57 /// @param Context Context in which to allocate globals info.
58 std::unique_ptr parseAssembly(std::unique_ptr F,
59 SMDiagnostic &Err, LLVMContext &Context);
5960
6061 } // End llvm namespace
6162
2020 #include
2121 using namespace llvm;
2222
23 Module *llvm::ParseAssembly(std::unique_ptr F, Module *M,
24 SMDiagnostic &Err, LLVMContext &Context) {
23 std::unique_ptr llvm::parseAssembly(std::unique_ptr F,
24 SMDiagnostic &Err,
25 LLVMContext &Context) {
2526 SourceMgr SM;
2627 MemoryBuffer *Buf = F.get();
2728 SM.AddNewSourceBuffer(F.release(), SMLoc());
2829
29 // If we are parsing into an existing module, do it.
30 if (M)
31 return LLParser(Buf->getBuffer(), SM, Err, M).Run() ? nullptr : M;
32
33 // Otherwise create a new module.
34 std::unique_ptr M2(new Module(Buf->getBufferIdentifier(), Context));
35 if (LLParser(Buf->getBuffer(), SM, Err, M2.get()).Run())
30 std::unique_ptr M =
31 make_unique(Buf->getBufferIdentifier(), Context);
32 if (LLParser(Buf->getBuffer(), SM, Err, M.get()).Run())
3633 return nullptr;
37 return M2.release();
34 return std::move(M);
3835 }
3936
40 Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
41 LLVMContext &Context) {
37 std::unique_ptr llvm::parseAssemblyFile(StringRef Filename,
38 SMDiagnostic &Err,
39 LLVMContext &Context) {
4240 ErrorOr> FileOrErr =
4341 MemoryBuffer::getFileOrSTDIN(Filename);
4442 if (std::error_code EC = FileOrErr.getError()) {
4745 return nullptr;
4846 }
4947
50 return ParseAssembly(std::move(FileOrErr.get()), nullptr, Err, Context);
48 return parseAssembly(std::move(FileOrErr.get()), Err, Context);
5149 }
5250
53 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
54 SMDiagnostic &Err, LLVMContext &Context) {
55 MemoryBuffer *F =
56 MemoryBuffer::getMemBuffer(StringRef(AsmString), "");
51 std::unique_ptr llvm::parseAssemblyString(StringRef AsmString,
52 SMDiagnostic &Err,
53 LLVMContext &Context) {
54 std::unique_ptr F(
55 MemoryBuffer::getMemBuffer(AsmString, ""));
5756
58 return ParseAssembly(std::unique_ptr(F), M, Err, Context);
57 return parseAssembly(std::move(F), Err, Context);
5958 }
2828 static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
2929 static const char *const TimeIRParsingName = "Parse IR";
3030
31 static Module *getLazyIRModule(std::unique_ptr Buffer,
32 SMDiagnostic &Err, LLVMContext &Context) {
31 static std::unique_ptr
32 getLazyIRModule(std::unique_ptr Buffer, SMDiagnostic &Err,
33 LLVMContext &Context) {
3334 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
3435 (const unsigned char *)Buffer->getBufferEnd())) {
3536 std::string ErrMsg;
4142 }
4243 // getLazyBitcodeModule takes ownership of the Buffer when successful.
4344 Buffer.release();
44 return ModuleOrErr.get();
45 return std::unique_ptr(ModuleOrErr.get());
4546 }
4647
47 return ParseAssembly(std::move(Buffer), nullptr, Err, Context);
48 return parseAssembly(std::move(Buffer), Err, Context);
4849 }
4950
5051 Module *llvm::getLazyIRFileModule(const std::string &Filename,
5758 return nullptr;
5859 }
5960
60 return getLazyIRModule(std::move(FileOrErr.get()), Err, Context);
61 return getLazyIRModule(std::move(FileOrErr.get()), Err, Context).release();
6162 }
6263
6364 Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
7778 return M;
7879 }
7980
80 return ParseAssembly(std::unique_ptr(MemoryBuffer::getMemBuffer(
81 return parseAssembly(std::unique_ptr(MemoryBuffer::getMemBuffer(
8182 Buffer->getBuffer(), Buffer->getBufferIdentifier())),
82 nullptr, Err, Context);
83 Err, Context).release();
8384 }
8485
8586 Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
9393
9494 // Parse the file now...
9595 SMDiagnostic Err;
96 std::unique_ptr M(ParseAssemblyFile(InputFilename, Err, Context));
96 std::unique_ptr M = parseAssemblyFile(InputFilename, Err, Context);
9797 if (!M.get()) {
9898 Err.print(argv[0], errs());
9999 return 1;
167167 std::unique_ptr TempFile::readAssembly(LLVMContext &Context) const {
168168 DEBUG(dbgs() << " - read assembly\n");
169169 SMDiagnostic Err;
170 std::unique_ptr M(ParseAssemblyFile(Filename, Err, Context));
170 std::unique_ptr M = parseAssemblyFile(Filename, Err, Context);
171171 if (!M.get())
172172 DEBUG(dbgs() << "error: "; Err.print("verify-use-list-order", dbgs()));
173173 return M;
2929 class IsPotentiallyReachableTest : public testing::Test {
3030 protected:
3131 void ParseAssembly(const char *Assembly) {
32 M.reset(new Module("Module", getGlobalContext()));
33
3432 SMDiagnostic Error;
35 bool Parsed = ParseAssemblyString(Assembly, M.get(),
36 Error, M->getContext()) == M.get();
33 M = parseAssemblyString(Assembly, Error, getGlobalContext());
3734
3835 std::string errMsg;
3936 raw_string_ostream os(errMsg);
4037 Error.print("", os);
4138
42 if (!Parsed) {
43 // A failure here means that the test itself is buggy.
39 // A failure here means that the test itself is buggy.
40 if (!M)
4441 report_fatal_error(os.str().c_str());
45 }
4642
4743 Function *F = M->getFunction("test");
4844 if (F == nullptr)
2121 namespace {
2222
2323 std::unique_ptr parseAssembly(const char *Assembly) {
24 auto M = make_unique("Module", getGlobalContext());
25
2624 SMDiagnostic Error;
27 bool Parsed =
28 ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get();
25 std::unique_ptr M =
26 parseAssemblyString(Assembly, Error, getGlobalContext());
2927
3028 std::string ErrMsg;
3129 raw_string_ostream OS(ErrMsg);
3230 Error.print("", OS);
3331
3432 // A failure here means that the test itself is buggy.
35 if (!Parsed)
33 if (!M)
3634 report_fatal_error(OS.str().c_str());
3735
3836 return M;
2525 namespace {
2626
2727 std::unique_ptr parseAssembly(const char *Assembly) {
28 auto M = make_unique("Module", getGlobalContext());
29
3028 SMDiagnostic Error;
31 bool Parsed =
32 ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get();
29 std::unique_ptr M =
30 parseAssemblyString(Assembly, Error, getGlobalContext());
3331
3432 std::string ErrMsg;
3533 raw_string_ostream OS(ErrMsg);
3634 Error.print("", OS);
3735
3836 // A failure here means that the test itself is buggy.
39 if (!Parsed)
37 if (!M)
4038 report_fatal_error(OS.str().c_str());
4139
4240 return M;
165165 }
166166 };
167167
168 bool LoadAssemblyInto(Module *M, const char *assembly) {
168 std::unique_ptr loadAssembly(LLVMContext &C, const char *Assembly) {
169169 SMDiagnostic Error;
170 bool success =
171 nullptr != ParseAssemblyString(assembly, M, Error, M->getContext());
170 std::unique_ptr M = parseAssemblyString(Assembly, Error, C);
172171 std::string errMsg;
173172 raw_string_ostream os(errMsg);
174173 Error.print("", os);
175 EXPECT_TRUE(success) << os.str();
176 return success;
174 EXPECT_TRUE((bool)M) << os.str();
175 return M;
177176 }
178177
179178 class JITTest : public testing::Test {
199198 }
200199
201200 void LoadAssembly(const char *assembly) {
202 LoadAssemblyInto(M, assembly);
201 M = loadAssembly(Context, assembly).release();
203202 }
204203
205204 LLVMContext Context;
614613 // Converts the LLVM assembly to bitcode and returns it in a std::string. An
615614 // empty string indicates an error.
616615 std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) {
617 Module TempModule("TempModule", Context);
618 if (!LoadAssemblyInto(&TempModule, Assembly)) {
616 std::unique_ptr TempModule = loadAssembly(Context, Assembly);
617 if (!TempModule)
619618 return "";
620 }
621619
622620 std::string Result;
623621 raw_string_ostream OS(Result);
624 WriteBitcodeToFile(&TempModule, OS);
622 WriteBitcodeToFile(TempModule.get(), OS);
625623 OS.flush();
626624 return Result;
627625 }
2626 std::unique_ptr loadAssembly(LLVMContext &Context,
2727 const char *Assembly) {
2828 SMDiagnostic Error;
29 std::unique_ptr Ret(
30 ParseAssemblyString(Assembly, nullptr, Error, Context));
29 std::unique_ptr Ret = parseAssemblyString(Assembly, Error, Context);
3130 std::string errMsg;
3231 raw_string_ostream os(errMsg);
3332 Error.print("", os);
185185 };
186186 char DPass::ID = 0;
187187
188
189 Module* makeLLVMModule(DPass *P) {
188 std::unique_ptr makeLLVMModule(DPass *P) {
190189 const char *ModuleStrig =
191190 "declare i32 @g()\n" \
192191 "define void @f(i32 %x) {\n" \
212211 "}\n";
213212 LLVMContext &C = getGlobalContext();
214213 SMDiagnostic Err;
215 return ParseAssemblyString(ModuleStrig, nullptr, Err, C);
214 return parseAssemblyString(ModuleStrig, Err, C);
216215 }
217216
218217 TEST(DominatorTree, Unreachable) {
219218 DPass *P = new DPass();
220 std::unique_ptr M(makeLLVMModule(P));
219 std::unique_ptr M = makeLLVMModule(P);
221220 PassManager Passes;
222221 Passes.add(P);
223222 Passes.run(*M);
167167 Module *parseIR(const char *IR) {
168168 LLVMContext &C = getGlobalContext();
169169 SMDiagnostic Err;
170 return ParseAssemblyString(IR, nullptr, Err, C);
170 return parseAssemblyString(IR, Err, C).release();
171171 }
172172
173173 class PassManagerTest : public ::testing::Test {
3737 "}\n";
3838 SMDiagnostic Err;
3939 char vnbuf[8];
40 Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
40 std::unique_ptr M = parseAssemblyString(ModuleString, Err, C);
4141 Function *F = M->getFunction("f");
4242 ASSERT_TRUE(F);
4343 ASSERT_TRUE(F->arg_begin() != F->arg_end());
8282 "}\n";
8383 SMDiagnostic Err;
8484 char vnbuf[8];
85 Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
85 std::unique_ptr M = parseAssemblyString(ModuleString, Err, C);
8686 Function *F = M->getFunction("f");
8787 ASSERT_TRUE(F);
8888 ASSERT_TRUE(F->arg_begin() != F->arg_end());
6464 " ret void\n"
6565 "}\n";
6666 SMDiagnostic Err;
67 Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
67 std::unique_ptr M = parseAssemblyString(ModuleString, Err, C);
6868
6969 Function *F = M->getFunction("f");
7070 BasicBlock &ExitBB = F->back();
3333 " ret void\n"
3434 "}\n";
3535 SMDiagnostic Err;
36 Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
36 std::unique_ptr M = parseAssemblyString(ModuleString, Err, C);
3737
3838 Function *F = M->getFunction("f");
3939