Skip to content

Commit 5e9d0e6

Browse files
committed
update(chore): add clang-tidy
Signed-off-by: sakumisu <1203593632@qq.com>
1 parent 22b07a1 commit 5e9d0e6

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

.clang-tidy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
Checks: >
3+
-*,
4+
clang-analyzer-core.*,
5+
clang-analyzer-deadcode.*,
6+
clang-analyzer-nullability.*,
7+
clang-analyzer-security.*,
8+
-clang-analyzer-security.insecureAPI.*,
9+
clang-analyzer-unix.*,
10+
bugprone-*,
11+
-bugprone-easily-swappable-parameters,
12+
-bugprone-macro-parentheses,
13+
-bugprone-reserved-identifier
14+
15+
WarningsAsErrors: ''
16+
HeaderFilterRegex: '.*(core|class|common|osal|port|platform)/.*'
17+
FormatStyle: none
18+
19+
CheckOptions:
20+
bugprone-argument-comment.StrictMode: false
21+
bugprone-sizeof-expression.WarnOnSizeOfConstant: true
22+
bugprone-sizeof-expression.WarnOnSizeOfIntegerExpression: false
23+
bugprone-suspicious-memset-usage.WarnOnImplicitCast: true
24+
...

.github/workflows/clang-tidy.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: Clang-Tidy action
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
inputs:
8+
scan_regex:
9+
description: "Regex used to select files from compile_commands.json"
10+
required: false
11+
default: "/(core|class|common|osal|port|platform)/.*[.]c$"
12+
13+
jobs:
14+
clang-tidy:
15+
name: clang-tidy
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
21+
- name: Install dependencies
22+
run: |
23+
sudo apt-get update
24+
sudo apt-get install -y clang-tidy cmake ninja-build jq ripgrep wget
25+
26+
- name: Download hpm_sdk
27+
run: |
28+
cd ~
29+
git clone --depth 1 https://github.com/hpmicro/hpm_sdk.git
30+
31+
- name: Download RISC-V toolchain
32+
run: |
33+
cd ~
34+
wget -q https://github.com/hpmicro/riscv-gnu-toolchain/releases/download/2023.10.18/rv32imac_zicsr_zifencei_multilib_b_ext-linux.tar.gz
35+
tar -xzf rv32imac_zicsr_zifencei_multilib_b_ext-linux.tar.gz
36+
37+
- name: Generate HPM compile database
38+
run: |
39+
cd tests/hpmicro
40+
export HPM_SDK_BASE=~/hpm_sdk
41+
export GNURISCV_TOOLCHAIN_PATH=~/rv32imac_zicsr_zifencei_multilib_b_ext-linux
42+
export HPM_SDK_TOOLCHAIN_VARIANT=
43+
cmake -S . -B build -GNinja \
44+
-DBOARD=hpm6800evk \
45+
-DHPM_BUILD_TYPE=flash_sdram_xip \
46+
-DCMAKE_BUILD_TYPE=debug \
47+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
48+
49+
- name: Run clang-tidy
50+
shell: bash
51+
env:
52+
SCAN_REGEX: ${{ github.event.inputs.scan_regex || '/(core|class|common|osal|port|platform)/.*[.]c$' }}
53+
run: |
54+
TOOLCHAIN="$HOME/rv32imac_zicsr_zifencei_multilib_b_ext-linux"
55+
GCC="$TOOLCHAIN/bin/riscv32-unknown-elf-gcc"
56+
GCC_INCLUDE="$("$GCC" -print-file-name=include)"
57+
GCC_INCLUDE_FIXED="$("$GCC" -print-file-name=include-fixed)"
58+
59+
set +e
60+
jq -r '.[].file' tests/hpmicro/build/compile_commands.json \
61+
| rg "^${GITHUB_WORKSPACE}${SCAN_REGEX}" \
62+
| xargs -r -n1 clang-tidy \
63+
-p tests/hpmicro/build \
64+
--quiet \
65+
--extra-arg-before=--target=riscv32-unknown-elf \
66+
--extra-arg=-isystem"$GCC_INCLUDE" \
67+
--extra-arg=-isystem"$GCC_INCLUDE_FIXED" \
68+
> clang-tidy.log 2>&1
69+
tidy_status=$?
70+
set -e
71+
72+
grep -E "warning:|error:" clang-tidy.log | tee clang-tidy-summary.log || true
73+
74+
while IFS= read -r line; do
75+
if [[ "$line" =~ ^([^:]+):([0-9]+):([0-9]+):[[:space:]]warning:[[:space:]](.*)$ ]]; then
76+
echo "::warning file=${BASH_REMATCH[1]},line=${BASH_REMATCH[2]},col=${BASH_REMATCH[3]}::${BASH_REMATCH[4]}"
77+
elif [[ "$line" =~ ^([^:]+):([0-9]+):([0-9]+):[[:space:]]error:[[:space:]](.*)$ ]]; then
78+
echo "::error file=${BASH_REMATCH[1]},line=${BASH_REMATCH[2]},col=${BASH_REMATCH[3]}::${BASH_REMATCH[4]}"
79+
fi
80+
done < clang-tidy-summary.log
81+
82+
if [ "$tidy_status" -ne 0 ]; then
83+
exit "$tidy_status"
84+
fi
85+
86+
- name: Upload clang-tidy logs
87+
if: always()
88+
uses: actions/upload-artifact@v4
89+
with:
90+
name: clang-tidy-logs
91+
path: |
92+
clang-tidy.log
93+
clang-tidy-summary.log

.github/workflows/cppcheck.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@ jobs:
1313
- name: cppcheck
1414
shell: bash
1515
run: |
16+
set -o pipefail
1617
sudo apt install cppcheck
17-
cppcheck --enable=warning,portability,performance --language=c --platform=unix32 --std=c99 --force . -i third_party/ -i class/template -i port/template/
18+
cppcheck --enable=warning,portability,performance --language=c --platform=unix32 --std=c99 --quiet --force . -i third_party/ -i class/template -i port/template/ -i tests/ --include=tests/hpmicro/inc/usb_config.h 2>&1 | tee cppcheck.log
19+
- name: Upload cppcheck log
20+
if: always()
21+
uses: actions/upload-artifact@v4
22+
with:
23+
name: cppcheck-log
24+
path: cppcheck.log

0 commit comments

Comments
 (0)