Automated Code Tidying

tidy-source.sh

This simple script searches a directory for source files, and performs the following actions:

  • Replaces tabs with spaces
  • Ensures the file ends with a newline
  • Replaces CRLF with LF
  • Removes any trailing whitespace on each line of code
#!/bin/bash

DIR=$1
TABWIDTH=4

( cd $DIR; for i in $( find -regex ".*\.\(c\|cpp\|cxx\|cc\|h\|\hpp\)" | sort ); do echo $i; expand -t$TABWIDTH $i | sed '${/^$/!s/$/\
/;}' | sed 's/\x0D$//' | sed 's/ *$//' > x; mv x $i; done )

git commit -am"Tidied $DIR. Replaced tabs with spaces, ensured each source file ends with a newline, replaced CRLF with LF, and removed trailing whitespace"

Other Tools

  • clang-tidy - Code formatting built around the clang C++ language front end.
  • astyle - A Free, Fast and Small Automatic Formatter for C, C++, C++/CLI, C#, and Java Source Code
automated_code_tidying.txt ยท Last modified: 2016/07/22 17:50 (external edit)