[PATCH] clang-replace: Delete change description files
Tareq A. Siraj
tareq.a.siraj at intel.com
Mon Aug 26 12:35:11 PDT 2013
Addressing review changes.
Hi revane, arielbernal, Sarcasm,
http://llvm-reviews.chandlerc.com/D1492
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D1492?vs=3758&id=3766#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) {
@@ -218,5 +221,22 @@
return true;
}
+bool deleteReplacementFiles(const TUReplacementFiles &Files,
+ clang::DiagnosticsEngine &Diagnostics) {
+ bool Success = true;
+ for (TUReplacementFiles::const_iterator I = Files.begin(), E = Files.end();
+ I != E; ++I) {
+ error_code Error = llvm::sys::fs::remove(*I);
+ if (Error) {
+ Success = false;
+ // FIXME: Use Diagnostics for outputting errors.
+ errs() << "Error deleting file: " << *I << "\n";
+ errs() << Error.message() << "\n";
+ errs() << "Please delete the file manually\n";
+ }
+ }
+ return Success;
+}
+
} // end namespace replace
} // end namespace clang
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
@@ -82,6 +89,17 @@
bool applyReplacements(const FileToReplacementsMap &GroupedReplacements,
clang::SourceManager &SM);
+/// \brief Delete the replacement files.
+///
+/// \param[in] Files Replacement files to delete.
+/// \param[in] Diagnostics DiagnosticsEngine used for error output.
+///
+/// \returns \li true If all files have been deleted successfully.
+/// \li false If at least one or more failures occur when deleting
+/// files.
+bool deleteReplacementFiles(const TUReplacementFiles &Files,
+ clang::DiagnosticsEngine &Diagnostics);
+
} // end namespace replace
} // end namespace clang
Index: clang-replace/tool/ClangReplaceMain.cpp
===================================================================
--- clang-replace/tool/ClangReplaceMain.cpp
+++ clang-replace/tool/ClangReplaceMain.cpp
@@ -25,6 +25,29 @@
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,
+ clang::DiagnosticsEngine &Diagnostics)
+ : TURFiles(Files), Diag(Diagnostics) {}
+
+ ~ScopedFileRemover() {
+ deleteReplacementFiles(TURFiles, Diag);
+ }
+
+private:
+ const TUReplacementFiles &TURFiles;
+ clang::DiagnosticsEngine &Diag;
+};
+
int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv);
@@ -34,16 +57,23 @@
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().
+ OwningPtr<ScopedFileRemover> Remover;
+ if (RemoveTUReplacementFiles)
+ Remover.reset(new ScopedFileRemover(TURFiles, Diagnostics));
+
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.4.patch
Type: text/x-patch
Size: 7700 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130826/4cf3be36/attachment.bin>
More information about the cfe-commits
mailing list