[cfe-dev] Finding default value for function argument with clang-c API

Milian Wolff mail at milianw.de
Tue Jan 7 03:08:22 PST 2014

On Monday 06 January 2014 22:00:48 Jacob Carlborg wrote:
> On 2014-01-06 17:10, Milian Wolff wrote:
> > Hello again.
> > 
> > When encountering a CXCursor_ParmDecl, how can I figure out if it has a
> > default value and what that value is? E.g.:
> > 
> > void foo(int i = 42);
> > 
> > Here I'd like to know that "i" is 42 by default. A string representation
> > of
> > the default value would be sufficient to me, but even that doesn't seem to
> > be accessible. I could find some range for where the "= 42" is, but then
> > I cannot find any function in the C-API of clang to get a string
> > representation of that range.
> I think you should just continue traversal the AST using
> clang_visitChildren. Also have a look at clang_Cursor_getArgument, you
> might need that as well.

When I'm already encountering a CXCursor_ParmDecl in my visitor, 
clang_Cursor_getArgument would not give me anything else - would it? The 
cursor it returns would point to the same ParmDecl I'm already trying to 

Now, I could continue visitChildren recursively on the ParmDecl cursor to 
figure out if it has any default argument. But I still cannot find any API to 
get a stringified version of the default argument.

In the example above the "42" default value is represented as a 
CXCursor_IntegerLiteral. But neither clang_getCursorSpelling nor 
clang_getCursorDisplayName give me a string containing "42" - both return a 
null CXString...

So, what other function can I use to get the stringified (raw) version of a 

Milian Wolff
mail at milianw.de

More information about the cfe-dev mailing list