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

Michael Bolin bolinfest at gmail.com
Fri Apr 4 16:40:08 PDT 2014


Also, I just tried the following on the FunctionDecl that corresponds to
`extern NSString *someFunction1(void) NS_RETURNS_NOT_RETAINED;`:

// const FunctionDecl *decl;
CharSourceRange range =
CharSourceRange::getCharRange(decl->getSourceRange());
LangOptions *opts = new LangOptions();
StringRef sourceText = Lexer::getSourceText(range, *sourceManager, *opts);

The sourceText that I get back is an empty string. For other, non-macro
FunctionDecl nodes that I tried (specifically ones with bodies), I seem to
get the right thing, though the closing curly braces often appears to be
missing.



On Fri, Apr 4, 2014 at 3:28 PM, Michael Bolin <bolinfest at gmail.com> wrote:

> Here is a better example:
>
> https://gist.github.com/bolinfest/9984257
>
> Here are the key lines in the output:
>
>
> |-FunctionDecl 0x10450ca90 <./example.h:15:1, /System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:137:72> someFunction1 'NSString *(void)' extern
>
> |-FunctionDecl 0x10450cc00 <./example.h:16:1, /System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:137:72> someFunction2 'NSString *(void)' extern
>
> Ideally, I would like to be able to get the following instead:
>
>
> |-FunctionDecl 0x10450ca90 <./example.h:15:1, ./example.h:15:61> someFunction1 'NSString *(void)' extern
> |-FunctionDecl 0x10450cc00 <./example.h:16:1, ./example.h:16:61> someFunction2 'NSString *(void)' extern
>
> That is, I would like to be able to extract a SourceRange from a single
> file that I could delete (or comment out) to remove the definition or
> someFunction1 or someFunction2. Obviously the existing SourceRange that I
> get from the AST makes that hard to do.
>
> Is it possible to get my desired SourceRange?
>
> Thank you,
> Michael
>
>
>
> On Wed, Apr 2, 2014 at 1:51 AM, Manuel Klimek <klimek at google.com> wrote:
>
>> 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/20140404/076d9def/attachment.html>


More information about the cfe-dev mailing list