Refactor symbolfile script
Introduce the class `Filter` which encapsulates conditions
(architecture, api level, whether llndk is included, etc.) so that we
don't need to touch many places everytime a new condition is added.
In addition, refactor test_symbolfile to reduce duplications
This CL doesn't add a new functionality.
Bug: 239274367
Test: run test_ndkstubgen and test_symbolfile
Change-Id: I188e482492d39ec18134ecc5c908f0d288a754a9
diff --git a/cc/symbolfile/__init__.py b/cc/symbolfile/__init__.py
index f8d1841..c5147d1 100644
--- a/cc/symbolfile/__init__.py
+++ b/cc/symbolfile/__init__.py
@@ -198,50 +198,55 @@
"""
return split_tag(tag)[1]
-
-def _should_omit_tags(tags: Tags, arch: Arch, api: int, llndk: bool,
- apex: bool) -> bool:
- """Returns True if the tagged object should be omitted.
-
- This defines the rules shared between version tagging and symbol tagging.
+class Filter:
+ """A filter encapsulates a condition that tells whether a version or a
+ symbol should be omitted or not
"""
- # 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:
- if not apex and not llndk:
+
+ def __init__(self, arch: Arch, api: int, llndk: bool = False, apex: bool = False):
+ self.arch = arch
+ self.api = api
+ self.llndk = llndk
+ self.apex = apex
+
+ 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:
+ if not self.apex and not self.llndk:
+ return True
+ if self.apex and not tags.has_apex_tags:
+ return True
+ if self.llndk and not tags.has_llndk_tags:
+ return True
+ if not symbol_in_arch(tags, self.arch):
return True
- if apex and not tags.has_apex_tags:
+ if not symbol_in_api(tags, self.arch, self.api):
return True
- if llndk and not tags.has_llndk_tags:
+ return False
+
+ def should_omit_version(self, version: Version) -> bool:
+ """Returns True if the version section should be omitted.
+
+ 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 not symbol_in_arch(tags, arch):
- return True
- if not symbol_in_api(tags, arch, api):
- return True
- return False
+ if version.tags.has_platform_only_tags:
+ return True
+ return self._should_omit_tags(version.tags)
-
-def should_omit_version(version: Version, arch: Arch, api: int, llndk: bool,
- apex: bool) -> bool:
- """Returns True if the version section should be omitted.
-
- 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 _should_omit_tags(version.tags, arch, api, llndk, apex)
-
-
-def should_omit_symbol(symbol: Symbol, arch: Arch, api: int, llndk: bool,
- apex: bool) -> bool:
- """Returns True if the symbol should be omitted."""
- return _should_omit_tags(symbol.tags, arch, api, llndk, apex)
+ def should_omit_symbol(self, symbol: Symbol) -> bool:
+ """Returns True if the symbol should be omitted."""
+ return self._should_omit_tags(symbol.tags)
def symbol_in_arch(tags: Tags, arch: Arch) -> bool:
@@ -316,14 +321,10 @@
class SymbolFileParser:
"""Parses NDK symbol files."""
- def __init__(self, input_file: TextIO, api_map: ApiMap, arch: Arch,
- api: int, llndk: bool, apex: bool) -> None:
+ def __init__(self, input_file: TextIO, api_map: ApiMap, filt: Filter) -> None:
self.input_file = input_file
self.api_map = api_map
- self.arch = arch
- self.api = api
- self.llndk = llndk
- self.apex = apex
+ self.filter = filt
self.current_line: Optional[str] = None
def parse(self) -> List[Version]:
@@ -352,13 +353,11 @@
symbol_names = set()
multiply_defined_symbols = set()
for version in versions:
- if should_omit_version(version, self.arch, self.api, self.llndk,
- self.apex):
+ if self.filter.should_omit_version(version):
continue
for symbol in version.symbols:
- if should_omit_symbol(symbol, self.arch, self.api, self.llndk,
- self.apex):
+ if self.filter.should_omit_symbol(symbol):
continue
if symbol.name in symbol_names: