Skip to content

Commit

Permalink
IWYU: phase 1
Browse files Browse the repository at this point in the history
  • Loading branch information
hexagonrecursion committed Dec 11, 2024
1 parent bcc1e30 commit fd6ee1b
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 4 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/include-what-you-use.yml
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions .github/workflows/include-what-you-use/c-to-cpp.py
Original file line number Diff line number Diff line change
@@ -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 = {
"<assert.h>": "<cassert>",
"<complex.h>": "<ccomplex>",
"<ctype.h>": "<cctype>",
"<errno.h>": "<cerrno>",
"<fenv.h>": "<cfenv>",
"<float.h>": "<cfloat>",
"<inttypes.h>": "<cinttypes>",
"<iso646.h>": "<ciso646>",
"<limits.h>": "<climits>",
"<locale.h>": "<clocale>",
"<math.h>": "<cmath>",
"<setjmp.h>": "<csetjmp>",
"<signal.h>": "<csignal>",
"<stdalign.h>": "<cstdalign>",
"<stdarg.h>": "<cstdarg>",
"<stdbool.h>": "<cstdbool>",
"<stddef.h>": "<cstddef>",
"<stdint.h>": "<cstdint>",
"<stdio.h>": "<cstdio>",
"<stdlib.h>": "<cstdlib>",
"<string.h>": "<cstring>",
"<tgmath.h>": "<ctgmath>",
"<time.h>": "<ctime>",
"<uchar.h>": "<cuchar>",
"<wchar.h>": "<cwchar>",
"<wctype.h>": "<cwctype>",
}

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)

7 changes: 7 additions & 0 deletions .github/workflows/include-what-you-use/dependencies.imp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{ "include": [ "@\"gtest/.*\"", "private", "<gtest/gtest.h>", "public" ] },
{ "include": [ "@<gtest/.*>", "private", "<gtest/gtest.h>", "public" ] },
{ "include": [ "\"nlohmann/json.hpp\"", "private", "<nlohmann/json.hpp>", "public" ] },
{ "ref": "glm.imp" },
{ "include": [ "<pngconf.h>", "private", "<png.h>", "public" ] }
]
289 changes: 289 additions & 0 deletions .github/workflows/include-what-you-use/glm.imp

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions CBot/src/CBot/CBot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,3 +33,4 @@
#include "CBot/CBotVar/CBotVar.h"

#include "CBot/stdlib/stdlib_public.h"
// IWYU pragma: end_exports
2 changes: 1 addition & 1 deletion CBot/src/CBot/CBotUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#pragma once

#include "CBot/CBotFileUtils.h"
#include "CBot/CBotFileUtils.h" // IWYU pragma: export

#include <cassert>
#include <cstdint>
Expand Down
3 changes: 1 addition & 2 deletions colobot-base/src/graphics/engine/terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@

#include <sstream>

#include <SDL.h>

#include <SDL_surface.h> // IWYU pragma: keep https://github.com/include-what-you-use/include-what-you-use/issues/1665

// Graphics module namespace
namespace Gfx
Expand Down
3 changes: 2 additions & 1 deletion colobot-base/src/math/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fd6ee1b

Please sign in to comment.