From 069d274d504b54d9805e729716691fb33789fd46 Mon Sep 17 00:00:00 2001 From: Frederik Hennig <frederik.hennig@fau.de> Date: Wed, 12 Mar 2025 10:57:02 +0100 Subject: [PATCH] permit modification of code style and clang-format options through the composer/context --- docs/source/_util/sfg_monkeypatch.py | 5 +++-- src/pystencilssfg/context.py | 10 +++++++++- src/pystencilssfg/generator.py | 21 ++++++++++++--------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/docs/source/_util/sfg_monkeypatch.py b/docs/source/_util/sfg_monkeypatch.py index 1277603..91a9623 100644 --- a/docs/source/_util/sfg_monkeypatch.py +++ b/docs/source/_util/sfg_monkeypatch.py @@ -30,8 +30,9 @@ class DocsPatchedGenerator(pystencilssfg.SourceFileGenerator): def __exit__(self, exc_type, exc_value, traceback): if exc_type is None: self._finish_files() + emitter = self._get_emitter() - header_code = self._emitter.dumps(self._header_file) + header_code = emitter.dumps(self._header_file) header_ext = splitext(self._header_file.name)[1] mdcode = ":::::{tab-set}\n" @@ -42,7 +43,7 @@ class DocsPatchedGenerator(pystencilssfg.SourceFileGenerator): mdcode += "\n:::\n::::\n" if self._impl_file is not None: - impl_code = self._emitter.dumps(self._impl_file) + impl_code = emitter.dumps(self._impl_file) impl_ext = splitext(self._impl_file.name)[1] mdcode += f"::::{{tab-item}} Generated Implementation ({impl_ext})\n" diff --git a/src/pystencilssfg/context.py b/src/pystencilssfg/context.py index 3ea82f2..5773455 100644 --- a/src/pystencilssfg/context.py +++ b/src/pystencilssfg/context.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import Sequence, Any, Generator from contextlib import contextmanager -from .config import CodeStyle +from .config import CodeStyle, ClangFormatOptions from .ir import ( SfgSourceFile, SfgNamespace, @@ -23,6 +23,7 @@ class SfgContext: impl_file: SfgSourceFile | None, namespace: str | None = None, codestyle: CodeStyle | None = None, + clang_format_opts: ClangFormatOptions | None = None, argv: Sequence[str] | None = None, project_info: Any = None, ): @@ -33,6 +34,9 @@ class SfgContext: self._inner_namespace: str | None = None self._codestyle = codestyle if codestyle is not None else CodeStyle() + self._clang_format: ClangFormatOptions = ( + clang_format_opts if clang_format_opts is not None else ClangFormatOptions() + ) self._header_file = header_file self._impl_file = impl_file @@ -73,6 +77,10 @@ class SfgContext: """The code style object for this generation context.""" return self._codestyle + @property + def clang_format(self) -> ClangFormatOptions: + return self._clang_format + @property def header_file(self) -> SfgSourceFile: return self._header_file diff --git a/src/pystencilssfg/generator.py b/src/pystencilssfg/generator.py index c314d67..fe4eb99 100644 --- a/src/pystencilssfg/generator.py +++ b/src/pystencilssfg/generator.py @@ -95,9 +95,7 @@ class SourceFileGenerator: self._impl_file = SfgSourceFile( output_files[1].name, SfgSourceFileType.TRANSLATION_UNIT ) - self._impl_file.includes.append( - HeaderFile.parse(self._header_file.name) - ) + self._impl_file.includes.append(HeaderFile.parse(self._header_file.name)) # TODO: Find a way to not hard-code the restrict qualifier in pystencils self._header_file.elements.append("#define RESTRICT __restrict__") @@ -115,14 +113,11 @@ class SourceFileGenerator: self._impl_file, namespace, config.codestyle, + config.clang_format, argv=script_args, project_info=cli_params.get_project_info(), ) - self._emitter = SfgCodeEmitter( - self._output_dir, config.codestyle, config.clang_format - ) - sort_key = config.codestyle.get_option("includes_sorting_key") if sort_key is None: @@ -161,6 +156,13 @@ class SourceFileGenerator: ) self._impl_file.includes.sort(key=self._include_sort_key) + def _get_emitter(self): + return SfgCodeEmitter( + self._output_dir, + self._context.codestyle, + self._context.clang_format, + ) + def __enter__(self) -> SfgComposer: self.clean_files() return SfgComposer(self._context) @@ -169,6 +171,7 @@ class SourceFileGenerator: if exc_type is None: self._finish_files() - self._emitter.emit(self._header_file) + emitter = self._get_emitter() + emitter.emit(self._header_file) if self._impl_file is not None: - self._emitter.emit(self._impl_file) + emitter.emit(self._impl_file) -- GitLab