-
Notifications
You must be signed in to change notification settings - Fork 47
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
Fix preprocessor expansion strategy #33
Conversation
@Vexu this is still not ready for a full review, but I have to suspend working on this for a week or so. Could you give it a quick skim regarding structure and memory handling? It's practically the first real piece of zig code I write, and I felt it was rough a lot of times. The entry point is expandMacro2(), if one substitutes it to expandMacro() in the two occurrences in the source, the new code will be used. I have added new test cases and everything passes now, but I have some regressions which aren't being tested and a lot of things I want to fix first. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far this seems mostly fine, I tried answering most of the questions in the TODOs.
I'd be nice if more allocations could be avoided but correctness comes first.
Small status update, don't review yet because the commits are a mess right now, It seems that the preprocessor is working ok. I've tested it with libpp and it passes all its tests. I also tested it with cloak, but that didn't work completely, I suspect it relies on the undefined behaviour that arocc does differently respect to gcc/clang. I will test it with more libraries from awesome-c-preprocessor A note about commit cf776f3 . That commit deals with the following: #define h(s) 30
#define H h
#define SS (s)
H SS // should this be h(s) or 30? gcc/clang expand as h(s), while arocc before cf776f3 expands to 30, after it does like gcc. What gcc does is reasonable, but I'm not sure it is strictly mandated by the standard |
@Vexu I'm stopping here with the fixes and enhancements, this is getting a bit out of hand already. After the rebasing, I will add documentation to all the parts I touched, and I will do following development in future PR:
#define str(x) #x
str(f(x)+1)
str(f(x) + 1) This should expand to "str(f(x)+1)" and "str(f(x) + 1)" respectively. The previous implementation always did the second, the current always does the former. |
130cd37
to
c0f0d4b
Compare
c0f0d4b
to
2a9e1b0
Compare
I can review it now, should make squashing any fixups easier too.
Yes that seems to be the case, it should also make properly implementing the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks good and removes some of the ugliest parts of the entire codebase.
I didn't make notes about all the debug comments and prints since you'll probably remove them as part of the rebasing.
31da779
to
8083895
Compare
The previous macro expansion implementation had several flaws regarding order and re-expansion logic. Those were fixed by a complete rewrite of the expandMacro() and its nested code paths. Also remove special handling for 'self' macros. In the future, the 'empty' macro will be removed as well.
fab1e21
to
dcb8477
Compare
src/Preprocessor.zig
Outdated
try buf.replaceRange(idx, macro_scan_idx-idx+1, res.items); | ||
// TODO: moving_end_idx += res.items.len - (macro_scan_idx-idx+1) | ||
// doesn't work when the RHS is negative (unsigned!) | ||
moving_end_idx = moving_end_idx + res.items.len - (macro_scan_idx-idx+1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggestion about this? It was moving_end_idx -= res.items.len - (macro_scan_idx-idx+1);
But the rhs can become negative, which is harmless but zig doesn't like it. It works right now, but I don't know if this is the zig way
@@ -70,11 +70,7 @@ defines: DefineMap, | |||
tokens: Token.List = .{}, | |||
generated: std.ArrayList(u8), | |||
token_buf: RawTokenList, | |||
char_buf: std.ArrayList(u8), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was only used during stringification, so I moved it there locally, is that okay?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason it was in the PreProcessor
was so that the buffer could be reused between stringifications redusing redundant allocations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reintroduced the global pp.char_buf
…s at beginning These modifications strive for compatibility with gcc, but I'm not sure it is strictly mandated by the standard
405b883
to
9e045e2
Compare
Rebased and applied zig fmt. I originally wanted to also add some docs to this PR, but maybe I'll leave that for another one if that's okay for you? |
Mainly changes to the control flow of the macro expansion/rescan loop Co-authored-by: Veikka Tuominen <git@vexu.eu>
9e045e2
to
e3e4ac4
Compare
Fixes #31
Fixes #30
Supersedes #29
Still WIP, I need to reintegrate the logic needed to handle
__VA_ARGS__
and some other things, but overall it works.I will tidy up the commits when done.