Skip to content

Use clang-format to consistently format the codebase #5143

Open
@guhetier

Description

@guhetier

Describe the feature you'd like supported

We should use clang-format to format the codebase to easily keep a consistent style in the codebase and in contributions.

Proposed solution

  • Create a .clang-format matching the style of the existing codebase as closely as possible
    • We should target less than 10% of line changed
  • Update the CI on PRs to check that the format is correct (output of clang-format is empty)

Additional context

Here is a base clang-format file that can serve as a base and be modified

---
Language: Cpp
# BasedOnStyle: LLVM

AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: false
AlignEscapedNewlines: DontAlign
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
  AfterCaseLabel: true
  AfterClass: true
  AfterControlStatement: true
  AfterEnum: true
  AfterFunction: true
  AfterNamespace: true
  AfterStruct: true
  AfterUnion: true
  AfterExternBlock: true
  BeforeCatch: true
  BeforeElse: true
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: AfterColon
BreakStringLiterals: false
ColumnLimit: 130
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
FixNamespaceComments: true
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeBlocks: Regroup
IndentCaseLabels: false
IncludeCategories:
  - Regex: '^"(stdafx.h|pch.h|precomp.h)"$'
    Priority: -1
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: '^BEGIN_COM_MAP$|^BEGIN_CONNECTION_POINT_MAP$|^BEGIN_HELPER_NODEMAP$|^BEGIN_MODULE$|^BEGIN_MSG_MAP$|^BEGIN_OBJECT_MAP$|^BEGIN_TEST_CLASS$|^BEGIN_TEST_METHOD$|^BEGIN_TEST_METHOD_PROPERTIES$'
MacroBlockEnd: '^END_COM_MAP$|^END_CONNECTION_POINT_MAP$|^END_HELPER_NODEMAP$|^END_MODULE$|^END_MSG_MAP$|^END_OBJECT_MAP$|^END_TEST_CLASS$|^END_TEST_METHOD$|^END_TEST_METHOD_PROPERTIES$'
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 2000
PenaltyExcessCharacter: 2
PenaltyReturnTypeOnItsOwnLine: 1000
PointerAlignment: Left
SortIncludes: false
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Latest
TabWidth: 4
UseTab: Never

AttributeMacros: [
  CALLBACK,
]

StatementMacros: [
  _Acquires_exclusive_lock_,
  _Acquires_lock_,
  _Acquires_nonreentrant_lock_,
  _Acquires_shared_lock_,
  _Analysis_assume_smart_lock_acquired_,
  _Analysis_assume_smart_lock_released_,
  _Create_lock_level_,
  _Detaches_lock_,
  _Function_class_,
  _Global_cancel_spin_lock_,
  _Global_critical_region_,
  _Global_interlock_,
  _Global_priority_region_,
  _Has_lock_kind_,
  _Has_lock_level_,
  _IRQL_always_function_max_,
  _IRQL_always_function_min_,
  _IRQL_raises_,
  _IRQL_requires_,
  _IRQL_requires_max_,
  _IRQL_requires_min_,
  _IRQL_requires_same_,
  _IRQL_restores_,
  _IRQL_restores_global_,
  _IRQL_saves_,
  _IRQL_saves_global_,
  _Lock_level_order_,
  _Moves_lock_,
  _Must_inspect_result_,
  _No_competing_thread_,
  _Post_same_lock_,
  _Post_writable_byte_size_,
  _Pre_satisfies_,
  _Releases_exclusive_lock_,
  _Releases_lock_,
  _Releases_nonreentrant_lock_,
  _Releases_shared_lock_,
  _Replaces_lock_,
  _Requires_exclusive_lock_held_,
  _Requires_lock_held_,
  _Requires_lock_not_held_,
  _Requires_no_locks_held_,
  _Requires_shared_lock_held_,
  _Ret_maybenull_,
  _Ret_range_,
  _Struct_size_bytes_,
  _Success_,
  _Swaps_locks_,
  _Use_decl_annotations_,
  _When_,

  DECLARE_ORDINAL_MAP,
  DECLARE_PROCNAME_MAP,
  DEFINE_ORDINAL_ENTRIES,
  DEFINE_ORDINAL_ENTRIES_ALTNAME,
  DEFINE_ORDINAL_ENTRIES_APISET,
  DEFINE_ORDINAL_MAP,
  DEFINE_PROCNAME_ENTRIES,
  DEFINE_PROCNAME_ENTRIES_ALTNAME,
  DEFINE_PROCNAME_ENTRIES_APISET,
  DEFINE_PROCNAME_MAP,
  DLOENTRY,
  DLOENTRY_APISET,
  DLPENTRY,
  DLPENTRY_APISET,

  RpcEndExcept,

  ActivatableClass,
  ActivatableClassWithFactory,
  ActivatableClassWithFactoryEx,
  ActivatableStaticOnlyFactory,
  ActivatableStaticOnlyFactoryEx,
  CoCreatableClass,
  CoCreatableClassWithFactory,
  CoCreatableClassWithFactoryEx,
  TEST_CLASS,
  TEST_METHOD  
]

TypenameMacros: [
  IFACEMETHOD,
  STDMETHOD,
  STDAPI_,
]
---
Language: CSharp
AlignAfterOpenBracket: AlwaysBreak
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BraceWrapping:
  AfterCaseLabel: true
  AfterClass: true
  AfterControlStatement: true
  AfterEnum: true
  AfterFunction: true
  AfterNamespace: true
  AfterStruct: true
  BeforeCatch: true
  BeforeElse: true
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
BreakBeforeBraces: Custom
ColumnLimit: 130
DerivePointerAlignment: false
IndentWidth: 4
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 2000
PenaltyExcessCharacter: 2
PenaltyReturnTypeOnItsOwnLine: 1000
PointerAlignment: Left
TabWidth: 4
UseTab: Never
...

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions