首页 > 解决方案 > 宏预处理时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

➜  ~ 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.

标签: clangllvm

解决方案


使用llvm 11.1.0可以绕过这个问题,这可能是版本 12.0.0 中的一个错误


推荐阅读