1#!/bin/bash 2# 3# Script to check include/test code for common pybind11 code style errors. 4# 5# This script currently checks for 6# 7# 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 10# if/while/for/do statement. 11# 12# Invoke as: tools/check-style.sh <filenames> 13# 14 15check_style_errors=0 16IFS=$'\n' 17 18 19found="$(grep '\<\(if\|for\|while\|catch\)(\|){' $@ -rn --color=always)" 20if [ -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 25 26found="$(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 : "" } 37' $(find include -type f) $@)" 38if [ -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 43 44exit $check_style_errors 45