[cfe-dev] Regarding clang-c and comments...
Douglas Gregor
dgregor at apple.com
Tue Nov 1 11:28:42 PDT 2011
On Oct 26, 2011, at 8:59 PM, Doug wrote:
> Hi there,
>
> 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.
>
> 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:
>
> for each cursor
> get cursor info
> save info (specifically, offset, filename, display name)
>
> for each saved symbol:
> find the closest previous symbol
> walk backwards through the file from position of symbol -> position of previous symbol looking for closest comment
>
> 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).
>
> Is there a to get comments as elements through to the visitor function?
> If no in clang-c, then perhaps in the full clang api?
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
http://llvm.org/viewvc/llvm-project?view=rev&revision=74704
but then reverted all of the code because it never actually got used.
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.
> 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..
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).
- Doug
More information about the cfe-dev
mailing list