[cfe-dev] Best way to get SourceLocation of a Declarator?

Nico Weber nicolasweber at gmx.de
Fri Aug 29 06:27:53 PDT 2008


>> identifier (e.g. `struct s{};`). Likewise, I could use one of the
>> TypeSpecLocs of the DeclSpec, but each of the typespecs is optional,
>> too (e.g. `int a;` only has a TypeSpecType, `long a;` does not have a
>> TypeSpecType location).
> I don't believe this is true. Just to confirm, I added the following  
> line to MinimalAction::ActOnDeclarator():
>   if (D.getDeclSpec().getTypeSpecTypeLoc().isValid())
>     printf("we have a valid source location\n");
> [steve-naroffs-imac-2:llvm/tools/clang] snaroff% cat xx.c
> int a;
> long b;
> [steve-naroffs-imac-2:llvm/tools/clang] snaroff% ../../Debug/bin/ 
> clang -parse-noop xx.c
> we have a valid source location
> we have a valid source location

$ touch empty.c
$ clang empty.c
we have a valid source location

I believe that's the `typedef char* __builtin_va_list` that's inserted  
by Preprocessor. So, there's no output for `long b;`.

>> So, is there a general way to get the SourceLocation of a Declarator?
> There isn't. As you note above, many structure definitions don't  
> even have a top-level declarator.

Ok, thanks.

> In the past (in another compiler), I solved this problem by creating  
> a node that represents the entire declaration. The declaration AST  
> node could record the start/end (which would be uniform with how we  
> implement ranges for statements).

While that's certainly useful in general, it doesn't help here,  
because there are no AST nodes when ActOnDeclarator() is called (it's  
part of the parser interface). But it's not a big deal for me, I was  
just wondering.


More information about the cfe-dev mailing list