Skip to content

Fix foreach macro range preprocessing#3816

Open
azwolski wants to merge 4 commits into
mainfrom
fix/issue-3809
Open

Fix foreach macro range preprocessing#3816
azwolski wants to merge 4 commits into
mainfrom
fix/issue-3809

Conversation

@azwolski
Copy link
Copy Markdown
Collaborator

Description

Fixes #3809.

Clang tokenizes cases like 0...MAXI as a single preprocessing-number token, so MAXI is not expanded as a macro. This breaks foreach ranges 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:

0...MAXI -> 0 ...MAXI

Related Issue

  • Linked to relevant issue(s)

Checklist

  • Code has been formatted with clang-format (e.g., clang-format -i src/ispc.cpp)
  • Git history has been squashed to meaningful commits (one commit per logical change)
  • Compiler changes are covered by lit tests
  • Language/stdlib changes include new functional tests for runtime behavior
  • Documentation updated if needed

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 a numeric_constant token 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.

Comment thread src/preprocessor.cpp Outdated
Comment thread src/preprocessor.cpp
Copy link
Copy Markdown
Collaborator

@zephyr111 zephyr111 left a comment

Choose a reason for hiding this comment

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

Looks good to me (besides the TODO note)!
I did not found any issues while performing some additional tests.

Comment thread src/preprocessor.cpp
// 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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you plan to add this in another PR or later in this one?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I would like to try

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Would like me to remove the TOODs?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please remove TODO and create an issues instead. Thanks for creating lit test for this.

Comment thread src/preprocessor.cpp
@dbabokin
Copy link
Copy Markdown
Collaborator

dbabokin commented May 1, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@azwolski azwolski marked this pull request as ready for review May 4, 2026 10:07
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread src/preprocessor.cpp
Comment thread tests/lit-tests/3809-include-limitation.ispc
Comment thread src/preprocessor.cpp
// 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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please remove TODO and create an issues instead. Thanks for creating lit test for this.

@elena901
Copy link
Copy Markdown

A little bit late with it, but i like this option.
Richard Jerome:
Fix the pre-processor in the clang code itself so for it not to recognize this as a single token.

@aneshlya
Copy link
Copy Markdown
Collaborator

A little bit late with it, but i like this #3809 (comment).

@elena901, this PR already implements the option 1 and it's not relevant place for such comment, it should go to the linked issue.

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.

Parsing error with defines in foreach ranges without spaces

7 participants