ASTMatchers: isVirtual and isOverride

Hi,

I need your help again. Look, in my tool I was trying to use the syntax that’s shown here:

http://clang.llvm.org/docs/LibASTMatchersTutorial.html

Namely I’m referring to this part:

int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());

LoopPrinter Printer;
MatchFinder Finder;
Finder.addMatcher(LoopMatcher, &Printer);

return Tool.run(newFrontendActionFactory(&Finder));
}

However, now I want to create a object “Printer” with different features depending on the arguments provided in command line. So I was thinking on implement a factory method pattern to create a different LoopPrinter object:
class OptionsFactory {
public:
LoopPrinter getOption() {
if(…)
return LoopPrinter(attribute1, attribute2);
else
return LoopPrinter(attribute1);
}
};

I was searching for a better solution and there are some things that I can’t completely understand.

is used for? I see we can ‘associate’ an ASTConsumer to the Frontend as in:
http://clang.llvm.org/docs/RAVFrontendAction.html

but, is this possible using Matchers? Would it have any sense to create an ASTConsumer in my class OptionsFactory?

I have improved a lot since you last helped me, but clang is too big!

By the way, do you know how to use CommandLine? I posted a new thread

http://lists.cs.uiuc.edu/pipermail/cfe-dev/2013-May/029473.html

If you know how to solve that problem, please, let me know.

Thanks in advance,

Pedro.

El dia 27 abr 2013 18:39, Manuel Klimek klimek@google.com escribió:

Given your description, I'm not sure matchers are what you want. If you just want to print information on certain types of nodes, you could use a RecursiveASTVisitor for that.

However, if what you're looking for is a little more complex then matchers may be what you want after all.

As for the two classes, you want to use tooling::MatchFinder as shown in the tutorial. The other is just an implementation detail of the match finding code.

newASTConsumer() is a function that's required to be defined for objects passed to newFrontendActionFactory(). You don't need to implement it. It's implemented by MatchFinder. Again, it's an implementation detail you don't need to worry about at this point.

The use of ASTConsumers is not necessary if you're using MatchFinder and ClangTool as described in the tutorial. MatchFinder is an abstraction around RecursiveASTVisitor so all that stuff in RecursiveASTVisitor you'd normally have to use is actually hidden away.

I think you should first decide which route you want to go: MatchFinder or RecursiveASTVisitor. The first question I'd ask is: how hard is it to find the nodes I want to print info on in the AST. If all I want is every for loop that's easy. If I want for loops within member functions of a specific class, that's hard and an excellent use case for ASTMatchers.