2016-06-18 01:45:24 +02:00
|
|
|
#
|
|
|
|
# Copyright (C) 2016 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2020-06-23 00:10:31 +02:00
|
|
|
"""Parser for Android's version script information."""
|
2021-06-04 23:30:40 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass, field
|
2016-07-29 22:06:22 +02:00
|
|
|
import logging
|
2016-06-18 01:45:24 +02:00
|
|
|
import re
|
2020-06-23 20:21:21 +02:00
|
|
|
from typing import (
|
|
|
|
Dict,
|
|
|
|
Iterable,
|
2021-06-04 23:30:40 +02:00
|
|
|
Iterator,
|
2020-06-23 20:21:21 +02:00
|
|
|
List,
|
|
|
|
Mapping,
|
|
|
|
NewType,
|
|
|
|
Optional,
|
|
|
|
TextIO,
|
|
|
|
Tuple,
|
2021-06-04 23:30:40 +02:00
|
|
|
Union,
|
2020-06-23 20:21:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
ApiMap = Mapping[str, int]
|
|
|
|
Arch = NewType('Arch', str)
|
|
|
|
Tag = NewType('Tag', str)
|
2016-06-18 01:45:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
ALL_ARCHITECTURES = (
|
2020-06-23 20:21:21 +02:00
|
|
|
Arch('arm'),
|
|
|
|
Arch('arm64'),
|
|
|
|
Arch('x86'),
|
|
|
|
Arch('x86_64'),
|
2016-06-18 01:45:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-11-08 22:35:12 +01:00
|
|
|
# Arbitrary magic number. We use the same one in api-level.h for this purpose.
|
|
|
|
FUTURE_API_LEVEL = 10000
|
|
|
|
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def logger() -> logging.Logger:
|
2016-07-29 22:06:22 +02:00
|
|
|
"""Return the main logger for this module."""
|
|
|
|
return logging.getLogger(__name__)
|
2016-06-18 01:45:24 +02:00
|
|
|
|
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
@dataclass
|
|
|
|
class Tags:
|
|
|
|
"""Container class for the tags attached to a symbol or version."""
|
|
|
|
|
|
|
|
tags: tuple[Tag, ...] = field(default_factory=tuple)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_strs(cls, strs: Iterable[str]) -> Tags:
|
|
|
|
"""Constructs tags from a collection of strings.
|
|
|
|
|
|
|
|
Does not decode API levels.
|
|
|
|
"""
|
|
|
|
return Tags(tuple(Tag(s) for s in strs))
|
|
|
|
|
|
|
|
def __contains__(self, tag: Union[Tag, str]) -> bool:
|
|
|
|
return tag in self.tags
|
|
|
|
|
|
|
|
def __iter__(self) -> Iterator[Tag]:
|
|
|
|
yield from self.tags
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_mode_tags(self) -> bool:
|
|
|
|
"""Returns True if any mode tags (apex, llndk, etc) are set."""
|
2022-07-17 04:30:47 +02:00
|
|
|
return self.has_apex_tags or self.has_llndk_tags or self.has_systemapi_tags
|
2021-06-04 23:30:40 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def has_apex_tags(self) -> bool:
|
|
|
|
"""Returns True if any APEX tags are set."""
|
2022-07-17 04:30:47 +02:00
|
|
|
return 'apex' in self.tags
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_systemapi_tags(self) -> bool:
|
|
|
|
"""Returns True if any APEX tags are set."""
|
|
|
|
return 'systemapi' in self.tags
|
2021-06-04 23:30:40 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def has_llndk_tags(self) -> bool:
|
|
|
|
"""Returns True if any LL-NDK tags are set."""
|
|
|
|
return 'llndk' in self.tags
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_platform_only_tags(self) -> bool:
|
|
|
|
"""Returns True if any platform-only tags are set."""
|
|
|
|
return 'platform-only' in self.tags
|
|
|
|
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
@dataclass
|
|
|
|
class Symbol:
|
|
|
|
"""A symbol definition from a symbol file."""
|
|
|
|
|
|
|
|
name: str
|
2021-06-04 23:30:40 +02:00
|
|
|
tags: Tags
|
2020-06-23 20:21:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Version:
|
|
|
|
"""A version block of a symbol file."""
|
|
|
|
|
|
|
|
name: str
|
|
|
|
base: Optional[str]
|
2021-06-04 23:30:40 +02:00
|
|
|
tags: Tags
|
2020-06-23 20:21:21 +02:00
|
|
|
symbols: List[Symbol]
|
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
@property
|
|
|
|
def is_private(self) -> bool:
|
|
|
|
"""Returns True if this version block is private (platform only)."""
|
|
|
|
return self.name.endswith('_PRIVATE') or self.name.endswith('_PLATFORM')
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
def get_tags(line: str, api_map: ApiMap) -> Tags:
|
2017-03-29 01:04:25 +02:00
|
|
|
"""Returns a list of all tags on this line."""
|
|
|
|
_, _, all_tags = line.strip().partition('#')
|
2021-06-04 23:30:40 +02:00
|
|
|
return Tags(tuple(
|
|
|
|
decode_api_level_tag(Tag(e), api_map)
|
|
|
|
for e in re.split(r'\s+', all_tags) if e.strip()
|
|
|
|
))
|
2017-03-29 01:04:25 +02:00
|
|
|
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def is_api_level_tag(tag: Tag) -> bool:
|
2017-03-29 01:04:25 +02:00
|
|
|
"""Returns true if this tag has an API level that may need decoding."""
|
|
|
|
if tag.startswith('introduced='):
|
|
|
|
return True
|
|
|
|
if tag.startswith('introduced-'):
|
|
|
|
return True
|
|
|
|
if tag.startswith('versioned='):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-11-08 22:35:12 +01:00
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def decode_api_level(api: str, api_map: ApiMap) -> int:
|
2020-06-23 00:10:31 +02:00
|
|
|
"""Decodes the API level argument into the API level number.
|
|
|
|
|
|
|
|
For the average case, this just decodes the integer value from the string,
|
|
|
|
but for unreleased APIs we need to translate from the API codename (like
|
|
|
|
"O") to the future API level for that codename.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return int(api)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if api == "current":
|
|
|
|
return FUTURE_API_LEVEL
|
|
|
|
|
|
|
|
return api_map[api]
|
|
|
|
|
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
def decode_api_level_tag(tag: Tag, api_map: ApiMap) -> Tag:
|
|
|
|
"""Decodes API level code name in a tag.
|
2016-11-08 22:35:12 +01:00
|
|
|
|
2017-03-29 01:04:25 +02:00
|
|
|
Raises:
|
|
|
|
ParseError: An unknown version name was found in a tag.
|
2016-11-08 22:35:12 +01:00
|
|
|
"""
|
2021-06-04 23:30:40 +02:00
|
|
|
if not is_api_level_tag(tag):
|
|
|
|
return tag
|
2016-11-08 22:35:12 +01:00
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
name, value = split_tag(tag)
|
|
|
|
try:
|
|
|
|
decoded = str(decode_api_level(value, api_map))
|
|
|
|
return Tag(f'{name}={decoded}')
|
|
|
|
except KeyError as ex:
|
|
|
|
raise ParseError(f'Unknown version name in tag: {tag}') from ex
|
2016-11-08 22:35:12 +01:00
|
|
|
|
2017-03-29 01:04:25 +02:00
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def split_tag(tag: Tag) -> Tuple[str, str]:
|
2017-03-29 01:04:25 +02:00
|
|
|
"""Returns a key/value tuple of the tag.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: Tag is not a key/value type tag.
|
|
|
|
|
|
|
|
Returns: Tuple of (key, value) of the tag. Both components are strings.
|
|
|
|
"""
|
|
|
|
if '=' not in tag:
|
|
|
|
raise ValueError('Not a key/value tag: ' + tag)
|
|
|
|
key, _, value = tag.partition('=')
|
|
|
|
return key, value
|
2016-07-29 01:58:27 +02:00
|
|
|
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def get_tag_value(tag: Tag) -> str:
|
2016-07-29 22:05:39 +02:00
|
|
|
"""Returns the value of a key/value tag.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: Tag is not a key/value type tag.
|
|
|
|
|
|
|
|
Returns: Value part of tag as a string.
|
|
|
|
"""
|
2017-03-29 01:04:25 +02:00
|
|
|
return split_tag(tag)[1]
|
2016-07-29 22:05:39 +02:00
|
|
|
|
2022-07-16 16:30:09 +02:00
|
|
|
class Filter:
|
|
|
|
"""A filter encapsulates a condition that tells whether a version or a
|
|
|
|
symbol should be omitted or not
|
|
|
|
"""
|
2016-07-29 22:05:39 +02:00
|
|
|
|
2022-09-26 13:58:27 +02:00
|
|
|
def __init__(self, arch: Arch, api: int, llndk: bool = False, apex: bool = False, systemapi:
|
|
|
|
bool = False, ndk: bool = True):
|
2022-07-16 16:30:09 +02:00
|
|
|
self.arch = arch
|
|
|
|
self.api = api
|
|
|
|
self.llndk = llndk
|
|
|
|
self.apex = apex
|
2022-07-17 04:30:47 +02:00
|
|
|
self.systemapi = systemapi
|
2022-09-26 13:58:27 +02:00
|
|
|
self.ndk = ndk
|
2021-06-04 23:30:40 +02:00
|
|
|
|
2022-07-16 16:30:09 +02:00
|
|
|
def _should_omit_tags(self, tags: Tags) -> bool:
|
|
|
|
"""Returns True if the tagged object should be omitted.
|
|
|
|
|
|
|
|
This defines the rules shared between version tagging and symbol tagging.
|
|
|
|
"""
|
|
|
|
# The apex and llndk tags will only exclude APIs from other modes. If in
|
|
|
|
# APEX or LLNDK mode and neither tag is provided, we fall back to the
|
|
|
|
# default behavior because all NDK symbols are implicitly available to
|
|
|
|
# APEX and LLNDK.
|
|
|
|
if tags.has_mode_tags:
|
2022-07-17 04:30:47 +02:00
|
|
|
if self.apex and tags.has_apex_tags:
|
|
|
|
return False
|
|
|
|
if self.llndk and tags.has_llndk_tags:
|
|
|
|
return False
|
|
|
|
if self.systemapi and tags.has_systemapi_tags:
|
|
|
|
return False
|
|
|
|
return True
|
2022-07-16 16:30:09 +02:00
|
|
|
if not symbol_in_arch(tags, self.arch):
|
2021-06-04 23:30:40 +02:00
|
|
|
return True
|
2022-07-16 16:30:09 +02:00
|
|
|
if not symbol_in_api(tags, self.arch, self.api):
|
2021-06-04 23:30:40 +02:00
|
|
|
return True
|
2022-07-16 16:30:09 +02:00
|
|
|
return False
|
2016-06-18 01:45:24 +02:00
|
|
|
|
2022-07-16 16:30:09 +02:00
|
|
|
def should_omit_version(self, version: Version) -> bool:
|
|
|
|
"""Returns True if the version section should be omitted.
|
2016-06-18 01:45:24 +02:00
|
|
|
|
2022-07-16 16:30:09 +02:00
|
|
|
We want to omit any sections that do not have any symbols we'll have in
|
|
|
|
the stub library. Sections that contain entirely future symbols or only
|
|
|
|
symbols for certain architectures.
|
|
|
|
"""
|
|
|
|
if version.is_private:
|
|
|
|
return True
|
|
|
|
if version.tags.has_platform_only_tags:
|
|
|
|
return True
|
|
|
|
return self._should_omit_tags(version.tags)
|
2018-10-10 01:36:03 +02:00
|
|
|
|
2022-07-16 16:30:09 +02:00
|
|
|
def should_omit_symbol(self, symbol: Symbol) -> bool:
|
|
|
|
"""Returns True if the symbol should be omitted."""
|
2022-09-26 13:58:27 +02:00
|
|
|
if not symbol.tags.has_mode_tags and not self.ndk:
|
|
|
|
# Symbols that don't have mode tags are NDK. They are usually
|
|
|
|
# included, but have to be omitted if NDK symbols are explicitly
|
|
|
|
# filtered-out
|
|
|
|
return True
|
2016-07-29 03:09:47 +02:00
|
|
|
|
2022-09-26 13:58:27 +02:00
|
|
|
return self._should_omit_tags(symbol.tags)
|
2016-07-29 03:09:47 +02:00
|
|
|
|
2021-06-04 23:30:40 +02:00
|
|
|
def symbol_in_arch(tags: Tags, arch: Arch) -> bool:
|
2016-06-18 01:45:24 +02:00
|
|
|
"""Returns true if the symbol is present for the given architecture."""
|
|
|
|
has_arch_tags = False
|
|
|
|
for tag in tags:
|
|
|
|
if tag == arch:
|
|
|
|
return True
|
|
|
|
if tag in ALL_ARCHITECTURES:
|
|
|
|
has_arch_tags = True
|
|
|
|
|
|
|
|
# If there were no arch tags, the symbol is available for all
|
|
|
|
# architectures. If there were any arch tags, the symbol is only available
|
|
|
|
# for the tagged architectures.
|
|
|
|
return not has_arch_tags
|
|
|
|
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def symbol_in_api(tags: Iterable[Tag], arch: Arch, api: int) -> bool:
|
2016-07-29 22:05:39 +02:00
|
|
|
"""Returns true if the symbol is present for the given API level."""
|
2016-06-18 01:45:24 +02:00
|
|
|
introduced_tag = None
|
|
|
|
arch_specific = False
|
|
|
|
for tag in tags:
|
|
|
|
# If there is an arch-specific tag, it should override the common one.
|
|
|
|
if tag.startswith('introduced=') and not arch_specific:
|
|
|
|
introduced_tag = tag
|
|
|
|
elif tag.startswith('introduced-' + arch + '='):
|
|
|
|
introduced_tag = tag
|
|
|
|
arch_specific = True
|
2016-07-29 01:58:27 +02:00
|
|
|
elif tag == 'future':
|
2016-11-08 22:35:12 +01:00
|
|
|
return api == FUTURE_API_LEVEL
|
2016-06-18 01:45:24 +02:00
|
|
|
|
|
|
|
if introduced_tag is None:
|
|
|
|
# We found no "introduced" tags, so the symbol has always been
|
|
|
|
# available.
|
|
|
|
return True
|
|
|
|
|
2016-07-29 22:05:39 +02:00
|
|
|
return api >= int(get_tag_value(introduced_tag))
|
|
|
|
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def symbol_versioned_in_api(tags: Iterable[Tag], api: int) -> bool:
|
2016-07-29 22:05:39 +02:00
|
|
|
"""Returns true if the symbol should be versioned for the given API.
|
|
|
|
|
|
|
|
This models the `versioned=API` tag. This should be a very uncommonly
|
|
|
|
needed tag, and is really only needed to fix versioning mistakes that are
|
|
|
|
already out in the wild.
|
|
|
|
|
|
|
|
For example, some of libc's __aeabi_* functions were originally placed in
|
|
|
|
the private version, but that was incorrect. They are now in LIBC_N, but
|
|
|
|
when building against any version prior to N we need the symbol to be
|
|
|
|
unversioned (otherwise it won't resolve on M where it is private).
|
|
|
|
"""
|
|
|
|
for tag in tags:
|
|
|
|
if tag.startswith('versioned='):
|
|
|
|
return api >= int(get_tag_value(tag))
|
|
|
|
# If there is no "versioned" tag, the tag has been versioned for as long as
|
|
|
|
# it was introduced.
|
|
|
|
return True
|
|
|
|
|
2016-06-18 01:45:24 +02:00
|
|
|
|
2016-07-29 22:06:22 +02:00
|
|
|
class ParseError(RuntimeError):
|
|
|
|
"""An error that occurred while parsing a symbol file."""
|
2016-06-18 01:45:24 +02:00
|
|
|
|
|
|
|
|
2018-10-10 01:36:03 +02:00
|
|
|
class MultiplyDefinedSymbolError(RuntimeError):
|
|
|
|
"""A symbol name was multiply defined."""
|
2020-06-23 20:21:21 +02:00
|
|
|
def __init__(self, multiply_defined_symbols: Iterable[str]) -> None:
|
|
|
|
super().__init__(
|
2018-10-10 01:36:03 +02:00
|
|
|
'Version script contains multiple definitions for: {}'.format(
|
|
|
|
', '.join(multiply_defined_symbols)))
|
|
|
|
self.multiply_defined_symbols = multiply_defined_symbols
|
|
|
|
|
|
|
|
|
2020-06-23 00:59:12 +02:00
|
|
|
class SymbolFileParser:
|
2016-07-29 22:06:22 +02:00
|
|
|
"""Parses NDK symbol files."""
|
2022-07-16 16:30:09 +02:00
|
|
|
def __init__(self, input_file: TextIO, api_map: ApiMap, filt: Filter) -> None:
|
2016-07-29 22:06:22 +02:00
|
|
|
self.input_file = input_file
|
2017-03-29 01:04:25 +02:00
|
|
|
self.api_map = api_map
|
2022-07-16 16:30:09 +02:00
|
|
|
self.filter = filt
|
2020-06-23 20:21:21 +02:00
|
|
|
self.current_line: Optional[str] = None
|
2016-07-29 22:06:22 +02:00
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def parse(self) -> List[Version]:
|
2016-07-29 22:06:22 +02:00
|
|
|
"""Parses the symbol file and returns a list of Version objects."""
|
|
|
|
versions = []
|
2021-08-19 21:31:54 +02:00
|
|
|
while self.next_line():
|
2020-06-23 20:21:21 +02:00
|
|
|
assert self.current_line is not None
|
2016-07-29 22:06:22 +02:00
|
|
|
if '{' in self.current_line:
|
|
|
|
versions.append(self.parse_version())
|
|
|
|
else:
|
|
|
|
raise ParseError(
|
2020-06-23 20:21:21 +02:00
|
|
|
f'Unexpected contents at top level: {self.current_line}')
|
2018-10-10 01:36:03 +02:00
|
|
|
|
|
|
|
self.check_no_duplicate_symbols(versions)
|
2016-07-29 22:06:22 +02:00
|
|
|
return versions
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def check_no_duplicate_symbols(self, versions: Iterable[Version]) -> None:
|
2018-10-10 01:36:03 +02:00
|
|
|
"""Raises errors for multiply defined symbols.
|
|
|
|
|
|
|
|
This situation is the normal case when symbol versioning is actually
|
|
|
|
used, but this script doesn't currently handle that. The error message
|
|
|
|
will be a not necessarily obvious "error: redefition of 'foo'" from
|
|
|
|
stub.c, so it's better for us to catch this situation and raise a
|
|
|
|
better error.
|
|
|
|
"""
|
|
|
|
symbol_names = set()
|
|
|
|
multiply_defined_symbols = set()
|
|
|
|
for version in versions:
|
2022-07-16 16:30:09 +02:00
|
|
|
if self.filter.should_omit_version(version):
|
2018-10-10 01:36:03 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
for symbol in version.symbols:
|
2022-07-16 16:30:09 +02:00
|
|
|
if self.filter.should_omit_symbol(symbol):
|
2018-10-10 01:36:03 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
if symbol.name in symbol_names:
|
|
|
|
multiply_defined_symbols.add(symbol.name)
|
|
|
|
symbol_names.add(symbol.name)
|
|
|
|
if multiply_defined_symbols:
|
|
|
|
raise MultiplyDefinedSymbolError(
|
|
|
|
sorted(list(multiply_defined_symbols)))
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def parse_version(self) -> Version:
|
2016-07-29 22:06:22 +02:00
|
|
|
"""Parses a single version section and returns a Version object."""
|
2020-06-23 20:21:21 +02:00
|
|
|
assert self.current_line is not None
|
2016-07-29 22:06:22 +02:00
|
|
|
name = self.current_line.split('{')[0].strip()
|
2021-06-04 23:30:40 +02:00
|
|
|
tags = get_tags(self.current_line, self.api_map)
|
2020-06-23 20:21:21 +02:00
|
|
|
symbols: List[Symbol] = []
|
2016-07-29 22:06:22 +02:00
|
|
|
global_scope = True
|
2017-11-21 17:47:33 +01:00
|
|
|
cpp_symbols = False
|
2021-08-19 21:31:54 +02:00
|
|
|
while self.next_line():
|
2016-07-29 22:06:22 +02:00
|
|
|
if '}' in self.current_line:
|
|
|
|
# Line is something like '} BASE; # tags'. Both base and tags
|
|
|
|
# are optional here.
|
|
|
|
base = self.current_line.partition('}')[2]
|
|
|
|
base = base.partition('#')[0].strip()
|
|
|
|
if not base.endswith(';'):
|
|
|
|
raise ParseError(
|
2017-11-21 17:47:33 +01:00
|
|
|
'Unterminated version/export "C++" block (expected ;).')
|
|
|
|
if cpp_symbols:
|
|
|
|
cpp_symbols = False
|
|
|
|
else:
|
|
|
|
base = base.rstrip(';').rstrip()
|
2020-06-23 20:21:21 +02:00
|
|
|
return Version(name, base or None, tags, symbols)
|
2017-11-21 17:47:33 +01:00
|
|
|
elif 'extern "C++" {' in self.current_line:
|
|
|
|
cpp_symbols = True
|
|
|
|
elif not cpp_symbols and ':' in self.current_line:
|
2016-07-29 22:06:22 +02:00
|
|
|
visibility = self.current_line.split(':')[0].strip()
|
|
|
|
if visibility == 'local':
|
|
|
|
global_scope = False
|
|
|
|
elif visibility == 'global':
|
|
|
|
global_scope = True
|
|
|
|
else:
|
|
|
|
raise ParseError('Unknown visiblity label: ' + visibility)
|
2017-11-21 17:47:33 +01:00
|
|
|
elif global_scope and not cpp_symbols:
|
2016-07-29 22:06:22 +02:00
|
|
|
symbols.append(self.parse_symbol())
|
|
|
|
else:
|
2018-09-25 22:39:25 +02:00
|
|
|
# We're in a hidden scope or in 'extern "C++"' block. Ignore
|
|
|
|
# everything.
|
2016-07-29 22:06:22 +02:00
|
|
|
pass
|
|
|
|
raise ParseError('Unexpected EOF in version block.')
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def parse_symbol(self) -> Symbol:
|
2016-07-29 22:06:22 +02:00
|
|
|
"""Parses a single symbol line and returns a Symbol object."""
|
2020-06-23 20:21:21 +02:00
|
|
|
assert self.current_line is not None
|
2016-07-29 22:06:22 +02:00
|
|
|
if ';' not in self.current_line:
|
|
|
|
raise ParseError(
|
|
|
|
'Expected ; to terminate symbol: ' + self.current_line)
|
|
|
|
if '*' in self.current_line:
|
|
|
|
raise ParseError(
|
|
|
|
'Wildcard global symbols are not permitted.')
|
|
|
|
# Line is now in the format "<symbol-name>; # tags"
|
|
|
|
name, _, _ = self.current_line.strip().partition(';')
|
2021-06-04 23:30:40 +02:00
|
|
|
tags = get_tags(self.current_line, self.api_map)
|
2016-07-29 22:06:22 +02:00
|
|
|
return Symbol(name, tags)
|
|
|
|
|
2020-06-23 20:21:21 +02:00
|
|
|
def next_line(self) -> str:
|
2016-07-29 22:06:22 +02:00
|
|
|
"""Returns the next non-empty non-comment line.
|
|
|
|
|
|
|
|
A return value of '' indicates EOF.
|
|
|
|
"""
|
|
|
|
line = self.input_file.readline()
|
2021-08-19 21:31:54 +02:00
|
|
|
while not line.strip() or line.strip().startswith('#'):
|
2016-07-29 22:06:22 +02:00
|
|
|
line = self.input_file.readline()
|
|
|
|
|
|
|
|
# We want to skip empty lines, but '' indicates EOF.
|
2021-08-19 21:31:54 +02:00
|
|
|
if not line:
|
2016-07-29 22:06:22 +02:00
|
|
|
break
|
|
|
|
self.current_line = line
|
|
|
|
return self.current_line
|