0

There is a problem with libclang — it does not seem to be able to get cursor for standard library functions. I have following Python code:

import clang.cindex

def traverse(node, depth=0):
    depth += 1
    print depth * '--', node.spelling or node.displayname, node.kind, node.location
    for c in node.get_children():
        traverse(c, depth)

index = clang.cindex.Index.create()
translation_unit = index.parse('main.cpp', args=['-x', 'c++', '-std=c++11'])
traverse(translation_unit.cursor)

For code like this

 6 int main(int argc, char* argv[])
 7 {
 8    printf("printf %s", "test");
 9    return 1;
10 }

it will print following:

---- main CursorKind.FUNCTION_DECL <SourceLocation file 'main.cpp', line 6, column 5>
------ argc CursorKind.PARM_DECL <SourceLocation file 'main.cpp', line 6, column 14>
------ argv CursorKind.PARM_DECL <SourceLocation file 'main.cpp', line 6, column 26>
------  CursorKind.COMPOUND_STMT <SourceLocation file 'main.cpp', line 7, column 1>
--------  CursorKind.RETURN_STMT <SourceLocation file 'main.cpp', line 9, column 5>
----------  CursorKind.INTEGER_LITERAL <SourceLocation file 'main.cpp', line 9, column 12>

As you see, there is no line 8.

Only way to bypass this issue I see is to process tokens and grab needed functions, but I thought there should be a "cleaner" way to do this. Or maybe I am missing something obvious.

unsigned
  • 11
  • 2
  • that code doesn't compile. It's not surprising you aren't getting expected results. – xaxxon Sep 18 '16 at 22:41
  • 1
    It does not matter if exactly this code does not compile (and it actually does http://pastebin.com/vw3Fz5Mg), in fact it is a part of some larger codebase. Just used as an example. Did you just put a -1 because you can't compile it? – unsigned Sep 19 '16 at 08:57
  • That link isn't the code you posted in the question. The code you posted doesn't compile (like I said). https://godbolt.org/g/PJmgg4 You complained that printf doesn't show up and you didn't include it's header in the code you posted. All we can answer is what you actually ask - we can't read your mind. Why don't you update your question with the actual code you're running your script on? – xaxxon Sep 19 '16 at 08:58
  • Because I asked people who have been working with libclang and C++. – unsigned Sep 19 '16 at 08:59
  • Possible duplicate of [libclang: missing some statements in the AST?](http://stackoverflow.com/questions/14250754/libclang-missing-some-statements-in-the-ast) – Andrew Walker Sep 22 '16 at 07:58
  • @unsigned did you finally find a solution for your question? – user2090491 Apr 15 '18 at 23:33

0 Answers0