blob: 2dd05ac9ea30e03baaa494dbaf89e2e17acc7038 [file] [log] [blame]
Tobin Ehlis236717c2015-08-31 12:42:38 -06001#!/usr/bin/env python3
Karl Schultzc85a02f2016-02-02 19:32:33 -07002# 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 Ehlis236717c2015-08-31 12:42:38 -06006#
Jon Ashburn43b53e82016-04-19 11:30:31 -06007# 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 Ehlis236717c2015-08-31 12:42:38 -060010#
Jon Ashburn43b53e82016-04-19 11:30:31 -060011# http://www.apache.org/licenses/LICENSE-2.0
Tobin Ehlis236717c2015-08-31 12:42:38 -060012#
Jon Ashburn43b53e82016-04-19 11:30:31 -060013# 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 Goeltzenleuchter96cd7952015-10-30 11:14:30 -060018#
19# Author: Tobin Ehlis <tobin@lunarg.com>
Tobin Ehlis236717c2015-08-31 12:42:38 -060020
21import argparse
22import os
23import sys
24import vulkan
25import 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 Ehlisfce84282016-04-21 14:19:26 -060052layer_inputs = { 'draw_state' : {'header' : 'layers/core_validation_error_enums.h',
Tobin Ehlisb54b2f42016-03-16 11:06:44 -060053 'source' : 'layers/core_validation.cpp',
Tobin Ehlis236717c2015-08-31 12:42:38 -060054 'generated' : False,
55 'error_enum' : 'DRAW_STATE_ERROR'},
Tobin Ehlisfce84282016-04-21 14:19:26 -060056 'shader_checker' : {'header' : 'layers/core_validation_error_enums.h',
Tobin Ehlisb54b2f42016-03-16 11:06:44 -060057 'source' : 'layers/core_validation.cpp',
Mark Lobodzinski5e5b98b2015-12-04 10:11:56 -070058 'generated' : False,
59 'error_enum' : 'SHADER_CHECKER_ERROR'},
Tobin Ehlisfce84282016-04-21 14:19:26 -060060 'mem_tracker' : {'header' : 'layers/core_validation_error_enums.h',
Tobin Ehlisb54b2f42016-03-16 11:06:44 -060061 'source' : 'layers/core_validation.cpp',
Tobin Ehlis236717c2015-08-31 12:42:38 -060062 'generated' : False,
63 'error_enum' : 'MEM_TRACK_ERROR'},
Tobin Ehlis236717c2015-08-31 12:42:38 -060064 'threading' : {'header' : 'layers/threading.h',
65 'source' : 'dbuild/layers/threading.cpp',
66 'generated' : True,
67 'error_enum' : 'THREADING_CHECKER_ERROR'},
Jon Ashburn6e277322015-12-31 09:44:26 -070068 'object_tracker' : {'header' : 'layers/object_tracker.h',
69 'source' : 'dbuild/layers/object_tracker.cpp',
Tobin Ehlis236717c2015-08-31 12:42:38 -060070 'generated' : True,
71 'error_enum' : 'OBJECT_TRACK_ERROR',},
Tobin Ehlisc345b8b2015-09-03 09:50:06 -060072 'device_limits' : {'header' : 'layers/device_limits.h',
Tobin Ehlis65380532015-09-21 15:20:28 -060073 '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 Elliottf81c2562015-09-25 15:50:55 -060080 'swapchain' : {'header' : 'layers/swapchain.h',
81 'source' : 'layers/swapchain.cpp',
82 'generated' : False,
83 'error_enum' : 'SWAPCHAIN_ERROR',},
Dustin Gravesce31fba2016-05-05 13:44:21 -060084 'parameter_validation' : {'header' : 'layers/parameter_validation_utils.h',
85 'source' : 'layers/parameter_validation.cpp',
86 'generated' : False,
87 'error_enum' : 'ErrorCode',},
Tobin Ehlis236717c2015-08-31 12:42:38 -060088 }
89
90builtin_headers = [layer_inputs[ln]['header'] for ln in layer_inputs]
91builtin_source = [layer_inputs[ln]['source'] for ln in layer_inputs]
Tobin Ehlisc35e23d2016-05-03 12:50:20 -060092builtin_tests = ['tests/layer_validation_tests.cpp', ]
Tobin Ehlis236717c2015-08-31 12:42:38 -060093
94# List of extensions in layers that are included in documentation, but not in vulkan.py API set
95layer_extension_functions = ['objTrackGetObjects', 'objTrackGetObjectsOfType']
96
97def handle_args():
98 parser = argparse.ArgumentParser(description='Generate layer documenation from source.')
99 parser.add_argument('--in_headers', required=False, default=builtin_headers, help='The input layer header files from which code will be generated.')
100 parser.add_argument('--in_source', required=False, default=builtin_source, help='The input layer source files from which code will be generated.')
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600101 parser.add_argument('--test_source', required=False, default=builtin_tests, help='The input test source files from which code will be generated.')
Tobin Ehlis236717c2015-08-31 12:42:38 -0600102 parser.add_argument('--layer_doc', required=False, default='layers/vk_validation_layer_details.md', help='Existing layer document to be validated against actual layers.')
103 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.')
104 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.')
105 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.')
106 return parser.parse_args()
107
108# Little helper class for coloring cmd line output
109class bcolors:
110
111 def __init__(self):
112 self.GREEN = '\033[0;32m'
113 self.RED = '\033[0;31m'
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600114 self.YELLOW = '\033[1;33m'
Tobin Ehlis236717c2015-08-31 12:42:38 -0600115 self.ENDC = '\033[0m'
116 if 'Linux' != platform.system():
117 self.GREEN = ''
118 self.RED = ''
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600119 self.YELLOW = ''
Tobin Ehlis236717c2015-08-31 12:42:38 -0600120 self.ENDC = ''
121
122 def green(self):
123 return self.GREEN
124
125 def red(self):
126 return self.RED
127
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600128 def yellow(self):
129 return self.YELLOW
130
Tobin Ehlis236717c2015-08-31 12:42:38 -0600131 def endc(self):
132 return self.ENDC
133
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600134# Class to parse the validation layer test source and store testnames
135class TestParser:
Tobin Ehlise30c1cb2016-05-11 07:26:33 -0600136 def __init__(self, test_file_list, test_group_name=['VkLayerTest', 'VkWsiEnabledLayerTest']):
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600137 self.test_files = test_file_list
138 self.tests_set = set()
Tobin Ehlise30c1cb2016-05-11 07:26:33 -0600139 self.test_trigger_txt_list = []
140 for tg in test_group_name:
141 self.test_trigger_txt_list.append('TEST_F(%s' % tg)
142 #print('Test trigger test list: %s' % (self.test_trigger_txt_list))
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600143
144 # Parse test files into internal data struct
145 def parse(self):
146 # For each test file, parse test names into set
147 grab_next_line = False # handle testname on separate line than wildcard
148 for test_file in self.test_files:
149 with open(test_file) as tf:
150 for line in tf:
151 if True in [line.strip().startswith(comment) for comment in ['//', '/*']]:
152 continue
153
Tobin Ehlise30c1cb2016-05-11 07:26:33 -0600154 if True in [ttt in line for ttt in self.test_trigger_txt_list]:
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600155 #print('Test wildcard in line: %s' % (line))
156 testname = line.split(',')[-1]
157 testname = testname.strip().strip(' {)')
158 #print('Inserting test: "%s"' % (testname))
159 if ('' == testname):
160 grab_next_line = True
161 continue
162 self.tests_set.add(testname)
163 if grab_next_line: # test name on its own line
164 grab_next_line = False
165 testname = testname.strip().strip(' {)')
166 self.tests_set.add(testname)
167
Tobin Ehlis236717c2015-08-31 12:42:38 -0600168# Class to parse the layer source code and store details in internal data structs
169class LayerParser:
170 def __init__(self, header_file_list, source_file_list):
171 self.header_files = header_file_list
172 self.source_files = source_file_list
173 self.layer_dict = {}
174 self.api_dict = {}
175
176 # Parse layer header files into internal dict data structs
177 def parse(self):
178 # For each header file, parse details into dicts
179 # TODO : Should have a global dict element to track overall list of checks
180 store_enum = False
Tobin Ehlis32479352015-12-01 09:48:58 -0700181 for layer_name in layer_inputs:
182 hf = layer_inputs[layer_name]['header']
Tobin Ehlis236717c2015-08-31 12:42:38 -0600183 self.layer_dict[layer_name] = {} # initialize a new dict for this layer
184 self.layer_dict[layer_name]['CHECKS'] = [] # enum of checks is stored in a list
185 #print('Parsing header file %s as layer name %s' % (hf, layer_name))
186 with open(hf) as f:
187 for line in f:
188 if True in [line.strip().startswith(comment) for comment in ['//', '/*']]:
189 #print("Skipping comment line: %s" % line)
190 # For now skipping lines starting w/ comment, may use these to capture
191 # documentation in the future
192 continue
Tobin Ehlis236717c2015-08-31 12:42:38 -0600193 # Find enums
194 if store_enum:
195 if '}' in line: # we're done with enum definition
196 store_enum = False
197 continue
198 # grab the enum name as a unique check
199 if ',' in line:
200 # TODO : When documentation for a check is contained in the source,
201 # this is where we should also capture that documentation so that
202 # it can then be transformed into desired doc format
203 enum_name = line.split(',')[0].strip()
204 # Flag an error if we have already seen this enum
205 if enum_name in self.layer_dict[layer_name]['CHECKS']:
206 print('ERROR : % layer has duplicate error enum: %s' % (layer_name, enum_name))
207 self.layer_dict[layer_name]['CHECKS'].append(enum_name)
Dustin Gravesce31fba2016-05-05 13:44:21 -0600208 # If the line includes 'enum' and the expected enum name, start capturing enums
209 if False not in [ex in line for ex in ['enum', layer_inputs[layer_name]['error_enum']]]:
Tobin Ehlis236717c2015-08-31 12:42:38 -0600210 store_enum = True
211
212 # For each source file, parse into dicts
213 for sf in self.source_files:
214 #print('Parsing source file %s' % sf)
215 pass
216 # TODO : In the source file we want to see where checks actually occur
217 # Need to build function tree of checks so that we know all of the
218 # checks that occur under a top-level Vulkan API call
219 # Eventually in the validation we can flag ENUMs that aren't being
220 # used in the source, and we can document source code lines as well
221 # as Vulkan API calls where each specific ENUM check is made
222
223 def print_structs(self):
224 print('This is where I print the data structs')
225 for layer in self.layer_dict:
226 print('Layer %s has %i checks:\n%s' % (layer, len(self.layer_dict[layer]['CHECKS'])-1, "\n\t".join(self.layer_dict[layer]['CHECKS'])))
227
228# Class to parse hand-written md layer documentation into a dict and then validate its contents
229class LayerDoc:
230 def __init__(self, source_file):
231 self.layer_doc_filename = source_file
232 self.txt_color = bcolors()
233 # Main data struct to store info from layer doc
234 self.layer_doc_dict = {}
235 # Comprehensive list of all validation checks recorded in doc
236 self.enum_list = []
237
238 # Parse the contents of doc into data struct
239 def parse(self):
240 layer_name = 'INIT'
241 parse_layer_details = False
242 detail_trigger = '| Check | '
243 parse_pending_work = False
244 pending_trigger = ' Pending Work'
245 parse_overview = False
246 overview_trigger = ' Overview'
247 enum_prefix = ''
248
249 with open(self.layer_doc_filename) as f:
250 for line in f:
251 if parse_pending_work:
252 if '.' in line and line.strip()[0].isdigit():
253 todo_item = line.split('.')[1].strip()
254 self.layer_doc_dict[layer_name]['pending'].append(todo_item)
255 if pending_trigger in line and '##' in line:
256 parse_layer_details = False
257 parse_pending_work = True
258 parse_overview = False
259 self.layer_doc_dict[layer_name]['pending'] = []
260 if parse_layer_details:
261 # Grab details but skip the fomat line with a bunch of '-' chars
262 if '|' in line and line.count('-') < 20:
263 detail_sections = line.split('|')
264 #print("Details elements from line %s: %s" % (line, detail_sections))
265 check_name = '%s%s' % (enum_prefix, detail_sections[3].strip())
266 if '_NA' in check_name:
267 # TODO : Should clean up these NA checks in the doc, skipping them for now
268 continue
269 self.enum_list.append(check_name)
270 self.layer_doc_dict[layer_name][check_name] = {}
271 self.layer_doc_dict[layer_name][check_name]['summary_txt'] = detail_sections[1].strip()
272 self.layer_doc_dict[layer_name][check_name]['details_txt'] = detail_sections[2].strip()
273 self.layer_doc_dict[layer_name][check_name]['api_list'] = detail_sections[4].split()
274 self.layer_doc_dict[layer_name][check_name]['tests'] = detail_sections[5].split()
275 self.layer_doc_dict[layer_name][check_name]['notes'] = detail_sections[6].strip()
276 # strip any unwanted commas from api and test names
277 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']]
Tobin Ehlise30c1cb2016-05-11 07:26:33 -0600278 test_list = [a.strip(',') for a in self.layer_doc_dict[layer_name][check_name]['tests']]
279 self.layer_doc_dict[layer_name][check_name]['tests'] = [a.split('.')[-1] for a in test_list]
Tobin Ehlis236717c2015-08-31 12:42:38 -0600280 # Trigger details parsing when we have table header
281 if detail_trigger in line:
282 parse_layer_details = True
283 parse_pending_work = False
284 parse_overview = False
285 enum_txt = line.split('|')[3]
286 if '*' in enum_txt:
287 enum_prefix = enum_txt.split()[-1].strip('*').strip()
288 #print('prefix: %s' % enum_prefix)
289 if parse_overview:
290 self.layer_doc_dict[layer_name]['overview'] += line
291 if overview_trigger in line and '##' in line:
292 parse_layer_details = False
293 parse_pending_work = False
294 parse_overview = True
295 layer_name = line.split()[1]
296 self.layer_doc_dict[layer_name] = {}
297 self.layer_doc_dict[layer_name]['overview'] = ''
298
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600299 # Verify that checks, tests and api references in layer doc match reality
Tobin Ehlis236717c2015-08-31 12:42:38 -0600300 # Report API calls from doc that are not found in API
301 # Report checks from doc that are not in actual layers
302 # Report checks from layers that are not captured in doc
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600303 # Report checks from doc that do not have a valid test
304 def validate(self, layer_dict, tests_set):
305 #print("tests_set: %s" % (tests_set))
Tobin Ehlis236717c2015-08-31 12:42:38 -0600306 # Count number of errors found and return it
307 errors_found = 0
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600308 warnings_found = 0
Tobin Ehlis236717c2015-08-31 12:42:38 -0600309 # First we'll go through the doc datastructures and flag any issues
310 for chk in self.enum_list:
311 doc_layer_found = False
312 for real_layer in layer_dict:
313 if chk in layer_dict[real_layer]['CHECKS']:
314 #print('Found actual layer check %s in doc' % (chk))
315 doc_layer_found = True
316 continue
317 if not doc_layer_found:
318 print(self.txt_color.red() + 'Actual layers do not contain documented check: %s' % (chk) + self.txt_color.endc())
319 errors_found += 1
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600320
Tobin Ehlis236717c2015-08-31 12:42:38 -0600321 # Now go through API names in doc and verify they're real
322 # First we're going to transform proto names from vulkan.py into single list
323 core_api_names = [p.name for p in vulkan.core.protos]
Jon Ashburn37155e32015-11-24 16:40:36 -0700324 wsi_s_names = [p.name for p in vulkan.ext_khr_surface.protos]
Ian Elliott338dedb2015-08-21 15:09:33 -0600325 wsi_ds_names = [p.name for p in vulkan.ext_khr_device_swapchain.protos]
David Pinedoa31fe0b2015-11-24 09:00:24 -0700326 dbg_rpt_names = [p.name for p in vulkan.lunarg_debug_report.protos]
Jon Ashburnf94d49c2016-03-03 12:03:58 -0700327 api_names = core_api_names + wsi_s_names + wsi_ds_names + dbg_rpt_names
Tobin Ehlis236717c2015-08-31 12:42:38 -0600328 for ln in self.layer_doc_dict:
329 for chk in self.layer_doc_dict[ln]:
330 if chk in ['overview', 'pending']:
331 continue
332 for api in self.layer_doc_dict[ln][chk]['api_list']:
333 if api[2:] not in api_names and api not in layer_extension_functions:
334 print(self.txt_color.red() + 'Doc references invalid function: %s' % (api) + self.txt_color.endc())
335 errors_found += 1
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600336 # For now warn on missing or invalid tests
337 for test in self.layer_doc_dict[ln][chk]['tests']:
338 if '*' in test:
339 # naive way to handle wildcards, just make sure we have matches on parts
340 test_parts = test.split('*')
341 for part in test_parts:
342 part_found = False
343 for t in tests_set:
344 if part in t:
345 part_found = True
346 break
347 if not part_found:
Tobin Ehlis1552ae12016-05-12 08:42:09 -0600348 print(self.txt_color.red() + 'Validation check %s has missing or invalid test : %s' % (chk, test))
349 errors_found += 1
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600350 break
Tobin Ehlis1552ae12016-05-12 08:42:09 -0600351 elif test not in tests_set and not chk.endswith('_NONE'):
352 if test == 'TODO':
353 warnings_found += 1
354 else:
355 print(self.txt_color.red() + 'Validation check %s has missing or invalid test : %s' % (chk, test))
356 errors_found += 1
Tobin Ehlis236717c2015-08-31 12:42:38 -0600357 # Now go through all of the actual checks in the layers and make sure they're covered in the doc
358 for ln in layer_dict:
359 for chk in layer_dict[ln]['CHECKS']:
360 if chk not in self.enum_list:
361 print(self.txt_color.red() + 'Doc is missing check: %s' % (chk) + self.txt_color.endc())
362 errors_found += 1
363
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600364 return (errors_found, warnings_found)
Tobin Ehlis236717c2015-08-31 12:42:38 -0600365
366 # Print all of the checks captured in the doc
367 def print_checks(self):
368 print('Checks captured in doc:\n%s' % ('\n\t'.join(self.enum_list)))
369
370def main(argv=None):
371 # Parse args
372 opts = handle_args()
373 # Create parser for layer files
374 layer_parser = LayerParser(opts.in_headers, opts.in_source)
375 # Parse files into internal data structs
376 layer_parser.parse()
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600377 # Parse test files
378 test_parser = TestParser(opts.test_source)
379 test_parser.parse()
Tobin Ehlis236717c2015-08-31 12:42:38 -0600380
381 # Generate requested types of output
382 if opts.print_structs: # Print details of internal data structs
383 layer_parser.print_structs()
384
385 layer_doc = LayerDoc(opts.layer_doc)
386 layer_doc.parse()
387 if opts.print_doc_checks:
388 layer_doc.print_checks()
389
390 if opts.validate:
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600391 (num_errors, num_warnings) = layer_doc.validate(layer_parser.layer_dict, test_parser.tests_set)
392 txt_color = bcolors()
393 if (0 == num_warnings):
394 print(txt_color.green() + 'No warning cases found between %s and implementation' % (os.path.basename(opts.layer_doc)) + txt_color.endc())
395 else:
Tobin Ehlis1552ae12016-05-12 08:42:09 -0600396 print(txt_color.yellow() + 'Found %s warnings due to missing tests. Missing tests are labeled as "TODO" in "%s."' % (num_warnings, opts.layer_doc))
Tobin Ehlis236717c2015-08-31 12:42:38 -0600397 if (0 == num_errors):
Tobin Ehlis236717c2015-08-31 12:42:38 -0600398 print(txt_color.green() + 'No mismatches found between %s and implementation' % (os.path.basename(opts.layer_doc)) + txt_color.endc())
399 else:
400 return num_errors
401 return 0
402
403if __name__ == "__main__":
404 sys.exit(main())
405