astyle-kdelibs 3.75 KB
Newer Older
David Faure's avatar
David Faure committed
1
#!/bin/sh
2

3
4
5
6
# Apply kdelibs coding style to all c, cpp and header files in and below the current directory
#
# The coding style is defined in http://techbase.kde.org/Policies/Kdelibs_Coding_Style
#
7
8
9
# Requirements:
# - installed astyle, with patches below
# - $QT_NORMALIZE_TOOL pointing to qtrepotools/util/normalize/normalize (after compiling it)
10
#
11
12
# IMPORTANT: use astyle 2.05 or later. Patch astyle if you use older version.
# Older versions of astyle misparse the Qt "keywords" like foreach and Q_FOREACH, which
13
14
15
16
# inside the parenthesis, leading to things like:
# Q_FOREACH(const QString & it, l) // note the space after the '&'.
#
# To fix this, patch astyle with http://www.davidfaure.fr/kde/astyle_qt.diff
17
# Mentionned upstream in https://sourceforge.net/p/astyle/bugs/154/
18
#
19
20
# Another feature was implemented: removing spaces before ',' and ';'.
# Apply http://www.davidfaure.fr/kde/astyle_comma.diff
21
# This has been sent upstream in https://sourceforge.net/p/astyle/bugs/100/
22
#
23
24
25
# Instructions for OpenSuSE users:
#   zypper si astyle
#   cd ~/rpmbuild/SOURCES ; wget http://www.davidfaure.fr/kde/astyle_qt.diff
26
#                           wget http://www.davidfaure.fr/kde/astyle_comma.diff
27
28
29
30
#   cd ../SPECS ; wget http://www.davidfaure.fr/kde/astyle.spec.diff
#   patch astyle.spec < astyle.spec.diff
#   rpmbuild -ba astyle.spec
#   sudo rpm -Uvh --force ~/rpmbuild/RPMS/x86_64/astyle-*.rpm
31

32
files=`find -type f -name '*.c' -or -name '*.cpp' -or -name '*.cc' -or -name '*.h'`
33
34
35
36
37
if [ -z "$files" ]; then
  # nothing to do
  exit 0
fi

38
39
40
41
42
if test -z "$QT_NORMALIZE_TOOL"; then
  echo "Please export QT_NORMALIZE_TOOL=<qt5>/qtrepotools/util/normalize/normalize"
  exit 1
fi

43
astyle -n -Q \
44
      --indent=spaces=4 --style=otbs \
45
      --indent-labels --pad-oper --unpad-paren --pad-header \
46
47
48
49
      --keep-one-line-statements \
      --convert-tabs \
      --indent-preprocessor \
      --align-pointer=name \
50
51
      $files

52
53
54
55
# --max-code-length=100 is not used, on purpose. It's a hard limit, sometimes making things
# less readable than if the code is kept on a single line. 100 is just a recommendation
# in the coding style.

56
57
58
59
# Watch out for things that lead to method implementations being parsed as if inside other methods,
# e.g. due to '{' inside #ifdef and #else and '}' outside.
grep '^\S* \S*::.*) {$' $files && echo "WARNING: check for wrong '{' placement in method definitions, in above grep results"

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Remove old emacs mode-lines
perl -pi -e '$_ = "" if /c-basic-offset: [1-8]/' $files

# Remove old kate mode-lines
perl -pi -e '$_ = "" if /kate: .*indent-width/ || /kate:.*tab-width/' $files

# Remove old vim mode-lines
perl -pi -e '$_ = "" if /\/\/.* vim:/' $files
# They are often in a two-liner C comment, so we need a bit of perl magic to remove these
perl - $files <<EOF
  foreach my \$file (@ARGV) {
   open F, "+<", \$file or do { print STDERR "open(\$file) failed : \"\$!\"\n"; next };
   my \$str = join '', <F>;
   if( \$str =~ m/vim:/ ) {
     #print STDERR "Removing multi-line vim modeline from \$file\n";
     \$str =~ s!/\*\**\s*\**\s*vim:[^\n]*\n\s*\*/!!smg;
     seek F, 0, 0;
     print F \$str;
     truncate F, tell(F);
   }
   close F;
  }
EOF

84
85
86
87
88
89
90
91
92
93
94
95
96
# Remove consecutive blank lines
perl - $files <<EOF
  foreach my \$file (@ARGV) {
   open F, "+<", \$file or do { print STDERR "open(\$file) failed : \"\$!\"\n"; next };
   my \$str = join '', <F>;
   if (\$str =~ s/\s*\n\s*\n\s*\n\n*/\n\n/smg ) {
     seek F, 0, 0;
     print F \$str;
     truncate F, tell(F);
   }
   close F;
  }
EOF
97
98
99
100

# Normalize signals/slots
$QT_NORMALIZE_TOOL --modify .

101
102
103
104
# TODO: add command-line option to trigger this
# It's not wanted when working on a fix
#git commit -q -a -m "Code reformatted using kde-dev-scripts/astyle-kdelibs.
#Use git blame -w `git rev-parse --short HEAD` to show authorship as it was before this commit."
105