Split parseAssembly into parseAssembly and parseAssemblyInto.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 19 Aug 2014 22:05:47 +0000 (22:05 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 19 Aug 2014 22:05:47 +0000 (22:05 +0000)
This should restore the functionality of parsing new code into an existing
module without the confusing interface.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216031 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/AsmParser/Parser.h
lib/AsmParser/Parser.cpp

index ab440cc47c77118d415d3583fb2d1352c3561660..c362a1774399f98d2a7f84b1dfd5f82f33701b5e 100644 (file)
@@ -50,8 +50,7 @@ std::unique_ptr<Module> 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.
+/// parseAssemblyFile and parseAssemblyString are wrappers around this function.
 /// @brief Parse LLVM Assembly from a MemoryBuffer.
 /// @param F The MemoryBuffer containing assembly
 /// @param Err Error result info.
@@ -59,6 +58,18 @@ std::unique_ptr<Module> parseAssemblyString(StringRef AsmString,
 std::unique_ptr<Module> parseAssembly(std::unique_ptr<MemoryBuffer> F,
                                       SMDiagnostic &Err, LLVMContext &Context);
 
+/// This function is the low-level interface to the LLVM Assembly Parser.
+/// This is kept as an independent function instead of being inlined into
+/// parseAssembly for the convenience of interactive users that want to add
+/// recently parsed bits to an existing module.
+///
+/// @param F The MemoryBuffer containing assembly
+/// @param M The module to add data to.
+/// @param Err Error result info.
+/// @return true on error.
+bool parseAssemblyInto(std::unique_ptr<MemoryBuffer> F, Module &M,
+                       SMDiagnostic &Err);
+
 } // End llvm namespace
 
 #endif
index 7c6598106ece07fdb9ebfc12b6e0d4b00676a379..9bc9b241666e1f83a84c46bdf88badbe881ab159 100644 (file)
 #include <system_error>
 using namespace llvm;
 
-std::unique_ptr<Module> llvm::parseAssembly(std::unique_ptr<MemoryBuffer> F,
-                                            SMDiagnostic &Err,
-                                            LLVMContext &Context) {
+bool llvm::parseAssemblyInto(std::unique_ptr<MemoryBuffer> F, Module &M,
+                             SMDiagnostic &Err) {
   SourceMgr SM;
-  MemoryBuffer *Buf = F.get();
+  StringRef Buf = F->getBuffer();
   SM.AddNewSourceBuffer(F.release(), SMLoc());
 
+  return LLParser(Buf, SM, Err, &M).Run();
+}
+
+std::unique_ptr<Module> llvm::parseAssembly(std::unique_ptr<MemoryBuffer> F,
+                                            SMDiagnostic &Err,
+                                            LLVMContext &Context) {
   std::unique_ptr<Module> M =
-      make_unique<Module>(Buf->getBufferIdentifier(), Context);
-  if (LLParser(Buf->getBuffer(), SM, Err, M.get()).Run())
+      make_unique<Module>(F->getBufferIdentifier(), Context);
+
+  if (parseAssemblyInto(std::move(F), *M, Err))
     return nullptr;
+
   return std::move(M);
 }