-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcheck_code_style.py
executable file
·71 lines (56 loc) · 2.51 KB
/
check_code_style.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# Usage: ./check_code_style.sh [c|p]
import os
import argparse
import re
import subprocess
# Options
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--version",
help="Prints script's version", action='store_true')
parser.add_argument("-c", "--checkCodeStyle",
help="Checks C code in LLVM style", action='store_true')
parser.add_argument("-p", "--checkCppCodeStyle",
help="Checks C++ code Google style", action='store_true')
parser.add_argument("-t", "--checkInTravis",
help="Checks the code style in Travis service", action='store_true')
args = parser.parse_args()
# for options
version = args.version
c_style = args.checkCodeStyle
cpp_style = args.checkCppCodeStyle
travis_flag = args.checkInTravis
def apply_clang_format(style, path, extensions):
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(extensions):
root = re.escape(root)
file = re.escape(file)
os.system("clang-format -i -style="
+ style + " " + root + "/" + file)
if version == True:
os.system(tool_path + "--version")
exit(0)
if c_style == True:
if not travis_flag:
print("Formatting C codes with clang-format adopting LLVM style ...")
# File Extension filter. You can add new extension
extensions = (".c", ".h", ".hh")
apply_clang_format('llvm', 'modules/backend/library', extensions)
if cpp_style == True:
# File Extension filter. You can add new extension
cpp_extensions = (".cxx", ".cpp", ".c", ".hxx", ".hh", ".cc", ".hpp")
paths_to_check = ["modules/frontend", "modules/backend/pass"]
if not travis_flag:
print("Formatting CPP codes with clang-format adopting Google style ...")
for path in paths_to_check:
apply_clang_format('google', path, cpp_extensions)
if travis_flag:
print("Check issues related to code style ...")
for path in paths_to_check:
if path == "modules/backend/pass":
subprocess.run(['python', 'utils/cpplint.py', '--recursive', '--filter=-runtime/references,-runtime/int',
'--linelength=120', '--counting=detailed', path], check=True)
else:
subprocess.run(['python', 'utils/cpplint.py', '--recursive', '--filter=-runtime/printf',
'--linelength=120', '--counting=detailed', path], check=True)