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

steve naroff snaroff at apple.com
Fri Aug 29 06:31:55 PDT 2008

On Aug 29, 2008, at 9:27 AM, Nico Weber wrote:

> Hi,
>>> 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.

Sure. I thought you were fiddling with ActOnDeclarator() because the  
AST nodes didn't provide the info you needed.

Is this not the case?



> Nico
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

More information about the cfe-dev mailing list