summaryrefslogtreecommitdiff
path: root/tools/ci/cpp_check.py
blob: 5e7e10052571b133c04d434a2a7c9b8df84eef3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#
# Copyright (c) 2006-2023, RT-Thread Development Team
#
# SPDX-License-Identifier: Apache-2.0
#
# Change Logs:
# Date           Author       Notes
# 2023-05-16     dejavudwh    the first version
#

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',
                    '-DRT_ASSERT(x)=',
                    '-DRTM_EXPORT(x)=',
                    '-Drt_list_for_each_entry(a,b,c)=a=(void*)b;',
                    '-I include',
                    '-I thread/components/finsh',
                    # it's okay because CI will do the real compilation to check this
                    '--suppress=syntaxError',
                    '--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()
def check():
    """
    static code analysis(cppcheck).
    """
    format_ignore.init_logger()
    # get modified files list
    checkout = format_ignore.CheckOut()
    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()