Note this requires clang-format from clang >= 3.8.0
(3.7.0 is too old as it does not have the BreakBeforeBraces: Custom
style).
---
.gitignore | 1 +
scripts/code-format.pl | 146 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 147 insertions(+)
create mode 100755 scripts/code-format.pl
diff --git a/.gitignore b/.gitignore
index ca4e89c..ad166d2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,6 +32,7 @@ cscope.out
Makefile
Makefile.in
+/.clang-format
/.sc-*
/ABOUT-NLS
/aclocal.m4
diff --git a/scripts/code-format.pl b/scripts/code-format.pl
new file mode 100755
index 0000000..f4c1f34
--- /dev/null
+++ b/scripts/code-format.pl
@@ -0,0 +1,146 @@
+#!/usr/bin/perl -w
+# libguestfs
+# Copyright (C) 2016 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Reformat the code in libguestfs (currently only C code).
+
+use warnings;
+use strict;
+
+use YAML qw(DumpFile);
+
+# Check we are run from the top level directory.
+die "$0: you must run this script from the top level source directory\n"
+ unless -f "BUGS";
+
+# Make sure we have the clang-format program.
+system ("clang-format --help >/dev/null 2>&1") == 0
+ or die "$0: 'clang-format' program (from Clang) must be
installed\n";
+
+my @c_files = `git ls-files '*.[ch]'`;
+chomp @c_files;
+
+#
http://clang.llvm.org/docs/ClangFormatStyleOptions.html
+my $clang_stylesheet = {
+ Language => "Cpp",
+ AccessModifierOffset => -2,
+ AlignAfterOpenBracket => "true",
+ AlignConsecutiveAssignments => "false",
+ AlignEscapedNewlinesLeft => "false",
+ AlignOperands => "true",
+ AlignTrailingComments => "true",
+ AllowAllParametersOfDeclarationOnNextLine => "true",
+ AllowShortBlocksOnASingleLine => "false",
+ AllowShortCaseLabelsOnASingleLine => "false",
+ AllowShortFunctionsOnASingleLine => "All",
+ AllowShortIfStatementsOnASingleLine => "false",
+ AllowShortLoopsOnASingleLine => "false",
+ AlwaysBreakAfterDefinitionReturnType => "TopLevel",
+ AlwaysBreakBeforeMultilineStrings => "false",
+ AlwaysBreakTemplateDeclarations => "false",
+ BinPackArguments => "true",
+ BinPackParameters => "true",
+ BraceWrapping => {
+ AfterClass => "false",
+ AfterControlStatement => "false",
+ AfterEnum => "false",
+ AfterFunction => "true",
+ AfterNamespace => "true",
+ AfterObjCDeclaration => "false",
+ AfterStruct => "true",
+ AfterUnion => "true",
+ BeforeCatch => "false",
+ BeforeElse => "false",
+ #AfterIndentBraces => "false", -- only in clang > 3.8.0
+ },
+ BreakBeforeBinaryOperators => "None",
+ BreakBeforeBraces => "Custom",
+ BreakBeforeTernaryOperators => "true",
+ BreakConstructorInitializersBeforeComma => "false",
+ #BreakStringLiterals => "false", -- only in clang > 3.8.0
+ ColumnLimit => 76,
+ CommentPragmas => "^ IWYU pragma:",
+ ConstructorInitializerAllOnOneLineOrOnePerLine => "false",
+ ConstructorInitializerIndentWidth => 4,
+ ContinuationIndentWidth => 4,
+ Cpp11BracedListStyle => "false",
+ DerivePointerAlignment => "false",
+ DisableFormat => "false",
+ ExperimentalAutoDetectBinPacking => "false",
+ ForEachMacros => [],
+ IndentCaseLabels => "false",
+ IndentWidth => 2,
+ IndentWrappedFunctionNames => "false",
+ KeepEmptyLinesAtTheStartOfBlocks => "true",
+ MacroBlockBegin => "",
+ MacroBlockEnd => "",
+ MaxEmptyLinesToKeep => 1,
+ NamespaceIndentation => "None",
+ ObjCBlockIndentWidth => 2,
+ ObjCSpaceAfterProperty => "false",
+ ObjCSpaceBeforeProtocolList => "true",
+ PenaltyBreakBeforeFirstCallParameter => 19,
+ PenaltyBreakComment => 300,
+ PenaltyBreakFirstLessLess => 120,
+ PenaltyBreakString => 1000,
+ PenaltyExcessCharacter => 1000000,
+ PenaltyReturnTypeOnItsOwnLine => 60,
+ PointerAlignment => "Right",
+ SortIncludes => "false",
+ SpaceAfterCStyleCast => "false",
+ SpaceBeforeAssignmentOperators => "true",
+ SpaceBeforeParens => "Always",
+ SpaceInEmptyParentheses => "false",
+ SpacesBeforeTrailingComments => 1,
+ SpacesInAngles => "false",
+ SpacesInContainerLiterals => "true",
+ SpacesInCStyleCastParentheses => "false",
+ SpacesInParentheses => "false",
+ SpacesInSquareBrackets => "false",
+ Standard => "Cpp11",
+ TabWidth => 8,
+ UseTab => "Never",
+};
+DumpFile (".clang-format", $clang_stylesheet);
+
+# Run clang-format on each file, then post-process the output further.
+foreach my $input (@c_files) {
+ my @o = ();
+
+ open INPUT, "clang-format $input |"
+ or die "clang-format: $input: $!";
+ push @o, $_ while <INPUT>;
+ close INPUT or die;
+
+ # Post-process the output.
+ my ($i, $j);
+ for ($i = 0; $i < @o; ++$i) {
+ # rewrite gettext _ ("") -> _("")
+ if (($o[$i] =~ s{_ \("}{_("}g) == 1) {
+ # If the following lines are strings, assume a continuation
+ # string and reduce its indent by 1 character.
+ for ($j = $i+1; $j < @o; ++$j) {
+ last if ($o[$j] =~ s{^(\s+)\s"}{$1"}) == 0;
+ }
+ }
+ }
+
+ # Overwrite the original file with the output.
+ open OUTPUT, "> $input" or die "$input: $!";
+ print OUTPUT $_ foreach @o;
+ close OUTPUT;
+}
--
2.7.4