Skip to content
Snippets Groups Projects
Commit 73ead40a authored by Frederik Hennig's avatar Frederik Hennig
Browse files

fix remaining bugs and tests

 - fix source file listing on cli
 - properly sort function and method args again
parent 35764348
No related branches found
No related tags found
1 merge request!17Improved Source File and Code Structure Modelling
Pipeline #73677 failed
......@@ -65,7 +65,7 @@ class CodeStyle(ConfigBase):
includes_sorting_key: BasicOption[Callable[[HeaderFile], Any]] = BasicOption()
"""Key function that will be used to sort `#include` statements in generated files.
Pystencils-sfg will instruct clang-tidy to forego include sorting if this option is set.
"""
......@@ -196,7 +196,8 @@ class SfgConfig(ConfigBase):
case OutputMode.STANDALONE:
impl_ext = "cpp"
if impl_ext is not None:
if output_mode != OutputMode.HEADER_ONLY:
assert impl_ext is not None
output_files.append(output_dir / f"{basename}.{impl_ext}")
return tuple(output_files)
......
......@@ -14,7 +14,7 @@ def invoke_clang_format(
Args:
code: Code string to format
options: Options controlling the clang-format invocation
sort_includes: Option to be passed on to clang-format's ``--sort-includes`` argument
sort_includes: Option to be passed on to clang-format's ``--sort-includes`` argument
Returns:
The formatted code, if `clang-format` was run sucessfully.
......@@ -33,7 +33,7 @@ def invoke_clang_format(
force = options.get_option("force")
style = options.get_option("code_style")
args = [binary, f"--style={style}"]
if sort_includes is not None:
args += ["--sort-includes", sort_includes]
......
......@@ -107,12 +107,13 @@ class SourceFileGenerator:
self._header_file.elements.append("#define RESTRICT __restrict__")
outer_namespace: str | _GlobalNamespace = config.get_option("outer_namespace")
match (outer_namespace, namespace):
case [_GlobalNamespace(), None]:
namespace = None
case [_GlobalNamespace(), nspace] if nspace is not None:
namespace = nspace
case [nspace, None]:
case [nspace, None] if not isinstance(nspace, _GlobalNamespace):
namespace = nspace
case [outer, inner]:
namespace = f"{outer}::{inner}"
......
......@@ -35,7 +35,9 @@ def collect_includes(file: SfgSourceFile) -> set[HeaderFile]:
| SfgMethod(_, _, parameters)
| SfgConstructor(_, parameters, _, _)
):
incls = reduce(set.union, (includes(p) for p in parameters), set())
incls: set[HeaderFile] = reduce(
lambda accu, p: accu | includes(p), parameters, set()
)
if isinstance(entity, (SfgFunction, SfgMethod)):
incls |= includes(entity.return_type)
return incls
......@@ -90,7 +92,7 @@ def collect_includes(file: SfgSourceFile) -> set[HeaderFile]:
case SfgNamespaceBlock(_, elements) | SfgVisibilityBlock(_, elements):
return reduce(
lambda accu, elem: accu | walk_syntax(elem), elements, set()
)
) # type: ignore
case SfgClassBody(_, vblocks):
return reduce(
......
......@@ -235,15 +235,17 @@ class SfgFunction(SfgCodeEntity):
self._return_type = return_type
self._inline = inline
self._parameters: set[SfgVar]
self._parameters: tuple[SfgVar, ...]
from .postprocessing import CallTreePostProcessing
param_collector = CallTreePostProcessing()
self._parameters = param_collector(self._tree).function_params
self._parameters = tuple(
sorted(param_collector(self._tree).function_params, key=lambda p: p.name)
)
@property
def parameters(self) -> set[SfgVar]:
def parameters(self) -> tuple[SfgVar, ...]:
return self._parameters
@property
......@@ -356,19 +358,21 @@ class SfgMethod(SfgClassMember):
self._inline = inline
self._const = const
self._parameters: set[SfgVar]
self._parameters: tuple[SfgVar, ...]
from .postprocessing import CallTreePostProcessing
param_collector = CallTreePostProcessing()
self._parameters = param_collector(self._tree).function_params
self._parameters = tuple(
sorted(param_collector(self._tree).function_params, key=lambda p: p.name)
)
@property
def name(self) -> str:
return self._name
@property
def parameters(self) -> set[SfgVar]:
def parameters(self) -> tuple[SfgVar, ...]:
return self._parameters
@property
......
......@@ -25,7 +25,7 @@ BasicDefinitions:
expect-code:
hpp:
- regex: >-
#include\s\"config\.h\"\s*
#include\s\"config\.h\"(\s|.)*
namespace\s+awesome\s+{\s+.+\s+
#define\sPI\s3\.1415\s+
using\snamespace\sstd\;\s+
......
from pystencilssfg import SourceFileGenerator
with SourceFileGenerator() as sfg:
sfg.namespace("gen")
with SourceFileGenerator(namespace="gen") as sfg:
retval = 42 if sfg.context.project_info is None else sfg.context.project_info
sfg.function("getValue", return_type="int")(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment