Skip to content

[CI] Added feature to filter out files to file_check.py that do not require cppcheck #7499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/file_check.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
name: Check File Format and License
name: Check File Format, License and Static Code Analysis(cppcheck)

on: [pull_request]

jobs:
scancode_job:
runs-on: ubuntu-latest
name: Scan code format and license
name: Scan code format, license and static code analysis
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: 3.8

- name: Check Format and License
- name: Check Format, License and Static Code Analysis(cppcheck)
shell: bash
run: |
sudo apt-get update
sudo apt-get -qq install cppcheck
cppcheck --version
pip install click chardet PyYaml
python tools/ci/file_check.py check 'https://github.com/RT-Thread/rt-thread' 'master'
18 changes: 6 additions & 12 deletions .github/workflows/static_code_analysis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Static code analysis

on:
pull_request:
branches:
- master
on: [push]
# pull_request:
# branches:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉

# - master

jobs:
scancode_job:
Expand All @@ -21,16 +21,10 @@ jobs:
run: |
sudo apt-get update
sudo apt-get -qq install cppcheck
pip install click PyYaml
git remote -v
git fetch origin
cppcheck --version
ls
git branch -a
changed_files=$(git diff --name-only HEAD origin/master | grep -E '\.(c|cpp|cc|cxx)$' || true)
if [ -n "$changed_files" ];then
cppcheck --enable=warning,performance,portability --inline-suppr --error-exitcode=1 --force $changed_files
err=$?
if [ $err -ne 0 ]; then
echo "CPPCHECK REPORT, PLEASE CHECK THE WARNING !!!!!!!!!"
fi
fi
python tools/ci/cpp_check.py check 'https://github.com/RT-Thread/rt-thread' 'master'
1 change: 1 addition & 0 deletions bsp/wch/risc-v/.ignore_format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

dir_path:
- Libraries/CH32V10x_StdPeriph_Driver
- Libraries/ch56x_drivers
2 changes: 1 addition & 1 deletion bsp/wch/risc-v/Libraries/ch56x_drivers/ch56x_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static rt_base_t gpio_pin_get(const char *name)
}
if (pin < 32 && (pin_ports[port].pin_mark & (1 << pin)))
{
return port * 32 + pin;
return port * 32 + pin;
}
}
}
Expand Down
75 changes: 75 additions & 0 deletions tools/ci/cpp_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#
# Copyright (c) 2006-2023, RT-Thread Development Team
#
# SPDX-License-Identifier: Apache-2.0
#
# Change Logs:
# Date Author Notes
# 2021-04-01 LiuKang the first version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释改下。

#

import click
import logging
import subprocess
import sys
import format_ignore

class CPPCheck:
def __init__(self, file_list):
self.file_list = file_list

def check(self):
file_list_filtered = [file for file in self.file_list if file.endswith(('.c', '.cpp', '.cc', '.cxx'))]
logging.info("Start to static code analysis.")
check_result = True
for file in file_list_filtered:
result = subprocess.run(['cppcheck', '--enable=warning', 'performance', 'portability', '--inline-suppr', '--error-exitcode=1', '--force', file], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
logging.info(result.stdout.decode())
logging.info(result.stderr.decode())
if result.stderr:
check_result = False
return check_result

@click.group()
@click.pass_context
def cli(ctx):
pass

@cli.command()
@click.argument(
'repo',
nargs=1,
type=click.STRING,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉

default='https://github.com/RT-Thread/rt-thread',
)
@click.argument(
'branch',
nargs=1,
type=click.STRING,
default='master',
)
def check(repo, branch):
"""
static code analysis(cppcheck).
"""
format_ignore.init_logger()
# get modified files list
checkout = format_ignore.CheckOut(repo, branch)
file_list = checkout.get_new_file()
if file_list is None:
logging.error("checkout files fail")
sys.exit(1)

# use cppcheck
cpp_check = CPPCheck(file_list)
cpp_check_result = cpp_check.check()

if not cpp_check_result:
logging.error("static code analysis(cppcheck) fail.")
sys.exit(1)
logging.info("check success.")
sys.exit(0)


if __name__ == '__main__':
cli()
27 changes: 22 additions & 5 deletions tools/ci/file_check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2006-2022, RT-Thread Development Team
# Copyright (c) 2006-2023, RT-Thread Development Team
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件不用改

#
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -16,7 +16,7 @@
import chardet
import logging
import datetime

import subprocess

def init_logger():
log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s "
Expand Down Expand Up @@ -221,6 +221,21 @@ def check(self):

return check_result

class CPPCheck:
def __init__(self, file_list):
self.file_list = file_list

def check(self):
file_list_filtered = [file for file in self.file_list if file.endswith(('.c', '.cpp', '.cc', '.cxx'))]
logging.info("Start to static code analysis.")
check_result = True
for file in file_list_filtered:
result = subprocess.run(['cppcheck', '--enable=warning', 'performance', 'portability', '--inline-suppr', '--error-exitcode=1', '--force', file], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
logging.info(result.stdout.decode())
logging.info(result.stderr.decode())
if result.stderr:
check_result = False
return check_result

@click.group()
@click.pass_context
Expand Down Expand Up @@ -251,7 +266,7 @@ def cli(ctx):
)
def check(check_license, repo, branch):
"""
check files license and format.
check files license, format and static code analysis(cppcheck).
"""
init_logger()
# get modified files list
Expand All @@ -264,13 +279,15 @@ def check(check_license, repo, branch):
# check modified files format
format_check = FormatCheck(file_list)
format_check_result = format_check.check()
cpp_check = CPPCheck(file_list)
cpp_check_result = cpp_check.check()
license_check_result = True
if check_license:
license_check = LicenseCheck(file_list)
license_check_result = license_check.check()

if not format_check_result or not license_check_result:
logging.error("file format check or license check fail.")
if not format_check_result or not cpp_check_result or not license_check_result:
logging.error("file format check or license check or static code analysis(cppcheck) fail.")
sys.exit(1)
logging.info("check success.")
sys.exit(0)
Expand Down
76 changes: 76 additions & 0 deletions tools/ci/format_ignore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import yaml
import logging
import os
import subprocess

def init_logger():
log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s "
date_format = '%Y-%m-%d %H:%M:%S %a '
logging.basicConfig(level=logging.INFO,
format=log_format,
datefmt=date_format,
)

class CheckOut:
def __init__(self, rtt_repo, rtt_branch):
self.root = os.getcwd()
self.rtt_repo = rtt_repo
self.rtt_branch = rtt_branch

def __exclude_file(self, file_path):
dir_number = file_path.split('/')
ignore_path = file_path

# gets the file path depth.
for i in dir_number:
# current directory.
dir_name = os.path.dirname(ignore_path)
ignore_path = dir_name
# judge the ignore file exists in the current directory.
ignore_file_path = os.path.join(dir_name, ".ignore_format.yml")
if not os.path.exists(ignore_file_path):
continue
try:
with open(ignore_file_path) as f:
ignore_config = yaml.safe_load(f.read())
file_ignore = ignore_config.get("file_path", [])
dir_ignore = ignore_config.get("dir_path", [])
except Exception as e:
logging.error(e)
continue
logging.debug("ignore file path: {}".format(ignore_file_path))
logging.debug("file_ignore: {}".format(file_ignore))
logging.debug("dir_ignore: {}".format(dir_ignore))
try:
# judge file_path in the ignore file.
for file in file_ignore:
if file is not None:
file_real_path = os.path.join(dir_name, file)
if file_real_path == file_path:
logging.info("ignore file path: {}".format(file_real_path))
return 0

file_dir_path = os.path.dirname(file_path)
for _dir in dir_ignore:
if _dir is not None:
dir_real_path = os.path.join(dir_name, _dir)
if file_dir_path.startswith(dir_real_path):
logging.info("ignore dir path: {}".format(dir_real_path))
return 0
except Exception as e:
logging.error(e)
continue

return 1

def get_new_file(self):
result = subprocess.run(['git', 'diff', '--name-only', 'HEAD', 'origin/master', '--diff-filter=ACMR', '--no-renames', '--full-index'], stdout = subprocess.PIPE)
file_list = result.stdout.decode().strip().split('\n')
new_files = []
for line in file_list:
logging.info("modified file -> {}".format(line))
result = self.__exclude_file(line)
if result != 0:
new_files.append(line)

return new_files