[PATCH v1] Add .clang-format style file

Noah Goldstein goldstein.w.n@gmail.com
Wed Mar 30 01:33:46 GMT 2022


On Tue, Mar 29, 2022 at 7:27 PM Fangrui Song <maskray@google.com> wrote:
>
> On 2022-03-28, Noah Goldstein via Libc-alpha wrote:
> >Went with version >= 9.0 since it covers most of the major features
> >and should be pretty universally accessibly.
>
> Why not 10.0?  clang-format 10.0.0 has been there for two years. Using
> it will remove some clang-format >= 10.0 below. Related:
> Linux kernel now requires Clang >= 11.0.
> Since this patch introduces new tool, we can require higher versions.
> Installing new clang-format isn't so difficult for old systems. There
> are prebuilt packages even if the distro stops upgrading them.

Fair enough. If linux is on 11.0+ that seems to be enough of a precedent.

Will post v2 with 11.0+ tomorrow so if anyone has a reason to stick with
9.0 they can comment.

>
> >There are some issues, particularly indention of preprocessor
> >directives. Unfortunately there doesn't appear to be a switch for
> >a seperate 'IndentWidth' for preprocessor directives vs. normal
> >code so we are stuck either not indenting the directives or
> >over-indenting them.
>
> https://github.com/llvm/llvm-project/issues/42264
> "Feature request: Two different indentation width options, one for code, one for pre-processor directives" :(
>
> >Chose to over-indent as it generally seems easier
> >to script halving all pre-processor indentations than counting the
> >nested depth and indenting from scratch.
>
> Yeah, I picked over-indent  for compiler-rt sanitizers as well
> https://reviews.llvm.org/D100238
>
> >Other than the pre-processor directives most of the changes it makes
> >are cleaning up inconsistencies that already existed.
> >---
> > .clang-format | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 164 insertions(+)
> > create mode 100644 .clang-format
> >
> >diff --git a/.clang-format b/.clang-format
> >new file mode 100644
> >index 0000000000..121b475dbd
> >--- /dev/null
> >+++ b/.clang-format
> >@@ -0,0 +1,164 @@
> >+#  clang-format file for GLIBC
> >+#  Copyright (C) 2022 Free Software Foundation, Inc.
> >+#  This file is part of the GNU C Library.
> >+#
> >+#  The GNU C Library is free software; you can redistribute it and/or
> >+#  modify it under the terms of the GNU Lesser General Public
> >+#  License as published by the Free Software Foundation; either
> >+#  version 2.1 of the License, or (at your option) any later version.
> >+#
> >+#  The GNU C Library 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
> >+#  Lesser General Public License for more details.
> >+#
> >+#  You should have received a copy of the GNU Lesser General Public
> >+#  License along with the GNU C Library; if not, see
> >+#  <https://www.gnu.org/licenses/>.
> >+#
> >+#  Requires clang-format version >= 9.0
> >+#
> >+#  For more information, see:
> >+#
> >+#   Documentation/process/clang-format.rst
> >+#   https://clang.llvm.org/docs/ClangFormat.html
> >+#   https://clang.llvm.org/docs/ClangFormatStyleOptions.html
> >+#
> >+#  There are some known cases that this doesn't produce the desired
> >+#  style (i.e Preprocessor Directives are over-indented and not
> >+#  auto-commented). As a result, this is meant to be a utility to make
> >+#  formatting easier, not a definitive standard.
> >+---
> >+# BasedOnStyle:  GNU
> >+AccessModifierOffset: -2
> >+AlignAfterOpenBracket: Align
> >+AlignConsecutiveMacros: false
> >+AlignConsecutiveAssignments: false
> >+#AlignConsecutiveBitFields: false # clang-format >= 11.0
> >+AlignConsecutiveDeclarations: false
> >+AlignEscapedNewlines: Right
> >+AlignOperands:   true
> >+AlignTrailingComments: true
> >+AllowAllArgumentsOnNextLine: true
> >+AllowAllConstructorInitializersOnNextLine: true
> >+AllowAllParametersOfDeclarationOnNextLine: true
> >+#AllowShortEnumsOnASingleLine: true # clang-format >= 11.0
> >+AllowShortBlocksOnASingleLine: false
> >+AllowShortCaseLabelsOnASingleLine: false
> >+AllowShortFunctionsOnASingleLine: All
> >+AllowShortLambdasOnASingleLine: All
> >+AllowShortIfStatementsOnASingleLine: Never
> >+AllowShortLoopsOnASingleLine: false
> >+AlwaysBreakAfterDefinitionReturnType: All
> >+AlwaysBreakAfterReturnType: AllDefinitions
> >+AlwaysBreakBeforeMultilineStrings: false
> >+AlwaysBreakTemplateDeclarations: MultiLine
> >+BinPackArguments: true
> >+BinPackParameters: true
> >+BraceWrapping:
> >+  AfterCaseLabel:  true
> >+  AfterClass:      true
> >+  AfterControlStatement: true
> >+  AfterEnum:       true
> >+  AfterFunction:   true
> >+  AfterNamespace:  true
> >+  AfterStruct:     true
> >+  AfterUnion:      true
> >+  AfterExternBlock: true
> >+  BeforeCatch:     true
> >+  BeforeElse:      true
> >+  #BeforeWhile:     true # clang-format >= 11.0
> >+  IndentBraces:    true
> >+  SplitEmptyFunction: true
> >+  SplitEmptyRecord: true
> >+  SplitEmptyNamespace: true
> >+BreakBeforeBinaryOperators: All
> >+BreakBeforeBraces: GNU
> >+BreakBeforeInheritanceComma: false
> >+BreakInheritanceList: BeforeColon
> >+BreakBeforeTernaryOperators: true
> >+BreakConstructorInitializersBeforeComma: false
> >+BreakConstructorInitializers: BeforeColon
> >+BreakStringLiterals: true
> >+ColumnLimit:     79
> >+CommentPragmas:  '^ IWYU pragma:'
> >+CompactNamespaces: false
> >+ConstructorInitializerAllOnOneLineOrOnePerLine: false
> >+ConstructorInitializerIndentWidth: 4
> >+ContinuationIndentWidth: 4
> >+Cpp11BracedListStyle: false
> >+#DeriveLineEnding: true # clang-format >= 10.0
> >+DerivePointerAlignment: false
> >+DisableFormat:   false
> >+ExperimentalAutoDetectBinPacking: false
> >+FixNamespaceComments: false
> >+ForEachMacros:
> >+  - foreach
> >+  - Q_FOREACH
> >+  - BOOST_FOREACH
> >+IncludeBlocks:   Preserve
> >+IncludeCategories:
> >+  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
> >+    Priority:        2
> >+  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
> >+    Priority:        3
> >+  - Regex:           '.*'
> >+    Priority:        1
> >+IncludeIsMainRegex: '(Test)?$'
> >+IndentCaseLabels: false
> >+#IndentCaseBlocks: false # clang-format >= 11.0
> >+#IndentGotoLabels: true # clang-format >= 10.0
> >+IndentWidth:     2
> >+IndentPPDirectives: AfterHash
> >+#IndentExternBlock: AfterExternBlock # clang-format >= 11.0
> >+IndentWrappedFunctionNames: false
> >+#InsertTrailingCommas: None # clang-format >= 11.0
> >+KeepEmptyLinesAtTheStartOfBlocks: true
> >+MacroBlockBegin: ''
> >+MacroBlockEnd:   ''
> >+MaxEmptyLinesToKeep: 1
> >+NamespaceIndentation: None
> >+PenaltyBreakAssignment: 2
> >+PenaltyBreakBeforeFirstCallParameter: 19
> >+PenaltyBreakComment: 300
> >+PenaltyBreakFirstLessLess: 120
> >+PenaltyBreakString: 1000
> >+PenaltyBreakTemplateDeclaration: 10
> >+PenaltyExcessCharacter: 1000000
> >+PenaltyReturnTypeOnItsOwnLine: 60
> >+PointerAlignment: Right
> >+ReflowComments:  true
> >+SortIncludes:    false
> >+SortUsingDeclarations: true
> >+SpaceAfterCStyleCast: true
> >+SpaceAfterLogicalNot: false
> >+SpaceAfterTemplateKeyword: true
> >+SpaceBeforeAssignmentOperators: true
> >+SpaceBeforeCpp11BracedList: false
> >+SpaceBeforeCtorInitializerColon: true
> >+SpaceBeforeInheritanceColon: true
> >+SpaceBeforeParens: Always
> >+SpaceBeforeRangeBasedForLoopColon: true
> >+#SpaceInEmptyBlock: false # clang-format >= 10.0
> >+SpaceInEmptyParentheses: false
> >+SpacesBeforeTrailingComments: 1
> >+SpacesInAngles:  false
> >+#SpacesInConditionalStatement: false # clang-format >= 10.0
> >+SpacesInContainerLiterals: true
> >+SpacesInCStyleCastParentheses: false
> >+SpacesInParentheses: false
> >+SpacesInSquareBrackets: false
> >+#SpaceBeforeSquareBrackets: false # clang-format >= 10.0
> >+Standard:        Cpp03
> >+StatementMacros:
> >+  - Q_UNUSED
> >+  - QT_REQUIRE_VERSION
> >+TabWidth:        8
> >+UseTab:          Never
> >+ForEachMacros:
> >+  - 'FOR_EACH_IMPL'
> >+  - 'list_for_each'
> >+  - 'list_for_each_prev'
> >+  - 'list_for_each_prev_safe'
> >+...
> >+
> >--
> >2.25.1
> >


More information about the Libc-alpha mailing list