Clang scan-build 将 CXX 编译器识别为 GNU 9.1.0,而不是 clang

Clang scan-build identifying CXX compiler as GNU 9.1.0, not clang

我正在尝试将 scan-build 与 cmake 结合使用。 在 scan-build cmake 之后关注 scan-build make。 但是当使用 scan-build 时,它将 CXX 编译器识别为 GNU 9.1.0。但是,如果我们不使用扫描构建,它会将 CXX 正确识别为 clang。由于 CXX 编译器标识为 GNU 9.1.0,检查 clang 中的某些诊断标志(例如,Wunreachable_code_break, Werror_range_loop_analysis)失败。

输出,同时使用扫描构建:

scan-build: Using '../clang/9.0.0/bin/clang-9' for static analysis
-- The CXX compiler identification is GNU 9.1.0
-- Check for working CXX compiler: ../clang/9.0.0/libexec/c++-analyzer
-- Check for working CXX compiler: ../clang/9.0.0/libexec/c++-analyzer -- works

输出,没有扫描构建:

-- The CXX compiler identification is Clang 9.0.0
-- Check for working CXX compiler: ../clang/9.0.0/bin/clang++
-- Check for working CXX compiler: ../clang/9.0.0/bin/clang++ -- works

如何确保扫描构建必须将 clang 识别为 CXX 编译器?

正在阅读Manual

The script uses simple heuristics to determine which compiler should be used (it defaults to clang on Darwin and gcc on other platforms).

...

scan-build provides the --use-cc and --use-c++ options to hardwire which compiler scan-build should use for building your code.

您需要更改 CMake 配置,以便对 scan-build 的调用看起来像...

scan-build --use-c++ ${CMAKE_CXX_COMPILER} ...