ClangTool 解析代码短路评估在宏预处理时不起作用

我使用 ClangTool 开发了一个代码生成工具,但我发现 Clang 代码解析器无法使用短路评估预处理宏。

这是最小的可重现代码

#include <clang/ASTMatchers/ASTMatchers.h>
#include <clang/Frontend/Frontendactions.h>
#include <clang/Tooling/Tooling.h>
#include <gtest/gtest.h>

using namespace clang::tooling;
using namespace clang::ast_matchers;
using namespace clang;

using namespace llvm::cl;


const char *simpleCode = R"(
#if defined(__MINGW32__)
class A {B}
#endif
#if defined(__MINGW32__) && __has_include(<pthread.h>)
class C {D}
#endif
class X {};
)";

TEST(runToolOnCode,CanSyntaxCheckCode)
{
    EXPECT_TRUE(runToolOnCode(std::make_unique<clang::SyntaxOnlyaction>(),simpleCode));
}

我希望能够正确解析,但我得到了

input.cc:5:43: fatal error: cannot open file '/library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/pthread.h': No such file or directory
Running main() from /Volumes/Demon/Demo/Polaris/cmake-build-debug/_deps/googletest-src/googletest/src/gtest_main.cc
#if defined(__MINGW32__) && __has_include(<pthread.h>)
                                          ^
1 error generated.
/Volumes/Demon/Demo/Polaris/tests/hello.cpp:28: Failure
Value of: runToolOnCode(std::make_unique<clang::SyntaxOnlyaction>(),simpleCode)
  actual: false
Expected: true

  • 关于我的环境
    • MaxOSX 11.3.1
    • XCode 12.5.1
➜  ~ clang++ -E -x c++ - -v
Homebrew clang version 12.0.0
Target: x86_64-apple-darwin20.4.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
 (in-process)
 "/usr/local/Cellar/llvm/12.0.0_1/bin/clang-12" -cc1 -triple x86_64-apple-macosx11.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name - -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-rounding-math -munwind-tables -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -debugger-tuning=lldb -target-linker-version 650.9 -v -resource-dir /usr/local/Cellar/llvm/12.0.0_1/lib/clang/12.0.0 -isysroot /library/Developer/CommandLineTools/SDKs/MacOSX.sdk -stdlib=libc++ -internal-isystem /usr/local/opt/llvm/bin/../include/c++/v1 -internal-isystem /library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /usr/local/Cellar/llvm/12.0.0_1/lib/clang/12.0.0/include -internal-externc-isystem /library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -fdeprecated-macro -fdebug-compilation-dir /Users/abao -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -o - -x c++ -
clang -cc1 version 12.0.0 based upon LLVM 12.0.0 default target x86_64-apple-darwin20.4.0
ignoring nonexistent directory "/library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/library/Developer/CommandLineTools/SDKs/MacOSX.sdk/library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/opt/llvm/bin/../include/c++/v1
 /usr/local/Cellar/llvm/12.0.0_1/lib/clang/12.0.0/include
 /library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/library/Frameworks (framework directory)
End of search list.
QQ516061641 回答:ClangTool 解析代码短路评估在宏预处理时不起作用

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/15908.html

大家都在问