blob: 5e793696449396aa92ee66904836417ce7c221a5 [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',},
Tobin Ehlis236717c2015-08-31 12:42:38 -060084 }
85
86builtin_headers = [layer_inputs[ln]['header'] for ln in layer_inputs]
87builtin_source = [layer_inputs[ln]['source'] for ln in layer_inputs]
Tobin Ehlisc35e23d2016-05-03 12:50:20 -060088builtin_tests = ['tests/layer_validation_tests.cpp', ]
Tobin Ehlis236717c2015-08-31 12:42:38 -060089
90# List of extensions in layers that are included in documentation, but not in vulkan.py API set
91layer_extension_functions = ['objTrackGetObjects', 'objTrackGetObjectsOfType']
92
93def handle_args():
94 parser = argparse.ArgumentParser(description='Generate layer documenation from source.')
95 parser.add_argument('--in_headers', required=False, default=builtin_headers, help='The input layer header files from which code will be generated.')
96 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 -060097 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 -060098 parser.add_argument('--layer_doc', required=False, default='layers/vk_validation_layer_details.md', help='Existing layer document to be validated against actual layers.')
99 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.')
100 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.')
101 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.')
102 return parser.parse_args()
103
104# Little helper class for coloring cmd line output
105class bcolors:
106
107 def __init__(self):
108 self.GREEN = '\033[0;32m'
109 self.RED = '\033[0;31m'
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600110 self.YELLOW = '\033[1;33m'
Tobin Ehlis236717c2015-08-31 12:42:38 -0600111 self.ENDC = '\033[0m'
112 if 'Linux' != platform.system():
113 self.GREEN = ''
114 self.RED = ''
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600115 self.YELLOW = ''
Tobin Ehlis236717c2015-08-31 12:42:38 -0600116 self.ENDC = ''
117
118 def green(self):
119 return self.GREEN
120
121 def red(self):
122 return self.RED
123
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600124 def yellow(self):
125 return self.YELLOW
126
Tobin Ehlis236717c2015-08-31 12:42:38 -0600127 def endc(self):
128 return self.ENDC
129
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600130# Class to parse the validation layer test source and store testnames
131class TestParser:
132 def __init__(self, test_file_list, test_group_name='VkLayerTest'):
133 self.test_files = test_file_list
134 self.tests_set = set()
135 self.test_trigger_txt = 'TEST_F(%s' % test_group_name
136
137 # Parse test files into internal data struct
138 def parse(self):
139 # For each test file, parse test names into set
140 grab_next_line = False # handle testname on separate line than wildcard
141 for test_file in self.test_files:
142 with open(test_file) as tf:
143 for line in tf:
144 if True in [line.strip().startswith(comment) for comment in ['//', '/*']]:
145 continue
146
147 if self.test_trigger_txt in line:
148 #print('Test wildcard in line: %s' % (line))
149 testname = line.split(',')[-1]
150 testname = testname.strip().strip(' {)')
151 #print('Inserting test: "%s"' % (testname))
152 if ('' == testname):
153 grab_next_line = True
154 continue
155 self.tests_set.add(testname)
156 if grab_next_line: # test name on its own line
157 grab_next_line = False
158 testname = testname.strip().strip(' {)')
159 self.tests_set.add(testname)
160
Tobin Ehlis236717c2015-08-31 12:42:38 -0600161# Class to parse the layer source code and store details in internal data structs
162class LayerParser:
163 def __init__(self, header_file_list, source_file_list):
164 self.header_files = header_file_list
165 self.source_files = source_file_list
166 self.layer_dict = {}
167 self.api_dict = {}
168
169 # Parse layer header files into internal dict data structs
170 def parse(self):
171 # For each header file, parse details into dicts
172 # TODO : Should have a global dict element to track overall list of checks
173 store_enum = False
Tobin Ehlis32479352015-12-01 09:48:58 -0700174 for layer_name in layer_inputs:
175 hf = layer_inputs[layer_name]['header']
Tobin Ehlis236717c2015-08-31 12:42:38 -0600176 self.layer_dict[layer_name] = {} # initialize a new dict for this layer
177 self.layer_dict[layer_name]['CHECKS'] = [] # enum of checks is stored in a list
178 #print('Parsing header file %s as layer name %s' % (hf, layer_name))
179 with open(hf) as f:
180 for line in f:
181 if True in [line.strip().startswith(comment) for comment in ['//', '/*']]:
182 #print("Skipping comment line: %s" % line)
183 # For now skipping lines starting w/ comment, may use these to capture
184 # documentation in the future
185 continue
Tobin Ehlis236717c2015-08-31 12:42:38 -0600186 # Find enums
187 if store_enum:
188 if '}' in line: # we're done with enum definition
189 store_enum = False
190 continue
191 # grab the enum name as a unique check
192 if ',' in line:
193 # TODO : When documentation for a check is contained in the source,
194 # this is where we should also capture that documentation so that
195 # it can then be transformed into desired doc format
196 enum_name = line.split(',')[0].strip()
197 # Flag an error if we have already seen this enum
198 if enum_name in self.layer_dict[layer_name]['CHECKS']:
199 print('ERROR : % layer has duplicate error enum: %s' % (layer_name, enum_name))
200 self.layer_dict[layer_name]['CHECKS'].append(enum_name)
201 # If the line includes 'typedef', 'enum', and the expected enum name, start capturing enums
202 if False not in [ex in line for ex in ['typedef', 'enum', layer_inputs[layer_name]['error_enum']]]:
203 store_enum = True
204
205 # For each source file, parse into dicts
206 for sf in self.source_files:
207 #print('Parsing source file %s' % sf)
208 pass
209 # TODO : In the source file we want to see where checks actually occur
210 # Need to build function tree of checks so that we know all of the
211 # checks that occur under a top-level Vulkan API call
212 # Eventually in the validation we can flag ENUMs that aren't being
213 # used in the source, and we can document source code lines as well
214 # as Vulkan API calls where each specific ENUM check is made
215
216 def print_structs(self):
217 print('This is where I print the data structs')
218 for layer in self.layer_dict:
219 print('Layer %s has %i checks:\n%s' % (layer, len(self.layer_dict[layer]['CHECKS'])-1, "\n\t".join(self.layer_dict[layer]['CHECKS'])))
220
221# Class to parse hand-written md layer documentation into a dict and then validate its contents
222class LayerDoc:
223 def __init__(self, source_file):
224 self.layer_doc_filename = source_file
225 self.txt_color = bcolors()
226 # Main data struct to store info from layer doc
227 self.layer_doc_dict = {}
228 # Comprehensive list of all validation checks recorded in doc
229 self.enum_list = []
230
231 # Parse the contents of doc into data struct
232 def parse(self):
233 layer_name = 'INIT'
234 parse_layer_details = False
235 detail_trigger = '| Check | '
236 parse_pending_work = False
237 pending_trigger = ' Pending Work'
238 parse_overview = False
239 overview_trigger = ' Overview'
240 enum_prefix = ''
241
242 with open(self.layer_doc_filename) as f:
243 for line in f:
244 if parse_pending_work:
245 if '.' in line and line.strip()[0].isdigit():
246 todo_item = line.split('.')[1].strip()
247 self.layer_doc_dict[layer_name]['pending'].append(todo_item)
248 if pending_trigger in line and '##' in line:
249 parse_layer_details = False
250 parse_pending_work = True
251 parse_overview = False
252 self.layer_doc_dict[layer_name]['pending'] = []
253 if parse_layer_details:
254 # Grab details but skip the fomat line with a bunch of '-' chars
255 if '|' in line and line.count('-') < 20:
256 detail_sections = line.split('|')
257 #print("Details elements from line %s: %s" % (line, detail_sections))
258 check_name = '%s%s' % (enum_prefix, detail_sections[3].strip())
259 if '_NA' in check_name:
260 # TODO : Should clean up these NA checks in the doc, skipping them for now
261 continue
262 self.enum_list.append(check_name)
263 self.layer_doc_dict[layer_name][check_name] = {}
264 self.layer_doc_dict[layer_name][check_name]['summary_txt'] = detail_sections[1].strip()
265 self.layer_doc_dict[layer_name][check_name]['details_txt'] = detail_sections[2].strip()
266 self.layer_doc_dict[layer_name][check_name]['api_list'] = detail_sections[4].split()
267 self.layer_doc_dict[layer_name][check_name]['tests'] = detail_sections[5].split()
268 self.layer_doc_dict[layer_name][check_name]['notes'] = detail_sections[6].strip()
269 # strip any unwanted commas from api and test names
270 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']]
271 self.layer_doc_dict[layer_name][check_name]['tests'] = [a.strip(',') for a in self.layer_doc_dict[layer_name][check_name]['tests']]
272 # Trigger details parsing when we have table header
273 if detail_trigger in line:
274 parse_layer_details = True
275 parse_pending_work = False
276 parse_overview = False
277 enum_txt = line.split('|')[3]
278 if '*' in enum_txt:
279 enum_prefix = enum_txt.split()[-1].strip('*').strip()
280 #print('prefix: %s' % enum_prefix)
281 if parse_overview:
282 self.layer_doc_dict[layer_name]['overview'] += line
283 if overview_trigger in line and '##' in line:
284 parse_layer_details = False
285 parse_pending_work = False
286 parse_overview = True
287 layer_name = line.split()[1]
288 self.layer_doc_dict[layer_name] = {}
289 self.layer_doc_dict[layer_name]['overview'] = ''
290
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600291 # Verify that checks, tests and api references in layer doc match reality
Tobin Ehlis236717c2015-08-31 12:42:38 -0600292 # Report API calls from doc that are not found in API
293 # Report checks from doc that are not in actual layers
294 # Report checks from layers that are not captured in doc
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600295 # Report checks from doc that do not have a valid test
296 def validate(self, layer_dict, tests_set):
297 #print("tests_set: %s" % (tests_set))
Tobin Ehlis236717c2015-08-31 12:42:38 -0600298 # Count number of errors found and return it
299 errors_found = 0
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600300 warnings_found = 0
Tobin Ehlis236717c2015-08-31 12:42:38 -0600301 # First we'll go through the doc datastructures and flag any issues
302 for chk in self.enum_list:
303 doc_layer_found = False
304 for real_layer in layer_dict:
305 if chk in layer_dict[real_layer]['CHECKS']:
306 #print('Found actual layer check %s in doc' % (chk))
307 doc_layer_found = True
308 continue
309 if not doc_layer_found:
310 print(self.txt_color.red() + 'Actual layers do not contain documented check: %s' % (chk) + self.txt_color.endc())
311 errors_found += 1
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600312
Tobin Ehlis236717c2015-08-31 12:42:38 -0600313 # Now go through API names in doc and verify they're real
314 # First we're going to transform proto names from vulkan.py into single list
315 core_api_names = [p.name for p in vulkan.core.protos]
Jon Ashburn37155e32015-11-24 16:40:36 -0700316 wsi_s_names = [p.name for p in vulkan.ext_khr_surface.protos]
Ian Elliott338dedb2015-08-21 15:09:33 -0600317 wsi_ds_names = [p.name for p in vulkan.ext_khr_device_swapchain.protos]
David Pinedoa31fe0b2015-11-24 09:00:24 -0700318 dbg_rpt_names = [p.name for p in vulkan.lunarg_debug_report.protos]
Jon Ashburnf94d49c2016-03-03 12:03:58 -0700319 api_names = core_api_names + wsi_s_names + wsi_ds_names + dbg_rpt_names
Tobin Ehlis236717c2015-08-31 12:42:38 -0600320 for ln in self.layer_doc_dict:
321 for chk in self.layer_doc_dict[ln]:
322 if chk in ['overview', 'pending']:
323 continue
324 for api in self.layer_doc_dict[ln][chk]['api_list']:
325 if api[2:] not in api_names and api not in layer_extension_functions:
326 print(self.txt_color.red() + 'Doc references invalid function: %s' % (api) + self.txt_color.endc())
327 errors_found += 1
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600328 # For now warn on missing or invalid tests
329 for test in self.layer_doc_dict[ln][chk]['tests']:
330 if '*' in test:
331 # naive way to handle wildcards, just make sure we have matches on parts
332 test_parts = test.split('*')
333 for part in test_parts:
334 part_found = False
335 for t in tests_set:
336 if part in t:
337 part_found = True
338 break
339 if not part_found:
340 print(self.txt_color.yellow() + 'Validation check %s has missing or invalid test : %s' % (chk, test))
341 warnings_found += 1
342 break
343 elif test not in tests_set and not chk.endswith('_NONE'):
344 print(self.txt_color.yellow() + 'Validation check %s has missing or invalid test : %s' % (chk, test))
345 warnings_found += 1
Tobin Ehlis236717c2015-08-31 12:42:38 -0600346 # Now go through all of the actual checks in the layers and make sure they're covered in the doc
347 for ln in layer_dict:
348 for chk in layer_dict[ln]['CHECKS']:
349 if chk not in self.enum_list:
350 print(self.txt_color.red() + 'Doc is missing check: %s' % (chk) + self.txt_color.endc())
351 errors_found += 1
352
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600353 return (errors_found, warnings_found)
Tobin Ehlis236717c2015-08-31 12:42:38 -0600354
355 # Print all of the checks captured in the doc
356 def print_checks(self):
357 print('Checks captured in doc:\n%s' % ('\n\t'.join(self.enum_list)))
358
359def main(argv=None):
360 # Parse args
361 opts = handle_args()
362 # Create parser for layer files
363 layer_parser = LayerParser(opts.in_headers, opts.in_source)
364 # Parse files into internal data structs
365 layer_parser.parse()
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600366 # Parse test files
367 test_parser = TestParser(opts.test_source)
368 test_parser.parse()
Tobin Ehlis236717c2015-08-31 12:42:38 -0600369
370 # Generate requested types of output
371 if opts.print_structs: # Print details of internal data structs
372 layer_parser.print_structs()
373
374 layer_doc = LayerDoc(opts.layer_doc)
375 layer_doc.parse()
376 if opts.print_doc_checks:
377 layer_doc.print_checks()
378
379 if opts.validate:
Tobin Ehlisc35e23d2016-05-03 12:50:20 -0600380 (num_errors, num_warnings) = layer_doc.validate(layer_parser.layer_dict, test_parser.tests_set)
381 txt_color = bcolors()
382 if (0 == num_warnings):
383 print(txt_color.green() + 'No warning cases found between %s and implementation' % (os.path.basename(opts.layer_doc)) + txt_color.endc())
384 else:
385 print(txt_color.yellow() + 'Found %s warnings. See above for details' % num_warnings)
Tobin Ehlis236717c2015-08-31 12:42:38 -0600386 if (0 == num_errors):
Tobin Ehlis236717c2015-08-31 12:42:38 -0600387 print(txt_color.green() + 'No mismatches found between %s and implementation' % (os.path.basename(opts.layer_doc)) + txt_color.endc())
388 else:
389 return num_errors
390 return 0
391
392if __name__ == "__main__":
393 sys.exit(main())
394