<div dir="ltr"><div style>Hmm... My main concern is whether this is really common enough that we don't want to just write FrontendActions for where we need it (it's not hard to write your own FrontendAction - the methods in tooling are mainly there to make the very common use cases less code to write).</div>
<div><br></div><div><br></div>On Wed, May 29, 2013 at 6:01 PM, Edwin Vane <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">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 source<br>
file was done being processed by an action. This revision updates the callback<br>
to include an ante-processing callback as well.<br>
<br>
Callback-providing class renamed and callback functions themselves renamed.<br>
Functions are no longer pure-virtual so users aren't forced to implement 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-project/cfe/trunk/include/clang/Tooling/Tooling.h?rev=182864&r1=182863&r2=182864&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Tooling.h?rev=182864&r1=182863&r2=182864&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Tooling/Tooling.h (original)<br>
+++ cfe/trunk/include/clang/Tooling/Tooling.h Wed May 29 11:01:10 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 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></blockquote><div style><br></div><div style>FrontendAction.</div><div style> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ /// \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 provides an<br>
@@ -95,7 +105,7 @@ public:<br>
/// newFrontendActionFactory(&Factory);<br>
template <typename FactoryT><br>
inline FrontendActionFactory *newFrontendActionFactory(<br>
- FactoryT *ConsumerFactory, EndOfSourceFileCallback *EndCallback = NULL);<br>
+ FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks = NULL);<br>
<br>
/// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.<br>
///<br>
@@ -226,23 +236,23 @@ FrontendActionFactory *newFrontendAction<br>
<br>
template <typename FactoryT><br>
inline FrontendActionFactory *newFrontendActionFactory(<br>
- FactoryT *ConsumerFactory, EndOfSourceFileCallback *EndCallback) {<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, 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>
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, 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, EndCallback);<br>
+ return new FrontendActionFactoryAdapter(ConsumerFactory, 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-project/cfe/trunk/unittests/Tooling/ToolingTest.cpp?rev=182864&r1=182863&r2=182864&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/ToolingTest.cpp?rev=182864&r1=182863&r2=182864&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/unittests/Tooling/ToolingTest.cpp (original)<br>
+++ cfe/trunk/unittests/Tooling/ToolingTest.cpp Wed May 29 11:01:10 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("/", 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>
</blockquote></div><br></div></div>