[PATCH] cpp11-migrate: New mechanism for overriding file contents
Edwin Vane
edwin.vane at intel.com
Tue Jun 11 10:27:36 PDT 2013
Hi arielbernal, tareqsiraj,
Next step toward supporting migrating headers. Instead of using
ClangTool's ability to override all files at once, use a custom
FrontendAction and override only the source (and eventually headers) the
action is about to parse.
Use of newFrontendActionFactory() is replaced with a new factory maker
provided by Transform.
http://llvm-reviews.chandlerc.com/D956
Files:
cpp11-migrate/AddOverride/AddOverride.cpp
cpp11-migrate/Core/Transform.cpp
cpp11-migrate/Core/Transform.h
cpp11-migrate/LoopConvert/LoopConvert.cpp
cpp11-migrate/UseAuto/UseAuto.cpp
cpp11-migrate/UseNullptr/UseNullptr.cpp
Index: cpp11-migrate/AddOverride/AddOverride.cpp
===================================================================
--- cpp11-migrate/AddOverride/AddOverride.cpp
+++ cpp11-migrate/AddOverride/AddOverride.cpp
@@ -53,8 +53,8 @@
Finder.addMatcher(makeCandidateForOverrideAttrMatcher(), &Fixer);
- if (int result = AddOverrideTool.run(
- newFrontendActionFactory(&Finder, /*Callbacks=*/ this))) {
+ if (int result =
+ AddOverrideTool.run(createActionFactory(Finder, InputStates))) {
llvm::errs() << "Error encountered during translation.\n";
return result;
}
Index: cpp11-migrate/Core/Transform.cpp
===================================================================
--- cpp11-migrate/Core/Transform.cpp
+++ cpp11-migrate/Core/Transform.cpp
@@ -1,11 +1,72 @@
#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"
using namespace clang;
+namespace {
+
+using namespace tooling;
+using namespace ast_matchers;
+
+class ActionFactory : public clang::tooling::FrontendActionFactory {
+public:
+ ActionFactory(MatchFinder &Finder, const FileContentsByPath &Overrides,
+ SourceFileCallbacks &Callbacks)
+ : Finder(Finder), Overrides(Overrides), Callbacks(Callbacks) {}
+
+ virtual FrontendAction *create() LLVM_OVERRIDE {
+ return new FactoryAdaptor(Finder, Overrides, Callbacks);
+ }
+
+private:
+ class FactoryAdaptor : public ASTFrontendAction {
+ public:
+ FactoryAdaptor(MatchFinder &Finder, const FileContentsByPath &Overrides,
+ SourceFileCallbacks &Callbacks)
+ : Finder(Finder), Overrides(Overrides), Callbacks(Callbacks) {}
+
+ ASTConsumer *CreateASTConsumer(CompilerInstance &, StringRef) {
+ return Finder.newASTConsumer();
+ }
+
+ virtual bool BeginSourceFileAction(CompilerInstance &CI,
+ StringRef Filename) LLVM_OVERRIDE {
+ 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));
+
+ return Callbacks.handleBeginSource(CI, Filename);
+ }
+
+ virtual void EndSourceFileAction() LLVM_OVERRIDE {
+ Callbacks.handleEndSource();
+ return ASTFrontendAction::EndSourceFileAction();
+ }
+
+ private:
+ MatchFinder &Finder;
+ const FileContentsByPath &Overrides;
+ SourceFileCallbacks &Callbacks;
+ };
+
+ MatchFinder &Finder;
+ const FileContentsByPath &Overrides;
+ SourceFileCallbacks &Callbacks;
+};
+
+} // namespace
+
void collectResults(clang::Rewriter &Rewrite,
const FileContentsByPath &InputStates,
FileContentsByPath &Results) {
@@ -55,3 +116,9 @@
void Transform::addTiming(llvm::StringRef Label, llvm::TimeRecord Duration) {
Timings.push_back(std::make_pair(Label.str(), Duration));
}
+
+FrontendActionFactory *
+Transform::createActionFactory(MatchFinder &Finder,
+ const FileContentsByPath &InputStates) {
+ return new ActionFactory(Finder, InputStates, *this);
+}
Index: cpp11-migrate/Core/Transform.h
===================================================================
--- cpp11-migrate/Core/Transform.h
+++ cpp11-migrate/Core/Transform.h
@@ -49,6 +49,9 @@
namespace tooling {
class CompilationDatabase;
} // namespace tooling
+namespace ast_matchers {
+class MatchFinder;
+} // namespace ast_matchers
} // namespace clang
/// \brief The key is the path of a file, which is mapped to a
@@ -233,6 +236,10 @@
const TransformOptions &Options() { return GlobalOptions; }
+ clang::tooling::FrontendActionFactory *
+ createActionFactory(clang::ast_matchers::MatchFinder &Finder,
+ const FileContentsByPath &InputStates);
+
private:
const std::string Name;
const TransformOptions &GlobalOptions;
Index: cpp11-migrate/LoopConvert/LoopConvert.cpp
===================================================================
--- cpp11-migrate/LoopConvert/LoopConvert.cpp
+++ cpp11-migrate/LoopConvert/LoopConvert.cpp
@@ -63,8 +63,7 @@
Options().MaxRiskLevel, LFK_PseudoArray);
Finder.addMatcher(makePseudoArrayLoopMatcher(), &PseudoarrrayLoopFixer);
- if (int result = LoopTool.run(
- newFrontendActionFactory(&Finder, /*Callbacks=*/ this))) {
+ if (int result = LoopTool.run(createActionFactory(Finder, InputStates))) {
llvm::errs() << "Error encountered during translation.\n";
return result;
}
Index: cpp11-migrate/UseAuto/UseAuto.cpp
===================================================================
--- cpp11-migrate/UseAuto/UseAuto.cpp
+++ cpp11-migrate/UseAuto/UseAuto.cpp
@@ -42,8 +42,7 @@
Finder.addMatcher(makeIteratorDeclMatcher(), &ReplaceIterators);
Finder.addMatcher(makeDeclWithNewMatcher(), &ReplaceNew);
- if (int Result = UseAutoTool.run(
- newFrontendActionFactory(&Finder, /*Callbacks=*/ this))) {
+ if (int Result = UseAutoTool.run(createActionFactory(Finder, InputStates))) {
llvm::errs() << "Error encountered during translation.\n";
return Result;
}
Index: cpp11-migrate/UseNullptr/UseNullptr.cpp
===================================================================
--- cpp11-migrate/UseNullptr/UseNullptr.cpp
+++ cpp11-migrate/UseNullptr/UseNullptr.cpp
@@ -46,8 +46,8 @@
Finder.addMatcher(makeCastSequenceMatcher(), &Fixer);
- if (int result = UseNullptrTool.run(
- newFrontendActionFactory(&Finder, /*Callbacks=*/ this))) {
+ if (int result =
+ UseNullptrTool.run(createActionFactory(Finder, InputStates))) {
llvm::errs() << "Error encountered during translation.\n";
return result;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D956.1.patch
Type: text/x-patch
Size: 6163 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130611/f7ef2184/attachment.bin>
More information about the cfe-commits
mailing list