Clang-Tidy 找不到我的头文件

Clang-Tidy can't find my header files

这里是 clang 和 clang-tidy 的新手。

我有一个具有这种结构的项目: project/ - build/ - cmake/ - component1/ - src/ - someFile.cpp - someFile2.cpp - someFile.hpp - someFile2.hpp - component2/ - etc... -

当我使用 clang-tidy 通过以下命令遍历 project/component1/ 中的所有文件时:clang-tidy project/component1/src/* -checks=-*,clang-analyzer-*,-clang-analyzer-alpha*

它最终会抛出这样的错误: $HOME/project/component1/src/someFile.cpp:18:10: error: 'project/component1/someFile.hpp' file not found [clang-diagnostic-error] \#include "component1/someFile.hpp"

只有当您使用 CMake 管理您的项目时,此答案才会对您有所帮助。

CMake 可以选择创建一个 .json 文件,其中包含所有带有命令行选项的编译器调用。可以使用以下选项将此文件提供给 clang-tidy:

-p <build-path> is used to read a compile command database.

    For example, it can be a CMake build directory in which a file named
    compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
    CMake option to get this output). When no build path is specified,
    a search for compile_commands.json will be attempted through all
    parent paths of the first input file . See:
    http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an
    example of setting up Clang Tooling on a source tree.

如文档所述,您必须设置 CMAKE_EXPORT_COMPILE_COMMANDS 变量以使用 CMake 生成 .json 文件,然后将 CMake 输出目录传递给 clang-tidy。 然后 Clang-tidy 将从 .json 文件中的命令获取包含路径。

我告诉 clang-tidy 使用普通编译器包含搜索它们,但它们必须在双破折号 (--) 之后引入。我也花了一段时间才发现它,因为它不包含在 --help:

clang-tidy -checks='...' <source0> ... -- -Iblabla/ ...

再次阅读选项,您可以尝试 -extra-arg= 参数,但我使用双破折号近似值,因为它允许我将所有选项放在一个文件中,以提供 clang 和 clang-tidy,两者的处理不超过 $(cat $file)


发件人:https://clang.llvm.org/extra/clang-tidy/#using-clang-tidy

clang-tidy is a LibTooling-based tool . You can also specify compilation options on the command line after --

在 CMake 中遇到了同样的问题。我的问题是 Clang-Tidy 无法通过响应文件处理包含目录——至少在版本 10 中是这样。

停用它们解决了问题:

# Disable response files
set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES Off)