blob: 050482dac4aff031a25be4d7521a2a14c2d7cd77 [file] [log] [blame]
Jason Rhinelanderac427892016-08-28 13:00:44 -04001#!/bin/bash
2#
3# Script to check include/test code for common pybind11 code style errors.
Wenzel Jakobfe342412016-09-06 13:02:29 +09004#
5# This script currently checks for
6#
7# 1. use of tabs instead of spaces
Wenzel Jakobde2c6df2016-12-13 00:17:29 +01008# 2. MSDOS-style CRLF endings
9# 3. trailing spaces
10# 4. missing space between keyword and parenthesis, e.g.: for(, if(, while(
Wenzel Jakob2b92a492016-11-08 10:58:22 +010011# 5. Missing space between right parenthesis and brace, e.g. 'for (...){'
Wenzel Jakobde2c6df2016-12-13 00:17:29 +010012# 6. opening brace on its own line. It should always be on the same line as the
13# if/while/for/do statment.
Jason Rhinelanderac427892016-08-28 13:00:44 -040014#
15# Invoke as: tools/check-style.sh
16#
17
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040018errors=0
19IFS=$'\n'
20found=
Jason Rhinelanderd472f0f2016-08-29 20:59:48 -040021# The mt=41 sets a red background for matched tabs:
Jason Rhinelanderd080f832017-06-14 15:19:42 -040022GREP_COLORS='mt=41' GREP_COLOR='41' grep $'\t' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always |
23 while read f; do
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040024 if [ -z "$found" ]; then
Jason Rhinelanderd080f832017-06-14 15:19:42 -040025 echo -e '\033[31m\033[01mError: found tabs instead of spaces in the following files:\033[0m'
Jason Rhinelanderac427892016-08-28 13:00:44 -040026 found=1
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040027 errors=1
Jason Rhinelanderac427892016-08-28 13:00:44 -040028 fi
29
30 echo " $f"
31done
32
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040033found=
Jason Rhinelanderd080f832017-06-14 15:19:42 -040034grep -IUlr $'\r' include tests/*.{cpp,py,h} docs/*.rst --color=always |
35 while read f; do
Wenzel Jakobde2c6df2016-12-13 00:17:29 +010036 if [ -z "$found" ]; then
Jason Rhinelanderd080f832017-06-14 15:19:42 -040037 echo -e '\033[31m\033[01mError: found CRLF characters in the following files:\033[0m'
Wenzel Jakobde2c6df2016-12-13 00:17:29 +010038 found=1
39 errors=1
40 fi
41
42 echo " $f"
43done
44
45found=
Wenzel Jakobfe342412016-09-06 13:02:29 +090046# The mt=41 sets a red background for matched trailing spaces
Jason Rhinelanderd080f832017-06-14 15:19:42 -040047GREP_COLORS='mt=41' GREP_COLOR='41' grep '[[:blank:]]\+$' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always |
48 while read f; do
Wenzel Jakobfe342412016-09-06 13:02:29 +090049 if [ -z "$found" ]; then
Jason Rhinelanderd080f832017-06-14 15:19:42 -040050 echo -e '\033[31m\033[01mError: found trailing spaces in the following files:\033[0m'
Wenzel Jakobfe342412016-09-06 13:02:29 +090051 found=1
52 errors=1
53 fi
54
55 echo " $f"
56done
57
58found=
Jason Rhinelanderd080f832017-06-14 15:19:42 -040059grep '\<\(if\|for\|while\|catch\)(\|){' include tests/*.{cpp,py,h} -rn --color=always |
60 while read line; do
Wenzel Jakobde2c6df2016-12-13 00:17:29 +010061 if [ -z "$found" ]; then
Jason Rhinelanderd080f832017-06-14 15:19:42 -040062 echo -e '\033[31m\033[01mError: found the following coding style problems:\033[0m'
Wenzel Jakobde2c6df2016-12-13 00:17:29 +010063 found=1
64 errors=1
65 fi
66
67 echo " $line"
68done
69
70found=
Jason Rhinelanderd080f832017-06-14 15:19:42 -040071GREP_COLORS='mt=41' GREP_COLOR='41' grep '^\s*{\s*$' include docs/*.rst -rn --color=always |
72 while read f; do
Wenzel Jakobcc4efe62016-11-08 10:53:30 +010073 if [ -z "$found" ]; then
Jason Rhinelanderd080f832017-06-14 15:19:42 -040074 echo -e '\033[31m\033[01mError: braces should occur on the same line as the if/while/.. statement. Found issues in the following files: \033[0m'
Wenzel Jakobcc4efe62016-11-08 10:53:30 +010075 found=1
76 errors=1
77 fi
78
79 echo " $f"
80done
81
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040082exit $errors