[cfe-dev] Inconsistency in libclang with source ranges?

Manuel Holtgrewe manuel.holtgrewe at fu-berlin.de
Tue Dec 27 15:47:50 PST 2011


Dear all,

I think I found an inconsistency in libclang.  When getting the extent 
of a AST node, you get a source range. As far as I understand (and 
ignoring files), both start and end positions a 1-based pairs (line, 
column).

Here, the end position is exclusive, so for the following compilation 
unit, the range for the conversion function "C::operator int" is 
(2:5)-(4:6). I wrote a small C program (attached) that gets a cursor to 
the conversion function and then prints the range and confirms this finding.

// starts after this line
class C {
     operator int() {
         return 5;
     }
};
// ends before this line

However, when I use this range for retrieving tokens, I get the "}" on 
line 4, too. Apparently, the annotation function interprets the end 
position as inclusive.

Below is the output of c-index-test, I get the same results with Python 
calls to the C functions.

Am I doing something wrong?

Bests,
Manuel

First with the too large range.

$ /opt/clang-3.0/bin/c-index-test -test-annotate-tokens=tiny.cpp:2:5:4:6 
tiny.cpp
Keyword: "operator" [2:5 - 2:13] CXXConversion=operator int:2:5 (Definition)
Keyword: "int" [2:14 - 2:17] CXXConversion=operator int:2:5 (Definition)
Punctuation: "(" [2:17 - 2:18] CXXConversion=operator int:2:5 (Definition)
Punctuation: ")" [2:18 - 2:19] CXXConversion=operator int:2:5 (Definition)
Punctuation: "{" [2:20 - 2:21] CompoundStmt=
Keyword: "return" [3:9 - 3:15] ReturnStmt=
Literal: "5" [3:16 - 3:17] IntegerLiteral=
Punctuation: ";" [3:17 - 3:18] CompoundStmt=
Punctuation: "}" [4:5 - 4:6] CompoundStmt=
Punctuation: "}" [5:1 - 5:2] ClassDecl=C:1:7 (Definition)

Note that the last closing curly brace is wrong.

Then with the smaller, correct range.

$ /opt/clang-3.0/bin/c-index-test -test-annotate-tokens=tiny.cpp:2:5:4:5 
tiny.cpp
Keyword: "operator" [2:5 - 2:13] CXXConversion=operator int:2:5 (Definition)
Keyword: "int" [2:14 - 2:17] CXXConversion=operator int:2:5 (Definition)
Punctuation: "(" [2:17 - 2:18] CXXConversion=operator int:2:5 (Definition)
Punctuation: ")" [2:18 - 2:19] CXXConversion=operator int:2:5 (Definition)
Punctuation: "{" [2:20 - 2:21] CompoundStmt=
Keyword: "return" [3:9 - 3:15] ReturnStmt=
Literal: "5" [3:16 - 3:17] IntegerLiteral=
Punctuation: ";" [3:17 - 3:18] CompoundStmt=
Punctuation: "}" [4:5 - 4:6] CompoundStmt=
-------------- next part --------------
A non-text attachment was scrubbed...
Name: use_clang.c
Type: text/x-csrc
Size: 2127 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20111228/2e167d6c/attachment.c>


More information about the cfe-dev mailing list