diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000000..325bd6f544
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,98 @@
+---
+BasedOnStyle: WebKit
+Language: Cpp
+LineEnding: LF
+ColumnLimit: 0
+
+AlignAfterOpenBracket: DontAlign
+AlignConsecutiveAssignments: None
+AlignConsecutiveBitFields: None
+AlignConsecutiveDeclarations: None
+AlignConsecutiveMacros: Consecutive
+AlignTrailingComments: false
+
+AllowShortBlocksOnASingleLine: Empty
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortEnumsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortIfStatementsOnASingleLine: Never
+AllowShortLoopsOnASingleLine: false
+
+BinPackArguments: false
+BinPackParameters: false
+BitFieldColonSpacing: Both
+
+InsertBraces: true
+BreakBeforeBraces: Custom
+BraceWrapping:
+ AfterCaseLabel: false
+ AfterClass: false
+ AfterControlStatement: Never
+ AfterEnum: false
+ AfterFunction: true
+ AfterStruct: false
+ AfterUnion: false
+ BeforeElse: false
+ BeforeWhile: false
+ IndentBraces: false
+ SplitEmptyFunction: true
+ SplitEmptyRecord: false
+
+AlwaysBreakAfterReturnType: None
+BreakBeforeBinaryOperators: All
+BreakBeforeTernaryOperators: true
+BreakStringLiterals: true
+
+DerivePointerAlignment: false
+PointerAlignment: Right
+ReferenceAlignment: Right
+
+ContinuationIndentWidth: 4
+IndentCaseBlocks: false
+IndentCaseLabels: false
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth: 4
+
+SortIncludes: CaseSensitive
+IncludeBlocks: Regroup
+IncludeCategories:
+ - Regex: '((<.+>)|("nitro/.*\.h"))'
+ Priority: 1
+ - Regex: '"(consts|constants)/.*\.h"'
+ Priority: 2
+ - Regex: '"(struct_(decls|defs))/.*\.h"'
+ Priority: 3
+ - Regex: '"(constdata|res)/.*\.(h|naix)"'
+ Priority: 6
+ - Regex: '"[[:alnum:]_]+/.*\.(h|naix)"'
+ Priority: 4
+ - Regex: '"[[:alnum:]_]+\.(h|naix)"'
+ Priority: 5
+ - Regex: '.*'
+ Priority: 7
+
+InsertTrailingCommas: Wrapped
+InsertNewlineAtEOF: true
+KeepEmptyLinesAtEOF: false
+MaxEmptyLinesToKeep: 1
+RemoveParentheses: ReturnStatement
+RemoveSemicolon: true
+
+# QualifierAlignment: Custom
+# QualifierOrder: [static, inline, const, volatile, type]
+
+SpaceAfterCStyleCast: false
+SpaceAfterLogicalNot: false
+SpaceAroundPointerQualifiers: Default
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCaseColon: false
+SpaceBeforeParens: ControlStatements
+SpaceBeforeSquareBrackets: false
+SpaceInEmptyBlock: false
+SpaceInEmptyParentheses: false
+SpacesInContainerLiterals: true
+SpacesInLineCommentPrefix:
+ Minimum: 1
+ Maximum: -1
+SpacesInParens: Never
diff --git a/.clang-format-ignore b/.clang-format-ignore
new file mode 100644
index 0000000000..7ca5b76af5
--- /dev/null
+++ b/.clang-format-ignore
@@ -0,0 +1,23 @@
+# These files still contain inline-ASM functions; ignore them for formatting for now
+src/overlay117/ov117_02260668.c
+src/overlay112/ov112_0225C700.c
+src/overlay110/ov110_021D0D80.c
+src/overlay020/ov20_021D4728.c
+src/unk_0208A3F4.c
+src/unk_02089604.c
+src/overlay073/ov73_021D3250.c
+src/overlay107/ov107_02245EB0.c
+src/overlay107/ov107_02241AE0.c
+src/overlay007/ov7_02249960.c
+src/overlay071/ov71_0223C69C.c
+src/overlay019/ov19_021D0D80.c
+src/overlay022/ov22_02259C58.c
+src/overlay023/ov23_02253598.c
+src/overlay065/ov65_0222DCE0.c
+src/overlay064/ov64_0222DCE0.c
+src/overlay098/ov98_022471C8.c
+src/overlay062/ov62_02241204.c
+src/overlay062/ov62_0223DFA8.c
+src/overlay062/ov62_0223CAEC.c
+src/overlay062/ov62_02231690.c
+src/overlay062/ov62_02237D24.c
diff --git a/.clang-format-include b/.clang-format-include
new file mode 100644
index 0000000000..059779a480
--- /dev/null
+++ b/.clang-format-include
@@ -0,0 +1,3 @@
+include/**/*
+src/**/*
+
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 2c8c1cc4de..0328da85ce 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -3,18 +3,19 @@
This document provides a synopsis and loose guidelines for how to contribute to this project. It is a work in progress. Maintainers should expand this document.
## Contents
-- [Editor enhancements](#editor-enhancements)
+- [Editor Enhancements](#editor-enhancements)
+- [Code Formatting](#code-formatting)
-## Editor enhancements
+## Editor Enhancements
This repository includes a script to generate a `compile_commands.json` that is compatible with C language servers such as `clangd`.
### Requirements
-- python3.8 or newer
-- gcc-arm-none-eabi
-- clangd
+- `python3.8` or newer
+- `gcc-arm-none-eabi`
+- `clangd`
### Usage
@@ -23,3 +24,21 @@ This repository includes a script to generate a `compile_commands.json` that is
```
This will create a file named `compile_commands.json` in the project root, overwriting the previous copy.
+
+
+## Code Formatting
+
+This repository includes an opinionated `clang-format` specification which is integrated into the build system for convenience in ensuring that your code adheres to repository style guidelines.
+
+### Requirements
+
+- `clang-format`
+
+### Usage
+
+```bash
+./build.sh format
+```
+
+This will traverse the source tree and format all found C sources and headers according to the specified style rules.
+
diff --git a/format.sh b/format.sh
new file mode 100755
index 0000000000..e9e1bcb953
--- /dev/null
+++ b/format.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+set -e
+
+"${NINJA:-ninja}" -C build clang-format