Questions tagged [libclang]

LibClang is a stable high level C interface to the clang compiler.

LibClang is a stable high level C interface to clang.

When in doubt, LibClang is probably the best clang interface to use, since it:

  • can be called from any language interfaced with C (python bindings come out of the box),
  • is stable,
  • provides high enough abstraction over clang's implementation details.

LibClang provides the following features:

  • C/C++/ObjC/ObjC++ code parsing and syntax checking,
  • AST indexing and cross-referencing,
  • source code tokenization (e.g. for syntax highlighting),
  • code completion.

Useful links

Related tags

230 questions
5
votes
1 answer

With libclang, how to get cursor for declaration type of a cursor at function call?

I have a cursor that's pointing to a function call for which the prototype is declared inside an included header file. I want to get the location for such declaration, but as can be seen in the gdb output bellow, after getting the type for the…
oblitum
  • 11,380
  • 6
  • 54
  • 120
4
votes
1 answer

Why is Libclang unable to get definition of a function defined in a header file?

I am using LibClang to list down all function calls and their corresponding definition. Following is the python script to do so: def traverse(node): if node.kind == CALL_EXPR: print(node.displayname, node.get_defintion()) for c in…
Jahnvi
  • 43
  • 1
  • 2
4
votes
0 answers

How can I parse c++ template class using libclang and python?

I have the following c++ template class: template class Data { public: T t; }; class A { }; int main() { Data data; return 0; } How can I read member t of Data class ? Maybe I need to read instantiated class Data (where A…
Denis Kotov
  • 857
  • 2
  • 10
  • 29
4
votes
1 answer

Parsing with libclang; unable to parse certain tokens (Python in Windows)

I have some code (taken and adapted from here and here), which uses libclang to parse C++ sourcefiles in Python (Widnows) and get all of its declaration statements, as seen here: import clang.cindex def parse_decl(node): reference_node =…
user6167676
4
votes
0 answers

libclang get AST tree of macro extension

I'm currently busy with using libclang library. My goal is to parse code that contains macro with parameters and to get the type of the macro parameters in order to make syntax coloration. If we consider the following piece of code #define MACRO(X)…
L. Carlier
  • 305
  • 1
  • 7
4
votes
2 answers

Get typedef type and name with libclang?

I can get the name and the type of typedefs if they're anonymous structures and the like, but normal typedefs(eg typedef int size_t) I can only get size_t. How can I get the type "int"?
4
votes
2 answers

Get operator type for CXCursor_BinaryOperator

I'm trying to find an assignment in C++ source file: x = 10; I'm using libclang to parse it and traverse AST. There is an CXCursor_BinaryOperator that represents binary operators. Is there a way to determine whether it is an assignment or any other…
maverik
  • 5,508
  • 3
  • 35
  • 55
4
votes
1 answer

How to get the AST of a single cpp file with clang?

I know that it will never be fully accurate without the headers because C++ isn't context free. Using the classic example of 'A B(C);', it means that it can be recognized as a function declaration or an object definition. Either is fine for me. I…
rubarax
  • 158
  • 1
  • 8
4
votes
3 answers

How to skip subtrees in AST traversal using python bindings for libclang

I have just started using libclang via python bindings. I understand that I can traverse the entire syntax tree (AST) using get_children, but I have not been able to find a get_next_sibling() (or whatever it might be called) function so that I can…
Roger House
  • 481
  • 1
  • 5
  • 12
4
votes
2 answers

construct AST from string with libclang

I generate some c++ codes, and store it in string, I want to construct an AST tree from the string using libclang. How to do it? I don't want to string into extra files.
WhatisThat
  • 267
  • 4
  • 10
4
votes
1 answer

C++ libclang: Retrieving cursor from CXSourceLocation returning wrong cursor?

I am currently writing a simple clone detector using libclang with C++. The program stores cursors using a struct, containing a pointer to the translation unit and the CXSourceLocation gained from calling clang_getCursorLocation(cursor). typedef…
4
votes
1 answer

Statically linking libclang in C code

I'm trying to write a simple syntax checker for C code using the frontend available in libclang. Due to deployment concerns, I need to be able to statically link all the libraries in libclang, and not pass around the .so file that has all the…
kfc9001
  • 41
  • 1
  • 3
3
votes
0 answers

python libclang: How to distinguish const vs constexpr types?

How can we determine if the type of a variable declaration is a constexpr in libclang? (In case it matters, I am using the python bindings) Here is an example to play with. I have not been able to figure out how to distinguish a 'const' vs…
dwbarkley
  • 131
  • 2
3
votes
1 answer

Is it possible to parse function proto in cpp file without header with libclang python

I am struggling parsing cpp file with libclang and python. Problem is that when I don't declare a class foo function is ignored by libclang while it is finding testString and main(). //test.cpp std::string test1::foo(){ } std::string *testString =…
necilAlbayrak
  • 824
  • 6
  • 9
3
votes
1 answer

extract argument & return types from C function pointer with libclang

I'm using libclang (API version 0.43) to parse a C header file. Is there a way to extract the argument & return type(s) from a function pointer definition, e.g. int (*my_add_fn)(int, int); I can get a cursor to my_add_fn, and can see some info…
Ben
  • 843
  • 8
  • 21