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