Skip to content

Commit 1eade53

Browse files
authored
Add targets to check/format Py and C style (#1495)
1 parent cf8d5a9 commit 1eade53

File tree

4 files changed

+298
-0
lines changed

4 files changed

+298
-0
lines changed

.clang-format

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
Language: Cpp
3+
AccessModifierOffset: -2
4+
AlignAfterOpenBracket: Align
5+
AlignConsecutiveMacros: true
6+
AlignConsecutiveAssignments: true
7+
AlignConsecutiveDeclarations: false
8+
AlignEscapedNewlines: Right
9+
AlignOperands: true
10+
AlignTrailingComments: true
11+
AllowAllArgumentsOnNextLine: true
12+
AllowAllConstructorInitializersOnNextLine: true
13+
AllowAllParametersOfDeclarationOnNextLine: false
14+
AllowShortBlocksOnASingleLine: Never
15+
AllowShortCaseLabelsOnASingleLine: false
16+
AllowShortFunctionsOnASingleLine: None
17+
AllowShortLambdasOnASingleLine: All
18+
AllowShortIfStatementsOnASingleLine: Never
19+
AllowShortLoopsOnASingleLine: false
20+
AlwaysBreakAfterDefinitionReturnType: None
21+
AlwaysBreakAfterReturnType: None
22+
AlwaysBreakBeforeMultilineStrings: true
23+
AlwaysBreakTemplateDeclarations: MultiLine
24+
BinPackArguments: true
25+
BinPackParameters: false
26+
BraceWrapping:
27+
AfterCaseLabel: false
28+
AfterClass: false
29+
AfterControlStatement: false
30+
AfterEnum: false
31+
AfterFunction: false
32+
AfterNamespace: false
33+
AfterObjCDeclaration: false
34+
AfterStruct: false
35+
AfterUnion: false
36+
AfterExternBlock: false
37+
BeforeCatch: false
38+
BeforeElse: false
39+
IndentBraces: false
40+
SplitEmptyFunction: true
41+
SplitEmptyRecord: true
42+
SplitEmptyNamespace: true
43+
BreakBeforeBinaryOperators: None
44+
BreakBeforeBraces: Custom
45+
BreakBeforeInheritanceComma: false
46+
BreakInheritanceList: BeforeColon
47+
BreakBeforeTernaryOperators: true
48+
BreakConstructorInitializersBeforeComma: false
49+
BreakConstructorInitializers: AfterColon
50+
BreakAfterJavaFieldAnnotations: false
51+
BreakStringLiterals: true
52+
ColumnLimit: 80
53+
CommentPragmas: '^ IWYU pragma:'
54+
CompactNamespaces: false
55+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
56+
ConstructorInitializerIndentWidth: 4
57+
ContinuationIndentWidth: 4
58+
Cpp11BracedListStyle: true
59+
DeriveLineEnding: true
60+
DerivePointerAlignment: false
61+
DisableFormat: false
62+
ExperimentalAutoDetectBinPacking: false
63+
FixNamespaceComments: true
64+
ForEachMacros:
65+
- foreach
66+
- Q_FOREACH
67+
- BOOST_FOREACH
68+
IncludeBlocks: Preserve
69+
IncludeCategories:
70+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
71+
Priority: 2
72+
SortPriority: 0
73+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
74+
Priority: 3
75+
SortPriority: 0
76+
- Regex: '.*'
77+
Priority: 1
78+
SortPriority: 0
79+
IncludeIsMainRegex: '(Test)?$'
80+
IncludeIsMainSourceRegex: ''
81+
IndentCaseLabels: false
82+
IndentGotoLabels: true
83+
IndentPPDirectives: None
84+
IndentWidth: 8
85+
IndentWrappedFunctionNames: false
86+
JavaScriptQuotes: Leave
87+
JavaScriptWrapImports: true
88+
KeepEmptyLinesAtTheStartOfBlocks: true
89+
MacroBlockBegin: ''
90+
MacroBlockEnd: ''
91+
MaxEmptyLinesToKeep: 3
92+
NamespaceIndentation: None
93+
ObjCBinPackProtocolList: Auto
94+
ObjCBlockIndentWidth: 2
95+
ObjCSpaceAfterProperty: false
96+
ObjCSpaceBeforeProtocolList: true
97+
PenaltyBreakAssignment: 2
98+
PenaltyBreakBeforeFirstCallParameter: 19
99+
PenaltyBreakComment: 300
100+
PenaltyBreakFirstLessLess: 120
101+
PenaltyBreakString: 1000
102+
PenaltyBreakTemplateDeclaration: 10
103+
PenaltyExcessCharacter: 1000000
104+
PenaltyReturnTypeOnItsOwnLine: 60
105+
PointerAlignment: Right
106+
ReflowComments: true
107+
SortIncludes: false
108+
SortUsingDeclarations: true
109+
SpaceAfterCStyleCast: false
110+
SpaceAfterLogicalNot: false
111+
SpaceAfterTemplateKeyword: true
112+
SpaceBeforeAssignmentOperators: true
113+
SpaceBeforeCpp11BracedList: true
114+
SpaceBeforeCtorInitializerColon: true
115+
SpaceBeforeInheritanceColon: true
116+
SpaceBeforeParens: ControlStatements
117+
SpaceBeforeRangeBasedForLoopColon: true
118+
SpaceInEmptyBlock: false
119+
SpaceInEmptyParentheses: false
120+
SpacesBeforeTrailingComments: 2
121+
SpacesInAngles: false
122+
SpacesInConditionalStatement: false
123+
SpacesInContainerLiterals: false
124+
SpacesInCStyleCastParentheses: false
125+
SpacesInParentheses: false
126+
SpacesInSquareBrackets: false
127+
SpaceBeforeSquareBrackets: false
128+
Standard: Latest
129+
StatementMacros:
130+
- Q_UNUSED
131+
- QT_REQUIRE_VERSION
132+
TabWidth: 8
133+
UseCRLF: false
134+
UseTab: Never
135+
...
136+

.formatignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Files to not check/fix coding style for.
2+
# These files are imported from other sources and we want to maintain
3+
# them in the original form to make future updates easier.
4+
docs/conf.py
5+
examples/protobuf/user_pb2.py
6+
tests/integration/schema_registry/data/proto/DependencyTestProto_pb2.py
7+
tests/integration/schema_registry/data/proto/NestedTestProto_pb2.py
8+
tests/integration/schema_registry/data/proto/PublicTestProto_pb2.py
9+
tests/integration/schema_registry/data/proto/SInt32Value_pb2.py
10+
tests/integration/schema_registry/data/proto/SInt64Value_pb2.py
11+
tests/integration/schema_registry/data/proto/TestProto_pb2.py
12+
tests/integration/schema_registry/data/proto/common_proto_pb2.py
13+
tests/integration/schema_registry/data/proto/exampleProtoCriteo_pb2.py
14+
tests/integration/schema_registry/data/proto/metadata_proto_pb2.py

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,19 @@ clean:
1212

1313
docs:
1414
$(MAKE) -C docs html
15+
16+
style-check:
17+
@(tools/style-format.sh \
18+
$$(git ls-tree -r --name-only HEAD | egrep '\.(c|h|py)$$') )
19+
20+
style-check-changed:
21+
@(tools/style-format.sh \
22+
$$( (git diff --name-only ; git diff --name-only --staged) | egrep '\.(c|h|py)$$'))
23+
24+
style-fix:
25+
@(tools/style-format.sh --fix \
26+
$$(git ls-tree -r --name-only HEAD | egrep '\.(c|h|py)$$'))
27+
28+
style-fix-changed:
29+
@(tools/style-format.sh --fix \
30+
$$( (git diff --name-only ; git diff --name-only --staged) | egrep '\.(c|h|py)$$'))

tools/style-format.sh

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/bin/bash
2+
#
3+
# Check or apply/fix the project coding style to all files passed as arguments.
4+
# Uses clang-format for C and flake8 for Python.
5+
#
6+
# Requires clang-format version 10 (apt install clang-format-10).
7+
#
8+
9+
10+
CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
11+
12+
set -e
13+
14+
ret=0
15+
16+
if [[ -z $1 ]]; then
17+
echo "Usage: $0 [--fix] srcfile1.c srcfile2.h srcfile3.c ..."
18+
echo ""
19+
exit 0
20+
fi
21+
22+
if [[ $1 == "--fix" ]]; then
23+
fix=1
24+
shift
25+
else
26+
fix=0
27+
fi
28+
29+
clang_format_version=$(${CLANG_FORMAT} --version | sed -Ee 's/.*version ([[:digit:]]+)\.[[:digit:]]+\.[[:digit:]]+.*/\1/')
30+
if [[ $clang_format_version != "10" ]] ; then
31+
echo "$0: clang-format version 10, '$clang_format_version' detected"
32+
exit 1
33+
fi
34+
35+
# Get list of files from .formatignore to ignore formatting for.
36+
ignore_files=( $(grep '^[^#]..' .formatignore) )
37+
38+
function ignore {
39+
local file=$1
40+
41+
local f
42+
for f in "${ignore_files[@]}" ; do
43+
[[ $file == $f ]] && return 0
44+
done
45+
46+
return 1
47+
}
48+
49+
extra_info=""
50+
51+
for f in $*; do
52+
53+
if ignore $f ; then
54+
echo "$f is ignored by .formatignore" 1>&2
55+
continue
56+
fi
57+
58+
lang="c"
59+
if [[ $f == *.py ]]; then
60+
lang="py"
61+
style="pep8"
62+
stylename="pep8"
63+
else
64+
style="file" # Use .clang-format
65+
stylename="C"
66+
fi
67+
68+
check=0
69+
70+
if [[ $fix == 1 ]]; then
71+
# Convert tabs to 8 spaces first.
72+
if grep -ql $'\t' "$f"; then
73+
sed -i -e 's/\t/ /g' "$f"
74+
echo "$f: tabs converted to spaces"
75+
fi
76+
77+
if [[ $lang == c ]]; then
78+
# Run clang-format to reformat the file
79+
${CLANG_FORMAT} --style="$style" "$f" > _styletmp
80+
81+
else
82+
# Run autopep8 to reformat the file.
83+
python3 -m autopep8 -a "$f" > _styletmp
84+
# autopep8 can't fix all errors, so we also perform a flake8 check.
85+
check=1
86+
fi
87+
88+
if ! cmp -s "$f" _styletmp; then
89+
echo "$f: style fixed ($stylename)"
90+
# Use cp to preserve target file mode/attrs.
91+
cp _styletmp "$f"
92+
rm _styletmp
93+
fi
94+
fi
95+
96+
if [[ $fix == 0 || $check == 1 ]]; then
97+
# Check for tabs
98+
if grep -q $'\t' "$f" ; then
99+
echo "$f: contains tabs: convert to 8 spaces instead"
100+
ret=1
101+
fi
102+
103+
# Check style
104+
if [[ $lang == c ]]; then
105+
if ! ${CLANG_FORMAT} --style="$style" --Werror --dry-run "$f" ; then
106+
echo "$f: had style errors ($stylename): see clang-format output above"
107+
ret=1
108+
fi
109+
elif [[ $lang == py ]]; then
110+
if ! python3 -m flake8 "$f"; then
111+
echo "$f: had style errors ($stylename): see flake8 output above"
112+
if [[ $fix == 1 ]]; then
113+
# autopep8 couldn't fix all errors. Let the user know.
114+
extra_info="Error: autopep8 could not fix all errors, fix the flake8 errors manually and run again."
115+
fi
116+
ret=1
117+
fi
118+
fi
119+
fi
120+
121+
done
122+
123+
rm -f _styletmp
124+
125+
if [[ $ret != 0 ]]; then
126+
echo ""
127+
echo "You can run the following command to automatically fix the style:"
128+
echo " $ make style-fix"
129+
[[ -n $extra_info ]] && echo "$extra_info"
130+
fi
131+
132+
exit $ret

0 commit comments

Comments
 (0)