libclang

Finding anonymous enums with libclang

心不动则不痛 提交于 2019-12-03 22:34:22
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() # translation unit parsed correctly tu = idx.parse(sys.argv[1], ['-std=c++11']) assert(len(tu

Generate assembly from C code in memory using libclang

为君一笑 提交于 2019-12-03 17:28:21
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 <clang/Basic/TargetInfo.h> #include <llvm/Support/TargetSelect.h> using namespace std; using namespace

How to find out whether a member function is const or volatile with libclang?

六眼飞鱼酱① 提交于 2019-12-03 16:40:42
问题 I have an instance of CXCursor of kind CXCursor_CXXMethod . I want to find out if the function is const or volatile , for example: class Foo { public: void bar() const; void baz() volatile; void qux() const volatile; }; I could not find anything useful in the documentation of libclang. I tried clang_isConstQualifiedType and clang_isVolatileQualifiedType but these always seem to return 0 on C++ member function types. 回答1: I can think of two approaches: Using the libclang lexer The code which

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

我的未来我决定 提交于 2019-12-03 13:23: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 line 5, I would like the find_all_function_decl_references (see below) to return the references of

How to use libclang with STL?

孤者浪人 提交于 2019-12-03 08:54:18
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 with various STL implementations : Visual studio MingW libCXX In each case, I ended up with unknown

How to extract comments and match to declaration with RecursiveASTVisitor in libclang c++?

老子叫甜甜 提交于 2019-12-03 06:47:10
I am writing a utility which is supposed to parse C++ (and C) header files, extract the structs, enums, fields etc. and generate code in other languages based on the extracted information. I decided to use libclang for this. I'm using a RecursiveASTVisitor and it seems I'm able to extract all the information I need, except for comments. I want to have the comment which appears right above every declaration (field, struct, class, enum) read, and add its text when I generate the code in other languages. The problem is that all the samples I saw which use comments use CxCursor and the C interface

How to find out whether a member function is const or volatile with libclang?

一个人想着一个人 提交于 2019-12-03 06:42:08
I have an instance of CXCursor of kind CXCursor_CXXMethod . I want to find out if the function is const or volatile , for example: class Foo { public: void bar() const; void baz() volatile; void qux() const volatile; }; I could not find anything useful in the documentation of libclang. I tried clang_isConstQualifiedType and clang_isVolatileQualifiedType but these always seem to return 0 on C++ member function types. user1071136 I can think of two approaches: Using the libclang lexer The code which appears in this SO answer works for me; it uses the libclang tokenizer to break a method

Why can't this python script find the libclang dll?

走远了吗. 提交于 2019-12-01 22:57:28
I would like to get started with using libclang with Python . I am trying to get a sample code ( http://www.altdevblogaday.com/2014/03/05/implementing-a-code-generator-with-libclang/ ) to work on Windows , here is a part of the code I'm trying to run: #!/usr/bin/python # vim: set fileencoding=utf-8 import sys import os import clang.cindex import itertools ... print("Setting clang path") # I tried multiple variations. Libclang is correctly installed in the specified location. #clang.cindex.Config.set_library_path('C:/Program Files (x86)/LLVM/bin') #clang.cindex.Config.set_library_path('C:

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

不想你离开。 提交于 2019-12-01 08:33:39
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 = 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 =

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

断了今生、忘了曾经 提交于 2019-12-01 04:44:12
问题 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 = 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