[PYTHON] C code review tool using pycparser

background purpose

I usually develop C language software Before requesting a code review, I wanted a tool to self-check that my code was written according to coding rules.

It's hard to make from the code parsing process, so I searched for a good library. It seemed that a library called pycparser could be used, so I made a code review tool, "pycreviewer", using this. https://github.com/dromar-soft/Pycreviewer

Execution environment

About pycparser

pycparser is a C language parsing library developed by eliben. https://github.com/eliben/pycparser

design

function

List of corresponding coding rules

Static variable name prefix

Make sure that the static variable name has a specific prefix (eg'm_').

#inclide <stdio.h>
static int m_var;

Global variable name prefix

Make sure that the global variable name has a specific prefix (for example,'g_').

#inclide <stdio.h>
int g_var;

Too short variable name

Detects variable names that are too short.

int i;

Recursive call

Detects recursive calls to functions.

Function blacklist

Detects a disabled function call. (Functions prohibited from use are set in the JSON file described later)

No break statement in the switch-case statement

Detects where the break () statement is not included in the switch-case statement.

    switch(flag){
        case 0:
            break;
        case 1:
            //No Break
        default:
            break;
    }

No default statement in switch statement

Detects where the default statement is not defined in the switch statement.

        switch(f){
            case 0:
                break;
            case 1:
                break;
            //No Default
        }

Setting detailed conditions by JSON file

{
    "version": "0.1.0",
    "conditions":{
        "static_variable_prefix":{
            "id":"R001",
            "param":"m_",
            "level":"SHOULD"
        },
        "global_variable_prefix":{
            "id":"R002",
            "param":"g_",
            "level":"SHOULD"
        },
        "variable_short_name":{
            "id":"R003",
            "param":2,
            "level":"MUST"
        },
        "recursive_call":{
            "id":"R004",
            "param":true,
            "level":"MUST"
        },
        "function_blacklist":{
            "id":"R005",
            "param":[
                "malloc",
                "free"
            ],
            "level":"WANT"
        },
        "no_break_in_switch":{
            "id":"R006",
            "param":true,
            "level":"SHOULD"
        },
        "no_default_in_switch":{
            "id":"R007",
            "param":true,
            "level":"SHOULD"
        }
    }
}

Installation

git clone https://github.com/dromar-soft/Pycreviewer.git

How to use

Used as a console application

python -m pycreviewer
input source folder >> 'your sourcecode directory'
{'id': 'R006', 'level': 'SHOULD', 'msg': 'No break statement in switch-case statement.', 'file': '/xxx/xxx/xxx.c', 'line': X, 'column': X}
{'id': 'R007', 'level': 'SHOULD', 'msg': 'No default statement in switch-case statement.', 'file': '/xxx/xxx/xxx.c', 'line': X, 'column': Y}
...
...
...
X files codereview completed. Please enter esc key.

Use as a library

You can perform a code review on a single source file by calling pycreviewer.review_file ().

def review_file(sourcefile: str, cpp_args=['-E', r'-Ipycreviewer/utils/fake_libc_include'], jsonfile='./default.json') ->list:
    """
    Perform code review on a single source file.
    The result of the code review is returned in the form of List<CheckResult>.
    sourcefile:
        the target source file path.
    cppargs:
        a list of command line arguments for the preprocessor execution of C compiler.
        Normally, specifies the preprocessor execution option '-E' and the include option '-Ixxxxx'.
    jsonfile:
        JSON file path describing the checking conditions for coding rules.
    """

Notes

Finally

Recommended Posts

C code review tool using pycparser
Execute Python code on C ++ (using Boost.Python)
Try using Sourcetrail, a source code visualization tool
Type-aware using c ++ templates
Using X11 with ubuntu18.04 (C)
Tool to check code style
Format C source with pycparser