Jason Rhinelander | ac42789 | 2016-08-28 13:00:44 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Script to check include/test code for common pybind11 code style errors. |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 4 | # |
| 5 | # This script currently checks for |
| 6 | # |
| 7 | # 1. use of tabs instead of spaces |
| 8 | # 2. trailing spaces |
| 9 | # 3. missing space between keyword and parenthesis, e.g.: for(, if(, while( |
Jason Rhinelander | ac42789 | 2016-08-28 13:00:44 -0400 | [diff] [blame] | 10 | # |
| 11 | # Invoke as: tools/check-style.sh |
| 12 | # |
| 13 | |
Jason Rhinelander | dbc4bf6 | 2016-08-28 14:53:04 -0400 | [diff] [blame] | 14 | errors=0 |
| 15 | IFS=$'\n' |
| 16 | found= |
Jason Rhinelander | d472f0f | 2016-08-29 20:59:48 -0400 | [diff] [blame] | 17 | # The mt=41 sets a red background for matched tabs: |
Wenzel Jakob | 85f07e1 | 2016-09-04 23:00:49 +0900 | [diff] [blame] | 18 | exec 3< <(GREP_COLORS='mt=41' grep $'\t' include/ tests/*.{cpp,py,h} docs/*.rst -rn --color=always) |
Jason Rhinelander | 5a3570c | 2016-08-29 19:04:12 -0400 | [diff] [blame] | 19 | while read -u 3 f; do |
Jason Rhinelander | dbc4bf6 | 2016-08-28 14:53:04 -0400 | [diff] [blame] | 20 | if [ -z "$found" ]; then |
Jason Rhinelander | ac42789 | 2016-08-28 13:00:44 -0400 | [diff] [blame] | 21 | echo -e '\e[31m\e[01mError: found tabs instead of spaces in the following files:\e[0m' |
| 22 | found=1 |
Jason Rhinelander | dbc4bf6 | 2016-08-28 14:53:04 -0400 | [diff] [blame] | 23 | errors=1 |
Jason Rhinelander | ac42789 | 2016-08-28 13:00:44 -0400 | [diff] [blame] | 24 | fi |
| 25 | |
| 26 | echo " $f" |
| 27 | done |
| 28 | |
Jason Rhinelander | dbc4bf6 | 2016-08-28 14:53:04 -0400 | [diff] [blame] | 29 | found= |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 30 | # The mt=41 sets a red background for matched trailing spaces |
| 31 | exec 3< <(GREP_COLORS='mt=41' grep '\s\+$' include/ tests/*.{cpp,py,h} docs/*.rst -rn --color=always) |
| 32 | while read -u 3 f; do |
| 33 | if [ -z "$found" ]; then |
| 34 | echo -e '\e[31m\e[01mError: found trailing spaces in the following files:\e[0m' |
| 35 | found=1 |
| 36 | errors=1 |
| 37 | fi |
| 38 | |
| 39 | echo " $f" |
| 40 | done |
| 41 | |
| 42 | found= |
Wenzel Jakob | 85f07e1 | 2016-09-04 23:00:49 +0900 | [diff] [blame] | 43 | exec 3< <(grep '\<\(if\|for\|while\)(\|){' include/ tests/*.{cpp,py,h} -rn --color=always) |
Jason Rhinelander | 5a3570c | 2016-08-29 19:04:12 -0400 | [diff] [blame] | 44 | while read -u 3 line; do |
Jason Rhinelander | dbc4bf6 | 2016-08-28 14:53:04 -0400 | [diff] [blame] | 45 | if [ -z "$found" ]; then |
| 46 | echo -e '\e[31m\e[01mError: found the following coding style problems:\e[0m' |
| 47 | found=1 |
| 48 | errors=1 |
| 49 | fi |
| 50 | |
| 51 | echo " $line" |
| 52 | done |
| 53 | |
| 54 | exit $errors |