blob: f7af2a4169744334af0b9c28823e98a502b813be [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#
Henry Schreiner8b0ccf72020-07-20 15:07:22 -04007# 1. missing space between keyword and parenthesis, e.g.: for(, if(, while(
8# 2. Missing space between right parenthesis and brace, e.g. 'for (...){'
9# 3. opening brace on its own line. It should always be on the same line as the
Unknown0b3f44e2017-11-01 21:08:06 -040010# if/while/for/do statement.
Henry Schreiner43126202017-09-10 06:24:33 -040011#
Henry Schreiner8b0ccf72020-07-20 15:07:22 -040012# Invoke as: tools/check-style.sh <filenames>
Jason Rhinelanderac427892016-08-28 13:00:44 -040013#
14
Henry Schreiner43126202017-09-10 06:24:33 -040015check_style_errors=0
Jason Rhinelanderdbc4bf62016-08-28 14:53:04 -040016IFS=$'\n'
Jason Rhinelanderac427892016-08-28 13:00:44 -040017
Jason Rhinelanderac427892016-08-28 13:00:44 -040018
Henry Schreiner8b0ccf72020-07-20 15:07:22 -040019found="$(grep '\<\(if\|for\|while\|catch\)(\|){' $@ -rn --color=always)"
Henry Schreiner43126202017-09-10 06:24:33 -040020if [ -n "$found" ]; then
21 echo -e '\033[31;01mError: found the following coding style problems:\033[0m'
22 check_style_errors=1
23 echo "$found" | sed -e 's/^/ /'
24fi
Wenzel Jakobfe342412016-09-06 13:02:29 +090025
Henry Schreiner43126202017-09-10 06:24:33 -040026found="$(awk '
27function prefix(filename, lineno) {
28 return " \033[35m" filename "\033[36m:\033[32m" lineno "\033[36m:\033[0m"
29}
30function mark(pattern, string) { sub(pattern, "\033[01;31m&\033[0m", string); return string }
31last && /^\s*{/ {
32 print prefix(FILENAME, FNR-1) mark("\\)\\s*$", last)
33 print prefix(FILENAME, FNR) mark("^\\s*{", $0)
34 last=""
35}
36{ last = /(if|for|while|catch|switch)\s*\(.*\)\s*$/ ? $0 : "" }
Henry Schreiner8b0ccf72020-07-20 15:07:22 -040037' $(find include -type f) $@)"
Henry Schreiner43126202017-09-10 06:24:33 -040038if [ -n "$found" ]; then
39 check_style_errors=1
40 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'
41 echo "$found"
42fi
Wenzel Jakobde2c6df2016-12-13 00:17:29 +010043
Henry Schreiner43126202017-09-10 06:24:33 -040044exit $check_style_errors