Skip to content

Commit f8ba686

Browse files
author
Lukasz Dalek
committed
Add format target for source code formatting
Signed-off-by: Lukasz Dalek <[email protected]>
1 parent e2a99f6 commit f8ba686

File tree

3 files changed

+238
-0
lines changed

3 files changed

+238
-0
lines changed

.clang-format

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
Language: Cpp
2+
BasedOnStyle: Chromium
3+
AccessModifierOffset: 0
4+
AlignAfterOpenBracket: DontAlign
5+
AlignConsecutiveAssignments: false
6+
AlignConsecutiveDeclarations: false
7+
AlignEscapedNewlines: Left
8+
AlignOperands: true
9+
AlignTrailingComments: true
10+
AllowAllParametersOfDeclarationOnNextLine: false
11+
AllowShortBlocksOnASingleLine: false
12+
AllowShortCaseLabelsOnASingleLine: false
13+
AllowShortFunctionsOnASingleLine: true
14+
AllowShortIfStatementsOnASingleLine: false
15+
AllowShortLoopsOnASingleLine: false
16+
AlwaysBreakAfterDefinitionReturnType: None
17+
AlwaysBreakAfterReturnType: None
18+
AlwaysBreakBeforeMultilineStrings: true
19+
AlwaysBreakTemplateDeclarations: true
20+
BinPackArguments: true
21+
BinPackParameters: false
22+
BraceWrapping:
23+
AfterClass: false
24+
AfterControlStatement: false
25+
AfterEnum: false
26+
AfterFunction: false
27+
AfterNamespace: false
28+
AfterObjCDeclaration: false
29+
AfterStruct: false
30+
AfterUnion: false
31+
BeforeCatch: false
32+
BeforeElse: false
33+
IndentBraces: false
34+
SplitEmptyFunction: false
35+
SplitEmptyRecord: true
36+
SplitEmptyNamespace: true
37+
BreakBeforeBinaryOperators: All
38+
BreakBeforeBraces: Custom
39+
BreakBeforeInheritanceComma: false
40+
BreakBeforeTernaryOperators: true
41+
BreakConstructorInitializersBeforeComma: false
42+
BreakConstructorInitializers: BeforeComma
43+
BreakAfterJavaFieldAnnotations: false
44+
BreakStringLiterals: true
45+
ColumnLimit: 0
46+
CommentPragmas: '^ IWYU pragma:'
47+
CompactNamespaces: false
48+
ConstructorInitializerAllOnOneLineOrOnePerLine: false
49+
ConstructorInitializerIndentWidth: 4
50+
ContinuationIndentWidth: 4
51+
Cpp11BracedListStyle: true
52+
DerivePointerAlignment: false
53+
DisableFormat: false
54+
ExperimentalAutoDetectBinPacking: false
55+
FixNamespaceComments: true
56+
ForEachMacros:
57+
- foreach
58+
- Q_FOREACH
59+
- BOOST_FOREACH
60+
IncludeCategories:
61+
- Regex: '^<.*\.h>'
62+
Priority: 1
63+
- Regex: '^<.*'
64+
Priority: 2
65+
- Regex: '.*'
66+
Priority: 3
67+
IncludeIsMainRegex: '([-_](test|unittest))?$'
68+
IndentCaseLabels: true
69+
IndentWidth: 4
70+
IndentWrappedFunctionNames: false
71+
JavaScriptQuotes: Leave
72+
JavaScriptWrapImports: true
73+
KeepEmptyLinesAtTheStartOfBlocks: false
74+
MacroBlockBegin: ''
75+
MacroBlockEnd: ''
76+
MaxEmptyLinesToKeep: 1
77+
NamespaceIndentation: None
78+
ObjCBlockIndentWidth: 2
79+
ObjCSpaceAfterProperty: false
80+
ObjCSpaceBeforeProtocolList: false
81+
PenaltyBreakAssignment: 2
82+
PenaltyBreakBeforeFirstCallParameter: 1
83+
PenaltyBreakComment: 300
84+
PenaltyBreakFirstLessLess: 120
85+
PenaltyBreakString: 1000
86+
PenaltyExcessCharacter: 1000000
87+
PenaltyReturnTypeOnItsOwnLine: 200
88+
PointerAlignment: Left
89+
ReflowComments: false
90+
SortIncludes: false
91+
SortUsingDeclarations: true
92+
SpaceAfterCStyleCast: false
93+
SpaceAfterTemplateKeyword: false
94+
SpaceBeforeAssignmentOperators: true
95+
SpaceBeforeParens: ControlStatements
96+
SpaceInEmptyParentheses: false
97+
SpacesBeforeTrailingComments: 2
98+
SpacesInAngles: false
99+
SpacesInContainerLiterals: true
100+
SpacesInCStyleCastParentheses: false
101+
SpacesInParentheses: false
102+
SpacesInSquareBrackets: false
103+
Standard: Auto
104+
TabWidth: 4
105+
UseTab: Never

CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,38 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
5757
endif()
5858

5959
install(TARGETS libblifparse blifparse_test DESTINATION bin)
60+
61+
#
62+
# ----------------------- Code format --------------------------
63+
#
64+
65+
set(code_format_files
66+
-name '*.cpp' -print -o -name '*.h' -print
67+
-o -name '*.tpp' -print -o -name '*.hpp' -print)
68+
69+
set(code_format_dirs ${PROJECT_SOURCE_DIR}/src)
70+
71+
#
72+
# Use clang-format-5.0 for code format
73+
#
74+
add_custom_target(format-cpp
75+
COMMAND find ${code_format_dirs} ${code_format_files} |
76+
xargs -P `nproc` clang-format-5.0 -style=file -i)
77+
78+
#
79+
# Use simple python script for fixing C like boxed comments
80+
#
81+
add_custom_target(format-cpp-fix-comments DEPENDS format-cpp
82+
COMMAND find ${code_format_dirs} ${code_format_files} |
83+
xargs -L 1 -P `nproc`
84+
python3 ${PROJECT_SOURCE_DIR}/scripts/format.py --inplace --fix-comments --input)
85+
86+
#
87+
# Use simple python script for fixing template brackets e.g. <<>
88+
#
89+
add_custom_target(format-cpp-fix-template-operators DEPENDS format-cpp
90+
COMMAND find ${code_format_dirs} ${code_format_files} |
91+
xargs -L 1 -P `nproc`
92+
python3 ${PROJECT_SOURCE_DIR}/scripts/format.py --inplace --fix-template-operators --input)
93+
94+
add_custom_target(format DEPENDS format-cpp-fix-comments format-cpp-fix-template-operators)

scripts/format.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python3
2+
3+
import sys, os
4+
import argparse
5+
6+
def fix_comments(f, o):
7+
space_string = ''
8+
9+
for line in f:
10+
# Skip lines with // comments
11+
if space_string == '' and line.find('//') != -1:
12+
o.write(line)
13+
continue
14+
15+
sc = line.find("/*")
16+
ec = line.find("*/")
17+
18+
# Skip /* foo bar */ comments
19+
if sc != -1 and ec != -1:
20+
o.write(line)
21+
else:
22+
# Start of /* comment */
23+
if sc != -1:
24+
space_string = (''.join(' ' for i in range(sc+1)))
25+
o.write(line)
26+
elif space_string != '':
27+
# Already in /* comment */
28+
ls = line.lstrip()
29+
30+
# Empty line, write aligned comment prefix
31+
if len(ls) == 0:
32+
o.write(space_string + "*" + "\n")
33+
elif ls[0] != '*':
34+
#
35+
# Fix comments like '********* sth **********'
36+
# to look like ' * ********** sth **********'
37+
#
38+
o.write(space_string + "* " + ls)
39+
else:
40+
# Rest of the /* comment */
41+
o.write(space_string + ls)
42+
else:
43+
# Outside /* comment */
44+
o.write(line)
45+
46+
# Moving outside /* comment */
47+
if ec != -1:
48+
space_string = ''
49+
50+
def fix_template_operators(f, o):
51+
for line in f:
52+
line = line.replace('<<>', '< <>');
53+
line = line.replace('><>', '> <>');
54+
line = line.replace('==<>', '== <>');
55+
line = line.replace('!=<>', '!= <>');
56+
57+
o.write(line);
58+
59+
def main(argv):
60+
parser = argparse.ArgumentParser(description='Tool for fixing clang code formatting')
61+
parser.add_argument('--inplace', dest='inplace',
62+
action='store_true',
63+
help='Inplace edit specified file')
64+
parser.add_argument('--fix-comments', dest='fix_comments',
65+
action='store_const',
66+
const='fix_comments',
67+
help='Fix C like box comments')
68+
parser.add_argument('--fix-template-operators', dest='fix_template_operators',
69+
action='store_const',
70+
const='fix_template_operators',
71+
help='Fix C++ template operators e.g. operator<<> => operator< <>')
72+
parser.add_argument('--input', dest='inputfile', action='store',
73+
help='Input file')
74+
parser.add_argument('--output', dest='outputfile', action='store',
75+
help='Output file')
76+
args = parser.parse_args(argv[1:]);
77+
78+
for p in args.fix_comments, args.fix_template_operators:
79+
if not p:
80+
continue
81+
82+
fin = open(args.inputfile, 'r')
83+
84+
if args.inplace:
85+
fout = open(args.inputfile + '.bak', 'w')
86+
else:
87+
fout = open(args.outputfile, 'w');
88+
89+
eval('' + p + '(fin, fout)')
90+
91+
fin.close();
92+
fout.close();
93+
94+
if args.inplace:
95+
os.rename(args.inputfile + '.bak', args.inputfile)
96+
97+
if __name__ == "__main__":
98+
main(sys.argv)

0 commit comments

Comments
 (0)