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