[cfe-commits] r149043 - in /cfe/trunk: include/clang/Rewrite/FixItRewriter.h lib/Rewrite/FixItRewriter.cpp lib/Rewrite/FrontendActions.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Wed Jan 25 20:19:04 PST 2012


Author: akirtzidis
Date: Wed Jan 25 22:19:04 2012
New Revision: 149043

URL: http://llvm.org/viewvc/llvm-project?rev=149043&view=rev
Log:
In FixItRewriteToTemp::RewriteFilename don't try to close the file descriptor
with close(); return it instead.

Fixes mingw build and eliminates possible racing issues.

Modified:
    cfe/trunk/include/clang/Rewrite/FixItRewriter.h
    cfe/trunk/lib/Rewrite/FixItRewriter.cpp
    cfe/trunk/lib/Rewrite/FrontendActions.cpp

Modified: cfe/trunk/include/clang/Rewrite/FixItRewriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Rewrite/FixItRewriter.h?rev=149043&r1=149042&r2=149043&view=diff
==============================================================================
--- cfe/trunk/include/clang/Rewrite/FixItRewriter.h (original)
+++ cfe/trunk/include/clang/Rewrite/FixItRewriter.h Wed Jan 25 22:19:04 2012
@@ -33,7 +33,12 @@
 
   /// \brief This file is about to be rewritten. Return the name of the file
   /// that is okay to write to.
-  virtual std::string RewriteFilename(const std::string &Filename) = 0;
+  ///
+  /// \param fd out parameter for file descriptor. After the call it may be set
+  /// to an open file descriptor for the returned filename, or it will be -1
+  /// otherwise.
+  ///
+  virtual std::string RewriteFilename(const std::string &Filename, int &fd) = 0;
 
   /// \brief Whether to abort fixing a file when not all errors could be fixed.
   bool FixWhatYouCan;

Modified: cfe/trunk/lib/Rewrite/FixItRewriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/FixItRewriter.cpp?rev=149043&r1=149042&r2=149043&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/FixItRewriter.cpp (original)
+++ cfe/trunk/lib/Rewrite/FixItRewriter.cpp Wed Jan 25 22:19:04 2012
@@ -60,18 +60,24 @@
 
   for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) {
     const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first);
-    std::string Filename = FixItOpts->RewriteFilename(Entry->getName());
+    int fd;
+    std::string Filename = FixItOpts->RewriteFilename(Entry->getName(), fd);
     std::string Err;
-    llvm::raw_fd_ostream OS(Filename.c_str(), Err,
-                            llvm::raw_fd_ostream::F_Binary);
+    llvm::OwningPtr<llvm::raw_fd_ostream> OS;
+    if (fd != -1) {
+      OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
+    } else {
+      OS.reset(new llvm::raw_fd_ostream(Filename.c_str(), Err,
+                                        llvm::raw_fd_ostream::F_Binary));
+    }
     if (!Err.empty()) {
       Diags.Report(clang::diag::err_fe_unable_to_open_output)
           << Filename << Err;
       continue;
     }
     RewriteBuffer &RewriteBuf = I->second;
-    RewriteBuf.write(OS);
-    OS.flush();
+    RewriteBuf.write(*OS);
+    OS->flush();
 
     if (RewrittenFiles)
       RewrittenFiles->push_back(std::make_pair(Entry->getName(), Filename));

Modified: cfe/trunk/lib/Rewrite/FrontendActions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Rewrite/FrontendActions.cpp?rev=149043&r1=149042&r2=149043&view=diff
==============================================================================
--- cfe/trunk/lib/Rewrite/FrontendActions.cpp (original)
+++ cfe/trunk/lib/Rewrite/FrontendActions.cpp Wed Jan 25 22:19:04 2012
@@ -55,7 +55,10 @@
 namespace {
 class FixItRewriteInPlace : public FixItOptions {
 public:
-  std::string RewriteFilename(const std::string &Filename) { return Filename; }
+  std::string RewriteFilename(const std::string &Filename, int &fd) {
+    fd = -1;
+    return Filename;
+  }
 };
 
 class FixItActionSuffixInserter : public FixItOptions {
@@ -67,7 +70,8 @@
       this->FixWhatYouCan = FixWhatYouCan;
   }
 
-  std::string RewriteFilename(const std::string &Filename) {
+  std::string RewriteFilename(const std::string &Filename, int &fd) {
+    fd = -1;
     llvm::SmallString<128> Path(Filename);
     llvm::sys::path::replace_extension(Path,
       NewSuffix + llvm::sys::path::extension(Path));
@@ -77,16 +81,13 @@
 
 class FixItRewriteToTemp : public FixItOptions {
 public:
-  std::string RewriteFilename(const std::string &Filename) {
+  std::string RewriteFilename(const std::string &Filename, int &fd) {
     llvm::SmallString<128> Path;
     Path = llvm::sys::path::filename(Filename);
     Path += "-%%%%%%%%";
     Path += llvm::sys::path::extension(Filename);
-    int fd;
     llvm::SmallString<128> NewPath;
-    if (llvm::sys::fs::unique_file(Path.str(), fd, NewPath)
-          == llvm::errc::success)
-      ::close(fd);
+    llvm::sys::fs::unique_file(Path.str(), fd, NewPath);
     return NewPath.str();
   }
 };





More information about the cfe-commits mailing list