[cfe-dev] ASTMatchers: isVirtual and isOverride

Pedro Delgado Perez pedro.delgadoperez at mail.uca.es
Mon May 6 09:57:47 PDT 2013


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 [ 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(),

 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 {
     LoopPrinter getOption() {
            return LoopPrinter(attribute1, attribute2);
            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_1tooling_1_1MatchFinder.html ]
http://clang.llvm.org/doxygen/classclang_1_1ast__matchers_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 [ 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 [ 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,


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 [ mailto:kozargabor at gmail.com ]> wrote:
> 2013/4/27 Manuel Klimek <klimek at google.com [ mailto: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 [ mailto:klimek at google.com ]>

On Sat, Apr 27, 2013 at 6:28 PM, Gábor Kozár <kozargabor at gmail.com [ mailto:kozargabor at gmail.com ]> wrote:


2013/4/26 Pedro Delgado Perez <pedro.delgadoperez at mail.uca.es [ mailto: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 [ mailto: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 [ mailto: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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130506/d8169313/attachment.html>

More information about the cfe-dev mailing list