【发布时间】:2020-12-18 02:01:52
【问题描述】:
我正在开发一个只有标头的库,并希望使用 clang-tidy 来确保我遵循“C++ 核心指南”https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
无论我尝试什么,我似乎都无法在仅标头库上工作(可能是因为在使用该库之前没有真正编译过)...但是必须有一些解决方法这工作正常。当然,其他人已经编写了一个他们想在其上使用 clang-tidy 的仅标头库。
为了尝试简化问题,我制作了一个小型测试项目来尝试使其正常工作。这个项目只有两个文件。一个 CMakeLists.txt 文件和一个头文件。
CMakeLists.txt:
cmake_minimum_required(VERSION 3.11.4)
project(my_project LANGUAGES CXX)
# This does not seem to work at all for header only libraries
# I even tried messing with the "-header-filter" parameter and had no luck
set(CMAKE_CXX_CLANG_TIDY clang-tidy;-checks=-*,cppcoreguidelines-*)
add_library(my_project INTERFACE)
target_include_directories(my_project
INTERFACE
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
包括/my_project.hpp:
#include <iostream>
// I know it is bad to do this in a header file.
// This is intentional to give clang-tidy something to catch
using namespace std;
template <int N>
void print()
{
for (int i = 0; i < N; ++i)
{
cout << "Hello, world!" << endl;
}
}
当我运行 CMake 时:
mkdir build
cd build
cmake ..
cmake --build .
我没有从 clang-tidy 得到任何输出。我怎样才能使 clang-tidy 仅解析标头库并报告潜在问题?
【问题讨论】:
-
您肯定正在为您的库编写测试,而这些测试是源文件吗?在这些上运行 clang-tidy!
-
我已经尝试过了,它确实适用于那些 .cpp 文件,但没有一个标题被拾取。我为“--header-filter”尝试了多个不同的值,但似乎没有任何效果。 (它们都在“include/”文件夹中。)另一个问题是所有的测试文件都使用了 Catch2 并且有很多宏会引发各种整洁的警告。理想情况下,我也想忽略任何与 Catch2 相关的内容。 (或者只是在处理标头时忽略测试文件)
标签: c++ cmake llvm clang-tidy