Skip to content

Commit

Permalink
check code formatting via clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev Walkin committed Jan 21, 2016
1 parent e31b21f commit 1e397a6
Show file tree
Hide file tree
Showing 38 changed files with 1,949 additions and 1,645 deletions.
67 changes: 67 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: false
AlignEscapedNewlinesLeft: true
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: TopLevel
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, TAILQ_FOREACH, TAILQ_FOREACH_SAFE ]
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 4
UseTab: Never
...

4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ dnl We use pandoc to generate documentation.
AC_PATH_PROG([PANDOC], pandoc)
AM_CONDITIONAL([HAVE_PANDOC], [test -n "$PANDOC"])

dnl Use clang-format to standardize on a style.
AC_PATH_PROG([CLANG_FORMAT], clang-format, [:])
AM_CONDITIONAL([HAVE_CLANG_FORMAT], [test "${CLANG_FORMAT}" != ":"])

dnl Python is used for tests.
AM_PATH_PYTHON(,, [:])
AM_CONDITIONAL([HAVE_PYTHON], [test "$PYTHON" != :])
Expand Down
6 changes: 5 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ check_tcpkali_iface_CFLAGS = -std=gnu99 $(TK_CFLAGS) -DTCPKALI_IFACE_UNIT_TEST
check_platform_SOURCES = check_platform.c
check_platform_CFLAGS = -std=gnu99 $(TK_CFLAGS)

TESTS = $(check_PROGRAMS)
TESTS = $(check_PROGRAMS) ${dist_check_SCRIPTS}
check_PROGRAMS = check_tcpkali_ring check_platform check_tcpkali_iface

dist_check_SCRIPTS = check_code_format.sh

reformat:
${CLANG_FORMAT} -i $(shell ls *.[ch] | grep -v "^tcpkali_expr_[yl].[ch]")
34 changes: 34 additions & 0 deletions src/check_code_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

set -eu
set -o pipefail

if ! which clang-format >/dev/null 2>&1; then
echo clang-format is not installed
echo Source code format not checked.
exit 0
fi

if ! git diff >/dev/null; then
echo The code is not part of git repo.
echo Source code format not checked.
fi

if git diff 2>/dev/null | grep -qc .; then
echo Git shows uncommitted changes. Fix that first.
git status -sbuno | grep '^ M'
echo Source code format not checked.
exit 1
fi

clang-format -i `ls *.[ch] | grep -v "^tcpkali_expr_[yl].[ch]$"`

if git diff 2>/dev/null | grep -qc .; then
echo clang-format has found formatting errors.
git status -sbuno | grep '^ M'
git checkout .
echo "Run \`make reformat\` to format the source code."
exit 1
fi

echo "Code formatting is OK"
15 changes: 9 additions & 6 deletions src/check_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ struct atomic_holder_2 {
atomic_wide_t atomic __attribute__((aligned(sizeof(atomic_wide_t))));
};

int main() {


int
main() {
/* Check that atomic is properly aligned for atomicity */
assert(((long)(&((struct atomic_holder_1 *)0)->atomic) & (sizeof(atomic_narrow_t)-1)) == 0);
assert(((long)(&((struct atomic_holder_2 *)0)->atomic) & (sizeof(atomic_narrow_t)-1)) == 0);
assert(((long)(&((struct atomic_holder_1 *)0)->atomic)
& (sizeof(atomic_narrow_t) - 1))
== 0);
assert(((long)(&((struct atomic_holder_2 *)0)->atomic)
& (sizeof(atomic_narrow_t) - 1))
== 0);

assert((&((struct atomic_holder_1 *)0)->atomic)
== (&((struct atomic_holder_2 *)0)->atomic));
== (&((struct atomic_holder_2 *)0)->atomic));

return 0;
}
Loading

0 comments on commit 1e397a6

Please sign in to comment.