Jason Rhinelander | ac42789 | 2016-08-28 13:00:44 -0400 | [diff] [blame] | 1 | #!/bin/bash |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 2 | # |
Jason Rhinelander | ac42789 | 2016-08-28 13:00:44 -0400 | [diff] [blame] | 3 | # Script to check include/test code for common pybind11 code style errors. |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 4 | # |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 5 | # This script currently checks for |
| 6 | # |
| 7 | # 1. use of tabs instead of spaces |
Wenzel Jakob | de2c6df | 2016-12-13 00:17:29 +0100 | [diff] [blame] | 8 | # 2. MSDOS-style CRLF endings |
| 9 | # 3. trailing spaces |
| 10 | # 4. missing space between keyword and parenthesis, e.g.: for(, if(, while( |
Wenzel Jakob | 2b92a49 | 2016-11-08 10:58:22 +0100 | [diff] [blame] | 11 | # 5. Missing space between right parenthesis and brace, e.g. 'for (...){' |
Wenzel Jakob | de2c6df | 2016-12-13 00:17:29 +0100 | [diff] [blame] | 12 | # 6. opening brace on its own line. It should always be on the same line as the |
| 13 | # if/while/for/do statment. |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 14 | # |
Jason Rhinelander | ac42789 | 2016-08-28 13:00:44 -0400 | [diff] [blame] | 15 | # Invoke as: tools/check-style.sh |
| 16 | # |
| 17 | |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 18 | check_style_errors=0 |
Jason Rhinelander | dbc4bf6 | 2016-08-28 14:53:04 -0400 | [diff] [blame] | 19 | IFS=$'\n' |
Jason Rhinelander | ac42789 | 2016-08-28 13:00:44 -0400 | [diff] [blame] | 20 | |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 21 | found="$( GREP_COLORS='mt=41' GREP_COLOR='41' grep $'\t' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )" |
| 22 | if [ -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/^/ /' |
| 27 | fi |
Jason Rhinelander | ac42789 | 2016-08-28 13:00:44 -0400 | [diff] [blame] | 28 | |
Wenzel Jakob | de2c6df | 2016-12-13 00:17:29 +0100 | [diff] [blame] | 29 | |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 30 | found="$( grep -IUlr $'\r' include tests/*.{cpp,py,h} docs/*.rst --color=always )" |
| 31 | if [ -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/^/ /' |
| 35 | fi |
Wenzel Jakob | de2c6df | 2016-12-13 00:17:29 +0100 | [diff] [blame] | 36 | |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 37 | found="$(GREP_COLORS='mt=41' GREP_COLOR='41' grep '[[:blank:]]\+$' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )" |
| 38 | if [ -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/^/ /' |
| 43 | fi |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 44 | |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 45 | found="$(grep '\<\(if\|for\|while\|catch\)(\|){' include tests/*.{cpp,h} -rn --color=always)" |
| 46 | if [ -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/^/ /' |
| 50 | fi |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 51 | |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 52 | found="$(awk ' |
| 53 | function prefix(filename, lineno) { |
| 54 | return " \033[35m" filename "\033[36m:\033[32m" lineno "\033[36m:\033[0m" |
| 55 | } |
| 56 | function mark(pattern, string) { sub(pattern, "\033[01;31m&\033[0m", string); return string } |
| 57 | last && /^\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)" |
| 64 | if [ -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" |
| 68 | fi |
Wenzel Jakob | de2c6df | 2016-12-13 00:17:29 +0100 | [diff] [blame] | 69 | |
Henry Schreiner | 4312620 | 2017-09-10 06:24:33 -0400 | [diff] [blame^] | 70 | exit $check_style_errors |