Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Karl Schultz | 8e42f40 | 2016-02-02 19:32:33 -0700 | [diff] [blame] | 2 | # Copyright (c) 2015-2016 The Khronos Group Inc. |
| 3 | # Copyright (c) 2015-2016 Valve Corporation |
| 4 | # Copyright (c) 2015-2016 LunarG, Inc. |
| 5 | # Copyright (c) 2015-2016 Google Inc. |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 6 | # |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame^] | 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 10 | # |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame^] | 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 12 | # |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame^] | 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 18 | # |
| 19 | # Author: Tobin Ehlis <tobin@lunarg.com> |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 20 | |
| 21 | import argparse |
| 22 | import os |
| 23 | import sys |
| 24 | import vulkan |
| 25 | import platform |
| 26 | |
| 27 | # vk_layer_documentation_generate.py overview |
| 28 | # This script is intended to generate documentation based on vulkan layers |
| 29 | # It parses known validation layer headers for details of the validation checks |
| 30 | # It parses validation layer source files for specific code where checks are implemented |
| 31 | # structs in a human-readable txt format, as well as utility functions |
| 32 | # to print enum values as strings |
| 33 | |
| 34 | # NOTE : Initially the script is performing validation of a hand-written document |
| 35 | # Right now it does 3 checks: |
| 36 | # 1. Verify ENUM codes declared in source are documented |
| 37 | # 2. Verify ENUM codes in document are declared in source |
| 38 | # 3. Verify API function names in document are in the actual API header (vulkan.py) |
| 39 | # Currently script will flag errors in all of these cases |
| 40 | |
| 41 | # TODO : Need a formal specification of the syntax for doc generation |
| 42 | # Initially, these are the basics: |
| 43 | # 1. Validation checks have unique ENUM values defined in validation layer header |
| 44 | # 2. ENUM includes comments for 1-line overview of check and more detailed description |
| 45 | # 3. Actual code implementing checks includes ENUM value in callback |
| 46 | # 4. Code to test checks should include reference to ENUM |
| 47 | |
| 48 | |
| 49 | # TODO : Need list of known validation layers to use as default input |
| 50 | # Just a couple of flat lists right now, but may need to make this input file |
| 51 | # or at least a more dynamic data structure |
Tobin Ehlis | d8e22bd | 2016-03-16 11:06:44 -0600 | [diff] [blame] | 52 | layer_inputs = { 'draw_state' : {'header' : 'layers/core_validation.h', |
| 53 | 'source' : 'layers/core_validation.cpp', |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 54 | 'generated' : False, |
| 55 | 'error_enum' : 'DRAW_STATE_ERROR'}, |
Tobin Ehlis | d8e22bd | 2016-03-16 11:06:44 -0600 | [diff] [blame] | 56 | 'shader_checker' : {'header' : 'layers/core_validation.h', |
| 57 | 'source' : 'layers/core_validation.cpp', |
Mark Lobodzinski | 9c75c16 | 2015-12-04 10:11:56 -0700 | [diff] [blame] | 58 | 'generated' : False, |
| 59 | 'error_enum' : 'SHADER_CHECKER_ERROR'}, |
Tobin Ehlis | d8e22bd | 2016-03-16 11:06:44 -0600 | [diff] [blame] | 60 | 'mem_tracker' : {'header' : 'layers/core_validation.h', |
| 61 | 'source' : 'layers/core_validation.cpp', |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 62 | 'generated' : False, |
| 63 | 'error_enum' : 'MEM_TRACK_ERROR'}, |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 64 | 'threading' : {'header' : 'layers/threading.h', |
| 65 | 'source' : 'dbuild/layers/threading.cpp', |
| 66 | 'generated' : True, |
| 67 | 'error_enum' : 'THREADING_CHECKER_ERROR'}, |
Jon Ashburn | d2cbdc9 | 2015-12-31 09:44:26 -0700 | [diff] [blame] | 68 | 'object_tracker' : {'header' : 'layers/object_tracker.h', |
| 69 | 'source' : 'dbuild/layers/object_tracker.cpp', |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 70 | 'generated' : True, |
| 71 | 'error_enum' : 'OBJECT_TRACK_ERROR',}, |
Tobin Ehlis | b5fc4fb | 2015-09-03 09:50:06 -0600 | [diff] [blame] | 72 | 'device_limits' : {'header' : 'layers/device_limits.h', |
Tobin Ehlis | ad8c446 | 2015-09-21 15:20:28 -0600 | [diff] [blame] | 73 | 'source' : 'layers/device_limits.cpp', |
| 74 | 'generated' : False, |
| 75 | 'error_enum' : 'DEV_LIMITS_ERROR',}, |
| 76 | 'image' : {'header' : 'layers/image.h', |
| 77 | 'source' : 'layers/image.cpp', |
| 78 | 'generated' : False, |
| 79 | 'error_enum' : 'IMAGE_ERROR',}, |
Ian Elliott | b0f474c | 2015-09-25 15:50:55 -0600 | [diff] [blame] | 80 | 'swapchain' : {'header' : 'layers/swapchain.h', |
| 81 | 'source' : 'layers/swapchain.cpp', |
| 82 | 'generated' : False, |
| 83 | 'error_enum' : 'SWAPCHAIN_ERROR',}, |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | builtin_headers = [layer_inputs[ln]['header'] for ln in layer_inputs] |
| 87 | builtin_source = [layer_inputs[ln]['source'] for ln in layer_inputs] |
| 88 | |
| 89 | # List of extensions in layers that are included in documentation, but not in vulkan.py API set |
| 90 | layer_extension_functions = ['objTrackGetObjects', 'objTrackGetObjectsOfType'] |
| 91 | |
| 92 | def handle_args(): |
| 93 | parser = argparse.ArgumentParser(description='Generate layer documenation from source.') |
| 94 | parser.add_argument('--in_headers', required=False, default=builtin_headers, help='The input layer header files from which code will be generated.') |
| 95 | parser.add_argument('--in_source', required=False, default=builtin_source, help='The input layer source files from which code will be generated.') |
| 96 | parser.add_argument('--layer_doc', required=False, default='layers/vk_validation_layer_details.md', help='Existing layer document to be validated against actual layers.') |
| 97 | parser.add_argument('--validate', action='store_true', default=False, help='Validate that there are no mismatches between layer documentation and source. This includes cross-checking the validation checks, and making sure documented Vulkan API calls exist.') |
| 98 | parser.add_argument('--print_structs', action='store_true', default=False, help='Primarily a debug option that prints out internal data structs used to generate layer docs.') |
| 99 | parser.add_argument('--print_doc_checks', action='store_true', default=False, help='Primarily a debug option that prints out all of the checks that are documented.') |
| 100 | return parser.parse_args() |
| 101 | |
| 102 | # Little helper class for coloring cmd line output |
| 103 | class bcolors: |
| 104 | |
| 105 | def __init__(self): |
| 106 | self.GREEN = '\033[0;32m' |
| 107 | self.RED = '\033[0;31m' |
| 108 | self.ENDC = '\033[0m' |
| 109 | if 'Linux' != platform.system(): |
| 110 | self.GREEN = '' |
| 111 | self.RED = '' |
| 112 | self.ENDC = '' |
| 113 | |
| 114 | def green(self): |
| 115 | return self.GREEN |
| 116 | |
| 117 | def red(self): |
| 118 | return self.RED |
| 119 | |
| 120 | def endc(self): |
| 121 | return self.ENDC |
| 122 | |
| 123 | # Class to parse the layer source code and store details in internal data structs |
| 124 | class LayerParser: |
| 125 | def __init__(self, header_file_list, source_file_list): |
| 126 | self.header_files = header_file_list |
| 127 | self.source_files = source_file_list |
| 128 | self.layer_dict = {} |
| 129 | self.api_dict = {} |
| 130 | |
| 131 | # Parse layer header files into internal dict data structs |
| 132 | def parse(self): |
| 133 | # For each header file, parse details into dicts |
| 134 | # TODO : Should have a global dict element to track overall list of checks |
| 135 | store_enum = False |
Tobin Ehlis | 7e2ad75 | 2015-12-01 09:48:58 -0700 | [diff] [blame] | 136 | for layer_name in layer_inputs: |
| 137 | hf = layer_inputs[layer_name]['header'] |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 138 | self.layer_dict[layer_name] = {} # initialize a new dict for this layer |
| 139 | self.layer_dict[layer_name]['CHECKS'] = [] # enum of checks is stored in a list |
| 140 | #print('Parsing header file %s as layer name %s' % (hf, layer_name)) |
| 141 | with open(hf) as f: |
| 142 | for line in f: |
| 143 | if True in [line.strip().startswith(comment) for comment in ['//', '/*']]: |
| 144 | #print("Skipping comment line: %s" % line) |
| 145 | # For now skipping lines starting w/ comment, may use these to capture |
| 146 | # documentation in the future |
| 147 | continue |
| 148 | |
| 149 | # Find enums |
| 150 | if store_enum: |
| 151 | if '}' in line: # we're done with enum definition |
| 152 | store_enum = False |
| 153 | continue |
| 154 | # grab the enum name as a unique check |
| 155 | if ',' in line: |
| 156 | # TODO : When documentation for a check is contained in the source, |
| 157 | # this is where we should also capture that documentation so that |
| 158 | # it can then be transformed into desired doc format |
| 159 | enum_name = line.split(',')[0].strip() |
| 160 | # Flag an error if we have already seen this enum |
| 161 | if enum_name in self.layer_dict[layer_name]['CHECKS']: |
| 162 | print('ERROR : % layer has duplicate error enum: %s' % (layer_name, enum_name)) |
| 163 | self.layer_dict[layer_name]['CHECKS'].append(enum_name) |
| 164 | # If the line includes 'typedef', 'enum', and the expected enum name, start capturing enums |
| 165 | if False not in [ex in line for ex in ['typedef', 'enum', layer_inputs[layer_name]['error_enum']]]: |
| 166 | store_enum = True |
| 167 | |
| 168 | # For each source file, parse into dicts |
| 169 | for sf in self.source_files: |
| 170 | #print('Parsing source file %s' % sf) |
| 171 | pass |
| 172 | # TODO : In the source file we want to see where checks actually occur |
| 173 | # Need to build function tree of checks so that we know all of the |
| 174 | # checks that occur under a top-level Vulkan API call |
| 175 | # Eventually in the validation we can flag ENUMs that aren't being |
| 176 | # used in the source, and we can document source code lines as well |
| 177 | # as Vulkan API calls where each specific ENUM check is made |
| 178 | |
| 179 | def print_structs(self): |
| 180 | print('This is where I print the data structs') |
| 181 | for layer in self.layer_dict: |
| 182 | print('Layer %s has %i checks:\n%s' % (layer, len(self.layer_dict[layer]['CHECKS'])-1, "\n\t".join(self.layer_dict[layer]['CHECKS']))) |
| 183 | |
| 184 | # Class to parse hand-written md layer documentation into a dict and then validate its contents |
| 185 | class LayerDoc: |
| 186 | def __init__(self, source_file): |
| 187 | self.layer_doc_filename = source_file |
| 188 | self.txt_color = bcolors() |
| 189 | # Main data struct to store info from layer doc |
| 190 | self.layer_doc_dict = {} |
| 191 | # Comprehensive list of all validation checks recorded in doc |
| 192 | self.enum_list = [] |
| 193 | |
| 194 | # Parse the contents of doc into data struct |
| 195 | def parse(self): |
| 196 | layer_name = 'INIT' |
| 197 | parse_layer_details = False |
| 198 | detail_trigger = '| Check | ' |
| 199 | parse_pending_work = False |
| 200 | pending_trigger = ' Pending Work' |
| 201 | parse_overview = False |
| 202 | overview_trigger = ' Overview' |
| 203 | enum_prefix = '' |
| 204 | |
| 205 | with open(self.layer_doc_filename) as f: |
| 206 | for line in f: |
| 207 | if parse_pending_work: |
| 208 | if '.' in line and line.strip()[0].isdigit(): |
| 209 | todo_item = line.split('.')[1].strip() |
| 210 | self.layer_doc_dict[layer_name]['pending'].append(todo_item) |
| 211 | if pending_trigger in line and '##' in line: |
| 212 | parse_layer_details = False |
| 213 | parse_pending_work = True |
| 214 | parse_overview = False |
| 215 | self.layer_doc_dict[layer_name]['pending'] = [] |
| 216 | if parse_layer_details: |
| 217 | # Grab details but skip the fomat line with a bunch of '-' chars |
| 218 | if '|' in line and line.count('-') < 20: |
| 219 | detail_sections = line.split('|') |
| 220 | #print("Details elements from line %s: %s" % (line, detail_sections)) |
| 221 | check_name = '%s%s' % (enum_prefix, detail_sections[3].strip()) |
| 222 | if '_NA' in check_name: |
| 223 | # TODO : Should clean up these NA checks in the doc, skipping them for now |
| 224 | continue |
| 225 | self.enum_list.append(check_name) |
| 226 | self.layer_doc_dict[layer_name][check_name] = {} |
| 227 | self.layer_doc_dict[layer_name][check_name]['summary_txt'] = detail_sections[1].strip() |
| 228 | self.layer_doc_dict[layer_name][check_name]['details_txt'] = detail_sections[2].strip() |
| 229 | self.layer_doc_dict[layer_name][check_name]['api_list'] = detail_sections[4].split() |
| 230 | self.layer_doc_dict[layer_name][check_name]['tests'] = detail_sections[5].split() |
| 231 | self.layer_doc_dict[layer_name][check_name]['notes'] = detail_sections[6].strip() |
| 232 | # strip any unwanted commas from api and test names |
| 233 | self.layer_doc_dict[layer_name][check_name]['api_list'] = [a.strip(',') for a in self.layer_doc_dict[layer_name][check_name]['api_list']] |
| 234 | self.layer_doc_dict[layer_name][check_name]['tests'] = [a.strip(',') for a in self.layer_doc_dict[layer_name][check_name]['tests']] |
| 235 | # Trigger details parsing when we have table header |
| 236 | if detail_trigger in line: |
| 237 | parse_layer_details = True |
| 238 | parse_pending_work = False |
| 239 | parse_overview = False |
| 240 | enum_txt = line.split('|')[3] |
| 241 | if '*' in enum_txt: |
| 242 | enum_prefix = enum_txt.split()[-1].strip('*').strip() |
| 243 | #print('prefix: %s' % enum_prefix) |
| 244 | if parse_overview: |
| 245 | self.layer_doc_dict[layer_name]['overview'] += line |
| 246 | if overview_trigger in line and '##' in line: |
| 247 | parse_layer_details = False |
| 248 | parse_pending_work = False |
| 249 | parse_overview = True |
| 250 | layer_name = line.split()[1] |
| 251 | self.layer_doc_dict[layer_name] = {} |
| 252 | self.layer_doc_dict[layer_name]['overview'] = '' |
| 253 | |
| 254 | # Verify that checks and api references in layer doc match reality |
| 255 | # Report API calls from doc that are not found in API |
| 256 | # Report checks from doc that are not in actual layers |
| 257 | # Report checks from layers that are not captured in doc |
| 258 | def validate(self, layer_dict): |
| 259 | # Count number of errors found and return it |
| 260 | errors_found = 0 |
| 261 | # First we'll go through the doc datastructures and flag any issues |
| 262 | for chk in self.enum_list: |
| 263 | doc_layer_found = False |
| 264 | for real_layer in layer_dict: |
| 265 | if chk in layer_dict[real_layer]['CHECKS']: |
| 266 | #print('Found actual layer check %s in doc' % (chk)) |
| 267 | doc_layer_found = True |
| 268 | continue |
| 269 | if not doc_layer_found: |
| 270 | print(self.txt_color.red() + 'Actual layers do not contain documented check: %s' % (chk) + self.txt_color.endc()) |
| 271 | errors_found += 1 |
| 272 | # Now go through API names in doc and verify they're real |
| 273 | # First we're going to transform proto names from vulkan.py into single list |
| 274 | core_api_names = [p.name for p in vulkan.core.protos] |
Jon Ashburn | d161d28 | 2015-11-24 16:40:36 -0700 | [diff] [blame] | 275 | wsi_s_names = [p.name for p in vulkan.ext_khr_surface.protos] |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 276 | wsi_ds_names = [p.name for p in vulkan.ext_khr_device_swapchain.protos] |
David Pinedo | abd0772 | 2015-11-24 09:00:24 -0700 | [diff] [blame] | 277 | dbg_rpt_names = [p.name for p in vulkan.lunarg_debug_report.protos] |
Jon Ashburn | 58048d0 | 2016-03-03 12:03:58 -0700 | [diff] [blame] | 278 | api_names = core_api_names + wsi_s_names + wsi_ds_names + dbg_rpt_names |
Tobin Ehlis | 12d4c6a | 2015-08-31 12:42:38 -0600 | [diff] [blame] | 279 | for ln in self.layer_doc_dict: |
| 280 | for chk in self.layer_doc_dict[ln]: |
| 281 | if chk in ['overview', 'pending']: |
| 282 | continue |
| 283 | for api in self.layer_doc_dict[ln][chk]['api_list']: |
| 284 | if api[2:] not in api_names and api not in layer_extension_functions: |
| 285 | print(self.txt_color.red() + 'Doc references invalid function: %s' % (api) + self.txt_color.endc()) |
| 286 | errors_found += 1 |
| 287 | # Now go through all of the actual checks in the layers and make sure they're covered in the doc |
| 288 | for ln in layer_dict: |
| 289 | for chk in layer_dict[ln]['CHECKS']: |
| 290 | if chk not in self.enum_list: |
| 291 | print(self.txt_color.red() + 'Doc is missing check: %s' % (chk) + self.txt_color.endc()) |
| 292 | errors_found += 1 |
| 293 | |
| 294 | return errors_found |
| 295 | |
| 296 | # Print all of the checks captured in the doc |
| 297 | def print_checks(self): |
| 298 | print('Checks captured in doc:\n%s' % ('\n\t'.join(self.enum_list))) |
| 299 | |
| 300 | def main(argv=None): |
| 301 | # Parse args |
| 302 | opts = handle_args() |
| 303 | # Create parser for layer files |
| 304 | layer_parser = LayerParser(opts.in_headers, opts.in_source) |
| 305 | # Parse files into internal data structs |
| 306 | layer_parser.parse() |
| 307 | |
| 308 | # Generate requested types of output |
| 309 | if opts.print_structs: # Print details of internal data structs |
| 310 | layer_parser.print_structs() |
| 311 | |
| 312 | layer_doc = LayerDoc(opts.layer_doc) |
| 313 | layer_doc.parse() |
| 314 | if opts.print_doc_checks: |
| 315 | layer_doc.print_checks() |
| 316 | |
| 317 | if opts.validate: |
| 318 | num_errors = layer_doc.validate(layer_parser.layer_dict) |
| 319 | if (0 == num_errors): |
| 320 | txt_color = bcolors() |
| 321 | print(txt_color.green() + 'No mismatches found between %s and implementation' % (os.path.basename(opts.layer_doc)) + txt_color.endc()) |
| 322 | else: |
| 323 | return num_errors |
| 324 | return 0 |
| 325 | |
| 326 | if __name__ == "__main__": |
| 327 | sys.exit(main()) |
| 328 | |