From e2915330eeab32c795afe8bb8f18f33874b6b3af Mon Sep 17 00:00:00 2001 From: Dirk Reinbach Date: Tue, 10 Dec 2024 07:56:33 +0000 Subject: [PATCH] Add clang-tidy and include-what-you-use --- .clang-tidy | 2 ++ .devcontainer/Dockerfile | 4 +--- .pre-commit-config.yaml | 36 ++++++++++++++++++++++++++++-------- CMakeLists.txt | 18 ++++++++++++++++++ examples/quickstart.cpp | 2 ++ 5 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 .clang-tidy diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..7561944 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,2 @@ +Checks: '-*,clang-analyzer-*' +WarningsAsErrors: '*,-clang-analyzer-core.uninitialized.UndefReturn' diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index d5ab749..08d5e51 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -8,9 +8,7 @@ RUN apt update && apt upgrade --yes # Install additional packages RUN apt install --yes gcc-14 g++-14 -RUN apt install --yes clang-format pre-commit -RUN apt install --yes lcov -RUN apt install --yes cppcheck +RUN apt install --yes clang-format clang-tidy cppcheck iwyu lcov pre-commit # Choose default gcc and g++ version diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4fb6f9f..7070bdb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,19 +1,30 @@ +fail_fast: false repos: - - repo: https://github.com/pre-commit/mirrors-clang-format - rev: v19.1.3 + - repo: https://github.com/sirosen/check-jsonschema + rev: 0.30.0 hooks: - - id: clang-format - types_or: [c++] + - id: check-github-actions + - id: check-github-workflows - repo: https://github.com/cheshirekow/cmake-format-precommit rev: v0.6.10 hooks: - id: cmake-format - id: cmake-lint - - repo: https://github.com/sirosen/check-jsonschema - rev: 0.30.0 + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v19.1.3 hooks: - - id: check-github-actions - - id: check-github-workflows + - id: clang-format + types_or: [c++] + # - repo: https://github.com/pocc/pre-commit-hooks + # rev: v1.3.5 + # hooks: + # - id: clang-tidy + # args: [-p=build] + # files: \.(h|cpp)$ +# - id: oclint +# - id: cppcheck +# - id: cpplint +# - id: include-what-you-use - repo: local hooks: - id: cppcheck @@ -33,3 +44,12 @@ repos: --error-exitcode=1, ] files: \.(h|cpp)$ + - id: clang-tidy + name: clang-tidy + entry: clang-tidy + language: system + args: + [ + -p=build + ] + files: \.(h|cpp)$ diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ad393a..331f530 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,24 @@ option(TRAITS_BUILD_EXAMPLES "whether or not examples should be built" ON) option(TRAITS_BUILD_TESTS "whether or not tests should be built" ON) option(TRAITS_TEST_COVERAGE "whether or not test coverage should be generated" OFF) +option(TRAITS_COMPILE_COMMANDS "whether or not to generate compile commands database" ON) +option(TRAITS_CLANG_TIDY "whether or not clang-tidy should be run" OFF) +option(TRAITS_INCLUDE_WHAT_YOU_USE "whether or not include-what-you-use should be run" OFF) + +if(TRAITS_COMPILE_COMMANDS) + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +endif() + +if(TRAITS_CLANG_TIDY) + find_program(CLANG_TIDY_EXE NAMES clang-tidy REQUIRED) + set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_EXE}) +endif() + +if(TRAITS_INCLUDE_WHAT_YOU_USE) + find_program(IWYU_EXE NAMES include-what-you-use REQUIRED) + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${IWYU_EXE}) +endif() + # installation rules configure_file("cmake/traits-config-version.cmake.in" diff --git a/examples/quickstart.cpp b/examples/quickstart.cpp index fd8b561..00b4509 100644 --- a/examples/quickstart.cpp +++ b/examples/quickstart.cpp @@ -1,5 +1,7 @@ +#include #include #include +#include #include #include "traits.h"