Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a tool for stubs in cstruct v4 #72

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open

Create a tool for stubs in cstruct v4 #72

wants to merge 47 commits into from

Conversation

Miauwkeru
Copy link
Contributor

@Miauwkeru Miauwkeru commented Feb 27, 2024

The only thing that does not work as intended yet is the dereference of pointer. As in, it doesn't return the desired type information but just T

@Miauwkeru Miauwkeru changed the base branch from main to refactor-v4 February 27, 2024 10:13
Copy link

codecov bot commented Feb 27, 2024

Codecov Report

Attention: Patch coverage is 90.08403% with 59 lines in your changes missing coverage. Please review.

Project coverage is 91.50%. Comparing base (d2f2ee7) to head (a6fb222).

Files with missing lines Patch % Lines
dissect/cstruct/tools/stubgen.py 77.69% 29 Missing ⚠️
dissect/cstruct/types/base.py 81.81% 12 Missing ⚠️
dissect/cstruct/bitbuffer.py 72.72% 3 Missing ⚠️
dissect/cstruct/types/structure.py 97.32% 3 Missing ⚠️
dissect/cstruct/utils.py 70.00% 3 Missing ⚠️
dissect/cstruct/compiler.py 87.50% 1 Missing ⚠️
dissect/cstruct/cstruct.py 99.17% 1 Missing ⚠️
dissect/cstruct/types/char.py 93.75% 1 Missing ⚠️
dissect/cstruct/types/enum.py 97.05% 1 Missing ⚠️
dissect/cstruct/types/flag.py 95.65% 1 Missing ⚠️
... and 4 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #72      +/-   ##
==========================================
- Coverage   92.33%   91.50%   -0.84%     
==========================================
  Files          20       21       +1     
  Lines        2180     2508     +328     
==========================================
+ Hits         2013     2295     +282     
- Misses        167      213      +46     
Flag Coverage Δ
unittests 91.50% <90.08%> (-0.84%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Schamper Schamper self-requested a review March 4, 2024 00:38
@Miauwkeru Miauwkeru marked this pull request as ready for review March 11, 2024 12:50
@Miauwkeru Miauwkeru marked this pull request as draft March 11, 2024 12:50
Base automatically changed from refactor-v4 to main May 17, 2024 13:15
@Miauwkeru Miauwkeru marked this pull request as ready for review August 9, 2024 14:56
def _type_stub(cls, name: str = "") -> str:
return f"{name}: {cls.__name__}"

def to_stub(cls, name: str) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe name this differently? Also I think this might as well be a private method. Maybe _to_type_stub? to_stub might be confusing because it also makes me think of the compiler.

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 renamed it to to_type_stub as it is a function that gets called externally.

Copy link
Member

Choose a reason for hiding this comment

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

I don't agree on the "externally" part, I'd classify this usage as internal and would never expect anyone to ever call this externally. This is not part of any expected usage API.

@Miauwkeru Miauwkeru requested a review from Schamper August 12, 2024 10:26
@Miauwkeru
Copy link
Contributor Author

I added some more tests, and noticed there was still some issues with import stuff from a file. As all signatures/names dissapear once the stub file is there. I attempted to fix this by adding more checks in stubify_file. This seems good enough for now, but I think it might be better to use ast in the future. And maybe look if we can use mypy to generate the stubs with hooks.

@Miauwkeru Miauwkeru requested a review from Schamper February 17, 2025 11:58
@Schamper
Copy link
Member

Schamper commented Mar 4, 2025

I have made a lot of suggested changes with 7c93f22

@Schamper Schamper requested a review from Copilot March 6, 2025 08:25

Choose a reason for hiding this comment

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

PR Overview

This PR introduces a new stub generation tool for cstruct v4 and updates many core type modules. Key changes include a transition from using public ‘name’ properties to the internal ‘_name’ attribute for fields, significant type‐annotation improvements (including generics and the addition of BaseArray), and refactoring in parsing and compiler functionalities for better consistency and type safety.

Reviewed Changes

File Description
dissect/cstruct/types/structure.py Updated field name references to use internal “_name” attribute.
dissect/cstruct/types/base.py Enhanced type annotations and introduced BaseArray.
dissect/cstruct/types/pointer.py Added generics and refined pointer arithmetic operations.
dissect/cstruct/parser.py Simplified struct registration logic by adding a “register_if_name” flag.
pyproject.toml Added a new command line script “cstruct-stubgen” and updated ruff settings.

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

Comments suppressed due to low confidence (2)

dissect/cstruct/types/structure.py:97

  • Ensure that all references to a field's name consistently use the internal '_name' attribute. Verify that no residual use of 'field.name' remains, as mismatches could lead to runtime errors.
if field._name in lookup and field._name != "_":

dissect/cstruct/parser.py:225

  • [nitpick] The combined use of 'register' and 'register_if_name' in the _struct method makes the registration logic complex and potentially error-prone. Consider refactoring this logic to separate struct creation from type registration more clearly for improved maintainability.
if register or register_if_name:
Miauwkeru and others added 27 commits March 6, 2025 09:45
A small fix was to just return 0 if the cls is a BaseType inside `MetaType`
In the case multiple cstruct definitions are in one file, it will not overwrite one another.
Otherwise the generated class stub wouldn't be a valid class definition
This is for those structures that define a type and a variable with a name.

E.G.
```c
typedef struct _NAME {
  ...
} NAME;
```

now generates:
```python
class _NAME(...): ...
NAME: _NAME
```
Co-authored-by: Erik Schamper <1254028+Schamper@users.noreply.github.com>
Comment on lines +89 to +90
elif isinstance(typedef, str):
stub = f"{name}: TypeAlias = {typedef}"
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 don't think this code will ever get reached. As the TypeAlias where there for the uint8 and those kinda types. These are now defined in cstruct proper no?


if field.name:
result[field.name] = value
if field._name:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

isn't field._name always defined as it either uses name or type.__name__?

Comment on lines +325 to +327
if field._name:
sizes[field._name] = stream.tell() - offset
result[field._name] = value
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants