From 4c783757e24aac39234cf3967e7048156a5d679c Mon Sep 17 00:00:00 2001
From: swym <0xfd000000@gmail.com>
Date: Thu, 15 Aug 2019 14:03:20 +0000
Subject: [PATCH] build: Add clang-tidy support

Co-authored-by: Rahix <rahix@rahix.de>
---
 .clang-tidy               | 34 ++++++++++++++++++++++++++++++++++
 .gitignore                |  1 +
 meson.build               | 16 ++++++++++++++++
 tools/compile_commands.jq | 11 +++++++++++
 tools/convert-ccjson.sh   | 18 ++++++++++++++++++
 5 files changed, 80 insertions(+)
 create mode 100644 .clang-tidy
 create mode 100644 tools/compile_commands.jq
 create mode 100755 tools/convert-ccjson.sh

diff --git a/.clang-tidy b/.clang-tidy
new file mode 100644
index 00000000..5c744faa
--- /dev/null
+++ b/.clang-tidy
@@ -0,0 +1,34 @@
+---
+Checks:          'clang-diagnostic-*,clang-analyzer-*,bugprone-*'
+WarningsAsErrors: ''
+HeaderFilterRegex: ''
+AnalyzeTemporaryDtors: false
+FormatStyle:     none
+User:            swym
+CheckOptions:    
+  - key:             cert-dcl16-c.NewSuffixes
+    value:           'L;LL;LU;LLU'
+  - key:             cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
+    value:           '1'
+  - key:             google-readability-braces-around-statements.ShortStatementLines
+    value:           '1'
+  - key:             google-readability-function-size.StatementThreshold
+    value:           '800'
+  - key:             google-readability-namespace-comments.ShortNamespaceLines
+    value:           '10'
+  - key:             google-readability-namespace-comments.SpacesBeforeComments
+    value:           '2'
+  - key:             modernize-loop-convert.MaxCopySize
+    value:           '16'
+  - key:             modernize-loop-convert.MinConfidence
+    value:           reasonable
+  - key:             modernize-loop-convert.NamingStyle
+    value:           CamelCase
+  - key:             modernize-pass-by-value.IncludeStyle
+    value:           llvm
+  - key:             modernize-replace-auto-ptr.IncludeStyle
+    value:           llvm
+  - key:             modernize-use-nullptr.NullMacros
+    value:           'NULL'
+...
+
diff --git a/.gitignore b/.gitignore
index 5fa9e7b8..7ca9401e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ __pycache__/
 *.pyc
 .*.swp
 *~
+compile_commands.json
diff --git a/meson.build b/meson.build
index 96a11fe4..3dd0578c 100644
--- a/meson.build
+++ b/meson.build
@@ -46,6 +46,22 @@ version_hdr = custom_target(
   command: [files('tools/version-header.sh'), '@OUTPUT@'],
 )
 
+jq = find_program('jq', required: false)
+if jq.found()
+  compile_commands = custom_target(
+    'compile_commands_tidy.json',
+    build_by_default: true,
+    output: 'compile_commands_tidy.json',
+    command: [
+      files('tools/convert-ccjson.sh'),
+      meson.current_source_dir(),
+      meson.current_build_dir(),
+      '@OUTPUT@',
+    ],
+  )
+endif
+
+
 subdir('lib/')
 subdir('bootloader/')
 
diff --git a/tools/compile_commands.jq b/tools/compile_commands.jq
new file mode 100644
index 00000000..ac0e3138
--- /dev/null
+++ b/tools/compile_commands.jq
@@ -0,0 +1,11 @@
+[.[]|({
+    directory:.directory,
+    command:(. as {file:$file, directory:$directory}
+        |.command
+        |sub("-fno-isolate-erroneous-paths-dereference";"-m32")
+        |sub("(?<a>\\s)\\.\\./"; (.a) + "\($directory)/../")
+        |sub("/build/\\.\\./"; "/")
+        )
+    ,
+    file:((.directory|sub("build$";""))+(.file|sub("^../";"")))
+})]
diff --git a/tools/convert-ccjson.sh b/tools/convert-ccjson.sh
new file mode 100755
index 00000000..8e444e27
--- /dev/null
+++ b/tools/convert-ccjson.sh
@@ -0,0 +1,18 @@
+#!/usr/bin/env bash
+set -e
+
+if [[ "$#" == 0 ]]; then
+    echo "usage: $0 <sourcedir> <compile_commands_file>"
+    exit 1
+fi
+
+script_dir="$(dirname "$0")"
+source_dir="$1"
+build_dir="$2"
+output="$3"
+
+jq -f "$script_dir/compile_commands.jq" "$build_dir/compile_commands.json" >"$source_dir/compile_commands.json"
+
+if [[ "$output" != "" ]]; then
+    touch "$output"
+fi
-- 
GitLab