[cfe-dev] interface name SourceLocation?
Steve Naroff
snaroff at apple.com
Fri Apr 11 11:47:00 PDT 2008
On Apr 11, 2008, at 10:29 AM, Emerson Murphy-Hill wrote:
> Thanks Steve, this worked. Although I had to use a slightly
> different endBuf, because the Lexer was complaining:
>
> const char *endBuf = SM->getBufferData(LocStart.getFileID()).second;
>
> So that solution works for now, but it's on my wish list for having
> in clang. Relatedly, one other thing for the wish list: the
> SourceLocation for the superclass name. For example, the location of
> token between the ':' and the '{' here:
>
Adding locations for the class/superclass is trivial (I'll add it
today).
Solving the more general problem will require more thought. For example:
@interface C : S <P1, P2, P3> {
For example, now that we have a DeclContext (thanks, Argiris:-), I'd
like to consider a "lazy" API that could provide all the location info
*without* bloating the AST's at birth. We could do this by having
getParent() return a TranslationUnit which could (in turn) store a
pointer to the respective SourceManager(). We could also pass the
SourceManager() in explicitly, however that would make the API less
self contained.
snaroff
> @interface MyClass : MySuperClass {
>
> Thanks again,
>
> e
>
>
>
> On Apr 10, 2008, at 3:11 PM, Steve Naroff wrote:
>>
>> On Apr 10, 2008, at 2:46 PM, Emerson Murphy-Hill wrote:
>>
>>> Howdy Steve,
>>>
>>> I asked Ted this question, but he suggested you might have a
>>> better way to do this. I have an ObjCInterfaceDecl and I need the
>>> location of the name of the interface. That is:
>>>
>>> @interface MyClass
>>> ^ ^
>>> Here to Here
>>>
>>> There's no AST node for that, as far as I know. Ted suggested
>>> using the Lexer, like TextDiagnosticPrinter does, but also thought
>>> there might be an easier way. Is there?
>>>
>>
>> As you noticed, we don't have location info for all interface
>> components. Currently, we only store a SourceLocation for the
>> beginning/middle/end. We could certainly add more SourceLocations
>> if necessary (since I'm not too concerned about the size of an
>> ObjCInterfaceDecl).
>>
>> That said, at the moment, here is the idiom for obtaining a pointer
>> to the characters (I do this in the rewriter).
>>
>> SourceLocation LocStart = CDecl->getLocStart(); // beginning
>> SourceLocation LocEnd = CDecl->getLocEnd(); // marks the '>',
>> '}', or identifier
>> SourceLocation LocAtEnd = CDecl->getAtEndLoc(); // @end
>>
>> SourceManager *SM; // set elsewhere, obviously
>> const char *startBuf = SM->getCharacterData(LocStart);
>> const char *endBuf = SM->getCharacterData(LocEnd);
>>
>> At this point, you can use a Lexer to "formally" tokenize the
>> buffer or you can do adhoc string manipulation (it's your call).
>>
>> Let me know if this works for you.
>>
>> snaroff
>>
>>> Thanks,
>>>
>>> Emerson
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20080411/9bbadefd/attachment.html>
More information about the cfe-dev
mailing list