]> granicus.if.org Git - clang/commitdiff
Tooling: Call back for both begin and end of sources
authorEdwin Vane <edwin.vane@intel.com>
Wed, 29 May 2013 16:01:10 +0000 (16:01 +0000)
committerEdwin Vane <edwin.vane@intel.com>
Wed, 29 May 2013 16:01:10 +0000 (16:01 +0000)
newFrontendActionFactory() took a pointer to a callback to call when a source
file was done being processed by an action. This revision updates the callback
to include an ante-processing callback as well.

Callback-providing class renamed and callback functions themselves renamed.
Functions are no longer pure-virtual so users aren't forced to implement both
callbacks if one isn't needed.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182864 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/Tooling.h
unittests/Tooling/ToolingTest.cpp

index 27e5a0af25730d4a99422b2945788275b499de7b..b446c94cb07148a3107c2b8a105a2b3d550ed230 100644 (file)
@@ -74,12 +74,22 @@ public:
 template <typename T>
 FrontendActionFactory *newFrontendActionFactory();
 
-/// \brief Called at the end of each source file when used with
-/// \c newFrontendActionFactory.
-class EndOfSourceFileCallback {
+/// \brief Callbacks called before and after each source file processed by a
+/// FrontendAction created by the FrontedActionFactory returned by \c
+/// newFrontendActionFactory.
+class SourceFileCallbacks {
 public:
-  virtual ~EndOfSourceFileCallback() {}
-  virtual void run() = 0;
+  virtual ~SourceFileCallbacks() {}
+
+  /// \brief Called before a source file is processed by a FrontEndAction.
+  /// \see clang::FrontendAction::BeginSourceFileAction
+  virtual bool BeginSource(CompilerInstance &CI, StringRef Filename) {
+    return true;
+  }
+
+  /// \brief Called after a source file is processed by a FrontendAction.
+  /// \see clang::FrontendAction::EndSourceFileAction
+  virtual void EndSource() {}
 };
 
 /// \brief Returns a new FrontendActionFactory for any type that provides an
@@ -95,7 +105,7 @@ public:
 ///   newFrontendActionFactory(&Factory);
 template <typename FactoryT>
 inline FrontendActionFactory *newFrontendActionFactory(
-    FactoryT *ConsumerFactory, EndOfSourceFileCallback *EndCallback = NULL);
+    FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks = NULL);
 
 /// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.
 ///
@@ -226,23 +236,23 @@ FrontendActionFactory *newFrontendActionFactory() {
 
 template <typename FactoryT>
 inline FrontendActionFactory *newFrontendActionFactory(
-    FactoryT *ConsumerFactory, EndOfSourceFileCallback *EndCallback) {
+    FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks) {
   class FrontendActionFactoryAdapter : public FrontendActionFactory {
   public:
     explicit FrontendActionFactoryAdapter(FactoryT *ConsumerFactory,
-                                          EndOfSourceFileCallback *EndCallback)
-      : ConsumerFactory(ConsumerFactory), EndCallback(EndCallback) {}
+                                          SourceFileCallbacks *Callbacks)
+      : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
 
     virtual clang::FrontendAction *create() {
-      return new ConsumerFactoryAdaptor(ConsumerFactory, EndCallback);
+      return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks);
     }
 
   private:
     class ConsumerFactoryAdaptor : public clang::ASTFrontendAction {
     public:
       ConsumerFactoryAdaptor(FactoryT *ConsumerFactory,
-                             EndOfSourceFileCallback *EndCallback)
-        : ConsumerFactory(ConsumerFactory), EndCallback(EndCallback) {}
+                             SourceFileCallbacks *Callbacks)
+        : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
 
       clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &,
                                             StringRef) {
@@ -250,21 +260,29 @@ inline FrontendActionFactory *newFrontendActionFactory(
       }
 
     protected:
-      virtual void EndSourceFileAction() {
-        if (EndCallback != NULL)
-          EndCallback->run();
+      virtual bool BeginSourceFileAction(CompilerInstance &CI,
+                                         StringRef Filename) LLVM_OVERRIDE {
+        if (!clang::ASTFrontendAction::BeginSourceFileAction(CI, Filename))
+          return false;
+        if (Callbacks != NULL)
+          return Callbacks->BeginSource(CI, Filename);
+        return true;
+      }
+      virtual void EndSourceFileAction() LLVM_OVERRIDE {
+        if (Callbacks != NULL)
+          Callbacks->EndSource();
         clang::ASTFrontendAction::EndSourceFileAction();
       }
 
     private:
       FactoryT *ConsumerFactory;
-      EndOfSourceFileCallback *EndCallback;
+      SourceFileCallbacks *Callbacks;
     };
     FactoryT *ConsumerFactory;
-    EndOfSourceFileCallback *EndCallback;
+    SourceFileCallbacks *Callbacks;
   };
 
-  return new FrontendActionFactoryAdapter(ConsumerFactory, EndCallback);
+  return new FrontendActionFactoryAdapter(ConsumerFactory, Callbacks);
 }
 
 /// \brief Returns the absolute path of \c File, by prepending it with
index a9319f2961207b11d661787fbf642a388194aefe..a7b9431918483b673b8103c2faf6630ab8cd9688 100644 (file)
@@ -131,20 +131,26 @@ TEST(ToolInvocation, TestMapVirtualFile) {
   EXPECT_TRUE(Invocation.run());
 }
 
-struct VerifyEndCallback : public EndOfSourceFileCallback {
-  VerifyEndCallback() : Called(0), Matched(false) {}
-  virtual void run() {
-    ++Called;
+struct VerifyEndCallback : public SourceFileCallbacks {
+  VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {}
+  virtual bool BeginSource(CompilerInstance &CI,
+                           StringRef Filename) LLVM_OVERRIDE {
+    ++BeginCalled;
+    return true;
+  }
+  virtual void EndSource() {
+    ++EndCalled;
   }
   ASTConsumer *newASTConsumer() {
     return new FindTopLevelDeclConsumer(&Matched);
   }
-  unsigned Called;
+  unsigned BeginCalled;
+  unsigned EndCalled;
   bool Matched;
 };
 
 #if !defined(_WIN32)
-TEST(newFrontendActionFactory, InjectsEndOfSourceFileCallback) {
+TEST(newFrontendActionFactory, InjectsSourceFileCallbacks) {
   VerifyEndCallback EndCallback;
 
   FixedCompilationDatabase Compilations("/", std::vector<std::string>());
@@ -159,7 +165,8 @@ TEST(newFrontendActionFactory, InjectsEndOfSourceFileCallback) {
   Tool.run(newFrontendActionFactory(&EndCallback, &EndCallback));
 
   EXPECT_TRUE(EndCallback.Matched);
-  EXPECT_EQ(2u, EndCallback.Called);
+  EXPECT_EQ(2u, EndCallback.BeginCalled);
+  EXPECT_EQ(2u, EndCallback.EndCalled);
 }
 #endif