Skip to contentSkip to conversation

clang-format: add clang-format configuration file from Linux kernel

PATCH In review Thomas Haller 2 participants 3 messages Aug 23, 2023 – Aug 24, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clang-format: add clang-format configuration file from Linux kernel

Clang-format is useful for developing, even if the code base does not enforce an automated style. With this, I can select a function in the editor, and reformat that portion with clang-format. In particular, it can fix indentation and tabs.

The style of nftables is close to kernel style, so take the file from Linux v6.4 ([1]). There are no changes, except (manually) adjusting "ForEachMacros".

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/.clang-format?id=6995e2de6891c724bfeb2db33d7b87775f913ad1

---
 .clang-format | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 140 insertions(+)
 create mode 100644 .clang-format
.clang-format new file +140 -0
new file mode 100644
index 000000000000..a86479ce053a
@@ -0,0 +1,140 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# clang-format configuration file. Intended for clang-format >= 11.
+#
+# For more information, see:
+#
+#   Documentation/process/clang-format.rst
+#   https://clang.llvm.org/docs/ClangFormat.html
+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
+#
+---
+AccessModifierOffset: -4
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: false
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+  AfterClass: false
+  AfterControlStatement: false
+  AfterEnum: false
+  AfterFunction: true
+  AfterNamespace: true
+  AfterObjCDeclaration: false
+  AfterStruct: false
+  AfterUnion: false
+  AfterExternBlock: false
+  BeforeCatch: false
+  BeforeElse: false
+  IndentBraces: false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: false
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 8
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+DerivePointerAlignment: false
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+
+# Taken from:
+#   git grep -h '^#define [^[:space:]]*for_each[^[:space:]]*(' include/ tools/ \
+#   | sed "s,^#define \([^[:space:]]*for_each[^[:space:]]*\)(.*$,  - '\1'," \
+#   | LC_ALL=C sort -u
+ForEachMacros:
+  - 'hlist_for_each'
+  - 'hlist_for_each_entry'
+  - 'hlist_for_each_entry_continue'
+  - 'hlist_for_each_entry_from'
+  - 'hlist_for_each_entry_safe'
+  - 'hlist_for_each_safe'
+  - 'list_for_each_entry'
+  - 'list_for_each_entry_continue'
+  - 'list_for_each_entry_continue_reverse'
+  - 'list_for_each_entry_from'
+  - 'list_for_each_entry_reverse'
+  - 'list_for_each_entry_safe'
+  - 'list_for_each_entry_safe_continue'
+  - 'list_for_each_entry_safe_from'
+  - 'list_for_each_entry_safe_reverse'
+
+IncludeBlocks: Preserve
+IncludeCategories:
+  - Regex: '.*'
+    Priority: 1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 8
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 8
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: true
+
+# Taken from git's rules
+PenaltyBreakAssignment: 10
+PenaltyBreakBeforeFirstCallParameter: 30
+PenaltyBreakComment: 10
+PenaltyBreakFirstLessLess: 0
+PenaltyBreakString: 10
+PenaltyExcessCharacter: 100
+PenaltyReturnTypeOnItsOwnLine: 60
+
+PointerAlignment: Right
+ReflowComments: false
+SortIncludes: false
+SortUsingDeclarations: false
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatementsExceptForEachMacros
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: false
+SpacesInContainerLiterals: false
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Cpp03
+TabWidth: 8
+UseTab: Always
+...
Signature · 1 line
2.41.0
#1 lore raw

Hi Thomas,

Quoted 8 lines · Thomas Haller

Clang-format is useful for developing, even if the code base does not enforce an automated style. With this, I can select a function in the editor, and reformat that portion with clang-format. In particular, it can fix indentation and tabs.

The style of nftables is close to kernel style, so take the file from Linux v6.4 ([1]). There are no changes, except (manually) adjusting "ForEachMacros".

I'd prefer you keep this locally by now.

If more developers are relying on this feature, we can revisit later.

#2 lore raw
Quoted 11 lines · Pablo Neira Ayuso

Hi Thomas,

Quoted 8 lines · Thomas Haller

Clang-format is useful for developing, even if the code base does not
enforce an automated style. With this, I can select a function in the
editor, and reformat that portion with clang-format. In particular, it
can fix indentation and tabs.

Quoted 9 lines
Quoted 4 lines

The style of nftables is close to kernel style, so take the file from
Linux v6.4 ([1]). There are no changes, except (manually) adjusting "ForEachMacros".

I'd prefer you keep this locally by now.

If more developers are relying on this feature, we can revisit later.

Hi Pablo,

OK.

If anybody is interested, I solved this on my end, by having nftables checked out in a subdirectory like

nftables

  ├── .clang-format
  └── nftables
      ├── aclocal.m4
      ├── ...

And for the record, these were the patches that introduced the file to kernel and the documentation in kernel:

- [PATCH] clang-format: add configuration file
https://lore.kernel.org/lkml/[email protected]/

- [PATCH v2] clang-format: add configuration file
https://lore.kernel.org/lkml/[email protected]/

- Linux Documentation/process
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/Documentation/process/clang-format.rst?id=v6.4

Thanks,
Thomas