From c7e7cebed6e40e0828aa36c3ed3f978c15ea8f5e Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 22 May 2014 15:03:40 +0900 Subject: [PATCH] Small tweaks to our whitespace handling This is still not (and likely will never be) intended to just be blindly run and mechanically applied to all files. It tries to implement our rules but it is not perfect and more importantly, we have parts of the code where we intentionally break our rules for various reasons of readability in that particular situation. But running this against the sources files you touch often will point out things that are wrong and should be fixed. This fixes the indentation for continuation lines and the handling of the for each style loops (clang 3.5 should have this built in - I'll play with the current development version of this later). Signed-off-by: Dirk Hohndel --- .clang-format | 2 ++ scripts/whitespace.pl | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.clang-format b/.clang-format index 6b75350e0..092136768 100644 --- a/.clang-format +++ b/.clang-format @@ -10,6 +10,7 @@ ConstructorInitializerAllOnOneLineOrOnePerLine: true ColumnLimit: 0 IndentFunctionDeclarationAfterType: false #personal taste, good for long methods IndentWidth: 8 +ContinuationIndentWidth: 8 MaxEmptyLinesToKeep: 2 NamespaceIndentation: All SpaceBeforeAssignmentOperators: true @@ -18,3 +19,4 @@ SpacesInParentheses: false SpacesBeforeTrailingComments: 1 UseTab: Always PointerBindsToType: false +# doesn't work, yet -> ForEachMacros: "Q_FOREACH" diff --git a/scripts/whitespace.pl b/scripts/whitespace.pl index cc7c6511b..3131d22d4 100755 --- a/scripts/whitespace.pl +++ b/scripts/whitespace.pl @@ -3,17 +3,23 @@ my $input = $ARGV[0]; my $source = `clang-format $input`; # for_each_dive (...) and Q_FOREACH and friends... -$source =~ s/^(.*each.*\(.*\).*)\n\s*{\s*$/$1 {/img; +$source =~ s/(?:\G|^)(.*each.*\(.*) \* (\S.*\))$/$1 *$2/img; # if a variable is declared in the argument, '*' is an indicator for a pointer, not arithmatic +$source =~ s/(?:\G|^)(.*each.*\(.*) \& (\S.*\))$/$1 &$2/img; # if a variable is declared in the argument, '&' is an indicator for a reference, not bit logic +$source =~ s/(?:\G|^)(.*each[^\s(]*)\s*(\(.*)$/$1 $2/img; # we want exactly one space between keyword and opening parenthesis '(' +$source =~ s/(?:\G|^)(.*each.*\(.*\).*)\n\s*{\s*$/$1 {/img; # we want the opening curly brace on the same line, separated by a space $source =~ s/(?:\G|^)(\s+[^#\/*].*each.*\(.*\))\n(\s*)([^{}\s])/$1\n\t$2$3/img; +# don't have '{' in the next line when declaring data types $source =~ s/^(\s*struct[^()\n]*)\n\s*{\s*$/$1 {/img; $source =~ s/^(\s*static\s+struct[^()\n]*)\n\s*{\s*$/$1 {/img; $source =~ s/^(\s*union[^()\n]*)\n\s*{\s*$/$1 {/img; $source =~ s/^(\s*static\s+union[^()\n]*)\n\s*{\s*$/$1 {/img; $source =~ s/^(\s*class.*)\n\s*{\s*$/$1 {/img; +# a namespace shouldn't look like a function +$source =~ s/(?:\G|^)(namespace.*)\n{/$1 {/img; # colon goes at the end of a line $source =~ s/^(\S*::\S*.*)\n\s*: /$1 : /img; -# odd indentations from flang-format: +# odd indentations from clang-format: # six spaces or four spaces after tabs (for continuation strings) $source =~ s/(?:\G|^)[ ]{6}/\t/mg; $source =~ s/(?:\G|^)(\t*)[ ]{4}"/$1\t"/mg; @@ -23,15 +29,16 @@ $source =~ s/(?:\G|^)(\t*)[ ]{4}"/$1\t"/mg; # from? # I couldn't figure out how to make it apply to an arbitrary number of # intermediate lines, so I hardcoded 0 through 5 lines between the #define -# or #id defined statements and the end of the multi line statement +# or #if defined statements and the end of the multi line statement $source =~ s/^(#(?:if |)define.*)\n +([^*].*)$/$1\n\t$2/mg; $source =~ s/^(#(?:if |)define.*)((?:\\\n.*){1})\n +([^*].*)$/$1$2\n\t$3/mg; $source =~ s/^(#(?:if |)define.*)((?:\\\n.*){2})\n +([^*].*)$/$1$2\n\t$3/mg; $source =~ s/^(#(?:if |)define.*)((?:\\\n.*){3})\n +([^*].*)$/$1$2\n\t$3/mg; $source =~ s/^(#(?:if |)define.*)((?:\\\n.*){4})\n +([^*].*)$/$1$2\n\t$3/mg; $source =~ s/^(#(?:if |)define.*)((?:\\\n.*){5})\n +([^*].*)$/$1$2\n\t$3/mg; -# don't put line break before the last single term argument of a -# calculation +# don't put line break before the last single term argument of a calculation $source =~ s/(?:\G|^)(.*[+-])\n\s*(\S*\;)$/$1 $2/mg; -open (DIFF, "| diff -u $input -"); +$quotedinput = $input; +$quotedinput =~ s|/|\\/|g; +open (DIFF, "| diff -u $input - | sed -e 's/--- $quotedinput/--- $quotedinput.old/' | sed -e 's/+++ -/+++ $quotedinput/'"); print DIFF $source ;