[cfe-dev] Parsing a type definition using clang infrastructure

Marco Craveiro marco.craveiro at gmail.com
Fri Apr 3 07:57:14 PDT 2015


Hi Clangers,

First, apologies if this question has already been dealt with in the
mailing list archives, but my google-fu didn't reveal anything
obvious.

I am the main developer of Dogen[1], a c++ code generator. At present
we have a _very_ basic hand-crafted boost::spirit parser [2] that
takes type definitions such as:

std::vector<boost::shared_ptr<some_type>>

and converts it into the domain types we use for code generation.
However, as you can imagine, the parser is extremely limited - mostly
due to my limited boost::spirit knowledge, to be fair - which means we
are missing support for many useful c++ features. In addition, error
reporting is limited to "parses" or "parses not", which is not
helpful.

So it seems time has come to start making use of the clang
infrastructure for this. I have been looking at the tutorials such as
loarabia's [3], but one thing I find in common with all tutorials up
till now is that they assume one needs all of the usual C++
infrastructure such as reading files, parsing includes and so on. I
was hoping to use just a parser with the smallest amount of
infrastructure around it. So my questions are:

- is this possible? i.e. use something like ParseExpr [4] to parse a
string in memory - or, if this is not the correct class, can you
please point me to the correct entry point.
- are there any examples of anyone going down such path that I could
use to get me started? I was hoping to find some unit tests but alas I
have failed so far.

Any other tips you may have are greatly appreciated. We will try to
submit a PR to loarabia's tutorial when we manage to get this working,
in case anyone else has such a need.

Many thanks for a fantastic tool and many thanks for your time.

Cheers

Marco
-- 
After all, bugs often arise when you're not looking for them, as
opposed to when you are, and some bugs, when looked for, vanish --
Kent Fredric

http://about.me/marcocraveiro

[1] https://github.com/DomainDrivenConsulting/dogen
[2] https://github.com/DomainDrivenConsulting/dogen/blob/master/projects/dia_to_sml/src/types/identifier_parser.cpp
[3] https://github.com/loarabia/Clang-tutorial
[4] http://clang.llvm.org/doxygen/ParseExpr_8cpp-source.html



More information about the cfe-dev mailing list