~aleteoryx/muditaos

muditaos/config/clang_check.sh -rwxr-xr-x 2.3 KiB
a405cad6Aleteoryx trim readme 5 days ago
                                                                                
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
#!/bin/env bash
# Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

set -euo pipefail

# shellcheck source=../config/format-config.sh
# shellcheck source=../config/clang/colors.sh
# shellcheck source=../config/clang/common.sh

L_GIT_DIR=$(git rev-parse --show-toplevel)
source $L_GIT_DIR/config/format-config.sh
source $L_GIT_DIR/config/clang/clang-common.sh
source $L_GIT_DIR/config/clang/colors.sh

help()
{
    cat <<- EOF
clang tidy diff tool used to perform check on our sources uses 2 defaults:
    .clang-tidy                 : configuration file, from the place where command is run
    compile_commands.json   : compile data, as well from the place where command is run
EOF
}

get_files_to_check()
{
    local files
    local endlist=()
    local file_with_ignores="$1"
    files=$(git diff -U0 --name-only remotes/origin/${CHANGE_TARGET}...HEAD --diff-filter='d')
    for file in ${files}; do
        if [[ ${file} =~ ^.*\.(cpp|hpp|c|h|cxx|gcc|cc)$ ]] && shouldnt_ignore "${file}" > "$file_with_ignores"; then
            endlist+=("$file")
        fi
    done
    echo "${endlist[*]}"
}

# if not exists create
# could be in better place than tmp
get_compile_commands()
{
    product=$1
    [[ -f build-$product-linux-Debug/compile_commands.json ]] || ./configure.sh $product linux debug -DCMAKE_EXPORT_COMPILE_COMMANDS=1 > /dev/null 2>&1
    cp build-$product-linux-Debug/compile_commands.json /tmp/compile_commands.json
    sed -i 's|-static-libasan||g'     /tmp/compile_commands.json
    sed -i 's|-Wno-literal-suffix||g' /tmp/compile_commands.json
}

main()
{
    if [[ $# -ne 1 ]]; then
        help
        exit 0
    fi
    local tool
    tool=$(get_clang_tidy_diff)
    echo "Target branch: ${CHANGE_TARGET}"

    local files_to_check
    local ignore_files
    ignore_files=$(mktemp /tmp/clang_check.XXXXXX)

    files_to_check=$(get_files_to_check "$ignore_files")
    cat "$ignore_files" && rm "$ignore_files"

    if [[ -z $files_to_check ]]; then
        echo "no files to check"
        exit 0
    fi

    # get the stage
    verify_clang_format_version
    get_compile_commands "$1"
    # run tidy
    git diff -U0 --no-color remotes/origin/${CHANGE_TARGET}...HEAD $files_to_check | ${tool[*]} -p 1 -path=/tmp/
}

main "$1"