-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathparse_analyze_errors.py
574 lines (515 loc) · 22.3 KB
/
parse_analyze_errors.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# This script is used to parse the results of the Visual C++ /analyze feature.
# See the 'usage' section for details.
# Regular expression experimentation was done at http://www.pythonregex.com/
# The buildbot warning parser that looks at this script uses the default compile warning
# parser which is documented at http://buildbot.net/buildbot/docs/0.8.4/Compile.html
# The regex used is '.*warning[: ].*'. This means that any instance of 'warning:' or
# 'warning ' will be flagged as a warning. The check is case sensitive so Warning will
# not be flagged as a warning. This script remaps warning to 'wrning' in some places so
# that lists of fixed warnings or old warnings will not trigger warning detection.
# Similarly it remaps error to 'eror'.
# Typical warning messages might look like this:
# 2>d:\dota\src\tier1\bitbuf.cpp(1336): warning C6001: Using uninitialized memory 'retval': Lines: 1327, 1328, 1331, 1332, 1333, 1334, 1336
import re
import sys
import os
# Grab per-project configuration information from the analyzeconfig package
import analyzeconfig
ignorePaths = analyzeconfig.ignorePaths
alwaysFatalWarnings = analyzeconfig.alwaysFatalWarnings.keys()
fatalWhenNewWarnings = analyzeconfig.fatalWhenNewWarnings.keys()
remaps = analyzeconfig.remaps
informationalWarnings = analyzeconfig.informationalWarnings
lkgFilename = "analyzelkg.txt"
# This matches 0-3 digits and an optional '>' character. Some builds prefix the output
# with '10>' or something equivalent, but some builds do not.
prefixRePattern = r"\d?\d?\d?>?"
warningWithLinesRe = re.compile(prefixRePattern + r"(.*)\((\d+)\): warning C(\d{4,5})(.*)(: Lines:.*)")
warningRe = re.compile(prefixRePattern + r"(.*)\((\d+)\): warning C(\d{4,5})(.*)")
errorRe = re.compile(prefixRePattern + r"(.*)\((\d+)\): error C(\d{4,5})(.*)")
# For reparsing the keys that we use to store the parsed log data:
# The format for keys is like this:
# key = "%s %s in %s" % (type, warningNumber, filename)
parseKeyRe = re.compile(r"(.*) (\d{4,5}) in (.*)")
warningsToText = {
2719 : "Formal parameter with __declspec(align('n')) won't be aligned",
4005 : "Macro redefinition",
4100 : "Unreferenced formal parameter",
4189 : "Local variable is initialized but not referenced",
4245 : "Signed/unsigned mismatch",
4505 : "Unreferenced local function has been removed",
4611 : "interaction between '_setjmp' and C++ object destruction is non-portable",
4703 : "Potentially uninitialized local pointer variable used",
4789 : "Destination of memory copy is too small",
6001 : "Using uninitialized memory",
6029 : "Possible buffer overrun: use of unchecked value",
6053 : "Call to <function> may not zero-terminate string",
6054 : "String may not be zero-terminated",
6057 : "Buffer overrun due to number of characters/number of bytes mismatch",
6059 : "Incorrect length parameter",
6063 : "Missing string argument",
6064 : "Missing integer argument",
6066 : "Non-pointer passed as parameter when pointer is required",
6067 : "Parameter in call must be the address of the string",
6200 : "Index is out of valid index range for non-stack buffer",
6201 : "Out of range index",
6202 : "Buffer overrun for stack allocated variable in call to function",
6203 : "Buffer overrun for non-stack buffer",
6204 : "Possible buffer overrun: use of unchecked parameter",
6209 : "Using sizeof when a character count might be needed. Annotate with OUT_Z_CAP or its relatives",
6216 : "Compiler-inserted cast between semantically different integral types: a Boolean type to HRESULT",
6221 : "Implicit cast between semantically different integer types",
6219 : "Implicit cast between semantically different integer types",
6236 : "(<expression> || <non-zero constant>) is always a non-zero constant",
6244 : "Local declaration shadows declaration of same name in global scope",
6246 : "Local declaration shadows declaration of same name in outer scope",
6248 : "Setting a SECURITY_DESCRIPTOR's DACL to NULL will result in an unprotected object",
6258 : "Using TerminateThread does not allow proper thread clean up",
6262 : "Excessive stack usage in function",
6263 : "Using _alloca in a loop: this can quickly overflow stack",
6269 : "Possible incorrect order of operations: dereference ignored",
6270 : "Missing float argument to varargs function",
6271 : "Extra argument passed: parameter is not used by the format string",
6272 : "Non-float passed as argument <number> when float is required",
6273 : "Non-integer passed as a parameter when integer is required",
6277 : "NULL application name with an unquoted path results in a security vulnerability if the path contains spaces",
6278 : "Buffer is allocated with array new [], but deleted with scalar delete. Destructors will not be called",
6281 : "Incorrect order of operations: relational operators have higher precedence than bitwise operators",
6282 : "Incorrect operator: assignment of constant in Boolean context",
6283 : "Buffer is allocated with array new [], but deleted with scalar delete",
6284 : "Object passed as a parameter when string is required",
6286 : "(<non-zero constant> || <expression>) is always a non-zero constant.",
6287 : "Redundant code: the left and right sub-expressions are identical",
6290 : "Bitwise operation on logical result: ! has higher precedence than &. Use && or (!(x & y)) instead",
6293 : "Ill-defined for-loop: counts down from minimum",
6294 : "Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed",
6295 : "Ill-defined for-loop: Loop executed indefinitely",
6297 : "Arithmetic overflow: 32-bit value is shifted, then cast to 64-bit value",
6298 : "Using a read-only string <pointer> as a writable string argument",
6302 : "Format string mismatch: character string passed as parameter when wide character string is required",
6306 : "Incorrect call to 'fprintf*': consider using 'vfprintf*' which accepts a va_list as an argument",
6313 : "Incorrect operator: zero-valued flag cannot be tested with bitwise-and. Use an equality test to check for zero-valued flags",
6316 : "Incorrect operator: tested expression is constant and non-zero. Use bitwise-and to determine whether bits are set",
6318 : "Ill-defined __try/__except: use of the constant EXCEPTION_CONTINUE_SEARCH ",
6328 : "Wrong parameter type passed",
6330 : "'const char' passed as a parameter when 'unsigned char' is required",
6333 : "Invalid parameter: passing MEM_RELEASE and a non-zero dwSize parameter to 'VirtualFree' is not allowed",
6334 : "Sizeof operator applied to an expression with an operator might yield unexpected results",
6336 : "Arithmetic operator has precedence over question operator, use parentheses to clarify intent",
6385 : "Out of range read",
6386 : "Out of range write",
6522 : "Invalid size specification: expression must be of integral type",
6523 : "Invalid size specification: parameter 'size' not found",
28199 : "Using possibly uninitialized: The variable has had its address taken but no assignment to it has been discovered.",
}
def Cleanup(textline):
for sourcePath in remaps.keys():
if textline.startswith(sourcePath):
return textline.replace(sourcePath, remaps[sourcePath])
return textline
def ParseLog(logName):
# Create a dictionary in which to store the results
# The keys for the dictionary are "warning 6328 in c:\buildbot\..."
# This means that the count of keys is not particularly meaningful. The
# length of each data item tells you the total number of raw warnings, but
# some of those are duplicates (from the same file being compiled multiple
# times). The UniqueWarningCount function can be used to find the number of
# unique warnings in each record.
#
# This probably could have been designed better, perhaps by having the key
# include the line number. Probably not worth changing now.
result = {}
lines = open(logName).readlines()
# First look for compiler crashes. Joy.
if analyzeconfig.abortOnCompilerCrash:
compilerCrashes = 0
for line in lines:
# Look for signs that the compiler crashed and if it did then abort.
if line.count("Please choose the Technical Support command on the Visual C++") > 0:
compilerCrashes += 1
# Print a message in the warning format so that we can see how many times the
# compiler crashed on the buildbot waterfall page.
print "cl.exe(1): warning : internal compiler error, the compiler has crashed. Aborting code analysis."
# If the compiler crashes one or more times then give up.
if compilerCrashes > 0:
sys.exit(0)
warningCount = 0
ignoredCount = 0
namePrinted = False
for line in lines:
# Some of the paths in the output lines have slashes instead of backslashes.
line = line.replace("/", "\\")
ignored = False
for path in ignorePaths:
if line.count(path) > 0:
ignored = True
ignoredCount += 1
if ignored:
continue
filename = ""
type = "warning"
# Look for warnings with filename and line number. The groups returned
# are:
# file name
# line number
# warning number
# warning text
# optionally (warningWithLinesRe only) the lines implicated in the warning
warningMatch = warningWithLinesRe.match(line)
if not warningMatch:
warningMatch = warningRe.match(line)
if not warningMatch:
warningMatch = errorRe.match(line)
if warningMatch:
type = "error"
# We want to record how many errors of a particular type occur in a particular source
# file so we create a dictionary with [file name, warning number, isError] as the key.
if warningMatch:
filename = warningMatch.groups()[0]
lineNumber = warningMatch.groups()[1]
warningNumber = warningMatch.groups()[2]
warningText = warningMatch.groups()[3]
key = "%s %s in %s" % (type, warningNumber, filename)
data = "%s(%s): %s C%s%s" % (filename, lineNumber, type, warningNumber, warningText)
warningCount += 1
if key in result:
result[key] += [data]
else:
result[key] = [data]
elif line.find(": warning") >= 0:
pass # Ignore these warnings for now
elif line.find(": error ") >= 0:
if not namePrinted:
namePrinted = True
print " Unhandled errors found in '%s'" % logName
print " %s" % line.strip()
uniqueWarningCount = 0
uniqueInformationalCount = 0
for key in result.keys():
count = UniqueWarningCount(result[key])
match = parseKeyRe.match(key)
warningNumber = match.groups()[1]
if warningNumber in informationalWarnings:
uniqueInformationalCount += count
else:
uniqueWarningCount += count
print "%d lines of output in %s, %d issues found, %d ignored, plus %d informational." % (len(lines), logName, uniqueWarningCount, ignoredCount, uniqueInformationalCount)
print ""
return result
# The output of this script is filtered by buildbot as described at
# http://buildbot.net/buildbot/docs/0.8.4/Compile.html which means that the
# warning text is generated by running it through re.match(".*warning[: ].*")
# The e-mails are generated by running them through BuildAnalyze.createSummary
# in //steam/main/tools/buildbot/shared_helpers.py. The two sets of regexes
# should be kept compatible.
# The matching is case sensitive so Warning is not matched.
def PrintEntries(newEntries, prefix, sanitize):
printedAlready = {}
for newEntry in newEntries:
if not newEntry in printedAlready:
printedAlready[newEntry] = True
# When printing out the list of warnings that have been fixed
# replace ": warning" with a string that will not be
# recognized by the buildbot parser as a warning so that the
# break e-mails will only include new warnings.
# Yes, this is a hack. In the future a custom parser/filter
# for the e-mails would be better.
if sanitize:
newEntry = newEntry.replace(": warning", ": wrning")
newEntry = newEntry.replace(": error", ": eror")
print "%s%s" % (prefix, Cleanup(newEntry))
def UniqueWarningCount(warningRecord):
# Warnings may be encountered multiple times (header files included
# from many places, or source files compiled multiple times) and these
# are all added to the warning record. However, for determining
# unique warnings we want to filter out these duplicates.
alreadySeen = {}
count = 0
for warning in warningRecord:
if not warning in alreadySeen:
alreadySeen[warning] = True
count += 1
return count
def DumpNewWarnings(old, new, oldname, newname):
newWarningsFound = False
warningsFixed = False
fatalWarningsFound = False
warningCounts = {}
oldWarningCounts = {}
sampleWarnings = {}
for key in new.keys():
match = parseKeyRe.match(key)
warningNumber = int(match.groups()[1])
if warningNumber in alwaysFatalWarnings:
fatalWarningsFound = True
if warningNumber in warningCounts:
warningCounts[warningNumber] += UniqueWarningCount(new[key])
else:
warningCounts[warningNumber] = UniqueWarningCount(new[key])
sampleWarnings[warningNumber] = new[key][0]
if not key in old:
newWarningsFound = True
if warningNumber in fatalWhenNewWarnings:
fatalWarningsFound = True
for key in old.keys():
match = parseKeyRe.match(key)
warningNumber = int(match.groups()[1])
if warningNumber in oldWarningCounts:
oldWarningCounts[warningNumber] += UniqueWarningCount(old[key])
else:
oldWarningCounts[warningNumber] = UniqueWarningCount(old[key])
if not warningNumber in sampleWarnings:
sampleWarnings[warningNumber] = old[key][0]
if not key in new:
warningsFixed = True
if fatalWarningsFound:
errorCode = 10
elif newWarningsFound:
errorCode = 10
else:
errorCode = 0
# Make three passes through the warnings so that we group fatal, fatal-when-new, and
# new warnings together, with the fatal warnings first.
# The colons at the beginning of blank lines are so that buildbot's BuildAnalyze.createSummary
# will retain those lines.
for type in ["Fatal", "Fatal-when-new", "New"]:
fixing = "required"
if type == "New":
fixing = "optional"
message = "%s warning or warnings found. Fixing these is %s:\n:" % (type, fixing)
for key in new.keys():
newEntries = new[key]
match = parseKeyRe.match(key)
warningNumber = int(match.groups()[1])
if warningNumber in alwaysFatalWarnings:
if type == "Fatal":
print message
message = ":"
PrintEntries(newEntries, " ", False)
elif not key in old:
if warningNumber in fatalWhenNewWarnings:
if type == "Fatal-when-new":
print message
message = ":"
PrintEntries(newEntries, " ", False)
else:
if type == "New":
print message
message = ":"
PrintEntries(newEntries, " ", False)
# If message is short then that means it was printed and then assigned to a short
# string, which means some warnings of this type were printed, which means we should
# print a separator.
if len(message) < 2:
print ":\n:\n:\n:\n:"
if warningsFixed:
print "\n\n\n\n\nOld issues that have been fixed:"
for key in old.keys():
oldEntries = old[key]
if not key in new:
print "Warning fixed in %s:" % newname
print "%d times:" % len(oldEntries)
PrintEntries(oldEntries, " ", True)
print ""
else:
newEntries = new[key]
# Disable printing decreased warning counts -- too much noise.
if False and len(newEntries) < len(oldEntries):
print "Decreased wrning count:"
print " Old (%s):" % oldname
print " %d times:" % len(oldEntries)
PrintEntries(oldEntries, " ", True)
print " New (%s):" % newname
print " %d times:" % len(newEntries)
PrintEntries(newEntries, " ", True)
print ""
print "\n\n\n"
warningStats = []
for warningNumber in warningCounts.keys():
warningCount = warningCounts[warningNumber]
if warningNumber in oldWarningCounts:
warningDiff = warningCount - oldWarningCounts[warningNumber]
else:
warningDiff = warningCount
warningStats.append((warningCount, warningNumber, warningDiff))
for warningNumber in oldWarningCounts.keys():
if not warningNumber in warningCounts:
warningStats.append((0, warningNumber, -oldWarningCounts[warningNumber]))
warningStats.sort()
warningStats.reverse()
for warningStat in warningStats:
warningNumber = warningStat[1]
description = ""
if warningNumber in warningsToText:
description = ", %s" % warningsToText[warningNumber]
else:
# Replace warning/error with wrning/eror so that these warning summaries don't trigger the
# warning detection logic.
description = ", example: %s" % sampleWarnings[warningNumber].replace("warning", "wrning").replace("error", "eror")
print "%3d occurrences of C%d, changed %d%s" % (warningStat[0], warningStat[1], warningStat[2], description)
# Print a summary of all stack related warnings in the new data, regardless of whether they were in the old.
bigStackCulprits = {}
allocaCulprits = {}
# c:\src\simplify.cpp(1840): warning C6262: : Function uses '28708' bytes of stack: exceeds /analyze:stacksize'16384'. Consider moving some data to heap
stackUsedRe = re.compile("(.*): warning C6262: Function uses '(\d*)' .*")
print "\n\n\n"
print "Stack related summary:"
print "C6263: Using _alloca in a loop: this can quickly overflow stack"
bigStackCulprits = []
for key in new.keys():
# warning C6262: Function uses '400352' bytes of stack
# warning C6263: Using _alloca in a loop
stackMatch = parseKeyRe.match(key)
if stackMatch:
warningNumber = stackMatch.groups()[1]
if warningNumber == "6262":
#print "Found warning %s in %s" % (warningNumber, stackMatch.groups()[2])
entries = new[key]
printed = {}
for entry in entries:
if not entry in printed:
match = stackUsedRe.match(entry)
if match:
location = match.groups()[0]
stackBytes = int(match.groups()[1])
printed[entry] = True
bigStackCulprits.append((stackBytes, location))
elif warningNumber == "6263":
#print "Found warning %s in %s" % (warningNumber, stackMatch.groups()[2])
entries = new[key]
printed = {}
for entry in entries:
if not entry in printed:
print Cleanup(entry[:entry.find(": ")])
printed[entry] = True
print "\n\n"
print "C6262: Functions that use many bytes of stack"
bigStackCulprits.sort()
bigStackCulprits.reverse()
print "filename(linenumber): bytes"
# Print a sorted summary of functions using excessive stack. It would be tidier
# to print the size first (better alignment) but then the output can't be used
# in the Visual Studio output window to jump to the code in question.
# Get the lengths of all of the file names
lengths = []
for val in bigStackCulprits:
lengths.append(len(Cleanup(val[1])))
lengths.sort()
if len(lengths) > 0:
# Set the length at the 9xth percentile so that most of the sizes
# are lined up.
formatLength = lengths[int(len(lengths)*.97)]
formatString = "%%-%ds: %%7d" % formatLength
for val in bigStackCulprits:
print formatString % (Cleanup(val[1]), val[0])
# Print a list of all of the outstanding warnings
print "\n\n\n"
print "Outstanding warnings are:"
DumpWarnings(new, True)
return (errorCode, fatalWarningsFound)
def DumpWarnings(new, ignoreInformational):
filePrinted = {}
# If we just scan the dictionary then warnings will be grouped
# by warning-number-in-file, but different warning numbers from the
# same file will be scattered, and different files from the same
# directory will also be scattered.
# We really want warnings sorted by path name. To do that we scan
# through the dictionary and add all of the entries to a dictionary
# whose primary key is filename (path). Then we sort those keys.
warningsByFile = {}
for key in new.keys():
match = parseKeyRe.match(key)
type, warningNumber, filename = match.groups()
if filename in warningsByFile:
warningsByFile[filename].append(key)
else:
warningsByFile[filename] = [key]
filenames = warningsByFile.keys()
filenames.sort();
for filename in filenames:
for key in warningsByFile[filename]:
match = parseKeyRe.match(key)
warningNumber = match.groups()[1]
if ignoreInformational and warningNumber in informationalWarnings:
pass
else:
newEntries = new[key]
print "%d times:" % len(newEntries)
PrintEntries(newEntries, " ", True)
print ""
if ignoreInformational:
# Print the 6244 and 6246 warnings together in a group. We print
# them here so that they are sorted by file name.
print "\n\n\nVariable shadowing warnings"
for filename in filenames:
for key in warningsByFile[filename]:
match = parseKeyRe.match(key)
warningNumber = match.groups()[1]
if warningNumber == "6244" or warningNumber == "6246":
newEntries = new[key]
PrintEntries(newEntries, " ", True)
print ""
def GetLogFileName(arg):
# Special indicator for last-known-good. This means that the script
# should look for analysislkg.txt and extract a file name from it.
# Temporarily have "2" be equivalent to "lkg" to allow for a transition
# to the lkg model.
if arg == "lkg" or arg == "2":
try:
lines = open(lkgFilename).readlines()
if len(lines) > 0:
result = lines[0].strip()
print "LKG analysis results are in '%s'" % result
return result
else:
print "No data found in %s" % lkgFilename
except IOError:
print "Failed to open %s" % lkgFilename
arg = 2
try:
x = int(arg)
except:
return arg
if x <= 0:
print "Numerical arguments must be from 1 to numlogs (%s)" % arg
sys.exit(10)
basedir = r"."
dirEntries = os.listdir(basedir)
logRe = re.compile(r"analyze(.*)_cl_(\d+).txt");
logs = []
for entry in dirEntries:
if logRe.match(entry):
logs.append(entry)
# This will throw an exception if there aren't enough log files
# available.
newname = os.path.join(basedir, logs[-x])
return newname
if len(sys.argv) < 2:
print "Usage:"
print "To get a comparison between two error log files:"
print " Syntax: parseerrors newlogfile oldlogfile"
print "To get a summary of a single log file:"
print " Syntax: parseerrors logfile"
print "To get a summary of the two most recent log files:"
print " Syntax: parseerrors 1 2"
print "Log files can also be indicated by number where '1' is the"
print "most recent, '2' is second oldest, etc."
sys.exit(0)
newname = GetLogFileName(sys.argv[1])
resultnew = ParseLog(newname)
if len(sys.argv) >= 3:
oldname = GetLogFileName(sys.argv[2])
resultold = ParseLog(oldname)
result = DumpNewWarnings(resultold, resultnew, oldname, newname)
errorCode = result[0]
fatalWarningsFound = result[1]
if fatalWarningsFound == 0:
if analyzeconfig.updateLastKnownGood:
print "Updating last-known-good."
lkgOutput = open(lkgFilename, "wt")
lkgOutput.write(newname)
else:
print "Updating last-known-good is disabled."
sys.exit(errorCode)
else:
DumpWarnings(resultnew, False)