From: Rafael Espindola Date: Tue, 19 Aug 2014 16:58:54 +0000 (+0000) Subject: Modernize the .ll parsing interface. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=9b29ff99c0443ec0c038adafc8d5c782a8624e17;p=oota-llvm.git Modernize the .ll parsing interface. * Use StringRef instead of std::string& * Return a std::unique_ptr 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 --- diff --git a/include/llvm/AsmParser/Parser.h b/include/llvm/AsmParser/Parser.h index 5a75308c770..ab440cc47c7 100644 --- a/include/llvm/AsmParser/Parser.h +++ b/include/llvm/AsmParser/Parser.h @@ -14,8 +14,8 @@ #ifndef LLVM_ASMPARSER_PARSER_H #define LLVM_ASMPARSER_PARSER_H +#include "llvm/ADT/StringRef.h" #include -#include namespace llvm { @@ -30,11 +30,12 @@ class LLVMContext; /// that this does not verify that the generated Module is valid, so you should /// run the verifier after parsing the file to check that it is okay. /// @brief Parse LLVM Assembly from a file -Module *ParseAssemblyFile( - const std::string &Filename, ///< The name of the file to parse - SMDiagnostic &Error, ///< Error result info. - LLVMContext &Context ///< Context in which to allocate globals info. -); +/// @param Filename The name of the file to parse +/// @param Error Error result info. +/// @param Context Context in which to allocate globals info. +std::unique_ptr parseAssemblyFile(StringRef Filename, + SMDiagnostic &Error, + LLVMContext &Context); /// The function is a secondary interface to the LLVM Assembly Parser. It parses /// an ASCII string that (presumably) contains LLVM Assembly code. It returns a @@ -42,21 +43,21 @@ Module *ParseAssemblyFile( /// that this does not verify that the generated Module is valid, so you should /// run the verifier after parsing the file to check that it is okay. /// @brief Parse LLVM Assembly from a string -Module *ParseAssemblyString( - const char *AsmString, ///< The string containing assembly - Module *M, ///< A module to add the assembly too. - SMDiagnostic &Error, ///< Error result info. - LLVMContext &Context -); +/// @param AsmString The string containing assembly +/// @param Error Error result info. +/// @param Context Context in which to allocate globals info. +std::unique_ptr parseAssemblyString(StringRef AsmString, + SMDiagnostic &Error, + LLVMContext &Context); /// This function is the low-level interface to the LLVM Assembly Parser. /// ParseAssemblyFile and ParseAssemblyString are wrappers around this function. /// @brief Parse LLVM Assembly from a MemoryBuffer. -Module *ParseAssembly( - std::unique_ptr F, ///< The MemoryBuffer containing assembly - Module *M, ///< A module to add the assembly too. - SMDiagnostic &Err, ///< Error result info. - LLVMContext &Context); +/// @param F The MemoryBuffer containing assembly +/// @param Err Error result info. +/// @param Context Context in which to allocate globals info. +std::unique_ptr parseAssembly(std::unique_ptr F, + SMDiagnostic &Err, LLVMContext &Context); } // End llvm namespace diff --git a/lib/AsmParser/Parser.cpp b/lib/AsmParser/Parser.cpp index 6cae013b129..7c6598106ec 100644 --- a/lib/AsmParser/Parser.cpp +++ b/lib/AsmParser/Parser.cpp @@ -21,25 +21,23 @@ #include using namespace llvm; -Module *llvm::ParseAssembly(std::unique_ptr F, Module *M, - SMDiagnostic &Err, LLVMContext &Context) { +std::unique_ptr llvm::parseAssembly(std::unique_ptr F, + SMDiagnostic &Err, + LLVMContext &Context) { SourceMgr SM; MemoryBuffer *Buf = F.get(); SM.AddNewSourceBuffer(F.release(), SMLoc()); - // If we are parsing into an existing module, do it. - if (M) - return LLParser(Buf->getBuffer(), SM, Err, M).Run() ? nullptr : M; - - // Otherwise create a new module. - std::unique_ptr M2(new Module(Buf->getBufferIdentifier(), Context)); - if (LLParser(Buf->getBuffer(), SM, Err, M2.get()).Run()) + std::unique_ptr M = + make_unique(Buf->getBufferIdentifier(), Context); + if (LLParser(Buf->getBuffer(), SM, Err, M.get()).Run()) return nullptr; - return M2.release(); + return std::move(M); } -Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err, - LLVMContext &Context) { +std::unique_ptr llvm::parseAssemblyFile(StringRef Filename, + SMDiagnostic &Err, + LLVMContext &Context) { ErrorOr> FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename); if (std::error_code EC = FileOrErr.getError()) { @@ -48,13 +46,14 @@ Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err, return nullptr; } - return ParseAssembly(std::move(FileOrErr.get()), nullptr, Err, Context); + return parseAssembly(std::move(FileOrErr.get()), Err, Context); } -Module *llvm::ParseAssemblyString(const char *AsmString, Module *M, - SMDiagnostic &Err, LLVMContext &Context) { - MemoryBuffer *F = - MemoryBuffer::getMemBuffer(StringRef(AsmString), ""); +std::unique_ptr llvm::parseAssemblyString(StringRef AsmString, + SMDiagnostic &Err, + LLVMContext &Context) { + std::unique_ptr F( + MemoryBuffer::getMemBuffer(AsmString, "")); - return ParseAssembly(std::unique_ptr(F), M, Err, Context); + return parseAssembly(std::move(F), Err, Context); } diff --git a/lib/IRReader/IRReader.cpp b/lib/IRReader/IRReader.cpp index aa7312ac041..22a7493b70f 100644 --- a/lib/IRReader/IRReader.cpp +++ b/lib/IRReader/IRReader.cpp @@ -29,8 +29,9 @@ namespace llvm { static const char *const TimeIRParsingGroupName = "LLVM IR Parsing"; static const char *const TimeIRParsingName = "Parse IR"; -static Module *getLazyIRModule(std::unique_ptr Buffer, - SMDiagnostic &Err, LLVMContext &Context) { +static std::unique_ptr +getLazyIRModule(std::unique_ptr Buffer, SMDiagnostic &Err, + LLVMContext &Context) { if (isBitcode((const unsigned char *)Buffer->getBufferStart(), (const unsigned char *)Buffer->getBufferEnd())) { std::string ErrMsg; @@ -42,10 +43,10 @@ static Module *getLazyIRModule(std::unique_ptr Buffer, } // getLazyBitcodeModule takes ownership of the Buffer when successful. Buffer.release(); - return ModuleOrErr.get(); + return std::unique_ptr(ModuleOrErr.get()); } - return ParseAssembly(std::move(Buffer), nullptr, Err, Context); + return parseAssembly(std::move(Buffer), Err, Context); } Module *llvm::getLazyIRFileModule(const std::string &Filename, @@ -58,7 +59,7 @@ Module *llvm::getLazyIRFileModule(const std::string &Filename, return nullptr; } - return getLazyIRModule(std::move(FileOrErr.get()), Err, Context); + return getLazyIRModule(std::move(FileOrErr.get()), Err, Context).release(); } Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, @@ -78,9 +79,9 @@ Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, return M; } - return ParseAssembly(std::unique_ptr(MemoryBuffer::getMemBuffer( + return parseAssembly(std::unique_ptr(MemoryBuffer::getMemBuffer( Buffer->getBuffer(), Buffer->getBufferIdentifier())), - nullptr, Err, Context); + Err, Context).release(); } Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err, diff --git a/tools/llvm-as/llvm-as.cpp b/tools/llvm-as/llvm-as.cpp index 007241c5068..ef4f7ba272a 100644 --- a/tools/llvm-as/llvm-as.cpp +++ b/tools/llvm-as/llvm-as.cpp @@ -94,7 +94,7 @@ int main(int argc, char **argv) { // Parse the file now... SMDiagnostic Err; - std::unique_ptr M(ParseAssemblyFile(InputFilename, Err, Context)); + std::unique_ptr M = parseAssemblyFile(InputFilename, Err, Context); if (!M.get()) { Err.print(argv[0], errs()); return 1; diff --git a/tools/verify-uselistorder/verify-uselistorder.cpp b/tools/verify-uselistorder/verify-uselistorder.cpp index 0bb9b0dce02..6a374279f8c 100644 --- a/tools/verify-uselistorder/verify-uselistorder.cpp +++ b/tools/verify-uselistorder/verify-uselistorder.cpp @@ -168,7 +168,7 @@ std::unique_ptr TempFile::readBitcode(LLVMContext &Context) const { std::unique_ptr TempFile::readAssembly(LLVMContext &Context) const { DEBUG(dbgs() << " - read assembly\n"); SMDiagnostic Err; - std::unique_ptr M(ParseAssemblyFile(Filename, Err, Context)); + std::unique_ptr M = parseAssemblyFile(Filename, Err, Context); if (!M.get()) DEBUG(dbgs() << "error: "; Err.print("verify-use-list-order", dbgs())); return M; diff --git a/unittests/Analysis/CFGTest.cpp b/unittests/Analysis/CFGTest.cpp index ac5e71061d8..dba9d4991ec 100644 --- a/unittests/Analysis/CFGTest.cpp +++ b/unittests/Analysis/CFGTest.cpp @@ -30,20 +30,16 @@ namespace { class IsPotentiallyReachableTest : public testing::Test { protected: void ParseAssembly(const char *Assembly) { - M.reset(new Module("Module", getGlobalContext())); - SMDiagnostic Error; - bool Parsed = ParseAssemblyString(Assembly, M.get(), - Error, M->getContext()) == M.get(); + M = parseAssemblyString(Assembly, Error, getGlobalContext()); std::string errMsg; raw_string_ostream os(errMsg); Error.print("", os); - if (!Parsed) { - // A failure here means that the test itself is buggy. + // A failure here means that the test itself is buggy. + if (!M) report_fatal_error(os.str().c_str()); - } Function *F = M->getFunction("test"); if (F == nullptr) diff --git a/unittests/Analysis/LazyCallGraphTest.cpp b/unittests/Analysis/LazyCallGraphTest.cpp index d7c70453c9b..5f73d83f092 100644 --- a/unittests/Analysis/LazyCallGraphTest.cpp +++ b/unittests/Analysis/LazyCallGraphTest.cpp @@ -22,18 +22,16 @@ using namespace llvm; namespace { std::unique_ptr parseAssembly(const char *Assembly) { - auto M = make_unique("Module", getGlobalContext()); - SMDiagnostic Error; - bool Parsed = - ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get(); + std::unique_ptr M = + parseAssemblyString(Assembly, Error, getGlobalContext()); std::string ErrMsg; raw_string_ostream OS(ErrMsg); Error.print("", OS); // A failure here means that the test itself is buggy. - if (!Parsed) + if (!M) report_fatal_error(OS.str().c_str()); return M; diff --git a/unittests/Bitcode/BitReaderTest.cpp b/unittests/Bitcode/BitReaderTest.cpp index 8331527437b..adc8851f279 100644 --- a/unittests/Bitcode/BitReaderTest.cpp +++ b/unittests/Bitcode/BitReaderTest.cpp @@ -26,18 +26,16 @@ using namespace llvm; namespace { std::unique_ptr parseAssembly(const char *Assembly) { - auto M = make_unique("Module", getGlobalContext()); - SMDiagnostic Error; - bool Parsed = - ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get(); + std::unique_ptr M = + parseAssemblyString(Assembly, Error, getGlobalContext()); std::string ErrMsg; raw_string_ostream OS(ErrMsg); Error.print("", OS); // A failure here means that the test itself is buggy. - if (!Parsed) + if (!M) report_fatal_error(OS.str().c_str()); return M; diff --git a/unittests/ExecutionEngine/JIT/JITTest.cpp b/unittests/ExecutionEngine/JIT/JITTest.cpp index 6110cb59747..4ba54b1d486 100644 --- a/unittests/ExecutionEngine/JIT/JITTest.cpp +++ b/unittests/ExecutionEngine/JIT/JITTest.cpp @@ -166,15 +166,14 @@ public: } }; -bool LoadAssemblyInto(Module *M, const char *assembly) { +std::unique_ptr loadAssembly(LLVMContext &C, const char *Assembly) { SMDiagnostic Error; - bool success = - nullptr != ParseAssemblyString(assembly, M, Error, M->getContext()); + std::unique_ptr M = parseAssemblyString(Assembly, Error, C); std::string errMsg; raw_string_ostream os(errMsg); Error.print("", os); - EXPECT_TRUE(success) << os.str(); - return success; + EXPECT_TRUE((bool)M) << os.str(); + return M; } class JITTest : public testing::Test { @@ -200,7 +199,7 @@ class JITTest : public testing::Test { } void LoadAssembly(const char *assembly) { - LoadAssemblyInto(M, assembly); + M = loadAssembly(Context, assembly).release(); } LLVMContext Context; @@ -615,14 +614,13 @@ TEST_F(JITTest, EscapedLazyStubStillCallable) { // Converts the LLVM assembly to bitcode and returns it in a std::string. An // empty string indicates an error. std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) { - Module TempModule("TempModule", Context); - if (!LoadAssemblyInto(&TempModule, Assembly)) { + std::unique_ptr TempModule = loadAssembly(Context, Assembly); + if (!TempModule) return ""; - } std::string Result; raw_string_ostream OS(Result); - WriteBitcodeToFile(&TempModule, OS); + WriteBitcodeToFile(TempModule.get(), OS); OS.flush(); return Result; } diff --git a/unittests/ExecutionEngine/JIT/MultiJITTest.cpp b/unittests/ExecutionEngine/JIT/MultiJITTest.cpp index 73acc6b1186..ceed7e826ad 100644 --- a/unittests/ExecutionEngine/JIT/MultiJITTest.cpp +++ b/unittests/ExecutionEngine/JIT/MultiJITTest.cpp @@ -27,8 +27,7 @@ namespace { std::unique_ptr loadAssembly(LLVMContext &Context, const char *Assembly) { SMDiagnostic Error; - std::unique_ptr Ret( - ParseAssemblyString(Assembly, nullptr, Error, Context)); + std::unique_ptr Ret = parseAssemblyString(Assembly, Error, Context); std::string errMsg; raw_string_ostream os(errMsg); Error.print("", os); diff --git a/unittests/IR/DominatorTreeTest.cpp b/unittests/IR/DominatorTreeTest.cpp index ab43d1c91fc..6c43d6fe7c7 100644 --- a/unittests/IR/DominatorTreeTest.cpp +++ b/unittests/IR/DominatorTreeTest.cpp @@ -186,8 +186,7 @@ namespace llvm { }; char DPass::ID = 0; - - Module* makeLLVMModule(DPass *P) { + std::unique_ptr makeLLVMModule(DPass *P) { const char *ModuleStrig = "declare i32 @g()\n" \ "define void @f(i32 %x) {\n" \ @@ -213,12 +212,12 @@ namespace llvm { "}\n"; LLVMContext &C = getGlobalContext(); SMDiagnostic Err; - return ParseAssemblyString(ModuleStrig, nullptr, Err, C); + return parseAssemblyString(ModuleStrig, Err, C); } TEST(DominatorTree, Unreachable) { DPass *P = new DPass(); - std::unique_ptr M(makeLLVMModule(P)); + std::unique_ptr M = makeLLVMModule(P); PassManager Passes; Passes.add(P); Passes.run(*M); diff --git a/unittests/IR/PassManagerTest.cpp b/unittests/IR/PassManagerTest.cpp index 25037a773cf..d493156a343 100644 --- a/unittests/IR/PassManagerTest.cpp +++ b/unittests/IR/PassManagerTest.cpp @@ -168,7 +168,7 @@ struct TestInvalidationFunctionPass { Module *parseIR(const char *IR) { LLVMContext &C = getGlobalContext(); SMDiagnostic Err; - return ParseAssemblyString(IR, nullptr, Err, C); + return parseAssemblyString(IR, Err, C).release(); } class PassManagerTest : public ::testing::Test { diff --git a/unittests/IR/UseTest.cpp b/unittests/IR/UseTest.cpp index fa73fe77bd3..3f33ca6a368 100644 --- a/unittests/IR/UseTest.cpp +++ b/unittests/IR/UseTest.cpp @@ -38,7 +38,7 @@ TEST(UseTest, sort) { "}\n"; SMDiagnostic Err; char vnbuf[8]; - Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C); + std::unique_ptr M = parseAssemblyString(ModuleString, Err, C); Function *F = M->getFunction("f"); ASSERT_TRUE(F); ASSERT_TRUE(F->arg_begin() != F->arg_end()); @@ -83,7 +83,7 @@ TEST(UseTest, reverse) { "}\n"; SMDiagnostic Err; char vnbuf[8]; - Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C); + std::unique_ptr M = parseAssemblyString(ModuleString, Err, C); Function *F = M->getFunction("f"); ASSERT_TRUE(F); ASSERT_TRUE(F->arg_begin() != F->arg_end()); diff --git a/unittests/IR/UserTest.cpp b/unittests/IR/UserTest.cpp index eb07e824d8b..5572424d19c 100644 --- a/unittests/IR/UserTest.cpp +++ b/unittests/IR/UserTest.cpp @@ -65,7 +65,7 @@ TEST(UserTest, ValueOpIteration) { " ret void\n" "}\n"; SMDiagnostic Err; - Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C); + std::unique_ptr M = parseAssemblyString(ModuleString, Err, C); Function *F = M->getFunction("f"); BasicBlock &ExitBB = F->back(); diff --git a/unittests/IR/ValueTest.cpp b/unittests/IR/ValueTest.cpp index 61e44a908d4..dc5794b46d2 100644 --- a/unittests/IR/ValueTest.cpp +++ b/unittests/IR/ValueTest.cpp @@ -34,7 +34,7 @@ TEST(ValueTest, UsedInBasicBlock) { " ret void\n" "}\n"; SMDiagnostic Err; - Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C); + std::unique_ptr M = parseAssemblyString(ModuleString, Err, C); Function *F = M->getFunction("f");