File indexing completed on 2024-12-15 05:11:24

0001 #!/bin/sh
0002 
0003 # Apply kdelibs 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 astyle, with patches below
0009 # - $QT_NORMALIZE_TOOL pointing to qtrepotools/util/normalize/normalize (after compiling it)
0010 #
0011 # IMPORTANT: use astyle 2.05 or later. Patch astyle if you use older version.
0012 # Older versions of astyle misparse the Qt "keywords" like foreach and Q_FOREACH, which
0013 # inside the parenthesis, leading to things like:
0014 # Q_FOREACH(const QString & it, l) // note the space after the '&'.
0015 #
0016 # To fix this, patch astyle with http://www.davidfaure.fr/kde/astyle_qt.diff
0017 # Mentionned upstream in https://sourceforge.net/p/astyle/bugs/154/
0018 #
0019 # Another feature was implemented: removing spaces before ',' and ';'.
0020 # Apply http://www.davidfaure.fr/kde/astyle_comma.diff
0021 # This has been sent upstream in https://sourceforge.net/p/astyle/bugs/100/
0022 #
0023 # Instructions for OpenSuSE users:
0024 #   zypper si astyle
0025 #   cd ~/rpmbuild/SOURCES ; wget http://www.davidfaure.fr/kde/astyle_qt.diff
0026 #                           wget http://www.davidfaure.fr/kde/astyle_comma.diff
0027 #   cd ../SPECS ; wget http://www.davidfaure.fr/kde/astyle.spec.diff
0028 #   patch astyle.spec < astyle.spec.diff
0029 #   rpmbuild -ba astyle.spec
0030 #   sudo rpm -Uvh --force ~/rpmbuild/RPMS/x86_64/astyle-*.rpm
0031 
0032 files=`find -type f -name '*.c' -or -name '*.cpp' -or -name '*.cc' -or -name '*.h'`
0033 if [ -z "$files" ]; then
0034   # nothing to do
0035   exit 0
0036 fi
0037 
0038 if test -z "$QT_NORMALIZE_TOOL"; then
0039   echo "Please export QT_NORMALIZE_TOOL=<qt5>/qtrepotools/util/normalize/normalize"
0040   exit 1
0041 fi
0042 
0043 astyle -n -Q \
0044       --indent=spaces=4 --style=otbs \
0045       --indent-labels --pad-oper --unpad-paren --pad-header \
0046       --keep-one-line-statements \
0047       --convert-tabs \
0048       --indent-preprocessor \
0049       --align-pointer=name \
0050       $files
0051 
0052 # --max-code-length=100 is not used, on purpose. It's a hard limit, sometimes making things
0053 # less readable than if the code is kept on a single line. 100 is just a recommendation
0054 # in the coding style.
0055 
0056 # Watch out for things that lead to method implementations being parsed as if inside other methods,
0057 # e.g. due to '{' inside #ifdef and #else and '}' outside.
0058 grep '^\S* \S*::.*) {$' $files && echo "WARNING: check for wrong '{' placement in method definitions, in above grep results"
0059 
0060 # Remove old emacs mode-lines
0061 perl -pi -e '$_ = "" if /c-basic-offset: [1-8]/' $files
0062 
0063 # Remove old kate mode-lines
0064 perl -pi -e '$_ = "" if /kate: .*indent-width/ || /kate:.*tab-width/' $files
0065 
0066 # Remove old vim mode-lines
0067 perl -pi -e '$_ = "" if /\/\/.* vim:/' $files
0068 # They are often in a two-liner C comment, so we need a bit of perl magic to remove these
0069 perl - $files <<EOF
0070   foreach my \$file (@ARGV) {
0071    open F, "+<", \$file or do { print STDERR "open(\$file) failed : \"\$!\"\n"; next };
0072    my \$str = join '', <F>;
0073    if( \$str =~ m/vim:/ ) {
0074      #print STDERR "Removing multi-line vim modeline from \$file\n";
0075      \$str =~ s!/\*\**\s*\**\s*vim:[^\n]*\n\s*\*/!!smg;
0076      seek F, 0, 0;
0077      print F \$str;
0078      truncate F, tell(F);
0079    }
0080    close F;
0081   }
0082 EOF
0083 
0084 # Remove consecutive blank lines
0085 perl - $files <<EOF
0086   foreach my \$file (@ARGV) {
0087    open F, "+<", \$file or do { print STDERR "open(\$file) failed : \"\$!\"\n"; next };
0088    my \$str = join '', <F>;
0089    if (\$str =~ s/\s*\n\s*\n\s*\n\n*/\n\n/smg ) {
0090      seek F, 0, 0;
0091      print F \$str;
0092      truncate F, tell(F);
0093    }
0094    close F;
0095   }
0096 EOF
0097 
0098 # Normalize signals/slots
0099 $QT_NORMALIZE_TOOL --modify .
0100 
0101 # TODO: add command-line option to trigger this
0102 # It's not wanted when working on a fix
0103 #git commit -q -a -m "Code reformatted using kde-dev-scripts/astyle-kdelibs.
0104 #Use git blame -w `git rev-parse --short HEAD` to show authorship as it was before this commit."
0105