scripts: Fix codegen with hard-coded enum indices
authorMike Schuchardt <mikes@lunarg.com>
Tue, 28 Sep 2021 16:45:09 +0000 (09:45 -0700)
committerMike Schuchardt <mikes@lunarg.com>
Tue, 28 Sep 2021 22:29:55 +0000 (15:29 -0700)
Currently some of the generators use a fixed child index to locate
extension enums, which will break if comment blocks are added near the
top of an extension definition. This change makes the enum lookup more
robust by searching for the expected enum name instead of using a
hard-coded offset.

scripts/mock_icd_generator.py
scripts/vulkan_tools_helper_file_generator.py

index 64ea7dc..d8b0739 100644 (file)
@@ -1,9 +1,9 @@
 #!/usr/bin/python3 -i
 #
-# Copyright (c) 2015-2017 The Khronos Group Inc.
-# Copyright (c) 2015-2017 Valve Corporation
-# Copyright (c) 2015-2017 LunarG, Inc.
-# Copyright (c) 2015-2017 Google Inc.
+# Copyright (c) 2015-2021 The Khronos Group Inc.
+# Copyright (c) 2015-2021 Valve Corporation
+# Copyright (c) 2015-2021 LunarG, Inc.
+# Copyright (c) 2015-2021 Google Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -1202,12 +1202,17 @@ class MockICDOutputGenerator(OutputGenerator):
             ignore_exts = ['VK_EXT_validation_cache', 'VK_KHR_portability_subset']
             for ext in self.registry.tree.findall("extensions/extension"):
                 if ext.attrib['supported'] != 'disabled': # Only include enabled extensions
-                    if (ext.attrib['name'] in ignore_exts):
-                        pass
-                    elif (ext.attrib.get('type') and 'instance' == ext.attrib['type']):
-                        instance_exts.append('    {"%s", %s},' % (ext.attrib['name'], ext[0][0].attrib['value']))
-                    else:
-                        device_exts.append('    {"%s", %s},' % (ext.attrib['name'], ext[0][0].attrib['value']))
+                    if (ext.attrib['name'] not in ignore_exts):
+                        # Search for extension version enum
+                        for enum in ext.findall('require/enum'):
+                            if enum.get('name', '').endswith('_SPEC_VERSION'):
+                                ext_version = enum.get('value')
+                                if (ext.attrib.get('type') == 'instance'):
+                                    instance_exts.append('    {"%s", %s},' % (ext.attrib['name'], ext_version))
+                                else:
+                                    device_exts.append('    {"%s", %s},' % (ext.attrib['name'], ext_version))
+                                break
+
             write('// Map of instance extension name to version', file=self.outFile)
             write('static const std::unordered_map<std::string, uint32_t> instance_extension_map = {', file=self.outFile)
             write('\n'.join(instance_exts), file=self.outFile)
index 1b24a5e..42ce019 100644 (file)
@@ -1,9 +1,9 @@
 #!/usr/bin/python3 -i
 #
-# Copyright (c) 2015-2017 The Khronos Group Inc.
-# Copyright (c) 2015-2017 Valve Corporation
-# Copyright (c) 2015-2017 LunarG, Inc.
-# Copyright (c) 2015-2017 Google Inc.
+# Copyright (c) 2015-2021 The Khronos Group Inc.
+# Copyright (c) 2015-2021 Valve Corporation
+# Copyright (c) 2015-2021 LunarG, Inc.
+# Copyright (c) 2015-2021 Google Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -176,10 +176,10 @@ class HelperFileOutputGenerator(OutputGenerator):
         if interface.tag != 'extension':
             return
         name = self.featureName
-        nameElem = interface[0][1]
-        name_define = nameElem.get('name')
-        if 'EXTENSION_NAME' not in name_define:
-            print("Error in vk.xml file -- extension name is not available")
+        for enum in interface.findall('require/enum'):
+            if enum.get('name', '').endswith('EXTENSION_NAME'):
+                name_define = enum.get('name')
+                break
         requires = interface.get('requires')
         if requires is not None:
             required_extensions = requires.split(',')
@@ -241,7 +241,7 @@ class HelperFileOutputGenerator(OutputGenerator):
     def paramIsPointer(self, param):
         ispointer = False
         for elem in param:
-            if ((elem.tag is not 'type') and (elem.tail is not None)) and '*' in elem.tail:
+            if ((elem.tag != 'type') and (elem.tail is not None)) and '*' in elem.tail:
                 ispointer = True
         return ispointer
     #