Last active
September 17, 2019 15:50
-
-
Save chtenb/fad264795d2bb61f0dc3199be0cf98b4 to your computer and use it in GitHub Desktop.
Example shell script that uses unix tools to check source code for some basic problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
exitcode=0 | |
root=`git rev-parse --show-toplevel` | |
echo "Root of repository: $root" | |
echo | |
cd "$root/Source" | |
source_files=$(find . \( -name \*.h -or -name \*.cpp \) \ | |
-not -path "./packages/*" \ | |
-not -path "./test/*" \ | |
-not -path "./bin/*") | |
header_files=$(find . \( -name \*.h \) \ | |
-not -path "./packages/*" \ | |
-not -path "./test/*" \ | |
-not -path "./bin/*") | |
# Check if there are header files without include guards | |
# The check can be disabled for any header file by inserting the exact text 'disable_include_guard_check', | |
# i.e. in a comment section. | |
files_without_include_guard=$(echo "$header_files" | xargs -I file sh -c \ | |
'guard=file;guard=${guard##*/};guard=${guard//./_};guard=${guard^^};grep -La "$guard\|#pragma once\|disable_include_guard_check" file') | |
if [[ -n "$files_without_include_guard" ]]; then | |
echo | |
echo "The following header files do not contain an include guard:" | |
echo | |
echo "$files_without_include_guard" | |
exitcode=1 | |
fi | |
# Check if source files contain tab characters. | |
files_with_tabs=$(echo "$source_files" | xargs grep -UPla '\t') | |
if [[ -n "$files_with_tabs" ]]; then | |
echo | |
echo "The following files contain tab characters:" | |
echo | |
echo "$files_with_tabs" | |
exitcode=1 | |
fi | |
# To replace all tabs: (warning! this introduces lf endings) | |
#echo "$files_with_tabs" | xargs sed -zi 's/\t/ /g' | |
# Check if all lines in source files end with a windows line ending | |
files_with_lf=$(echo "$source_files" | xargs grep -UPlazv '\r') | |
if [[ -n "$files_with_lf" ]]; then | |
echo | |
echo "The following files contain unix line endings:" | |
echo | |
echo "$files_with_lf" | |
exitcode=1 | |
fi | |
# To replace all lf with crlf: | |
#echo "$files_with_lf" | xargs unix2dos | |
# Check if all source files consist of ascii characters only. | |
files_with_non_ascii=$(echo "$source_files" | xargs grep -UPla '[\x80-\xFF]') | |
if [[ -n "$files_with_non_ascii" ]]; then | |
echo | |
echo "The following files contain non-ascii characters:" | |
echo | |
echo "$files_with_non_ascii" | |
exitcode=1 | |
fi | |
# Check if all source files consist of printable characters only. | |
# Note that we omit the -U flag for grep, such that carriage returns are ignored. | |
# Also note that we exclude tab characters, since we have a separate check for that. | |
lines_with_non_printable=$(echo "$source_files" | xargs grep -Pna '[^[:print:]\t]') | |
if [[ -n "$lines_with_non_printable" ]]; then | |
echo | |
echo "The following lines contain non-printable characters:" | |
echo | |
echo "$lines_with_non_printable" | |
exitcode=1 | |
fi | |
# Check if the length of all source lines are limited by some predefined constant. | |
# Both for readability, and to catch respective zos compilation errors early on. | |
files_with_too_long_lines=$(echo "$source_files" | xargs grep -la '.\{100\}.') | |
if [[ -n "$files_with_too_long_lines" ]]; then | |
echo | |
echo "The following files contain lines that are too long:" | |
echo | |
echo "$files_with_too_long_lines" | |
exitcode=1 | |
fi | |
echo | |
[[ $exitcode -eq 0 ]] && echo Passed | |
[[ $exitcode -ne 0 ]] && echo Failed | |
exit $exitcode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment