check-style.sh (11986:c12e4625ab56) check-style.sh (12391:ceeca8b41e4b)
1#!/bin/bash
1#!/bin/bash
2#
2#
3# Script to check include/test code for common pybind11 code style errors.
3# Script to check include/test code for common pybind11 code style errors.
4#
4#
5# This script currently checks for
6#
7# 1. use of tabs instead of spaces
8# 2. MSDOS-style CRLF endings
9# 3. trailing spaces
10# 4. missing space between keyword and parenthesis, e.g.: for(, if(, while(
11# 5. Missing space between right parenthesis and brace, e.g. 'for (...){'
12# 6. opening brace on its own line. It should always be on the same line as the
13# if/while/for/do statment.
5# This script currently checks for
6#
7# 1. use of tabs instead of spaces
8# 2. MSDOS-style CRLF endings
9# 3. trailing spaces
10# 4. missing space between keyword and parenthesis, e.g.: for(, if(, while(
11# 5. Missing space between right parenthesis and brace, e.g. 'for (...){'
12# 6. opening brace on its own line. It should always be on the same line as the
13# if/while/for/do statment.
14#
14#
15# Invoke as: tools/check-style.sh
16#
17
15# Invoke as: tools/check-style.sh
16#
17
18errors=0
18check_style_errors=0
19IFS=$'\n'
19IFS=$'\n'
20found=
21# The mt=41 sets a red background for matched tabs:
22exec 3< <(GREP_COLORS='mt=41' grep $'\t' include/ tests/*.{cpp,py,h} docs/*.rst -rn --color=always)
23while read -u 3 f; do
24 if [ -z "$found" ]; then
25 echo -e '\e[31m\e[01mError: found tabs instead of spaces in the following files:\e[0m'
26 found=1
27 errors=1
28 fi
29
20
30 echo " $f"
31done
21found="$( GREP_COLORS='mt=41' GREP_COLOR='41' grep $'\t' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
22if [ -n "$found" ]; then
23 # The mt=41 sets a red background for matched tabs:
24 echo -e '\033[31;01mError: found tab characters in the following files:\033[0m'
25 check_style_errors=1
26 echo "$found" | sed -e 's/^/ /'
27fi
32
28
33found=
34# The mt=41 sets a red background for matched MS-DOS CRLF line endings
35exec 3< <(GREP_COLORS='mt=41' grep -IUlr $'\r' include/ tests/*.{cpp,py,h} docs/*.rst --color=always)
36while read -u 3 f; do
37 if [ -z "$found" ]; then
38 echo -e '\e[31m\e[01mError: found CRLF characters in the following files:\e[0m'
39 found=1
40 errors=1
41 fi
42
29
43 echo " $f"
44done
30found="$( grep -IUlr $'\r' include tests/*.{cpp,py,h} docs/*.rst --color=always )"
31if [ -n "$found" ]; then
32 echo -e '\033[31;01mError: found CRLF characters in the following files:\033[0m'
33 check_style_errors=1
34 echo "$found" | sed -e 's/^/ /'
35fi
45
36
46found=
47# The mt=41 sets a red background for matched trailing spaces
48exec 3< <(GREP_COLORS='mt=41' grep '\s\+$' include/ tests/*.{cpp,py,h} docs/*.rst -rn --color=always)
49while read -u 3 f; do
50 if [ -z "$found" ]; then
51 echo -e '\e[31m\e[01mError: found trailing spaces in the following files:\e[0m'
52 found=1
53 errors=1
54 fi
37found="$(GREP_COLORS='mt=41' GREP_COLOR='41' grep '[[:blank:]]\+$' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
38if [ -n "$found" ]; then
39 # The mt=41 sets a red background for matched trailing spaces
40 echo -e '\033[31;01mError: found trailing spaces in the following files:\033[0m'
41 check_style_errors=1
42 echo "$found" | sed -e 's/^/ /'
43fi
55
44
56 echo " $f"
57done
45found="$(grep '\<\(if\|for\|while\|catch\)(\|){' include tests/*.{cpp,h} -rn --color=always)"
46if [ -n "$found" ]; then
47 echo -e '\033[31;01mError: found the following coding style problems:\033[0m'
48 check_style_errors=1
49 echo "$found" | sed -e 's/^/ /'
50fi
58
51
59found=
60exec 3< <(grep '\<\(if\|for\|while\|catch\)(\|){' include/ tests/*.{cpp,py,h} -rn --color=always)
61while read -u 3 line; do
62 if [ -z "$found" ]; then
63 echo -e '\e[31m\e[01mError: found the following coding style problems:\e[0m'
64 found=1
65 errors=1
66 fi
52found="$(awk '
53function prefix(filename, lineno) {
54 return " \033[35m" filename "\033[36m:\033[32m" lineno "\033[36m:\033[0m"
55}
56function mark(pattern, string) { sub(pattern, "\033[01;31m&\033[0m", string); return string }
57last && /^\s*{/ {
58 print prefix(FILENAME, FNR-1) mark("\\)\\s*$", last)
59 print prefix(FILENAME, FNR) mark("^\\s*{", $0)
60 last=""
61}
62{ last = /(if|for|while|catch|switch)\s*\(.*\)\s*$/ ? $0 : "" }
63' $(find include -type f) tests/*.{cpp,h} docs/*.rst)"
64if [ -n "$found" ]; then
65 check_style_errors=1
66 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'
67 echo "$found"
68fi
67
69
68 echo " $line"
69done
70
71found=
72exec 3< <(GREP_COLORS='mt=41' grep '^\s*{\s*$' include/ docs/*.rst -rn --color=always)
73while read -u 3 f; do
74 if [ -z "$found" ]; then
75 echo -e '\e[31m\e[01mError: braces should occur on the same line as the if/while/.. statement. Found issues in the following files: \e[0m'
76 found=1
77 errors=1
78 fi
79
80 echo " $f"
81done
82
83exit $errors
70exit $check_style_errors