cpp11-migrate: New mechanism for overriding file contents
authorEdwin Vane <edwin.vane@intel.com>
Wed, 12 Jun 2013 19:52:13 +0000 (19:52 +0000)
committerEdwin Vane <edwin.vane@intel.com>
Wed, 12 Jun 2013 19:52:13 +0000 (19:52 +0000)
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.

llvm-svn: 183855

clang-tools-extra/cpp11-migrate/AddOverride/AddOverride.cpp
clang-tools-extra/cpp11-migrate/Core/Transform.cpp
clang-tools-extra/cpp11-migrate/Core/Transform.h
clang-tools-extra/cpp11-migrate/LoopConvert/LoopConvert.cpp
clang-tools-extra/cpp11-migrate/UseAuto/UseAuto.cpp
clang-tools-extra/cpp11-migrate/UseNullptr/UseNullptr.cpp

index df0ed10d9bdc0277d4f8cc1666c516e8e9e63350..0deb8d22f2c4e31d802a13c048f2e5b048f8496b 100644 (file)
@@ -37,12 +37,6 @@ int AddOverrideTransform::apply(const FileContentsByPath &InputStates,
                                 FileContentsByPath &ResultStates) {
   RefactoringTool AddOverrideTool(Database, SourcePaths);
 
-  for (FileContentsByPath::const_iterator I = InputStates.begin(),
-       E = InputStates.end();
-       I != E; ++I) {
-    AddOverrideTool.mapVirtualFile(I->first, I->second);
-  }
-
   unsigned AcceptedChanges = 0;
 
   MatchFinder Finder;
@@ -53,8 +47,8 @@ int AddOverrideTransform::apply(const FileContentsByPath &InputStates,
 
   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 97597d6304770740a8e5cc726858a9d37d543d45..a781e912002c984db5b66946576e46cea19b84e1 100644 (file)
@@ -1,11 +1,79 @@
 #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;
+
+/// \brief Custom FrontendActionFactory to produce FrontendActions that handle
+/// overriding source file contents before parsing.
+///
+/// The nested class FactoryAdaptor overrides BeginSourceFileAction to override
+/// source file contents before parsing happens. Both Begin and
+/// EndSourceFileAction call corresponding callbacks provided by
+/// SourceFileCallbacks.
+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 +123,9 @@ void Transform::handleEndSource() {
 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 17ec6ce91206be62aca68124770007d2edf23a85..632d30b1496a2fd6c0808c30be260d776c49abf8 100644 (file)
@@ -49,6 +49,9 @@ namespace clang {
 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,13 @@ protected:
 
   const TransformOptions &Options() { return GlobalOptions; }
 
+  /// \brief Subclasses call this function to create a FrontendActionFactory to
+  /// pass to ClangTool. The factory returned by this function is responsible
+  /// for overriding source file contents with results of previous transforms.
+  clang::tooling::FrontendActionFactory *
+      createActionFactory(clang::ast_matchers::MatchFinder &Finder,
+                          const FileContentsByPath &InputStates);
+
 private:
   const std::string Name;
   const TransformOptions &GlobalOptions;
index 38659f854fa291ecab6669ad76176d020ef2872a..355dfa362e1c40fff1d1346804317f46463e3e1d 100644 (file)
@@ -31,12 +31,6 @@ int LoopConvertTransform::apply(const FileContentsByPath &InputStates,
                                 FileContentsByPath &ResultStates) {
   RefactoringTool LoopTool(Database, SourcePaths);
 
-  for (FileContentsByPath::const_iterator I = InputStates.begin(),
-       E = InputStates.end();
-       I != E; ++I) {
-    LoopTool.mapVirtualFile(I->first, I->second);
-  }
-
   StmtAncestorASTVisitor ParentFinder;
   StmtGeneratedVarNameMap GeneratedDecls;
   ReplacedVarsMap ReplacedVars;
@@ -63,8 +57,7 @@ int LoopConvertTransform::apply(const FileContentsByPath &InputStates,
                                   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 3c75246ea345b015326e996e877cec2c23a80fb9..2a76b304ad593aabc4027ac9176297c298ae3650 100644 (file)
@@ -26,11 +26,6 @@ int UseAutoTransform::apply(const FileContentsByPath &InputStates,
                             FileContentsByPath &ResultStates) {
   RefactoringTool UseAutoTool(Database, SourcePaths);
 
-  for (FileContentsByPath::const_iterator I = InputStates.begin(),
-                                          E = InputStates.end();
-       I != E; ++I)
-    UseAutoTool.mapVirtualFile(I->first, I->second);
-
   unsigned AcceptedChanges = 0;
 
   MatchFinder Finder;
@@ -42,8 +37,7 @@ int UseAutoTransform::apply(const FileContentsByPath &InputStates,
   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 58c6d438b61119ab643d735d24eec7d70c7c0023..fa5811bae3bc612abfef0811da34cecda41b398b 100644 (file)
@@ -31,12 +31,6 @@ int UseNullptrTransform::apply(const FileContentsByPath &InputStates,
                                FileContentsByPath &ResultStates) {
   RefactoringTool UseNullptrTool(Database, SourcePaths);
 
-  for (FileContentsByPath::const_iterator I = InputStates.begin(),
-       E = InputStates.end();
-       I != E; ++I) {
-    UseNullptrTool.mapVirtualFile(I->first, I->second);
-  }
-
   unsigned AcceptedChanges = 0;
 
   MatchFinder Finder;
@@ -46,8 +40,8 @@ int UseNullptrTransform::apply(const FileContentsByPath &InputStates,
 
   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;
   }