File: check_header.sh

package info (click to toggle)
vite 1.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,112 kB
  • sloc: cpp: 30,167; makefile: 467; sh: 233; python: 140; ansic: 67
file content (186 lines) | stat: -rwxr-xr-x 4,115 bytes parent folder | download | duplicates (3)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#
#  @file check_header.sh
#
#  @copyright 2008-2024 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
#                       Univ. Bordeaux. All rights reserved.
#
#  @author Camille Ordronneau
#  
#  @date 2024-07-16
#
# This script check that basic informations is present and correct in
# headers of source files.
#
#!/usr/bin/env sh
header=1
nberr=0

# Print the file name if its the first error in the file
print_header()
{
    if [ $header -ne 0 ]
    then
        echo "------ $1 --------"
        header=0
    fi
}

# Check if there is the name of the file in the header
check_header_file()
{
    filepath=$1
    toto=$( grep " @file $filepath" $filepath )

    if [ $? -ne 0 ]
    then
        print_header $filepath
        echo -n "@file line missing or incorrect:"; grep "@file" $filepath; echo ""
        nberr=$(( nberr + 1 ))
    fi

}

# Check if there is a copyright line in the header, with correct year
check_header_copyright()
{
    filepath=$1

    # Get the year of last modification in the file
    # Get date in YYYY-MM-DD format -> take the first 4 characters
    year=$( git log -1 --pretty="format:%cd" --date=format:%Y $filepath)
    toto=$( grep -E " @copyright 2008-$year Bordeaux INP" $filepath )

    if [ $? -ne 0 ]
    then
        print_header $filepath
        echo -n "@copyright line missing or incorrect:"; grep "@copyright" $filepath; echo "";
    fi
}

# Check if there is at least on author line in the header
check_header_author()
{
    filename=$1
    basename=$( basename $filename )

    toto=$( grep -E " @author " $filename )

    if [ $? -ne 0 ]
    then
        print_header $filename
        echo "@author line missing";
        nberr=$(( nberr + 1 ))
    fi
}

# Check if there is a date line in the header
check_header_date()
{
    filename=$1
    basename=$( basename $filename )

    toto=$( grep -E " @date [0-9]{4}-[01][0-9]-[0-3][0-9]" $filename )
    if [ $? -ne 0 ]
    then
        print_header $filename
        echo -n "@date line missing or incorrect"; grep "@date" $filename; echo "";
        nberr=$(( nberr + 1 ))
    fi
}

check_header_define()
{
    filename=$1
    basename=$( basename $filename )

    case $basename in
        *.hpp)
            n=$( basename $basename .hpp | awk '{print toupper($0)}' )

            macro="${n}_HPP"
            err=0

            toto=$( grep "#ifndef .*$macro" $filename )
            ret=$?
            err=$((err + ret))

            if [ $ret -eq 0 ]
            then
                macro=$( grep "#ifndef" $filename | sed 's/#ifndef //' )
            fi
            toto=$( grep "#define $macro" $filename )
            ret=$?
            err=$((err + ret))

            toto=$( grep "#endif /\* $macro \*/" $filename )
            ret=$?
            err=$((err + ret))

            if [ $err -ne 0 ]
            then
                print_header $filename
                grep "#ifndef" $filename
                grep "#define" $filename
                grep "#endif"  $filename
                nberr=$(( nberr + 1 ))
            fi
            ;;
        *)
    esac

}

#
# Launch a check for each one of these lines
#
# @file filename
# @copyright
# @author
# @date
#
check_header()
{
    header=1
    check_header_file $1
    check_header_copyright $1
    check_header_author $1
    check_header_date $1
    
    # For now there is still a lot of files to change so they all have the same define format
    # So this verification is not done
    # This could be used later
    # check_header_define $1
}

#
# Get the files to check
#
files=$(git ls-files '*.cpp' '*.hpp' '*.c' '*.h'    |
        grep -v "externals/*"                       |
        grep -v "src/trace/portable_*"              |
        grep -v "src/core/getopt.*"                 )

if [ $# -gt 0 ]
then
    files=$*
fi

# For eac file the check is applied
for f in $files
do
    if [ -d $f ]
    then
        continue;
    fi

    check_header $f
done

# If there is any error, the number of errors is printed
if [ $nberr -gt 0 ]
then
    echo "${nberr} mistakes have been found in the header files."
    exit 1
else
    exit 0
fi