]> granicus.if.org Git - clang/commitdiff
Return a std::unique_ptr from getBufferForFile. NFC.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 26 Aug 2014 19:54:40 +0000 (19:54 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 26 Aug 2014 19:54:40 +0000 (19:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@216476 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/FileManager.h
lib/Basic/FileManager.cpp
lib/Basic/SourceManager.cpp
lib/CodeGen/CodeGenAction.cpp
lib/Frontend/ASTUnit.cpp
lib/Frontend/CompilerInstance.cpp
lib/Frontend/FrontendActions.cpp
lib/Serialization/ASTReader.cpp
lib/Serialization/GlobalModuleIndex.cpp
lib/Serialization/ModuleManager.cpp
tools/libclang/CXLoadedDiagnostic.cpp

index a7069e65edf885dc75100ad473bbc0ebfbc3fb92..dc9d329ab7ddb2384b381ef5909aae2534856825 100644 (file)
@@ -241,12 +241,11 @@ public:
 
   /// \brief Open the specified file as a MemoryBuffer, returning a new
   /// MemoryBuffer if successful, otherwise returning null.
-  llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry,
-                                       std::string *ErrorStr = nullptr,
-                                       bool isVolatile = false,
-                                       bool ShouldCloseOpenFile = true);
-  llvm::MemoryBuffer *getBufferForFile(StringRef Filename,
-                                       std::string *ErrorStr = nullptr);
+  std::unique_ptr<llvm::MemoryBuffer>
+  getBufferForFile(const FileEntry *Entry, std::string *ErrorStr = nullptr,
+                   bool isVolatile = false, bool ShouldCloseOpenFile = true);
+  std::unique_ptr<llvm::MemoryBuffer>
+  getBufferForFile(StringRef Filename, std::string *ErrorStr = nullptr);
 
   /// \brief Get the 'stat' information for the given \p Path.
   ///
index 07297baf6d5d24835a3f94ead90e53c17041b7d2..c0522966e38e20d5978a6746ba1ce6bf569430d7 100644 (file)
@@ -386,9 +386,9 @@ void FileManager::FixupRelativePath(SmallVectorImpl<char> &path) const {
   path = NewPath;
 }
 
-llvm::MemoryBuffer *FileManager::
-getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
-                 bool isVolatile, bool ShouldCloseOpenFile) {
+std::unique_ptr<llvm::MemoryBuffer>
+FileManager::getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
+                              bool isVolatile, bool ShouldCloseOpenFile) {
   std::unique_ptr<llvm::MemoryBuffer> Result;
   std::error_code ec;
 
@@ -409,7 +409,7 @@ getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
     // FileEntry is open or not.
     if (ShouldCloseOpenFile)
       Entry->closeFile();
-    return Result.release();
+    return Result;
   }
 
   // Otherwise, open the file.
@@ -419,7 +419,7 @@ getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
                               /*RequiresNullTerminator=*/true, isVolatile);
     if (ec && ErrorStr)
       *ErrorStr = ec.message();
-    return Result.release();
+    return Result;
   }
 
   SmallString<128> FilePath(Entry->getName());
@@ -428,18 +428,18 @@ getBufferForFile(const FileEntry *Entry, std::string *ErrorStr,
                             /*RequiresNullTerminator=*/true, isVolatile);
   if (ec && ErrorStr)
     *ErrorStr = ec.message();
-  return Result.release();
+  return Result;
 }
 
-llvm::MemoryBuffer *FileManager::
-getBufferForFile(StringRef Filename, std::string *ErrorStr) {
+std::unique_ptr<llvm::MemoryBuffer>
+FileManager::getBufferForFile(StringRef Filename, std::string *ErrorStr) {
   std::unique_ptr<llvm::MemoryBuffer> Result;
   std::error_code ec;
   if (FileSystemOpts.WorkingDir.empty()) {
     ec = FS->getBufferForFile(Filename, Result);
     if (ec && ErrorStr)
       *ErrorStr = ec.message();
-    return Result.release();
+    return Result;
   }
 
   SmallString<128> FilePath(Filename);
@@ -447,7 +447,7 @@ getBufferForFile(StringRef Filename, std::string *ErrorStr) {
   ec = FS->getBufferForFile(FilePath.c_str(), Result);
   if (ec && ErrorStr)
     *ErrorStr = ec.message();
-  return Result.release();
+  return Result;
 }
 
 /// getStatValue - Get the 'stat' information for the specified path,
index 14de7607c3bbeb036092b9dd018bc335b43fc83e..ef52e824b14054b82e6aa6c260d0595b1558275a 100644 (file)
@@ -96,9 +96,9 @@ llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag,
 
   std::string ErrorStr;
   bool isVolatile = SM.userFilesAreVolatile() && !IsSystemFile;
-  Buffer.setPointer(SM.getFileManager().getBufferForFile(ContentsEntry,
-                                                         &ErrorStr,
-                                                         isVolatile));
+  Buffer.setPointer(SM.getFileManager()
+                        .getBufferForFile(ContentsEntry, &ErrorStr, isVolatile)
+                        .release());
 
   // If we were unable to open the file, then we are in an inconsistent
   // situation where the content cache referenced a file which no longer
index d3bfc077dad3f34784a581dfdb798dc13130e60c..f2698f34bdafa626037b9e4ce1776bb2d1befac5 100644 (file)
@@ -624,7 +624,7 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
   if (!LinkModuleToUse && !LinkBCFile.empty()) {
     std::string ErrorStr;
 
-    llvm::MemoryBuffer *BCBuf =
+    std::unique_ptr<llvm::MemoryBuffer> BCBuf =
       CI.getFileManager().getBufferForFile(LinkBCFile, &ErrorStr);
     if (!BCBuf) {
       CI.getDiagnostics().Report(diag::err_cannot_open_file)
@@ -633,12 +633,13 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
     }
 
     ErrorOr<llvm::Module *> ModuleOrErr =
-        getLazyBitcodeModule(BCBuf, *VMContext);
+        getLazyBitcodeModule(BCBuf.get(), *VMContext);
     if (std::error_code EC = ModuleOrErr.getError()) {
       CI.getDiagnostics().Report(diag::err_cannot_open_file)
         << LinkBCFile << EC.message();
       return nullptr;
     }
+    BCBuf.release(); // Owned by the module now.
     LinkModuleToUse = ModuleOrErr.get();
   }
 
index 8101732f10c2a190b9e817af2b6e105dd82bb52e..b5f1b4f93d0994b7ccb981fceff84ab1086e3954 100644 (file)
@@ -638,7 +638,7 @@ ASTDeserializationListener *ASTUnit::getDeserializationListener() {
 llvm::MemoryBuffer *ASTUnit::getBufferForFile(StringRef Filename,
                                               std::string *ErrorStr) {
   assert(FileMgr);
-  return FileMgr->getBufferForFile(Filename, ErrorStr);
+  return FileMgr->getBufferForFile(Filename, ErrorStr).release();
 }
 
 /// \brief Configure the diagnostics object for use with ASTUnit.
index 9537e85e6310818ac06dbad54ba0064d308708f0..fa0209887ff7511062beeaa463ca1fcec3d98456 100644 (file)
@@ -723,11 +723,11 @@ bool CompilerInstance::InitializeSourceManager(const FrontendInputFile &Input,
     // STDIN.
     if (File->isNamedPipe()) {
       std::string ErrorStr;
-      if (llvm::MemoryBuffer *MB =
+      if (std::unique_ptr<llvm::MemoryBuffer> MB =
               FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true)) {
         // Create a new virtual file that will have the correct size.
         File = FileMgr.getVirtualFile(InputFile, MB->getBufferSize(), 0);
-        SourceMgr.overrideFileContents(File, MB);
+        SourceMgr.overrideFileContents(File, MB.release());
       } else {
         Diags.Report(diag::err_cannot_open_file) << InputFile << ErrorStr;
         return false;
index 31e232139f0f349dfe763a82d43b32aebc300ac3..b6ec895a5b248fd5a46ae0a3745eaf2c1ea47a9d 100644 (file)
@@ -681,14 +681,13 @@ void PrintPreambleAction::ExecuteAction() {
     // We can't do anything with these.
     return;
   }
-  
+
   CompilerInstance &CI = getCompilerInstance();
-  llvm::MemoryBuffer *Buffer
+  std::unique_ptr<llvm::MemoryBuffer> Buffer
       = CI.getFileManager().getBufferForFile(getCurrentFile());
   if (Buffer) {
     unsigned Preamble =
         Lexer::ComputePreamble(Buffer->getBuffer(), CI.getLangOpts()).first;
     llvm::outs().write(Buffer->getBufferStart(), Preamble);
-    delete Buffer;
   }
 }
index 601e711ae2a227e2278b5306ae8692eb84fb2c57..98ba9af74f1816c3540be6027027cf7fcbe68d03 100644 (file)
@@ -4030,8 +4030,8 @@ std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
                                              DiagnosticsEngine &Diags) {
   // Open the AST file.
   std::string ErrStr;
-  std::unique_ptr<llvm::MemoryBuffer> Buffer;
-  Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
+  std::unique_ptr<llvm::MemoryBuffer> Buffer =
+      FileMgr.getBufferForFile(ASTFileName, &ErrStr);
   if (!Buffer) {
     Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
     return std::string();
@@ -4119,8 +4119,8 @@ bool ASTReader::readASTFileControlBlock(StringRef Filename,
                                         ASTReaderListener &Listener) {
   // Open the AST file.
   std::string ErrStr;
-  std::unique_ptr<llvm::MemoryBuffer> Buffer;
-  Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
+  std::unique_ptr<llvm::MemoryBuffer> Buffer =
+      FileMgr.getBufferForFile(Filename, &ErrStr);
   if (!Buffer) {
     return true;
   }
index 8a3d990b6f2466c8550966f39652039ead6fb7b6..f43fbaca1400195ae344c6869fe72670ddde32ea 100644 (file)
@@ -493,9 +493,10 @@ namespace {
 
 bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) {
   // Open the module file.
-  std::unique_ptr<llvm::MemoryBuffer> Buffer;
+
   std::string ErrorStr;
-  Buffer.reset(FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true));
+  std::unique_ptr<llvm::MemoryBuffer> Buffer =
+      FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true);
   if (!Buffer) {
     return true;
   }
index 7bdc40a9967d1577914fc44edcbd97b33ec12036..18fe035456d44eb90b3f51454bb4bd8be68d4e0f 100644 (file)
@@ -118,9 +118,9 @@ ModuleManager::addModule(StringRef FileName, ModuleKind Type,
         // ModuleManager it must be the same underlying file.
         // FIXME: Because FileManager::getFile() doesn't guarantee that it will
         // give us an open file, this may not be 100% reliable.
-        New->Buffer.reset(FileMgr.getBufferForFile(New->File, &ErrorStr,
-                                                   /*IsVolatile*/false,
-                                                   /*ShouldClose*/false));
+        New->Buffer = FileMgr.getBufferForFile(New->File, &ErrorStr,
+                                               /*IsVolatile*/ false,
+                                               /*ShouldClose*/ false);
       }
       
       if (!New->Buffer)
index ddf374903a93b6712d5aedc3dc310cb61875c0c2..002b8c439eb2ddf892335333828439fa7f4d29f6 100644 (file)
@@ -260,9 +260,7 @@ CXDiagnosticSet DiagLoader::load(const char *file) {
   FileSystemOptions FO;
   FileManager FileMgr(FO);
 
-  std::unique_ptr<llvm::MemoryBuffer> Buffer;
-  Buffer.reset(FileMgr.getBufferForFile(file));
-
+  std::unique_ptr<llvm::MemoryBuffer> Buffer = FileMgr.getBufferForFile(file);
   if (!Buffer) {
     reportBad(CXLoadDiag_CannotLoad, ErrStr);
     return nullptr;