blob: 9019aab272437f6caf8256f96184d5da688fed95 [file] [log] [blame]
Tobin Ehlis35308dd2016-10-31 13:27:36 -06001#!/usr/bin/env python3
2# Copyright (c) 2015-2016 The Khronos Group Inc.
3# Copyright (c) 2015-2016 Valve Corporation
4# Copyright (c) 2015-2016 LunarG, Inc.
5# Copyright (c) 2015-2016 Google Inc.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# Author: Tobin Ehlis <tobine@google.com>
20
21import argparse
22import os
23import sys
24import platform
25
26# vk_validation_stats.py overview
27# This script is intended to generate statistics on the state of validation code
28# based on information parsed from the source files and the database file
29# Here's what it currently does:
30# 1. Parse vk_validation_error_database.txt to store claimed state of validation checks
31# 2. Parse vk_validation_error_messages.h to verify the actual checks in header vs. the
32# claimed state of the checks
33# 3. Parse source files to identify which checks are implemented and verify that this
34# exactly matches the list of checks claimed to be implemented in the database
35# 4. Parse test file(s) and verify that reported tests exist
36# 5. Report out stats on number of checks, implemented checks, and duplicated checks
37#
Tobin Ehlis20e32582016-12-05 14:50:03 -070038# If a mis-match is found during steps 2, 3, or 4, then the script exits w/ a non-zero error code
39# otherwise, the script will exit(0)
40#
Tobin Ehlis35308dd2016-10-31 13:27:36 -060041# TODO:
42# 1. Would also like to report out number of existing checks that don't yet use new, unique enum
43# 2. Could use notes to store custom fields (like TODO) and print those out here
44# 3. Update test code to check if tests use new, unique enums to check for errors instead of strings
45
46db_file = 'vk_validation_error_database.txt'
47layer_source_files = [
48'core_validation.cpp',
49'descriptor_sets.cpp',
50'parameter_validation.cpp',
51'object_tracker.cpp',
Tobin Ehlis3f0b2772016-11-18 16:56:15 -070052'image.cpp'
Tobin Ehlis35308dd2016-10-31 13:27:36 -060053]
54header_file = 'vk_validation_error_messages.h'
55# TODO : Don't hardcode linux path format if we want this to run on windows
56test_file = '../tests/layer_validation_tests.cpp'
57
58
59class ValidationDatabase:
60 def __init__(self, filename=db_file):
61 self.db_file = filename
62 self.delimiter = '~^~'
63 self.db_dict = {} # complete dict of all db values per error enum
64 # specialized data structs with slices of complete dict
65 self.db_implemented_enums = [] # list of all error enums claiming to be implemented in database file
66 self.db_enum_to_tests = {} # dict where enum is key to lookup list of tests implementing the enum
67 #self.src_implemented_enums
68 def read(self):
69 """Read a database file into internal data structures, format of each line is <enum><implemented Y|N?><testname><api><errormsg><notes>"""
70 #db_dict = {} # This is a simple db of just enum->errormsg, the same as is created from spec
71 #max_id = 0
72 with open(self.db_file, "r") as infile:
73 for line in infile:
74 line = line.strip()
75 if line.startswith('#') or '' == line:
76 continue
77 db_line = line.split(self.delimiter)
78 if len(db_line) != 6:
Tobin Ehlis027f3212016-12-09 12:15:26 -070079 print("ERROR: Bad database line doesn't have 6 elements: %s" % (line))
Tobin Ehlis35308dd2016-10-31 13:27:36 -060080 error_enum = db_line[0]
81 implemented = db_line[1]
82 testname = db_line[2]
83 api = db_line[3]
84 error_str = db_line[4]
85 note = db_line[5]
86 # Read complete database contents into our class var for later use
87 self.db_dict[error_enum] = {}
88 self.db_dict[error_enum]['check_implemented'] = implemented
89 self.db_dict[error_enum]['testname'] = testname
90 self.db_dict[error_enum]['api'] = api
91 self.db_dict[error_enum]['error_string'] = error_str
92 self.db_dict[error_enum]['note'] = note
93 # Now build custom data structs
94 if 'Y' == implemented:
95 self.db_implemented_enums.append(error_enum)
96 if testname.lower() not in ['unknown', 'none']:
97 self.db_enum_to_tests[error_enum] = testname.split(',')
98 #if len(self.db_enum_to_tests[error_enum]) > 1:
99 # print "Found check %s that has multiple tests: %s" % (error_enum, self.db_enum_to_tests[error_enum])
100 #else:
101 # print "Check %s has single test: %s" % (error_enum, self.db_enum_to_tests[error_enum])
102 #unique_id = int(db_line[0].split('_')[-1])
103 #if unique_id > max_id:
104 # max_id = unique_id
105 #print "Found %d total enums in database" % (len(self.db_dict.keys()))
106 #print "Found %d enums claiming to be implemented in source" % (len(self.db_implemented_enums))
107 #print "Found %d enums claiming to have tests implemented" % (len(self.db_enum_to_tests.keys()))
108
109class ValidationHeader:
110 def __init__(self, filename=header_file):
111 self.filename = header_file
112 self.enums = []
113 def read(self):
114 """Read unique error enum header file into internal data structures"""
115 grab_enums = False
116 with open(self.filename, "r") as infile:
117 for line in infile:
118 line = line.strip()
119 if 'enum UNIQUE_VALIDATION_ERROR_CODE {' in line:
120 grab_enums = True
121 continue
122 if grab_enums:
123 if 'VALIDATION_ERROR_MAX_ENUM' in line:
124 grab_enums = False
125 break # done
Tobin Ehlisf53eac32016-12-09 14:10:47 -0700126 elif 'VALIDATION_ERROR_UNDEFINED' in line:
127 continue
128 elif 'VALIDATION_ERROR_' in line:
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600129 enum = line.split(' = ')[0]
130 self.enums.append(enum)
131 #print "Found %d error enums. First is %s and last is %s." % (len(self.enums), self.enums[0], self.enums[-1])
132
133class ValidationSource:
134 def __init__(self, source_file_list):
135 self.source_files = source_file_list
136 self.enum_count_dict = {} # dict of enum values to the count of how much they're used
Tobin Ehlis3d9dd942016-11-23 13:08:01 -0700137 # 1790 is a special case that provides an exception when an extension is enabled. No specific error is flagged, but the exception is handled so add it here
138 self.enum_count_dict['VALIDATION_ERROR_01790'] = 1
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600139 def parse(self):
140 duplicate_checks = 0
141 for sf in self.source_files:
142 with open(sf) as f:
143 for line in f:
144 if True in [line.strip().startswith(comment) for comment in ['//', '/*']]:
145 continue
146 # Find enums
147 #if 'VALIDATION_ERROR_' in line and True not in [ignore in line for ignore in ['[VALIDATION_ERROR_', 'UNIQUE_VALIDATION_ERROR_CODE']]:
Tobin Ehlisf53eac32016-12-09 14:10:47 -0700148 if ' VALIDATION_ERROR_' in line:
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600149 # Need to isolate the validation error enum
150 #print("Line has check:%s" % (line))
151 line_list = line.split()
Tobin Ehlis928742e2016-12-09 17:11:13 -0700152 enum_list = []
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600153 for str in line_list:
Tobin Ehlisf53eac32016-12-09 14:10:47 -0700154 if 'VALIDATION_ERROR_' in str and True not in [ignore_str in str for ignore_str in ['[VALIDATION_ERROR_', 'VALIDATION_ERROR_UNDEFINED', 'UNIQUE_VALIDATION_ERROR_CODE']]:
Tobin Ehlis928742e2016-12-09 17:11:13 -0700155 enum_list.append(str.strip(',);'))
156 #break
157 for enum in enum_list:
158 if enum != '':
159 if enum not in self.enum_count_dict:
160 self.enum_count_dict[enum] = 1
161 #print "Found enum %s implemented for first time in file %s" % (enum, sf)
162 else:
163 self.enum_count_dict[enum] = self.enum_count_dict[enum] + 1
164 #print "Found enum %s implemented for %d time in file %s" % (enum, self.enum_count_dict[enum], sf)
165 duplicate_checks = duplicate_checks + 1
166 #else:
167 #print("Didn't find actual check in line:%s" % (line))
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600168 #print "Found %d unique implemented checks and %d are duplicated at least once" % (len(self.enum_count_dict.keys()), duplicate_checks)
169
170# Class to parse the validation layer test source and store testnames
171# TODO: Enhance class to detect use of unique error enums in the test
172class TestParser:
173 def __init__(self, test_file_list, test_group_name=['VkLayerTest', 'VkPositiveLayerTest', 'VkWsiEnabledLayerTest']):
174 self.test_files = test_file_list
175 self.tests_set = set()
176 self.test_trigger_txt_list = []
177 for tg in test_group_name:
178 self.test_trigger_txt_list.append('TEST_F(%s' % tg)
179 #print('Test trigger test list: %s' % (self.test_trigger_txt_list))
180
181 # Parse test files into internal data struct
182 def parse(self):
183 # For each test file, parse test names into set
184 grab_next_line = False # handle testname on separate line than wildcard
185 for test_file in self.test_files:
186 with open(test_file) as tf:
187 for line in tf:
188 if True in [line.strip().startswith(comment) for comment in ['//', '/*']]:
189 continue
190
191 if True in [ttt in line for ttt in self.test_trigger_txt_list]:
192 #print('Test wildcard in line: %s' % (line))
193 testname = line.split(',')[-1]
194 testname = testname.strip().strip(' {)')
195 #print('Inserting test: "%s"' % (testname))
196 if ('' == testname):
197 grab_next_line = True
198 continue
199 self.tests_set.add(testname)
200 if grab_next_line: # test name on its own line
201 grab_next_line = False
202 testname = testname.strip().strip(' {)')
203 self.tests_set.add(testname)
204
205# Little helper class for coloring cmd line output
206class bcolors:
207
208 def __init__(self):
209 self.GREEN = '\033[0;32m'
210 self.RED = '\033[0;31m'
211 self.YELLOW = '\033[1;33m'
212 self.ENDC = '\033[0m'
213 if 'Linux' != platform.system():
214 self.GREEN = ''
215 self.RED = ''
216 self.YELLOW = ''
217 self.ENDC = ''
218
219 def green(self):
220 return self.GREEN
221
222 def red(self):
223 return self.RED
224
225 def yellow(self):
226 return self.YELLOW
227
228 def endc(self):
229 return self.ENDC
230
231# Class to parse the validation layer test source and store testnames
232class TestParser:
233 def __init__(self, test_file_list, test_group_name=['VkLayerTest', 'VkPositiveLayerTest', 'VkWsiEnabledLayerTest']):
234 self.test_files = test_file_list
235 self.tests_set = set()
236 self.test_trigger_txt_list = []
237 for tg in test_group_name:
238 self.test_trigger_txt_list.append('TEST_F(%s' % tg)
239 #print('Test trigger test list: %s' % (self.test_trigger_txt_list))
240
241 # Parse test files into internal data struct
242 def parse(self):
243 # For each test file, parse test names into set
244 grab_next_line = False # handle testname on separate line than wildcard
245 for test_file in self.test_files:
246 with open(test_file) as tf:
247 for line in tf:
248 if True in [line.strip().startswith(comment) for comment in ['//', '/*']]:
249 continue
250
251 if True in [ttt in line for ttt in self.test_trigger_txt_list]:
252 #print('Test wildcard in line: %s' % (line))
253 testname = line.split(',')[-1]
254 testname = testname.strip().strip(' {)')
255 #print('Inserting test: "%s"' % (testname))
256 if ('' == testname):
257 grab_next_line = True
258 continue
259 self.tests_set.add(testname)
260 if grab_next_line: # test name on its own line
261 grab_next_line = False
262 testname = testname.strip().strip(' {)')
263 self.tests_set.add(testname)
264
265def main(argv=None):
Tobin Ehlis20e32582016-12-05 14:50:03 -0700266 result = 0 # Non-zero result indicates an error case
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600267 # parse db
268 val_db = ValidationDatabase()
269 val_db.read()
270 # parse header
271 val_header = ValidationHeader()
272 val_header.read()
273 # Create parser for layer files
274 val_source = ValidationSource(layer_source_files)
275 val_source.parse()
276 # Parse test files
277 test_parser = TestParser([test_file, ])
278 test_parser.parse()
279
280 # Process stats - Just doing this inline in main, could make a fancy class to handle
281 # all the processing of data and then get results from that
282 txt_color = bcolors()
283 print("Validation Statistics")
284 # First give number of checks in db & header and report any discrepancies
285 db_enums = len(val_db.db_dict.keys())
286 hdr_enums = len(val_header.enums)
287 print(" Database file includes %d unique checks" % (db_enums))
288 print(" Header file declares %d unique checks" % (hdr_enums))
289 tmp_db_dict = val_db.db_dict
290 db_missing = []
291 for enum in val_header.enums:
292 if not tmp_db_dict.pop(enum, False):
293 db_missing.append(enum)
294 if db_enums == hdr_enums and len(db_missing) == 0 and len(tmp_db_dict.keys()) == 0:
295 print(txt_color.green() + " Database and Header match, GREAT!" + txt_color.endc())
296 else:
297 print(txt_color.red() + " Uh oh, Database doesn't match Header :(" + txt_color.endc())
Tobin Ehlis20e32582016-12-05 14:50:03 -0700298 result = 1
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600299 if len(db_missing) != 0:
300 print(txt_color.red() + " The following checks are in header but missing from database:" + txt_color.endc())
301 for missing_enum in db_missing:
302 print(txt_color.red() + " %s" % (missing_enum) + txt_color.endc())
303 if len(tmp_db_dict.keys()) != 0:
304 print(txt_color.red() + " The following checks are in database but haven't been declared in the header:" + txt_color.endc())
305 for extra_enum in tmp_db_dict:
306 print(txt_color.red() + " %s" % (extra_enum) + txt_color.endc())
307 # Report out claimed implemented checks vs. found actual implemented checks
308 imp_not_found = [] # Checks claimed to implemented in DB file but no source found
309 imp_not_claimed = [] # Checks found implemented but not claimed to be in DB
310 multiple_uses = False # Flag if any enums are used multiple times
311 for db_imp in val_db.db_implemented_enums:
312 if db_imp not in val_source.enum_count_dict:
313 imp_not_found.append(db_imp)
314 for src_enum in val_source.enum_count_dict:
315 if val_source.enum_count_dict[src_enum] > 1:
316 multiple_uses = True
317 if src_enum not in val_db.db_implemented_enums:
318 imp_not_claimed.append(src_enum)
319 print(" Database file claims that %d checks (%s) are implemented in source." % (len(val_db.db_implemented_enums), "{0:.0f}%".format(float(len(val_db.db_implemented_enums))/db_enums * 100)))
320 if len(imp_not_found) == 0 and len(imp_not_claimed) == 0:
321 print(txt_color.green() + " All claimed Database implemented checks have been found in source, and no source checks aren't claimed in Database, GREAT!" + txt_color.endc())
322 else:
Tobin Ehlis20e32582016-12-05 14:50:03 -0700323 result = 1
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600324 print(txt_color.red() + " Uh oh, Database claimed implemented don't match Source :(" + txt_color.endc())
325 if len(imp_not_found) != 0:
Tobin Ehlis3f0b2772016-11-18 16:56:15 -0700326 print(txt_color.red() + " The following %d checks are claimed to be implemented in Database, but weren't found in source:" % (len(imp_not_found)) + txt_color.endc())
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600327 for not_imp_enum in imp_not_found:
328 print(txt_color.red() + " %s" % (not_imp_enum) + txt_color.endc())
329 if len(imp_not_claimed) != 0:
330 print(txt_color.red() + " The following checks are implemented in source, but not claimed to be in Database:" + txt_color.endc())
331 for imp_enum in imp_not_claimed:
332 print(txt_color.red() + " %s" % (imp_enum) + txt_color.endc())
333 if multiple_uses:
334 print(txt_color.yellow() + " Note that some checks are used multiple times. These may be good candidates for new valid usage spec language." + txt_color.endc())
335 print(txt_color.yellow() + " Here is a list of each check used multiple times with its number of uses:" + txt_color.endc())
336 for enum in val_source.enum_count_dict:
337 if val_source.enum_count_dict[enum] > 1:
338 print(txt_color.yellow() + " %s: %d" % (enum, val_source.enum_count_dict[enum]) + txt_color.endc())
339 # Now check that tests claimed to be implemented are actual test names
340 bad_testnames = []
341 for enum in val_db.db_enum_to_tests:
342 for testname in val_db.db_enum_to_tests[enum]:
343 if testname not in test_parser.tests_set:
344 bad_testnames.append(testname)
345 print(" Database file claims that %d checks have tests written." % len(val_db.db_enum_to_tests))
346 if len(bad_testnames) == 0:
347 print(txt_color.green() + " All claimed tests have valid names. That's good!" + txt_color.endc())
348 else:
349 print(txt_color.red() + " The following testnames in Database appear to be invalid:")
Tobin Ehlis20e32582016-12-05 14:50:03 -0700350 result = 1
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600351 for bt in bad_testnames:
Tobin Ehlisb04c2c62016-11-21 15:51:45 -0700352 print(txt_color.red() + " %s" % (bt) + txt_color.endc())
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600353
Tobin Ehlis20e32582016-12-05 14:50:03 -0700354 return result
Tobin Ehlis35308dd2016-10-31 13:27:36 -0600355
356if __name__ == "__main__":
357 sys.exit(main())
358