blob: bc52dc8631b4f16c242d86f988765a2c5afa53b3 [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
8# 2. trailing spaces
9# 3. missing space between keyword and parenthesis, e.g.: for(, if(, while(
Jason Rhinelanderac427892016-08-28 13:00:44 -040010#
11# Invoke as: tools/check-style.sh
12#
13
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040014errors=0
15IFS=$'\n'
16found=
Jason Rhinelanderd472f0f2016-08-29 20:59:48 -040017# The mt=41 sets a red background for matched tabs:
Wenzel Jakob85f07e12016-09-04 23:00:49 +090018exec 3< <(GREP_COLORS='mt=41' grep $'\t' include/ tests/*.{cpp,py,h} docs/*.rst -rn --color=always)
Jason Rhinelander5a3570c2016-08-29 19:04:12 -040019while read -u 3 f; do
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040020 if [ -z "$found" ]; then
Jason Rhinelanderac427892016-08-28 13:00:44 -040021 echo -e '\e[31m\e[01mError: found tabs instead of spaces in the following files:\e[0m'
22 found=1
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040023 errors=1
Jason Rhinelanderac427892016-08-28 13:00:44 -040024 fi
25
26 echo " $f"
27done
28
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040029found=
Wenzel Jakobfe342412016-09-06 13:02:29 +090030# The mt=41 sets a red background for matched trailing spaces
31exec 3< <(GREP_COLORS='mt=41' grep '\s\+$' include/ tests/*.{cpp,py,h} docs/*.rst -rn --color=always)
32while 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"
40done
41
42found=
Wenzel Jakob85f07e12016-09-04 23:00:49 +090043exec 3< <(grep '\<\(if\|for\|while\)(\|){' include/ tests/*.{cpp,py,h} -rn --color=always)
Jason Rhinelander5a3570c2016-08-29 19:04:12 -040044while read -u 3 line; do
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040045 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"
52done
53
54exit $errors