<div dir="ltr">Hi,<div><br></div><div>The AST dump above clearly shows how the AST is structured, and that should be all the information you need.</div><div><br></div><div>Basically, you can see that the children of the InitListExpr are the nodes you're interested in. So you bind the whole InitListExpr that you're interested, and you iterate through its children. According to the AST dump, the actual StringLiteral nodes are wrapped by ImplicitCastExpr-s - a phenomenon that's very common in Clang's AST - so you need to throw these away: Expr-s have a lot of methods to do this, IgnoreImpCasts() and so on - see the documentation for the exact naming and descriptions.</div>

<div>After this, all you need to do is llvm::dyn_cast the Expr to StringLiteral, and the StringLiteral class has methods to get the actual string value.</div><div><br></div><div>AST matchers are usually used to give you a rough approximation of the information you need - you'll find that usually you will need to perform some additional logic to get to the data you actually want. AST matchers give you a substitute to the complex state machines you'd have to implement in RecursiveASTVisitor, but they're not meant to be a complete way of getting all the information from the AST.</div>

<div>Nonetheless, if you find that you still want to do this using AST matchers, feel free to implement them yourself using the macros and classes defined in ASTMatchersMacros.h. For examples, the best place to look at is ASTMatchers.h. However, you may find this confusing, and the compiler error messages are not that helpful, due to the fact that AST matchers rely heavily on C++ templates (and some macros).</div>

<div><br></div><div>Hope this helps!</div><div><br></div><div>Gabor</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/8/30 Jesper Eskilson <span dir="ltr"><<a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.com</a>></span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On 08/30/2013 04:17 PM, Vane, Edwin wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Wouldn't binding the result of the initListExpr() give you access to the InitListExpr that you could get the items from? Or did you want something that was a little nicer?<br>
</blockquote>
<br></div>
How do I get out the value used in the initializer? I've tried finding a way to "evaluate" an "Expr *" and get out a value (if possible), but I cannot figure out what API to use to do that. I realize that it may not always be possible to find a value, but it's ok to bail out if the expression does not consist of (string) literals.<br>


<br>
Ideally I'd like to be able to something like this:<br>
<br>
   // Evaluate the initializer list<br>
<br>
   std::vector<std::string> listOfInitializers = initListExpr->Evaluate(...);<div class="im"><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Another option would be to bind to every child of the initListExpr() but then you don't get the matches all at once to your match callback. Rather the callback gets called once per child.<br>
<br>
If you wanted every child at once, you'd need to bind a different name to each child. There's no way to do that currently. Or we'd need some mechanism to collect a bunch of matches under a single name.<br>
</blockquote></div>
I can't do that because I don't know how many children there are.<span class="HOEnZb"><font color="#888888"><br>
<br>
/Jesper</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-----Original Message-----<br>
From: <a href="mailto:cfe-dev-bounces@cs.uiuc.edu" target="_blank">cfe-dev-bounces@cs.uiuc.edu</a> [mailto:<a href="mailto:cfe-dev-bounces@cs.uiuc.edu" target="_blank">cfe-dev-bounces@cs.<u></u>uiuc.edu</a>] On<br>
Behalf Of Jesper Eskilson<br>
Sent: Friday, August 30, 2013 7:23 AM<br>
To: <a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
Subject: [cfe-dev] Getting multiple AST matches as a list<br>
<br>
<br>
Hello,<br>
<br>
I'm learning about how to use AST matchers, but one usecase is giving me<br>
headaches. I have a declaration like this:<br>
<br>
const char *list = { "a", "b" };<br>
<br>
and I would like to retrieve the array initializer as a list of strings in my matcher<br>
callback.  I've managed to get a pointer to the InitListExpr which looks like this:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
InitListExpr 0x46be670 'const char *[2]'<br>
|-ImplicitCastExpr 0x46be6c0 'const char *' <ArrayToPointerDecay><br>
|`-StringLiteral 0x46be5b8 'const char [12]' lvalue "semihosting"<br>
`-ImplicitCastExpr 0x46be6d8 'const char *' <ArrayToPointerDecay><br>
   `-StringLiteral 0x46be5f0 'const char [4]' lvalue "swo"<br>
</blockquote>
I've looked through the traversal matchers, but it seems like I can only get<br>
matchers which will generate a match for each element in the list.<br>
Is there any way to generate a single match with all elements wrapped up in a<br>
list?<br>
<br>
/Jesper<br>
--<br>
*Jesper Eskilson* /Development Engineer/ IAR Systems AB Box 23051,<br>
Strandbodgatan 1<br>
SE-750 23 Uppsala, SWEDEN<br>
E-mail: <a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.com</a> <mailto:<a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.<u></u>com</a>><br>
Website: <a href="http://www.iar.com" target="_blank">www.iar.com</a><br>
<<a href="http://www.iar.com" target="_blank">http://www.iar.com</a>> Twitter: <a href="http://www.twitter.com/iarsystems" target="_blank">www.twitter.com/iarsystems</a><br>
<<a href="http://www.twitter.com/iarsystems" target="_blank">http://www.twitter.com/<u></u>iarsystems</a>><br>
______________________________<u></u>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/cfe-dev</a><br>
</blockquote></blockquote>
<br>
<br>
-- <br>
*Jesper Eskilson* /Development Engineer/<br>
IAR Systems AB<br>
Box 23051, Strandbodgatan 1<br>
SE-750 23 Uppsala, SWEDEN<br>
E-mail: <a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.com</a> <mailto:<a href="mailto:jesper.eskilson@iar.com" target="_blank">jesper.eskilson@iar.<u></u>com</a>> Website: <a href="http://www.iar.com" target="_blank">www.iar.com</a><br>


<<a href="http://www.iar.com" target="_blank">http://www.iar.com</a>> Twitter: <a href="http://www.twitter.com/iarsystems" target="_blank">www.twitter.com/iarsystems</a> <<a href="http://www.twitter.com/iarsystems" target="_blank">http://www.twitter.com/<u></u>iarsystems</a>><br>


______________________________<u></u>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/cfe-dev</a><br>
</div></div></blockquote></div><br></div>