标签: libclang

使用libclang检查通用属性

我想在下面的例子中解析类成员函数的通用属性:

class Foo
{
public:
    void foo [[interesting]] ();
    void bar ();
};
Run Code Online (Sandbox Code Playgroud)

使用libclang C API,我想在源代码中区分foobar(并知道它foo具有interesting属性).这可能吗?我很难找到解释API中使用的概念的示例或文档(我找到了一个参考,但是当没有解释这些概念时,这很难使用).

c++ clang abstract-syntax-tree libclang

7
推荐指数
1
解决办法
2986
查看次数

libclang获得原始价值

如何使用libclang获取原始文字的值?

例如,如果我有一个游标类型CXCursor_IntegerLiteral的CXCursor,我该如何提取文字值.


更新:

我使用libclang遇到了很多问题.我强烈建议完全避免它,而是使用clang提供的C++接口.C++接口非常有用,并且有很好的文档记录:http://clang.llvm.org/doxygen/annotated.html

我现在看到libclang的唯一目的是为你生成ASTUnit对象,就像下面的代码一样(否则它并不容易):

ASTUnit * astUnit;
{
    index = clang_createIndex(0, 0);
    tu = clang_parseTranslationUnit(
        index, 0,
        clangArgs, nClangArgs,
        0, 0, CXTranslationUnit_None
        );
    astUnit = static_cast<ASTUnit *>(tu->TUData);
}
Run Code Online (Sandbox Code Playgroud)

现在你可能会说libclang是稳定的而C++接口不是.这几乎不重要,因为你用libclang计算AST并用它创造kludges的时间浪费了你的大部分时间.我只是花了几个小时来修复在版本升级后无法编译的代码(如果需要的话).

c++ libclang

6
推荐指数
3
解决办法
2688
查看次数

不能在Qt中使用libclang

当我尝试在Qt应用程序中使用libclang时遇到了一个奇怪的错误.

TEST.CPP

#include <QApplication>
#include <QMainWindow>

#include <clang-c/Index.h>

int main (int argc, char *argv[]) {
    QApplication a(argc, argv);

    QMainWindow w;
    w.show();

    CXIndex index = clang_createIndex(0, 0);
    Q_UNUSED(index)

    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

test.pro

QT += core widgets

TARGET = test
TEMPLATE = app

SOURCES += test.cpp

LIBS += -lclang
Run Code Online (Sandbox Code Playgroud)

Shell命令和输出:

$ ls
test.cpp test.pro
$ qmake
$ make
g++ -c -pipe -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt/mkspecs/linux-g++ -I. -I/usr/include/qt -I/usr/include/qt/QtWidgets -I/usr/include/qt/QtGui -I/usr/include/qt/QtCore -I. …
Run Code Online (Sandbox Code Playgroud)

c++ qt clang libclang

6
推荐指数
1
解决办法
916
查看次数

如何使用libclang检索函数调用参数值

是否可以检索clang.cindex.CursorKind.CALL_EXPR游标的参数值?

当我使用编译器(clang ++ -ast-dump source.cpp)转储AST时,我得到有关函数调用(调用表达式)及其参数的信息.但是我无法使用python的绑定来复制它(使用libclang的解析器检索AST).

这是我正在使用的源代码:

#include <iostream>
#include <GL/glut.h>
#include <EGL/egl.h>

#define MULTILINE(...) #__VA_ARGS__

void renderFunction()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    glBegin(GL_QUADS);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
    glEnd();
    glFlush();
}

int main(int argc, char *argv[])
{          
    glutInit(&argc, argv);       
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("OpenGL - First window demo");
    glutDisplayFunc(renderFunction);
    glutMainLoop();    

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是AST转储的一部分:

|   |-CallExpr 0x430b540 <line:10:5, col:32> 'void'
|   | |-ImplicitCastExpr 0x430b528 <col:5> 'void …
Run Code Online (Sandbox Code Playgroud)

python libclang

6
推荐指数
1
解决办法
1762
查看次数

get_includes 找不到标准库头文件

我正在玩弄 libclang 的 Python 绑定。目前,我正在尝试执行一些非常简单的任务,例如查找 C++ 文件中包含的所有标头。我使用的代码如下:

from clang.cindex import Index

index = Index.create()
tu = index.parse("hello.cpp", args=["-std=c++14"])
for it in tu.get_includes():
    print(it.include.name)
Run Code Online (Sandbox Code Playgroud)

该文件hello.cpp如下:

#include <iostream>
#include <stdio.h>
#include "hello.h"

int main()
{
    std::cout << "Hello world\n";
}
Run Code Online (Sandbox Code Playgroud)

文件hello.h如下:

#include <list>
Run Code Online (Sandbox Code Playgroud)

我认为上面的代码会打印iostream, stdio.hand hello.h,list如果考虑到可传递的包含,可能甚至更多。但是,它只打印./hello.h,公然忽略标准库头文件。

我在文档中找不到任何关于它是否是设计的。是设计的吗?如果是这样,有什么方法可以实际获取文件clang.cindex包含的所有标头,包括标准库的标头?

c++ python libclang

6
推荐指数
1
解决办法
566
查看次数

libclang:添加编译器系统包含路径(Windows中的Python)

按照这个问题Andrew的建议,我试图让liblang 添加编译器系统包含路径(在Windows中)以便我的Python代码

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\sourcefile\test.cpp', args=['-std=c++11'])

parse_decl(trans_unit.cursor)
Run Code Online (Sandbox Code Playgroud)

完全 解析C++源文件像这样的

/* test.cpp
*/
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <iomanip>

using namespace std;

void readfunction(vector<double>& numbers, ifstream& myfile)
{

  double number;
  while (myfile >> number) {
  numbers.push_back(number);}

}

double …
Run Code Online (Sandbox Code Playgroud)

c++ python clang libclang

6
推荐指数
1
解决办法
1114
查看次数

在libclang(Python)中查找特定函数声明的所有引用

我试图通过Python中的libclang解析C++源文件时找到(行和列位置)特定函数声明的所有引用.

例如:

#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;
}
Run Code Online (Sandbox Code Playgroud)

所以,对于上面的源文件,我想要addition第5行的函数声明,我希望find_all_function_decl_references(见下文)返回第addition15行和第16行的引用.

我试过这个(改编自这里)

import clang.cindex
import ccsyspath

index = clang.cindex.Index.create()
translation_unit = index.parse(filename, args=args)

for node in …
Run Code Online (Sandbox Code Playgroud)

c++ python clang libclang

6
推荐指数
1
解决办法
2588
查看次数

使用 libclang 6.0.1:找不到 stddef.h

为了开始使用 libclang,我构建了一个非常简单的程序,尝试加载一个非常简单的源文件。它因找不到“stddef.h”文件而失败。

这是使用 libclang 的程序:

#include <clang-c/Index.h>
int main() {
    CXIndex index = clang_createIndex(1,1);
    const char *source_path = "foo.cpp";
    clang_createTranslationUnitFromSourceFile(index,"foo.cpp",0,0,0,0);
}
Run Code Online (Sandbox Code Playgroud)

(为了简洁起见,我省略了与重现问题无关的代码)。

这是我尝试加载的文件 foo.cpp:

#include <stddef.h>
int main() {}
Run Code Online (Sandbox Code Playgroud)

我使用的是 LLVM 和 Clang 6.0.1,从源代码编译如下:

cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/local -DCMAKE_PREFIX_PATH=$HOME/local -DCMAKE_BUILD_TYPE=Release
make
make install
Run Code Online (Sandbox Code Playgroud)

快速搜索会产生这个有希望的帖子:Clang Error - stddef file not find? 不幸的是,这是关于 llvm 3.5 的,而我使用的是 llvm 6.0.1。另外,我安装 LLVM 和 Clang 的目录 $HOME/local 没有 /usr/lib 目录,因此那里提出的解决方案在这里不起作用。

stddef.h 标头位于$HOME/lib/clang/6.0.1/include/stddef.h。将此路径作为 -isystem 选项显式添加到 clang_createTranslationUnitFromSourceFile 调用中可以解决该问题。

而且clang_createTranslationUnitFromSourceFile使用的include搜索路径与clang++使用的不一样; clang++ foo.cpp工作没有错误。

是否有关于 clang_createTranslationUnitFromSourceFile 和 libclang …

c++ clang libclang

6
推荐指数
1
解决办法
2041
查看次数

为什么这个 python 脚本找不到 libclang dll?

我想开始使用libclangPython。我正在尝试获取在Windows上工作的示例代码( http://www.altdevblogaday.com/2014/03/05/implementing-a-code-generator-with-libclang/ ) ,这是代码的一部分我正在尝试运行:

#!/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:/Program Files (x86)/LLVM/bin/libclang.dll')

# I also tried moving the dll into the Python installation folder.
clang.cindex.Config.set_library_file('C:/Python27/DLLs/libclang.dll')
print("Clang path set")

index = clang.cindex.Index.create()

...
Run Code Online (Sandbox Code Playgroud)

我删除了代码的所有其他部分,但如果它们相关,我可以发布它们。线路

index = clang.cindex.Index.create()
Run Code Online (Sandbox Code Playgroud)

抛出以下错误:

Setting clang path
Clang path set
Traceback (most recent …
Run Code Online (Sandbox Code Playgroud)

python clang libclang

5
推荐指数
2
解决办法
9180
查看次数

在 clang 格式的控制语句后中断

BreakBeforeBraces: Allman在我的.clang-format文件中使用,但控制语句中的大括号(例如if, for, while, ...)没有放在自己的行上。

// Currently:
void foo()
{
    while(true) {
        bar();
    }
}

// What I want:
void foo()
{
    while(true) 
    {
        bar();
    }
}
Run Code Online (Sandbox Code Playgroud)

我读过您可以在 中为大括号设置嵌套配置类BraceWrapping,但我无法找出正确的 YAML 语法(以及 sublime text 插件的 JSON 语法),并且找不到任何现有示例。

有没有办法做到这一点?

c++ formatting clang libclang clang-format

5
推荐指数
1
解决办法
2533
查看次数