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

Argiris Kirtzidis akyrtzi at gmail.com
Fri Aug 29 08:38:58 PDT 2008


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).
>
> So, is there a general way to get the SourceLocation of a Declarator?
>   

Hi Nico,

ActOnDeclarator will be called when there is an identifier for the 
declaration, (if there isn't, it's erroneous declaration), so you can 
use D.getIdentifierLoc() for that.
If it's a only-declspec declaration (like in "struct s{};"), 
ParsedFreeStandingDeclSpec will be called, and you can use 
DS.getSourceRange() then (DS is the DeclSpec passed to 
ParsedFreeStandingDeclSpec).


-Argiris



More information about the cfe-dev mailing list