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