<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Mar 19, 2012, at 3:32 PM, Clint Caywood wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hi Argyrios,<br><br><div class="gmail_quote">On Mon, Mar 19, 2012 at 4:35 PM, Argyrios Kyrtzidis <span dir="ltr"><<a href="mailto:kyrtzidis@apple.com">kyrtzidis@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; position: static; z-index: auto; ">

<div style="word-wrap:break-word"><div><div>Hi Clint,</div><div><div class="h5"><div><br></div><div>On Mar 18, 2012, at 1:18 PM, Clint Caywood wrote:</div><br><blockquote type="cite">Ping.<div><br></div><div>I apologize for the poorly named subject of this submission, but rather than resubmit, I figured it would be better to continue the thread. </div>

<div><br></div><div>This patch fixes clang_getCursorExtent() when called with a CXCursor of kind CXCursor_TranslationUnit.</div>

<div><br></div><div>Currently, the following snippet (using libclang) will return an empty range:<div><br></div><div><div>    CXCursor Cursor = clang_getTranslationUnitCursor(TranslationUnit);</div><div>    CXSourceRange Range = clang_getCursorExtent(Cursor);</div>



</div><div><br></div><div>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.</div><div><br></div><div>Thanks,</div><div>Clint</div><br>



<div class="gmail_quote">On Sat, Mar 10, 2012 at 8:40 PM, Clint Caywood <span dir="ltr"><<a href="mailto:clint@clintcaywood.com" target="_blank">clint@clintcaywood.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">



Hi folks,<div><br></div><div>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.</div>




<div><br></div><div>Thanks,</div><div>Clint</div><div><br></div><div><div>Index: tools/libclang/CIndex.cpp</div><div>===================================================================</div><div>--- tools/libclang/CIndex.cpp<span style="white-space:pre-wrap">     </span>(revision 152526)</div>




<div>+++ tools/libclang/CIndex.cpp<span style="white-space:pre-wrap">     </span>(working copy)</div><div>@@ -3869,6 +3869,15 @@</div><div>     return TU->mapRangeFromPreamble(Range);</div><div>   }</div>
<div> </div><div>+  if (C.kind == CXCursor_TranslationUnit) {</div><div>+    ASTUnit *TU = getCursorASTUnit(C);</div><div>+    FileID MainID = TU->getSourceManager().getMainFileID();</div><div>+    SourceLocation Start = TU->getSourceManager().getLocForStartOfFile(MainID);</div>




<div>+    SourceLocation End = TU->getSourceManager().getLocForEndOfFile(MainID);</div><div>+    SourceRange Range(Start, End);</div><div>+    return TU->mapRangeFromPreamble(Range);</div></div></blockquote></div></div>

</blockquote><div><br></div></div></div><div>The mapRangeFromPreamble() call is not necessary, since the range comes from the main FileID, not the preamble FileID.</div><div><br></div><div>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 ?</div>

<div><br></div><div>-Argyrios</div><br><blockquote type="cite"><div class="im"><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div><div>+  }</div><div>+</div>


<div>   if (C.kind >= CXCursor_FirstDecl && C.kind <= CXCursor_LastDecl) {</div><div>     Decl *D = cxcursor::getCursorDecl(C);</div><div>     if (!D)</div></div>
</blockquote></div><br></div>
</div><span><tUnitExtents.patch></span>_______________________________________________<br>cfe-commits mailing list<br><a href="mailto:cfe-commits@cs.uiuc.edu" target="_blank">cfe-commits@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>

</blockquote></div><br></div></blockquote></div><br><div>You are correct about the call to mapRangeFromPreamble().</div><div><br></div><div>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.</div></blockquote><div><br></div><div>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.</div><div><br></div><div>Having said that, I think it makes sense to have clang_getCursorExtent() do as you suggest; committed in r153062.</div><div><br></div><div>Thanks!</div><br><blockquote type="cite">

<div><br></div><div>Clint</div>
</blockquote></div><br></body></html>