Skip to content

Commit 0a6b917

Browse files
WavyEbuilderstephensmalley
authored andcommitted
treewide: add .clang-format configuration file
Add the .clang-format configuration file, taken from the Linux kernel repository. We don't have any official style guidelines in tree at present, which makes it a bit unclear how to format C code for new contributors. As well as this, different parts of the codebase seem to been formatted with different styles on occasion, so using an automatic formatter should resolve this. As well as this, replace all the existing indent targets with a single toplevel `format` target. Managing all the source files to be formatted is not pretty to maintain, and doesn't really give us much. Also define a toplevel `check-format` target to verify that all code is formatted properly. This only becomes useful in the future once we have reformatted all existing code, but is wired up for now. For the future: 1. Reformat all existing code. I understand this is a big change, but we may as well get all code formatted if we go down this route; afterall, it's not like this will cause any breaking changes. 2. Add a CI target to check that all code is formatted as per the new clang-format configuration. The `check-format` target can be used for this. Signed-off-by: Rahul Sandhu <[email protected]> Acked-by: Stephen Smalley <[email protected]>
1 parent 5ada42e commit 0a6b917

File tree

46 files changed

+148
-143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+148
-143
lines changed

.clang-format

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
#
3+
# clang-format configuration file. Intended for clang-format >= 11.
4+
#
5+
# For more information, see:
6+
#
7+
# Documentation/dev-tools/clang-format.rst
8+
# https://clang.llvm.org/docs/ClangFormat.html
9+
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
10+
#
11+
---
12+
AccessModifierOffset: -4
13+
AlignAfterOpenBracket: Align
14+
AlignConsecutiveAssignments: false
15+
AlignConsecutiveDeclarations: false
16+
AlignEscapedNewlines: Left
17+
AlignOperands: true
18+
AlignTrailingComments: false
19+
AllowAllParametersOfDeclarationOnNextLine: false
20+
AllowShortBlocksOnASingleLine: false
21+
AllowShortCaseLabelsOnASingleLine: false
22+
AllowShortFunctionsOnASingleLine: None
23+
AllowShortIfStatementsOnASingleLine: false
24+
AllowShortLoopsOnASingleLine: false
25+
AlwaysBreakAfterDefinitionReturnType: None
26+
AlwaysBreakAfterReturnType: None
27+
AlwaysBreakBeforeMultilineStrings: false
28+
AlwaysBreakTemplateDeclarations: false
29+
BinPackArguments: true
30+
BinPackParameters: true
31+
BraceWrapping:
32+
AfterClass: false
33+
AfterControlStatement: false
34+
AfterEnum: false
35+
AfterFunction: true
36+
AfterNamespace: true
37+
AfterObjCDeclaration: false
38+
AfterStruct: false
39+
AfterUnion: false
40+
AfterExternBlock: false
41+
BeforeCatch: false
42+
BeforeElse: false
43+
IndentBraces: false
44+
SplitEmptyFunction: true
45+
SplitEmptyRecord: true
46+
SplitEmptyNamespace: true
47+
BreakBeforeBinaryOperators: None
48+
BreakBeforeBraces: Custom
49+
BreakBeforeInheritanceComma: false
50+
BreakBeforeTernaryOperators: false
51+
BreakConstructorInitializersBeforeComma: false
52+
BreakConstructorInitializers: BeforeComma
53+
BreakAfterJavaFieldAnnotations: false
54+
BreakStringLiterals: false
55+
ColumnLimit: 80
56+
CommentPragmas: '^ IWYU pragma:'
57+
CompactNamespaces: false
58+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
59+
ConstructorInitializerIndentWidth: 8
60+
ContinuationIndentWidth: 8
61+
Cpp11BracedListStyle: false
62+
DerivePointerAlignment: false
63+
DisableFormat: false
64+
ExperimentalAutoDetectBinPacking: false
65+
FixNamespaceComments: false
66+
67+
# Taken from:
68+
# git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' \
69+
# | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$, - '\1'," \
70+
# | LC_ALL=C sort -u
71+
ForEachMacros:
72+
- 'cil_list_for_each'
73+
- 'cil_stack_for_each'
74+
- 'cil_stack_for_each_starting_at'
75+
- 'ebitmap_for_each_bit'
76+
- 'ebitmap_for_each_positive_bit'
77+
78+
IncludeBlocks: Preserve
79+
IncludeCategories:
80+
- Regex: '.*'
81+
Priority: 1
82+
IncludeIsMainRegex: '(Test)?$'
83+
IndentCaseLabels: false
84+
IndentGotoLabels: false
85+
IndentPPDirectives: None
86+
IndentWidth: 8
87+
IndentWrappedFunctionNames: false
88+
JavaScriptQuotes: Leave
89+
JavaScriptWrapImports: true
90+
KeepEmptyLinesAtTheStartOfBlocks: false
91+
MacroBlockBegin: ''
92+
MacroBlockEnd: ''
93+
MaxEmptyLinesToKeep: 1
94+
NamespaceIndentation: None
95+
ObjCBinPackProtocolList: Auto
96+
ObjCBlockIndentWidth: 8
97+
ObjCSpaceAfterProperty: true
98+
ObjCSpaceBeforeProtocolList: true
99+
100+
# Taken from git's rules
101+
PenaltyBreakAssignment: 10
102+
PenaltyBreakBeforeFirstCallParameter: 30
103+
PenaltyBreakComment: 10
104+
PenaltyBreakFirstLessLess: 0
105+
PenaltyBreakString: 10
106+
PenaltyExcessCharacter: 100
107+
PenaltyReturnTypeOnItsOwnLine: 60
108+
109+
PointerAlignment: Right
110+
ReflowComments: false
111+
SortIncludes: false
112+
SortUsingDeclarations: false
113+
SpaceAfterCStyleCast: false
114+
SpaceAfterTemplateKeyword: true
115+
SpaceBeforeAssignmentOperators: true
116+
SpaceBeforeCtorInitializerColon: true
117+
SpaceBeforeInheritanceColon: true
118+
SpaceBeforeParens: ControlStatementsExceptForEachMacros
119+
SpaceBeforeRangeBasedForLoopColon: true
120+
SpaceInEmptyParentheses: false
121+
SpacesBeforeTrailingComments: 1
122+
SpacesInAngles: false
123+
SpacesInContainerLiterals: false
124+
SpacesInCStyleCastParentheses: false
125+
SpacesInParentheses: false
126+
SpacesInSquareBrackets: false
127+
Standard: Cpp03
128+
TabWidth: 8
129+
UseTab: Always
130+
...

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ When preparing patches, please follow these guidelines:
5353
- Separate large patches into logical patches
5454
- Patch descriptions must end with your "Signed-off-by" line. This means your
5555
code meets the Developer's certificate of origin, see below.
56+
- C code should be formatted using clang-format, using the .clang-format
57+
configuration file at the root of this repository.
5658

5759
When adding new, large features or tools it is best to discuss the
5860
design on the mailing list prior to submitting the patch.

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ifneq ($(DESTDIR),)
3636
export LIBSEPOLA
3737
endif
3838

39-
all install relabel clean test indent:
39+
all install relabel clean test:
4040
@for subdir in $(SUBDIRS); do \
4141
(cd $$subdir && $(MAKE) $@) || exit 1; \
4242
done
@@ -50,3 +50,12 @@ distclean:
5050
@for subdir in $(DISTCLEANSUBDIRS); do \
5151
(cd $$subdir && $(MAKE) $@) || exit 1; \
5252
done
53+
54+
# We shouldn't have any unformatted files in the repo without an explicit exception.
55+
FORMAT_SOURCE_FILES := $(shell find $(SUBDIRS) -type f \( -name '*.c' -o -name '*.h' \))
56+
57+
format:
58+
clang-format -i $(FORMAT_SOURCE_FILES)
59+
60+
check-format:
61+
clang-format --dry-run -Werror $(FORMAT_SOURCE_FILES)

checkpolicy/Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,3 @@ relabel: install
7777
clean:
7878
-rm -f $(TARGETS) $(CHECKPOLOBJS) $(CHECKMODOBJS) y.tab.c y.tab.h lex.yy.c tests/testpol.conf tests/testpol.bin
7979
$(MAKE) -C test clean
80-
81-
indent:
82-
../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))

gui/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ install: all
6060
clean:
6161
(cd po && $(MAKE) $@)
6262

63-
indent:
64-
6563
relabel:
6664

6765
test:

libselinux/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ COMPILER := clang
5050
endif
5151
export COMPILER
5252

53-
all install relabel clean distclean indent:
53+
all install relabel clean distclean:
5454
@for subdir in $(SUBDIRS); do \
5555
(cd $$subdir && $(MAKE) $@) || exit 1; \
5656
done

libselinux/include/Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ install: all
1010

1111
relabel:
1212

13-
indent:
14-
../../scripts/Lindent $(wildcard selinux/*.h)
15-
1613
distclean clean:
1714
-rm -f selinux/*~
18-

libselinux/man/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ install: all
3535

3636
relabel:
3737

38-
indent distclean clean:
38+
format distclean clean:

libselinux/src/Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,4 @@ clean: clean-pywrap clean-rubywrap
223223
distclean: clean
224224
rm -f $(GENERATED) $(SWIGFILES)
225225

226-
indent:
227-
../../scripts/Lindent $(filter-out $(GENERATED),$(wildcard *.[ch]))
228-
229226
.PHONY: all clean clean-pywrap clean-rubywrap pywrap rubywrap swigify install install-pywrap install-rubywrap distclean

libselinux/utils/Makefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,4 @@ clean:
7272

7373
distclean: clean
7474

75-
indent:
76-
../../scripts/Lindent $(wildcard *.[ch])
77-
7875
relabel:
79-

0 commit comments

Comments
 (0)