• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2#
3# Copyright (c) 2018-2019 Collabora, Ltd.
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7# Author(s):    Ryan Pavlik <ryan.pavlik@collabora.com>
8#
9# Purpose:      This file performs some basic checks of the custom macros
10#               used in the AsciiDoctor source for the spec, especially
11#               related to the validity of the entities linked-to.
12
13from pathlib import Path
14
15from reg import Registry
16from spec_tools.entity_db import EntityDatabase
17from spec_tools.macro_checker import MacroChecker
18from spec_tools.macro_checker_file import MacroCheckerFile
19from spec_tools.main import checkerMain
20from spec_tools.shared import (AUTO_FIX_STRING, EXTENSION_CATEGORY, MessageId,
21                               MessageType)
22
23###
24# "Configuration" constants
25
26FREEFORM_CATEGORY = 'freeform'
27
28# defines mentioned in spec but not needed in registry
29EXTRA_DEFINES = ('VKAPI_ATTR', 'VKAPI_CALL', 'VKAPI_PTR', 'VK_NO_STDINT_H')
30
31# Extra freeform refpages in addition to EXTRA_DEFINES
32EXTRA_REFPAGES = ('WSIheaders', 'provisional-headers')
33
34# These are marked with the code: macro
35SYSTEM_TYPES = set(('void', 'char', 'float', 'size_t', 'uintptr_t',
36                    'int8_t', 'uint8_t',
37                    'int32_t', 'uint32_t',
38                    'int64_t', 'uint64_t'))
39
40ROOT = Path(__file__).resolve().parent.parent
41DEFAULT_DISABLED_MESSAGES = set((
42    MessageId.LEGACY,
43    MessageId.REFPAGE_MISSING,
44    MessageId.MISSING_MACRO,
45    MessageId.EXTENSION,
46    # TODO *text macro checking actually needs fixing for Vulkan
47    MessageId.MISUSED_TEXT,
48    MessageId.MISSING_TEXT
49))
50
51CWD = Path('.').resolve()
52
53
54class VulkanEntityDatabase(EntityDatabase):
55    """Vulkan-specific subclass of EntityDatabase."""
56
57    def __init__(self, *args, **kwargs):
58        super().__init__(*args, **kwargs)
59        self._conditionally_recognized = set(('fname', 'sname'))
60
61    def makeRegistry(self):
62        registryFile = str(ROOT / 'xml/vk.xml')
63        registry = Registry()
64        registry.loadFile(registryFile)
65        return registry
66
67    def getNamePrefix(self):
68        return "vk"
69
70    def getPlatformRequires(self):
71        return 'vk_platform'
72
73    def getSystemTypes(self):
74        return SYSTEM_TYPES
75
76    def populateMacros(self):
77        self.addMacros('t', ['link', 'name'], ['funcpointers', 'flags'])
78
79    def populateEntities(self):
80        # These are not mentioned in the XML
81        for name in EXTRA_DEFINES:
82            self.addEntity(name, 'dlink',
83                           category=FREEFORM_CATEGORY, generates=False)
84        for name in EXTRA_REFPAGES:
85            self.addEntity(name, 'code',
86                           category=FREEFORM_CATEGORY, generates=False)
87
88    def shouldBeRecognized(self, macro, entity_name):
89        """Determine, based on the macro and the name provided, if we should expect to recognize the entity."""
90        if super().shouldBeRecognized(macro, entity_name):
91            return True
92
93        # The *name: macros in Vulkan should also be recognized if the entity name matches the pattern.
94        if macro in self._conditionally_recognized and self.likelyRecognizedEntity(entity_name):
95            return True
96        return False
97
98
99class VulkanMacroCheckerFile(MacroCheckerFile):
100    """Vulkan-specific subclass of MacroCheckerFile."""
101
102    def handleWrongMacro(self, msg, data):
103        """Report an appropriate message when we found that the macro used is incorrect.
104
105        May be overridden depending on each API's behavior regarding macro misuse:
106        e.g. in some cases, it may be considered a MessageId.LEGACY warning rather than
107        a MessageId.WRONG_MACRO or MessageId.EXTENSION.
108        """
109        message_type = MessageType.WARNING
110        message_id = MessageId.WRONG_MACRO
111        group = 'macro'
112
113        if data.category == EXTENSION_CATEGORY:
114            # Ah, this is an extension
115            msg.append(
116                'This is apparently an extension name, which should be marked up as a link.')
117            message_id = MessageId.EXTENSION
118            group = None  # replace the whole thing
119        else:
120            # Non-extension, we found the macro though.
121            if data.macro[0] == self.macro[0] and data.macro[1:] == 'link' and self.macro[1:] == 'name':
122                # First letter matches, old is 'name', new is 'link':
123                # This is legacy markup
124                msg.append(
125                    'This is legacy markup that has not been updated yet.')
126                message_id = MessageId.LEGACY
127            else:
128                # Not legacy, just wrong.
129                message_type = MessageType.ERROR
130
131        msg.append(AUTO_FIX_STRING)
132        self.diag(message_type, message_id, msg,
133                  group=group, replacement=self.makeMacroMarkup(data=data), fix=self.makeFix(data=data))
134
135
136def makeMacroChecker(enabled_messages):
137    """Create a correctly-configured MacroChecker instance."""
138    entity_db = VulkanEntityDatabase()
139    return MacroChecker(enabled_messages, entity_db, VulkanMacroCheckerFile, ROOT)
140
141
142if __name__ == '__main__':
143    default_enabled_messages = set(MessageId).difference(
144        DEFAULT_DISABLED_MESSAGES)
145
146    all_docs = [str(fn)
147                for fn in sorted((ROOT / 'chapters/').glob('**/*.txt'))]
148    all_docs.extend([str(fn)
149                     for fn in sorted((ROOT / 'appendices/').glob('**/*.txt'))])
150
151    checkerMain(default_enabled_messages, makeMacroChecker,
152                all_docs)
153