<div dir="ltr">On Mon, Jun 3, 2013 at 5:06 PM, Vane, Edwin <span dir="ltr"><<a href="mailto:edwin.vane@intel.com" target="_blank">edwin.vane@intel.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This was the sort of question I had originally: is a begin callback useful enough to be put in Tooling code? Then I thought: if an end callback is available it only makes sense to have a begin callback for symmetry. Otherwise, one day someone will come along and, like me, say: "Here's an end callback but no begin callback but it's the begin callback I really want. Why is it missing?"<br>
</blockquote><div><br></div><div style>Well, "why is it missing" is easy to answer: the minimal needed solution to run a clang tool is to hook into the end callback. </div><div style><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

If the callback is a problem I'll remove it and add a custom FrontendAction where it's needed in the migrator.</blockquote><div><br></div><div style>I don't think it's harmful (as it's usually not in the "visible" interface space anyway).</div>
<div style><br></div><div style>One question is whether we need the CompilerInstance in the callback, as that is a kind of coupling that goes beyond interface symmetry ...</div><div style><br></div><div style>Thoughts?</div>
<div style>/Manuel</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><br>
> -----Original Message-----<br>
> From: Manuel Klimek [mailto:<a href="mailto:klimek@google.com">klimek@google.com</a>]<br>
> Sent: Monday, June 03, 2013 8:59 AM<br>
> To: Vane, Edwin<br>
> Cc: <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
> Subject: Re: r182864 - Tooling: Call back for both begin and end of sources<br>
><br>
> Hmm... My main concern is whether this is really common enough that we don't<br>
> want to just write FrontendActions for where we need it (it's not hard to write<br>
> your own FrontendAction - the methods in tooling are mainly there to make the<br>
> very common use cases less code to write).<br>
><br>
><br>
> On Wed, May 29, 2013 at 6:01 PM, Edwin Vane <<a href="mailto:edwin.vane@intel.com">edwin.vane@intel.com</a>> wrote:<br>
><br>
><br>
>       Author: revane<br>
>       Date: Wed May 29 11:01:10 2013<br>
>       New Revision: 182864<br>
><br>
>       URL: <a href="http://llvm.org/viewvc/llvm-project?rev=182864&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=182864&view=rev</a><br>
>       Log:<br>
>       Tooling: Call back for both begin and end of sources<br>
><br>
>       newFrontendActionFactory() took a pointer to a callback to call when a<br>
> source<br>
>       file was done being processed by an action. This revision updates the<br>
> callback<br>
>       to include an ante-processing callback as well.<br>
><br>
>       Callback-providing class renamed and callback functions themselves<br>
> renamed.<br>
>       Functions are no longer pure-virtual so users aren't forced to implement<br>
> both<br>
>       callbacks if one isn't needed.<br>
><br>
><br>
>       Modified:<br>
>           cfe/trunk/include/clang/Tooling/Tooling.h<br>
>           cfe/trunk/unittests/Tooling/ToolingTest.cpp<br>
><br>
>       Modified: cfe/trunk/include/clang/Tooling/Tooling.h<br>
>       URL: <a href="http://llvm.org/viewvc/llvm-" target="_blank">http://llvm.org/viewvc/llvm-</a><br>
> project/cfe/trunk/include/clang/Tooling/Tooling.h?rev=182864&r1=182863&r2<br>
> =182864&view=diff<br>
>       ==========================================================<br>
> ====================<br>
>       --- cfe/trunk/include/clang/Tooling/Tooling.h (original)<br>
>       +++ cfe/trunk/include/clang/Tooling/Tooling.h Wed May 29 11:01:10<br>
> 2013<br>
>       @@ -74,12 +74,22 @@ public:<br>
>        template <typename T><br>
>        FrontendActionFactory *newFrontendActionFactory();<br>
><br>
>       -/// \brief Called at the end of each source file when used with<br>
>       -/// \c newFrontendActionFactory.<br>
>       -class EndOfSourceFileCallback {<br>
>       +/// \brief Callbacks called before and after each source file processed<br>
> by a<br>
>       +/// FrontendAction created by the FrontedActionFactory returned by \c<br>
>       +/// newFrontendActionFactory.<br>
>       +class SourceFileCallbacks {<br>
>        public:<br>
>       -  virtual ~EndOfSourceFileCallback() {}<br>
>       -  virtual void run() = 0;<br>
>       +  virtual ~SourceFileCallbacks() {}<br>
>       +<br>
>       +  /// \brief Called before a source file is processed by a FrontEndAction.<br>
><br>
><br>
><br>
> FrontendAction.<br>
><br>
><br>
>       +  /// \see clang::FrontendAction::BeginSourceFileAction<br>
>       +  virtual bool BeginSource(CompilerInstance &CI, StringRef Filename) {<br>
>       +    return true;<br>
>       +  }<br>
>       +<br>
>       +  /// \brief Called after a source file is processed by a FrontendAction.<br>
>       +  /// \see clang::FrontendAction::EndSourceFileAction<br>
>       +  virtual void EndSource() {}<br>
>        };<br>
><br>
>        /// \brief Returns a new FrontendActionFactory for any type that<br>
> provides an<br>
>       @@ -95,7 +105,7 @@ public:<br>
>        ///   newFrontendActionFactory(&Factory);<br>
>        template <typename FactoryT><br>
>        inline FrontendActionFactory *newFrontendActionFactory(<br>
>       -    FactoryT *ConsumerFactory, EndOfSourceFileCallback *EndCallback<br>
> = NULL);<br>
>       +    FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks =<br>
> NULL);<br>
><br>
>        /// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only<br>
> flag.<br>
>        ///<br>
>       @@ -226,23 +236,23 @@ FrontendActionFactory *newFrontendAction<br>
><br>
>        template <typename FactoryT><br>
>        inline FrontendActionFactory *newFrontendActionFactory(<br>
>       -    FactoryT *ConsumerFactory, EndOfSourceFileCallback *EndCallback)<br>
> {<br>
>       +    FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks) {<br>
>          class FrontendActionFactoryAdapter : public FrontendActionFactory {<br>
>          public:<br>
>            explicit FrontendActionFactoryAdapter(FactoryT *ConsumerFactory,<br>
>       -                                          EndOfSourceFileCallback *EndCallback)<br>
>       -      : ConsumerFactory(ConsumerFactory), EndCallback(EndCallback) {}<br>
>       +                                          SourceFileCallbacks *Callbacks)<br>
>       +      : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}<br>
><br>
>            virtual clang::FrontendAction *create() {<br>
>       -      return new ConsumerFactoryAdaptor(ConsumerFactory,<br>
> EndCallback);<br>
>       +      return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks);<br>
>            }<br>
><br>
>          private:<br>
>            class ConsumerFactoryAdaptor : public clang::ASTFrontendAction {<br>
>            public:<br>
>              ConsumerFactoryAdaptor(FactoryT *ConsumerFactory,<br>
>       -                             EndOfSourceFileCallback *EndCallback)<br>
>       -        : ConsumerFactory(ConsumerFactory), EndCallback(EndCallback) {}<br>
>       +                             SourceFileCallbacks *Callbacks)<br>
>       +        : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}<br>
><br>
>              clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance<br>
> &,<br>
>                                                    StringRef) {<br>
>       @@ -250,21 +260,29 @@ inline FrontendActionFactory *newFronten<br>
>              }<br>
><br>
>            protected:<br>
>       -      virtual void EndSourceFileAction() {<br>
>       -        if (EndCallback != NULL)<br>
>       -          EndCallback->run();<br>
>       +      virtual bool BeginSourceFileAction(CompilerInstance &CI,<br>
>       +                                         StringRef Filename) LLVM_OVERRIDE {<br>
>       +        if (!clang::ASTFrontendAction::BeginSourceFileAction(CI,<br>
> Filename))<br>
>       +          return false;<br>
>       +        if (Callbacks != NULL)<br>
>       +          return Callbacks->BeginSource(CI, Filename);<br>
>       +        return true;<br>
>       +      }<br>
>       +      virtual void EndSourceFileAction() LLVM_OVERRIDE {<br>
>       +        if (Callbacks != NULL)<br>
>       +          Callbacks->EndSource();<br>
>                clang::ASTFrontendAction::EndSourceFileAction();<br>
>              }<br>
><br>
>            private:<br>
>              FactoryT *ConsumerFactory;<br>
>       -      EndOfSourceFileCallback *EndCallback;<br>
>       +      SourceFileCallbacks *Callbacks;<br>
>            };<br>
>            FactoryT *ConsumerFactory;<br>
>       -    EndOfSourceFileCallback *EndCallback;<br>
>       +    SourceFileCallbacks *Callbacks;<br>
>          };<br>
><br>
>       -  return new FrontendActionFactoryAdapter(ConsumerFactory,<br>
> EndCallback);<br>
>       +  return new FrontendActionFactoryAdapter(ConsumerFactory,<br>
> Callbacks);<br>
>        }<br>
><br>
>        /// \brief Returns the absolute path of \c File, by prepending it with<br>
><br>
>       Modified: cfe/trunk/unittests/Tooling/ToolingTest.cpp<br>
>       URL: <a href="http://llvm.org/viewvc/llvm-" target="_blank">http://llvm.org/viewvc/llvm-</a><br>
> project/cfe/trunk/unittests/Tooling/ToolingTest.cpp?rev=182864&r1=182863&r<br>
> 2=182864&view=diff<br>
>       ==========================================================<br>
> ====================<br>
>       --- cfe/trunk/unittests/Tooling/ToolingTest.cpp (original)<br>
>       +++ cfe/trunk/unittests/Tooling/ToolingTest.cpp Wed May 29 11:01:10<br>
> 2013<br>
>       @@ -131,20 +131,26 @@ TEST(ToolInvocation, TestMapVirtualFile)<br>
>          EXPECT_TRUE(Invocation.run());<br>
>        }<br>
><br>
>       -struct VerifyEndCallback : public EndOfSourceFileCallback {<br>
>       -  VerifyEndCallback() : Called(0), Matched(false) {}<br>
>       -  virtual void run() {<br>
>       -    ++Called;<br>
>       +struct VerifyEndCallback : public SourceFileCallbacks {<br>
>       +  VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {}<br>
>       +  virtual bool BeginSource(CompilerInstance &CI,<br>
>       +                           StringRef Filename) LLVM_OVERRIDE {<br>
>       +    ++BeginCalled;<br>
>       +    return true;<br>
>       +  }<br>
>       +  virtual void EndSource() {<br>
>       +    ++EndCalled;<br>
>          }<br>
>          ASTConsumer *newASTConsumer() {<br>
>            return new FindTopLevelDeclConsumer(&Matched);<br>
>          }<br>
>       -  unsigned Called;<br>
>       +  unsigned BeginCalled;<br>
>       +  unsigned EndCalled;<br>
>          bool Matched;<br>
>        };<br>
><br>
>        #if !defined(_WIN32)<br>
>       -TEST(newFrontendActionFactory, InjectsEndOfSourceFileCallback) {<br>
>       +TEST(newFrontendActionFactory, InjectsSourceFileCallbacks) {<br>
>          VerifyEndCallback EndCallback;<br>
><br>
>          FixedCompilationDatabase Compilations("/",<br>
> std::vector<std::string>());<br>
>       @@ -159,7 +165,8 @@ TEST(newFrontendActionFactory, InjectsEn<br>
>          Tool.run(newFrontendActionFactory(&EndCallback, &EndCallback));<br>
><br>
>          EXPECT_TRUE(EndCallback.Matched);<br>
>       -  EXPECT_EQ(2u, EndCallback.Called);<br>
>       +  EXPECT_EQ(2u, EndCallback.BeginCalled);<br>
>       +  EXPECT_EQ(2u, EndCallback.EndCalled);<br>
>        }<br>
>        #endif<br>
><br>
><br>
><br>
>       _______________________________________________<br>
>       cfe-commits mailing list<br>
>       <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
>       <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
><br>
><br>
<br>
</div></div></blockquote></div><br></div></div>