File indexing completed on 2024-04-14 05:35:12

0001 #!/bin/sh
0002 
0003 # Apply KF5, Qt or custom coding style to all c, cpp and header files in and below the current directory
0004 #
0005 # The coding style is defined in http://techbase.kde.org/Policies/Kdelibs_Coding_Style
0006 #
0007 # Requirements:
0008 # - installed uncrustify >= 0.63
0009 #          Do not use an older version, it will lead to bad reformatting.
0010 # - $QT_NORMALIZE_TOOL pointing to qtrepotools/util/normalize/normalize (after compiling it)
0011 #
0012 # Report bugs in uncrustify at https://github.com/uncrustify/uncrustify/issues
0013 # Documentation of the uncrustify options at http://uncrustify.sourceforge.net/default.cfg
0014 #
0015 
0016 help() {
0017   echo "`basename $0` [--qt] [--commit] - Apply KF5, Qt or custom coding style to all c, cpp and header files in and below the current directory"
0018   echo "Simply chdir to the directory containing your code and run this program."
0019   echo
0020   echo "Options:"
0021   echo "    --qt               - use Qt style (default is KDE Frameworks 5 style, which could be overriden by a file uncrustify.cfg in the current directory)"
0022   echo "    --commit           - commit changes"
0023   echo "    --ignore-connects  - do not reformat connects"
0024   echo
0025   echo
0026 }
0027 
0028 qt=0
0029 commit=0
0030 no_slots=1
0031 until [ -z "$1" ]; do
0032   case $1 in
0033     --qt)
0034       qt=1
0035       ;;
0036     --commit)
0037       commit=1
0038       ;;
0039     --ignore-connects)
0040       ignore_connects=1
0041       ;;
0042     *)
0043       help
0044       exit 1
0045       ;;
0046   esac
0047   shift
0048 done
0049 
0050 files=`find -type f -name '*.c' -or -name '*.cpp' -or -name '*.cc' -or -name '*.h'`
0051 if [ -z "$files" ]; then
0052   # nothing to do
0053   exit 0
0054 fi
0055 
0056 if test -z "$QT_NORMALIZE_TOOL" && test "$ignore_connects" == "0"; then
0057   echo "Please export QT_NORMALIZE_TOOL=<qt5>/qtrepotools/util/normalize/normalize"
0058   exit 1
0059 fi
0060 
0061 cfg=uncrustify.cfg
0062 if ! test -f "$cfg"; then
0063   if test "$qt" == "1"; then
0064     cfgfile=uncrustify-qt.cfg
0065   else
0066     cfgfile=uncrustify-kf5.cfg
0067   fi
0068   cfg=`qtpaths --locate-file GenericDataLocation uncrustify/$cfgfile`
0069   if test -z "$cfg"; then
0070     cfg=$(dirname $0)/$cfgfile
0071     if ! test -f "$cfg"; then
0072       echo "Config file uncrustify/$cfgfile not found in prefix/share (GenericDataLocation) or $(dirname $0). Check that XDG_DATA_DIRS contains the install prefix for kde-dev-scripts."
0073       exit 1
0074     fi
0075   fi
0076 fi
0077 
0078 uncrustify --no-backup -c "$cfg" $files
0079 
0080 # Watch out for things that lead to method implementations being parsed as if inside other methods,
0081 # e.g. due to '{' inside #ifdef and #else and '}' outside.
0082 grep '^\S* \S*::.*) {$' $files && echo "WARNING: check for wrong '{' placement in method definitions, in above grep results"
0083 
0084 # Remove old emacs mode-lines
0085 perl -pi -e 's/ *-\*- c-basic-offset: [1-8] -\*-//' $files
0086 
0087 # Remove old kate mode-lines
0088 perl -pi -e '$_ = "" if /kate: .*indent-width/ || /kate:.*tab-width/' $files
0089 
0090 # Remove old vim mode-lines
0091 perl -pi -e '$_ = "" if /\/\/.* vim:/' $files
0092 # They are often in a two-liner C comment, so we need a bit of perl magic to remove these
0093 perl - $files <<EOF
0094   foreach my \$file (@ARGV) {
0095    open F, "+<", \$file or do { print STDERR "open(\$file) failed : \"\$!\"\n"; next };
0096    my \$str = join '', <F>;
0097    if( \$str =~ m/vim:/ ) {
0098      #print STDERR "Removing multi-line vim modeline from \$file\n";
0099      \$str =~ s!/\*\**\s*\**\s*vim:[^\n]*\n\s*\*/!!smg;
0100      seek F, 0, 0;
0101      print F \$str;
0102      truncate F, tell(F);
0103    }
0104    close F;
0105   }
0106 EOF
0107 
0108 # Remove consecutive blank lines
0109 perl - $files <<EOF
0110   foreach my \$file (@ARGV) {
0111    open F, "+<", \$file or do { print STDERR "open(\$file) failed : \"\$!\"\n"; next };
0112    my \$str = join '', <F>;
0113    if (\$str =~ s/\s*\n\s*\n\s*\n\n*/\n\n/smg ) {
0114      seek F, 0, 0;
0115      print F \$str;
0116      truncate F, tell(F);
0117    }
0118    close F;
0119   }
0120 EOF
0121 
0122 # Normalize signals/slots
0123 if test -n "$QT_NORMALIZE_TOOL" && test "$ignore_connects" == "1"; then
0124   $QT_NORMALIZE_TOOL --modify .
0125 fi
0126 
0127 if test "$commit" == "1"; then
0128   git commit -q -a -m "Code reformatted using kde-dev-scripts/`basename $0`"
0129 fi
0130 
0131 #Use git blame -w `git rev-parse --short HEAD` to show authorship as it was before this commit."