Skip to content

Commit 8827fb3

Browse files
committed
Adding a script to format source code and a github action to check
1 parent 71577ff commit 8827fb3

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed

.clang-format

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: -2
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveMacros: false
7+
AlignConsecutiveAssignments: false
8+
AlignConsecutiveDeclarations: false
9+
AlignEscapedNewlines: Right
10+
AlignOperands: true
11+
AlignTrailingComments: true
12+
AllowAllArgumentsOnNextLine: true
13+
AllowAllConstructorInitializersOnNextLine: true
14+
AllowAllParametersOfDeclarationOnNextLine: true
15+
AllowShortBlocksOnASingleLine: Never
16+
AllowShortCaseLabelsOnASingleLine: false
17+
AllowShortFunctionsOnASingleLine: All
18+
AllowShortLambdasOnASingleLine: All
19+
AllowShortIfStatementsOnASingleLine: Never
20+
AllowShortLoopsOnASingleLine: false
21+
AlwaysBreakAfterDefinitionReturnType: None
22+
AlwaysBreakAfterReturnType: None
23+
AlwaysBreakBeforeMultilineStrings: false
24+
AlwaysBreakTemplateDeclarations: MultiLine
25+
BinPackArguments: true
26+
BinPackParameters: true
27+
BraceWrapping:
28+
AfterCaseLabel: false
29+
AfterClass: false
30+
AfterControlStatement: false
31+
AfterEnum: false
32+
AfterFunction: false
33+
AfterNamespace: false
34+
AfterObjCDeclaration: false
35+
AfterStruct: false
36+
AfterUnion: false
37+
AfterExternBlock: false
38+
BeforeCatch: false
39+
BeforeElse: false
40+
IndentBraces: false
41+
SplitEmptyFunction: true
42+
SplitEmptyRecord: true
43+
SplitEmptyNamespace: true
44+
BreakBeforeBinaryOperators: All
45+
BreakBeforeBraces: Attach
46+
BreakBeforeInheritanceComma: false
47+
BreakInheritanceList: BeforeColon
48+
BreakBeforeTernaryOperators: true
49+
BreakConstructorInitializersBeforeComma: false
50+
BreakConstructorInitializers: BeforeColon
51+
BreakAfterJavaFieldAnnotations: false
52+
BreakStringLiterals: true
53+
ColumnLimit: 120
54+
CommentPragmas: '^ IWYU pragma:'
55+
CompactNamespaces: false
56+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
57+
ConstructorInitializerIndentWidth: 4
58+
ContinuationIndentWidth: 4
59+
Cpp11BracedListStyle: true
60+
DeriveLineEnding: true
61+
DerivePointerAlignment: false
62+
DisableFormat: false
63+
ExperimentalAutoDetectBinPacking: false
64+
FixNamespaceComments: true
65+
ForEachMacros:
66+
- foreach
67+
- Q_FOREACH
68+
- BOOST_FOREACH
69+
IncludeBlocks: Preserve
70+
IncludeCategories:
71+
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
72+
Priority: 2
73+
SortPriority: 0
74+
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
75+
Priority: 3
76+
SortPriority: 0
77+
- Regex: '.*'
78+
Priority: 1
79+
SortPriority: 0
80+
IncludeIsMainRegex: '(Test)?$'
81+
IncludeIsMainSourceRegex: ''
82+
IndentCaseLabels: false
83+
IndentGotoLabels: true
84+
IndentPPDirectives: None
85+
IndentWidth: 2
86+
IndentWrappedFunctionNames: false
87+
# InsertNewlineAtEOF: true # clang 16 which is not widely installed
88+
JavaScriptQuotes: Leave
89+
JavaScriptWrapImports: true
90+
KeepEmptyLinesAtTheStartOfBlocks: true
91+
MacroBlockBegin: ''
92+
MacroBlockEnd: ''
93+
MaxEmptyLinesToKeep: 1
94+
NamespaceIndentation: None
95+
ObjCBinPackProtocolList: Auto
96+
ObjCBlockIndentWidth: 2
97+
ObjCSpaceAfterProperty: false
98+
ObjCSpaceBeforeProtocolList: true
99+
PenaltyBreakAssignment: 2
100+
PenaltyBreakBeforeFirstCallParameter: 19
101+
PenaltyBreakComment: 300
102+
PenaltyBreakFirstLessLess: 120
103+
PenaltyBreakString: 1000
104+
PenaltyBreakTemplateDeclaration: 10
105+
PenaltyExcessCharacter: 1000000
106+
PenaltyReturnTypeOnItsOwnLine: 60
107+
PointerAlignment: Right
108+
ReflowComments: true
109+
SortIncludes: false
110+
SortUsingDeclarations: true
111+
SpaceAfterCStyleCast: false
112+
SpaceAfterLogicalNot: false
113+
SpaceAfterTemplateKeyword: true
114+
SpaceBeforeAssignmentOperators: true
115+
SpaceBeforeCpp11BracedList: false
116+
SpaceBeforeCtorInitializerColon: true
117+
SpaceBeforeInheritanceColon: true
118+
SpaceBeforeParens: ControlStatements
119+
SpaceBeforeRangeBasedForLoopColon: true
120+
SpaceInEmptyBlock: false
121+
SpaceInEmptyParentheses: false
122+
SpacesBeforeTrailingComments: 1
123+
SpacesInAngles: false
124+
SpacesInConditionalStatement: false
125+
SpacesInContainerLiterals: true
126+
SpacesInCStyleCastParentheses: false
127+
SpacesInParentheses: false
128+
SpacesInSquareBrackets: false
129+
SpaceBeforeSquareBrackets: false
130+
Standard: Latest
131+
StatementMacros:
132+
- Q_UNUSED
133+
- QT_REQUIRE_VERSION
134+
TabWidth: 8
135+
UseCRLF: false
136+
UseTab: Never
137+
...
138+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: clang-format Check
2+
on: [push, pull_request]
3+
jobs:
4+
formatting-check:
5+
name: Formatting Check
6+
runs-on: ubuntu-latest
7+
strategy:
8+
matrix:
9+
path:
10+
- check: 'src'
11+
exclude: '(Globals.c|GlobalsBrogue.c|GlobalsRapidBrogue.c)'
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Run clang-format style check for C/C++/Protobuf programs.
16+
uses: jidicula/[email protected]
17+
with:
18+
clang-format-version: '10'
19+
check-path: ${{ matrix.path['check'] }}
20+
exclude-regex: ${{ matrix.path['exclude'] }}
21+
fallback-style: 'Mozilla' # optional

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
include config.mk
22

3+
FORMATTER_EXCLUSION_LIST := "src/brogue/Globals.c" "src/variants/GlobalsRapidBrogue.c" "src/variants/GlobalsBrogue.c"
4+
35
cflags := -Isrc/brogue -Isrc/platform -Isrc/variants -std=c99 \
46
-Wall -Wpedantic -Werror=implicit -Wno-parentheses -Wno-unused-result \
57
-Wformat -Werror=format-security -Wformat-overflow=0
@@ -78,6 +80,9 @@ include make/*.mk
7880
clean:
7981
$(warning 'make clean' is no longer needed in many situations, so is not supported. Use 'make -B' to force rebuild something.)
8082

83+
format:
84+
@./formatter.sh src $(FORMATTER_EXCLUSION_LIST)
85+
8186
escape = $(subst ','\'',$(1))
8287
vars:
8388
mkdir -p vars
@@ -86,5 +91,6 @@ vars/%: vars FORCE
8691
@echo '$(call escape,$($*))' > vars/$*.tmp
8792
@if cmp --quiet vars/$*.tmp vars/$*; then :; else cp vars/$*.tmp vars/$*; fi
8893

94+
.PHONY: format
8995

9096
FORCE:

formatter.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
# Function to recursively find source files and run clang-format
4+
function format_source_files() {
5+
local root_dir="$1"
6+
local exclude_list=("$@")
7+
8+
find "$root_dir" -type f \( -name "*.c" -o -name "*.cpp" -o -name "*.h" \) | while read -r file; do
9+
# Check if the file should be excluded
10+
if [[ ! " ${exclude_list[@]} " =~ " ${file} " ]]; then
11+
echo "Formatting: $file"
12+
clang-format -i "$file"
13+
# sed command to ensure files end with a newline
14+
sed -i -e '$!b' -e '/^$/!a\' "$file"
15+
else
16+
echo "Excluding: $file"
17+
fi
18+
done
19+
}
20+
21+
# Check if the correct number of arguments is provided
22+
if [ "$#" -lt 2 ]; then
23+
echo "Usage: $0 <directory_root> <exclude_file1> <exclude_file2> ... <exclude_fileN>"
24+
exit 1
25+
fi
26+
27+
directory_root="$1"
28+
shift # Remove the first argument (directory_root) from the arguments list
29+
30+
# Call the format_source_files function with the provided arguments
31+
format_source_files "$directory_root" "$@"

0 commit comments

Comments
 (0)