Fix foreach macro range preprocessing#3816
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a Clang preprocessor corner case that breaks foreach ranges like 0...MAXI when MAXI is a macro, by normalizing the main input file prior to preprocessing so that ... is tokenized separately and the macro expands correctly.
Changes:
- Added a raw-lexer based normalization pass that inserts a space before
...when it appears inside anumeric_constanttoken followed by an identifier-start character. - Updated preprocessor setup to feed Clang a normalized main file buffer (including stdin handling).
- Added lit coverage for the regression (and an explicit XFAIL documenting the current included-file limitation).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/preprocessor.cpp |
Adds source normalization using Clang’s raw lexer and applies it to the main input before preprocessing. |
tests/lit-tests/3809.ispc |
Regression test ensuring foreach(i = 0...MACRO) compiles and comments are not rewritten. |
tests/lit-tests/3809-include-limitation.isph |
Header used to demonstrate the current limitation for included files. |
tests/lit-tests/3809-include-limitation.ispc |
XFAIL lit test documenting that included files aren’t normalized yet. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
zephyr111
left a comment
There was a problem hiding this comment.
Looks good to me (besides the TODO note)!
I did not found any issues while performing some additional tests.
| // Clang's raw lexer is used to avoid changing comments, strings, or | ||
| // character literals. | ||
| // | ||
| // TODO: This currently only handles the main input file. Included files need separate support. |
There was a problem hiding this comment.
Do you plan to add this in another PR or later in this one?
There was a problem hiding this comment.
I would like to try
There was a problem hiding this comment.
Would like me to remove the TOODs?
There was a problem hiding this comment.
Please remove TODO and create an issues instead. Thanks for creating lit test for this.
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Agent-Logs-Url: https://github.com/ispc/ispc/sessions/ce923e6f-c77d-410e-810f-55d4d920a03e Co-authored-by: azwolski <175651206+azwolski@users.noreply.github.com>
| // Clang's raw lexer is used to avoid changing comments, strings, or | ||
| // character literals. | ||
| // | ||
| // TODO: This currently only handles the main input file. Included files need separate support. |
There was a problem hiding this comment.
Please remove TODO and create an issues instead. Thanks for creating lit test for this.
|
A little bit late with it, but i like this option. |
@elena901, this PR already implements the option 1 and it's not relevant place for such comment, it should go to the linked issue. |
Description
Fixes #3809.
Clang tokenizes cases like
0...MAXIas a single preprocessing-number token, soMAXIis not expanded as a macro. This breaksforeachranges when there is no space between the numeric start value, the...operator, and the macro upper bound.This patch normalizes the main input file before running Clang's preprocessor. It uses Clang's raw lexer to find numeric tokens containing
...followed by an identifier-start character, then inserts a space before the ellipsis:Related Issue
Checklist
clang-format(e.g.,clang-format -i src/ispc.cpp)