使用 clang AST 解析器忽略丢失的标头 [英] Ignore missing headers with clang AST parser
问题描述
我在 Windows 上,使用 MSVC 编译我的项目,但我需要 clang 来使用它整洁的 AST 解析器,它允许我编写一个小代码生成器.
问题是,clang 无法解析 MSVC 标头(一个众所周知且易于理解的问题).
I'm on Windows, using MSVC to compile my project, but I need clang for its neat AST parser, which allow me to write a little code generator.
Problem is, clang cannot parse MSVC headers (a very-well known and understandable problem).
我尝试了两个选项:
- 我包含了 MSVC 标头文件夹,解析包含在我的代码中的内置标头最终会在某个时候导致致命错误,从而阻止我正确解析我想要的部分.
- 我之前所做的只是不提供任何内置标头并转发声明我需要的类型.它运行良好,不知何故它不再适用于最新的 Clang.我真的不知道关于缺少标头的解析器策略是否已更改,但是每次包含诸如
<string>
之类的内容并且没有太多解析的内容时,它都会导致完全失败.
- I include MSVC header folder, parsing the built-in headers included in my code will end-up leading to a fatal error at some point, preventing me from parsing the parts I want correctly.
- What I did before is simply not provide any built-in headers and forward declare the types I needed. It worked fine and somehow it doesn't anymore with latest Clang. I don't really know if the parser policy on missing header changed, but it is causing complete failure every time something like
<string>
is included and not much get parsed.
我正在使用 python 绑定 (libclang),但如果那里有解决方案,我会考虑切换到 C/C++ API.
I am using the python bindings (libclang), but I would consider switching to C/C++ API if there would be a solution there.
无论如何我可以改变这种行为并使clang继续解析,即使没有找到某些标题?
Is there anyway I can alter this behavior and make clang continue parsing even when some headers are not found ?
推荐答案
使用 SetSuppressIncludeNotFoundError.我花了一个小时才找到!你可以想象我找到它有多高兴!
Use SetSuppressIncludeNotFoundError. Took me an hour to find! You can imagine how glad I was to find it!
https://clang.llvm.org/doxygen/classclang_16fe30300000.16fc60fc32f32e41460855b39d20ff6af
这篇关于使用 clang AST 解析器忽略丢失的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!