Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add get_includes() methods
* Make Codegen.ifndef_symbols private.
* Make Codegen.includes private.
* Make CConverter.includes private.
  • Loading branch information
vstinner committed Apr 9, 2024
commit e446461d607e71ce53eb83cf5258879ffb26c411
5 changes: 3 additions & 2 deletions Tools/clinic/libclinic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,10 @@ def parse(self, input: str) -> str:
pass

block.input = 'preserve\n'
includes = self.codegen.get_includes()

printer_2 = BlockPrinter(self.language)
printer_2.print_block(block,
header_includes=self.codegen.includes)
printer_2.print_block(block, header_includes=includes)
libclinic.write_file(destination.filename,
printer_2.f.getvalue())
continue
Expand Down
2 changes: 1 addition & 1 deletion Tools/clinic/libclinic/clanguage.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ def parser_body(
# Copy includes from parameters to Clinic after parse_arg() has been
# called above.
for converter in converters:
for include in converter.includes:
for include in converter.get_includes():
codegen.add_include(include.filename, include.reason,
condition=include.condition)

Expand Down
21 changes: 12 additions & 9 deletions Tools/clinic/libclinic/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def print_block(
self,
block: Block,
*,
header_includes: dict[str, Include] | None = None,
header_includes: list[Include] | None = None,
) -> None:
input = block.input
output = block.output
Expand Down Expand Up @@ -138,8 +138,7 @@ def print_block(
output += '\n'

current_condition: str | None = None
includes = sorted(header_includes.values(), key=Include.sort_key)
for include in includes:
for include in header_includes:
if include.condition != current_condition:
if current_condition:
output += '#endif\n'
Expand Down Expand Up @@ -270,20 +269,20 @@ def dump(self) -> str:
class Codegen:
def __init__(self, limited_capi: bool) -> None:
self.limited_capi = limited_capi
self.ifndef_symbols: set[str] = set()
self._ifndef_symbols: set[str] = set()
# dict: include name => Include instance
self.includes: dict[str, Include] = {}
self._includes: dict[str, Include] = {}

def add_ifndef_symbol(self, name: str) -> bool:
if name in self.ifndef_symbols:
if name in self._ifndef_symbols:
return False
self.ifndef_symbols.add(name)
self._ifndef_symbols.add(name)
return True
Comment thread
erlend-aasland marked this conversation as resolved.

def add_include(self, name: str, reason: str,
*, condition: str | None = None) -> None:
try:
existing = self.includes[name]
existing = self._includes[name]
except KeyError:
pass
else:
Expand All @@ -296,4 +295,8 @@ def add_include(self, name: str, reason: str,
# no need to list all of them.
return

self.includes[name] = Include(name, reason, condition)
self._includes[name] = Include(name, reason, condition)

def get_includes(self) -> list[Include]:
return sorted(self._includes.values(),
key=Include.sort_key)
7 changes: 5 additions & 2 deletions Tools/clinic/libclinic/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def __init__(self,
self.name = libclinic.ensure_legal_c_identifier(name)
self.py_name = py_name
self.unused = unused
self.includes: list[Include] = []
self._includes: list[Include] = []

if default is not unspecified:
if (self.default_type
Expand Down Expand Up @@ -513,7 +513,10 @@ def parser_name(self) -> str:
def add_include(self, name: str, reason: str,
*, condition: str | None = None) -> None:
include = Include(name, reason, condition)
self.includes.append(include)
self._includes.append(include)

def get_includes(self) -> list[Include]:
return self._includes


ConverterType = Callable[..., CConverter]
Expand Down