[cfe-dev] Inconsistent cursors returned for inclusion statements

waveform waveform at sdf.lonestar.org
Sun Jan 5 11:33:41 PST 2014

When retrieving a cursor at a given offset the result is inconsistent when
the offset is within an #include statement
For both below examples I would expect the cursor kind for line 1, column 16
to be CXCursor_InclusionDirective

  clang -lclang -L/usr/lib/llvm -o test test.c

A  ./test example1.c 1 16
B  ./test example2.c 1 16
C  ./test example2.c 2 16
A  'string.h' (inclusion directive)
B  '' (NoDeclFound)
C  'string.h' (inclusion directive)

Further investigation suggests the exact ordering of the inclusion
directives seems to make a difference and
the inconsistent behaviour affects only the filename and occurs only with
<foo.h> and never with "foo.h"

uname -smr
   Linux 3.11.10-200.fc19.i686.PAE i686
clang --version
   clang version 3.3 (tags/RELEASE_33/final)

Any thoughts?


# example1.c
#include "string.h"

# example2.c
#include <string.h>
#include <string.h>

# test.c
#include <stdlib.h>
#include <clang-c/Index.h>

int main(int argc, char *argv[]) {
  CXIndex idx;
  CXTranslationUnit tr;
  CXFile f;
  CXSourceLocation l;
  CXCursor c;

  idx = clang_createIndex(0, 0);
//tr  = clang_createTranslationUnitFromSourceFile(idx, argv[1], 0, NULL, 0,
  tr  = clang_parseTranslationUnit(idx, argv[1], NULL, 0, NULL, 0,
  f   = clang_getFile(tr, argv[1]);
  l   = clang_getLocation(tr, f, atoi(argv[2]), atoi(argv[3]));
  c   = clang_getCursor(tr, l);

  CXString str1, str2;
  str1 = clang_getCursorSpelling(c);
  str2 = clang_getCursorKindSpelling(clang_getCursorKind(c));
  fprintf(stderr, "'%s' (%s)\n", clang_getCString(str1),

  return 0;

View this message in context: http://clang-developers.42468.n3.nabble.com/Inconsistent-cursors-returned-for-inclusion-statements-tp4036903.html
Sent from the Clang Developers mailing list archive at Nabble.com.

More information about the cfe-dev mailing list