It looks like what you want to do is to run a RecursiveASTVisitor over the AST and essentially cherry-pick certain information off of it. It may be a lot of work, but I think you could do it.<div><br></div><div>Oh, except for the comment. That would be *much* more difficult. In your example you seem to be including the comment as though it were a statement in the body. How would your representation (which reminds me of Prolog btw) represent:</div>
<div><br><span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">  int main(int argc, char * argv[])</span><br style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">
<span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">  {</span><br style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)"><span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">    return </span><span style="background-color:rgb(255,255,255);font-family:arial,sans-serif;font-size:12px"> </span><span style="background-color:rgb(255,255,255);font-family:arial,sans-serif;font-size:12px">/* all ok */</span><span style="background-color:rgb(255,255,255);font-family:arial,sans-serif;font-size:12px"> 0;</span></div>
<div><span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">  }</span><br><br>or</div><div><br></div><div><div><span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">  int main(int argc, char * argv[])</span><br style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">
<span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">  {</span></div><div>    doSomethingWithALotOfArgs(argv[0], argv, argv+argc, /*verbose=*/false);<br style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">
<span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">    return </span><span style="background-color:rgb(255,255,255);font-family:arial,sans-serif;font-size:12px">0;</span></div><div>
<span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">  }</span></div><div><span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)"><br></span></div>
<div>Code like that last example is extremely common.</div><div><br></div><div>For a more pathological example, consider</div><div><br></div><div>  #define X(a,b) a##b</div><div><div><span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">  int X(ma,/*pure evil*/in)(int argc, char * argv[])</span><br style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">
<span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">  {</span></div><div>    doSomethingWithALotOfArgs(argv[0], argv, argv+argc, /*verbose=*/false);<br style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">
<span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">    return </span><span style="background-color:rgb(255,255,255);font-family:arial,sans-serif;font-size:12px">0;</span></div><div>
<span style="font-family:arial,sans-serif;font-size:12px;background-color:rgb(255,255,255)">  }</span></div></div><div><br></div><div>The conclusion is that to actually be useful, your representation would want to make certain things "off limits" or purposefully not representable. It's up to you to draw the line. Once you have that line, you can then get what you want in a pretty straightforward way from clang.</div>
<div><br></div><div>--Sean Silva</div><br><div class="gmail_quote">On Tue, Feb 28, 2012 at 2:21 AM, Philip Ashmore <span dir="ltr"><<a href="mailto:contact@philipashmore.com">contact@philipashmore.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 28/02/12 07:01, Philip Ashmore wrote:<br>
> Hi there.<br>
><br>
> Here's the problem:<br>
> Given the source file with this content:<br>
><br>
>     int main(int argc, char * argv[])<br>
>     {<br>
>       /* all ok */<br>
>       return 0;<br>
>     }<br>
><br>
> I want to convert it into something like this:<br>
><br>
>     module<br>
>     ( function<br>
>       ( Name("main")<br>
>       , Returns("int")<br>
>       , Parameters<br>
>         ( Parameter(Type("int"), Name("argc")<br>
>         , Parameter(Type(Array(Pointer("char"), Size()), Name("argv")<br>
>         )<br>
>       , Body<br>
>         ( Comment("all ok")<br>
>         , Return(Int(0i32))<br>
>         )<br>
>       )<br>
>     )<br>
><br>
> This is a description format that has a binary representation that allows for<br>
> easy depth-first and breadth-first traversal.<br>
><br>
> With it one can describe C/C++, make files, pre-processor macros etc. - the<br>
> reader supplies the meaning to the "calls" like "module".<br>
><br>
> With it I hope to be able to describe things like interfaces and be able to<br>
> automate the glue that allows it to be called from scripting languages,<br>
> and much more.<br>
><br>
> I haven't even given this format a name, but I can convert the text above to<br>
> and from the binary representation.<br>
><br>
> So that's the challenge - any takers?<br>
><br>
> Regards,<br>
> Philip Ashmore<br>
><br>
> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@cs.uiuc.edu">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/mailman/listinfo/cfe-dev</a><br>
</div></div>OK, maybe not this exact example - the parameters are missing ')', but<br>
you get the idea.<br>
<span class="HOEnZb"><font color="#888888"><br>
Philip<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">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/mailman/listinfo/cfe-dev</a><br>
</div></div></blockquote></div><br></div>