blob: 671bd2767796ccd16cb4e299361a100663fe1825 [file] [log] [blame]
Tobin Ehlis5ade0692016-10-05 17:18:15 -06001#!/usr/bin/python3 -i
2
3import sys
4import xml.etree.ElementTree as etree
5import urllib2
Tobin Ehlis5ade0692016-10-05 17:18:15 -06006
7#############################
8# spec.py script
9#
10# Overview - this script is intended to generate validation error codes and message strings from the xhtml version of
11# the specification. In addition to generating the header file, it provides a number of corrollary services to aid in
12# generating/updating the header.
13#
14# Ideal flow - Not there currently, but the ideal flow for this script would be that you run the script, it pulls the
15# latest spec, compares it to the current set of generated error codes, and makes any updates as needed
16#
17# Current flow - the current flow acheives all of the ideal flow goals, but with more steps than are desired
18# 1. Get the spec - right now spec has to be manually generated or pulled from the web
19# 2. Generate header from spec - This is done in a single command line
20# 3. Generate database file from spec - Can be done along with step #2 above, the database file contains a list of
21# all error enums and message strings, along with some other info on if those errors are implemented/tested
22# 4. Update header using a given database file as the root and a new spec file as goal - This makes sure that existing
23# errors keep the same enum identifier while also making sure that new errors get a unique_id that continues on
24# from the end of the previous highest unique_id.
25#
26# TODO:
27# 1. Improve string matching to add more automation for figuring out which messages are changed vs. completely new
Tobin Ehlis5ade0692016-10-05 17:18:15 -060028#
29#############################
30
31
32spec_filename = "vkspec.html" # can override w/ '-spec <filename>' option
33out_filename = "vk_validation_error_messages.h" # can override w/ '-out <filename>' option
34db_filename = "vk_validation_error_database.txt" # can override w/ '-gendb <filename>' option
35gen_db = False # set to True when '-gendb <filename>' option provided
36spec_compare = False # set to True with '-compare <db_filename>' option
37# This is the root spec link that is used in error messages to point users to spec sections
Tobin Ehlisbd0a9c62016-10-14 18:06:16 -060038#old_spec_url = "https://www.khronos.org/registry/vulkan/specs/1.0/xhtml/vkspec.html"
39spec_url = "https://www.khronos.org/registry/vulkan/specs/1.0-extensions/xhtml/vkspec.html"
Tobin Ehlis5ade0692016-10-05 17:18:15 -060040# After the custom validation error message, this is the prefix for the standard message that includes the
41# spec valid usage language as well as the link to nearest section of spec to that language
42error_msg_prefix = "For more information refer to Vulkan Spec Section "
43ns = {'ns': 'http://www.w3.org/1999/xhtml'}
Mark Lobodzinski629d47b2016-10-18 13:34:58 -060044validation_error_enum_name = "VALIDATION_ERROR_"
Tobin Ehlis5ade0692016-10-05 17:18:15 -060045# Dict of new enum values that should be forced to remap to old handles, explicitly set by -remap option
46remap_dict = {}
47
48def printHelp():
49 print "Usage: python spec.py [-spec <specfile.html>] [-out <headerfile.h>] [-gendb <databasefile.txt>] [-compare <databasefile.txt>] [-update] [-remap <new_id-old_id,count>] [-help]"
50 print "\n Default script behavior is to parse the specfile and generate a header of unique error enums and corresponding error messages based on the specfile.\n"
51 print " Default specfile is from online at %s" % (spec_url)
52 print " Default headerfile is %s" % (out_filename)
53 print " Default databasefile is %s" % (db_filename)
54 print "\nIf '-gendb' option is specified then a database file is generated to default file or <databasefile.txt> if supplied. The database file stores"
55 print " the list of enums and their error messages."
56 print "\nIf '-compare' option is specified then the given database file will be read in as the baseline for generating the new specfile"
57 print "\nIf '-update' option is specified this triggers the master flow to automate updating header and database files using default db file as baseline"
58 print " and online spec file as the latest. The default header and database files will be updated in-place for review and commit to the git repo."
59 print "\nIf '-remap' option is specified it supplies forced remapping from new enum ids to old enum ids. This should only be specified along with -update"
60 print " option. Starting at newid and remapping to oldid, count ids will be remapped. Default count is '1' and use ':' to specify multiple remappings."
61
62class Specification:
63 def __init__(self):
64 self.tree = None
Tobin Ehlise7560e72016-10-19 15:59:38 -060065 self.val_error_dict = {} # string for enum is key that references 'error_msg' and 'api'
Tobin Ehlis5ade0692016-10-05 17:18:15 -060066 self.error_db_dict = {} # dict of previous error values read in from database file
67 self.delimiter = '~^~' # delimiter for db file
Tobin Ehlis2a176b12017-01-11 16:18:20 -070068 self.implicit_count = 0
Tobin Ehlis5ade0692016-10-05 17:18:15 -060069 self.copyright = """/* THIS FILE IS GENERATED. DO NOT EDIT. */
70
71/*
72 * Vulkan
73 *
74 * Copyright (c) 2016 Google Inc.
Mark Lobodzinski629d47b2016-10-18 13:34:58 -060075 * Copyright (c) 2016 LunarG, Inc.
Tobin Ehlis5ade0692016-10-05 17:18:15 -060076 *
77 * Licensed under the Apache License, Version 2.0 (the "License");
78 * you may not use this file except in compliance with the License.
79 * You may obtain a copy of the License at
80 *
81 * http://www.apache.org/licenses/LICENSE-2.0
82 *
83 * Unless required by applicable law or agreed to in writing, software
84 * distributed under the License is distributed on an "AS IS" BASIS,
85 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
86 * See the License for the specific language governing permissions and
87 * limitations under the License.
88 *
89 * Author: Tobin Ehlis <tobine@google.com>
90 */"""
91 def _checkInternetSpec(self):
92 """Verify that we can access the spec online"""
93 try:
94 online = urllib2.urlopen(spec_url,timeout=1)
95 return True
96 except urllib2.URLError as err:
97 return False
98 return False
99 def loadFile(self, online=True, spec_file=spec_filename):
100 """Load an API registry XML file into a Registry object and parse it"""
101 # Check if spec URL is available
102 if (online and self._checkInternetSpec()):
103 print "Using spec from online at %s" % (spec_url)
104 self.tree = etree.parse(urllib2.urlopen(spec_url))
105 else:
106 print "Using local spec %s" % (spec_file)
107 self.tree = etree.parse(spec_file)
108 #self.tree.write("tree_output.xhtml")
109 #self.tree = etree.parse("tree_output.xhtml")
110 self.parseTree()
111 def updateDict(self, updated_dict):
112 """Assign internal dict to use updated_dict"""
113 self.val_error_dict = updated_dict
114 def parseTree(self):
115 """Parse the registry Element, once created"""
116 print "Parsing spec file..."
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600117 unique_enum_id = 0
118 self.root = self.tree.getroot()
119 #print "ROOT: %s" % self.root
120 prev_heading = '' # Last seen section heading or sub-heading
121 prev_link = '' # Last seen link id within the spec
Tobin Ehlise7560e72016-10-19 15:59:38 -0600122 api_function = '' # API call that a check appears under
Tobin Ehlis85008cd2016-10-19 15:32:35 -0600123 error_strings = set() # Flag any exact duplicate error strings and skip them
Tobin Ehlis2a176b12017-01-11 16:18:20 -0700124 implicit_count = 0
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600125 for tag in self.root.iter(): # iterate down tree
126 # Grab most recent section heading and link
Tobin Ehlisce1e56f2017-01-25 12:42:55 -0800127 if tag.tag in ['h2', 'h3', 'h4']:
128 #if tag.get('class') != 'title':
129 # continue
130 print "Found heading %s" % (tag.tag)
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600131 prev_heading = "".join(tag.itertext())
132 # Insert a space between heading number & title
133 sh_list = prev_heading.rsplit('.', 1)
134 prev_heading = '. '.join(sh_list)
Tobin Ehlisce1e56f2017-01-25 12:42:55 -0800135 prev_link = tag.get('id')
136 print "Set prev_heading %s to have link of %s" % (prev_heading.encode("ascii", "ignore"), prev_link.encode("ascii", "ignore"))
137 elif tag.tag == 'a': # grab any intermediate links
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600138 if tag.get('id') != None:
139 prev_link = tag.get('id')
Tobin Ehlis16b159c2016-10-25 06:33:27 -0600140 #print "Updated prev link to %s" % (prev_link)
Tobin Ehlisce1e56f2017-01-25 12:42:55 -0800141 elif tag.tag == 'div' and tag.get('class') == 'listingblock':
Tobin Ehlise7560e72016-10-19 15:59:38 -0600142 # Check and see if this is API function
143 code_text = "".join(tag.itertext()).replace('\n', '')
144 code_text_list = code_text.split()
145 if len(code_text_list) > 1 and code_text_list[1].startswith('vk'):
146 api_function = code_text_list[1].strip('(')
Tobin Ehlisce1e56f2017-01-25 12:42:55 -0800147 print "Found API function: %s" % (api_function)
148 #elif tag.tag == '{http://www.w3.org/1999/xhtml}div' and tag.get('class') == 'sidebar':
149 elif tag.tag == 'div' and tag.get('class') == 'content':
Tobin Ehlis69ebddf2016-10-18 15:55:07 -0600150 # parse down sidebar to check for valid usage cases
151 valid_usage = False
Tobin Ehlis2a176b12017-01-11 16:18:20 -0700152 implicit = False
Tobin Ehlis69ebddf2016-10-18 15:55:07 -0600153 for elem in tag.iter():
Tobin Ehlisce1e56f2017-01-25 12:42:55 -0800154 if elem.tag == 'div' and None != elem.text and 'Valid Usage' in elem.text:
Tobin Ehlis69ebddf2016-10-18 15:55:07 -0600155 valid_usage = True
Tobin Ehlis2a176b12017-01-11 16:18:20 -0700156 if '(Implicit)' in elem.text:
157 implicit = True
158 else:
159 implicit = False
Tobin Ehlisce1e56f2017-01-25 12:42:55 -0800160 elif valid_usage and elem.tag == 'li': # grab actual valid usage requirements
Tobin Ehlis69ebddf2016-10-18 15:55:07 -0600161 error_msg_str = "%s '%s' which states '%s' (%s#%s)" % (error_msg_prefix, prev_heading, "".join(elem.itertext()).replace('\n', ''), spec_url, prev_link)
162 # Some txt has multiple spaces so split on whitespace and join w/ single space
163 error_msg_str = " ".join(error_msg_str.split())
Tobin Ehlis85008cd2016-10-19 15:32:35 -0600164 if error_msg_str in error_strings:
165 print "WARNING: SKIPPING adding repeat entry for string. Please review spec and file issue as appropriate. Repeat string is: %s" % (error_msg_str)
166 else:
167 error_strings.add(error_msg_str)
168 enum_str = "%s%05d" % (validation_error_enum_name, unique_enum_id)
169 # TODO : '\' chars in spec error messages are most likely bad spec txt that needs to be updated
Tobin Ehlise7560e72016-10-19 15:59:38 -0600170 self.val_error_dict[enum_str] = {}
171 self.val_error_dict[enum_str]['error_msg'] = error_msg_str.encode("ascii", "ignore").replace("\\", "/")
172 self.val_error_dict[enum_str]['api'] = api_function
Tobin Ehlis2a176b12017-01-11 16:18:20 -0700173 self.val_error_dict[enum_str]['implicit'] = False
174 if implicit:
175 self.val_error_dict[enum_str]['implicit'] = True
176 self.implicit_count = self.implicit_count + 1
Tobin Ehlis85008cd2016-10-19 15:32:35 -0600177 unique_enum_id = unique_enum_id + 1
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600178 #print "Validation Error Dict has a total of %d unique errors and contents are:\n%s" % (unique_enum_id, self.val_error_dict)
179 def genHeader(self, header_file):
180 """Generate a header file based on the contents of a parsed spec"""
181 print "Generating header %s..." % (header_file)
182 file_contents = []
183 file_contents.append(self.copyright)
184 file_contents.append('\n#pragma once')
Tobin Ehlisbf98b692016-10-06 12:58:06 -0600185 file_contents.append('#include <unordered_map>')
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600186 file_contents.append('\n// enum values for unique validation error codes')
187 file_contents.append('// Corresponding validation error message for each enum is given in the mapping table below')
188 file_contents.append('// When a given error occurs, these enum values should be passed to the as the messageCode')
189 file_contents.append('// parameter to the PFN_vkDebugReportCallbackEXT function')
Tobin Ehlis387fd632016-12-08 13:32:05 -0700190 enum_decl = ['enum UNIQUE_VALIDATION_ERROR_CODE {\n VALIDATION_ERROR_UNDEFINED = -1,']
Tobin Ehlisbf98b692016-10-06 12:58:06 -0600191 error_string_map = ['static std::unordered_map<int, char const *const> validation_error_map{']
Tobin Ehlisce1e56f2017-01-25 12:42:55 -0800192 enum_value = 0
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600193 for enum in sorted(self.val_error_dict):
194 #print "Header enum is %s" % (enum)
Tobin Ehlisce1e56f2017-01-25 12:42:55 -0800195 enum_value = int(enum.split('_')[-1])
196 enum_decl.append(' %s = %d,' % (enum, enum_value))
Tobin Ehlise7560e72016-10-19 15:59:38 -0600197 error_string_map.append(' {%s, "%s"},' % (enum, self.val_error_dict[enum]['error_msg']))
Tobin Ehlisce1e56f2017-01-25 12:42:55 -0800198 enum_decl.append(' %sMAX_ENUM = %d,' % (validation_error_enum_name, enum_value + 1))
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600199 enum_decl.append('};')
Tobin Ehlise7560e72016-10-19 15:59:38 -0600200 error_string_map.append('};\n')
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600201 file_contents.extend(enum_decl)
202 file_contents.append('\n// Mapping from unique validation error enum to the corresponding error message')
203 file_contents.append('// The error message should be appended to the end of a custom error message that is passed')
204 file_contents.append('// as the pMessage parameter to the PFN_vkDebugReportCallbackEXT function')
205 file_contents.extend(error_string_map)
206 #print "File contents: %s" % (file_contents)
207 with open(header_file, "w") as outfile:
208 outfile.write("\n".join(file_contents))
209 def analyze(self):
210 """Print out some stats on the valid usage dict"""
211 # Create dict for # of occurences of identical strings
212 str_count_dict = {}
213 unique_id_count = 0
214 for enum in self.val_error_dict:
Tobin Ehlise7560e72016-10-19 15:59:38 -0600215 err_str = self.val_error_dict[enum]['error_msg']
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600216 if err_str in str_count_dict:
Tobin Ehlis69ebddf2016-10-18 15:55:07 -0600217 print "Found repeat error string"
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600218 str_count_dict[err_str] = str_count_dict[err_str] + 1
219 else:
220 str_count_dict[err_str] = 1
221 unique_id_count = unique_id_count + 1
222 print "Processed %d unique_ids" % (unique_id_count)
223 repeat_string = 0
224 for es in str_count_dict:
225 if str_count_dict[es] > 1:
226 repeat_string = repeat_string + 1
Tobin Ehlis69ebddf2016-10-18 15:55:07 -0600227 print "String '%s' repeated %d times" % (es, repeat_string)
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600228 print "Found %d repeat strings" % (repeat_string)
Tobin Ehlis2a176b12017-01-11 16:18:20 -0700229 print "Found %d implicit checks" % (self.implicit_count)
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600230 def genDB(self, db_file):
231 """Generate a database of check_enum, check_coded?, testname, error_string"""
232 db_lines = []
233 # Write header for database file
234 db_lines.append("# This is a database file with validation error check information")
235 db_lines.append("# Comments are denoted with '#' char")
236 db_lines.append("# The format of the lines is:")
Tobin Ehlise7560e72016-10-19 15:59:38 -0600237 db_lines.append("# <error_enum>%s<check_implemented>%s<testname>%s<api>%s<errormsg>%s<note>" % (self.delimiter, self.delimiter, self.delimiter, self.delimiter, self.delimiter))
Mark Lobodzinski629d47b2016-10-18 13:34:58 -0600238 db_lines.append("# error_enum: Unique error enum for this check of format %s<uniqueid>" % validation_error_enum_name)
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600239 db_lines.append("# check_implemented: 'Y' if check has been implemented in layers, 'U' for unknown, or 'N' for not implemented")
240 db_lines.append("# testname: Name of validation test for this check, 'Unknown' for unknown, or 'None' if not implmented")
Tobin Ehlise7560e72016-10-19 15:59:38 -0600241 db_lines.append("# api: Vulkan API function that this check is related to")
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600242 db_lines.append("# errormsg: The unique error message for this check that includes spec language and link")
Tobin Ehlise7560e72016-10-19 15:59:38 -0600243 db_lines.append("# note: Free txt field with any custom notes related to the check in question")
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600244 for enum in sorted(self.val_error_dict):
245 # Default to unknown if check or test are implemented, then update below if appropriate
246 implemented = 'U'
247 testname = 'Unknown'
Tobin Ehlis70980c02016-10-25 14:00:20 -0600248 note = ''
Tobin Ehlis2a176b12017-01-11 16:18:20 -0700249 implicit = self.val_error_dict[enum]['implicit']
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600250 # If we have an existing db entry for this enum, use its implemented/testname values
251 if enum in self.error_db_dict:
252 implemented = self.error_db_dict[enum]['check_implemented']
253 testname = self.error_db_dict[enum]['testname']
Tobin Ehlis70980c02016-10-25 14:00:20 -0600254 note = self.error_db_dict[enum]['note']
Tobin Ehlis2a176b12017-01-11 16:18:20 -0700255 if implicit and 'implicit' not in note: # add implicit note
256 if '' != note:
257 note = "implicit, %s" % (note)
258 else:
259 note = "implicit"
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600260 #print "delimiter: %s, id: %s, str: %s" % (self.delimiter, enum, self.val_error_dict[enum])
261 # No existing entry so default to N for implemented and None for testname
Tobin Ehlis70980c02016-10-25 14:00:20 -0600262 db_lines.append("%s%s%s%s%s%s%s%s%s%s%s" % (enum, self.delimiter, implemented, self.delimiter, testname, self.delimiter, self.val_error_dict[enum]['api'], self.delimiter, self.val_error_dict[enum]['error_msg'], self.delimiter, note))
Tobin Ehlisaf75f7c2016-10-31 11:10:38 -0600263 db_lines.append("\n") # newline at end of file
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600264 print "Generating database file %s" % (db_file)
265 with open(db_file, "w") as outfile:
266 outfile.write("\n".join(db_lines))
267 def readDB(self, db_file):
268 """Read a db file into a dict, format of each line is <enum><implemented Y|N?><testname><errormsg>"""
269 db_dict = {} # This is a simple db of just enum->errormsg, the same as is created from spec
270 max_id = 0
271 with open(db_file, "r") as infile:
272 for line in infile:
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600273 line = line.strip()
Tobin Ehlisf4245cb2016-10-31 07:55:19 -0600274 if line.startswith('#') or '' == line:
275 continue
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600276 db_line = line.split(self.delimiter)
Tobin Ehlis70980c02016-10-25 14:00:20 -0600277 if len(db_line) != 6:
278 print "ERROR: Bad database line doesn't have 6 elements: %s" % (line)
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600279 error_enum = db_line[0]
280 implemented = db_line[1]
281 testname = db_line[2]
Tobin Ehlis70980c02016-10-25 14:00:20 -0600282 api = db_line[3]
283 error_str = db_line[4]
284 note = db_line[5]
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600285 db_dict[error_enum] = error_str
286 # Also read complete database contents into our class var for later use
287 self.error_db_dict[error_enum] = {}
288 self.error_db_dict[error_enum]['check_implemented'] = implemented
289 self.error_db_dict[error_enum]['testname'] = testname
Tobin Ehlis70980c02016-10-25 14:00:20 -0600290 self.error_db_dict[error_enum]['api'] = api
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600291 self.error_db_dict[error_enum]['error_string'] = error_str
Tobin Ehlis70980c02016-10-25 14:00:20 -0600292 self.error_db_dict[error_enum]['note'] = note
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600293 unique_id = int(db_line[0].split('_')[-1])
294 if unique_id > max_id:
295 max_id = unique_id
296 return (db_dict, max_id)
297 # Compare unique ids from original database to data generated from updated spec
298 # 1. If a new id and error code exactly match original, great
299 # 2. If new id is not in original, but exact error code is, need to use original error code
300 # 3. If new id and new error are not in original, make sure new id picks up from end of original list
301 # 4. If new id in original, but error strings don't match then:
302 # 4a. If error string has exact match in original, update new to use original
303 # 4b. If error string not in original, may be updated error message, manually address
Tobin Ehlise7560e72016-10-19 15:59:38 -0600304 def compareDB(self, orig_error_msg_dict, max_id):
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600305 """Compare orig database dict to new dict, report out findings, and return potential new dict for parsed spec"""
306 # First create reverse dicts of err_strings to IDs
307 next_id = max_id + 1
308 orig_err_to_id_dict = {}
309 # Create an updated dict in-place that will be assigned to self.val_error_dict when done
310 updated_val_error_dict = {}
Tobin Ehlise7560e72016-10-19 15:59:38 -0600311 for enum in orig_error_msg_dict:
312 orig_err_to_id_dict[orig_error_msg_dict[enum]] = enum
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600313 new_err_to_id_dict = {}
314 for enum in self.val_error_dict:
Tobin Ehlise7560e72016-10-19 15:59:38 -0600315 new_err_to_id_dict[self.val_error_dict[enum]['error_msg']] = enum
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600316 ids_parsed = 0
Tobin Ehlise7560e72016-10-19 15:59:38 -0600317 # Values to be used for the update dict
318 update_enum = ''
319 update_msg = ''
320 update_api = ''
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600321 # Now parse through new dict and figure out what to do with non-matching things
322 for enum in sorted(self.val_error_dict):
323 ids_parsed = ids_parsed + 1
324 enum_list = enum.split('_') # grab sections of enum for use below
Tobin Ehlise7560e72016-10-19 15:59:38 -0600325 # Default update values to be the same
326 update_enum = enum
327 update_msg = self.val_error_dict[enum]['error_msg']
328 update_api = self.val_error_dict[enum]['api']
Tobin Ehlis2a176b12017-01-11 16:18:20 -0700329 implicit = self.val_error_dict[enum]['implicit']
Tobin Ehlisbd0a9c62016-10-14 18:06:16 -0600330 # Any user-forced remap takes precendence
331 if enum_list[-1] in remap_dict:
332 enum_list[-1] = remap_dict[enum_list[-1]]
333 new_enum = "_".join(enum_list)
334 print "NOTE: Using user-supplied remap to force %s to be %s" % (enum, new_enum)
Tobin Ehlise7560e72016-10-19 15:59:38 -0600335 update_enum = new_enum
336 elif enum in orig_error_msg_dict:
337 if self.val_error_dict[enum]['error_msg'] == orig_error_msg_dict[enum]:
Tobin Ehlisbd0a9c62016-10-14 18:06:16 -0600338 print "Exact match for enum %s" % (enum)
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600339 # Nothing to see here
340 if enum in updated_val_error_dict:
341 print "ERROR: About to overwrite entry for %s" % (enum)
Tobin Ehlise7560e72016-10-19 15:59:38 -0600342 elif self.val_error_dict[enum]['error_msg'] in orig_err_to_id_dict:
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600343 # Same value w/ different error id, need to anchor to original id
Tobin Ehlise7560e72016-10-19 15:59:38 -0600344 print "Need to switch new id %s to original id %s" % (enum, orig_err_to_id_dict[self.val_error_dict[enum]['error_msg']])
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600345 # Update id at end of new enum to be same id from original enum
Tobin Ehlise7560e72016-10-19 15:59:38 -0600346 enum_list[-1] = orig_err_to_id_dict[self.val_error_dict[enum]['error_msg']].split('_')[-1]
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600347 new_enum = "_".join(enum_list)
348 if new_enum in updated_val_error_dict:
349 print "ERROR: About to overwrite entry for %s" % (new_enum)
Tobin Ehlise7560e72016-10-19 15:59:38 -0600350 update_enum = new_enum
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600351 else:
352 # No error match:
353 # First check if only link has changed, in which case keep ID but update message
Tobin Ehlise7560e72016-10-19 15:59:38 -0600354 orig_msg_list = orig_error_msg_dict[enum].split('(', 1)
355 new_msg_list = self.val_error_dict[enum]['error_msg'].split('(', 1)
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600356 if orig_msg_list[0] == new_msg_list[0]: # Msg is same bug link has changed, keep enum & update msg
357 print "NOTE: Found that only spec link changed for %s so keeping same id w/ new link" % (enum)
Tobin Ehlisbd0a9c62016-10-14 18:06:16 -0600358 # This seems to be a new error so need to pick it up from end of original unique ids & flag for review
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600359 else:
360 enum_list[-1] = "%05d" % (next_id)
361 new_enum = "_".join(enum_list)
362 next_id = next_id + 1
363 print "MANUALLY VERIFY: Updated new enum %s to be unique %s. Make sure new error msg is actually unique and not just changed" % (enum, new_enum)
Tobin Ehlise7560e72016-10-19 15:59:38 -0600364 print " New error string: %s" % (self.val_error_dict[enum]['error_msg'])
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600365 if new_enum in updated_val_error_dict:
366 print "ERROR: About to overwrite entry for %s" % (new_enum)
Tobin Ehlise7560e72016-10-19 15:59:38 -0600367 update_enum = new_enum
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600368 else: # new enum is not in orig db
Tobin Ehlise7560e72016-10-19 15:59:38 -0600369 if self.val_error_dict[enum]['error_msg'] in orig_err_to_id_dict:
370 print "New enum %s not in orig dict, but exact error message matches original unique id %s" % (enum, orig_err_to_id_dict[self.val_error_dict[enum]['error_msg']])
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600371 # Update new unique_id to use original
Tobin Ehlise7560e72016-10-19 15:59:38 -0600372 enum_list[-1] = orig_err_to_id_dict[self.val_error_dict[enum]['error_msg']].split('_')[-1]
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600373 new_enum = "_".join(enum_list)
374 if new_enum in updated_val_error_dict:
375 print "ERROR: About to overwrite entry for %s" % (new_enum)
Tobin Ehlise7560e72016-10-19 15:59:38 -0600376 update_enum = new_enum
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600377 else:
378 enum_list[-1] = "%05d" % (next_id)
379 new_enum = "_".join(enum_list)
380 next_id = next_id + 1
381 print "Completely new id and error code, update new id from %s to unique %s" % (enum, new_enum)
382 if new_enum in updated_val_error_dict:
383 print "ERROR: About to overwrite entry for %s" % (new_enum)
Tobin Ehlise7560e72016-10-19 15:59:38 -0600384 update_enum = new_enum
385 updated_val_error_dict[update_enum] = {}
386 updated_val_error_dict[update_enum]['error_msg'] = update_msg
387 updated_val_error_dict[update_enum]['api'] = update_api
Tobin Ehlis2a176b12017-01-11 16:18:20 -0700388 updated_val_error_dict[update_enum]['implicit'] = implicit
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600389 # Assign parsed dict to be the udpated dict based on db compare
390 print "In compareDB parsed %d entries" % (ids_parsed)
391 return updated_val_error_dict
392 def validateUpdateDict(self, update_dict):
393 """Compare original dict vs. update dict and make sure that all of the checks are still there"""
394 # Currently just make sure that the same # of checks as the original checks are there
395 #orig_ids = {}
396 orig_id_count = len(self.val_error_dict)
397 #update_ids = {}
398 update_id_count = len(update_dict)
399 if orig_id_count != update_id_count:
400 print "Original dict had %d unique_ids, but updated dict has %d!" % (orig_id_count, update_id_count)
401 return False
402 print "Original dict and updated dict both have %d unique_ids. Great!" % (orig_id_count)
403 return True
404 # TODO : include some more analysis
405
406# User passes in arg of form <new_id1>-<old_id1>[,count1]:<new_id2>-<old_id2>[,count2]:...
407# new_id# = the new enum id that was assigned to an error
408# old_id# = the previous enum id that was assigned to the same error
409# [,count#] = The number of ids to remap starting at new_id#=old_id# and ending at new_id[#+count#-1]=old_id[#+count#-1]
410# If not supplied, then ,1 is assumed, which will only update a single id
411def updateRemapDict(remap_string):
412 """Set up global remap_dict based on user input"""
413 remap_list = remap_string.split(":")
414 for rmap in remap_list:
415 count = 1 # Default count if none supplied
416 id_count_list = rmap.split(',')
417 if len(id_count_list) > 1:
418 count = int(id_count_list[1])
419 new_old_id_list = id_count_list[0].split('-')
420 for offset in range(count):
421 remap_dict["%05d" % (int(new_old_id_list[0]) + offset)] = "%05d" % (int(new_old_id_list[1]) + offset)
422 for new_id in sorted(remap_dict):
423 print "Set to remap new id %s to old id %s" % (new_id, remap_dict[new_id])
424
425if __name__ == "__main__":
426 i = 1
427 use_online = True # Attempt to grab spec from online by default
428 update_option = False
429 while (i < len(sys.argv)):
430 arg = sys.argv[i]
431 i = i + 1
432 if (arg == '-spec'):
433 spec_filename = sys.argv[i]
434 # If user specifies local specfile, skip online
435 use_online = False
436 i = i + 1
437 elif (arg == '-out'):
438 out_filename = sys.argv[i]
439 i = i + 1
440 elif (arg == '-gendb'):
441 gen_db = True
442 # Set filename if supplied, else use default
443 if i < len(sys.argv) and not sys.argv[i].startswith('-'):
444 db_filename = sys.argv[i]
445 i = i + 1
446 elif (arg == '-compare'):
447 db_filename = sys.argv[i]
448 spec_compare = True
449 i = i + 1
450 elif (arg == '-update'):
451 update_option = True
452 spec_compare = True
453 gen_db = True
454 elif (arg == '-remap'):
455 updateRemapDict(sys.argv[i])
456 i = i + 1
457 elif (arg in ['-help', '-h']):
458 printHelp()
459 sys.exit()
460 if len(remap_dict) > 1 and not update_option:
461 print "ERROR: '-remap' option can only be used along with '-update' option. Exiting."
462 sys.exit()
463 spec = Specification()
464 spec.loadFile(use_online, spec_filename)
465 #spec.parseTree()
466 #spec.genHeader(out_filename)
467 spec.analyze()
468 if (spec_compare):
469 # Read in old spec info from db file
Tobin Ehlise7560e72016-10-19 15:59:38 -0600470 (orig_err_msg_dict, max_id) = spec.readDB(db_filename)
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600471 # New spec data should already be read into self.val_error_dict
Tobin Ehlise7560e72016-10-19 15:59:38 -0600472 updated_dict = spec.compareDB(orig_err_msg_dict, max_id)
Tobin Ehlis5ade0692016-10-05 17:18:15 -0600473 update_valid = spec.validateUpdateDict(updated_dict)
474 if update_valid:
475 spec.updateDict(updated_dict)
476 else:
477 sys.exit()
478 if (gen_db):
479 spec.genDB(db_filename)
480 print "Writing out file (-out) to '%s'" % (out_filename)
481 spec.genHeader(out_filename)
482
483##### Example dataset
484# <div class="sidebar">
485# <div class="titlepage">
486# <div>
487# <div>
488# <p class="title">
489# <strong>Valid Usage</strong> # When we get to this guy, we know we're under interesting sidebar
490# </p>
491# </div>
492# </div>
493# </div>
494# <div class="itemizedlist">
495# <ul class="itemizedlist" style="list-style-type: disc; ">
496# <li class="listitem">
497# <em class="parameter">
498# <code>device</code>
499# </em>
500# <span class="normative">must</span> be a valid
501# <code class="code">VkDevice</code> handle
502# </li>
503# <li class="listitem">
504# <em class="parameter">
505# <code>commandPool</code>
506# </em>
507# <span class="normative">must</span> be a valid
508# <code class="code">VkCommandPool</code> handle
509# </li>
510# <li class="listitem">
511# <em class="parameter">
512# <code>flags</code>
513# </em>
514# <span class="normative">must</span> be a valid combination of
515# <code class="code">
516# <a class="link" href="#VkCommandPoolResetFlagBits">VkCommandPoolResetFlagBits</a>
517# </code> values
518# </li>
519# <li class="listitem">
520# <em class="parameter">
521# <code>commandPool</code>
522# </em>
523# <span class="normative">must</span> have been created, allocated, or retrieved from
524# <em class="parameter">
525# <code>device</code>
526# </em>
527# </li>
528# <li class="listitem">All
529# <code class="code">VkCommandBuffer</code>
530# objects allocated from
531# <em class="parameter">
532# <code>commandPool</code>
533# </em>
534# <span class="normative">must</span> not currently be pending execution
535# </li>
536# </ul>
537# </div>
538# </div>
539##### Second example dataset
540# <div class="sidebar">
541# <div class="titlepage">
542# <div>
543# <div>
544# <p class="title">
545# <strong>Valid Usage</strong>
546# </p>
547# </div>
548# </div>
549# </div>
550# <div class="itemizedlist">
551# <ul class="itemizedlist" style="list-style-type: disc; ">
552# <li class="listitem">The <em class="parameter"><code>queueFamilyIndex</code></em> member of any given element of <em class="parameter"><code>pQueueCreateInfos</code></em> <span class="normative">must</span> be unique within <em class="parameter"><code>pQueueCreateInfos</code></em>
553# </li>
554# </ul>
555# </div>
556# </div>
557# <div class="sidebar">
558# <div class="titlepage">
559# <div>
560# <div>
561# <p class="title">
562# <strong>Valid Usage (Implicit)</strong>
563# </p>
564# </div>
565# </div>
566# </div>
567# <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
568#<em class="parameter"><code>sType</code></em> <span class="normative">must</span> be <code class="code">VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO</code>
569#</li><li class="listitem">
570#<em class="parameter"><code>pNext</code></em> <span class="normative">must</span> be <code class="literal">NULL</code>
571#</li><li class="listitem">
572#<em class="parameter"><code>flags</code></em> <span class="normative">must</span> be <code class="literal">0</code>
573#</li><li class="listitem">
574#<em class="parameter"><code>pQueueCreateInfos</code></em> <span class="normative">must</span> be a pointer to an array of <em class="parameter"><code>queueCreateInfoCount</code></em> valid <code class="code">VkDeviceQueueCreateInfo</code> structures
575#</li>