[cfe-dev] determining Decl's source

Joshua Cranmer Pidgeot18 at verizon.net
Tue Jul 19 16:54:27 PDT 2011


On 7/19/2011 4:44 PM, ret val wrote:
> I am using  RecursiveASTVistor and implementing the VisitDecl() 
> method. While trying to collect all the Decs for function calls and 
> function pointers, I also collect those from standard libraries. I 
> would like to skip these and only look at Decs from the input files.

The way I've been doing this is the following:

   bool interestingLocation(SourceLocation loc) {
     // If we don't have a valid location... it's probably not interesting.
     if (loc.isInvalid())
       return false;
     // I'm not sure this is the best, since it's affected by #line and 
#file
     // et al. On the other hand, if I just do spelling, I get really wrong
     // values for locations in macros, especially when ## is involved.
     std::string filename = sm.getPresumedLoc(loc).getFilename();
     // Invalid locations and built-ins: not interesting at all
     if (filename[0] == '<')
       return false;

     return /* filename is in a directory I want */
   }

(I actually end up having a full hashtable of the returned filenames to 
the absolute path filename, since filenames are a lot more important to 
me than just skipping the "is this interesting")

-- 
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth




More information about the cfe-dev mailing list