【发布时间】:2018-04-23 18:35:47
【问题描述】:
我编写了一个脚本来使用 libclang 库解析 C/C++ 文件。
我试图解析的文件:osf_sys.c。程序打印文件中的每个函数/变量等。示例输出:
...
CURSORKIND: 6; __user at line: 469
CURSORKIND: 43; struct proplistname_args at line: 469
CURSORKIND: 6; nbytes at line: 470
...
所以我运行我的脚本./script osf_sys.c 并且解析大部分都有效。但是,我注意到osf_sys.c 的某些部分没有被打印(缺少变量等等),我怀疑它与头文件(位于单独的目录中)有关。我添加了一个代码 sn-p 来帮助调试/打印如下所示的错误消息。
尝试 1
使用CMake 构建脚本后,我继续运行它。这是打印的错误:
/Linux_Kernel/linux/arch/alpha/kernel/osf_sys.c:13:10:
fatal error: 'linux/errno.h' file not found
似乎在查找此osf_sys.c 的头文件时遇到了一些问题。所以我将此添加到CMakeLists.txt(这可能是错误的,因为问题似乎不在于我的程序的编译,而在于 libclang 如何定位内容):
-I/Linux_Kernel/linux/include
注意errno.h的完整路径是/Linux_Kernel/linux/include/linux/errno.h
尝试 2
我决定将整个 include/ 文件夹拖到 osf_sys.c 所在的同一目录中。
现在,运行它会给我一个新的错误打印输出:
/Linux_Kernel/linux/arch/alpha/kernel/osf_sys.c:13:10: error:
'linux/errno.h' file not found with <angled> include; use "quotes" instead
接着是:
/Linux_Kernel/linux/arch/alpha/kernel/linux/errno.h:4:10:
fatal error: 'asm/errno.h' file not found
好的,现在它似乎正在读取一些东西。果然,有问题的行是来自osf_sys.c 文件的#include <linux/errno.h>。
出现第二条错误消息大概是因为我没有将asm/ 文件夹拖到与osf_sys.c 相同的工作目录中。
尝试 3
因此,根据要求,我手动编辑 osf_sys.c 以将 #include <linux/errno.h> 更改为 #include "linux.errno.h"
这一次,我得到以下错误消息:
/Linux_Kernel/linux/arch/alpha/kernel/linux/errno.h:4:10:
fatal error: 'asm/errno.h' file not found
尝试 4
将-I/Linux_Kernel/linux/include 作为参数传递到脚本中。该脚本对CXTranslationUnit 使用以下参数:
CXTranslationUnit translationUnit = clang_parseTranslationUnit(index, nullptr, argv, argc, 0, 0, CXTranslationUnit_KeepGoing);
这会产生其他类型的错误消息:
warning: 'linker' input unused [-Wunused-command-line-argument]
/Linux_Kernel/linux/arch/alpha/kernel/linux/errno.h:4:10: error: 'asm/errno.h' file not found
/Linux_Kernel/linux/include/linux/types.h:4:10: error: 'asm/types.h' file not found
...
还有什么事情要做?
我无意编辑所有 #include 语句(在每个 linux 内核文件中)以使用双引号并将 include/ 文件夹拖到带有 .c 文件的每个目录中。
不胜感激有关如何使 libclang 搜索我指定的 include/ 文件目录的一些建议。
关于如何解决这个问题的任何建议/替代方案?
【问题讨论】:
-
您可以重试尝试#3,但也可以添加包含
asm/errno.h的包含目录 -
你解决过这个问题吗?我似乎和你在同一条船上。
标签: c++ cmake clang clang++ libclang