Hey, I ran into this thread when looking for the PEP 7 compatible C code formatting configuration. I found that the CPython repo does not have it yet.
I made a .clang-format
configuration. I hope it is helpful.
.clang-format
---
# Reference: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
Language: Cpp
BasedOnStyle: Google
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignArrayOfStructures: Left
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: AcrossEmptyLines
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: Consecutive
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments:
Kind: Always
OverEmptyLines: 1
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowBreakBeforeNoexceptSpecifier: OnlyWithParen
AllowShortBlocksOnASingleLine: Never
AllowShortCaseExpressionOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: false
AllowShortCompoundRequirementOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakBeforeMultilineStrings: true
BinPackArguments: true
BinPackParameters: false
BitFieldColonSpacing: None
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterExternBlock: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAdjacentStringLiterals: true
BreakAfterAttributes: Leave
BreakAfterReturnType: AllDefinitions
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Custom
BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakFunctionDefinitionParameters: false
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
BreakTemplateDeclarations: Yes
ColumnLimit: 79
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: true
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^[<"]Python\.h[">]$'
Priority: 2
CaseSensitive: true
- Regex: '^<[[:alnum:]_/]+(\.h)?>$'
Priority: 1
- Regex: '^"cpython/.*\.h"$'
Priority: 3
CaseSensitive: true
- Regex: '^"pycore.*\.h"$'
Priority: 4
CaseSensitive: true
- Regex: '.*'
Priority: 5
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: true
IndentExternBlock: NoIndent
IndentGotoLabels: false
IndentPPDirectives: None
IndentRequiresClause: true
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertBraces: true
InsertNewlineAtEOF: true
InsertTrailingCommas: None
KeepEmptyLines:
AtEndOfFile: false
AtStartOfBlock: false
AtStartOfFile: false
LambdaBodyIndentation: Signature
LineEnding: DeriveLF
MacroBlockBegin: ''
MacroBlockEnd: ''
Macros:
- >-
PyObject_HEAD_INIT(type)={
/* this is not exactly match with PyObject_HEAD_INIT in Python source code
* but it is enough for clang-format */
{ 0xFFFFFFFF },
(type)
},
- >-
PyVarObject_HEAD_INIT(type, size)={
{
/* manually expand PyObject_HEAD_INIT(type) above
* because clang-format do not support recursive expansion */
{ 0xFFFFFFFF },
(type)
},
(size)
},
# Add indentation for aligning parameters when the definition spans multiple lines.
- PyAPI_FUNC(RTYPE)=extern RTYPE /* */
# Add a qualifier to properly indent right aligned pointer macro PyAPI_DATA(PyObject *)
# This is a workaround for clang-format and has no effect on the formatted code.
- PyAPI_DATA(RTYPE)=extern RTYPE const
- PyMODINIT_FUNC=PyObject *
MainIncludeChar: Quote
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
PackConstructorInitializers: NextLine
PenaltyBreakAssignment: 20
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakScopeResolution: 500
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 1000000000
PointerAlignment: Right
PPIndentWidth: -1
QualifierAlignment: Custom
QualifierOrder:
[friend, static, inline, const, constexpr, volatile, type, restrict]
ReferenceAlignment: Right
ReflowComments: false
RemoveBracesLLVM: false
RemoveParentheses: MultipleParentheses
RemoveSemicolon: true
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SkipMacroDefinitionBody: false
SortIncludes: CaseSensitive
SortUsingDeclarations: LexicographicNumeric
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeJsonColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: Never
SpacesInContainerLiterals: true
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParens: Never
SpacesInSquareBrackets: false
Standard: c++11
StatementAttributeLikeMacros:
# Defined in Include/pyport.h
- Py_DEPRECATED
- _Py_HOT_FUNCTION
- Py_ALWAYS_INLINE
- Py_NO_INLINE
- Py_ALIGNED
- Py_GCC_ATTRIBUTE
- _Py_NO_RETURN
- _Py_FALLTHROUGH
StatementMacros:
- PyObject_HEAD
- PyObject_VAR_HEAD
- PyException_HEAD
- _PyTZINFO_HEAD
- _PyDateTime_TIMEHEAD
- _PyDateTime_DATETIMEHEAD
- _PyGenObject_HEAD
- Window_NoArgNoReturnFunction
- Window_NoArgTrueFalseFunction
- Window_NoArgNoReturnVoidFunction
- Window_NoArg2TupleReturnFunction
- Window_OneArgNoReturnVoidFunction
- Window_OneArgNoReturnFunction
- Window_TwoArgNoReturnFunction
TableGenBreakInsideDAGArg: DontBreak
TabWidth: 4
TypeNames:
# Defined in Include/pytypedefs.h
- PyModuleDef
- PyModuleDef_Slot
- PyMethodDef
- PyGetSetDef
- PyMemberDef
- PyObject
- PyLongObject
- PyTypeObject
- PyCodeObject
- PyFrameObject
- PyThreadState
- PyInterpreterState
UseTab: Never
WhitespaceSensitiveMacros:
- _Py_XSTRINGIFY
- Py_STRINGIFY
...