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

Array symbolic dimensions #186

Merged
merged 3 commits into from
Oct 21, 2023
Merged

Array symbolic dimensions #186

merged 3 commits into from
Oct 21, 2023

Conversation

jnikula
Copy link
Owner

@jnikula jnikula commented Oct 6, 2023

Things like

#define SIZE 5
int foo[SIZE];

get documented with the actual value 5 rather than the symbolic SIZE. Fix it.

(This includes #184 to ensure it works at all.)

Copy link
Collaborator

@BrunoMSantos BrunoMSantos left a comment

Choose a reason for hiding this comment

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

There's one comment, but I don't think the behaviour is wrong if you don't address it. Nice one!

# dim should not be None here
yield ' '.join(dim)
dim = None
elif dim is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is really just an else: right? I think it's better to do it that way. If due to some bug it tries to append something to None it will fail loudly instead of continuing silently.

Moreover, at 1st glance this made it look like the dims has a secondary function here as a way of signalling where we are in the token processing when in fact it isn't: we're either at the beginning [, the end ], or it's something to append. I think? I'm tired, so... 😅

Copy link
Owner Author

Choose a reason for hiding this comment

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

dims actually does have that secondary function! The tokens also contain stuff before the first [, and we want to ignore them. A quick debug print reveals it:

diff --git a/src/hawkmoth/parser.py b/src/hawkmoth/parser.py
index 49bcfe96db2a..a9b08cb8ad4c 100644
--- a/src/hawkmoth/parser.py
+++ b/src/hawkmoth/parser.py
@@ -427,6 +427,8 @@ def _normalize_type(type_string):
     return 'bool' if type_string == '_Bool' else type_string
 
 def _symbolic_dims(cursor):
+    print([t.spelling for t in _cursor_get_tokens(cursor)])
+
     dim = None
     for spelling in [t.spelling for t in _cursor_get_tokens(cursor)]:
         if spelling == '[':

Running hawkmoth test/c/array-symbolic-dimensions.c with that on top produces e.g.:

['static', 'int', 'foo', '[', 'MACRO_SIZE', ']']
['static', 'int', 'bar', '[', 'MACRO_SIZE', '*', 'ENUM_SIZE', ']', '[', '3', ']']

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, right! I guess I should have tested it.

Not sure I'm a fan, it may be a bit too clever. But tests are failing on my side (probably some update, I hadn't picked this up in a while), so I won't be volunteering any alternative right now.

It's quite readable anyway, it's 👍 from me.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm always eager to merge after seeing the thumbs up... but just double checking, by "a bit too clever" you mean the details of the implementation (which can be improved later), not the idea?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey, sorry I thought I had replied to this. Yeah, I meant the implementation with regards to the use of the dims variable and such. But again, it's totally readable so I think it's fine.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Merged now anyway, thanks!

In preparation for upcoming changes, store array dimensions in a list
while parsing types.
In most cases, magic values for array dimensions in documentation are
meaningless, and the symbolic name in code is desirable. Try to parse
the symbolic names from tokens.
@jnikula
Copy link
Owner Author

jnikula commented Oct 6, 2023

The force push was just a rebase because there were conflicts.

@jnikula jnikula merged commit 64a67ee into master Oct 21, 2023
5 checks passed
@jnikula jnikula deleted the array-symbolic-dimensions branch October 21, 2023 17:52
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