[cfe-commits] Patch suggestion for libclang

Argyrios Kyrtzidis kyrtzidis at apple.com
Mon Mar 19 16:24:36 PDT 2012


On Mar 19, 2012, at 3:32 PM, Clint Caywood wrote:

> Hi Argyrios,
> 
> On Mon, Mar 19, 2012 at 4:35 PM, Argyrios Kyrtzidis <kyrtzidis at apple.com> wrote:
> Hi Clint,
> 
> On Mar 18, 2012, at 1:18 PM, Clint Caywood wrote:
> 
>> Ping.
>> 
>> I apologize for the poorly named subject of this submission, but rather than resubmit, I figured it would be better to continue the thread. 
>> 
>> This patch fixes clang_getCursorExtent() when called with a CXCursor of kind CXCursor_TranslationUnit.
>> 
>> Currently, the following snippet (using libclang) will return an empty range:
>> 
>>     CXCursor Cursor = clang_getTranslationUnitCursor(TranslationUnit);
>>     CXSourceRange Range = clang_getCursorExtent(Cursor);
>> 
>> This patch will cause the range to include the entire file from which the translation unit was created. I have a attached the fresh patch.
>> 
>> Thanks,
>> Clint
>> 
>> On Sat, Mar 10, 2012 at 8:40 PM, Clint Caywood <clint at clintcaywood.com> wrote:
>> Hi folks,
>> 
>> I'm new to this, but I've made a patch to libclang that allows it to properly get the range for a translation unit cursor (which I needed for a project I'm working on).  It's pretty straightforward.
>> 
>> Thanks,
>> Clint
>> 
>> Index: tools/libclang/CIndex.cpp
>> ===================================================================
>> --- tools/libclang/CIndex.cpp	(revision 152526)
>> +++ tools/libclang/CIndex.cpp	(working copy)
>> @@ -3869,6 +3869,15 @@
>>      return TU->mapRangeFromPreamble(Range);
>>    }
>>  
>> +  if (C.kind == CXCursor_TranslationUnit) {
>> +    ASTUnit *TU = getCursorASTUnit(C);
>> +    FileID MainID = TU->getSourceManager().getMainFileID();
>> +    SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID);
>> +    SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID);
>> +    SourceRange Range(Start, End);
>> +    return TU->mapRangeFromPreamble(Range);
> 
> The mapRangeFromPreamble() call is not necessary, since the range comes from the main FileID, not the preamble FileID.
> 
> Can you elaborate on how you are using the this extent range later on your application ? Would it be useful to be able to get the extent of any file, not just the main one ?
> 
> -Argyrios
> 
>> +  }
>> +
>>    if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {
>>      Decl *D = cxcursor::getCursorDecl(C);
>>      if (!D)
>> 
>> <tUnitExtents.patch>_______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
> 
> 
> You are correct about the call to mapRangeFromPreamble().
> 
> I'm using this to tokenize the entire contents of the main file, because clang_tokenize() requires a range to be given. My particular application would have no use for retrieving the extents of a file other than the main one, and since there is no CXCursor representation of a file object, I find it intuitive that the source range of a translation unit cursor would be the entire range of the main file.

FYI, the file object is represented with CXFile. Once you have the CXFile of the file you are interested in you can call clang_getLocation with [1,1] (line, column) and then clang_getLocation with a huge line number to get the start/end of the range of the file. Though I consider the "clang_getLocation with a huge line number", to get the end location, a hack, we should really have a "get range for this CXFile" function.

Having said that, I think it makes sense to have clang_getCursorExtent() do as you suggest; committed in r153062.

Thanks!

> 
> Clint

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120319/bea9bd74/attachment.html>


More information about the cfe-commits mailing list