From dd4743d18a0b4b0260f0fc30c3726544cd71cb74 Mon Sep 17 00:00:00 2001 From: Edwin Vane Date: Fri, 15 Feb 2013 19:38:28 +0000 Subject: [PATCH] Propagate changes through no-op transforms Currently, changes made by previous transforms are not kept if a transform doesn't make any changes itself to a given file. Now file states are propagated properly through transforms that don't make changes. Fixes: PR15281 Author: Jack Yang Reviewer: klimek llvm-svn: 175288 --- clang-tools-extra/cpp11-migrate/Cpp11Migrate.cpp | 4 ---- .../cpp11-migrate/LoopConvert/LoopConvert.cpp | 2 +- clang-tools-extra/cpp11-migrate/Transform.cpp | 14 ++++++++------ clang-tools-extra/cpp11-migrate/Transform.h | 8 +++++--- clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/clang-tools-extra/cpp11-migrate/Cpp11Migrate.cpp b/clang-tools-extra/cpp11-migrate/Cpp11Migrate.cpp index b3804f9..92ef180 100644 --- a/clang-tools-extra/cpp11-migrate/Cpp11Migrate.cpp +++ b/clang-tools-extra/cpp11-migrate/Cpp11Migrate.cpp @@ -84,12 +84,8 @@ int main(int argc, const char **argv) { return 1; } - unsigned int NumFiles = OptionsParser.getSourcePathList().size(); - FileContentsByPath FileStates1, FileStates2, *InputFileStates = &FileStates1, *OutputFileStates = &FileStates2; - FileStates1.reserve(NumFiles); - FileStates2.reserve(NumFiles); // Apply transforms. for (Transforms::const_iterator I = TransformManager.begin(), diff --git a/clang-tools-extra/cpp11-migrate/LoopConvert/LoopConvert.cpp b/clang-tools-extra/cpp11-migrate/LoopConvert/LoopConvert.cpp index 0af3226..dc82976 100644 --- a/clang-tools-extra/cpp11-migrate/LoopConvert/LoopConvert.cpp +++ b/clang-tools-extra/cpp11-migrate/LoopConvert/LoopConvert.cpp @@ -74,7 +74,7 @@ int LoopConvertTransform::apply(const FileContentsByPath &InputStates, // FIXME: Do something if some replacements didn't get applied? LoopTool.applyAllReplacements(Rewrite.getRewriter()); - collectResults(Rewrite.getRewriter(), ResultStates); + collectResults(Rewrite.getRewriter(), InputStates, ResultStates); if (AcceptedChanges > 0) { setChangesMade(); diff --git a/clang-tools-extra/cpp11-migrate/Transform.cpp b/clang-tools-extra/cpp11-migrate/Transform.cpp index 6520536..6d48f4b 100644 --- a/clang-tools-extra/cpp11-migrate/Transform.cpp +++ b/clang-tools-extra/cpp11-migrate/Transform.cpp @@ -7,7 +7,11 @@ using namespace clang; void collectResults(clang::Rewriter &Rewrite, + const FileContentsByPath &InputStates, FileContentsByPath &Results) { + // Copy the contents of InputStates to be modified. + Results = InputStates; + for (Rewriter::buffer_iterator I = Rewrite.buffer_begin(), E = Rewrite.buffer_end(); I != E; ++I) { @@ -16,20 +20,18 @@ void collectResults(clang::Rewriter &Rewrite, assert(Entry->getName() != 0 && "Unexpected NULL return from FileEntry::getName()"); - FileContentsByPath::value_type ResultEntry; - - ResultEntry.first = Entry->getName(); + std::string ResultBuf; // Get a copy of the rewritten buffer from the Rewriter. - llvm::raw_string_ostream StringStream(ResultEntry.second); + llvm::raw_string_ostream StringStream(ResultBuf); I->second.write(StringStream); - // Cause results to be written to ResultEntry.second. + // Cause results to be written to ResultBuf. StringStream.str(); // FIXME: Use move semantics to avoid copies of the buffer contents if // benchmarking shows the copies are expensive, especially for large source // files. - Results.push_back(ResultEntry); + Results[Entry->getName()] = ResultBuf; } } diff --git a/clang-tools-extra/cpp11-migrate/Transform.h b/clang-tools-extra/cpp11-migrate/Transform.h index 3de989c..6f0718c 100644 --- a/clang-tools-extra/cpp11-migrate/Transform.h +++ b/clang-tools-extra/cpp11-migrate/Transform.h @@ -48,16 +48,18 @@ class CompilationDatabase; } // namespace tooling } // namespace clang -/// \brief First item in the pair is the path of a file and the second is a +/// \brief The key is the path of a file, which is mapped to a /// buffer with the possibly modified contents of that file. -typedef std::vector > FileContentsByPath; +typedef std::map FileContentsByPath; /// \brief In \p Results place copies of the buffers resulting from applying /// all rewrites represented by \p Rewrite. /// /// \p Results is made up of pairs {filename, buffer contents}. Pairs are /// simply appended to \p Results. -void collectResults(clang::Rewriter &Rewrite, FileContentsByPath &Results); +void collectResults(clang::Rewriter &Rewrite, + const FileContentsByPath &InputStates, + FileContentsByPath &Results); /// \brief Class for containing a Rewriter instance and all of /// its lifetime dependencies. diff --git a/clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp b/clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp index d4ade1c..59e9f8b 100644 --- a/clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp +++ b/clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp @@ -58,7 +58,7 @@ int UseNullptrTransform::apply(const FileContentsByPath &InputStates, // FIXME: Do something if some replacements didn't get applied? UseNullptrTool.applyAllReplacements(Rewrite.getRewriter()); - collectResults(Rewrite.getRewriter(), ResultStates); + collectResults(Rewrite.getRewriter(), InputStates, ResultStates); if (AcceptedChanges > 0) { setChangesMade(); -- 2.7.4