From 1493e99008dba385b4e173f7753c2a7e8f10adc2 Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fischer@muhq.space>
Date: Fri, 30 Jul 2021 20:56:49 +0200
Subject: [PATCH] add more tooling and CI jobs

* clang-tidy
* clang-format
---
 .clang-format         |   5 +
 .clang-tidy           |  24 +++
 .gitlab-ci.yml        |   8 +
 Makefile              |   9 ++
 compile_commands.json | 359 ++++++++++++++++++++++++++++++++++++++++++
 tools/check-format    |  51 ++++++
 6 files changed, 456 insertions(+)
 create mode 100644 .clang-format
 create mode 100644 .clang-tidy
 create mode 100644 compile_commands.json
 create mode 100755 tools/check-format

diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..5f8df3c
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,5 @@
+---
+BasedOnStyle:  Google
+ColumnLimit:     100
+TabWidth: 2
+UseTab: Always
diff --git a/.clang-tidy b/.clang-tidy
new file mode 100644
index 0000000..b914e66
--- /dev/null
+++ b/.clang-tidy
@@ -0,0 +1,24 @@
+Checks: >
+  bugprone-*,
+  cert-*,
+  linuxkernel-*,
+  modernize-*,
+  performance-*,
+  portability-*,
+  readability-*,
+  -cert-err58-cpp,
+  -clang-diagnostic-empty-translation-unit,
+  -readability-braces-around-statements,
+  -readability-function-cognitive-complexity,
+  -readability-implicit-bool-conversion,
+  -readability-isolate-declaration,
+  -readability-magic-numbers,
+
+WarningsAsErrors: >
+  bugprone-*,
+  modernize-*,
+  clang-*,
+  readability-*,
+  performance-*,
+
+HeaderFilterRegex: .*
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 550e2f0..ccdc51f 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -14,3 +14,11 @@ before_script:
 test-eval:
   script:
     - make
+
+tidy:
+  script:
+    - make tidy
+
+check-format:
+  script:
+    - make check-format
diff --git a/Makefile b/Makefile
index 139ebf0..addbf94 100644
--- a/Makefile
+++ b/Makefile
@@ -27,3 +27,12 @@ $(foreach syscall,$(SYSCALLS),$(eval $(call generateTargets,$(syscall))))
 
 clean:
 	rm -f $(OBJ)
+
+tidy:
+	clang-tidy *.c
+
+check-format:
+	tools/check-format
+
+format:
+	clang-format -i *.c *.h
diff --git a/compile_commands.json b/compile_commands.json
new file mode 100644
index 0000000..ca4b944
--- /dev/null
+++ b/compile_commands.json
@@ -0,0 +1,359 @@
+[
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-blocking",
+      "blocking.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/blocking.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-blocking"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-blocking",
+      "bench.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/bench.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-blocking"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-blocking",
+      "stopwatch.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/stopwatch.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-blocking"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-io-uring",
+      "io-uring.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/io-uring.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-io-uring"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-io-uring",
+      "bench.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/bench.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-io-uring"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-io-uring",
+      "stopwatch.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/stopwatch.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-io-uring"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-io-uring-sqpoll",
+      "io-uring-sqpoll.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/io-uring-sqpoll.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-io-uring-sqpoll"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-io-uring-sqpoll",
+      "bench.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/bench.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-io-uring-sqpoll"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-io-uring-sqpoll",
+      "stopwatch.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/stopwatch.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-io-uring-sqpoll"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-io-uring-no-syscall",
+      "io-uring-no-syscall.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/io-uring-no-syscall.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-io-uring-no-syscall"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-io-uring-no-syscall",
+      "bench.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/bench.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-io-uring-no-syscall"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-io-uring-no-syscall",
+      "stopwatch.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/stopwatch.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-io-uring-no-syscall"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-epoll",
+      "epoll.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/epoll.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-epoll"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-epoll",
+      "bench.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/bench.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-epoll"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-epoll",
+      "stopwatch.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/stopwatch.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-epoll"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-aio-sig",
+      "aio-sig.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/aio-sig.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-aio-sig"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-aio-sig",
+      "bench.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/bench.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-aio-sig"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-aio-sig",
+      "stopwatch.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/stopwatch.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-aio-sig"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-aio-thrd",
+      "aio-thrd.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/aio-thrd.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-aio-thrd"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-aio-thrd",
+      "bench.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/bench.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-aio-thrd"
+  },
+  {
+    "arguments": [
+      "/sbin/cc",
+      "-c",
+      "-Werror",
+      "-Wall",
+      "-g",
+      "-O0",
+      "-pthread",
+      "-o",
+      "bench-aio-thrd",
+      "stopwatch.c"
+    ],
+    "directory": "/home/muhq/code/fgbs-syscall-eval",
+    "file": "/home/muhq/code/fgbs-syscall-eval/stopwatch.c",
+    "output": "/home/muhq/code/fgbs-syscall-eval/bench-aio-thrd"
+  }
+]
diff --git a/tools/check-format b/tools/check-format
new file mode 100755
index 0000000..005c77c
--- /dev/null
+++ b/tools/check-format
@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+# Pretty fancy method to get reliable the absolute path of a shell
+# script, *even if it is sourced*. Credits go to GreenFox on
+# stackoverflow: http://stackoverflow.com/a/12197518/194894
+pushd . > /dev/null
+SCRIPTDIR="${BASH_SOURCE[0]}";
+while([ -h "${SCRIPTDIR}" ]); do
+    cd "`dirname "${SCRIPTDIR}"`"
+    SCRIPTDIR="$(readlink "`basename "${SCRIPTDIR}"`")";
+done
+cd "`dirname "${SCRIPTDIR}"`" > /dev/null
+SCRIPTDIR="`pwd`";
+popd  > /dev/null
+
+DEBUG=false
+while getopts d OPT; do
+	case $OPT in
+		d)
+			set -x
+			DEBUG=true
+			;;
+		*)
+			echo "usage: ${0##*/} [-dq} [--] ARGS..."
+			exit 2
+	esac
+done
+shift $(( OPTIND - 1 ))
+OPTIND=1
+
+ROOTDIR=$(readlink -f "${SCRIPTDIR}/..")
+
+MAX_PROCS=$(nproc)
+
+CHECKED_FILES_FILE=$(mktemp)
+if ! $DEBUG; then
+	trap 'rm "${CHECKED_FILES_FILE}"' EXIT
+fi
+
+cd "${ROOTDIR}"
+# Note that the --dry-run and --Werror clang-format arguments require
+# clang-format 10 or higher. See https://reviews.llvm.org/D68554
+find . \( -path '*/\.*' -o -path "./subprojects*" -o -path "./build*" \) -prune -o \
+	-type f -regextype posix-extended -regex '.*\.(c|h|cpp|hpp)' -print0 |\
+	tee "${CHECKED_FILES_FILE}" |\
+	xargs --null --max-args=3 --max-procs="${MAX_PROCS}" \
+		  clang-format --style=file --dry-run -Werror
+
+FILE_COUNT=$(<"${CHECKED_FILES_FILE}" tr -cd '\0' | wc -c)
+echo "Checked ${FILE_COUNT} files for format violations"
-- 
GitLab