forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathCmpDriver
executable file
·215 lines (180 loc) · 6.19 KB
/
CmpDriver
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
"""
A simple utility that compares tool invocations and exit codes issued by
compiler drivers that support -### (e.g. gcc and clang).
"""
importsubprocess
defsplitArgs(s):
it=iter(s)
current=''
inQuote=False
forcinit:
ifc=='"':
ifinQuote:
inQuote=False
yieldcurrent+'"'
else:
inQuote=True
current='"'
elifinQuote:
ifc=='\\':
current+=c
current+=it.next()
else:
current+=c
elifnotc.isspace():
yieldc
definsertMinimumPadding(a, b, dist):
"""insertMinimumPadding(a,b) -> (a',b')
Return two lists of equal length, where some number of Nones have
been inserted into the shorter list such that sum(map(dist, a',
b')) is minimized.
Assumes dist(X, Y) -> int and non-negative.
"""
defcost(a, b):
returnsum(map(dist, a+ [None] * (len(b) -len(a)), b))
# Normalize so a is shortest.
iflen(b) <len(a):
b, a=insertMinimumPadding(b, a, dist)
returna,b
# For each None we have to insert...
foriinrange(len(b) -len(a)):
# For each position we could insert it...
current=cost(a, b)
best=None
forjinrange(len(a) +1):
a_0=a[:j] + [None] +a[j:]
candidate=cost(a_0, b)
ifbestisNoneorcandidate<best[0]:
best= (candidate, a_0, j)
a=best[1]
returna,b
classZipperDiff(object):
"""ZipperDiff - Simple (slow) diff only accommodating inserts."""
def__init__(self, a, b):
self.a=a
self.b=b
defdist(self, a, b):
returna!=b
defgetDiffs(self):
a,b=insertMinimumPadding(self.a, self.b, self.dist)
foraElt,bEltinzip(a,b):
ifself.dist(aElt, bElt):
yieldaElt,bElt
classDriverZipperDiff(ZipperDiff):
defisTempFile(self, filename):
iffilename[0] !='"'orfilename[-1] !='"':
returnFalse
return (filename.startswith('/tmp/', 1) or
filename.startswith('/var/', 1))
defdist(self, a, b):
ifaandbandself.isTempFile(a) andself.isTempFile(b):
return0
returnsuper(DriverZipperDiff, self).dist(a,b)
classCompileInfo:
def__init__(self, out, err, res):
self.commands= []
# Standard out isn't used for much.
self.stdout=out
self.stderr=''
# FIXME: Compare error messages as well.
forlninerr.split('\n'):
if (ln=='Using built-in specs.'or
ln.startswith('Target: ') or
ln.startswith('Configured with: ') or
ln.startswith('Thread model: ') or
ln.startswith('gcc version') or
ln.startswith('clang version')):
pass
elifln.strip().startswith('"'):
self.commands.append(list(splitArgs(ln)))
else:
self.stderr+=ln+'\n'
self.stderr=self.stderr.strip()
self.exitCode=res
defcaptureDriverInfo(cmd, args):
p=subprocess.Popen([cmd,'-###'] +args,
stdin=None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out,err=p.communicate()
res=p.wait()
returnCompileInfo(out,err,res)
defmain():
importos, sys
args=sys.argv[1:]
driverA=os.getenv('DRIVER_A') or'gcc'
driverB=os.getenv('DRIVER_B') or'clang'
infoA=captureDriverInfo(driverA, args)
infoB=captureDriverInfo(driverB, args)
differ=False
# Compare stdout.
ifinfoA.stdout!=infoB.stdout:
print'-- STDOUT DIFFERS -'
print'A OUTPUT: ',infoA.stdout
print'B OUTPUT: ',infoB.stdout
print
diff=ZipperDiff(infoA.stdout.split('\n'),
infoB.stdout.split('\n'))
fori,(aElt,bElt) inenumerate(diff.getDiffs()):
ifaEltisNone:
print'A missing: %s'%bElt
elifbEltisNone:
print'B missing: %s'%aElt
else:
print'mismatch: A: %s'%aElt
print' B: %s'%bElt
differ=True
# Compare stderr.
ifinfoA.stderr!=infoB.stderr:
print'-- STDERR DIFFERS -'
print'A STDERR: ',infoA.stderr
print'B STDERR: ',infoB.stderr
print
diff=ZipperDiff(infoA.stderr.split('\n'),
infoB.stderr.split('\n'))
fori,(aElt,bElt) inenumerate(diff.getDiffs()):
ifaEltisNone:
print'A missing: %s'%bElt
elifbEltisNone:
print'B missing: %s'%aElt
else:
print'mismatch: A: %s'%aElt
print' B: %s'%bElt
differ=True
# Compare commands.
fori,(a,b) inenumerate(map(None, infoA.commands, infoB.commands)):
ifaisNone:
print'A MISSING:',' '.join(b)
differ=True
continue
elifbisNone:
print'B MISSING:',' '.join(a)
differ=True
continue
diff=DriverZipperDiff(a,b)
diffs=list(diff.getDiffs())
ifdiffs:
print'-- COMMAND %d DIFFERS -'%i
print'A COMMAND:',' '.join(a)
print'B COMMAND:',' '.join(b)
print
fori,(aElt,bElt) inenumerate(diffs):
ifaEltisNone:
print'A missing: %s'%bElt
elifbEltisNone:
print'B missing: %s'%aElt
else:
print'mismatch: A: %s'%aElt
print' B: %s'%bElt
differ=True
# Compare result codes.
ifinfoA.exitCode!=infoB.exitCode:
print'-- EXIT CODES DIFFER -'
print'A: ',infoA.exitCode
print'B: ',infoB.exitCode
differ=True
ifdiffer:
sys.exit(1)
if__name__=='__main__':
main()