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

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


On Aug 29, 2008, at 6:23 AM, Nico Weber wrote:

> Hi,
>
> what's the best way to get a SourceLocation of a Declarator? I want to
> skip all Declarators from system headers, so my ActOnDeclarator looks
> like this:
>
>   Action::DeclTy *
>   ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {
>     SourceLocation loc =  // loc of D
>     SourceManager& sm = pp.getSourceManager();
>     if (!sm.isInSystemHeader(loc)) {
>       // do stuff
>     }
>     return MinimalAction::ActOnDeclarator(S, D, LastInGroup);
>   }
>
> However, what's the best way to get a SourceLocation of a Declarator?
> I can use D.getIdentifierLoc(), but not all Declarators have an
> 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

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

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). When I was working on the ObjC  
rewriter, I wished clang modeled it this way. Instead, I developed  
hacks to work around it.

If we are moving forward with Ted's DeclGroup proposal, it might make  
sense to consider a Declaration AST node as well? It would certainly  
simplify source analysis tools in particular...

Thoughts?

snaroff

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20080829/cd3463f5/attachment.html>


More information about the cfe-dev mailing list