<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Aug 29, 2008, at 6:23 AM, Nico Weber wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Hi,<br><br>what's the best way to get a SourceLocation of a Declarator? I want to  <br>skip all Declarators from system headers, so my ActOnDeclarator looks  <br>like this:<br><br>   Action::DeclTy *<br>   ActOnDeclarator(Scope *S, Declarator &D, DeclTy *LastInGroup) {<br>     SourceLocation loc =  // loc of D<br>     SourceManager& sm = pp.getSourceManager();<br>     if (!sm.isInSystemHeader(loc)) {<br>       // do stuff<br>     }<br>     return MinimalAction::ActOnDeclarator(S, D, LastInGroup);<br>   }<br><br>However, what's the best way to get a SourceLocation of a Declarator?  <br>I can use D.getIdentifierLoc(), but not all Declarators have an  <br>identifier (e.g. `struct s{};`). Likewise, I could use one of the  <br>TypeSpecLocs of the DeclSpec, but each of the typespecs is optional,  <br>too (e.g. `int a;` only has a TypeSpecType, `long a;` does not have a  <br>TypeSpecType location).<br><br></div></blockquote><div><br></div>I don't believe this is true. Just to confirm, I added the following line to MinimalAction::ActOnDeclarator():</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(38, 71, 75); "><span style="color: #000000">  </span><span style="color: #aa0d91">if</span><span style="color: #000000"> (</span><span style="color: #3f6e74">D</span><span style="color: #000000">.</span>getDeclSpec<span style="color: #000000">().</span>getTypeSpecTypeLoc<span style="color: #000000">().</span>isValid<span style="color: #000000">())</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 10px/normal Monaco; color: rgb(196, 26, 22); "><span style="color: #000000">    </span><span style="color: #2e0d6e">printf</span><span style="color: #000000">(</span>"we have a valid source location\n"<span style="color: #000000">);</span></div><div><font class="Apple-style-span" face="Monaco" size="2"><span class="Apple-style-span" style="font-size: 10px;"><br></span></font></div><div><font class="Apple-style-span" face="Monaco" size="2"><span class="Apple-style-span" style="font-size: 10px;"><div>[steve-naroffs-imac-2:llvm/tools/clang] snaroff% cat xx.c</div><div>int a;</div><div>long b;</div><div><br></div><div><div>[steve-naroffs-imac-2:llvm/tools/clang] snaroff% ../../Debug/bin/clang -parse-noop xx.c</div><div>we have a valid source location</div><div>we have a valid source location</div><div><br></div></div></span></font></div><blockquote type="cite"><div>So, is there a general way to get the SourceLocation of a Declarator?</div></blockquote><div><br></div>There isn't. As you note above, many structure definitions don't even have a top-level declarator. </div><div><br></div><div>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. </div><div><br></div><div>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...</div><div><br></div><div>Thoughts?</div><div><br></div><div>snaroff</div><div><br><blockquote type="cite"><div><br><br>Thanks,<br>Nico<br>_______________________________________________<br>cfe-dev mailing list<br><a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev<br></div></blockquote></div><br></body></html>