使用 libclang 解析 windows С++ 项目文件(带有 VC 代码库)

Posted

技术标签:

【中文标题】使用 libclang 解析 windows С++ 项目文件(带有 VC 代码库)【英文标题】:Parse windows С++ project files using libclang (with VC code base) 【发布时间】:2020-08-02 09:41:43 【问题描述】:

我需要解析我用 VS C++ 编写的项目的 AST。所以我编译了 llvm/clang 并使用它的 API 来解析 AST。

但出现下一个错误:

G:/Hades/Hades/source/Main.cpp:2:10: fatal error: 'memory' file not found
#include <memory>
         ^~~~~~~~

然后我为预处理器指定了 headerSearch 并添加了路径:"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include" 到 HeaderSearchOptions(通过使用 HeaderSearchOptions::AddPath)来检测缺失的包含。

但是在解析VC头的过程中导致了很多错误,下面是部分日志:

In file included from C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\memory:9:
In file included from C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\yvals_core.h:297:
In file included from C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\vcruntime.h:58:
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\vadefs.h:61:33: error: expected ';' after top level declarator
        typedef unsigned __int64  uintptr_t;
                                ^
                                ;
In file included from G:/Hades/Hades/source/Main.cpp:2:
In file included from C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\memory:9:
In file included from C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\yvals_core.h:297:
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\vcruntime.h:96:1: warning: declaration specifier missing, defaulting to 'int'
_CRT_BEGIN_C_HEADER
^
int
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\vcruntime.h:69:9: note: expanded from macro '_CRT_BEGIN_C_HEADER'
        __pragma(pack(push, _CRT_PACKING)) \
        ^
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\vcruntime.h:96:1: error: use of undeclared identifier 'push'
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\vcruntime.h:69:23: note: expanded from macro '_CRT_BEGIN_C_HEADER'
        __pragma(pack(push, _CRT_PACKING)) \
                      ^
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\vcruntime.h:96:1: error: expected ';' after top level declarator
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.26.28801/include\vcruntime.h:70:9: note: expanded from macro '_CRT_BEGIN_C_HEADER'
        extern "C" 

我该如何解决这个问题?也许我应该在某处指定一些选项,让 clang 知道如果它是 MS 源,它必须解析这些源的具体程度?

或者我必须使用另一个标准包含的代码库吗?也许我必须以某种方式跳过像Windows.h 这样的包含,因为它只是 Windows 标头?

【问题讨论】:

【参考方案1】:

我知道这是一个奇怪的解决方法,因为 memory.h 应该是标准库的一部分。

尝试使用 https://developer.mindsphere.io/resources/mindconnect-lib/doxygen/html/memory_8h.html

#include "mcl/mcl_common.h"

可能会工作并提供所有必要的东西

【讨论】:

以上是关于使用 libclang 解析 windows С++ 项目文件(带有 VC 代码库)的主要内容,如果未能解决你的问题,请参考以下文章

如何在 libclang c++ 中使用 RecursiveASTVisitor 提取注释并匹配声明?

Cppy cmake 构建无法找到 LibClang

Cannot install ‘libclang-dev‘--Ubuntu20.04 安装 libclang-dev 插件

Cannot install ‘libclang-dev‘--Ubuntu20.04 安装 libclang-dev 插件

解析xml kotlin android

架构 i386 的未定义符号 - Lipo 错误?