forked from s5z/zsim
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlint_includes.py
executable file
·98 lines (82 loc) · 3.07 KB
/
lint_includes.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
# Copyright (C) 2013-2015 by Massachusetts Institute of Technology
#
# This file is part of zsim.
#
# zsim is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, version 2.
#
# If you use this software in your research, we request that you reference
# the zsim paper ("ZSim: Fast and Accurate Microarchitectural Simulation of
# Thousand-Core Systems", Sanchez and Kozyrakis, ISCA-40, June 2013) as the
# source of the simulator in any publications that use this software, and that
# you send us a citation of your work.
#
# zsim is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
import os, sys
#dryRun = True
dryRun = False
srcs = sys.argv[1:]
def sortIncludes(lines, fname):
def prefix(l):
if l.find("<") >= 0:
return "2"
# if you want to differentiate...
#if l.find(".h") >= 0: return "2" # C system headers
#else: return "3" # C++ system headers
else:
if l.find('"' + fname + '.') >= 0: return "1" # Our own header
return "4" # Program headers
ll = [prefix(l) + l.strip() for l in lines if len(l.strip()) > 0]
sl = [l[1:] for l in sorted(ll)]
if lines[-1].strip() == "": sl += [""]
#print sl
return sl
for src in srcs:
f = open(src, 'r+') # we open for read/write here to fail early on read-only files
txt = f.read()
f.close()
bName = os.path.basename(src).split(".")[0]
print bName
lines = [l for l in txt.split("\n")]
includeBlocks = []
blockStart = -1
for i in range(len(lines)):
l = lines[i].strip()
isInclude = l.startswith("#include") and l.find("NOLINT") == -1
isEmpty = l == ""
if blockStart == -1:
if isInclude: blockStart = i # start block
else:
if not (isInclude or isEmpty): # close block
includeBlocks.append((blockStart, i))
blockStart = -1
print src, len(includeBlocks), "blocks"
newIncludes = [(s , e, sortIncludes(lines[s:e], bName)) for (s, e) in includeBlocks]
for (s , e, ii) in newIncludes:
# Print?
if ii == lines[s:e]:
print "Block in lines %d-%d matches" % (s, e-1)
continue
for i in range(s, e):
print "%3d: %s%s | %s" % (i, lines[i], " "*(40 - len(lines[i][:39])), ii[i-s] if i-s < len(ii) else "")
print ""
prevIdx = 0
newLines = []
for (s , e, ii) in newIncludes:
newLines += lines[prevIdx:s] + ii
prevIdx = e
newLines += lines[prevIdx:]
if not dryRun and len(includeBlocks):
outTxt = "\n".join(newLines)
f = open(src, 'w')
f.write(outTxt)
f.close()
print "Done!"