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

Xin Hwang patz.hwang at gmail.com
Mon Oct 13 21:01:22 PDT 2014


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.)

Or am I doing it in the right way?

Any other suggestion? Thanks!

Regards
Xin Huang



More information about the cfe-dev mailing list