[cfe-dev] Instantiating a ToolAction with constructor args

James Dennett via cfe-dev cfe-dev at lists.llvm.org
Mon Jul 4 22:43:12 PDT 2016


On Mon, Jul 4, 2016 at 9:27 PM, Himanshu via cfe-dev <cfe-dev at lists.llvm.org
> wrote:

> Hi All,
>
> I am fairly new to Clang, so please forgive me if my question is dumb. I
> am trying to create a ASTFrontendAction to visit functions with a specific
> name and modifying them. I do the following:
>
> *class* FindNamedFunctionAction: *public* clang::ASTFrontendAction {
>
> *public*:
>
> *virtual* std::unique_ptr<clang::ASTConsumer> *CreateASTConsumer*(
>
> clang::CompilerInstance &Compiler, *const* llvm::StringRef InFile) {
>
>             // implementation of creating consumer and visitor etc. ...
>
> }
>
> };
> I would like the function name to be a parameter to the action. If I
> provide an explicit argument
> to the constructor of this action by having something like:
>
> *class* FindNamedFunctionAction: *public* clang::ASTFrontendAction {
>
> public:
>
> FindNamedFunctionAction(std::string Name): Name(Name){}
>
> private:
>
> std::string Name;
>
> ...
>
> };
>
>
> But doing this doesn't work with running the tool with:  tool.*run*(
> *newFrontendActionFactory*<FindNamedFunctionAction>().get());
>  I can't find any way of instantiating my action so that I can pass it to
> ClangTool's run method. Could anyone kindly point me in the right direction?
>
>
You're close; to complete the picture you need to create your own factory
`newMyFrontendActionFactory(std::string Name)` that returns a
FindNamedFrontendAction, and pass that:
  tool.run(newMyFrontendActionFactory(Name).get());

-- James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160704/0905b28b/attachment.html>


More information about the cfe-dev mailing list