I am working on a header only library and would like to use clang-tidy to make sure I am following the "C++ Core Guidelines" https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
No matter what I try I can't seem to get clang-tidy to work on a header only library (probably because nothing is really compiled until the library is used)... But there has to be some workaround to make this work correctly. Surely someone else has written a header only library that they wanted to use clang-tidy on.
To try and simplify the issue I made a small test project to try and get it to work. This project is just two files. A CMakeLists.txt file and a header file.
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>
)
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;
}
}
When I run CMake with:
mkdir build
cd build
cmake ..
cmake --build .
I get no output from clang-tidy. How can I make clang-tidy parse header only libraries and report potential issues?
The following command worked for me:
It looks for header and CPP files, passes them to clang-tidy. The parameter
--header-filter=
accepts a regex for header file paths.In your case it will be: