libclang

Find all references of specific function declaration in libclang (Python)

混江龙づ霸主 提交于 2019-12-06 02:39:26
问题 I am trying to find (line and column position) all the references of a specific function declaration when parsing a C++ source file via libclang in Python. For example: #include <iostream> using namespace std; int addition (int a, int b) { int r; r=a+b; return r; } int main () { int z, q; z = addition (5,3); q = addition (5,5); cout << "The first result is " << z; cout << "The second result is " << q; } So, for the source file above, I would like for the function declaration for addition in

Python clang does not search system include paths

十年热恋 提交于 2019-12-05 11:53:45
When using libclang from Python, it doesn't seem to automatically search the system's include paths. Is there a reliable way to get these paths? I don't like hardcoding paths as I'm writing code that will run on a variety of UNIX systems. For example, given test.cpp #include <stdio.h> int main() { puts("Hello, world!"); } and test.py from clang.cindex import Index tu = Index.create().parse(None, ["test.cpp"]) print(list(tu.diagnostics)) running python test.py will print: [<Diagnostic severity 4, location <SourceLocation file 'test.cpp', line 1, column 10>, spelling "'stdio.h' file not found">]

Finding anonymous enums with libclang

北战南征 提交于 2019-12-05 10:07:26
问题 Is there a way to detect anonymous enumerations using libclang without relying on the text in the spelling name? The python bindings to libclang include functionality to detect whether C/C++ structs or unions are anonymous using clang.cindex.Cursor.is_anonymous, which ends up calling clang_Cursor_isAnonymous. The following sample demonstrates the issue. import sys from clang.cindex import * def nodeinfo(n): return (n.kind, n.is_anonymous(), n.spelling, n.type.spelling) idx = Index.create() #

Generate assembly from C code in memory using libclang

孤人 提交于 2019-12-05 02:15:25
问题 I need to implement a library that compiles C code to eBPF bytecode using LLVM/Clang as backend. The codes will be read from memory and I need to get the resultant assembly code in memory too. Until now, I have been able to compile to LLVM IR using the following code: #include <string> #include <vector> #include <clang/Frontend/CompilerInstance.h> #include <clang/Basic/DiagnosticOptions.h> #include <clang/Frontend/TextDiagnosticPrinter.h> #include <clang/CodeGen/CodeGenAction.h> #include

Libclang API to get function definitions defined in a different file

99封情书 提交于 2019-12-04 20:53:19
Suppose I have two files main.c and func.c func.c is called from main.c's main function. Normally, I would generate main.o and func.o and linker would find definition of func and tie it up to it's call in main .c Now, I want to do same thing through libclang APIs. This is for a Doxygen type code browsing utility I am making. I am able to parse the two files. From here, I don't know how to proceed. Should I generate *.o files and make clang link them? Thanks, I hope I am clear in asking the question No, there is no need for actually compiling your code to object files. The link between symbols

Any tutorial on libclang? [closed]

冷暖自知 提交于 2019-12-04 18:23:53
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I've been looking for some easy to understand guide into libclang. I've seen some threads either here or other forums but the only recommended sources of information were libclang source/doxygen doc, clang complete plugin for vim or Thinking Beyond the Compiler presentation. However, none of them provide

How to use libclang with STL?

断了今生、忘了曾经 提交于 2019-12-04 14:10:19
问题 I'm trying to parse a library using libclang, and I'm stuck with what could be a very simple issue: how to configure it with STL? At the moment, it fails to parse a translation unit because it can't find <string> . Here's what I tried : char *args[] = {"-x", "c++", "-Ic:/my/library/includes", "-IG:/Prog/libcxx-3.4/include"}; clang_parseTranslationUnit(index, "c:/my/library/test.cpp", args, 4, 0, 0, 0); I'm on windows, with the precompiled clang binaries downloaded from llvm.org, and I tried

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

时光毁灭记忆、已成空白 提交于 2019-12-04 09:51:15
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 struct { CXTranslationUnit* tu; CXSourceLocation srcLoc; } t_cursorLocation; For the sake of this error, the child visitor function visits each node and creates a struct from each cursor. With a struct of type t_cursorLocation, I wrote this function to retrieve the corresponding cursor: CXCursor getCursor(t_cursorLocation *loc1) { return clang

libclang: add compiler system include path (Python in Windows)

≯℡__Kan透↙ 提交于 2019-12-04 05:08:18
Following this question and Andrew's suggestions, I am trying to have liblang add the compiler system include paths (in Windows) in order for my Python code import clang.cindex def parse_decl(node): reference_node = node.get_definition() if node.kind.is_declaration(): print(node.kind, node.kind.name, node.location.line, ',', node.location.column, reference_node.displayname) for ch in node.get_children(): parse_decl(ch) # configure path clang.cindex.Config.set_library_file('C:/Program Files (x86)/LLVM/bin/libclang.dll') index = clang.cindex.Index.create() trans_unit = index.parse(r'C:\path\to

AST generated by Libclang’s python binding unable to parse certain tokens in C++ source codes

旧街凉风 提交于 2019-12-04 02:39:07
问题 I am using Libclang’s python binding. I have basically two queries: I want to know how can we parse library function which are neither defined by user nor for which a library has been included.. For e.g. when I have the following source code – char* a=(char *)malloc(4); Libclang is unable to parse malloc( ) because neither stdlib has been included in this code nor has a user-defined definition provided for malloc. An object not defined using a constructor is not recognized by Libclang’s AST.