From c7f1b6983174c19bc8429bd63bbaab54f0886561 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 2 Dec 2024 13:11:06 -0500 Subject: [PATCH 01/17] First pass on clang format file --- .clang-format | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..88d0ee46c --- /dev/null +++ b/.clang-format @@ -0,0 +1,26 @@ +--- +Language: Cpp +BasedOnStyle: LLVM + +IndentWidth: 4 # 4 spaces per indent +IndentAccessModifiers: false # Align access modifiers to braces +BreakBeforeBraces: Allman # Braces on their own lines +SpaceBeforeParens: SBPO_ControlStatementsExceptControlMacros +SpaceAfterCStyleCast: true +SeparateDefinitionBlocks: Always # Separate definitions (functions etc.) with an empty line +IncludeBlocks: Merge # Regroup includes based on config +IncludeCategories: + - Regex: '^"' # 'local' includes + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '/' # Library includes + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' # Everything else + Priority: 1 + SortPriority: 0 + CaseSensitive: false +... + From 967dee9c9b29a8f560dd06f1b15848b246e60ca6 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 2 Dec 2024 13:31:14 -0500 Subject: [PATCH 02/17] Fixed a few issues - Fixed an incorrect option name causing the formatting to not work - Increased column limit to 120 - Moved access modifiers back --- .clang-format | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.clang-format b/.clang-format index 88d0ee46c..21f6055d9 100644 --- a/.clang-format +++ b/.clang-format @@ -2,10 +2,12 @@ Language: Cpp BasedOnStyle: LLVM +ColumnLimit: 120 IndentWidth: 4 # 4 spaces per indent +AccessModifierOffset: -4 IndentAccessModifiers: false # Align access modifiers to braces BreakBeforeBraces: Allman # Braces on their own lines -SpaceBeforeParens: SBPO_ControlStatementsExceptControlMacros +SpaceBeforeParens: ControlStatementsExceptControlMacros SpaceAfterCStyleCast: true SeparateDefinitionBlocks: Always # Separate definitions (functions etc.) with an empty line IncludeBlocks: Merge # Regroup includes based on config From 3e9617311911ff43f84130d6f04e4849420ee0cd Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 2 Dec 2024 13:53:03 -0500 Subject: [PATCH 03/17] Put templates on their own lines --- .clang-format | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/.clang-format b/.clang-format index 21f6055d9..cbee12611 100644 --- a/.clang-format +++ b/.clang-format @@ -3,24 +3,25 @@ Language: Cpp BasedOnStyle: LLVM ColumnLimit: 120 -IndentWidth: 4 # 4 spaces per indent +IndentWidth: 4 # 4 spaces per indent AccessModifierOffset: -4 -IndentAccessModifiers: false # Align access modifiers to braces -BreakBeforeBraces: Allman # Braces on their own lines +IndentAccessModifiers: false # Align access modifiers to braces +BreakBeforeBraces: Allman # Braces on their own lines SpaceBeforeParens: ControlStatementsExceptControlMacros SpaceAfterCStyleCast: true -SeparateDefinitionBlocks: Always # Separate definitions (functions etc.) with an empty line -IncludeBlocks: Merge # Regroup includes based on config +SeparateDefinitionBlocks: Always # Separate definitions (functions etc.) with an empty line +BreakTemplateDeclarations: Always # Put template on their own lines +IncludeBlocks: Merge # Regroup includes based on config IncludeCategories: - - Regex: '^"' # 'local' includes + - Regex: '^"' # 'local' includes Priority: 2 SortPriority: 0 CaseSensitive: false - - Regex: '/' # Library includes + - Regex: '/' # Library includes Priority: 3 SortPriority: 0 CaseSensitive: false - - Regex: '.*' # Everything else + - Regex: '.*' # Everything else Priority: 1 SortPriority: 0 CaseSensitive: false From ea479e52e34375074d3a14b9f96171ad527506ee Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 2 Dec 2024 16:04:20 -0500 Subject: [PATCH 04/17] Organize .clang-format file into sections --- .clang-format | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/.clang-format b/.clang-format index cbee12611..befe9858e 100644 --- a/.clang-format +++ b/.clang-format @@ -2,15 +2,22 @@ Language: Cpp BasedOnStyle: LLVM -ColumnLimit: 120 +# Indentation IndentWidth: 4 # 4 spaces per indent AccessModifierOffset: -4 IndentAccessModifiers: false # Align access modifiers to braces + +# Newlines +ColumnLimit: 120 BreakBeforeBraces: Allman # Braces on their own lines +SeparateDefinitionBlocks: Always # Separate definitions (functions etc.) with an empty line +AlwaysBreakTemplateDeclarations: true # Put template on their own lines + +# Spaces SpaceBeforeParens: ControlStatementsExceptControlMacros SpaceAfterCStyleCast: true -SeparateDefinitionBlocks: Always # Separate definitions (functions etc.) with an empty line -BreakTemplateDeclarations: Always # Put template on their own lines + +# Includes IncludeBlocks: Merge # Regroup includes based on config IncludeCategories: - Regex: '^"' # 'local' includes From 34b76489ec3f3f716ae01f247c3d1d980d6036bc Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 2 Dec 2024 16:04:39 -0500 Subject: [PATCH 05/17] Indent namespace contents --- .clang-format | 1 + 1 file changed, 1 insertion(+) diff --git a/.clang-format b/.clang-format index befe9858e..9d5e61ab1 100644 --- a/.clang-format +++ b/.clang-format @@ -6,6 +6,7 @@ BasedOnStyle: LLVM IndentWidth: 4 # 4 spaces per indent AccessModifierOffset: -4 IndentAccessModifiers: false # Align access modifiers to braces +NamespaceIndentation: All # Indent namspace contents # Newlines ColumnLimit: 120 From 170725efae638c82475d85aa3abc76afac7455c9 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 2 Dec 2024 16:04:52 -0500 Subject: [PATCH 06/17] Properly regroup --- .clang-format | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.clang-format b/.clang-format index 9d5e61ab1..8b642ca1e 100644 --- a/.clang-format +++ b/.clang-format @@ -19,14 +19,14 @@ SpaceBeforeParens: ControlStatementsExceptControlMacros SpaceAfterCStyleCast: true # Includes -IncludeBlocks: Merge # Regroup includes based on config +IncludeBlocks: Regroup # Regroup includes based on config IncludeCategories: - - Regex: '^"' # 'local' includes - Priority: 2 + - Regex: '(^"|\.hpp)' # 'local' includes + Priority: 3 SortPriority: 0 CaseSensitive: false - - Regex: '/' # Library includes - Priority: 3 + - Regex: '\/' # Library includes + Priority: 2 SortPriority: 0 CaseSensitive: false - Regex: '.*' # Everything else From 8490fa6133f6a6cdb16a467f88a559312d0b28b6 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 2 Dec 2024 17:22:36 -0500 Subject: [PATCH 07/17] Indent only inner namespace contents this aligns more cloesly with the current codebase, and seems like a good idea since most of the code will be in the `GridKit` namespace --- .clang-format | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-format b/.clang-format index 8b642ca1e..6042d1ea2 100644 --- a/.clang-format +++ b/.clang-format @@ -6,7 +6,7 @@ BasedOnStyle: LLVM IndentWidth: 4 # 4 spaces per indent AccessModifierOffset: -4 IndentAccessModifiers: false # Align access modifiers to braces -NamespaceIndentation: All # Indent namspace contents +NamespaceIndentation: Inner # Indent namspace contents # Newlines ColumnLimit: 120 From e77fd7a988d89700f4efcf6e7c895c61751827b2 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 2 Dec 2024 17:24:09 -0500 Subject: [PATCH 08/17] More small formatting to align with current code - Short functions will never be on a single line - Don't binpack arguments/parameters - Align pointers and references to the left --- .clang-format | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.clang-format b/.clang-format index 6042d1ea2..4bd4aca0f 100644 --- a/.clang-format +++ b/.clang-format @@ -13,10 +13,17 @@ ColumnLimit: 120 BreakBeforeBraces: Allman # Braces on their own lines SeparateDefinitionBlocks: Always # Separate definitions (functions etc.) with an empty line AlwaysBreakTemplateDeclarations: true # Put template on their own lines +AllowShortBlocksOnASingleLine: Never +# On a newer version of clang-format, replace with BinPackArguments: OnePerLine +BinPackArguments: false # Don't allow multiple function arguments on the same line unless they all fit +BinPackParameters: false # Same but for parameters +PackConstructorInitializers: NextLine +AllowShortFunctionsOnASingleLine: None # Spaces SpaceBeforeParens: ControlStatementsExceptControlMacros SpaceAfterCStyleCast: true +PointerAlignment: Left # Includes IncludeBlocks: Regroup # Regroup includes based on config From 7befea5553d7784eef3a5df43630709b05165d7f Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 2 Dec 2024 17:25:26 -0500 Subject: [PATCH 09/17] Alignment --- .clang-format | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/.clang-format b/.clang-format index 4bd4aca0f..72c0613a1 100644 --- a/.clang-format +++ b/.clang-format @@ -8,6 +8,29 @@ AccessModifierOffset: -4 IndentAccessModifiers: false # Align access modifiers to braces NamespaceIndentation: Inner # Indent namspace contents +# Alignment +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: false + AcrossComments: true + AlignCompound: true + PadOperators: true +AlignConsecutiveBitFields: + Enabled: true + AcrossEmptyLines: false + AcrossComments: true +AlignConsecutiveDeclarations: + Enabled: true + AcrossEmptyLines: false + AcrossComments: true + # For future versions of clang-format + # AlignFunctionDeclarations: false + # AlignFunctionPointers: false +AlignConsecutiveMacros: + Enabled: true + AcrossEmptyLines: false + AcrossComments: true + # Newlines ColumnLimit: 120 BreakBeforeBraces: Allman # Braces on their own lines From 44176bac6312500a78a678353517f5298268bace Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Tue, 3 Dec 2024 10:24:04 -0500 Subject: [PATCH 10/17] Add clang-format as developer dependency --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 600516ea6..83248a0af 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,9 @@ You should have all of the following installed before installing GridKit™ - [CMake](https://cmake.org/) >= 3.12 - C++ 17 compliant compiler +## Developer Dependencies +- `clang-format`, which can be obtained through most linux distribution package managers, or through the [LLVM project](https://github.com/llvm/llvm-project) >= 15.0.0 + ### Installing GridKit™ uses CMake for build configuration. Per CMake best practices it is recommended From 1494e7db474b7d04107217c368c8f54166ea8ee3 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Tue, 3 Dec 2024 10:25:24 -0500 Subject: [PATCH 11/17] Fix readme heading --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 83248a0af..a2a14e36e 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ You should have all of the following installed before installing GridKit™ - [CMake](https://cmake.org/) >= 3.12 - C++ 17 compliant compiler -## Developer Dependencies +#### Developer Dependencies - `clang-format`, which can be obtained through most linux distribution package managers, or through the [LLVM project](https://github.com/llvm/llvm-project) >= 15.0.0 ### Installing From c2fdfb8ca471d75edf841ae9d2e2071cdc5b1cf8 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Sun, 9 Feb 2025 12:19:46 -0500 Subject: [PATCH 12/17] Better comment handling - Comments effectively have an extra 80 character column limit from the rest of code (bringing it up to 200 characters) - Comments which are not meant to be written/read in-editor (such as in-documentation Latex or Matlab code, meant to be rendered by other software or copied/pasted directly from/to other software) can start with ** to indicate that they should not be formatted in any way, and clang-format will skip those lines. --- .clang-format | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.clang-format b/.clang-format index 72c0613a1..0ccdfdf36 100644 --- a/.clang-format +++ b/.clang-format @@ -8,6 +8,14 @@ AccessModifierOffset: -4 IndentAccessModifiers: false # Align access modifiers to braces NamespaceIndentation: Inner # Indent namspace contents +# Comments get a little more leeway to go over the column limit (about 100 characters) +PenaltyBreakComment: 80000 +PenaltyExcessCharacter: 1000 + +# Comments which match this regex will be unformatted (and therefore can be longer or have more whitespace than other comments) +CommentPragmas: '^\*\*' + + # Alignment AlignConsecutiveAssignments: Enabled: true From b4e317267e6fe72bf32834c3266dcecf30ca79cb Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Fri, 14 Feb 2025 14:56:11 -0500 Subject: [PATCH 13/17] Move binary operators in broken lines to the next line --- .clang-format | 1 + 1 file changed, 1 insertion(+) diff --git a/.clang-format b/.clang-format index 0ccdfdf36..19ae7b971 100644 --- a/.clang-format +++ b/.clang-format @@ -50,6 +50,7 @@ BinPackArguments: false # Don't allow multiple function arguments on t BinPackParameters: false # Same but for parameters PackConstructorInitializers: NextLine AllowShortFunctionsOnASingleLine: None +BreakBeforeBinaryOperators: NonAssignment # Put binary operators after a line break, rather than before # Spaces SpaceBeforeParens: ControlStatementsExceptControlMacros From 03259329fc78f99a16fea1e8b78862dac3e70b67 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Fri, 14 Feb 2025 14:59:24 -0500 Subject: [PATCH 14/17] Remove extra comment columns --- .clang-format | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.clang-format b/.clang-format index 19ae7b971..1f8fb5be2 100644 --- a/.clang-format +++ b/.clang-format @@ -8,10 +8,6 @@ AccessModifierOffset: -4 IndentAccessModifiers: false # Align access modifiers to braces NamespaceIndentation: Inner # Indent namspace contents -# Comments get a little more leeway to go over the column limit (about 100 characters) -PenaltyBreakComment: 80000 -PenaltyExcessCharacter: 1000 - # Comments which match this regex will be unformatted (and therefore can be longer or have more whitespace than other comments) CommentPragmas: '^\*\*' From 96dcf87425152c79678099cb4839d13b7b889d59 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Fri, 14 Feb 2025 15:28:36 -0500 Subject: [PATCH 15/17] Remove empty lines --- .clang-format | 2 -- 1 file changed, 2 deletions(-) diff --git a/.clang-format b/.clang-format index 1f8fb5be2..7c75ea0f5 100644 --- a/.clang-format +++ b/.clang-format @@ -68,5 +68,3 @@ IncludeCategories: Priority: 1 SortPriority: 0 CaseSensitive: false -... - From 91f48ec88a85b0e0c5833455d5e942018d9e0c13 Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 17 Feb 2025 13:02:25 -0500 Subject: [PATCH 16/17] Add auto-formatting section to CONTRIBUTING.md --- CONTRIBUTING.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6f6916d15..2b0436e00 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -321,4 +321,10 @@ class Matrix // No, class is outside GridKit namespace { // matrix code }; +``` + +## Developer tooling + +### Auto-formatting +To help enforce the above code style guidelines, we use autoformatting provided by [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html), which is distributed as a part of `clang` and `llvm`. Pull-requests must be formatted using the version of `clang-format` distributed by the version of `llvm` required by development with Enzyme. \ No newline at end of file From 74a821870c03923c9c1a2d55359ef4fdc1f1c8ba Mon Sep 17 00:00:00 2001 From: Alexander Novotny Date: Mon, 17 Feb 2025 13:13:12 -0500 Subject: [PATCH 17/17] Added usage details for auto-formatting --- CONTRIBUTING.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2b0436e00..a62f660c6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -327,4 +327,8 @@ class Matrix // No, class is outside GridKit namespace ### Auto-formatting -To help enforce the above code style guidelines, we use autoformatting provided by [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html), which is distributed as a part of `clang` and `llvm`. Pull-requests must be formatted using the version of `clang-format` distributed by the version of `llvm` required by development with Enzyme. \ No newline at end of file +To help enforce the above code style guidelines, we use autoformatting provided by [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html), which is distributed as a part of `clang` and `llvm`. Pull-requests must be formatted using the version of `clang-format` distributed by the version of `llvm` required by development with Enzyme. + +VS Code users can use `clang-format` to automatically format their code by installing the [C/C++ Extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) and using the `Format Document` command in the command palette with the file to be formatted open. As well, you can enabled the `editor.formatOnSave` option to auto-format any file when you save it. + +Other users can use the [`git clang-format`](https://clang.llvm.org/docs/ClangFormat.html#git-integration) subcommand installed along with `clang-format` to auto-format all staged files. \ No newline at end of file