Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1 | #!/usr/bin/python3 -i |
| 2 | # |
| 3 | # Copyright (c) 2015-2016 The Khronos Group Inc. |
| 4 | # Copyright (c) 2015-2016 Valve Corporation |
| 5 | # Copyright (c) 2015-2016 LunarG, Inc. |
| 6 | # Copyright (c) 2015-2016 Google Inc. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | # |
| 20 | # Author: Dustin Graves <dustin@lunarg.com> |
Mark Lobodzinski | 2611259 | 2017-05-30 12:02:17 -0600 | [diff] [blame] | 21 | # Author: Mark Lobodzinski <mark@lunarg.com> |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 22 | |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 23 | import os,re,sys,string |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 24 | import xml.etree.ElementTree as etree |
| 25 | from generator import * |
| 26 | from collections import namedtuple |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 27 | from vuid_mapping import * |
| 28 | |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 29 | |
| 30 | |
| 31 | # ParamCheckerGeneratorOptions - subclass of GeneratorOptions. |
| 32 | # |
| 33 | # Adds options used by ParamCheckerOutputGenerator object during Parameter |
| 34 | # validation layer generation. |
| 35 | # |
| 36 | # Additional members |
| 37 | # prefixText - list of strings to prefix generated header with |
| 38 | # (usually a copyright statement + calling convention macros). |
| 39 | # protectFile - True if multiple inclusion protection should be |
| 40 | # generated (based on the filename) around the entire header. |
| 41 | # protectFeature - True if #ifndef..#endif protection should be |
| 42 | # generated around a feature interface in the header file. |
| 43 | # genFuncPointers - True if function pointer typedefs should be |
| 44 | # generated |
| 45 | # protectProto - If conditional protection should be generated |
| 46 | # around prototype declarations, set to either '#ifdef' |
| 47 | # to require opt-in (#ifdef protectProtoStr) or '#ifndef' |
| 48 | # to require opt-out (#ifndef protectProtoStr). Otherwise |
| 49 | # set to None. |
| 50 | # protectProtoStr - #ifdef/#ifndef symbol to use around prototype |
| 51 | # declarations, if protectProto is set |
| 52 | # apicall - string to use for the function declaration prefix, |
| 53 | # such as APICALL on Windows. |
| 54 | # apientry - string to use for the calling convention macro, |
| 55 | # in typedefs, such as APIENTRY. |
| 56 | # apientryp - string to use for the calling convention macro |
| 57 | # in function pointer typedefs, such as APIENTRYP. |
| 58 | # indentFuncProto - True if prototype declarations should put each |
| 59 | # parameter on a separate line |
| 60 | # indentFuncPointer - True if typedefed function pointers should put each |
| 61 | # parameter on a separate line |
| 62 | # alignFuncParam - if nonzero and parameters are being put on a |
| 63 | # separate line, align parameter names at the specified column |
| 64 | class ParamCheckerGeneratorOptions(GeneratorOptions): |
| 65 | def __init__(self, |
| 66 | filename = None, |
| 67 | directory = '.', |
| 68 | apiname = None, |
| 69 | profile = None, |
| 70 | versions = '.*', |
| 71 | emitversions = '.*', |
| 72 | defaultExtensions = None, |
| 73 | addExtensions = None, |
| 74 | removeExtensions = None, |
| 75 | sortProcedure = regSortFeatures, |
| 76 | prefixText = "", |
| 77 | genFuncPointers = True, |
| 78 | protectFile = True, |
| 79 | protectFeature = True, |
| 80 | protectProto = None, |
| 81 | protectProtoStr = None, |
| 82 | apicall = '', |
| 83 | apientry = '', |
| 84 | apientryp = '', |
| 85 | indentFuncProto = True, |
| 86 | indentFuncPointer = False, |
| 87 | alignFuncParam = 0): |
| 88 | GeneratorOptions.__init__(self, filename, directory, apiname, profile, |
| 89 | versions, emitversions, defaultExtensions, |
| 90 | addExtensions, removeExtensions, sortProcedure) |
| 91 | self.prefixText = prefixText |
| 92 | self.genFuncPointers = genFuncPointers |
| 93 | self.protectFile = protectFile |
| 94 | self.protectFeature = protectFeature |
| 95 | self.protectProto = protectProto |
| 96 | self.protectProtoStr = protectProtoStr |
| 97 | self.apicall = apicall |
| 98 | self.apientry = apientry |
| 99 | self.apientryp = apientryp |
| 100 | self.indentFuncProto = indentFuncProto |
| 101 | self.indentFuncPointer = indentFuncPointer |
| 102 | self.alignFuncParam = alignFuncParam |
| 103 | |
| 104 | # ParamCheckerOutputGenerator - subclass of OutputGenerator. |
| 105 | # Generates param checker layer code. |
| 106 | # |
| 107 | # ---- methods ---- |
| 108 | # ParamCheckerOutputGenerator(errFile, warnFile, diagFile) - args as for |
| 109 | # OutputGenerator. Defines additional internal state. |
| 110 | # ---- methods overriding base class ---- |
| 111 | # beginFile(genOpts) |
| 112 | # endFile() |
| 113 | # beginFeature(interface, emit) |
| 114 | # endFeature() |
| 115 | # genType(typeinfo,name) |
| 116 | # genStruct(typeinfo,name) |
| 117 | # genGroup(groupinfo,name) |
| 118 | # genEnum(enuminfo, name) |
| 119 | # genCmd(cmdinfo) |
| 120 | class ParamCheckerOutputGenerator(OutputGenerator): |
| 121 | """Generate ParamChecker code based on XML element attributes""" |
| 122 | # This is an ordered list of sections in the header file. |
| 123 | ALL_SECTIONS = ['command'] |
| 124 | def __init__(self, |
| 125 | errFile = sys.stderr, |
| 126 | warnFile = sys.stderr, |
| 127 | diagFile = sys.stdout): |
| 128 | OutputGenerator.__init__(self, errFile, warnFile, diagFile) |
| 129 | self.INDENT_SPACES = 4 |
Mark Lobodzinski | b6b8bbd | 2017-02-08 14:37:15 -0700 | [diff] [blame] | 130 | self.intercepts = [] |
| 131 | self.declarations = [] |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 132 | # Commands to ignore |
| 133 | self.blacklist = [ |
| 134 | 'vkGetInstanceProcAddr', |
| 135 | 'vkGetDeviceProcAddr', |
| 136 | 'vkEnumerateInstanceLayerProperties', |
| 137 | 'vkEnumerateInstanceExtensionsProperties', |
| 138 | 'vkEnumerateDeviceLayerProperties', |
| 139 | 'vkEnumerateDeviceExtensionsProperties', |
| 140 | 'vkCreateDebugReportCallbackEXT', |
| 141 | 'vkDebugReportMessageEXT'] |
Dustin Graves | ce68f08 | 2017-03-30 15:42:16 -0600 | [diff] [blame] | 142 | # Structure fields to ignore |
| 143 | self.structMemberBlacklist = { 'VkWriteDescriptorSet' : ['dstSet'] } |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 144 | # Validation conditions for some special case struct members that are conditionally validated |
| 145 | self.structMemberValidationConditions = { 'VkPipelineColorBlendStateCreateInfo' : { 'logicOp' : '{}logicOpEnable == VK_TRUE' } } |
| 146 | # Header version |
| 147 | self.headerVersion = None |
| 148 | # Internal state - accumulators for different inner block text |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 149 | self.validation = [] # Text comprising the main per-api parameter validation routines |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 150 | self.structNames = [] # List of Vulkan struct typenames |
| 151 | self.stypes = [] # Values from the VkStructureType enumeration |
| 152 | self.structTypes = dict() # Map of Vulkan struct typename to required VkStructureType |
| 153 | self.handleTypes = set() # Set of handle type names |
| 154 | self.commands = [] # List of CommandData records for all Vulkan commands |
| 155 | self.structMembers = [] # List of StructMemberData records for all Vulkan structs |
| 156 | self.validatedStructs = dict() # Map of structs type names to generated validation code for that struct type |
| 157 | self.enumRanges = dict() # Map of enum name to BEGIN/END range values |
| 158 | self.flags = set() # Map of flags typenames |
| 159 | self.flagBits = dict() # Map of flag bits typename to list of values |
Chris Forbes | 78ea32d | 2016-11-28 11:14:17 +1300 | [diff] [blame] | 160 | self.newFlags = set() # Map of flags typenames /defined in the current feature/ |
Mark Lobodzinski | 2611259 | 2017-05-30 12:02:17 -0600 | [diff] [blame] | 161 | self.required_extensions = [] # List of required extensions for the current extension |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 162 | self.extension_type = '' # Type of active feature (extension), device or instance |
| 163 | self.extension_names = dict() # Dictionary of extension names to extension name defines |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 164 | self.valid_vuids = set() # Set of all valid VUIDs |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 165 | # Named tuples to store struct and command data |
| 166 | self.StructType = namedtuple('StructType', ['name', 'value']) |
| 167 | self.CommandParam = namedtuple('CommandParam', ['type', 'name', 'ispointer', 'isstaticarray', 'isbool', 'israngedenum', |
| 168 | 'isconst', 'isoptional', 'iscount', 'noautovalidity', 'len', 'extstructs', |
| 169 | 'condition', 'cdecl']) |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 170 | self.CommandData = namedtuple('CommandData', ['name', 'params', 'cdecl', 'extension_type']) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 171 | self.StructMemberData = namedtuple('StructMemberData', ['name', 'members']) |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 172 | |
| 173 | self.vuid_file = None |
| 174 | # Cover cases where file is built from scripts directory, Lin/Win, or Android build structure |
| 175 | vuid_filename_locations = [ |
| 176 | '../layers/vk_validation_error_messages.h', |
| 177 | '../../layers/vk_validation_error_messages.h', |
| 178 | '../../../layers/vk_validation_error_messages.h', |
| 179 | ] |
| 180 | for vuid_filename in vuid_filename_locations: |
| 181 | if os.path.isfile(vuid_filename): |
| 182 | self.vuid_file = open(vuid_filename, "r") |
| 183 | break |
| 184 | if self.vuid_file == None: |
| 185 | print("Error: Could not find vk_validation_error_messages.h") |
| 186 | quit() |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 187 | # |
| 188 | def incIndent(self, indent): |
| 189 | inc = ' ' * self.INDENT_SPACES |
| 190 | if indent: |
| 191 | return indent + inc |
| 192 | return inc |
| 193 | # |
| 194 | def decIndent(self, indent): |
| 195 | if indent and (len(indent) > self.INDENT_SPACES): |
| 196 | return indent[:-self.INDENT_SPACES] |
| 197 | return '' |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 198 | # Convert decimal number to 8 digit hexadecimal lower-case representation |
| 199 | def IdToHex(self, dec_num): |
| 200 | if dec_num > 4294967295: |
| 201 | print ("ERROR: Decimal # %d can't be represented in 8 hex digits" % (dec_num)) |
| 202 | sys.exit() |
| 203 | hex_num = hex(dec_num) |
| 204 | return hex_num[2:].zfill(8) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 205 | # |
| 206 | def beginFile(self, genOpts): |
| 207 | OutputGenerator.beginFile(self, genOpts) |
| 208 | # C-specific |
| 209 | # |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 210 | # Open vk_validation_error_messages.h file to verify computed VUIDs |
| 211 | for line in self.vuid_file: |
| 212 | # Grab hex number from enum definition |
| 213 | vuid_list = line.split('0x') |
| 214 | # If this is a valid enumeration line, remove trailing comma and CR |
| 215 | if len(vuid_list) == 2: |
| 216 | vuid_num = vuid_list[1][:-2] |
| 217 | # Make sure this is a good hex number before adding to set |
| 218 | if len(vuid_num) == 8 and all(c in string.hexdigits for c in vuid_num): |
| 219 | self.valid_vuids.add(vuid_num) |
| 220 | # |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 221 | # User-supplied prefix text, if any (list of strings) |
| 222 | if (genOpts.prefixText): |
| 223 | for s in genOpts.prefixText: |
| 224 | write(s, file=self.outFile) |
| 225 | # |
| 226 | # Multiple inclusion protection & C++ wrappers. |
| 227 | if (genOpts.protectFile and self.genOpts.filename): |
| 228 | headerSym = re.sub('\.h', '_H', os.path.basename(self.genOpts.filename)).upper() |
| 229 | write('#ifndef', headerSym, file=self.outFile) |
| 230 | write('#define', headerSym, '1', file=self.outFile) |
| 231 | self.newline() |
| 232 | # |
| 233 | # Headers |
| 234 | write('#include <string>', file=self.outFile) |
| 235 | self.newline() |
| 236 | write('#include "vulkan/vulkan.h"', file=self.outFile) |
| 237 | write('#include "vk_layer_extension_utils.h"', file=self.outFile) |
| 238 | write('#include "parameter_validation_utils.h"', file=self.outFile) |
| 239 | # |
| 240 | # Macros |
| 241 | self.newline() |
| 242 | write('#ifndef UNUSED_PARAMETER', file=self.outFile) |
| 243 | write('#define UNUSED_PARAMETER(x) (void)(x)', file=self.outFile) |
| 244 | write('#endif // UNUSED_PARAMETER', file=self.outFile) |
| 245 | # |
| 246 | # Namespace |
| 247 | self.newline() |
| 248 | write('namespace parameter_validation {', file = self.outFile) |
| 249 | def endFile(self): |
| 250 | # C-specific |
| 251 | self.newline() |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 252 | commands_text = '\n'.join(self.validation) |
| 253 | write(commands_text, file=self.outFile) |
| 254 | self.newline() |
Mark Lobodzinski | b6b8bbd | 2017-02-08 14:37:15 -0700 | [diff] [blame] | 255 | # Output declarations and record intercepted procedures |
| 256 | write('// Declarations', file=self.outFile) |
| 257 | write('\n'.join(self.declarations), file=self.outFile) |
Mark Lobodzinski | de43e64 | 2017-06-07 14:00:31 -0600 | [diff] [blame] | 258 | write('// Map of all APIs to be intercepted by this layer', file=self.outFile) |
| 259 | write('static const std::unordered_map<std::string, void*> name_to_funcptr_map = {', file=self.outFile) |
Mark Lobodzinski | b6b8bbd | 2017-02-08 14:37:15 -0700 | [diff] [blame] | 260 | write('\n'.join(self.intercepts), file=self.outFile) |
| 261 | write('};\n', file=self.outFile) |
| 262 | self.newline() |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 263 | # Namespace |
| 264 | write('} // namespace parameter_validation', file = self.outFile) |
| 265 | # Finish C++ wrapper and multiple inclusion protection |
| 266 | if (self.genOpts.protectFile and self.genOpts.filename): |
| 267 | self.newline() |
| 268 | write('#endif', file=self.outFile) |
| 269 | # Finish processing in superclass |
| 270 | OutputGenerator.endFile(self) |
| 271 | def beginFeature(self, interface, emit): |
| 272 | # Start processing in superclass |
| 273 | OutputGenerator.beginFeature(self, interface, emit) |
| 274 | # C-specific |
| 275 | # Accumulate includes, defines, types, enums, function pointer typedefs, |
| 276 | # end function prototypes separately for this feature. They're only |
| 277 | # printed in endFeature(). |
| 278 | self.headerVersion = None |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 279 | self.structNames = [] |
| 280 | self.stypes = [] |
| 281 | self.structTypes = dict() |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 282 | self.commands = [] |
| 283 | self.structMembers = [] |
Chris Forbes | 78ea32d | 2016-11-28 11:14:17 +1300 | [diff] [blame] | 284 | self.newFlags = set() |
Mark Lobodzinski | 2611259 | 2017-05-30 12:02:17 -0600 | [diff] [blame] | 285 | # Save list of required extensions for this extension |
| 286 | self.required_extensions = [] |
| 287 | if self.featureName != "VK_VERSION_1_0": |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 288 | # Save Name Define to get correct enable name later |
| 289 | nameElem = interface[0][1] |
| 290 | name = nameElem.get('name') |
| 291 | self.extension_names[self.featureName] = name |
| 292 | # This extension is the first dependency for this command |
Mark Lobodzinski | 2611259 | 2017-05-30 12:02:17 -0600 | [diff] [blame] | 293 | self.required_extensions.append(self.featureName) |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 294 | # Add any defined extension dependencies to the dependency list for this command |
Mark Lobodzinski | 2611259 | 2017-05-30 12:02:17 -0600 | [diff] [blame] | 295 | required_extensions = interface.get('requires') |
| 296 | if required_extensions is not None: |
| 297 | self.required_extensions.extend(required_extensions.split(',')) |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 298 | # And note if this is an Instance or Device extension |
| 299 | self.extension_type = interface.get('type') |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 300 | def endFeature(self): |
| 301 | # C-specific |
| 302 | # Actually write the interface to the output file. |
| 303 | if (self.emit): |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 304 | # If type declarations are needed by other features based on |
| 305 | # this one, it may be necessary to suppress the ExtraProtect, |
| 306 | # or move it below the 'for section...' loop. |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 307 | ifdef = '' |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 308 | if (self.featureExtraProtect != None): |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 309 | ifdef = '#ifdef %s\n' % self.featureExtraProtect |
| 310 | self.validation.append(ifdef) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 311 | # Generate the struct member checking code from the captured data |
| 312 | self.processStructMemberData() |
| 313 | # Generate the command parameter checking code from the captured data |
| 314 | self.processCmdData() |
| 315 | # Write the declaration for the HeaderVersion |
| 316 | if self.headerVersion: |
| 317 | write('const uint32_t GeneratedHeaderVersion = {};'.format(self.headerVersion), file=self.outFile) |
| 318 | self.newline() |
| 319 | # Write the declarations for the VkFlags values combining all flag bits |
Chris Forbes | 78ea32d | 2016-11-28 11:14:17 +1300 | [diff] [blame] | 320 | for flag in sorted(self.newFlags): |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 321 | flagBits = flag.replace('Flags', 'FlagBits') |
| 322 | if flagBits in self.flagBits: |
| 323 | bits = self.flagBits[flagBits] |
| 324 | decl = 'const {} All{} = {}'.format(flag, flagBits, bits[0]) |
| 325 | for bit in bits[1:]: |
| 326 | decl += '|' + bit |
| 327 | decl += ';' |
| 328 | write(decl, file=self.outFile) |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 329 | endif = '\n' |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 330 | if (self.featureExtraProtect != None): |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 331 | endif = '#endif // %s\n' % self.featureExtraProtect |
| 332 | self.validation.append(endif) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 333 | # Finish processing in superclass |
| 334 | OutputGenerator.endFeature(self) |
| 335 | # |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 336 | # Type generation |
| 337 | def genType(self, typeinfo, name): |
| 338 | OutputGenerator.genType(self, typeinfo, name) |
| 339 | typeElem = typeinfo.elem |
| 340 | # If the type is a struct type, traverse the imbedded <member> tags |
| 341 | # generating a structure. Otherwise, emit the tag text. |
| 342 | category = typeElem.get('category') |
| 343 | if (category == 'struct' or category == 'union'): |
| 344 | self.structNames.append(name) |
| 345 | self.genStruct(typeinfo, name) |
| 346 | elif (category == 'handle'): |
| 347 | self.handleTypes.add(name) |
| 348 | elif (category == 'bitmask'): |
| 349 | self.flags.add(name) |
Chris Forbes | 78ea32d | 2016-11-28 11:14:17 +1300 | [diff] [blame] | 350 | self.newFlags.add(name) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 351 | elif (category == 'define'): |
| 352 | if name == 'VK_HEADER_VERSION': |
| 353 | nameElem = typeElem.find('name') |
| 354 | self.headerVersion = noneStr(nameElem.tail).strip() |
| 355 | # |
| 356 | # Struct parameter check generation. |
| 357 | # This is a special case of the <type> tag where the contents are |
| 358 | # interpreted as a set of <member> tags instead of freeform C |
| 359 | # C type declarations. The <member> tags are just like <param> |
| 360 | # tags - they are a declaration of a struct or union member. |
| 361 | # Only simple member declarations are supported (no nested |
| 362 | # structs etc.) |
| 363 | def genStruct(self, typeinfo, typeName): |
| 364 | OutputGenerator.genStruct(self, typeinfo, typeName) |
| 365 | conditions = self.structMemberValidationConditions[typeName] if typeName in self.structMemberValidationConditions else None |
| 366 | members = typeinfo.elem.findall('.//member') |
| 367 | # |
| 368 | # Iterate over members once to get length parameters for arrays |
| 369 | lens = set() |
| 370 | for member in members: |
| 371 | len = self.getLen(member) |
| 372 | if len: |
| 373 | lens.add(len) |
| 374 | # |
| 375 | # Generate member info |
| 376 | membersInfo = [] |
| 377 | for member in members: |
| 378 | # Get the member's type and name |
| 379 | info = self.getTypeNameTuple(member) |
| 380 | type = info[0] |
| 381 | name = info[1] |
| 382 | stypeValue = '' |
| 383 | cdecl = self.makeCParamDecl(member, 0) |
| 384 | # Process VkStructureType |
| 385 | if type == 'VkStructureType': |
| 386 | # Extract the required struct type value from the comments |
| 387 | # embedded in the original text defining the 'typeinfo' element |
| 388 | rawXml = etree.tostring(typeinfo.elem).decode('ascii') |
| 389 | result = re.search(r'VK_STRUCTURE_TYPE_\w+', rawXml) |
| 390 | if result: |
| 391 | value = result.group(0) |
| 392 | else: |
| 393 | value = self.genVkStructureType(typeName) |
| 394 | # Store the required type value |
| 395 | self.structTypes[typeName] = self.StructType(name=name, value=value) |
| 396 | # |
| 397 | # Store pointer/array/string info |
| 398 | # Check for parameter name in lens set |
| 399 | iscount = False |
| 400 | if name in lens: |
| 401 | iscount = True |
| 402 | # The pNext members are not tagged as optional, but are treated as |
| 403 | # optional for parameter NULL checks. Static array members |
| 404 | # are also treated as optional to skip NULL pointer validation, as |
| 405 | # they won't be NULL. |
| 406 | isstaticarray = self.paramIsStaticArray(member) |
| 407 | isoptional = False |
| 408 | if self.paramIsOptional(member) or (name == 'pNext') or (isstaticarray): |
| 409 | isoptional = True |
Dustin Graves | ce68f08 | 2017-03-30 15:42:16 -0600 | [diff] [blame] | 410 | # Determine if value should be ignored by code generation. |
| 411 | noautovalidity = False |
| 412 | if (member.attrib.get('noautovalidity') is not None) or ((typeName in self.structMemberBlacklist) and (name in self.structMemberBlacklist[typeName])): |
| 413 | noautovalidity = True |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 414 | membersInfo.append(self.CommandParam(type=type, name=name, |
| 415 | ispointer=self.paramIsPointer(member), |
| 416 | isstaticarray=isstaticarray, |
| 417 | isbool=True if type == 'VkBool32' else False, |
| 418 | israngedenum=True if type in self.enumRanges else False, |
| 419 | isconst=True if 'const' in cdecl else False, |
| 420 | isoptional=isoptional, |
| 421 | iscount=iscount, |
Dustin Graves | ce68f08 | 2017-03-30 15:42:16 -0600 | [diff] [blame] | 422 | noautovalidity=noautovalidity, |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 423 | len=self.getLen(member), |
| 424 | extstructs=member.attrib.get('validextensionstructs') if name == 'pNext' else None, |
| 425 | condition=conditions[name] if conditions and name in conditions else None, |
| 426 | cdecl=cdecl)) |
| 427 | self.structMembers.append(self.StructMemberData(name=typeName, members=membersInfo)) |
| 428 | # |
| 429 | # Capture group (e.g. C "enum" type) info to be used for |
| 430 | # param check code generation. |
| 431 | # These are concatenated together with other types. |
| 432 | def genGroup(self, groupinfo, groupName): |
| 433 | OutputGenerator.genGroup(self, groupinfo, groupName) |
| 434 | groupElem = groupinfo.elem |
| 435 | # |
| 436 | # Store the sType values |
| 437 | if groupName == 'VkStructureType': |
| 438 | for elem in groupElem.findall('enum'): |
| 439 | self.stypes.append(elem.get('name')) |
| 440 | elif 'FlagBits' in groupName: |
| 441 | bits = [] |
| 442 | for elem in groupElem.findall('enum'): |
| 443 | bits.append(elem.get('name')) |
| 444 | if bits: |
| 445 | self.flagBits[groupName] = bits |
| 446 | else: |
| 447 | # Determine if begin/end ranges are needed (we don't do this for VkStructureType, which has a more finely grained check) |
| 448 | expandName = re.sub(r'([0-9a-z_])([A-Z0-9][^A-Z0-9]?)',r'\1_\2',groupName).upper() |
| 449 | expandPrefix = expandName |
| 450 | expandSuffix = '' |
| 451 | expandSuffixMatch = re.search(r'[A-Z][A-Z]+$',groupName) |
| 452 | if expandSuffixMatch: |
| 453 | expandSuffix = '_' + expandSuffixMatch.group() |
| 454 | # Strip off the suffix from the prefix |
| 455 | expandPrefix = expandName.rsplit(expandSuffix, 1)[0] |
| 456 | isEnum = ('FLAG_BITS' not in expandPrefix) |
| 457 | if isEnum: |
| 458 | self.enumRanges[groupName] = (expandPrefix + '_BEGIN_RANGE' + expandSuffix, expandPrefix + '_END_RANGE' + expandSuffix) |
| 459 | # |
| 460 | # Capture command parameter info to be used for param |
| 461 | # check code generation. |
| 462 | def genCmd(self, cmdinfo, name): |
| 463 | OutputGenerator.genCmd(self, cmdinfo, name) |
Mark Lobodzinski | b6b8bbd | 2017-02-08 14:37:15 -0700 | [diff] [blame] | 464 | interface_functions = [ |
| 465 | 'vkEnumerateInstanceLayerProperties', |
| 466 | 'vkEnumerateInstanceExtensionProperties', |
| 467 | 'vkEnumerateDeviceLayerProperties', |
Mark Lobodzinski | 099b3b6 | 2017-02-08 16:04:35 -0700 | [diff] [blame] | 468 | 'vkCmdDebugMarkerEndEXT', # No validation! |
Mark Lobodzinski | b6b8bbd | 2017-02-08 14:37:15 -0700 | [diff] [blame] | 469 | ] |
| 470 | # Record that the function will be intercepted |
| 471 | if name not in interface_functions: |
| 472 | if (self.featureExtraProtect != None): |
| 473 | self.declarations += [ '#ifdef %s' % self.featureExtraProtect ] |
| 474 | self.intercepts += [ '#ifdef %s' % self.featureExtraProtect ] |
Mark Lobodzinski | de43e64 | 2017-06-07 14:00:31 -0600 | [diff] [blame] | 475 | self.intercepts += [ ' {"%s", (void*)%s},' % (name,name[2:]) ] |
Mark Lobodzinski | b6b8bbd | 2017-02-08 14:37:15 -0700 | [diff] [blame] | 476 | decls = self.makeCDecls(cmdinfo.elem) |
| 477 | # Strip off 'vk' from API name |
| 478 | self.declarations += [ '%s' % decls[0].replace("VKAPI_CALL vk", "VKAPI_CALL ") ] |
| 479 | if (self.featureExtraProtect != None): |
| 480 | self.intercepts += [ '#endif' ] |
| 481 | self.declarations += [ '#endif' ] |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 482 | if name not in self.blacklist: |
| 483 | params = cmdinfo.elem.findall('param') |
| 484 | # Get list of array lengths |
| 485 | lens = set() |
| 486 | for param in params: |
| 487 | len = self.getLen(param) |
| 488 | if len: |
| 489 | lens.add(len) |
| 490 | # Get param info |
| 491 | paramsInfo = [] |
| 492 | for param in params: |
| 493 | paramInfo = self.getTypeNameTuple(param) |
| 494 | cdecl = self.makeCParamDecl(param, 0) |
| 495 | # Check for parameter name in lens set |
| 496 | iscount = False |
| 497 | if paramInfo[1] in lens: |
| 498 | iscount = True |
| 499 | paramsInfo.append(self.CommandParam(type=paramInfo[0], name=paramInfo[1], |
| 500 | ispointer=self.paramIsPointer(param), |
| 501 | isstaticarray=self.paramIsStaticArray(param), |
| 502 | isbool=True if paramInfo[0] == 'VkBool32' else False, |
| 503 | israngedenum=True if paramInfo[0] in self.enumRanges else False, |
| 504 | isconst=True if 'const' in cdecl else False, |
| 505 | isoptional=self.paramIsOptional(param), |
| 506 | iscount=iscount, |
| 507 | noautovalidity=True if param.attrib.get('noautovalidity') is not None else False, |
| 508 | len=self.getLen(param), |
| 509 | extstructs=None, |
| 510 | condition=None, |
| 511 | cdecl=cdecl)) |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 512 | self.commands.append(self.CommandData(name=name, params=paramsInfo, cdecl=self.makeCDecls(cmdinfo.elem)[0], extension_type=self.extension_type)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 513 | # |
| 514 | # Check if the parameter passed in is a pointer |
| 515 | def paramIsPointer(self, param): |
| 516 | ispointer = 0 |
| 517 | paramtype = param.find('type') |
| 518 | if (paramtype.tail is not None) and ('*' in paramtype.tail): |
| 519 | ispointer = paramtype.tail.count('*') |
| 520 | elif paramtype.text[:4] == 'PFN_': |
| 521 | # Treat function pointer typedefs as a pointer to a single value |
| 522 | ispointer = 1 |
| 523 | return ispointer |
| 524 | # |
| 525 | # Check if the parameter passed in is a static array |
| 526 | def paramIsStaticArray(self, param): |
| 527 | isstaticarray = 0 |
| 528 | paramname = param.find('name') |
| 529 | if (paramname.tail is not None) and ('[' in paramname.tail): |
| 530 | isstaticarray = paramname.tail.count('[') |
| 531 | return isstaticarray |
| 532 | # |
| 533 | # Check if the parameter passed in is optional |
| 534 | # Returns a list of Boolean values for comma separated len attributes (len='false,true') |
| 535 | def paramIsOptional(self, param): |
| 536 | # See if the handle is optional |
| 537 | isoptional = False |
| 538 | # Simple, if it's optional, return true |
| 539 | optString = param.attrib.get('optional') |
| 540 | if optString: |
| 541 | if optString == 'true': |
| 542 | isoptional = True |
| 543 | elif ',' in optString: |
| 544 | opts = [] |
| 545 | for opt in optString.split(','): |
| 546 | val = opt.strip() |
| 547 | if val == 'true': |
| 548 | opts.append(True) |
| 549 | elif val == 'false': |
| 550 | opts.append(False) |
| 551 | else: |
| 552 | print('Unrecognized len attribute value',val) |
| 553 | isoptional = opts |
| 554 | return isoptional |
| 555 | # |
| 556 | # Check if the handle passed in is optional |
| 557 | # Uses the same logic as ValidityOutputGenerator.isHandleOptional |
| 558 | def isHandleOptional(self, param, lenParam): |
| 559 | # Simple, if it's optional, return true |
| 560 | if param.isoptional: |
| 561 | return True |
| 562 | # If no validity is being generated, it usually means that validity is complex and not absolute, so let's say yes. |
| 563 | if param.noautovalidity: |
| 564 | return True |
| 565 | # If the parameter is an array and we haven't already returned, find out if any of the len parameters are optional |
| 566 | if lenParam and lenParam.isoptional: |
| 567 | return True |
| 568 | return False |
| 569 | # |
| 570 | # Generate a VkStructureType based on a structure typename |
| 571 | def genVkStructureType(self, typename): |
| 572 | # Add underscore between lowercase then uppercase |
| 573 | value = re.sub('([a-z0-9])([A-Z])', r'\1_\2', typename) |
Mark Young | 3938987 | 2017-01-19 21:10:49 -0700 | [diff] [blame] | 574 | value = value.replace('D3_D12', 'D3D12') |
| 575 | value = value.replace('Device_IDProp', 'Device_ID_Prop') |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 576 | # Change to uppercase |
| 577 | value = value.upper() |
| 578 | # Add STRUCTURE_TYPE_ |
| 579 | return re.sub('VK_', 'VK_STRUCTURE_TYPE_', value) |
| 580 | # |
| 581 | # Get the cached VkStructureType value for the specified struct typename, or generate a VkStructureType |
| 582 | # value assuming the struct is defined by a different feature |
| 583 | def getStructType(self, typename): |
| 584 | value = None |
| 585 | if typename in self.structTypes: |
| 586 | value = self.structTypes[typename].value |
| 587 | else: |
| 588 | value = self.genVkStructureType(typename) |
| 589 | self.logMsg('diag', 'ParameterValidation: Generating {} for {} structure type that was not defined by the current feature'.format(value, typename)) |
| 590 | return value |
| 591 | # |
| 592 | # Retrieve the value of the len tag |
| 593 | def getLen(self, param): |
| 594 | result = None |
| 595 | len = param.attrib.get('len') |
| 596 | if len and len != 'null-terminated': |
| 597 | # For string arrays, 'len' can look like 'count,null-terminated', |
| 598 | # indicating that we have a null terminated array of strings. We |
| 599 | # strip the null-terminated from the 'len' field and only return |
| 600 | # the parameter specifying the string count |
| 601 | if 'null-terminated' in len: |
| 602 | result = len.split(',')[0] |
| 603 | else: |
| 604 | result = len |
| 605 | result = str(result).replace('::', '->') |
| 606 | return result |
| 607 | # |
| 608 | # Retrieve the type and name for a parameter |
| 609 | def getTypeNameTuple(self, param): |
| 610 | type = '' |
| 611 | name = '' |
| 612 | for elem in param: |
| 613 | if elem.tag == 'type': |
| 614 | type = noneStr(elem.text) |
| 615 | elif elem.tag == 'name': |
| 616 | name = noneStr(elem.text) |
| 617 | return (type, name) |
| 618 | # |
| 619 | # Find a named parameter in a parameter list |
| 620 | def getParamByName(self, params, name): |
| 621 | for param in params: |
| 622 | if param.name == name: |
| 623 | return param |
| 624 | return None |
| 625 | # |
| 626 | # Extract length values from latexmath. Currently an inflexible solution that looks for specific |
| 627 | # patterns that are found in vk.xml. Will need to be updated when new patterns are introduced. |
| 628 | def parseLateXMath(self, source): |
| 629 | name = 'ERROR' |
| 630 | decoratedName = 'ERROR' |
| 631 | if 'mathit' in source: |
Mark Lobodzinski | 36c3386 | 2017-02-13 10:15:53 -0700 | [diff] [blame] | 632 | # Matches expressions similar to 'latexmath:[\lceil{\mathit{rasterizationSamples} \over 32}\rceil]' |
| 633 | match = re.match(r'latexmath\s*\:\s*\[\s*\\l(\w+)\s*\{\s*\\mathit\s*\{\s*(\w+)\s*\}\s*\\over\s*(\d+)\s*\}\s*\\r(\w+)\s*\]', source) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 634 | if not match or match.group(1) != match.group(4): |
| 635 | raise 'Unrecognized latexmath expression' |
| 636 | name = match.group(2) |
| 637 | decoratedName = '{}({}/{})'.format(*match.group(1, 2, 3)) |
| 638 | else: |
Mark Lobodzinski | 36c3386 | 2017-02-13 10:15:53 -0700 | [diff] [blame] | 639 | # Matches expressions similar to 'latexmath : [dataSize \over 4]' |
Mark Young | 0f183a8 | 2017-02-28 09:58:04 -0700 | [diff] [blame] | 640 | match = re.match(r'latexmath\s*\:\s*\[\s*(\w+)\s*\\over\s*(\d+)\s*\]', source) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 641 | name = match.group(1) |
| 642 | decoratedName = '{}/{}'.format(*match.group(1, 2)) |
| 643 | return name, decoratedName |
| 644 | # |
| 645 | # Get the length paramater record for the specified parameter name |
| 646 | def getLenParam(self, params, name): |
| 647 | lenParam = None |
| 648 | if name: |
| 649 | if '->' in name: |
| 650 | # The count is obtained by dereferencing a member of a struct parameter |
| 651 | lenParam = self.CommandParam(name=name, iscount=True, ispointer=False, isbool=False, israngedenum=False, isconst=False, |
| 652 | isstaticarray=None, isoptional=False, type=None, noautovalidity=False, len=None, extstructs=None, |
| 653 | condition=None, cdecl=None) |
| 654 | elif 'latexmath' in name: |
| 655 | lenName, decoratedName = self.parseLateXMath(name) |
| 656 | lenParam = self.getParamByName(params, lenName) |
| 657 | # TODO: Zero-check the result produced by the equation? |
| 658 | # Copy the stored len parameter entry and overwrite the name with the processed latexmath equation |
| 659 | #param = self.getParamByName(params, lenName) |
| 660 | #lenParam = self.CommandParam(name=decoratedName, iscount=param.iscount, ispointer=param.ispointer, |
| 661 | # isoptional=param.isoptional, type=param.type, len=param.len, |
| 662 | # isstaticarray=param.isstaticarray, extstructs=param.extstructs, |
| 663 | # noautovalidity=True, condition=None, cdecl=param.cdecl) |
| 664 | else: |
| 665 | lenParam = self.getParamByName(params, name) |
| 666 | return lenParam |
| 667 | # |
| 668 | # Convert a vulkan.h command declaration into a parameter_validation.h definition |
| 669 | def getCmdDef(self, cmd): |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 670 | # Strip the trailing ';' and split into individual lines |
| 671 | lines = cmd.cdecl[:-1].split('\n') |
| 672 | # Replace Vulkan prototype |
| 673 | lines[0] = 'static bool parameter_validation_' + cmd.name + '(' |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 674 | # Replace the first argument with debug_report_data, when the first argument is a handle (not vkCreateInstance) |
| 675 | if cmd.name == 'vkCreateInstance': |
| 676 | lines.insert(1, ' instance_layer_data*'.ljust(self.genOpts.alignFuncParam) + 'layer_data,') |
| 677 | else: |
| 678 | if cmd.params[0].type in ["VkInstance", "VkPhysicalDevice"]: |
| 679 | reportData = ' instance_layer_data*'.ljust(self.genOpts.alignFuncParam) + 'layer_data,' |
| 680 | else: |
| 681 | reportData = ' layer_data*'.ljust(self.genOpts.alignFuncParam) + 'layer_data,' |
Mark Lobodzinski | 2611259 | 2017-05-30 12:02:17 -0600 | [diff] [blame] | 682 | if len(lines) < 3: # Terminate correctly if single parameter |
| 683 | lines[1] = reportData[:-1] + ')' |
| 684 | else: |
| 685 | lines[1] = reportData |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 686 | return '\n'.join(lines) |
| 687 | # |
| 688 | # Generate the code to check for a NULL dereference before calling the |
| 689 | # validation function |
| 690 | def genCheckedLengthCall(self, name, exprs): |
| 691 | count = name.count('->') |
| 692 | if count: |
| 693 | checkedExpr = [] |
| 694 | localIndent = '' |
| 695 | elements = name.split('->') |
| 696 | # Open the if expression blocks |
| 697 | for i in range(0, count): |
| 698 | checkedExpr.append(localIndent + 'if ({} != NULL) {{\n'.format('->'.join(elements[0:i+1]))) |
| 699 | localIndent = self.incIndent(localIndent) |
| 700 | # Add the validation expression |
| 701 | for expr in exprs: |
| 702 | checkedExpr.append(localIndent + expr) |
| 703 | # Close the if blocks |
| 704 | for i in range(0, count): |
| 705 | localIndent = self.decIndent(localIndent) |
| 706 | checkedExpr.append(localIndent + '}\n') |
| 707 | return [checkedExpr] |
| 708 | # No if statements were required |
| 709 | return exprs |
| 710 | # |
| 711 | # Generate code to check for a specific condition before executing validation code |
| 712 | def genConditionalCall(self, prefix, condition, exprs): |
| 713 | checkedExpr = [] |
| 714 | localIndent = '' |
| 715 | formattedCondition = condition.format(prefix) |
| 716 | checkedExpr.append(localIndent + 'if ({})\n'.format(formattedCondition)) |
| 717 | checkedExpr.append(localIndent + '{\n') |
| 718 | localIndent = self.incIndent(localIndent) |
| 719 | for expr in exprs: |
| 720 | checkedExpr.append(localIndent + expr) |
| 721 | localIndent = self.decIndent(localIndent) |
| 722 | checkedExpr.append(localIndent + '}\n') |
| 723 | return [checkedExpr] |
| 724 | # |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 725 | # Get VUID identifier from implicit VUID tag |
| 726 | def GetVuid(self, vuid_string): |
Mark Lobodzinski | 1e707bd | 2017-06-28 10:54:55 -0600 | [diff] [blame] | 727 | if '->' in vuid_string: |
| 728 | return "VALIDATION_ERROR_UNDEFINED" |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 729 | vuid_num = self.IdToHex(convertVUID(vuid_string)) |
| 730 | if vuid_num in self.valid_vuids: |
| 731 | vuid = "VALIDATION_ERROR_%s" % vuid_num |
| 732 | else: |
| 733 | vuid = "VALIDATION_ERROR_UNDEFINED" |
| 734 | return vuid |
| 735 | # |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 736 | # Generate the sType check string |
| 737 | def makeStructTypeCheck(self, prefix, value, lenValue, valueRequired, lenValueRequired, lenPtrRequired, funcPrintName, lenPrintName, valuePrintName, postProcSpec): |
| 738 | checkExpr = [] |
| 739 | stype = self.structTypes[value.type] |
| 740 | if lenValue: |
| 741 | # This is an array with a pointer to a count value |
| 742 | if lenValue.ispointer: |
| 743 | # When the length parameter is a pointer, there is an extra Boolean parameter in the function call to indicate if it is required |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 744 | checkExpr.append('skipCall |= validate_struct_type_array(layer_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, "{sv}", {pf}{ln}, {pf}{vn}, {sv}, {}, {}, {});\n'.format( |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 745 | funcPrintName, lenPtrRequired, lenValueRequired, valueRequired, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, sv=stype.value, pf=prefix, **postProcSpec)) |
| 746 | # This is an array with an integer count value |
| 747 | else: |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 748 | checkExpr.append('skipCall |= validate_struct_type_array(layer_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, "{sv}", {pf}{ln}, {pf}{vn}, {sv}, {}, {});\n'.format( |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 749 | funcPrintName, lenValueRequired, valueRequired, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, sv=stype.value, pf=prefix, **postProcSpec)) |
| 750 | # This is an individual struct |
| 751 | else: |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 752 | vuid = self.GetVuid("VUID-%s-sType-sType" % value.type) |
| 753 | checkExpr.append('skipCall |= validate_struct_type(layer_data->report_data, "{}", {ppp}"{}"{pps}, "{sv}", {}{vn}, {sv}, {}, {});\n'.format( |
| 754 | funcPrintName, valuePrintName, prefix, valueRequired, vuid, vn=value.name, sv=stype.value, vt=value.type, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 755 | return checkExpr |
| 756 | # |
| 757 | # Generate the handle check string |
| 758 | def makeHandleCheck(self, prefix, value, lenValue, valueRequired, lenValueRequired, funcPrintName, lenPrintName, valuePrintName, postProcSpec): |
| 759 | checkExpr = [] |
| 760 | if lenValue: |
| 761 | if lenValue.ispointer: |
| 762 | # This is assumed to be an output array with a pointer to a count value |
| 763 | raise('Unsupported parameter validation case: Output handle array elements are not NULL checked') |
| 764 | else: |
| 765 | # This is an array with an integer count value |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 766 | checkExpr.append('skipCall |= validate_handle_array(layer_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, {pf}{ln}, {pf}{vn}, {}, {});\n'.format( |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 767 | funcPrintName, lenValueRequired, valueRequired, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, pf=prefix, **postProcSpec)) |
| 768 | else: |
| 769 | # This is assumed to be an output handle pointer |
| 770 | raise('Unsupported parameter validation case: Output handles are not NULL checked') |
| 771 | return checkExpr |
| 772 | # |
| 773 | # Generate check string for an array of VkFlags values |
| 774 | def makeFlagsArrayCheck(self, prefix, value, lenValue, valueRequired, lenValueRequired, funcPrintName, lenPrintName, valuePrintName, postProcSpec): |
| 775 | checkExpr = [] |
| 776 | flagBitsName = value.type.replace('Flags', 'FlagBits') |
| 777 | if not flagBitsName in self.flagBits: |
| 778 | raise('Unsupported parameter validation case: array of reserved VkFlags') |
| 779 | else: |
| 780 | allFlags = 'All' + flagBitsName |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 781 | checkExpr.append('skipCall |= validate_flags_array(layer_data->report_data, "{}", {ppp}"{}"{pps}, {ppp}"{}"{pps}, "{}", {}, {pf}{}, {pf}{}, {}, {});\n'.format(funcPrintName, lenPrintName, valuePrintName, flagBitsName, allFlags, lenValue.name, value.name, lenValueRequired, valueRequired, pf=prefix, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 782 | return checkExpr |
| 783 | # |
| 784 | # Generate pNext check string |
Mark Lobodzinski | 3c82852 | 2017-06-26 13:05:57 -0600 | [diff] [blame] | 785 | def makeStructNextCheck(self, prefix, value, funcPrintName, valuePrintName, postProcSpec, struct_type_name): |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 786 | checkExpr = [] |
| 787 | # Generate an array of acceptable VkStructureType values for pNext |
| 788 | extStructCount = 0 |
| 789 | extStructVar = 'NULL' |
| 790 | extStructNames = 'NULL' |
Mark Lobodzinski | 3c82852 | 2017-06-26 13:05:57 -0600 | [diff] [blame] | 791 | vuid = self.GetVuid("VUID-%s-pNext-pNext" % struct_type_name) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 792 | if value.extstructs: |
| 793 | structs = value.extstructs.split(',') |
| 794 | checkExpr.append('const VkStructureType allowedStructs[] = {' + ', '.join([self.getStructType(s) for s in structs]) + '};\n') |
| 795 | extStructCount = 'ARRAY_SIZE(allowedStructs)' |
| 796 | extStructVar = 'allowedStructs' |
| 797 | extStructNames = '"' + ', '.join(structs) + '"' |
Mark Lobodzinski | 3c82852 | 2017-06-26 13:05:57 -0600 | [diff] [blame] | 798 | checkExpr.append('skipCall |= validate_struct_pnext(layer_data->report_data, "{}", {ppp}"{}"{pps}, {}, {}{}, {}, {}, GeneratedHeaderVersion, {});\n'.format( |
| 799 | funcPrintName, valuePrintName, extStructNames, prefix, value.name, extStructCount, extStructVar, vuid, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 800 | return checkExpr |
| 801 | # |
| 802 | # Generate the pointer check string |
Mark Lobodzinski | 1e707bd | 2017-06-28 10:54:55 -0600 | [diff] [blame] | 803 | def makePointerCheck(self, prefix, value, lenValue, valueRequired, lenValueRequired, lenPtrRequired, funcPrintName, lenPrintName, valuePrintName, postProcSpec, struct_type_name): |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 804 | checkExpr = [] |
| 805 | if lenValue: |
Mark Lobodzinski | 1e707bd | 2017-06-28 10:54:55 -0600 | [diff] [blame] | 806 | vuid_tag_name = struct_type_name if struct_type_name is not None else funcPrintName |
| 807 | count_required_vuid = self.GetVuid("VUID-%s-%s-arraylength" % (vuid_tag_name, lenValue.name)) |
| 808 | array_required_vuid = self.GetVuid("VUID-%s-%s-parameter" % (vuid_tag_name, value.name)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 809 | # This is an array with a pointer to a count value |
| 810 | if lenValue.ispointer: |
| 811 | # If count and array parameters are optional, there will be no validation |
| 812 | if valueRequired == 'true' or lenPtrRequired == 'true' or lenValueRequired == 'true': |
| 813 | # When the length parameter is a pointer, there is an extra Boolean parameter in the function call to indicate if it is required |
Mark Lobodzinski | 1e707bd | 2017-06-28 10:54:55 -0600 | [diff] [blame] | 814 | checkExpr.append('skipCall |= validate_array(layer_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, {pf}{ln}, {pf}{vn}, {}, {}, {}, {}, {});\n'.format( |
| 815 | funcPrintName, lenPtrRequired, lenValueRequired, valueRequired, count_required_vuid, array_required_vuid, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, pf=prefix, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 816 | # This is an array with an integer count value |
| 817 | else: |
| 818 | # If count and array parameters are optional, there will be no validation |
| 819 | if valueRequired == 'true' or lenValueRequired == 'true': |
Mark Lobodzinski | 1e707bd | 2017-06-28 10:54:55 -0600 | [diff] [blame] | 820 | if value.type != 'char': |
| 821 | checkExpr.append('skipCall |= validate_array(layer_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, {pf}{ln}, {pf}{vn}, {}, {}, {}, {});\n'.format( |
| 822 | funcPrintName, lenValueRequired, valueRequired, count_required_vuid, array_required_vuid, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, pf=prefix, **postProcSpec)) |
| 823 | else: |
| 824 | # Arrays of strings receive special processing |
| 825 | checkExpr.append('skipCall |= validate_string_array(layer_data->report_data, "{}", {ppp}"{ldn}"{pps}, {ppp}"{dn}"{pps}, {pf}{ln}, {pf}{vn}, {}, {}, {}, {});\n'.format( |
| 826 | funcPrintName, lenValueRequired, valueRequired, count_required_vuid, array_required_vuid, ln=lenValue.name, ldn=lenPrintName, dn=valuePrintName, vn=value.name, pf=prefix, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 827 | if checkExpr: |
| 828 | if lenValue and ('->' in lenValue.name): |
| 829 | # Add checks to ensure the validation call does not dereference a NULL pointer to obtain the count |
| 830 | checkExpr = self.genCheckedLengthCall(lenValue.name, checkExpr) |
| 831 | # This is an individual struct that is not allowed to be NULL |
| 832 | elif not value.isoptional: |
| 833 | # Function pointers need a reinterpret_cast to void* |
| 834 | if value.type[:4] == 'PFN_': |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 835 | checkExpr.append('skipCall |= validate_required_pointer(layer_data->report_data, "{}", {ppp}"{}"{pps}, reinterpret_cast<const void*>({}{}));\n'.format(funcPrintName, valuePrintName, prefix, value.name, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 836 | else: |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 837 | checkExpr.append('skipCall |= validate_required_pointer(layer_data->report_data, "{}", {ppp}"{}"{pps}, {}{});\n'.format(funcPrintName, valuePrintName, prefix, value.name, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 838 | return checkExpr |
| 839 | # |
| 840 | # Process struct member validation code, performing name suibstitution if required |
| 841 | def processStructMemberCode(self, line, funcName, memberNamePrefix, memberDisplayNamePrefix, postProcSpec): |
| 842 | # Build format specifier list |
| 843 | kwargs = {} |
| 844 | if '{postProcPrefix}' in line: |
| 845 | # If we have a tuple that includes a format string and format parameters, need to use ParameterName class |
| 846 | if type(memberDisplayNamePrefix) is tuple: |
| 847 | kwargs['postProcPrefix'] = 'ParameterName(' |
| 848 | else: |
| 849 | kwargs['postProcPrefix'] = postProcSpec['ppp'] |
| 850 | if '{postProcSuffix}' in line: |
| 851 | # If we have a tuple that includes a format string and format parameters, need to use ParameterName class |
| 852 | if type(memberDisplayNamePrefix) is tuple: |
| 853 | kwargs['postProcSuffix'] = ', ParameterName::IndexVector{{ {}{} }})'.format(postProcSpec['ppi'], memberDisplayNamePrefix[1]) |
| 854 | else: |
| 855 | kwargs['postProcSuffix'] = postProcSpec['pps'] |
| 856 | if '{postProcInsert}' in line: |
| 857 | # If we have a tuple that includes a format string and format parameters, need to use ParameterName class |
| 858 | if type(memberDisplayNamePrefix) is tuple: |
| 859 | kwargs['postProcInsert'] = '{}{}, '.format(postProcSpec['ppi'], memberDisplayNamePrefix[1]) |
| 860 | else: |
| 861 | kwargs['postProcInsert'] = postProcSpec['ppi'] |
| 862 | if '{funcName}' in line: |
| 863 | kwargs['funcName'] = funcName |
| 864 | if '{valuePrefix}' in line: |
| 865 | kwargs['valuePrefix'] = memberNamePrefix |
| 866 | if '{displayNamePrefix}' in line: |
| 867 | # Check for a tuple that includes a format string and format parameters to be used with the ParameterName class |
| 868 | if type(memberDisplayNamePrefix) is tuple: |
| 869 | kwargs['displayNamePrefix'] = memberDisplayNamePrefix[0] |
| 870 | else: |
| 871 | kwargs['displayNamePrefix'] = memberDisplayNamePrefix |
| 872 | |
| 873 | if kwargs: |
| 874 | # Need to escape the C++ curly braces |
| 875 | if 'IndexVector' in line: |
| 876 | line = line.replace('IndexVector{ ', 'IndexVector{{ ') |
| 877 | line = line.replace(' }),', ' }}),') |
| 878 | return line.format(**kwargs) |
| 879 | return line |
| 880 | # |
| 881 | # Process struct validation code for inclusion in function or parent struct validation code |
| 882 | def expandStructCode(self, lines, funcName, memberNamePrefix, memberDisplayNamePrefix, indent, output, postProcSpec): |
| 883 | for line in lines: |
| 884 | if output: |
| 885 | output[-1] += '\n' |
| 886 | if type(line) is list: |
| 887 | for sub in line: |
| 888 | output.append(self.processStructMemberCode(indent + sub, funcName, memberNamePrefix, memberDisplayNamePrefix, postProcSpec)) |
| 889 | else: |
| 890 | output.append(self.processStructMemberCode(indent + line, funcName, memberNamePrefix, memberDisplayNamePrefix, postProcSpec)) |
| 891 | return output |
| 892 | # |
| 893 | # Process struct pointer/array validation code, perfoeming name substitution if required |
| 894 | def expandStructPointerCode(self, prefix, value, lenValue, funcName, valueDisplayName, postProcSpec): |
| 895 | expr = [] |
| 896 | expr.append('if ({}{} != NULL)\n'.format(prefix, value.name)) |
| 897 | expr.append('{') |
| 898 | indent = self.incIndent(None) |
| 899 | if lenValue: |
| 900 | # Need to process all elements in the array |
| 901 | indexName = lenValue.name.replace('Count', 'Index') |
| 902 | expr[-1] += '\n' |
Mark Young | 3938987 | 2017-01-19 21:10:49 -0700 | [diff] [blame] | 903 | if lenValue.ispointer: |
| 904 | # If the length value is a pointer, de-reference it for the count. |
| 905 | expr.append(indent + 'for (uint32_t {iname} = 0; {iname} < *{}{}; ++{iname})\n'.format(prefix, lenValue.name, iname=indexName)) |
| 906 | else: |
| 907 | expr.append(indent + 'for (uint32_t {iname} = 0; {iname} < {}{}; ++{iname})\n'.format(prefix, lenValue.name, iname=indexName)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 908 | expr.append(indent + '{') |
| 909 | indent = self.incIndent(indent) |
| 910 | # Prefix for value name to display in error message |
Mark Lobodzinski | 6f82eb5 | 2016-12-05 07:38:41 -0700 | [diff] [blame] | 911 | if value.ispointer == 2: |
| 912 | memberNamePrefix = '{}{}[{}]->'.format(prefix, value.name, indexName) |
| 913 | memberDisplayNamePrefix = ('{}[%i]->'.format(valueDisplayName), indexName) |
| 914 | else: |
| 915 | memberNamePrefix = '{}{}[{}].'.format(prefix, value.name, indexName) |
| 916 | memberDisplayNamePrefix = ('{}[%i].'.format(valueDisplayName), indexName) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 917 | else: |
| 918 | memberNamePrefix = '{}{}->'.format(prefix, value.name) |
| 919 | memberDisplayNamePrefix = '{}->'.format(valueDisplayName) |
| 920 | # |
| 921 | # Expand the struct validation lines |
| 922 | expr = self.expandStructCode(self.validatedStructs[value.type], funcName, memberNamePrefix, memberDisplayNamePrefix, indent, expr, postProcSpec) |
| 923 | # |
| 924 | if lenValue: |
| 925 | # Close if and for scopes |
| 926 | indent = self.decIndent(indent) |
| 927 | expr.append(indent + '}\n') |
| 928 | expr.append('}\n') |
| 929 | return expr |
| 930 | # |
| 931 | # Generate the parameter checking code |
| 932 | def genFuncBody(self, funcName, values, valuePrefix, displayNamePrefix, structTypeName): |
| 933 | lines = [] # Generated lines of code |
| 934 | unused = [] # Unused variable names |
| 935 | for value in values: |
| 936 | usedLines = [] |
| 937 | lenParam = None |
| 938 | # |
| 939 | # Prefix and suffix for post processing of parameter names for struct members. Arrays of structures need special processing to include the array index in the full parameter name. |
| 940 | postProcSpec = {} |
| 941 | postProcSpec['ppp'] = '' if not structTypeName else '{postProcPrefix}' |
| 942 | postProcSpec['pps'] = '' if not structTypeName else '{postProcSuffix}' |
| 943 | postProcSpec['ppi'] = '' if not structTypeName else '{postProcInsert}' |
| 944 | # |
| 945 | # Generate the full name of the value, which will be printed in the error message, by adding the variable prefix to the value name |
| 946 | valueDisplayName = '{}{}'.format(displayNamePrefix, value.name) |
| 947 | # |
| 948 | # Check for NULL pointers, ignore the inout count parameters that |
| 949 | # will be validated with their associated array |
| 950 | if (value.ispointer or value.isstaticarray) and not value.iscount: |
| 951 | # |
| 952 | # Parameters for function argument generation |
| 953 | req = 'true' # Paramerter cannot be NULL |
| 954 | cpReq = 'true' # Count pointer cannot be NULL |
| 955 | cvReq = 'true' # Count value cannot be 0 |
| 956 | lenDisplayName = None # Name of length parameter to print with validation messages; parameter name with prefix applied |
| 957 | # |
| 958 | # Generate required/optional parameter strings for the pointer and count values |
| 959 | if value.isoptional: |
| 960 | req = 'false' |
| 961 | if value.len: |
| 962 | # The parameter is an array with an explicit count parameter |
| 963 | lenParam = self.getLenParam(values, value.len) |
| 964 | lenDisplayName = '{}{}'.format(displayNamePrefix, lenParam.name) |
| 965 | if lenParam.ispointer: |
| 966 | # Count parameters that are pointers are inout |
| 967 | if type(lenParam.isoptional) is list: |
| 968 | if lenParam.isoptional[0]: |
| 969 | cpReq = 'false' |
| 970 | if lenParam.isoptional[1]: |
| 971 | cvReq = 'false' |
| 972 | else: |
| 973 | if lenParam.isoptional: |
| 974 | cpReq = 'false' |
| 975 | else: |
| 976 | if lenParam.isoptional: |
| 977 | cvReq = 'false' |
| 978 | # |
| 979 | # The parameter will not be processes when tagged as 'noautovalidity' |
| 980 | # For the pointer to struct case, the struct pointer will not be validated, but any |
| 981 | # members not tagged as 'noatuvalidity' will be validated |
| 982 | if value.noautovalidity: |
| 983 | # Log a diagnostic message when validation cannot be automatically generated and must be implemented manually |
| 984 | self.logMsg('diag', 'ParameterValidation: No validation for {} {}'.format(structTypeName if structTypeName else funcName, value.name)) |
| 985 | else: |
| 986 | # |
| 987 | # If this is a pointer to a struct with an sType field, verify the type |
| 988 | if value.type in self.structTypes: |
| 989 | usedLines += self.makeStructTypeCheck(valuePrefix, value, lenParam, req, cvReq, cpReq, funcName, lenDisplayName, valueDisplayName, postProcSpec) |
| 990 | # If this is an input handle array that is not allowed to contain NULL handles, verify that none of the handles are VK_NULL_HANDLE |
| 991 | elif value.type in self.handleTypes and value.isconst and not self.isHandleOptional(value, lenParam): |
| 992 | usedLines += self.makeHandleCheck(valuePrefix, value, lenParam, req, cvReq, funcName, lenDisplayName, valueDisplayName, postProcSpec) |
| 993 | elif value.type in self.flags and value.isconst: |
| 994 | usedLines += self.makeFlagsArrayCheck(valuePrefix, value, lenParam, req, cvReq, funcName, lenDisplayName, valueDisplayName, postProcSpec) |
| 995 | elif value.isbool and value.isconst: |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 996 | usedLines.append('skipCall |= validate_bool32_array(layer_data->report_data, "{}", {ppp}"{}"{pps}, {ppp}"{}"{pps}, {pf}{}, {pf}{}, {}, {});\n'.format(funcName, lenDisplayName, valueDisplayName, lenParam.name, value.name, cvReq, req, pf=valuePrefix, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 997 | elif value.israngedenum and value.isconst: |
| 998 | enumRange = self.enumRanges[value.type] |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 999 | usedLines.append('skipCall |= validate_ranged_enum_array(layer_data->report_data, "{}", {ppp}"{}"{pps}, {ppp}"{}"{pps}, "{}", {}, {}, {pf}{}, {pf}{}, {}, {});\n'.format(funcName, lenDisplayName, valueDisplayName, value.type, enumRange[0], enumRange[1], lenParam.name, value.name, cvReq, req, pf=valuePrefix, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1000 | elif value.name == 'pNext': |
| 1001 | # We need to ignore VkDeviceCreateInfo and VkInstanceCreateInfo, as the loader manipulates them in a way that is not documented in vk.xml |
| 1002 | if not structTypeName in ['VkDeviceCreateInfo', 'VkInstanceCreateInfo']: |
Mark Lobodzinski | 3c82852 | 2017-06-26 13:05:57 -0600 | [diff] [blame] | 1003 | usedLines += self.makeStructNextCheck(valuePrefix, value, funcName, valueDisplayName, postProcSpec, structTypeName) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1004 | else: |
Mark Lobodzinski | 1e707bd | 2017-06-28 10:54:55 -0600 | [diff] [blame] | 1005 | usedLines += self.makePointerCheck(valuePrefix, value, lenParam, req, cvReq, cpReq, funcName, lenDisplayName, valueDisplayName, postProcSpec, structTypeName) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1006 | # |
| 1007 | # If this is a pointer to a struct (input), see if it contains members that need to be checked |
| 1008 | if value.type in self.validatedStructs and value.isconst: |
| 1009 | usedLines.append(self.expandStructPointerCode(valuePrefix, value, lenParam, funcName, valueDisplayName, postProcSpec)) |
| 1010 | # Non-pointer types |
| 1011 | else: |
| 1012 | # |
| 1013 | # The parameter will not be processes when tagged as 'noautovalidity' |
| 1014 | # For the struct case, the struct type will not be validated, but any |
| 1015 | # members not tagged as 'noatuvalidity' will be validated |
| 1016 | if value.noautovalidity: |
| 1017 | # Log a diagnostic message when validation cannot be automatically generated and must be implemented manually |
| 1018 | self.logMsg('diag', 'ParameterValidation: No validation for {} {}'.format(structTypeName if structTypeName else funcName, value.name)) |
| 1019 | else: |
Mark Lobodzinski | 024b282 | 2017-06-27 13:22:05 -0600 | [diff] [blame] | 1020 | vuid_name_tag = structTypeName if structTypeName is not None else funcName |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1021 | if value.type in self.structTypes: |
| 1022 | stype = self.structTypes[value.type] |
Mark Lobodzinski | 06954ea | 2017-06-21 12:21:45 -0600 | [diff] [blame] | 1023 | vuid = self.GetVuid("VUID-%s-sType-sType" % value.type) |
| 1024 | usedLines.append('skipCall |= validate_struct_type(layer_data->report_data, "{}", {ppp}"{}"{pps}, "{sv}", &({}{vn}), {sv}, false, {});\n'.format( |
| 1025 | funcName, valueDisplayName, valuePrefix, vuid, vn=value.name, sv=stype.value, vt=value.type, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1026 | elif value.type in self.handleTypes: |
| 1027 | if not self.isHandleOptional(value, None): |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 1028 | usedLines.append('skipCall |= validate_required_handle(layer_data->report_data, "{}", {ppp}"{}"{pps}, {}{});\n'.format(funcName, valueDisplayName, valuePrefix, value.name, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1029 | elif value.type in self.flags: |
| 1030 | flagBitsName = value.type.replace('Flags', 'FlagBits') |
| 1031 | if not flagBitsName in self.flagBits: |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 1032 | usedLines.append('skipCall |= validate_reserved_flags(layer_data->report_data, "{}", {ppp}"{}"{pps}, {pf}{});\n'.format(funcName, valueDisplayName, value.name, pf=valuePrefix, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1033 | else: |
Mark Lobodzinski | e643fcc | 2017-06-26 16:27:15 -0600 | [diff] [blame] | 1034 | if value.isoptional: |
| 1035 | flagsRequired = 'false' |
Mark Lobodzinski | 024b282 | 2017-06-27 13:22:05 -0600 | [diff] [blame] | 1036 | vuid = self.GetVuid("VUID-%s-%s-parameter" % (vuid_name_tag, value.name)) |
Mark Lobodzinski | e643fcc | 2017-06-26 16:27:15 -0600 | [diff] [blame] | 1037 | else: |
| 1038 | flagsRequired = 'true' |
Mark Lobodzinski | e643fcc | 2017-06-26 16:27:15 -0600 | [diff] [blame] | 1039 | vuid = self.GetVuid("VUID-%s-%s-requiredbitmask" % (vuid_name_tag, value.name)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1040 | allFlagsName = 'All' + flagBitsName |
Mark Lobodzinski | e643fcc | 2017-06-26 16:27:15 -0600 | [diff] [blame] | 1041 | usedLines.append('skipCall |= validate_flags(layer_data->report_data, "{}", {ppp}"{}"{pps}, "{}", {}, {pf}{}, {}, false, {});\n'.format(funcName, valueDisplayName, flagBitsName, allFlagsName, value.name, flagsRequired, vuid, pf=valuePrefix, **postProcSpec)) |
Mike Schuchardt | 47619c8 | 2017-05-31 09:14:22 -0600 | [diff] [blame] | 1042 | elif value.type in self.flagBits: |
| 1043 | flagsRequired = 'false' if value.isoptional else 'true' |
| 1044 | allFlagsName = 'All' + value.type |
Mark Lobodzinski | 024b282 | 2017-06-27 13:22:05 -0600 | [diff] [blame] | 1045 | vuid = self.GetVuid("VUID-%s-%s-parameter" % (vuid_name_tag, value.name)) |
| 1046 | usedLines.append('skipCall |= validate_flags(layer_data->report_data, "{}", {ppp}"{}"{pps}, "{}", {}, {pf}{}, {}, true, {});\n'.format(funcName, valueDisplayName, value.type, allFlagsName, value.name, flagsRequired, vuid, pf=valuePrefix, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1047 | elif value.isbool: |
Mark Lobodzinski | 858d5df | 2017-05-30 14:11:04 -0600 | [diff] [blame] | 1048 | usedLines.append('skipCall |= validate_bool32(layer_data->report_data, "{}", {ppp}"{}"{pps}, {}{});\n'.format(funcName, valueDisplayName, valuePrefix, value.name, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1049 | elif value.israngedenum: |
| 1050 | enumRange = self.enumRanges[value.type] |
Mark Lobodzinski | 42eb3c3 | 2017-06-28 11:47:22 -0600 | [diff] [blame^] | 1051 | vuid = self.GetVuid("VUID-%s-%s-parameter" % (vuid_name_tag, value.name)) |
| 1052 | usedLines.append('skipCall |= validate_ranged_enum(layer_data->report_data, "{}", {ppp}"{}"{pps}, "{}", {}, {}, {}{}, {});\n'.format(funcName, valueDisplayName, value.type, enumRange[0], enumRange[1], valuePrefix, value.name, vuid, **postProcSpec)) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1053 | # |
| 1054 | # If this is a struct, see if it contains members that need to be checked |
| 1055 | if value.type in self.validatedStructs: |
| 1056 | memberNamePrefix = '{}{}.'.format(valuePrefix, value.name) |
| 1057 | memberDisplayNamePrefix = '{}.'.format(valueDisplayName) |
| 1058 | usedLines.append(self.expandStructCode(self.validatedStructs[value.type], funcName, memberNamePrefix, memberDisplayNamePrefix, '', [], postProcSpec)) |
| 1059 | # |
| 1060 | # Append the parameter check to the function body for the current command |
| 1061 | if usedLines: |
| 1062 | # Apply special conditional checks |
| 1063 | if value.condition: |
| 1064 | usedLines = self.genConditionalCall(valuePrefix, value.condition, usedLines) |
| 1065 | lines += usedLines |
| 1066 | elif not value.iscount: |
| 1067 | # If no expression was generated for this value, it is unreferenced by the validation function, unless |
| 1068 | # it is an array count, which is indirectly referenced for array valiadation. |
| 1069 | unused.append(value.name) |
| 1070 | return lines, unused |
| 1071 | # |
| 1072 | # Generate the struct member check code from the captured data |
| 1073 | def processStructMemberData(self): |
| 1074 | indent = self.incIndent(None) |
| 1075 | for struct in self.structMembers: |
| 1076 | # |
| 1077 | # The string returned by genFuncBody will be nested in an if check for a NULL pointer, so needs its indent incremented |
| 1078 | lines, unused = self.genFuncBody('{funcName}', struct.members, '{valuePrefix}', '{displayNamePrefix}', struct.name) |
| 1079 | if lines: |
| 1080 | self.validatedStructs[struct.name] = lines |
| 1081 | # |
| 1082 | # Generate the command param check code from the captured data |
| 1083 | def processCmdData(self): |
| 1084 | indent = self.incIndent(None) |
| 1085 | for command in self.commands: |
| 1086 | # Skip first parameter if it is a dispatch handle (everything except vkCreateInstance) |
| 1087 | startIndex = 0 if command.name == 'vkCreateInstance' else 1 |
| 1088 | lines, unused = self.genFuncBody(command.name, command.params[startIndex:], '', '', None) |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 1089 | # Cannot validate extension dependencies for device extension APIs a physical device as their dispatchable object |
| 1090 | if self.required_extensions and self.extension_type == 'device' and command.params[0].type != 'VkPhysicalDevice': |
| 1091 | # The actual extension names are not all available yet, so we'll tag these now and replace them just before |
| 1092 | # writing the validation routines out to a file |
| 1093 | ext_test = '' |
Mark Lobodzinski | 2611259 | 2017-05-30 12:02:17 -0600 | [diff] [blame] | 1094 | for ext in self.required_extensions: |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 1095 | ext_name_define = '' |
| 1096 | ext_enable_name = '' |
| 1097 | for extension in self.registry.extensions: |
| 1098 | if extension.attrib['name'] == ext: |
| 1099 | ext_name_define = extension[0][1].get('name') |
| 1100 | ext_enable_name = ext_name_define.lower() |
| 1101 | ext_enable_name = re.sub('_extension_name', '', ext_enable_name) |
| 1102 | break |
| 1103 | ext_test = 'if (!layer_data->extensions.%s) skipCall |= OutputExtensionError(layer_data, "%s", %s);\n' % (ext_enable_name, command.name, ext_name_define) |
| 1104 | lines.insert(0, ext_test) |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1105 | if lines: |
| 1106 | cmdDef = self.getCmdDef(command) + '\n' |
| 1107 | cmdDef += '{\n' |
| 1108 | # Process unused parameters, Ignoring the first dispatch handle parameter, which is not |
| 1109 | # processed by parameter_validation (except for vkCreateInstance, which does not have a |
| 1110 | # handle as its first parameter) |
| 1111 | if unused: |
| 1112 | for name in unused: |
| 1113 | cmdDef += indent + 'UNUSED_PARAMETER({});\n'.format(name) |
| 1114 | if len(unused) > 0: |
| 1115 | cmdDef += '\n' |
| 1116 | cmdDef += indent + 'bool skipCall = false;\n' |
Mark Lobodzinski | 2611259 | 2017-05-30 12:02:17 -0600 | [diff] [blame] | 1117 | |
Mark Lobodzinski | 8567267 | 2016-10-13 08:36:42 -0600 | [diff] [blame] | 1118 | for line in lines: |
| 1119 | cmdDef += '\n' |
| 1120 | if type(line) is list: |
| 1121 | for sub in line: |
| 1122 | cmdDef += indent + sub |
| 1123 | else: |
| 1124 | cmdDef += indent + line |
| 1125 | cmdDef += '\n' |
| 1126 | cmdDef += indent + 'return skipCall;\n' |
| 1127 | cmdDef += '}\n' |
Mark Lobodzinski | d8b7e02 | 2017-06-01 15:10:13 -0600 | [diff] [blame] | 1128 | self.validation.append(cmdDef) |