Skip to content

Commit 46e5a1f

Browse files
committed
Extract the CPPCheck and get_new_file separately
1 parent d931fbe commit 46e5a1f

File tree

4 files changed

+193
-13
lines changed

4 files changed

+193
-13
lines changed

Diff for: .github/workflows/static_code_analysis.yml

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: Static code analysis
22

3-
on:
4-
pull_request:
5-
branches:
6-
- master
3+
on: [push]
4+
# pull_request:
5+
# branches:
6+
# - master
77

88
jobs:
99
scancode_job:
@@ -21,16 +21,10 @@ jobs:
2121
run: |
2222
sudo apt-get update
2323
sudo apt-get -qq install cppcheck
24+
pip install click PyYaml
2425
git remote -v
2526
git fetch origin
2627
cppcheck --version
2728
ls
2829
git branch -a
29-
changed_files=$(git diff --name-only HEAD origin/master | grep -E '\.(c|cpp|cc|cxx)$' || true)
30-
if [ -n "$changed_files" ];then
31-
cppcheck --enable=warning,performance,portability --inline-suppr --error-exitcode=1 --force $changed_files
32-
err=$?
33-
if [ $err -ne 0 ]; then
34-
echo "CPPCHECK REPORT, PLEASE CHECK THE WARNING !!!!!!!!!"
35-
fi
36-
fi
30+
python tools/ci/cpp_check.py check 'https://github.com/RT-Thread/rt-thread' 'master'

Diff for: bsp/wch/risc-v/Libraries/ch56x_drivers/ch56x_gpio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static rt_base_t gpio_pin_get(const char *name)
186186
}
187187
if (pin < 32 && (pin_ports[port].pin_mark & (1 << pin)))
188188
{
189-
return port * 32 + pin;
189+
return port * 32 + pin;
190190
}
191191
}
192192
}

Diff for: tools/ci/cpp_check.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#
2+
# Copyright (c) 2006-2023, RT-Thread Development Team
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Change Logs:
7+
# Date Author Notes
8+
# 2021-04-01 LiuKang the first version
9+
#
10+
11+
import click
12+
import logging
13+
import subprocess
14+
import sys
15+
import format_ignore
16+
17+
class CPPCheck:
18+
def __init__(self, file_list):
19+
self.file_list = file_list
20+
21+
def check(self):
22+
file_list_filtered = [file for file in self.file_list if file.endswith(('.c', '.cpp', '.cc', '.cxx'))]
23+
logging.info("Start to static code analysis.")
24+
check_result = True
25+
for file in file_list_filtered:
26+
result = subprocess.run(['cppcheck', '--enable=warning', 'performance', 'portability', '--inline-suppr', '--error-exitcode=1', '--force', file], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
27+
logging.info(result.stdout.decode())
28+
logging.info(result.stderr.decode())
29+
if result.stderr:
30+
check_result = False
31+
return check_result
32+
33+
@click.group()
34+
@click.pass_context
35+
def cli(ctx):
36+
pass
37+
38+
@cli.command()
39+
@click.argument(
40+
'repo',
41+
nargs=1,
42+
type=click.STRING,
43+
default='https://github.com/RT-Thread/rt-thread',
44+
)
45+
@click.argument(
46+
'branch',
47+
nargs=1,
48+
type=click.STRING,
49+
default='master',
50+
)
51+
def check(repo, branch):
52+
"""
53+
static code analysis(cppcheck).
54+
"""
55+
format_ignore.init_logger()
56+
# get modified files list
57+
checkout = format_ignore.CheckOut(repo, branch)
58+
file_list = checkout.get_new_file()
59+
if file_list is None:
60+
logging.error("checkout files fail")
61+
sys.exit(1)
62+
63+
# use cppcheck
64+
cpp_check = CPPCheck(file_list)
65+
cpp_check_result = cpp_check.check()
66+
67+
if not cpp_check_result:
68+
logging.error("static code analysis(cppcheck) fail.")
69+
sys.exit(1)
70+
logging.info("check success.")
71+
sys.exit(0)
72+
73+
74+
if __name__ == '__main__':
75+
cli()

Diff for: tools/ci/format_ignore.py

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import yaml
2+
import logging
3+
import os
4+
import subprocess
5+
6+
def init_logger():
7+
log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s "
8+
date_format = '%Y-%m-%d %H:%M:%S %a '
9+
logging.basicConfig(level=logging.INFO,
10+
format=log_format,
11+
datefmt=date_format,
12+
)
13+
14+
class CheckOut:
15+
def __init__(self, rtt_repo, rtt_branch):
16+
self.root = os.getcwd()
17+
self.rtt_repo = rtt_repo
18+
self.rtt_branch = rtt_branch
19+
20+
def __exclude_file(self, file_path):
21+
dir_number = file_path.split('/')
22+
ignore_path = file_path
23+
24+
# gets the file path depth.
25+
for i in dir_number:
26+
# current directory.
27+
dir_name = os.path.dirname(ignore_path)
28+
ignore_path = dir_name
29+
# judge the ignore file exists in the current directory.
30+
ignore_file_path = os.path.join(dir_name, ".ignore_format.yml")
31+
if not os.path.exists(ignore_file_path):
32+
continue
33+
try:
34+
with open(ignore_file_path) as f:
35+
ignore_config = yaml.safe_load(f.read())
36+
file_ignore = ignore_config.get("file_path", [])
37+
dir_ignore = ignore_config.get("dir_path", [])
38+
except Exception as e:
39+
logging.error(e)
40+
continue
41+
logging.debug("ignore file path: {}".format(ignore_file_path))
42+
logging.debug("file_ignore: {}".format(file_ignore))
43+
logging.debug("dir_ignore: {}".format(dir_ignore))
44+
try:
45+
# judge file_path in the ignore file.
46+
for file in file_ignore:
47+
if file is not None:
48+
file_real_path = os.path.join(dir_name, file)
49+
if file_real_path == file_path:
50+
logging.info("ignore file path: {}".format(file_real_path))
51+
return 0
52+
53+
file_dir_path = os.path.dirname(file_path)
54+
for _dir in dir_ignore:
55+
if _dir is not None:
56+
dir_real_path = os.path.join(dir_name, _dir)
57+
if file_dir_path.startswith(dir_real_path):
58+
logging.info("ignore dir path: {}".format(dir_real_path))
59+
return 0
60+
except Exception as e:
61+
logging.error(e)
62+
continue
63+
64+
return 1
65+
66+
# def get_new_file(self):
67+
# file_list = list()
68+
# try:
69+
# os.system('git remote add rtt_repo {}'.format(self.rtt_repo))
70+
# os.system('git fetch rtt_repo')
71+
# os.system('git merge rtt_repo/{}'.format(self.rtt_branch))
72+
# os.system('git reset rtt_repo/{} --soft'.format(self.rtt_branch))
73+
# os.system('git status > git.txt')
74+
# except Exception as e:
75+
# logging.error(e)
76+
# return None
77+
# try:
78+
# with open('git.txt', 'r') as f:
79+
# file_lines = f.readlines()
80+
# except Exception as e:
81+
# logging.error(e)
82+
# return None
83+
# file_path = ''
84+
# for line in file_lines:
85+
# if 'new file' in line:
86+
# file_path = line.split('new file:')[1].strip()
87+
# logging.info('new file -> {}'.format(file_path))
88+
# elif 'deleted' in line:
89+
# logging.info('deleted file -> {}'.format(line.split('deleted:')[1].strip()))
90+
# elif 'modified' in line:
91+
# file_path = line.split('modified:')[1].strip()
92+
# logging.info('modified file -> {}'.format(file_path))
93+
# else:
94+
# continue
95+
96+
# result = self.__exclude_file(file_path)
97+
# if result != 0:
98+
# file_list.append(file_path)
99+
100+
# return file_list
101+
102+
def get_new_file(self):
103+
result = subprocess.run(['git', 'diff', '--name-only', 'HEAD', 'origin/master', '--diff-filter=ACMR', '--no-renames', '--full-index'], stdout = subprocess.PIPE)
104+
file_list = result.stdout.decode().strip().split('\n')
105+
for line in file_list:
106+
logging.info("modified file -> {}".format(line))
107+
result = self.__exclude_file(line)
108+
if result != 0:
109+
file_list.append(line)
110+
111+
return file_list

0 commit comments

Comments
 (0)