[cfe-dev] ASTMatchers: isVirtual and isOverride

Vane, Edwin edwin.vane at intel.com
Mon May 6 13:32:26 PDT 2013


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.

> -----Original Message-----
> From: cfe-dev-bounces at cs.uiuc.edu [mailto:cfe-dev-bounces at cs.uiuc.edu] On
> Behalf Of Pedro Delgado Perez
> Sent: Monday, May 06, 2013 12:58 PM
> To: klimek at google.com; kozargabor at gmail.com
> Cc: cfe-dev at cs.uiuc.edu
> Subject: Re: [cfe-dev] 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.
> 
> - Why are there two classes MatchFinder:
> http://clang.llvm.org/doxygen/classclang_1_1tooling_1_1MatchFinder.html
> http://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder.
> html
> 
> - What the method in ast_matchers:MatchFinder
> 
> clang::ASTConsumer
> <http://clang.llvm.org/doxygen/classclang_1_1ASTConsumer.html>  *
> 
> newASTConsumer
> <http://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder
> .html#a4807049e6e39572d19ff127406df3d81>  ()
> 
> 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 at google.com> escribió:
> 
> 	On Sat, Apr 27, 2013 at 6:36 PM, Gábor Kozár
> <kozargabor at gmail.com> wrote:
> 
> 
> 
> 		2013/4/27 Manuel Klimek <klimek at google.com>
> 
> 
> 			Just use the empty string for binding and getNodeAs :)
> 
> 
> 		That would potentially lead to confusion when there are more
> nodes bound, but the programmer forgot to supply the proper name. In my
> suggestion, the parameterless getNodeAs would have an assert to check there is
> exactly one node bound (and whose name is the default name).
> 
> 	If you put everything behind constants, I think it'll be easy enough to see
> what's happening - and that's a generally good strategy anyway, as you get a
> compile error if you mistype...
> 	Thus, I think it'd not add enough value to special case the interface.
> 
> 
> 
> 
> 		2013/4/27 Manuel Klimek <klimek at google.com>
> 
> 
> 			On Sat, Apr 27, 2013 at 6:28 PM, Gábor Kozár
> <kozargabor at gmail.com> wrote:
> 
> 				Hi,
> 				2013/4/26 Pedro Delgado Perez
> <pedro.delgadoperez at mail.uca.es>
> 
> 
> 					Hehehe... I found the problem with
> this. I was binding wrongly the matcher! I used a id in the matcher thas was
> different from the id in the function that retrieves the nodes... I think this will be
> a typical mistake for newbies...
> 
> 				Ah, yes, that happens a lot to me as well. Now
> that I think about it, it might be worthwhile adding a parameterless .bind() and
> .getNodeAs<T>() for situations where only one node is bound. Should be fairly
> trivial, but also not all that useful...
> 
> 			Just use the empty string for binding and getNodeAs :)
> 
> 
> 
> 				2013/4/26 Pedro Delgado Perez
> <pedro.delgadoperez at mail.uca.es>
> 
> 
> 					Thanks both! Now I can see all this
> much clearer and I have the enough knowledge to start out with clang.
> 
> 
> 				You're welcome. Good luck!
> 
> 
> 	2013/4/25 Manuel Klimek <klimek at google.com>
> 
> 
> 		And btw thanks a lot for all the great user support you're giving
> here!
> 
> 
> 	Thank you, I'm happy to help. Clang is a great project!
> 
> 	As soon as the university term is over, I'm also planning on trying to
> contribute code-wise, mainly to the Static Analyzer but I guess also on just about
> anything that catches my attention. :) I'm quite excited - this is going to be the
> first open source project I contribute to.
> 	Gabor
> 
> 
> 
> 





More information about the cfe-dev mailing list