Right, well thanks for the help.<div><br></div><div>Looks like for the time being it's going to be a case of handling it manually. </div><div><br></div><div>Once the whole project is working I may come back and dig this thread backup again for a bit of help with the PreprocessedEntity as you've describe.</div>
<div><br></div><div>Personally I think it'd be very valuable to support comments as some kind of entity in the AST; its pretty much a fundamental requirement for building an visual studio style editor with contextual tooltips (something I notice the eclipse CDT is particularly poor at, probably for this reason).</div>
<div><br></div><div>Cheers,</div><div>Doug.<br><br><div class="gmail_quote">On Wed, Nov 2, 2011 at 2:28 AM, Douglas Gregor <span dir="ltr"><<a href="mailto:dgregor@apple.com">dgregor@apple.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="im">On Oct 26, 2011, at 8:59 PM, Doug wrote:<br>
<br>
> Hi there,<br>
><br>
> I'm writing a very simple c parser (dumps formatted symbols, something similar to ctags but actually working) using clang-c, which is working well, but I'm hoping to add some support for source comments in the code.<br>

><br>
> At the moment I'm using the clang-c subset for this, but there doesn't seem to be any api for accessing comments; specifically CXCursorKind doesn't seem to have a type for 'comment', and I've ended up doing something similar to:<br>

><br>
> for each cursor<br>
>   get cursor info<br>
>   save info (specifically, offset, filename, display name)<br>
><br>
> for each saved symbol:<br>
>   find the closest previous symbol<br>
>   walk backwards through the file from position of symbol -> position of previous symbol looking for closest comment<br>
><br>
> This is quite annoying to do, and has a few problems (example: symbols with no filename, unsaved files, large projects means loading every symbol into memory).<br>
><br>
> Is there a to get comments as elements through to the visitor function?<br>
> If no in clang-c, then perhaps in the full clang api?<br>
<br>
</div>As Eli noted, Clang's ASTs don't keep track of comments at all. I took a stab at this a few years ago, here<br>
<br>
        <a href="http://llvm.org/viewvc/llvm-project?view=rev&revision=74704" target="_blank">http://llvm.org/viewvc/llvm-project?view=rev&revision=74704</a><br>
<br>
but then reverted all of the code because it never actually got used.<br>
<br>
For Clang's C API, there's another (fairly simple) approach: one could extend the PreprocessingRecord to include comments as a new kind of "PreprocessedEntity", and then provide cursors for those comment entities. It would then be fairly easy to provide an API function in the C API to retrieve the comment text.<br>

<div class="im"><br>
> Finally, as a slightly better alternative, is there a way to query a cursor to get the previous lexical cursor? At the very least this would save me having to save the entire set of data in memory first..<br>
<br>
</div>No, there is no way to get the previous lexical cursor. This information is not stored in the AST (we don't want to pay a pointer per node for something that's rarely needed).<br>
<br>
        - Doug<br>
<br>
</blockquote></div><br></div>