[cfe-dev] SourceRange for a macro-expanded function definition?

Manuel Klimek klimek at google.com
Wed Apr 2 01:51:34 PDT 2014


On Wed, Apr 2, 2014 at 2:31 AM, Michael Bolin <bolinfest at gmail.com> wrote:

> Hi, I am traversing the AST for an obj-c file using a clang plugin. I have
> a code snippet like the following:
>
> #define JK_ATTRIBUTES(attr, ...)        __attribute__((attr,
> ##__VA_ARGS__))
> #define JK_STATIC_INLINE static __inline__ JK_ATTRIBUTES(always_inline)
>
> JK_STATIC_INLINE int jk_parse_skip_newline();
>
> @implementation Hello
> - (void)say:(int)i {
>   if (i > 0) {
>     NSLog(@"Hello, world! (%d)", i);
>   }
>
>   // Example of selector with unknown method declaration.
>   [(id)@"Anything" length];
> }
> @end
>
> JK_STATIC_INLINE int jk_parse_skip_newline() {
>   return 0;
> }
>
> so while traversing the AST, I am trying to find the SourceRange of the
> jk_parse_skip_newline() definition at the bottom of the file. For non-macro
> expanded functions, this is very straightforward. But due to the macro
> expansion, I get a SourceRange that spans from the start of the macro to
> the end of my code snippet.
>

How do you get that? If you only use functions that handle SourceRanges
(and never combine locations you get from getBegin() getEnd()) that should
not be possible.
Take a look at Lexer::makeFileCharRange and Lexer::getSourceText for how to
deal with macro-ized ranges.


> That means that it also includes the Hello class, which has nothing to do
> with jk_parse_skip_newline(). This is a problem because I am trying to use
> the AST to determine where jk_parse_skip_newline is defined so I can
> comment it out of the source file.
>
> Is it possible for me to get the necessary SourceRange from the clang API?
> Ideally, I would get a SourceRange that identifies only the last three
> lines of my code sample.
>
> Thank you,
> Michael
>
> _______________________________________________
> 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/20140402/08c9ee41/attachment.html>


More information about the cfe-dev mailing list