Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2016 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
Dan Albert | 06f58af | 2020-06-22 15:10:31 -0700 | [diff] [blame] | 16 | """Parser for Android's version script information.""" |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 17 | from __future__ import annotations |
| 18 | |
| 19 | from dataclasses import dataclass, field |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 20 | import logging |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 21 | import re |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 22 | from typing import ( |
| 23 | Dict, |
| 24 | Iterable, |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 25 | Iterator, |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 26 | List, |
| 27 | Mapping, |
| 28 | NewType, |
| 29 | Optional, |
| 30 | TextIO, |
| 31 | Tuple, |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 32 | Union, |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | |
| 36 | ApiMap = Mapping[str, int] |
| 37 | Arch = NewType('Arch', str) |
| 38 | Tag = NewType('Tag', str) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 39 | |
| 40 | |
| 41 | ALL_ARCHITECTURES = ( |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 42 | Arch('arm'), |
| 43 | Arch('arm64'), |
| 44 | Arch('x86'), |
| 45 | Arch('x86_64'), |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 49 | # Arbitrary magic number. We use the same one in api-level.h for this purpose. |
| 50 | FUTURE_API_LEVEL = 10000 |
| 51 | |
| 52 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 53 | def logger() -> logging.Logger: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 54 | """Return the main logger for this module.""" |
| 55 | return logging.getLogger(__name__) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 56 | |
| 57 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 58 | @dataclass |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 59 | class Tags: |
| 60 | """Container class for the tags attached to a symbol or version.""" |
| 61 | |
| 62 | tags: tuple[Tag, ...] = field(default_factory=tuple) |
| 63 | |
| 64 | @classmethod |
| 65 | def from_strs(cls, strs: Iterable[str]) -> Tags: |
| 66 | """Constructs tags from a collection of strings. |
| 67 | |
| 68 | Does not decode API levels. |
| 69 | """ |
| 70 | return Tags(tuple(Tag(s) for s in strs)) |
| 71 | |
| 72 | def __contains__(self, tag: Union[Tag, str]) -> bool: |
| 73 | return tag in self.tags |
| 74 | |
| 75 | def __iter__(self) -> Iterator[Tag]: |
| 76 | yield from self.tags |
| 77 | |
| 78 | @property |
| 79 | def has_mode_tags(self) -> bool: |
| 80 | """Returns True if any mode tags (apex, llndk, etc) are set.""" |
| 81 | return self.has_apex_tags or self.has_llndk_tags |
| 82 | |
| 83 | @property |
| 84 | def has_apex_tags(self) -> bool: |
| 85 | """Returns True if any APEX tags are set.""" |
Dan Albert | 56f52de | 2021-06-04 14:31:58 -0700 | [diff] [blame] | 86 | return 'apex' in self.tags or 'systemapi' in self.tags |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 87 | |
| 88 | @property |
| 89 | def has_llndk_tags(self) -> bool: |
| 90 | """Returns True if any LL-NDK tags are set.""" |
| 91 | return 'llndk' in self.tags |
| 92 | |
| 93 | @property |
| 94 | def has_platform_only_tags(self) -> bool: |
| 95 | """Returns True if any platform-only tags are set.""" |
| 96 | return 'platform-only' in self.tags |
| 97 | |
| 98 | |
| 99 | @dataclass |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 100 | class Symbol: |
| 101 | """A symbol definition from a symbol file.""" |
| 102 | |
| 103 | name: str |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 104 | tags: Tags |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 105 | |
| 106 | |
| 107 | @dataclass |
| 108 | class Version: |
| 109 | """A version block of a symbol file.""" |
| 110 | |
| 111 | name: str |
| 112 | base: Optional[str] |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 113 | tags: Tags |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 114 | symbols: List[Symbol] |
| 115 | |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 116 | @property |
| 117 | def is_private(self) -> bool: |
| 118 | """Returns True if this version block is private (platform only).""" |
| 119 | return self.name.endswith('_PRIVATE') or self.name.endswith('_PLATFORM') |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 120 | |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 121 | |
| 122 | def get_tags(line: str, api_map: ApiMap) -> Tags: |
Dan Albert | a85042a | 2016-07-28 16:58:27 -0700 | [diff] [blame] | 123 | """Returns a list of all tags on this line.""" |
| 124 | _, _, all_tags = line.strip().partition('#') |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 125 | return Tags(tuple( |
| 126 | decode_api_level_tag(Tag(e), api_map) |
| 127 | for e in re.split(r'\s+', all_tags) if e.strip() |
| 128 | )) |
Dan Albert | a85042a | 2016-07-28 16:58:27 -0700 | [diff] [blame] | 129 | |
| 130 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 131 | def is_api_level_tag(tag: Tag) -> bool: |
Dan Albert | 3f6fb2d | 2017-03-28 16:04:25 -0700 | [diff] [blame] | 132 | """Returns true if this tag has an API level that may need decoding.""" |
| 133 | if tag.startswith('introduced='): |
| 134 | return True |
| 135 | if tag.startswith('introduced-'): |
| 136 | return True |
| 137 | if tag.startswith('versioned='): |
| 138 | return True |
| 139 | return False |
| 140 | |
| 141 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 142 | def decode_api_level(api: str, api_map: ApiMap) -> int: |
Dan Albert | 06f58af | 2020-06-22 15:10:31 -0700 | [diff] [blame] | 143 | """Decodes the API level argument into the API level number. |
| 144 | |
| 145 | For the average case, this just decodes the integer value from the string, |
| 146 | but for unreleased APIs we need to translate from the API codename (like |
| 147 | "O") to the future API level for that codename. |
| 148 | """ |
| 149 | try: |
| 150 | return int(api) |
| 151 | except ValueError: |
| 152 | pass |
| 153 | |
| 154 | if api == "current": |
| 155 | return FUTURE_API_LEVEL |
| 156 | |
| 157 | return api_map[api] |
| 158 | |
| 159 | |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 160 | def decode_api_level_tag(tag: Tag, api_map: ApiMap) -> Tag: |
| 161 | """Decodes API level code name in a tag. |
Dan Albert | 3f6fb2d | 2017-03-28 16:04:25 -0700 | [diff] [blame] | 162 | |
| 163 | Raises: |
| 164 | ParseError: An unknown version name was found in a tag. |
| 165 | """ |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 166 | if not is_api_level_tag(tag): |
| 167 | return tag |
Dan Albert | 3f6fb2d | 2017-03-28 16:04:25 -0700 | [diff] [blame] | 168 | |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 169 | name, value = split_tag(tag) |
| 170 | try: |
| 171 | decoded = str(decode_api_level(value, api_map)) |
| 172 | return Tag(f'{name}={decoded}') |
| 173 | except KeyError as ex: |
| 174 | raise ParseError(f'Unknown version name in tag: {tag}') from ex |
Dan Albert | 3f6fb2d | 2017-03-28 16:04:25 -0700 | [diff] [blame] | 175 | |
| 176 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 177 | def split_tag(tag: Tag) -> Tuple[str, str]: |
Dan Albert | 3f6fb2d | 2017-03-28 16:04:25 -0700 | [diff] [blame] | 178 | """Returns a key/value tuple of the tag. |
| 179 | |
| 180 | Raises: |
| 181 | ValueError: Tag is not a key/value type tag. |
| 182 | |
| 183 | Returns: Tuple of (key, value) of the tag. Both components are strings. |
| 184 | """ |
| 185 | if '=' not in tag: |
| 186 | raise ValueError('Not a key/value tag: ' + tag) |
| 187 | key, _, value = tag.partition('=') |
| 188 | return key, value |
| 189 | |
| 190 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 191 | def get_tag_value(tag: Tag) -> str: |
Dan Albert | c42458e | 2016-07-29 13:05:39 -0700 | [diff] [blame] | 192 | """Returns the value of a key/value tag. |
| 193 | |
| 194 | Raises: |
| 195 | ValueError: Tag is not a key/value type tag. |
| 196 | |
| 197 | Returns: Value part of tag as a string. |
| 198 | """ |
Dan Albert | 3f6fb2d | 2017-03-28 16:04:25 -0700 | [diff] [blame] | 199 | return split_tag(tag)[1] |
Dan Albert | c42458e | 2016-07-29 13:05:39 -0700 | [diff] [blame] | 200 | |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 201 | class Filter: |
| 202 | """A filter encapsulates a condition that tells whether a version or a |
| 203 | symbol should be omitted or not |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 204 | """ |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 205 | |
| 206 | def __init__(self, arch: Arch, api: int, llndk: bool = False, apex: bool = False): |
| 207 | self.arch = arch |
| 208 | self.api = api |
| 209 | self.llndk = llndk |
| 210 | self.apex = apex |
| 211 | |
| 212 | def _should_omit_tags(self, tags: Tags) -> bool: |
| 213 | """Returns True if the tagged object should be omitted. |
| 214 | |
| 215 | This defines the rules shared between version tagging and symbol tagging. |
| 216 | """ |
| 217 | # The apex and llndk tags will only exclude APIs from other modes. If in |
| 218 | # APEX or LLNDK mode and neither tag is provided, we fall back to the |
| 219 | # default behavior because all NDK symbols are implicitly available to |
| 220 | # APEX and LLNDK. |
| 221 | if tags.has_mode_tags: |
| 222 | if not self.apex and not self.llndk: |
| 223 | return True |
| 224 | if self.apex and not tags.has_apex_tags: |
| 225 | return True |
| 226 | if self.llndk and not tags.has_llndk_tags: |
| 227 | return True |
| 228 | if not symbol_in_arch(tags, self.arch): |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 229 | return True |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 230 | if not symbol_in_api(tags, self.arch, self.api): |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 231 | return True |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 232 | return False |
| 233 | |
| 234 | def should_omit_version(self, version: Version) -> bool: |
| 235 | """Returns True if the version section should be omitted. |
| 236 | |
| 237 | We want to omit any sections that do not have any symbols we'll have in |
| 238 | the stub library. Sections that contain entirely future symbols or only |
| 239 | symbols for certain architectures. |
| 240 | """ |
| 241 | if version.is_private: |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 242 | return True |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 243 | if version.tags.has_platform_only_tags: |
| 244 | return True |
| 245 | return self._should_omit_tags(version.tags) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 246 | |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 247 | def should_omit_symbol(self, symbol: Symbol) -> bool: |
| 248 | """Returns True if the symbol should be omitted.""" |
| 249 | return self._should_omit_tags(symbol.tags) |
Dan Albert | 08532b6 | 2016-07-28 18:09:47 -0700 | [diff] [blame] | 250 | |
| 251 | |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 252 | def symbol_in_arch(tags: Tags, arch: Arch) -> bool: |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 253 | """Returns true if the symbol is present for the given architecture.""" |
| 254 | has_arch_tags = False |
| 255 | for tag in tags: |
| 256 | if tag == arch: |
| 257 | return True |
| 258 | if tag in ALL_ARCHITECTURES: |
| 259 | has_arch_tags = True |
| 260 | |
| 261 | # If there were no arch tags, the symbol is available for all |
| 262 | # architectures. If there were any arch tags, the symbol is only available |
| 263 | # for the tagged architectures. |
| 264 | return not has_arch_tags |
| 265 | |
| 266 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 267 | def symbol_in_api(tags: Iterable[Tag], arch: Arch, api: int) -> bool: |
Dan Albert | c42458e | 2016-07-29 13:05:39 -0700 | [diff] [blame] | 268 | """Returns true if the symbol is present for the given API level.""" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 269 | introduced_tag = None |
| 270 | arch_specific = False |
| 271 | for tag in tags: |
| 272 | # If there is an arch-specific tag, it should override the common one. |
| 273 | if tag.startswith('introduced=') and not arch_specific: |
| 274 | introduced_tag = tag |
| 275 | elif tag.startswith('introduced-' + arch + '='): |
| 276 | introduced_tag = tag |
| 277 | arch_specific = True |
Dan Albert | a85042a | 2016-07-28 16:58:27 -0700 | [diff] [blame] | 278 | elif tag == 'future': |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 279 | return api == FUTURE_API_LEVEL |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 280 | |
| 281 | if introduced_tag is None: |
| 282 | # We found no "introduced" tags, so the symbol has always been |
| 283 | # available. |
| 284 | return True |
| 285 | |
Dan Albert | c42458e | 2016-07-29 13:05:39 -0700 | [diff] [blame] | 286 | return api >= int(get_tag_value(introduced_tag)) |
| 287 | |
| 288 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 289 | def symbol_versioned_in_api(tags: Iterable[Tag], api: int) -> bool: |
Dan Albert | c42458e | 2016-07-29 13:05:39 -0700 | [diff] [blame] | 290 | """Returns true if the symbol should be versioned for the given API. |
| 291 | |
| 292 | This models the `versioned=API` tag. This should be a very uncommonly |
| 293 | needed tag, and is really only needed to fix versioning mistakes that are |
| 294 | already out in the wild. |
| 295 | |
| 296 | For example, some of libc's __aeabi_* functions were originally placed in |
| 297 | the private version, but that was incorrect. They are now in LIBC_N, but |
| 298 | when building against any version prior to N we need the symbol to be |
| 299 | unversioned (otherwise it won't resolve on M where it is private). |
| 300 | """ |
| 301 | for tag in tags: |
| 302 | if tag.startswith('versioned='): |
| 303 | return api >= int(get_tag_value(tag)) |
| 304 | # If there is no "versioned" tag, the tag has been versioned for as long as |
| 305 | # it was introduced. |
| 306 | return True |
| 307 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 308 | |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 309 | class ParseError(RuntimeError): |
| 310 | """An error that occurred while parsing a symbol file.""" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 311 | |
| 312 | |
Dan Albert | 756f2d0 | 2018-10-09 16:36:03 -0700 | [diff] [blame] | 313 | class MultiplyDefinedSymbolError(RuntimeError): |
| 314 | """A symbol name was multiply defined.""" |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 315 | def __init__(self, multiply_defined_symbols: Iterable[str]) -> None: |
| 316 | super().__init__( |
Dan Albert | 756f2d0 | 2018-10-09 16:36:03 -0700 | [diff] [blame] | 317 | 'Version script contains multiple definitions for: {}'.format( |
| 318 | ', '.join(multiply_defined_symbols))) |
| 319 | self.multiply_defined_symbols = multiply_defined_symbols |
| 320 | |
| 321 | |
Dan Albert | 802cc82 | 2020-06-22 15:59:12 -0700 | [diff] [blame] | 322 | class SymbolFileParser: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 323 | """Parses NDK symbol files.""" |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 324 | def __init__(self, input_file: TextIO, api_map: ApiMap, filt: Filter) -> None: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 325 | self.input_file = input_file |
Dan Albert | 3f6fb2d | 2017-03-28 16:04:25 -0700 | [diff] [blame] | 326 | self.api_map = api_map |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 327 | self.filter = filt |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 328 | self.current_line: Optional[str] = None |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 329 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 330 | def parse(self) -> List[Version]: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 331 | """Parses the symbol file and returns a list of Version objects.""" |
| 332 | versions = [] |
Spandan Das | 3f5659f | 2021-08-19 19:31:54 +0000 | [diff] [blame] | 333 | while self.next_line(): |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 334 | assert self.current_line is not None |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 335 | if '{' in self.current_line: |
| 336 | versions.append(self.parse_version()) |
| 337 | else: |
| 338 | raise ParseError( |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 339 | f'Unexpected contents at top level: {self.current_line}') |
Dan Albert | 756f2d0 | 2018-10-09 16:36:03 -0700 | [diff] [blame] | 340 | |
| 341 | self.check_no_duplicate_symbols(versions) |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 342 | return versions |
| 343 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 344 | def check_no_duplicate_symbols(self, versions: Iterable[Version]) -> None: |
Dan Albert | 756f2d0 | 2018-10-09 16:36:03 -0700 | [diff] [blame] | 345 | """Raises errors for multiply defined symbols. |
| 346 | |
| 347 | This situation is the normal case when symbol versioning is actually |
| 348 | used, but this script doesn't currently handle that. The error message |
| 349 | will be a not necessarily obvious "error: redefition of 'foo'" from |
| 350 | stub.c, so it's better for us to catch this situation and raise a |
| 351 | better error. |
| 352 | """ |
| 353 | symbol_names = set() |
| 354 | multiply_defined_symbols = set() |
| 355 | for version in versions: |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 356 | if self.filter.should_omit_version(version): |
Dan Albert | 756f2d0 | 2018-10-09 16:36:03 -0700 | [diff] [blame] | 357 | continue |
| 358 | |
| 359 | for symbol in version.symbols: |
Jiyong Park | 3f9c41d | 2022-07-16 23:30:09 +0900 | [diff] [blame^] | 360 | if self.filter.should_omit_symbol(symbol): |
Dan Albert | 756f2d0 | 2018-10-09 16:36:03 -0700 | [diff] [blame] | 361 | continue |
| 362 | |
| 363 | if symbol.name in symbol_names: |
| 364 | multiply_defined_symbols.add(symbol.name) |
| 365 | symbol_names.add(symbol.name) |
| 366 | if multiply_defined_symbols: |
| 367 | raise MultiplyDefinedSymbolError( |
| 368 | sorted(list(multiply_defined_symbols))) |
| 369 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 370 | def parse_version(self) -> Version: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 371 | """Parses a single version section and returns a Version object.""" |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 372 | assert self.current_line is not None |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 373 | name = self.current_line.split('{')[0].strip() |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 374 | tags = get_tags(self.current_line, self.api_map) |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 375 | symbols: List[Symbol] = [] |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 376 | global_scope = True |
dimitry | 2be7fa9 | 2017-11-21 17:47:33 +0100 | [diff] [blame] | 377 | cpp_symbols = False |
Spandan Das | 3f5659f | 2021-08-19 19:31:54 +0000 | [diff] [blame] | 378 | while self.next_line(): |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 379 | if '}' in self.current_line: |
| 380 | # Line is something like '} BASE; # tags'. Both base and tags |
| 381 | # are optional here. |
| 382 | base = self.current_line.partition('}')[2] |
| 383 | base = base.partition('#')[0].strip() |
| 384 | if not base.endswith(';'): |
| 385 | raise ParseError( |
dimitry | 2be7fa9 | 2017-11-21 17:47:33 +0100 | [diff] [blame] | 386 | 'Unterminated version/export "C++" block (expected ;).') |
| 387 | if cpp_symbols: |
| 388 | cpp_symbols = False |
| 389 | else: |
| 390 | base = base.rstrip(';').rstrip() |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 391 | return Version(name, base or None, tags, symbols) |
dimitry | 2be7fa9 | 2017-11-21 17:47:33 +0100 | [diff] [blame] | 392 | elif 'extern "C++" {' in self.current_line: |
| 393 | cpp_symbols = True |
| 394 | elif not cpp_symbols and ':' in self.current_line: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 395 | visibility = self.current_line.split(':')[0].strip() |
| 396 | if visibility == 'local': |
| 397 | global_scope = False |
| 398 | elif visibility == 'global': |
| 399 | global_scope = True |
| 400 | else: |
| 401 | raise ParseError('Unknown visiblity label: ' + visibility) |
dimitry | 2be7fa9 | 2017-11-21 17:47:33 +0100 | [diff] [blame] | 402 | elif global_scope and not cpp_symbols: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 403 | symbols.append(self.parse_symbol()) |
| 404 | else: |
Dan Albert | f50b6ce | 2018-09-25 13:39:25 -0700 | [diff] [blame] | 405 | # We're in a hidden scope or in 'extern "C++"' block. Ignore |
| 406 | # everything. |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 407 | pass |
| 408 | raise ParseError('Unexpected EOF in version block.') |
| 409 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 410 | def parse_symbol(self) -> Symbol: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 411 | """Parses a single symbol line and returns a Symbol object.""" |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 412 | assert self.current_line is not None |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 413 | if ';' not in self.current_line: |
| 414 | raise ParseError( |
| 415 | 'Expected ; to terminate symbol: ' + self.current_line) |
| 416 | if '*' in self.current_line: |
| 417 | raise ParseError( |
| 418 | 'Wildcard global symbols are not permitted.') |
| 419 | # Line is now in the format "<symbol-name>; # tags" |
| 420 | name, _, _ = self.current_line.strip().partition(';') |
Dan Albert | ead2155 | 2021-06-04 14:30:40 -0700 | [diff] [blame] | 421 | tags = get_tags(self.current_line, self.api_map) |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 422 | return Symbol(name, tags) |
| 423 | |
Dan Albert | af7b36d | 2020-06-23 11:21:21 -0700 | [diff] [blame] | 424 | def next_line(self) -> str: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 425 | """Returns the next non-empty non-comment line. |
| 426 | |
| 427 | A return value of '' indicates EOF. |
| 428 | """ |
| 429 | line = self.input_file.readline() |
Spandan Das | 3f5659f | 2021-08-19 19:31:54 +0000 | [diff] [blame] | 430 | while not line.strip() or line.strip().startswith('#'): |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 431 | line = self.input_file.readline() |
| 432 | |
| 433 | # We want to skip empty lines, but '' indicates EOF. |
Spandan Das | 3f5659f | 2021-08-19 19:31:54 +0000 | [diff] [blame] | 434 | if not line: |
Dan Albert | 8bdccb9 | 2016-07-29 13:06:22 -0700 | [diff] [blame] | 435 | break |
| 436 | self.current_line = line |
| 437 | return self.current_line |