[llvm-commits] CVS: llvm/lib/Bytecode/Reader/ReaderWrappers.cpp

Reid Spencer reid at x10sys.com
Fri Aug 25 10:43:34 PDT 2006



Changes in directory llvm/lib/Bytecode/Reader:

ReaderWrappers.cpp updated: 1.56 -> 1.57
---
Log message:

For PR797: http://llvm.org/PR797 :
Remove exception throwing/handling from lib/Bytecode, and adjust its users
to compensate for changes in the interface.


---
Diffs of the changes:  (+134 -103)

 ReaderWrappers.cpp |  237 +++++++++++++++++++++++++++++------------------------
 1 files changed, 134 insertions(+), 103 deletions(-)


Index: llvm/lib/Bytecode/Reader/ReaderWrappers.cpp
diff -u llvm/lib/Bytecode/Reader/ReaderWrappers.cpp:1.56 llvm/lib/Bytecode/Reader/ReaderWrappers.cpp:1.57
--- llvm/lib/Bytecode/Reader/ReaderWrappers.cpp:1.56	Tue Aug 22 11:10:12 2006
+++ llvm/lib/Bytecode/Reader/ReaderWrappers.cpp	Fri Aug 25 12:43:11 2006
@@ -35,6 +35,7 @@
   ///
   class BytecodeFileReader : public BytecodeReader {
   private:
+    std::string fileName;
     sys::MappedFile mapFile;
 
     BytecodeFileReader(const BytecodeFileReader&); // Do not implement
@@ -42,23 +43,30 @@
 
   public:
     BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
+    bool read(std::string* ErrMsg);
   };
 }
 
 BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
                                        llvm::BytecodeHandler* H )
   : BytecodeReader(H)
+  , fileName(Filename)
   , mapFile()
 {
-  std::string ErrMsg;
-  if (mapFile.open(sys::Path(Filename), sys::MappedFile::READ_ACCESS, &ErrMsg))
-    throw ErrMsg;
-  if (!mapFile.map(&ErrMsg))
-    throw ErrMsg;
+}
+
+bool BytecodeFileReader::read(std::string* ErrMsg) {
+  if (mapFile.open(sys::Path(fileName), sys::MappedFile::READ_ACCESS, ErrMsg))
+    return true;
+  if (!mapFile.map(ErrMsg)) {
+    mapFile.close();
+    return true;
+  }
   unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
-  if (ParseBytecode(buffer, mapFile.size(), Filename, &ErrMsg)) {
-    throw ErrMsg;
+  if (ParseBytecode(buffer, mapFile.size(), fileName, ErrMsg)) {
+    return true;
   }
+  return false;
 }
 
 //===----------------------------------------------------------------------===//
@@ -71,6 +79,9 @@
   class BytecodeBufferReader : public BytecodeReader {
   private:
     const unsigned char *Buffer;
+    const unsigned char *Buf;
+    unsigned Length;
+    std::string ModuleID;
     bool MustDelete;
 
     BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
@@ -82,15 +93,30 @@
                          llvm::BytecodeHandler* Handler = 0);
     ~BytecodeBufferReader();
 
+    bool read(std::string* ErrMsg);
+
   };
 }
 
-BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
-                                           unsigned Length,
-                                           const std::string &ModuleID,
-                                           llvm::BytecodeHandler* H )
+BytecodeBufferReader::BytecodeBufferReader(const unsigned char *buf,
+                                           unsigned len,
+                                           const std::string &modID,
+                                           llvm::BytecodeHandler* H)
   : BytecodeReader(H)
+  , Buffer(0)
+  , Buf(buf)
+  , Length(len)
+  , ModuleID(modID)
+  , MustDelete(false)
 {
+}
+
+BytecodeBufferReader::~BytecodeBufferReader() {
+  if (MustDelete) delete [] Buffer;
+}
+
+bool
+BytecodeBufferReader::read(std::string* ErrMsg) {
   // If not aligned, allocate a new buffer to hold the bytecode...
   const unsigned char *ParseBegin = 0;
   if (reinterpret_cast<uint64_t>(Buf) & 3) {
@@ -104,15 +130,11 @@
     ParseBegin = Buffer = Buf;
     MustDelete = false;
   }
-  std::string ErrMsg;
-  if (ParseBytecode(ParseBegin, Length, ModuleID, &ErrMsg)) {
+  if (ParseBytecode(ParseBegin, Length, ModuleID, ErrMsg)) {
     if (MustDelete) delete [] Buffer;
-    throw ErrMsg;
+    return true;
   }
-}
-
-BytecodeBufferReader::~BytecodeBufferReader() {
-  if (MustDelete) delete [] Buffer;
+  return false;
 }
 
 //===----------------------------------------------------------------------===//
@@ -132,12 +154,18 @@
 
   public:
     BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
+    bool read(std::string* ErrMsg);
   };
 }
 
 BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
   : BytecodeReader(H)
 {
+}
+
+bool
+BytecodeStdinReader::read(std::string* ErrMsg)
+{
   sys::Program::ChangeStdinToBinary();
   char Buffer[4096*4];
 
@@ -150,14 +178,16 @@
     FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
   }
 
-  if (FileData.empty())
-    throw std::string("Standard Input empty!");
+  if (FileData.empty()) {
+    if (ErrMsg)
+      *ErrMsg = "Standard Input is empty!";
+    return true;
+  }
 
   FileBuf = &FileData[0];
-  std::string ErrMsg;
-  if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", &ErrMsg)) {
-    throw ErrMsg;
-  }
+  if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", ErrMsg))
+    return true;
+  return false;
 }
 
 //===----------------------------------------------------------------------===//
@@ -270,66 +300,71 @@
 llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
                                       unsigned Length,
                                       const std::string &ModuleID,
-                                      BytecodeHandler* H ) {
-  return CheckVarargs(
-     new BytecodeBufferReader(Buffer, Length, ModuleID, H));
+                                      std::string* ErrMsg, 
+                                      BytecodeHandler* H) {
+  BytecodeBufferReader* rdr = 
+    new BytecodeBufferReader(Buffer, Length, ModuleID, H);
+  if (rdr->read(ErrMsg))
+    return 0;
+  return CheckVarargs(rdr);
 }
 
 /// ParseBytecodeBuffer - Parse a given bytecode buffer
 ///
 Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
                                   const std::string &ModuleID,
-                                  std::string *ErrorStr){
-  try {
-    std::auto_ptr<ModuleProvider>
-      AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
-    return AMP->releaseModule();
-  } catch (std::string &err) {
-    if (ErrorStr) *ErrorStr = err;
+                                  std::string *ErrMsg){
+  ModuleProvider* MP = 
+    getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
+  if (!MP)
     return 0;
-  }
+  return MP->releaseModule();
 }
 
 /// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
 ///
-ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
-                                                BytecodeHandler* H) {
-  if (Filename != std::string("-"))        // Read from a file...
-    return CheckVarargs(new BytecodeFileReader(Filename,H));
-  else                                     // Read from stdin
-    return CheckVarargs(new BytecodeStdinReader(H));
+ModuleProvider *
+llvm::getBytecodeModuleProvider(const std::string &Filename,
+                                std::string* ErrMsg,
+                                BytecodeHandler* H) {
+  // Read from a file
+  if (Filename != std::string("-")) {
+    BytecodeFileReader* rdr = new BytecodeFileReader(Filename, H);
+    if (rdr->read(ErrMsg))
+      return 0;
+    return CheckVarargs(rdr);
+  }
+
+  // Read from stdin
+  BytecodeStdinReader* rdr = new BytecodeStdinReader(H);
+  if (rdr->read(ErrMsg))
+    return 0;
+  return CheckVarargs(rdr);
 }
 
 /// ParseBytecodeFile - Parse the given bytecode file
 ///
 Module *llvm::ParseBytecodeFile(const std::string &Filename,
-                                std::string *ErrorStr) {
-  try {
-    std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
-    return AMP->releaseModule();
-  } catch (std::string &err) {
-    if (ErrorStr) *ErrorStr = err;
+                                std::string *ErrMsg) {
+  ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg);
+  if (!MP)
     return 0;
-  }
+  return MP->releaseModule();
 }
 
 // AnalyzeBytecodeFile - analyze one file
 Module* llvm::AnalyzeBytecodeFile(
   const std::string &Filename,  ///< File to analyze
   BytecodeAnalysis& bca,        ///< Statistical output
-  std::string *ErrorStr,        ///< Error output
+  std::string *ErrMsg,          ///< Error output
   std::ostream* output          ///< Dump output
 )
 {
-  try {
-    BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output);
-    std::auto_ptr<ModuleProvider> AMP(
-      getBytecodeModuleProvider(Filename,analyzerHandler));
-    return AMP->releaseModule();
-  } catch (std::string &err) {
-    if (ErrorStr) *ErrorStr = err;
+  BytecodeHandler* AH = createBytecodeAnalyzerHandler(bca,output);
+  ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg, AH);
+  if (!MP)
     return 0;
-  }
+  return MP->releaseModule();
 }
 
 // AnalyzeBytecodeBuffer - analyze a buffer
@@ -338,34 +373,30 @@
   unsigned Length,             ///< Size of the bytecode buffer
   const std::string& ModuleID, ///< Identifier for the module
   BytecodeAnalysis& bca,       ///< The results of the analysis
-  std::string* ErrorStr,       ///< Errors, if any.
+  std::string* ErrMsg,         ///< Errors, if any.
   std::ostream* output         ///< Dump output, if any
 )
 {
-  try {
-    BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
-    std::auto_ptr<ModuleProvider>
-      AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
-    return AMP->releaseModule();
-  } catch (std::string &err) {
-    if (ErrorStr) *ErrorStr = err;
+  BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
+  ModuleProvider* MP = 
+    getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, hdlr);
+  if (!MP)
     return 0;
-  }
+  return MP->releaseModule();
 }
 
 bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
-                                         Module::LibraryListType& deplibs) {
-  try {
-    std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
-    Module* M = AMP->releaseModule();
-
-    deplibs = M->getLibraries();
-    delete M;
-    return true;
-  } catch (...) {
+                                         Module::LibraryListType& deplibs,
+                                         std::string* ErrMsg) {
+  ModuleProvider* MP =  getBytecodeModuleProvider(fname, ErrMsg);
+  if (!MP) {
     deplibs.clear();
-    return false;
+    return true;
   }
+  Module* M = MP->releaseModule();
+  deplibs = M->getLibraries();
+  delete M;
+  return false;
 }
 
 static void getSymbols(Module*M, std::vector<std::string>& symbols) {
@@ -384,13 +415,16 @@
 
 // Get just the externally visible defined symbols from the bytecode
 bool llvm::GetBytecodeSymbols(const sys::Path& fName,
-                              std::vector<std::string>& symbols) {
-  std::auto_ptr<ModuleProvider> AMP(
-      getBytecodeModuleProvider(fName.toString()));
+                              std::vector<std::string>& symbols,
+                              std::string* ErrMsg) {
+  ModuleProvider* MP = getBytecodeModuleProvider(fName.toString(), ErrMsg);
+  if (!MP)
+    return true;
 
   // Get the module from the provider
-  Module* M = AMP->materializeModule();
-  if (M == 0) return false;
+  Module* M = MP->materializeModule();
+  if (M == 0) 
+    return true;
 
   // Get the symbols
   getSymbols(M, symbols);
@@ -402,29 +436,26 @@
 ModuleProvider*
 llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
                          const std::string& ModuleID,
-                         std::vector<std::string>& symbols) {
+                         std::vector<std::string>& symbols,
+                         std::string* ErrMsg) {
+  // Get the module provider
+  ModuleProvider* MP = 
+    getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
+  if (!MP)
+    return 0;
 
-  ModuleProvider* MP = 0;
-  try {
-    // Get the module provider
-    MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
-
-    // Get the module from the provider
-    Module* M = MP->materializeModule();
-    if (M == 0) return 0;
-
-    // Get the symbols
-    getSymbols(M, symbols);
-
-    // Done with the module. Note that ModuleProvider will delete the
-    // Module when it is deleted. Also note that its the caller's responsibility
-    // to delete the ModuleProvider.
-    return MP;
-
-  } catch (...) {
-    // We delete only the ModuleProvider here because its destructor will
-    // also delete the Module (we used materializeModule not releaseModule).
+  // Get the module from the provider
+  Module* M = MP->materializeModule();
+  if (M == 0) {
     delete MP;
+    return 0;
   }
-  return 0;
+
+  // Get the symbols
+  getSymbols(M, symbols);
+
+  // Done with the module. Note that ModuleProvider will delete the
+  // Module when it is deleted. Also note that its the caller's responsibility
+  // to delete the ModuleProvider.
+  return MP;
 }






More information about the llvm-commits mailing list