[PATCH] clang-replace: Delete change description files

Tareq A. Siraj tareq.a.siraj at intel.com
Fri Aug 23 12:53:31 PDT 2013


  Addressing review comments.

Hi revane, arielbernal, Sarcasm,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1492?vs=3709&id=3712#toc

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.push_back(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,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/system_error.h"
 #include <vector>
+#include <string>
 
 namespace clang {
 
@@ -32,6 +33,9 @@
 typedef std::vector<clang::tooling::TranslationUnitReplacements>
 TUReplacements;
 
+/// \brief Collection of TranslationUnitReplacement files.
+typedef std::vector<std::string> TUReplacementFiles;
+
 /// \brief Map mapping file name to Replacements targeting that file.
 typedef llvm::StringMap<std::vector<clang::tooling::Replacement> >
 FileToReplacementsMap;
@@ -47,13 +51,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,36 @@
 static cl::opt<std::string> Directory(cl::Positional, cl::Required,
                                       cl::desc("<Search Root Directory>"));
 
+static cl::opt<bool> RemoveTUReplacementFiles(
+    "remove-change-desc-files",
+    cl::desc("Remove the change description files regardless of successful\n"
+             "merging/replacing."),
+    cl::init(false));
+
+// Helper object to remove the TUReplacement files (triggered by
+// "remove-change-desc-files" command line option) when exiting current scope.
+class ScopedFileRemover {
+public:
+  ScopedFileRemover(const TUReplacementFiles &Files) : TURFiles(Files) {}
+
+  ~ScopedFileRemover() {
+    if (RemoveTUReplacementFiles)
+      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 +64,21 @@
       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 (triggered by "remove-change-desc-files"
+  // command line option) 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,14 @@
 // 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 *not* deleted after running clang-replace without remove-change-desc-files.
+// RUN: ls -1 %T/Inputs/basic | FileCheck %s --check-prefix=YAML
+//
+// Check that the yaml files *are* deleted after running clang-replace with remove-change-desc-files.
+// RUN: grep -Ev "// *[A-Z-]+:" %S/Inputs/basic/basic.h > %T/Inputs/basic/basic.h
+// RUN: clang-replace -remove-change-desc-files %T/Inputs/basic
+// RUN: ls -1 %T/Inputs/basic | FileCheck %s --check-prefix=NO_YAML
+//
+// YAML: {{^file.\.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,13 @@
 // 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 *not* deleted after running clang-replace without remove-change-desc-files even when there is a failure.
+// RUN: ls -1 %T/Inputs/conflict | FileCheck %s --check-prefix=YAML
+//
+// Check that the yaml files *are* deleted after running clang-replace with remove-change-desc-files even when there is a failure.
+// RUN: not clang-replace %T/Inputs/conflict -remove-change-desc-files > %T/Inputs/conflict/output.txt 2>&1
+// RUN: ls -1 %T/Inputs/conflict | FileCheck %s --check-prefix=NO_YAML
+//
+// YAML: {{^file.\.yaml$}}
+// NO_YAML-NOT: {{^file.\.yaml$}}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1492.2.patch
Type: text/x-patch
Size: 6538 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130823/5da0152e/attachment.bin>


More information about the cfe-commits mailing list