[cfe-dev] How to combine PPCallback with ASTMatcher?

Manuel Klimek klimek at google.com
Tue Oct 14 02:27:48 PDT 2014


On Tue Oct 14 2014 at 6:08:51 AM Xin Hwang <patz.hwang at gmail.com> wrote:

> Hello clang developers,
>
> I'm making a clang tool that will transform vec.begin() into
> std::begin(vec), and add #include <iterator> if there isn't one.
> ASTMatcher is used to match all member call to vec::begin(), and I
> need a PPCallback to analyze include files.
>
> I want to create a PPCallback with a ASTMatcher, but find no way to
> create a FrontendActionFactory to achive that.
>
> With a FrontendAction class like below:
>
> class MyFrontendAction : public clang::FrontendAction {
> public:
>     MyFrontendAction(SomeArgs args) { /* ... */ }
>
>     std::unique_ptr<clang::ASTComsumer>
> CreateASTConsumer(clang::CompilerInstance& ci, clang::StringRef) {
>         auto myCallback = CreateCallback(...);
>         ci.getPreprocessor().addPPCallbacks(myCallback);
>         auto myMatcher = CreateMatcher(...);
>         return myMatcher.newASTConsumer();
>     }
> };
>


>
> However, newFrontendActionFactory<MyFrontendAction>() cannot be used
> because there is no way to provide constructor arguments; nor can
> another overload version be used
> because it doesn't pass CompilerInstance to newASTConsumer() function:
>
> template <typename FactoryT>
> inline std::unique_ptr<FrontendActionFactory>
> newFrontendActionFactory(FactoryT *ConsumerFactory,
> SourceFileCallbacks *Callbacks)
>
> Is there any other way so that I can have a parameterized constructor
> of FrontendActionFactory and a parameterized interface of
> newASTConsumer/createASTConsumer at same time? (Except for
> re-implementing a newFrontendActionFactory that match my needs.)
>

Implement:
class MFAF : public clang::tooling::FrontendActionFactory {
  MFAF(SomeArgs args) : args(args) {}

  clang::FrontendAction *create() {
    return new MyFrontendAction(args);
  }

  SomeArgs args;
};

And use that? newFrontendActionFactory is just a convenience function for
cases where it makes your life easier - in the end, what it spews out is a
FrontendActionFactory anyway...

Cheers,
/Manuel


>
> Or am I doing it in the right way?
>
> Any other suggestion? Thanks!
>
> Regards
> Xin Huang
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141014/d3997dc3/attachment.html>


More information about the cfe-dev mailing list