From 0ba0d57d54ac5224437bf1a5e6620e351e835780 Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fischer@muhq.space>
Date: Wed, 4 Aug 2021 18:41:14 +0200
Subject: [PATCH] [meson] exclude subprojects from static analysis

Generate a new compile_commands database in compile_commands_wo_subprojects/
without the files in subprojects/.
This new compile_commands database is than consumed by iwyu and clang-tidy.

Co-Authored-By: Florian Schmaus <schmaus@cs.fau.de>
Suggested--By: Florian Schmaus <schmaus@cs.fau.de>
---
 .clang-tidy                                |  2 +-
 Makefile                                   | 11 ++++--
 compile_commands_wo_subprojects/.gitignore |  1 +
 tools/check-iwyu                           |  2 +-
 tools/gen-compile-commands-wo-subprojects  | 26 ++++++++++++++
 tools/run-clang-tidy                       | 42 ++++++++++++++++++++++
 6 files changed, 79 insertions(+), 5 deletions(-)
 create mode 100644 compile_commands_wo_subprojects/.gitignore
 create mode 100755 tools/gen-compile-commands-wo-subprojects
 create mode 100755 tools/run-clang-tidy

diff --git a/.clang-tidy b/.clang-tidy
index b914e66b..25c56ca8 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -21,4 +21,4 @@ WarningsAsErrors: >
   readability-*,
   performance-*,
 
-HeaderFilterRegex: .*
+HeaderFilterRegex: '(?!subprojects).*'
diff --git a/Makefile b/Makefile
index 5081a62e..f3e0357d 100644
--- a/Makefile
+++ b/Makefile
@@ -88,13 +88,18 @@ format: all
 	$(NINJA) -C build clang-format
 
 .PHONY: tidy
-tidy: all
-	$(NINJA) -C build clang-tidy
+tidy: compile_commands_wo_subprojects/compile_commands.json
+	./tools/run-clang-tidy
 
 PHONY: iwyu
-iwyu: all
+iwyu: compile_commands_wo_subprojects/compile_commands.json
 	$(NINJA) -C build $@
 
+build/compile_commands.json: all
+
+compile_commands_wo_subprojects/compile_commands.json: all build/compile_commands.json
+	./tools/gen-compile-commands-wo-subprojects
+
 PHONY: fix-includes
 fix-includes: all
 	./tools/fix-includes
diff --git a/compile_commands_wo_subprojects/.gitignore b/compile_commands_wo_subprojects/.gitignore
new file mode 100644
index 00000000..cfd3a8cd
--- /dev/null
+++ b/compile_commands_wo_subprojects/.gitignore
@@ -0,0 +1 @@
+/compile_commands.json
diff --git a/tools/check-iwyu b/tools/check-iwyu
index 67b431ea..e303e91f 100755
--- a/tools/check-iwyu
+++ b/tools/check-iwyu
@@ -53,7 +53,7 @@ NPROC=$(nproc)
 LOAD=$(python -c "print(${NPROC} * 1.5)")
 
 IWYU_TOOL_ARGS=(
-	-p "${MESON_BUILD_ROOT}"
+	-p "${MESON_SOURCE_ROOT}/compile_commands_wo_subprojects"
 	--jobs "${NPROC}"
 )
 
diff --git a/tools/gen-compile-commands-wo-subprojects b/tools/gen-compile-commands-wo-subprojects
new file mode 100755
index 00000000..924ae107
--- /dev/null
+++ b/tools/gen-compile-commands-wo-subprojects
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+from pathlib import Path
+
+import json
+
+COMPILE_COMMANDS_FILENAME = "compile_commands.json"
+
+input_compile_db_path = Path(COMPILE_COMMANDS_FILENAME)
+
+with input_compile_db_path.open() as f:
+    input_compile_db = json.load(f)
+
+output_compile_db = []
+
+for entry in input_compile_db:
+    entry_file = entry["file"]
+    if entry_file.startswith("../subprojects/"):
+        continue
+
+    output_compile_db.append(entry)
+
+output_compile_db_path = Path("compile_commands_wo_subprojects") / Path(COMPILE_COMMANDS_FILENAME)
+
+with output_compile_db_path.open(mode='w') as f:
+    json.dump(output_compile_db, f, indent=4)
diff --git a/tools/run-clang-tidy b/tools/run-clang-tidy
new file mode 100755
index 00000000..8a60ee29
--- /dev/null
+++ b/tools/run-clang-tidy
@@ -0,0 +1,42 @@
+#!/usr/bin/env bash
+
+SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
+ROOTDIR="$(realpath "${SCRIPTDIR}/..")"
+
+while getopts dv OPT; do
+	case $OPT in
+		d)
+			set -x
+			;;
+
+		*)
+			echo "usage: ${0##*/} [-d]"
+			exit 2
+	esac
+done
+shift $(( OPTIND - 1 ))
+OPTIND=1
+
+RUN_CLANG_TIDY_CANDIDATES=(
+	run-clang-tidy
+	run-clang-tidy.py
+	/usr/share/clang/run-clang-tidy.py
+)
+
+RUN_CLANG_TIDY=""
+
+for candidate in ${RUN_CLANG_TIDY_CANDIDATES[@]}; do
+	if ! command -v "${candidate}"; then
+		continue;
+	fi
+
+	RUN_CLANG_TIDY="${candidate}"
+	break;
+done
+
+if [[ -z "${RUN_CLANG_TIDY}" ]]; then
+	echo "No run-clang-tidy executable found"
+	exit 1
+fi
+
+${RUN_CLANG_TIDY} -p "${ROOTDIR}/compile_commands_wo_subprojects/"
-- 
GitLab