diff options
author | Edwin Vane <edwin.vane@intel.com> | 2013-06-13 16:00:46 +0000 |
---|---|---|
committer | Edwin Vane <edwin.vane@intel.com> | 2013-06-13 16:00:46 +0000 |
commit | e0a7d9cefffeea97c2d922afdd8a373b329135e3 (patch) | |
tree | 38c2dbd6eaf429248430775b1aaffe8986cf9ccf /clang-tools-extra/cpp11-migrate/Core/Transform.cpp | |
parent | c0bbe36245df1b4feaee6239f344738bea31d244 (diff) | |
download | bcm5719-llvm-e0a7d9cefffeea97c2d922afdd8a373b329135e3.tar.gz bcm5719-llvm-e0a7d9cefffeea97c2d922afdd8a373b329135e3.zip |
cpp11-migrate: Replace file override container
A more flexible container for storing overrides is required for headers. Before
a source goes through the transform pipeline, any headers it references will be
in their original state and unaffected by transforms applied to other sources.
Therefore overrides for headers need to be kept separate for each source file.
This patch doesn't introduce support for storing header overrides yet. It only
replaces the existing structure and makes any necessary changes to support it.
llvm-svn: 183910
Diffstat (limited to 'clang-tools-extra/cpp11-migrate/Core/Transform.cpp')
-rw-r--r-- | clang-tools-extra/cpp11-migrate/Core/Transform.cpp | 48 |
1 files changed, 23 insertions, 25 deletions
diff --git a/clang-tools-extra/cpp11-migrate/Core/Transform.cpp b/clang-tools-extra/cpp11-migrate/Core/Transform.cpp index 718ed0785ad..3a054e330df 100644 --- a/clang-tools-extra/cpp11-migrate/Core/Transform.cpp +++ b/clang-tools-extra/cpp11-migrate/Core/Transform.cpp @@ -1,7 +1,5 @@ #include "Core/Transform.h" #include "clang/ASTMatchers/ASTMatchFinder.h" -#include "clang/Basic/FileManager.h" -#include "clang/Basic/SourceManager.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Rewrite/Core/Rewriter.h" #include "llvm/Support/raw_ostream.h" @@ -22,7 +20,7 @@ using namespace ast_matchers; /// SourceFileCallbacks. class ActionFactory : public clang::tooling::FrontendActionFactory { public: - ActionFactory(MatchFinder &Finder, const FileContentsByPath &Overrides, + ActionFactory(MatchFinder &Finder, const FileOverrides &Overrides, SourceFileCallbacks &Callbacks) : Finder(Finder), Overrides(Overrides), Callbacks(Callbacks) {} @@ -33,7 +31,7 @@ public: private: class FactoryAdaptor : public ASTFrontendAction { public: - FactoryAdaptor(MatchFinder &Finder, const FileContentsByPath &Overrides, + FactoryAdaptor(MatchFinder &Finder, const FileOverrides &Overrides, SourceFileCallbacks &Callbacks) : Finder(Finder), Overrides(Overrides), Callbacks(Callbacks) {} @@ -46,12 +44,10 @@ private: if (!ASTFrontendAction::BeginSourceFileAction(CI, Filename)) return false; - FileContentsByPath::const_iterator I = Overrides.find(Filename.str()); - if (I != Overrides.end()) - // If an override exists, use it. - CI.getSourceManager() - .overrideFileContents(CI.getFileManager().getFile(I->first), - llvm::MemoryBuffer::getMemBuffer(I->second)); + FileOverrides::const_iterator I = Overrides.find(Filename.str()); + if (I != Overrides.end()) { + I->second.applyOverrides(CI.getSourceManager(), CI.getFileManager()); + } return Callbacks.handleBeginSource(CI, Filename); } @@ -63,38 +59,34 @@ private: private: MatchFinder &Finder; - const FileContentsByPath &Overrides; + const FileOverrides &Overrides; SourceFileCallbacks &Callbacks; }; MatchFinder &Finder; - const FileContentsByPath &Overrides; + const FileOverrides &Overrides; SourceFileCallbacks &Callbacks; }; } // namespace RewriterContainer::RewriterContainer(clang::FileManager &Files, - const FileContentsByPath &InputStates) + const FileOverrides &InputStates) : DiagOpts(new clang::DiagnosticOptions()), DiagnosticPrinter(llvm::errs(), DiagOpts.getPtr()), Diagnostics(llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs>( new clang::DiagnosticIDs()), DiagOpts.getPtr(), &DiagnosticPrinter, false), Sources(Diagnostics, Files), Rewrite(Sources, DefaultLangOptions) { - - // Overwrite source manager's file contents with data from InputStates - for (FileContentsByPath::const_iterator I = InputStates.begin(), - E = InputStates.end(); - I != E; ++I) { - Sources.overrideFileContents(Files.getFile(I->first), - llvm::MemoryBuffer::getMemBuffer(I->second)); - } + for (FileOverrides::const_iterator I = InputStates.begin(), + E = InputStates.end(); + I != E; ++I) + I->second.applyOverrides(Sources, Files); } void collectResults(clang::Rewriter &Rewrite, - const FileContentsByPath &InputStates, - FileContentsByPath &Results) { + const FileOverrides &InputStates, + FileOverrides &Results) { // Copy the contents of InputStates to be modified. Results = InputStates; @@ -106,6 +98,12 @@ void collectResults(clang::Rewriter &Rewrite, assert(Entry->getName() != 0 && "Unexpected NULL return from FileEntry::getName()"); + FileOverrides::iterator OverrideI = Results.find(Entry->getName()); + if (OverrideI == Results.end()) { + OverrideI = Results.insert(FileOverrides::value_type( + Entry->getName(), Entry->getName())).first; + } + std::string ResultBuf; // Get a copy of the rewritten buffer from the Rewriter. @@ -118,7 +116,7 @@ void collectResults(clang::Rewriter &Rewrite, // FIXME: Use move semantics to avoid copies of the buffer contents if // benchmarking shows the copies are expensive, especially for large source // files. - Results[Entry->getName()] = ResultBuf; + OverrideI->second.MainFileOverride = ResultBuf; } } @@ -144,6 +142,6 @@ void Transform::addTiming(llvm::StringRef Label, llvm::TimeRecord Duration) { FrontendActionFactory * Transform::createActionFactory(MatchFinder &Finder, - const FileContentsByPath &InputStates) { + const FileOverrides &InputStates) { return new ActionFactory(Finder, InputStates, *this); } |