standardese/cppast

source location of parsed entities

Philip-Trettner opened this issue · 5 comments

Hopefully I didn't miss anything but I searched for it in the code:

Is there a way to get the line number where a cpp_entity was found during parsing?
For example, the line of a cpp_include_directive.

The only line information I found was in the cpp_doc_comment.

No, that is not possible. Why do you need it?

I'm trying to build a documentation generator and want to generate direct links to the repository for each documented entity.
For example, const char* to_string(cpp_entity_kind kind) noexcept; would get a link to https://github.com/foonathan/cppast/blob/master/include/cppast/cpp_entity_kind.hpp#L69 and for that line information would be nice.

When you say "not possible", do you mean it is just not exposed or is the information already lost after libclang?

(if it's the former I wouldn't mind working on a PR)

When you say "not possible", do you mean it is just not exposed or is the information already lost after libclang?

The information is not exposed, but it could be. libclang gives you the location of a cursor, and you can extract line/column number etc. like so: https://github.com/foonathan/cppast/blob/master/src/libclang/libclang_parser.cpp#L552
It would just need to be stored in a cpp_entity here: https://github.com/foonathan/cppast/blob/master/src/libclang/libclang_parser.cpp#L633

Thanks! I'll try that.

BTW, since I've started this project clang added -ast-dump=json option, which prints the AST as JSON. That way gives you better support for C++17/20.