Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pnut.c
Original file line number Diff line number Diff line change
Expand Up @@ -1660,8 +1660,11 @@ void begin_macro_expansion(int ident, int tokens, int args) {
// Search the macro stack to see if the macro is already expanding.
bool macro_is_already_expanding(int ident) {
int i = macro_stack_ix;
if (ident == 0 || macro_ident == 0) return false; // Unnamed macro or no macro is expanding
if (ident == macro_ident) return true; // The same macro is already expanding
// Unnamed macro (i.e. replaying tokens or playing a macro argument)
if (ident == 0) return false;

// The macro is currently expanding
if (ident == macro_ident) return true;

// Traverse the stack to see if the macro is already expanding
while (i > 0) {
Expand Down
16 changes: 15 additions & 1 deletion tests/_all/preprocessor/macro/self-reference.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tests for recursion depth of macros

// putchar
#include <stdio.h>
#include <stdlib.h>

int test1 = 1;
int test2 = 2;
Expand Down Expand Up @@ -33,7 +33,21 @@ void B(int a, int b) {
#define B(a1, a2) C(a1, a2)
#define C(a1, a2) B(a1, a2)

struct STATE {
int regA;
int regB;
};

struct STATE *global_state;

#define struct_field(sym) global_state->sym
#define regA struct_field(regA)

void main() {
global_state = malloc(sizeof (struct STATE));
regA = 'A';
putchar(regA); putchar('\n');

putdigit(test1);
putdigit(test2);
putdigit(test3);
Expand Down
1 change: 1 addition & 0 deletions tests/_all/preprocessor/macro/self-reference.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
A
1
2
3
Expand Down
Loading