summaryrefslogtreecommitdiff
path: root/codegen/vulkan/scripts/spec_tools/console_printer.py
blob: 18acabfd5e6a1bbc4d27cdce76d9df33f3f35ee5 (plain)
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
"""Defines ConsolePrinter, a BasePrinter subclass for appealing console output."""

# Copyright (c) 2018-2019 Collabora, Ltd.
#
# SPDX-License-Identifier: Apache-2.0
#
# Author(s):    Ryan Pavlik <ryan.pavlik@collabora.com>

from sys import 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."""
    # We need a list, not a generator or anything else.
    if not isinstance(collection, list):
        collection = list(collection)

    # Empty collection means no table
    if not collection:
        return None

    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:
        tabulated = tabulate_impl(collection, headers=headers)
    else:
        tabulated = alternateTabulate(collection, headers=headers)
    if tabulated:
        print(tabulated)


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

    stdout.buffer.write(line.encode('utf-8'))
    print()

    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."""

    def __init__(self):
        self.show_script_location = False
        super().__init__()

    ###
    # 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 broken:
                self.outputBrokenLinks(checker, broken)
        if missing_includes:
            missing = checker.getMissingUnreferencedApiIncludes()
            if missing:
                self.outputMissingIncludes(checker, missing)

    def outputBrokenLinks(self, checker, broken):
        """Output a table of broken links.

        Called by self.outputBrokenAndMissing() if requested.
        """
        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((makeRowOfBroken(entity, uses)
                        for entity, uses in sorted(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.outputBrokenAndMissing() if requested.
        """
        missing = list(sorted(missing))
        if not missing:
            # Exit if none
            return
        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((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:
            lines.append('See also:')
            lines.extend(('  {}'.format(self.formatBrief(see))
                          for see in msg.see_also))

        if msg.fix:
            lines.append('Note: Auto-fix available')

        for line in msg.message:
            if not printedHeading:
                scriptloc = ''
                if msg.script_location and self.show_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()
        return super(ConsolePrinter, self).formatMessageTypeBrief(
            message_type, with_color)