From fd6ee1be805bc0a0de3002096a0face27d2c1809 Mon Sep 17 00:00:00 2001 From: Andrey Andreyevich Bienkowski Date: Tue, 26 Nov 2024 17:22:36 +0300 Subject: [PATCH] IWYU: phase 1 --- .github/workflows/include-what-you-use.yml | 31 ++ .../include-what-you-use/c-to-cpp.py | 68 +++++ .../include-what-you-use/dependencies.imp | 7 + .../workflows/include-what-you-use/glm.imp | 289 ++++++++++++++++++ CBot/src/CBot/CBot.h | 2 + CBot/src/CBot/CBotUtils.h | 2 +- colobot-base/src/graphics/engine/terrain.cpp | 3 +- colobot-base/src/math/all.h | 3 +- 8 files changed, 401 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/include-what-you-use.yml create mode 100755 .github/workflows/include-what-you-use/c-to-cpp.py create mode 100644 .github/workflows/include-what-you-use/dependencies.imp create mode 100644 .github/workflows/include-what-you-use/glm.imp diff --git a/.github/workflows/include-what-you-use.yml b/.github/workflows/include-what-you-use.yml new file mode 100644 index 000000000..7e40b5096 --- /dev/null +++ b/.github/workflows/include-what-you-use.yml @@ -0,0 +1,31 @@ +on: [push, pull_request] +jobs: + include-what-you-use: + if: false + runs-on: ubuntu-24.04 + steps: + - name: Install dependencies + run: sudo apt-get install iwyu build-essential cmake libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev libsndfile1-dev libvorbis-dev libogg-dev libpng-dev libglew-dev libopenal-dev libphysfs-dev gettext git po4a vorbis-tools libglm-dev + - name: Download the code + uses: actions/checkout@v4 + with: + submodules: recursive + - name: Genarate a compilation database + run: cmake -S . -B build -D=CMAKE_EXPORT_COMPILE_COMMANDS=ON -D=TESTS=ON + - name: Generate iwyu mappings for C++ + run: .github/workflows/include-what-you-use/c-to-cpp.py /usr/share/include-what-you-use ./build + - name: Collect include-what-you-use recommendations + run: | + set -o pipefail; + iwyu_tool \ + -j 4 \ + -p build \ + -- \ + -Xiwyu --no_default_mapping \ + -Xiwyu --mapping_file=$PWD/build/iwyu.gcc.imp \ + -Xiwyu --mapping_file=$PWD/.github/workflows/include-what-you-use/dependencies.imp \ + | tee build/iwyu-log.txt + - name: Apply include-what-you-use recommendations + run: fix_include --safe_headers --noreorder --ignore_re '/lib/' < build/iwyu-log.txt + - name: Check whether there are any changes + run: git diff --exit-code diff --git a/.github/workflows/include-what-you-use/c-to-cpp.py b/.github/workflows/include-what-you-use/c-to-cpp.py new file mode 100755 index 000000000..b80bab350 --- /dev/null +++ b/.github/workflows/include-what-you-use/c-to-cpp.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +''' +Generate C++-centric iwyu mappings. + +The default mappings shipped with iwyu prefer C library headers over their C++ counterparts. +This script generates new mappings with swapped C and C++ headers + +Usage: +c-to-cpp.py path/to/iwyu output/path +''' + +import yaml, glob, pathlib, sys + + +_, SRC_DIR, DST_DIR = sys.argv + + +mapping = { + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", + "": "", +} + +for k, v in list(mapping.items()): + mapping[v] = k + +def transform_row(row): + if 'ref' in row: + return row + ((key, value),) = row.items() + return { + key: [ + mapping.get(s, s) + for s in value + ] + } + +for name in pathlib.Path(SRC_DIR).glob('*.imp'): + with name.open('r') as f: + data = yaml.safe_load(f) + data = [transform_row(row) for row in data] + with (pathlib.Path(DST_DIR) / name.name).open('w') as f: + yaml.dump(data, f) + diff --git a/.github/workflows/include-what-you-use/dependencies.imp b/.github/workflows/include-what-you-use/dependencies.imp new file mode 100644 index 000000000..bb32215bc --- /dev/null +++ b/.github/workflows/include-what-you-use/dependencies.imp @@ -0,0 +1,7 @@ +[ + { "include": [ "@\"gtest/.*\"", "private", "", "public" ] }, + { "include": [ "@", "private", "", "public" ] }, + { "include": [ "\"nlohmann/json.hpp\"", "private", "", "public" ] }, + { "ref": "glm.imp" }, + { "include": [ "", "private", "", "public" ] } +] diff --git a/.github/workflows/include-what-you-use/glm.imp b/.github/workflows/include-what-you-use/glm.imp new file mode 100644 index 000000000..63e1074d6 --- /dev/null +++ b/.github/workflows/include-what-you-use/glm.imp @@ -0,0 +1,289 @@ +[ + +# The below lines were generated by this script: +# SCRATCH="$(mktemp -d)" +# for HEADER in glm/glm.hpp glm/ext.hpp; do +# echo "#include <$HEADER>" \ +# | cpp -x c++ -o /dev/null -H - 2>&1 \ +# | sed 's/^\.* //' \ +# | grep '^/usr/include/glm' \ +# | sort -u \ +# > "$SCRATCH"/includes.txt +# +# echo "/usr/include/$HEADER" >> "$SCRATCH"/seen.txt +# sort -u -o "$SCRATCH"/seen.txt "$SCRATCH"/seen.txt +# join -t '' -v 1 "$SCRATCH"/includes.txt "$SCRATCH"/seen.txt \ +# | sed -e 's%^/usr/include/%%' -e "s%^%{ 'include': [ '<%" -e "s%$%>', 'private', '<$HEADER>', 'public' ] },%" +# +# cat "$SCRATCH"/includes.txt >> "$SCRATCH"/seen.txt +# done + +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, +{ 'include': [ '', 'private', '', 'public' ] }, + +] diff --git a/CBot/src/CBot/CBot.h b/CBot/src/CBot/CBot.h index ecdbba085..6225755c5 100644 --- a/CBot/src/CBot/CBot.h +++ b/CBot/src/CBot/CBot.h @@ -23,6 +23,7 @@ * that should be included by any Colobot files outside of the CBot module. */ +// IWYU pragma: begin_exports #include "CBot/CBotFileUtils.h" #include "CBot/CBotClass.h" #include "CBot/CBotToken.h" @@ -32,3 +33,4 @@ #include "CBot/CBotVar/CBotVar.h" #include "CBot/stdlib/stdlib_public.h" +// IWYU pragma: end_exports diff --git a/CBot/src/CBot/CBotUtils.h b/CBot/src/CBot/CBotUtils.h index 656acd00f..446828fd9 100644 --- a/CBot/src/CBot/CBotUtils.h +++ b/CBot/src/CBot/CBotUtils.h @@ -19,7 +19,7 @@ #pragma once -#include "CBot/CBotFileUtils.h" +#include "CBot/CBotFileUtils.h" // IWYU pragma: export #include #include diff --git a/colobot-base/src/graphics/engine/terrain.cpp b/colobot-base/src/graphics/engine/terrain.cpp index 1fc411a56..0c2007d5c 100644 --- a/colobot-base/src/graphics/engine/terrain.cpp +++ b/colobot-base/src/graphics/engine/terrain.cpp @@ -34,8 +34,7 @@ #include -#include - +#include // IWYU pragma: keep https://github.com/include-what-you-use/include-what-you-use/issues/1665 // Graphics module namespace namespace Gfx diff --git a/colobot-base/src/math/all.h b/colobot-base/src/math/all.h index 328c3dc88..72618654f 100644 --- a/colobot-base/src/math/all.h +++ b/colobot-base/src/math/all.h @@ -24,9 +24,10 @@ #pragma once - +// IWYU pragma: begin_exports #include "math/const.h" #include "math/func.h" #include "math/geometry.h" #include "math/half.h" +// IWYU pragma: end_exports