[PATCH] clang-replace: Delete change description files

Tareq A. Siraj tareq.a.siraj at intel.com
Fri Aug 23 11:23:16 PDT 2013


Hi revane, arielbernal, Sarcasm,

Delete change description files after merging and applying regardless of
success.

http://llvm-reviews.chandlerc.com/D1492

Files:
  clang-replace/ApplyReplacements.cpp
  clang-replace/ApplyReplacements.h
  clang-replace/tool/ClangReplaceMain.cpp
  test/clang-replace/basic.cpp
  test/clang-replace/conflict.cpp

Index: clang-replace/ApplyReplacements.cpp
===================================================================
--- clang-replace/ApplyReplacements.cpp
+++ clang-replace/ApplyReplacements.cpp
@@ -34,6 +34,7 @@
 llvm::error_code
 collectReplacementsFromDirectory(const llvm::StringRef Directory,
                                  TUReplacements &TUs,
+                                 TUReplacementFiles & TURFiles,
                                  clang::DiagnosticsEngine &Diagnostics) {
   using namespace llvm::sys::fs;
   using namespace llvm::sys::path;
@@ -51,6 +52,8 @@
     if (extension(I->path()) != ".yaml")
       continue;
 
+    TURFiles.insert(I->path());
+
     OwningPtr<MemoryBuffer> Out;
     error_code BufferError = MemoryBuffer::getFile(I->path(), Out);
     if (BufferError) {
Index: clang-replace/ApplyReplacements.h
===================================================================
--- clang-replace/ApplyReplacements.h
+++ clang-replace/ApplyReplacements.h
@@ -21,6 +21,8 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/system_error.h"
 #include <vector>
+#include <set>
+#include <string>
 
 namespace clang {
 
@@ -32,6 +34,9 @@
 typedef std::vector<clang::tooling::TranslationUnitReplacements>
 TUReplacements;
 
+/// \brief Collection of TranslationUnitReplacement files.
+typedef std::set<std::string> TUReplacementFiles;
+
 /// \brief Map mapping file name to Replacements targeting that file.
 typedef llvm::StringMap<std::vector<clang::tooling::Replacement> >
 FileToReplacementsMap;
@@ -47,13 +52,16 @@
 /// TranslationUnitReplacements.
 /// \param[out] TUs Collection of all found and deserialized
 /// TranslationUnitReplacements.
+/// \param[out] TURFiles Collection of all TranslationUnitReplacement files
+/// found in \c Directory.
 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
 ///
 /// \returns An error_code indicating success or failure in navigating the
 /// directory structure.
 llvm::error_code
 collectReplacementsFromDirectory(const llvm::StringRef Directory,
                                  TUReplacements &TUs,
+                                 TUReplacementFiles &TURFiles,
                                  clang::DiagnosticsEngine &Diagnostics);
 
 /// \brief Deduplicate, check for conflicts, and apply all Replacements stored
Index: clang-replace/tool/ClangReplaceMain.cpp
===================================================================
--- clang-replace/tool/ClangReplaceMain.cpp
+++ clang-replace/tool/ClangReplaceMain.cpp
@@ -25,6 +25,28 @@
 static cl::opt<std::string> Directory(cl::Positional, cl::Required,
                                       cl::desc("<Search Root Directory>"));
 
+// Helper object to remove the TUReplacement files when exiting current scope.
+class ScopedFileRemover {
+public:
+  ScopedFileRemover(const TUReplacementFiles &Files) : TURFiles(Files) {}
+
+  ~ScopedFileRemover() {
+    for (TUReplacementFiles::const_iterator I = TURFiles.begin(),
+                                            E = TURFiles.end();
+         I != E; ++I) {
+      error_code Error = llvm::sys::fs::remove(*I);
+      if (Error) {
+        errs() << "Error deleting file: " << *I << "\n";
+        errs() << Error.message() << "\n";
+        errs() << "Please delete the file manually\n";
+      }
+    }
+  }
+
+private:
+  const TUReplacementFiles &TURFiles;
+};
+
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv);
 
@@ -34,16 +56,20 @@
       DiagOpts.getPtr());
 
   TUReplacements TUs;
+  TUReplacementFiles TURFiles;
 
   error_code ErrorCode =
-      collectReplacementsFromDirectory(Directory, TUs, Diagnostics);
+      collectReplacementsFromDirectory(Directory, TUs, TURFiles, Diagnostics);
 
   if (ErrorCode) {
     errs() << "Trouble iterating over directory '" << Directory
            << "': " << ErrorCode.message() << "\n";
     return false;
   }
 
+  // Remove the TUReplacementFiles when exiting main().
+  ScopedFileRemover Remover(TURFiles);
+
   FileManager Files((FileSystemOptions()));
   SourceManager SM(Diagnostics, Files);
 
Index: test/clang-replace/basic.cpp
===================================================================
--- test/clang-replace/basic.cpp
+++ test/clang-replace/basic.cpp
@@ -4,3 +4,8 @@
 // RUN: sed "s#\$(path)#%/T/Inputs/basic#" %S/Inputs/basic/file2.yaml > %T/Inputs/basic/file2.yaml
 // RUN: clang-replace %T/Inputs/basic
 // RUN: FileCheck -input-file=%T/Inputs/basic/basic.h %S/Inputs/basic/basic.h
+//
+// Check that the yaml files are deleted after running clang-replace.
+// RUN: ls -1 %T/Inputs/basic | FileCheck %s --check-prefix=NO_YAML
+//
+// NO_YAML-NOT: {{^file.\.yaml$}}
Index: test/clang-replace/conflict.cpp
===================================================================
--- test/clang-replace/conflict.cpp
+++ test/clang-replace/conflict.cpp
@@ -5,3 +5,8 @@
 // RUN: sed "s#\$(path)#%/S/Inputs/conflict#" %S/Inputs/conflict/expected.txt > %T/Inputs/conflict/expected.txt
 // RUN: not clang-replace %T/Inputs/conflict > %T/Inputs/conflict/output.txt 2>&1
 // RUN: diff -b %T/Inputs/conflict/output.txt %T/Inputs/conflict/expected.txt
+//
+// Check that the yaml files are deleted after running clang-replace.
+// RUN: ls -1 %T/Inputs/conflict | FileCheck %s --check-prefix=NO_YAML
+//
+// NO_YAML-NOT: {{^file.\.yaml$}}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1492.1.patch
Type: text/x-patch
Size: 5337 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130823/4ca6ce43/attachment.bin>


More information about the cfe-commits mailing list