forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateErrorTest.py
executable file
·193 lines (156 loc) · 5.09 KB
/
updateErrorTest.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import print_function
import os
import re
import sys
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
baseName = ""
headingLines = []
sourceLines = {}
lastSourceLine = 0
errors = []
def parseErrorLine(line):
m = re.match(r".*:(\d+):(\d+): (.*)", line)
if not m:
return None
return (int(m.group(1)), int(m.group(2)), m.group(3))
def parseSource(lines):
global lastSourceLine
i = 0
# Collect the commented out lines at the beginning.
while i < len(lines):
if not re.match(r"^\s*//|^$|^\/\*|^ \*", lines[i]):
break
headingLines.append(lines[i])
i += 1
# Collect the source, exlcuding // CHECK: and similar lines.
while i < len(lines):
if not re.match(r"^\s*//\s*(\S+):", lines[i]):
if lines[i].strip():
sourceLines[i + 1] = lines[i]
lastSourceLine = i + 1
i += 1
def parseErrors(lines):
i = 0
lastError = None
while i < len(lines):
errDesc = parseErrorLine(lines[i])
if errDesc:
if lastError:
errors.append(lastError)
lastError = (errDesc, [])
else:
if lastError:
lastError[1].append(lines[i])
else:
eprint("ignored error line", lines[i])
i += 1
if lastError:
errors.append(lastError)
list.sort(errors, key=lambda error: error[0][0])
def generateOutput(f):
lineNo = 0
for hl in headingLines:
print(hl, file=f)
lineNo += 1
prevErrorLine = lineNo
errorIndex = 0
while errorIndex < len(errors):
origErrorLine = errors[errorIndex][0][0]
# Detect all errors on the same line
nextErrorIndex = errorIndex + 1
while (
nextErrorIndex < len(errors)
and errors[nextErrorIndex][0][0] == origErrorLine
):
nextErrorIndex += 1
# Output all source up to and including the line of the error, but skip empty lines
# in the beginning and end.
# Skip empty lines in the beginning.
fromLine = prevErrorLine + 1
prevErrorLine = origErrorLine
while fromLine < origErrorLine and not sourceLines.get(fromLine, ""):
fromLine += 1
# Skip empty lines at the end.
toLine = origErrorLine
while toLine > fromLine and not sourceLines.get(toLine, ""):
toLine -= 1
# Output a separator line, but not the first time.
if errorIndex:
print("", file=f)
lineNo += 1
# Output the source lines.
for i in range(fromLine, toLine + 1):
print(sourceLines.get(i, ""), file=f)
lineNo += 1
newErrorLine = lineNo
while errorIndex < nextErrorIndex:
error = errors[errorIndex]
errorIndex += 1
print(
"//CHECK: {{.*}}%s:%d:%d: %s"
% (baseName, newErrorLine, error[0][1], error[0][2]),
file=f,
)
lineNo += 1
for eline in error[1]:
print("//CHECK-NEXT:", eline, file=f)
lineNo += 1
# Output the rest of the source.
fromLine = prevErrorLine + 1
while fromLine < lastSourceLine and not sourceLines.get(fromLine, ""):
fromLine += 1
if fromLine <= lastSourceLine:
print("", file=f)
lineNo += 1
for i in range(fromLine, lastSourceLine + 1):
print(sourceLines.get(i, ""), file=f)
lineNo += 1
def printHelp():
print("syntax: updateErrorTest.py [options] input-file")
print(" -h help")
print(" -i replace the input file inplace")
def main(argv):
argI = 1
inputName = ""
inplace = False
while argI < len(argv):
if argv[argI] == "-h":
printHelp()
sys.exit()
elif argv[argI] == "-i":
inplace = True
elif argv[argI].startswith("-"):
eprint("Invalid option '%s'. -h for help." % argv[argI])
sys.exit(1)
else:
if inputName:
eprint("More than one input-file specified")
sys.exit(1)
inputName = argv[argI]
argI += 1
if not inputName:
eprint("Input-file not specified. -h for help.")
sys.exit(1)
global baseName
baseName = os.path.basename(inputName)
# Read the source file
with open(inputName, "r") as f:
source = [x.strip("\n") for x in f.readlines()]
# Read the error output
errorOutput = [x.strip("\n") for x in sys.stdin.readlines()]
parseSource(source)
parseErrors(errorOutput)
eprint("Scanned %d source lines and %d errors" % (len(sourceLines), len(errors)))
if inplace:
with open(inputName, "w") as f:
generateOutput(f)
else:
generateOutput(sys.stdout)
if __name__ == "__main__":
main(sys.argv)