check_header.sh 3.82 KB
Newer Older
1 2 3 4 5 6 7 8
#
#  @file check_header.sh
#
#  @copyright 2016-2017 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
#                       Univ. Bordeaux. All rights reserved.
#
#  @version 6.0.0
#  @author Mathieu Faverge
PRUVOST Florent's avatar
PRUVOST Florent committed
9
#  @date 2018-10-12
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
#
# This script check that basic informations is present and correct in
# headers of source files.
#
#!/bin/sh
header=1

print_header()
{
    if [ $header -ne 0 ]
    then
        echo "------ $1 --------"
        header=0
    fi
}

check_header_file()
{
    filename=$1
    basename=`basename $filename .in`

    if [ "$basename" != "CMakeLists.txt" ]
    then
        toto=`grep " @file $basename" $filename`
        if [ $? -ne 0 ]
        then
            toto=`grep " @file .*/$basename" $filename`
        fi
Mathieu Faverge's avatar
Mathieu Faverge committed
38 39 40 41
        if [ $? -ne 0 ]
        then
            toto=`grep " @file chameleon_$basename" $filename`
        fi
42 43 44 45 46 47 48 49 50 51 52 53 54 55

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

check_header_copyright()
{
    filename=$1
    basename=`basename $filename`

PRUVOST Florent's avatar
PRUVOST Florent committed
56
    toto=`grep -E " @copyright [0-9]{4}-2019 Bordeaux INP" $filename`
57 58
    if [ $? -ne 0 ]
    then
PRUVOST Florent's avatar
PRUVOST Florent committed
59
        toto=`grep -E " @copyright 2019      Bordeaux INP" $filename`
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 187
    fi

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

check_header_version()
{
    filename=$1
    basename=`basename $filename`

    toto=`grep -E " @version [0-9]\.[0-9]\.[0-9]" $filename`
    if [ $? -ne 0 ]
    then
        print_header $filename
        echo -n "@version line missing or incorrect:"; grep "@version" $filename; echo "";
    fi
}

check_header_author()
{
    filename=$1
    basename=`basename $filename`

    toto=`grep -E " @author " $filename`
    if [ $? -ne 0 ]
    then
        print_header $filename
        echo "@author line missing";
    fi
}

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 "";
    fi
}

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

    case $basename in
        *.h)
            n=`basename $basename .h | awk '{print tolower($0)}'`

            macro="_${n}_h_"
            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
            fi
            ;;
        *)
    esac

}

#
# Check that the given source file contains
#
# @file filename
# @copyright
# @version
# @date
#
check_header()
{
    header=1
    check_header_file $1
    check_header_copyright $1
    check_header_version $1
#    check_header_author $1
#    check_header_date $1
    check_header_define $1
}

#
# Check headers
#
files=`git ls-files | grep -v "^\." | grep -v ".*\.md" | grep -v LICENSE | grep -v ".*\.cmake" | grep -v "testing/lin" | grep -v doc/ | grep -v CTest | grep -v cblas.h | grep -v lapacke.h | grep -v "simucore/perfmodels/\.starpu" | grep -v "\.org"`

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

for f in $files
do
    #echo $f
    if [ -d $f ]
    then
        continue;
    fi

    check_header $f
done