Skip to content

Commit b8f7dcd

Browse files
committed
added Michaels clang-format scripts
1 parent 8443c03 commit b8f7dcd

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

.clang-format

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
Language: Cpp
3+
AccessModifierOffset: -1
4+
AlignAfterOpenBracket: Align
5+
AlignConsecutiveAssignments: true
6+
AlignConsecutiveDeclarations: false
7+
AlignEscapedNewlinesLeft: true
8+
AlignOperands: true
9+
AlignTrailingComments: true
10+
AllowAllParametersOfDeclarationOnNextLine: true
11+
AllowShortBlocksOnASingleLine: false
12+
AllowShortCaseLabelsOnASingleLine: false
13+
AllowShortFunctionsOnASingleLine: All
14+
AllowShortIfStatementsOnASingleLine: true
15+
AllowShortLoopsOnASingleLine: true
16+
AlwaysBreakAfterReturnType: None
17+
AlwaysBreakBeforeMultilineStrings: true
18+
AlwaysBreakTemplateDeclarations: true
19+
BinPackArguments: true
20+
BinPackParameters: true
21+
BraceWrapping:
22+
AfterClass: false
23+
AfterControlStatement: false
24+
AfterEnum: false
25+
AfterFunction: false
26+
AfterNamespace: false
27+
AfterObjCDeclaration: false
28+
AfterStruct: false
29+
AfterUnion: false
30+
BeforeCatch: false
31+
BeforeElse: false
32+
IndentBraces: false
33+
BreakBeforeBinaryOperators: None
34+
BreakBeforeBraces: Attach
35+
BreakBeforeTernaryOperators: true
36+
BreakConstructorInitializersBeforeComma: false
37+
ColumnLimit: 90
38+
CommentPragmas: '^ IWYU pragma:'
39+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
40+
ConstructorInitializerIndentWidth: 6
41+
ContinuationIndentWidth: 6
42+
Cpp11BracedListStyle: true
43+
DerivePointerAlignment: false
44+
DisableFormat: false
45+
ExperimentalAutoDetectBinPacking: false
46+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
47+
IndentCaseLabels: true
48+
IndentWidth: 2
49+
IndentWrappedFunctionNames: false
50+
KeepEmptyLinesAtTheStartOfBlocks: true
51+
MaxEmptyLinesToKeep: 1
52+
NamespaceIndentation: None
53+
ObjCBlockIndentWidth: 2
54+
ObjCSpaceAfterProperty: false
55+
ObjCSpaceBeforeProtocolList: false
56+
PenaltyBreakBeforeFirstCallParameter: 1
57+
PenaltyBreakComment: 300
58+
PenaltyBreakFirstLessLess: 120
59+
PenaltyBreakString: 1000
60+
PenaltyExcessCharacter: 1000000
61+
PenaltyReturnTypeOnItsOwnLine: 200
62+
PointerAlignment: Left
63+
ReflowComments: true
64+
SortIncludes: true
65+
SpaceAfterCStyleCast: false
66+
SpaceBeforeAssignmentOperators: true
67+
SpaceBeforeParens: ControlStatements
68+
SpaceInEmptyParentheses: false
69+
SpacesBeforeTrailingComments: 2
70+
SpacesInAngles: false
71+
SpacesInContainerLiterals: true
72+
SpacesInCStyleCastParentheses: false
73+
SpacesInParentheses: false
74+
SpacesInSquareBrackets: false
75+
Standard: Cpp11
76+
TabWidth: 8
77+
UseTab: Never
78+
...
79+

.clang-format.ignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
external/**

scripts/common.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
## vi: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
3+
## ---------------------------------------------------------------------
4+
##
5+
## Copyright 2019 Michael F. Herbst
6+
##
7+
## Licensed under the Apache License, Version 2.0 (the "License");
8+
## you may not use this file except in compliance with the License.
9+
## You may obtain a copy of the License at
10+
##
11+
## http://www.apache.org/licenses/LICENSE-2.0
12+
##
13+
## Unless required by applicable law or agreed to in writing, software
14+
## distributed under the License is distributed on an "AS IS" BASIS,
15+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
## See the License for the specific language governing permissions and
17+
## limitations under the License.
18+
##
19+
## ---------------------------------------------------------------------
20+
import os
21+
import subprocess
22+
23+
24+
def list_committed_files():
25+
"""List all files which are commited in the repo located
26+
in directory"""
27+
command = "git ls-tree --full-tree --name-only -r HEAD".split(" ")
28+
output = subprocess.check_output(command, universal_newlines=True)
29+
return [line for line in output.split("\n") if line != ""]
30+
31+
32+
def get_repo_root_path():
33+
return subprocess.check_output("git rev-parse --show-toplevel".split(" "),
34+
universal_newlines=True).strip()
35+
36+
37+
def read_ignore_globs(ignore_file):
38+
"""
39+
Read a file, given relative to the root of the repo
40+
and return the list of globs of files to be ignored.
41+
42+
If the file does not exist, returns an empty list.
43+
"""
44+
if not os.path.isabs(ignore_file):
45+
ignore_file = os.path.join(get_repo_root_path(), ignore_file)
46+
47+
if not os.path.isfile(ignore_file):
48+
return []
49+
with open(ignore_file) as ignf:
50+
return [line.strip() for line in ignf if not line.startswith("#")]

scripts/run-clang-format.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
## vi: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
3+
## ---------------------------------------------------------------------
4+
##
5+
## Copyright 2019 Michael F. Herbst
6+
##
7+
## Licensed under the Apache License, Version 2.0 (the "License");
8+
## you may not use this file except in compliance with the License.
9+
## You may obtain a copy of the License at
10+
##
11+
## http://www.apache.org/licenses/LICENSE-2.0
12+
##
13+
## Unless required by applicable law or agreed to in writing, software
14+
## distributed under the License is distributed on an "AS IS" BASIS,
15+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
## See the License for the specific language governing permissions and
17+
## limitations under the License.
18+
##
19+
## ---------------------------------------------------------------------
20+
import os
21+
import glob
22+
import common
23+
import argparse
24+
import subprocess
25+
26+
27+
def read_ignored_files():
28+
ignore_file = os.path.join(common.get_repo_root_path(), ".clang-format.ignore")
29+
if not os.path.isfile(ignore_file):
30+
return []
31+
with open(ignore_file) as ignf:
32+
return [line.strip() for line in ignf if not line.startswith("#")]
33+
34+
35+
def main():
36+
parser = argparse.ArgumentParser(
37+
description="Run clang-format on all currently committed c++ files"
38+
)
39+
parser.add_argument("-clang-format", metavar="PATH", default="clang-format",
40+
help="Name or path of clang-format executable to use.")
41+
args = parser.parse_args()
42+
43+
cpp_extensions = [".cpp", ".hpp", ".cxx", ".hxx", ".hh", ".cc", ".h", ".c"]
44+
root = common.get_repo_root_path()
45+
cpp_files = [
46+
os.path.join(root, file) for file in common.list_committed_files()
47+
if os.path.splitext(file)[1] in cpp_extensions
48+
]
49+
ignore_globs = common.read_ignore_globs(".clang-format.ignore")
50+
cpp_files = [
51+
os.path.join(root, name) for name in common.list_committed_files()
52+
if os.path.splitext(name)[1] in cpp_extensions and
53+
not any(glob.fnmatch.fnmatch(name, ignore) for ignore in ignore_globs)
54+
]
55+
56+
commandline = [args.clang_format, "-style=file", "-i"]
57+
for cfile in cpp_files:
58+
subprocess.check_call(commandline + [cfile])
59+
60+
61+
if __name__ == "__main__":
62+
main()

0 commit comments

Comments
 (0)