File: error_location_helper_generator.py

package info (click to toggle)
vulkan-validationlayers 1.4.321.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,412 kB
  • sloc: cpp: 594,175; python: 11,321; sh: 24; makefile: 20; xml: 14
file content (328 lines) | stat: -rw-r--r-- 12,216 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/python3 -i
#
# Copyright (c) 2023-2025 The Khronos Group Inc.
# Copyright (c) 2023-2025 Valve Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from base_generator import BaseGenerator

class ErrorLocationHelperOutputGenerator(BaseGenerator):
    def __init__(self):
        BaseGenerator.__init__(self)

        self.fields = set()
        self.pointer_fields = set()

    def generate(self):
        self.write(f'''// *** THIS FILE IS GENERATED - DO NOT EDIT ***
            // See {os.path.basename(__file__)} for modifications

            /***************************************************************************
            *
            * Copyright (c) 2023-2025 The Khronos Group Inc.
            * Copyright (c) 2023-2025 Valve Corporation
            *
            * Licensed under the Apache License, Version 2.0 (the "License");
            * you may not use this file except in compliance with the License.
            * You may obtain a copy of the License at
            *
            *     http://www.apache.org/licenses/LICENSE-2.0
            *
            * Unless required by applicable law or agreed to in writing, software
            * distributed under the License is distributed on an "AS IS" BASIS,
            * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
            * See the License for the specific language governing permissions and
            * limitations under the License.
            ****************************************************************************/
            ''')
        self.write('// NOLINTBEGIN') # Wrap for clang-tidy to ignore

        # Build set of all field names found in all structs and commands
        for command in [x for x in self.vk.commands.values() if not x.alias]:
            for param in command.params:
                self.fields.add(param.name)
                if param.pointer:
                    self.pointer_fields.add(param.name)
        for struct in self.vk.structs.values():
            for member in struct.members:
                self.fields.add(member.name)
                if member.pointer:
                    self.pointer_fields.add(member.name)

        # Pointers in spec start with 'p' except a few cases when dealing with external items (etc WSI).
        # These are names that are also not pointers and removing them in effort to keep the code
        # simpler and just have a few cases where we use a 'dot' instead of an 'arrow' for the error messages.
        self.pointer_fields.remove('buffer') # VkImportAndroidHardwareBufferInfoANDROID
        self.pointer_fields.remove('display') # VkWaylandSurfaceCreateInfoKHR
        self.pointer_fields.remove('window') # VkAndroidSurfaceCreateInfoKHR (and few others)
        self.pointer_fields.remove('surface') # VkDirectFBSurfaceCreateInfoEXT

        # Sort alphabetically
        self.fields = sorted(self.fields)
        self.pointer_fields = sorted(self.pointer_fields)

        if self.filename == 'error_location_helper.h':
            self.generateHeader()
        elif self.filename == 'error_location_helper.cpp':
            self.generateSource()
        else:
            self.write(f'\nFile name {self.filename} has no code to generate\n')

        self.write('// NOLINTEND') # Wrap for clang-tidy to ignore

    def generateHeader(self):
        out = []
        out.append('''
            #pragma once
            #include <vulkan/vulkan.h>
            #include "containers/small_vector.h"
            #include "generated/vk_api_version.h"

            namespace vvl {
            enum class Func {
                Empty = 0,
            ''')
        # Want alpha-sort for ease of look at list while debugging
        for command in sorted(self.vk.commands.values()):
            out.append(f'    {command.name},\n')
        out.append('};\n')

        out.append('\n')
        out.append('enum class Struct {\n')
        out.append('    Empty = 0,\n')
        # Want alpha-sort for ease of look at list while debugging
        for struct in sorted(self.vk.structs.values()):
            out.append(f'    {struct.name},\n')
        out.append('};\n')

        out.append('\n')
        out.append('enum class Field {\n')
        out.append('    Empty = 0,\n')
        # Already alpha-sorted
        for field in self.fields:
            out.append(f'    {field},\n')
        out.append('};\n')
        out.append('\n')

        out.append('enum class Enum {\n')
        out.append('    Empty = 0,\n')
        # Want alpha-sort for ease of look at list while debugging
        for enum in sorted(self.vk.enums.values()):
            out.append(f'    {enum.name},\n')
        out.append('};\n')

        out.append('enum class FlagBitmask {\n')
        out.append('    Empty = 0,\n')
        # Want alpha-sort for ease of look at list while debugging
        for bitmask in sorted(self.vk.bitmasks.values()):
            out.append(f'    {bitmask.name},\n')
        out.append('};\n')

        out.append('\n')
        out.append('// Need underscore prefix to not conflict with namespace, but still easy to match generation\n')
        out.append('enum class Extension {\n')
        out.append('    Empty = 0,\n')
        for extension in sorted(self.vk.extensions.values(), key=lambda x: x.name):
            out.append(f'    _{extension.name},\n')
        out.append('};\n')

        out.append('''

            // Sometimes you know the requirement list doesn't contain any version values
            typedef small_vector<vvl::Extension, 2, size_t> Extensions;

            struct Requirement {
                const vvl::Extension extension;
                const vvl::Version version;

                Requirement(vvl::Extension extension_) : extension(extension_), version(vvl::Version::Empty) {}
                Requirement(vvl::Version version_) : extension(vvl::Extension::Empty), version(version_) {}
            };
            typedef small_vector <Requirement, 2, size_t> Requirements;

            const char* String(Func func);
            const char* String(Struct structure);
            const char* String(Field field);
            const char* String(Enum value);
            const char* String(FlagBitmask value);
            const char* String(Extension extension);
            std::string String(const Extensions& extensions);
            std::string String(const Requirement& requirement);
            std::string String(const Requirements& requirements);

            bool IsFieldPointer(Field field);

            // Used for VUID maps were we only want the new function name
            Func FindAlias(Func func);

            // Used to help know which struct in a pNext chain is
            Struct StypeToStruct(VkStructureType stype);
            }  // namespace vvl
            ''')
        self.write("".join(out))

    def generateSource(self):
        out = []
        out.append('''
            #include "error_location_helper.h"
            #include "generated/vk_api_version.h"
            #include <assert.h>
            #include <string_view>
            ''')

        out.append('''
// clang-format off
namespace vvl {
''')
        out.append('''
const char* String(Func func) {
    static const std::string_view table[] = {
    {"INVALID_EMPTY", 15}, // Func::Empty
''')
        # Need to be alpha-sort also to match array indexing
        for command in sorted(self.vk.commands.values()):
            out.append(f'    {{"{command.name}", {len(command.name) + 1}}},\n')
        out.append('''    };
    return table[(int)func].data();
}

const char* String(Struct structure) {
    static const std::string_view table[] = {
    {"INVALID_EMPTY", 15}, // Struct::Empty
''')
        # Need to be alpha-sort also to match array indexing
        for struct in sorted(self.vk.structs.values()):
            out.append(f'    {{"{struct.name}", {len(struct.name) + 1}}},\n')
        out.append('''    };
    return table[(int)structure].data();
}

const char* String(Field field) {
    static const std::string_view table[] = {
    {"INVALID_EMPTY", 15}, // Field::Empty
''')
        for field in self.fields:
            out.append(f'    {{"{field}", {len(field) + 1}}},\n')
        out.append('''    };
    return table[(int)field].data();
}

const char* String(Enum value) {
    static const std::string_view table[] = {
    {"INVALID_EMPTY", 15}, // Enum::Empty
''')
        # Need to be alpha-sort also to match array indexing
        for enum in sorted(self.vk.enums.values()):
            out.append(f'    {{"{enum.name}", {len(enum.name) + 1}}},\n')
        out.append('''    };
    return table[(int)value].data();
}

const char* String(FlagBitmask value) {
    static const std::string_view table[] = {
    {"INVALID_EMPTY", 15}, // FlagBitmask::Empty
''')
        # Need to be alpha-sort also to match array indexing
        for bitmask in sorted(self.vk.bitmasks.values()):
            out.append(f'    {{"{bitmask.name}", {len(bitmask.name) + 1}}},\n')
        out.append('''    };
    return table[(int)value].data();
}

const char* String(Extension extension) {
    static const std::string_view table[] = {
    {"INVALID_EMPTY", 15}, // Extension::Empty
''')
        for extension in sorted(self.vk.extensions.values(), key=lambda x: x.name):
            out.append(f'    {{"{extension.name}", {len(extension.name) + 1}}},\n')
        out.append('''    };
    return table[(int)extension].data();
}

bool IsFieldPointer(Field field) {
    switch (field) {
''')
        for field in self.pointer_fields:
            out.append(f'    case Field::{field}:\n')
        out.append('''        return true;
    default:
        return false;
    }
}

Struct StypeToStruct(VkStructureType stype) {
    switch (stype) {
''')
        for struct in [x for x in self.vk.structs.values() if x.sType]:
            out.append(f'    case {struct.sType}:\n')
            out.append(f'       return Struct::{struct.name};\n')
        out.append('''
    default:
        break;
    }
    return Struct::Empty;
}

Func FindAlias(Func func) {
    switch (func) {
''')
        for command in [x for x in self.vk.commands.values() if x.alias]:
            out.append(f'    case Func::{command.name}:\n')
            out.append(f'       return Func::{command.alias};\n')
        out.append('''
    default:
        break;
    }
    return func;
}
// clang-format on
''')

        out.append('''
            std::string String(const Extensions& extensions) {
                std::stringstream out;
                for (size_t i = 0; i < extensions.size(); i++) {
                    out << String(extensions[i]);
                    if (i + 1 != extensions.size()) {
                        out << " or ";
                    }
                }
                return out.str();
            }

            std::string String(const Requirement& requirement) {
                if (requirement.extension == Extension::Empty) {
                    APIVersion api_version(static_cast<uint32_t>(requirement.version));
                    return StringAPIVersion(api_version);
                } else {
                    return String(requirement.extension);
                }
            }

            std::string String(const Requirements& requirements) {
                std::stringstream out;
                for (size_t i = 0; i < requirements.size(); i++) {
                    out << String(requirements[i]);
                    if (i + 1 != requirements.size()) {
                        out << " or ";
                    }
                }
                return out.str();
            }

        }  // namespace vvl
        ''')

        self.write("".join(out))