Blame external/pybind11/tools/check-style.sh

Packit 534379
#!/bin/bash
Packit 534379
#
Packit 534379
# Script to check include/test code for common pybind11 code style errors.
Packit 534379
#
Packit 534379
# This script currently checks for
Packit 534379
#
Packit 534379
# 1. use of tabs instead of spaces
Packit 534379
# 2. MSDOS-style CRLF endings
Packit 534379
# 3. trailing spaces
Packit 534379
# 4. missing space between keyword and parenthesis, e.g.: for(, if(, while(
Packit 534379
# 5. Missing space between right parenthesis and brace, e.g. 'for (...){'
Packit 534379
# 6. opening brace on its own line. It should always be on the same line as the
Packit 534379
#    if/while/for/do statement.
Packit 534379
#
Packit 534379
# Invoke as: tools/check-style.sh
Packit 534379
#
Packit 534379
Packit 534379
check_style_errors=0
Packit 534379
IFS=$'\n'
Packit 534379
Packit 534379
found="$( GREP_COLORS='mt=41' GREP_COLOR='41' grep $'\t' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
Packit 534379
if [ -n "$found" ]; then
Packit 534379
    # The mt=41 sets a red background for matched tabs:
Packit 534379
    echo -e '\033[31;01mError: found tab characters in the following files:\033[0m'
Packit 534379
    check_style_errors=1
Packit 534379
    echo "$found" | sed -e 's/^/    /'
Packit 534379
fi
Packit 534379
Packit 534379
Packit 534379
found="$( grep -IUlr $'\r' include tests/*.{cpp,py,h} docs/*.rst --color=always )"
Packit 534379
if [ -n "$found" ]; then
Packit 534379
    echo -e '\033[31;01mError: found CRLF characters in the following files:\033[0m'
Packit 534379
    check_style_errors=1
Packit 534379
    echo "$found" | sed -e 's/^/    /'
Packit 534379
fi
Packit 534379
Packit 534379
found="$(GREP_COLORS='mt=41' GREP_COLOR='41' grep '[[:blank:]]\+$' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
Packit 534379
if [ -n "$found" ]; then
Packit 534379
    # The mt=41 sets a red background for matched trailing spaces
Packit 534379
    echo -e '\033[31;01mError: found trailing spaces in the following files:\033[0m'
Packit 534379
    check_style_errors=1
Packit 534379
    echo "$found" | sed -e 's/^/    /'
Packit 534379
fi
Packit 534379
Packit 534379
found="$(grep '\<\(if\|for\|while\|catch\)(\|){' include tests/*.{cpp,h} -rn --color=always)"
Packit 534379
if [ -n "$found" ]; then
Packit 534379
    echo -e '\033[31;01mError: found the following coding style problems:\033[0m'
Packit 534379
    check_style_errors=1
Packit 534379
    echo "$found" | sed -e 's/^/    /'
Packit 534379
fi
Packit 534379
Packit 534379
found="$(awk '
Packit 534379
function prefix(filename, lineno) {
Packit 534379
    return "    \033[35m" filename "\033[36m:\033[32m" lineno "\033[36m:\033[0m"
Packit 534379
}
Packit 534379
function mark(pattern, string) { sub(pattern, "\033[01;31m&\033[0m", string); return string }
Packit 534379
last && /^\s*{/ {
Packit 534379
    print prefix(FILENAME, FNR-1) mark("\\)\\s*$", last)
Packit 534379
    print prefix(FILENAME, FNR)   mark("^\\s*{", $0)
Packit 534379
    last=""
Packit 534379
}
Packit 534379
{ last = /(if|for|while|catch|switch)\s*\(.*\)\s*$/ ? $0 : "" }
Packit 534379
' $(find include -type f) tests/*.{cpp,h} docs/*.rst)"
Packit 534379
if [ -n "$found" ]; then
Packit 534379
    check_style_errors=1
Packit 534379
    echo -e '\033[31;01mError: braces should occur on the same line as the if/while/.. statement. Found issues in the following files:\033[0m'
Packit 534379
    echo "$found"
Packit 534379
fi
Packit 534379
Packit 534379
exit $check_style_errors