blob: 0a9f7d24fcbf5ef4a86dab0bccceb9b331bdb4e7 [file] [log] [blame]
Jason Rhinelanderac427892016-08-28 13:00:44 -04001#!/bin/bash
Henry Schreiner43126202017-09-10 06:24:33 -04002#
Jason Rhinelanderac427892016-08-28 13:00:44 -04003# Script to check include/test code for common pybind11 code style errors.
Henry Schreiner43126202017-09-10 06:24:33 -04004#
Wenzel Jakobfe342412016-09-06 13:02:29 +09005# 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
Unknown0b3f44e2017-11-01 21:08:06 -040013# if/while/for/do statement.
Henry Schreiner43126202017-09-10 06:24:33 -040014#
Jason Rhinelanderac427892016-08-28 13:00:44 -040015# Invoke as: tools/check-style.sh
16#
17
Henry Schreiner43126202017-09-10 06:24:33 -040018check_style_errors=0
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040019IFS=$'\n'
Jason Rhinelanderac427892016-08-28 13:00:44 -040020
Henry Schreiner43126202017-09-10 06:24:33 -040021found="$( 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
Jason Rhinelanderac427892016-08-28 13:00:44 -040028
Wenzel Jakobde2c6df2016-12-13 00:17:29 +010029
Henry Schreiner43126202017-09-10 06:24:33 -040030found="$( 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
Wenzel Jakobde2c6df2016-12-13 00:17:29 +010036
Henry Schreiner43126202017-09-10 06:24:33 -040037found="$(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
Wenzel Jakobfe342412016-09-06 13:02:29 +090044
Henry Schreiner43126202017-09-10 06:24:33 -040045found="$(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
Wenzel Jakobfe342412016-09-06 13:02:29 +090051
Henry Schreiner43126202017-09-10 06:24:33 -040052found="$(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
Wenzel Jakobde2c6df2016-12-13 00:17:29 +010069
Henry Schreiner43126202017-09-10 06:24:33 -040070exit $check_style_errors