Skip to content

[BUG] Passthrough fails for #if with unbalanced brace on both branches #146

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

Closed
simonbuchan opened this issue Nov 28, 2022 · 2 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@simonbuchan
Copy link

Describe the bug
When the pre-processor is being used to switch between code that e.g. starts a block with different code, the braces are being counted in both branches, causing it to expect two closing braces.

To Reproduce

#if WINDOWS
int WinMain(HINSTANCE hinstance, HINSTANCE, LPSTR cmdline, int showCmd) {
#else
void main(int argc, char** argv) {
#endif
  // do something cool
}

gives

scratch.cpp2(8,0): error: end of file reached with 1 missing } to match earlier { on line 2

Additional context
Hit this porting some real code (that I didn't write), so this is a potential migration stumbling block.

I don't think it's plausible to catch everything a pre-processor can do, of course, but perhaps some form of pass-through for old syntax so long as the braces in both branches are equal would be fine.

@simonbuchan simonbuchan added the bug Something isn't working label Nov 28, 2022
@hsutter hsutter self-assigned this Dec 19, 2022
@hsutter
Copy link
Owner

hsutter commented Dec 20, 2022

Thanks! That's good practical feedback and a pragmatic solution proposal. I do care about real-world code and want to reduce adoption friction, even if it can't be bulletproof without implementing the preprocessor and doing macro expansion (which I don't want to do that this point).

perhaps some form of pass-through for old syntax so long as the braces in both branches are equal

OK, that seems like a reasonable heuristic... done, see above commit. It should now handle a net-positive number of braces introduced in both branches. I didn't try to deal with branches that introduce the same net-negative number of braces; that would require a bit more work.

Thanks again!

@simonbuchan
Copy link
Author

No problem! It's pretty much why I was throwing some real code at it in the first place, afterall.

stefanofiorentino pushed a commit to stefanofiorentino/cppfront that referenced this issue Dec 20, 2022
… brace count, closes hsutter#146

And do better encapsulation of the brace tracking in a `braces_tracker` class
Azmah-Bad pushed a commit to Azmah-Bad/cppfront that referenced this issue Feb 24, 2023
… brace count, closes hsutter#146

And do better encapsulation of the brace tracking in a `braces_tracker` class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants