Vulkan-Docs/xml/spec_tools/console_printer.py

266 lines
9.8 KiB
Python
Raw Normal View History

Change log for February 3, 2019 Vulkan 1.1.99 spec update: * Update release number to 99. Public Issues: * Add missing pname:pMemoryHostPointerProperties description to flink:vkGetMemoryHostPointerPropertiesEXT.txt (public pull request 896). * Minor markup fixes (public pull request 900). * Minor update to `khronos.css` and markup fixes (originally proposed in public pull request 901, but done via an internal MR). Internal Issues: * Document restrictions on image queries for Y'CbCr formats in the <<features-formats-requiring-sampler-ycbcr-conversion>> table as well as for slink:sname:VkImageFormatProperties and slink:VkImageCreateInfo (internal issue 1361). * Correct type of the code:FragSizeEXT built-in in the <<interfaces-builtin-variables, Built-In Variables>> section (internal issue 1526). * Clean up math in the <<textures, Image Operations>> chapter by refactoring, using better naming conventions, updating diagrams to use the correct orientation, etc. (internal merge request 2968). * Fix minor typos for slink:VkImageCreateInfo and slink:VkImageStencilUsageCreateInfoEXT. * Add missing documentation for tlink:VkResolveModeFlagsKHR. * Fix extension dependency of pname:scalarBlockLayout in the <<features-features-requirements, Feature Requirements>> section. * Fix indexing math for shader binding table calculations in the <<shader-binding-table-indexing-rules, Indexing Rules>> section, and use spelling "`any-hit`" consistently. * Reconcile valid usage statement and text for sampled image layouts in slink:VkWriteDescriptorSet (https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/551). * Make SPIR-V code:OpConvertUToPtr and code:OpConvertPtrToU operations require a 64-bit integer for physical storage buffer pointers in the <<spirvenv-module-validation, Validation Rules within a Module>> section. * Update to KaTeX 10.0. New Extensions: * `VK_EXT_filter_cubic` * `VK_NV_dedicated_allocation_image_aliasing`
2019-02-04 09:26:23 +00:00
"""Defines ConsolePrinter, a BasePrinter subclass for appealing console output."""
# Copyright (c) 2018-2019 Collabora, Ltd.
#
# 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.
#
# Author(s): Ryan Pavlik <ryan.pavlik@collabora.com>
from pathlib import Path
from sys import exit, stdout
from .base_printer import BasePrinter
from .shared import (colored, getHighlightedRange, getInterestedRange,
toNameAndLine)
try:
from tabulate import tabulate_impl
HAVE_TABULATE = True
except ImportError:
HAVE_TABULATE = False
def colWidth(collection, columnNum):
"""Compute the required width of a column in a collection of row-tuples."""
MIN_PADDING = 5
return MIN_PADDING + max([len(row[columnNum]) for row in collection])
def alternateTabulate(collection, headers=None):
"""Minimal re-implementation of the tabulate module."""
if headers is None:
fullTable = collection
else:
underline = ['-' * len(header) for header in headers]
fullTable = [headers, underline] + collection
widths = [colWidth(collection, colNum)
for colNum in range(len(fullTable[0]))]
widths[-1] = None
lines = []
for row in fullTable:
fields = []
for data, width in zip(row, widths):
if width:
spaces = ' ' * (width - len(data))
fields.append(data + spaces)
else:
fields.append(data)
lines.append(''.join(fields))
return '\n'.join(lines)
def printTabulated(collection, headers=None):
"""Call either tabulate.tabulate(), or our internal alternateTabulate()."""
if HAVE_TABULATE:
print(tabulate_impl(collection, headers=headers))
else:
print(alternateTabulate(collection, headers=headers))
def printLineSubsetWithHighlighting(
line, start, end, highlightStart=None, highlightEnd=None, maxLen=120, replacement=None):
"""Print a (potential subset of a) line, with highlighting/underline and optional replacement.
Will print at least the characters line[start:end], and potentially more if possible
to do so without making the output too wide.
Will highlight (underline) line[highlightStart:highlightEnd], where the default
value for highlightStart is simply start, and the default value for highlightEnd is simply end.
Replacment, if supplied, will be aligned with the highlighted range.
Output is intended to look like part of a Clang compile error/warning message.
"""
# Fill in missing start/end with start/end of range.
if highlightStart is None:
highlightStart = start
if highlightEnd is None:
highlightEnd = end
# Expand interested range start/end.
start = min(start, highlightStart)
end = max(end, highlightEnd)
tildeLength = highlightEnd - highlightStart - 1
caretLoc = highlightStart
continuation = '[...]'
if len(line) > maxLen:
# Too long
# the max is to handle -1 from .find() (which indicates "not found")
followingSpaceIndex = max(end, line.find(' ', min(len(line), end + 1)))
# Maximum length has decreased by at least
# the length of a single continuation we absolutely need.
maxLen -= len(continuation)
if followingSpaceIndex <= maxLen:
# We can grab the whole beginning of the line,
# and not adjust caretLoc
line = line[:maxLen] + continuation
elif (len(line) - followingSpaceIndex) < 5:
# We need to truncate the beginning,
# but we're close to the end of line.
newBeginning = len(line) - maxLen
caretLoc += len(continuation)
caretLoc -= newBeginning
line = continuation + line[newBeginning:]
else:
# Need to truncate the beginning of the string too.
newEnd = followingSpaceIndex
# Now we need two continuations
# (and to adjust caret to the right accordingly)
maxLen -= len(continuation)
caretLoc += len(continuation)
newBeginning = newEnd - maxLen
caretLoc -= newBeginning
line = continuation + line[newBeginning:newEnd] + continuation
print(line)
spaces = ' ' * caretLoc
tildes = '~' * tildeLength
print(spaces + colored('^' + tildes, 'green'))
if replacement is not None:
print(spaces + colored(replacement, 'green'))
class ConsolePrinter(BasePrinter):
"""Implementation of BasePrinter for generating diagnostic reports in colored, helpful console output."""
###
# Output methods: these all print directly.
def outputResults(self, checker, broken_links=True,
missing_includes=False):
"""Output the full results of a checker run.
Includes the diagnostics, broken links (if desired),
and missing includes (if desired).
"""
self.output(checker)
if broken_links:
broken = checker.getBrokenLinks()
if len(broken) > 0:
self.outputBrokenLinks(checker, broken)
if missing_includes:
missing = checker.getMissingUnreferencedApiIncludes()
if len(missing) > 0:
self.outputMissingIncludes(checker, missing)
def outputBrokenLinks(self, checker, broken):
"""Output a table of broken links.
Called by self.outputResults().
"""
print('Missing API includes that are referenced by a linking macro: these result in broken links in the spec!')
def makeRowOfBroken(entity, uses):
fn = checker.findEntity(entity).filename
anchor = '[[{}]]'.format(entity)
locations = ', '.join([toNameAndLine(context, root_path=checker.root_path)
for context in uses])
return (fn, anchor, locations)
printTabulated(sorted([makeRowOfBroken(entity, uses)
for entity, uses in broken.items()]),
headers=['Include File', 'Anchor in lieu of include', 'Links to this entity'])
def outputMissingIncludes(self, checker, missing):
"""Output a table of missing includes.
Called by self.outputResults().
"""
print(
'Missing, but unreferenced, API includes/anchors - potentially not-documented entities:')
def makeRowOfMissing(entity):
fn = checker.findEntity(entity).filename
anchor = '[[{}]]'.format(entity)
return (fn, anchor)
printTabulated(sorted([makeRowOfMissing(entity) for entity in missing]),
headers=['Include File', 'Anchor in lieu of include'])
def outputMessage(self, msg):
"""Output a Message, with highlighted range and replacement, if appropriate."""
highlightStart, highlightEnd = getHighlightedRange(msg.context)
if '\n' in msg.context.filename:
# This is a multi-line string "filename".
# Extra blank line and delimiter line for readability:
print()
print('--------------------------------------------------------------------')
fileAndLine = colored('{}:'.format(
self.formatBrief(msg.context)), attrs=['bold'])
headingSize = len('{context}: {mtype}: '.format(
context=self.formatBrief(msg.context),
mtype=self.formatBrief(msg.message_type, False)))
indent = ' ' * headingSize
printedHeading = False
lines = msg.message[:]
if msg.see_also is not None and len(msg.see_also) != 0:
lines.append('See also:')
for see in msg.see_also:
lines.append(' {}'.format(self.formatBrief(see)))
if msg.fix is not None:
lines.append('Note: Auto-fix available')
for line in msg.message:
if not printedHeading:
scriptloc = ''
if msg.script_location:
scriptloc = ', ' + msg.script_location
print('{fileLine} {mtype} {msg} (-{arg}{loc})'.format(
fileLine=fileAndLine, mtype=msg.message_type.formattedWithColon(),
msg=colored(line, attrs=['bold']), arg=msg.message_id.enable_arg(), loc=scriptloc))
printedHeading = True
else:
print(colored(indent + line, attrs=['bold']))
if len(msg.message) > 1:
# extra blank line after multiline message
print('')
start, end = getInterestedRange(msg.context)
printLineSubsetWithHighlighting(
msg.context.line,
start, end,
highlightStart, highlightEnd,
replacement=msg.replacement)
def outputFallback(self, obj):
"""Output by calling print."""
print(obj)
###
# Format methods: these all return a string.
def formatFilename(self, fn, with_color=True):
"""Format a local filename, as a relative path if possible."""
return self.getRelativeFilename(fn)
def formatMessageTypeBrief(self, message_type, with_color=True):
"""Format a message type briefly, applying color if desired and possible.
Delegates to the superclass if not formatting with color.
"""
if with_color:
return message_type.formattedWithColon()
else:
return super(ConsolePrinter, self).formatMessageTypeBrief(
message_type, with_color)