Fix preprocessor infinite loop with self-referencing object-like macros - #109
Merged
Conversation
Apply recursion guard to non-parameterized macro expansion path: - Pre-process body with macro name removed from defines (prevents self-recursion) - Advance index past inserted body tokens (prevents re-expansion in same iteration) - Don't set anotherIterationNeeded since inner loop handles chain expansion This matches the recursion guard pattern already used for parameterized macros. Per C standard §6.10.3.4, a macro's own name found during expansion is not re-expanded. Agent-Logs-Url: https://github.com/praeclarum/CLanguage/sessions/81cfde6c-7cda-4f58-88b2-4e64e91a3ac2 Co-authored-by: praeclarum <323548+praeclarum@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix preprocessor infinite loop with self-referencing macros
Fix preprocessor infinite loop with self-referencing object-like macros
Mar 31, 2026
praeclarum
marked this pull request as ready for review
March 31, 2026 20:24
praeclarum
approved these changes
Mar 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#define FOO FOOcauses the preprocessor to loop forever. The non-parameterized macro expansion path lacked the recursion guard that the parameterized path already had.Changes
CLanguage/Parser/Preprocessor.cs: Apply the same expansion pattern used for parameterized macros to object-like macros: pre-process the body with the macro's own name removed from defines, then advanceipast the inserted tokens.anotherIterationNeededis not set since the inner loop already handles chain expansion.CLanguageTests/PreprocessorTests.cs: Add tests for self-referencing defines (#define FOO FOO), mutual recursion (#define A B/#define B A), chain expansion (#define X Y/#define Y 42), and self-reference in expressions.Before/after