Skip to content

Conversation

Pennycook
Copy link
Contributor

Related issues

Proposed changes

  • Rename Platform to Preprocessor, since Platform is not consistent with how we use the name "platform" elsewhere.
  • Simplify Preprocessor API to improve readability.

This is a much better name for the class, because it represents the state of a
specific instance of a C preprocessor.

The rename also makes it easier to justify why things like include paths and
defines are an essential part of construction, allowing these to be moved into
the __init__() function.

Signed-off-by: John Pennycook <[email protected]>
define() previously took a name and a macro, despite a macro's name being
encoded in the macro itself.

undefine() and get_macro() expected a string, despite the rest of the
preprocessor code using an Identifier token. Using the Identifier directly
is less error-prone than having to remember to extract the string.

Signed-off-by: John Pennycook <[email protected]>
is_defined() confusingly returned a string instead of a bool,
and has_macro() is more aligned with the naming of get_macro().

This change also moves the handling of defined() into a single place,
making it clearer that defined() will return a "1" or "0" wrapped in a
NumericalConstant based on whether the macro exists.

Signed-off-by: John Pennycook <[email protected]>
Signed-off-by: John Pennycook <[email protected]>
The naming of these functions was really unclear:

- process_include didn't process includes, but rather returned a bool denoting
  whether an include file should be processed.

- add_include_to_skip exposed an implementation detail (specifically, storing a
  "skip list") that wasn't obviously tied to usage (#pragma once).

We can replace both of these functions with a new FileInfo concept that stores
information about files, including whether a file has previously been marked as
"include once".

Signed-off-by: John Pennycook <[email protected]>
@Pennycook Pennycook added this to the 2.x milestone Aug 7, 2025
@Pennycook Pennycook added the refactor Improvements to code structure label Aug 7, 2025
@Pennycook Pennycook requested review from Copilot and swright87 August 7, 2025 13:26
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the Platform class to Preprocessor, improving the naming consistency since "platform" is used differently elsewhere in the codebase. The refactoring also simplifies the API by using keyword-only arguments in the constructor and improving method signatures.

Key Changes:

  • Renamed Platform class to Preprocessor with updated imports across test files
  • Simplified constructor API with keyword-only parameters and type validation
  • Updated method signatures to be more explicit (e.g., evaluate_for_platformevaluate)

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
codebasin/preprocessor.py Main refactoring from Platform to Preprocessor class with improved API design
codebasin/finder.py Updated imports and method calls to use new Preprocessor class
tests/operators/test_operators.py Updated test to use new Preprocessor constructor with keyword arguments
tests/macro_expansion/test_macro_expansion.py Updated multiple test methods to use new Preprocessor constructor

Comment on lines 1681 to 1691
platform_name: str | None = None,
rootdir: str | os.PathLike[str] | None = None,
include_paths: list[str | os.PathLike[str]] | None = None,
defines: list[str] | None = None,
) -> None:
if platform_name is None:
self.platform_name = None
elif not isinstance(platform_name, str):
raise TypeError("'platform_name' must be a string.")
else:
self.platform_name = platform_name
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor allows platform_name to be None, but the logic in associate method assumes it has a value when calling association[node].add(preprocessor.platform_name). This could lead to runtime errors if None is passed.

Suggested change
platform_name: str | None = None,
rootdir: str | os.PathLike[str] | None = None,
include_paths: list[str | os.PathLike[str]] | None = None,
defines: list[str] | None = None,
) -> None:
if platform_name is None:
self.platform_name = None
elif not isinstance(platform_name, str):
raise TypeError("'platform_name' must be a string.")
else:
self.platform_name = platform_name
platform_name: str,
rootdir: str | os.PathLike[str] | None = None,
include_paths: list[str | os.PathLike[str]] | None = None,
defines: list[str] | None = None,
) -> None:
if not isinstance(platform_name, str):
raise TypeError("'platform_name' must be a string.")
self.platform_name = platform_name

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the comment but not the suggestion.

I've added a check for None inside associate, in 0214a3f.

The root directory was used by a previous implementation of find_include_file,
but is no longer required.

Signed-off-by: John Pennycook <[email protected]>
The new Preprocessor class allows platform_name to be None, to enable uses
that invoke the preprocessor without also building a setmap.

Attempting to build a setmap when the preprocessor does not have an associated
platform_name should result in an error.

Signed-off-by: John Pennycook <[email protected]>
The tests highlighted a bug in the implementation where an iterable (like a
string) was not detected as an invalid argument and did not throw a TypeError.

Signed-off-by: John Pennycook <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Improvements to code structure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant