Skip to content

Commit d99ae77

Browse files
[git] Add clang code formatting
1 parent d147e97 commit d99ae77

File tree

6 files changed

+332
-1
lines changed

6 files changed

+332
-1
lines changed

.clang-format

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
Language: Cpp
2+
# BasedOnStyle: Google
3+
AccessModifierOffset: -2
4+
AlignAfterOpenBracket: AlwaysBreak
5+
AlignConsecutiveAssignments: false
6+
AlignConsecutiveDeclarations: false
7+
AlignEscapedNewlines: DontAlign
8+
AlignOperands: true
9+
AlignTrailingComments: true
10+
AllowAllArgumentsOnNextLine: true
11+
AllowAllConstructorInitializersOnNextLine: false
12+
AllowAllParametersOfDeclarationOnNextLine: false
13+
AllowShortBlocksOnASingleLine: false
14+
AllowShortCaseLabelsOnASingleLine: false
15+
AllowShortFunctionsOnASingleLine: None
16+
AllowShortLambdasOnASingleLine: Empty
17+
AllowShortIfStatementsOnASingleLine: false
18+
AllowShortLoopsOnASingleLine: false
19+
AlwaysBreakAfterDefinitionReturnType: None
20+
AlwaysBreakAfterReturnType: None
21+
AlwaysBreakBeforeMultilineStrings: true
22+
AlwaysBreakTemplateDeclarations: Yes
23+
BinPackArguments: false
24+
BinPackParameters: false
25+
BraceWrapping:
26+
AfterCaseLabel: true
27+
AfterClass: true
28+
AfterControlStatement: Always
29+
AfterEnum: true
30+
AfterFunction: true
31+
AfterNamespace: true
32+
AfterObjCDeclaration: false
33+
AfterStruct: true
34+
AfterUnion: true
35+
AfterExternBlock: true
36+
BeforeCatch: true
37+
BeforeElse: true
38+
BeforeLambdaBody: true
39+
BeforeWhile: true
40+
IndentBraces: false
41+
SplitEmptyFunction: true
42+
SplitEmptyRecord: true
43+
SplitEmptyNamespace: true
44+
45+
BreakBeforeBinaryOperators: None
46+
BreakBeforeBraces: Custom
47+
BreakBeforeInheritanceComma: true
48+
BreakInheritanceList: BeforeComma
49+
BreakBeforeTernaryOperators: true
50+
BreakConstructorInitializersBeforeComma: false
51+
BreakConstructorInitializers: BeforeComma
52+
BreakAfterJavaFieldAnnotations: false
53+
BreakStringLiterals: true
54+
ColumnLimit: 120
55+
CommentPragmas: '^\s*(\*)?\s*\\.+'
56+
CompactNamespaces: false
57+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
58+
ConstructorInitializerIndentWidth: 2
59+
ContinuationIndentWidth: 4
60+
Cpp11BracedListStyle: true
61+
DerivePointerAlignment: false
62+
DisableFormat: false
63+
# EmptyLineBeforeAccessModifier: Always
64+
ExperimentalAutoDetectBinPacking: false
65+
FixNamespaceComments: true
66+
ForEachMacros:
67+
- foreach
68+
- Q_FOREACH
69+
- BOOST_FOREACH
70+
IncludeBlocks: Regroup
71+
IncludeCategories:
72+
- Regex: '^<(gtest|gmock)/.*'
73+
Priority: 1
74+
75+
- Regex: '^<sc-memory/*.*'
76+
Priority: 11
77+
78+
- Regex: '^"sc_*.*'
79+
Priority: 21
80+
81+
- Regex: '.*'
82+
Priority: 100
83+
# IncludeIsMainRegex: '([-_](test|unittest))?$'
84+
IndentCaseLabels: false
85+
IndentPPDirectives: AfterHash
86+
IndentWidth: 2
87+
IndentWrappedFunctionNames: false
88+
JavaScriptQuotes: Leave
89+
JavaScriptWrapImports: true
90+
KeepEmptyLinesAtTheStartOfBlocks: false
91+
MacroBlockBegin: ''
92+
MacroBlockEnd: ''
93+
MaxEmptyLinesToKeep: 1
94+
NamespaceIndentation: None
95+
ObjCBinPackProtocolList: Never
96+
ObjCBlockIndentWidth: 2
97+
ObjCSpaceAfterProperty: false
98+
ObjCSpaceBeforeProtocolList: true
99+
PenaltyBreakAssignment: 2
100+
PenaltyBreakBeforeFirstCallParameter: 1
101+
PenaltyBreakComment: 300
102+
PenaltyBreakFirstLessLess: 120
103+
PenaltyBreakString: 1000
104+
PenaltyBreakTemplateDeclaration: 10
105+
PenaltyExcessCharacter: 1000
106+
PenaltyReturnTypeOnItsOwnLine: 2000
107+
PointerAlignment: Middle
108+
RawStringFormats:
109+
- Language: Cpp
110+
Delimiters:
111+
- cc
112+
- CC
113+
- cpp
114+
- Cpp
115+
- CPP
116+
- 'c++'
117+
- 'C++'
118+
CanonicalDelimiter: ''
119+
BasedOnStyle: google
120+
- Language: TextProto
121+
Delimiters:
122+
- pb
123+
- PB
124+
- proto
125+
- PROTO
126+
EnclosingFunctions:
127+
- EqualsProto
128+
- EquivToProto
129+
- PARSE_PARTIAL_TEXT_PROTO
130+
- PARSE_TEST_PROTO
131+
- PARSE_TEXT_PROTO
132+
- ParseTextOrDie
133+
- ParseTextProtoOrDie
134+
CanonicalDelimiter: ''
135+
BasedOnStyle: google
136+
ReflowComments: true
137+
SortIncludes: true
138+
SortUsingDeclarations: true
139+
SpaceAfterCStyleCast: false
140+
SpaceAfterLogicalNot: false
141+
SpaceAfterTemplateKeyword: true
142+
# SpaceAroundPointerQualifiers: After/Both
143+
SpaceBeforeAssignmentOperators: true
144+
SpaceBeforeCpp11BracedList: false
145+
SpaceBeforeCtorInitializerColon: true
146+
SpaceBeforeInheritanceColon: true
147+
SpaceBeforeParens: ControlStatements
148+
SpaceBeforeRangeBasedForLoopColon: true
149+
SpaceInEmptyParentheses: false
150+
SpacesBeforeTrailingComments: 2
151+
SpacesInAngles: false
152+
SpacesInContainerLiterals: false
153+
SpacesInCStyleCastParentheses: false
154+
SpacesInParentheses: false
155+
SpacesInSquareBrackets: false
156+
Standard: Auto
157+
StatementMacros:
158+
- Q_UNUSED
159+
- QT_REQUIRE_VERSION
160+
TabWidth: 2
161+
UseTab: Never
162+
...

.github/workflows/main.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ jobs:
3232
checkAllCommitMessages: true # optional, default is false
3333
# you must provide GITHUB_TOKEN to this input if checkAllCommitMessages is true
3434
accessToken: ${{ secrets.GITHUB_TOKEN }} # optional, default is false
35+
36+
check_formatting:
37+
name: Check code formatting
38+
runs-on: ubuntu-latest
39+
40+
steps:
41+
- name: Checkout
42+
uses: actions/checkout@v1
43+
44+
- name: Check
45+
id: check_formatting
46+
run: scripts/ci/check-formatting.sh
47+
3548
run_tests:
3649
name: ${{matrix.config.name}}
3750
runs-on: ${{matrix.config.os}}

scripts/ci/check-formatting.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
# sudo apt-get update
6+
# sudo apt-get install -y clang-format
7+
8+
python3 scripts/tools/format-code.py --check \
9+
sc-kpm \
10+
sc-memory \
11+
sc-network \
12+
sc-server \
13+
tools

scripts/ci/install-deps.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ packagelist=(
1616
python3-setuptools
1717
)
1818
sudo apt-get update
19-
sudo apt-get install ${packagelist[@]}
19+
sudo apt-get install -y ${packagelist[@]}

scripts/format-code.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
python3 scripts/tools/format-code.py \
6+
sc-kpm \
7+
sc-memory \
8+
sc-network \
9+
sc-server \
10+
tools

scripts/tools/format-code.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import argparse
2+
import os
3+
import shutil
4+
import subprocess
5+
import sys
6+
7+
import xml.etree.ElementTree as ET
8+
9+
allowed_extensions = [
10+
".cpp",
11+
".hpp",
12+
".c"
13+
".h"
14+
]
15+
16+
ignore_files = [
17+
"json/json.hpp",
18+
"sha256",
19+
"tinyxml",
20+
]
21+
22+
class Formatter:
23+
24+
Executable = "clang-format"
25+
26+
def __init__(self):
27+
if shutil.which(Formatter.Executable) is None:
28+
raise "clang-format is not exists"
29+
30+
def collect_files(self, paths):
31+
files = []
32+
33+
def add_file(file_path):
34+
need_ignore = False
35+
36+
for i in ignore_files:
37+
if i.lower() in file_path:
38+
need_ignore = True
39+
break
40+
41+
if not need_ignore:
42+
files.append(file_path)
43+
44+
for d in paths:
45+
if not os.path.exists(d):
46+
continue
47+
if os.path.isdir(d):
48+
for root, _, filenames in os.walk(d):
49+
for filename in filenames:
50+
_, ext = os.path.splitext(filename)
51+
if ext in allowed_extensions:
52+
add_file(os.path.join(root, filename))
53+
elif os.path.splitext(d)[1] in allowed_extensions:
54+
add_file(d)
55+
return files
56+
57+
def format(self, search_paths):
58+
files = self.collect_files(search_paths)
59+
60+
for f in files:
61+
print("Format file {}".format(f))
62+
cmd = "{} -i {}".format(Formatter.Executable, f)
63+
process = subprocess.Popen(cmd, shell=True, cwd=os.getcwd(), stdout=None, stderr=None)
64+
rc = process.wait()
65+
if rc != 0:
66+
raise "Error while formatting {}".format(f)
67+
68+
def check(self, search_paths) -> bool:
69+
files = self.collect_files(search_paths)
70+
71+
final_result = True
72+
non_formatted_files = []
73+
74+
for f in files:
75+
print('Check {}'.format(f), end='')
76+
cmd = "{} --output-replacements-xml {}".format(Formatter.Executable, f)
77+
78+
process = subprocess.Popen(cmd, shell=True, cwd=os.getcwd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
79+
rc = process.wait()
80+
output = process.communicate()[0]
81+
82+
passed = False
83+
84+
if rc == 0:
85+
86+
passed = True
87+
# check if there are any replacements
88+
s = output.decode("utf-8")
89+
if len(s) > 0:
90+
root = ET.fromstring(s)
91+
replacements = root.findall("replacement")
92+
93+
if len(replacements) > 0:
94+
passed = False
95+
96+
if not passed:
97+
final_result = False
98+
non_formatted_files.append(f)
99+
100+
print (" - {}".format("ok" if passed else "fail"))
101+
102+
return (final_result, non_formatted_files)
103+
104+
105+
if __name__ == "__main__":
106+
107+
parser = argparse.ArgumentParser(description="Process some integers.")
108+
parser.add_argument(
109+
"files",
110+
nargs="*",
111+
help=
112+
"List of input files or folders (relative to the working directory)",
113+
)
114+
parser.add_argument("--check", action="store_true")
115+
116+
args = parser.parse_args()
117+
118+
c = Formatter()
119+
if args.check:
120+
passed, files = c.check(args.files)
121+
if not passed:
122+
if len(files) > 0:
123+
print ("-" * 10)
124+
print("Files that need formatting:")
125+
for f in files:
126+
print(" {}".format(f))
127+
else:
128+
print ("-" * 10)
129+
print ("All checks passed")
130+
131+
sys.exit(0 if passed else 1)
132+
else:
133+
c.format(args.files)

0 commit comments

Comments
 (0)