]> git.sesse.net Git - stockfish/commitdiff
Let CI check C++ includes
authorSebastian Buchwald <UniQP@web.de>
Mon, 11 Sep 2023 22:03:04 +0000 (00:03 +0200)
committerJoost VandeVondele <Joost.VandeVondele@gmail.com>
Fri, 22 Sep 2023 17:12:53 +0000 (19:12 +0200)
The commit adds a CI workflow that uses the included-what-you-use (IWYU)
tool to check for missing or superfluous includes in .cpp files and
their corresponding .h files. This means that some .h files (especially
in the nnue folder) are not checked yet.

The CI setup looks like this:
- We build IWYU from source to include some yet unreleased fixes.
  This IWYU version targets LLVM 17. Thus, we get the latest release
  candidate of LLVM 17 from LLVM's nightly packages.
- The Makefile now has an analyze target that just build the object
  files (without linking)
- The CI uses the analyze target with the IWYU tool as compiler to
  analyze the compiled .cpp file and its corresponding .h file.
- If IWYU suggests a change the build fails (-Xiwyu --error).
- To avoid false positives we use LLVM's libc++ as standard library
- We have a custom mappings file that adds some mappings that are
  missing in IWYU's default mappings

We also had to add one IWYU pragma to prevent a false positive in
movegen.h.

https://github.com/official-stockfish/Stockfish/pull/4783

No functional change

.github/workflows/libcxx17.imp [new file with mode: 0644]
.github/workflows/stockfish.yml
.github/workflows/stockfish_analyzers.yml [new file with mode: 0644]
src/Makefile
src/evaluate.h
src/movegen.h

diff --git a/.github/workflows/libcxx17.imp b/.github/workflows/libcxx17.imp
new file mode 100644 (file)
index 0000000..7bdcf5b
--- /dev/null
@@ -0,0 +1,21 @@
+[
+    # Mappings for libcxx's internal headers
+    { include: [ "<__fwd/fstream.h>", private, "<iosfwd>", public ] },
+    { include: [ "<__fwd/ios.h>", private, "<iosfwd>", public ] },
+    { include: [ "<__fwd/istream.h>", private, "<iosfwd>", public ] },
+    { include: [ "<__fwd/ostream.h>", private, "<iosfwd>", public ] },
+    { include: [ "<__fwd/sstream.h>", private, "<iosfwd>", public ] },
+    { include: [ "<__fwd/streambuf.h>", private, "<iosfwd>", public ] },
+    { include: [ "<__fwd/string_view.h>", private, "<string_view>", public ] },
+
+    # Mappings for includes between public headers
+    { include: [ "<ios>", public, "<iostream>", public ] },
+    { include: [ "<streambuf>", public, "<iostream>", public ] },
+    { include: [ "<istream>", public, "<iostream>", public ] },
+    { include: [ "<ostream>", public, "<iostream>", public ] },
+    { include: [ "<iosfwd>", public, "<iostream>", public ] },
+
+    # Missing mappings in include-what-you-use's libcxx.imp
+    { include: ["@<__condition_variable/.*>", private, "<condition_variable>", public ] },
+    { include: ["@<__mutex/.*>", private, "<mutex>", public ] },
+]
index 8ea1837d032eb1ff563933f87dbef4e9d9c773ac..1ed4b92d4ca79d717656f3aee682279413c40413 100644 (file)
@@ -33,6 +33,8 @@ jobs:
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 
+  Analyzers:
+    uses: ./.github/workflows/stockfish_analyzers.yml
   Sanitizers:
     uses: ./.github/workflows/stockfish_sanitizers.yml
   Tests:
diff --git a/.github/workflows/stockfish_analyzers.yml b/.github/workflows/stockfish_analyzers.yml
new file mode 100644 (file)
index 0000000..5f985cc
--- /dev/null
@@ -0,0 +1,47 @@
+name: Stockfish
+on:
+  workflow_call:
+jobs:
+  Analyzers:
+    name: Check includes
+    runs-on: ubuntu-22.04
+    defaults:
+      run:
+        working-directory: Stockfish/src
+        shell: bash
+    steps:
+      - name: Checkout Stockfish
+        uses: actions/checkout@v3
+        with:
+          path: Stockfish
+
+      - name: Checkout include-what-you-use
+        uses: actions/checkout@v3
+        with:
+          repository: include-what-you-use/include-what-you-use
+          ref: f25caa280dc3277c4086ec345ad279a2463fea0f
+          path: include-what-you-use
+
+      - name: Download required linux packages
+        run: |
+          sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main'
+          wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
+          sudo apt update
+          sudo apt install -y libclang-17-dev clang-17 libc++-17-dev
+
+      - name: Set up include-what-you-use
+        run: |
+          mkdir build && cd build
+          cmake -G "Unix Makefiles" -DCMAKE_PREFIX_PATH="/usr/lib/llvm-17" ..
+          sudo make install
+        working-directory: include-what-you-use
+
+      - name: Check include-what-you-use
+        run: include-what-you-use --version
+
+      - name: Check includes
+        run: >
+          make analyze
+          COMP=clang
+          CXX=include-what-you-use
+          CXXFLAGS="-stdlib=libc++ -Xiwyu --comment_style=long -Xiwyu --mapping='${{ github.workspace }}/Stockfish/.github/workflows/libcxx17.imp' -Xiwyu --error"
index e7c06389bdd5d57917c0fac5a34c7951a50ddfb4..1b03bbc2b0ac1c26237830dc2a510743bb5babb7 100644 (file)
@@ -837,12 +837,15 @@ ifneq ($(SUPPORTED_ARCH), true)
 endif
 
 
-.PHONY: help build profile-build strip install clean net objclean profileclean \
-       config-sanity \
+.PHONY: help analyze build profile-build strip install clean net \
+       objclean profileclean config-sanity \
        icx-profile-use icx-profile-make \
        gcc-profile-use gcc-profile-make \
        clang-profile-use clang-profile-make FORCE
 
+analyze: net config-sanity objclean
+       $(MAKE) -k ARCH=$(ARCH) COMP=$(COMP) $(OBJS)
+
 build: net config-sanity
        $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
 
index 8ac24daea178040201a62683a4c2b4628237b84d..fd1b0de1f6ea10074d55535c056ec5a0b039fd35 100644 (file)
@@ -26,7 +26,6 @@
 namespace Stockfish {
 
 class Position;
-enum Value : int;
 
 namespace Eval {
 
index 6449de25794b2ef187ad3e57e2a95a1c01ee56d9..b15f1230b130b7280217ede217c5023460588e08 100644 (file)
@@ -19,7 +19,7 @@
 #ifndef MOVEGEN_H_INCLUDED
 #define MOVEGEN_H_INCLUDED
 
-#include <algorithm>
+#include <algorithm> // IWYU pragma: keep
 #include <cstddef>
 
 #include "types.h"