Add flag to suppress printing of put-back tokens with #pragma expand.

This is currently used in a couple places in the designated initializer code (solving the problem with #pragma expand in the last commit). It could probably be used elsewhere too, but for now it is not.
This commit is contained in:
Stephen Heumann 2022-11-28 21:22:56 -06:00
parent 39250629bd
commit dc305a86b2
2 changed files with 43 additions and 35 deletions

View File

@ -988,7 +988,7 @@ case token.kind of
lToken := token; lToken := token;
NextToken; NextToken;
tToken := token; tToken := token;
PutBackToken(token, true); PutBackToken(token, true, false);
token := lToken; token := lToken;
suppressMacroExpansions := lSuppressMacroExpansions; suppressMacroExpansions := lSuppressMacroExpansions;
if tToken.kind = colonch then begin if tToken.kind = colonch then begin
@ -1110,12 +1110,12 @@ Gen1(dc_lab, stPtr^.continueLab); {define the continue label}
tl := stPtr^.e3List; {place the expression back in the list} tl := stPtr^.e3List; {place the expression back in the list}
if tl <> nil then begin if tl <> nil then begin
PutBackToken(token, false); PutBackToken(token, false, false);
ltoken.kind := semicolonch; ltoken.kind := semicolonch;
ltoken.class := reservedSymbol; ltoken.class := reservedSymbol;
PutBackToken(ltoken, false); PutBackToken(ltoken, false, false);
while tl <> nil do begin while tl <> nil do begin
PutBackToken(tl^.token, false); PutBackToken(tl^.token, false, false);
tk := tl; tk := tl;
tl := tl^.next; tl := tl^.next;
dispose(tk); dispose(tk);
@ -1463,12 +1463,12 @@ var
NextToken; NextToken;
suppressMacroExpansions := lSuppressMacroExpansions; suppressMacroExpansions := lSuppressMacroExpansions;
if token.kind = rparench then begin if token.kind = rparench then begin
PutBackToken(token, false); PutBackToken(token, false, false);
NextToken; NextToken;
tPtr2^.prototyped := true; tPtr2^.prototyped := true;
end end
else begin else begin
PutBackToken(token, false); PutBackToken(token, false, false);
token.kind := voidsy; token.kind := voidsy;
token.class := reservedSymbol; token.class := reservedSymbol;
end; {else} end; {else}
@ -1861,7 +1861,6 @@ var
iPtr,jPtr,kPtr: initializerPtr; {for reversing the list} iPtr,jPtr,kPtr: initializerPtr; {for reversing the list}
ip: identList; {used to place an id in the list} ip: identList; {used to place an id in the list}
luseGlobalPool: boolean; {local copy of useGlobalPool} luseGlobalPool: boolean; {local copy of useGlobalPool}
skipComma: boolean; {skip an expected comma}
procedure InsertInitializerRecord (iPtr: initializerPtr; size: longint); procedure InsertInitializerRecord (iPtr: initializerPtr; size: longint);
@ -2442,11 +2441,8 @@ var
{check for designators that need to} {check for designators that need to}
{be handled at an outer level } {be handled at an outer level }
if token.kind in [dotch,lbrackch] then if token.kind in [dotch,lbrackch] then
if not (braces or nestedDesignator) then begin if not (braces or nestedDesignator) then
{TODO fill?}
skipComma := true;
goto 1; goto 1;
end; {if}
startingDisp := disp; startingDisp := disp;
setNoFill := noFill; setNoFill := noFill;
if kind = arrayType then begin if kind = arrayType then begin
@ -2521,7 +2517,9 @@ var
if token.kind in [lbrackch,dotch] then begin if token.kind in [lbrackch,dotch] then begin
if not (braces or (nestedDesignator and (disp=startingDisp))) if not (braces or (nestedDesignator and (disp=startingDisp)))
then begin then begin
skipComma := true; PutBackToken(token, false, true);
token.kind := commach;
token.class := reservedSymbol;
goto 1; goto 1;
end; {if} end; {if}
Match(lbrackch, 35); Match(lbrackch, 35);
@ -2561,10 +2559,7 @@ var
count := count+1; count := count+1;
if (count = maxCount) and not braces then if (count = maxCount) and not braces then
done := true done := true
else if (token.kind = commach) or skipComma then begin else if (token.kind = commach) then begin
if skipComma then
skipComma := false
else
NextToken; NextToken;
done := token.kind = rbracech; done := token.kind = rbracech;
if not done then if not done then
@ -2613,7 +2608,9 @@ var
if token.kind in [dotch,lbrackch] then begin if token.kind in [dotch,lbrackch] then begin
if not (braces or (nestedDesignator and (disp=startingDisp))) if not (braces or (nestedDesignator and (disp=startingDisp)))
then begin then begin
skipComma := true; PutBackToken(token, false, true);
token.kind := commach;
token.class := reservedSymbol;
goto 1; goto 1;
end; {if} end; {if}
Match(dotch, 35); Match(dotch, 35);
@ -2628,7 +2625,7 @@ var
if ip = nil then if ip = nil then
Error(81); Error(81);
if ip^.anonMemberField then begin if ip^.anonMemberField then begin
PutBackToken(token, false); PutBackToken(token, false, true);
token.kind := dotch; token.kind := dotch;
token.class := reservedSymbol; token.class := reservedSymbol;
token.isDigraph := false; token.isDigraph := false;
@ -2735,7 +2732,6 @@ var
begin {Initializer} begin {Initializer}
disp := 0; {start at beginning of the object} disp := 0; {start at beginning of the object}
errorFound := false; {no errors found so far} errorFound := false; {no errors found so far}
skipComma := false;
luseGlobalPool := useGlobalPool; {use global memory for global vars} luseGlobalPool := useGlobalPool; {use global memory for global vars}
useGlobalPool := (variable^.storage in [external,global,private]) useGlobalPool := (variable^.storage in [external,global,private])
or useGlobalPool; or useGlobalPool;
@ -4357,7 +4353,7 @@ case statementList^.kind of
NextToken; NextToken;
suppressMacroExpansions := lSuppressMacroExpansions; suppressMacroExpansions := lSuppressMacroExpansions;
nToken := token; nToken := token;
PutBackToken(nToken, false); PutBackToken(nToken, false, false);
token := lToken; token := lToken;
if nToken.kind <> colonch then if nToken.kind <> colonch then
DoDeclaration(false) DoDeclaration(false)

View File

@ -49,6 +49,7 @@ type
next: tokenListRecordPtr; {next element in list} next: tokenListRecordPtr; {next element in list}
token: tokenType; {token} token: tokenType; {token}
expandEnabled: boolean; {can this token be macro expanded?} expandEnabled: boolean; {can this token be macro expanded?}
suppressPrint: boolean; {suppress printing with #pragma expand?}
tokenStart,tokenEnd: ptr; {token start/end markers} tokenStart,tokenEnd: ptr; {token start/end markers}
end; end;
macroRecordPtr = ^macroRecord; macroRecordPtr = ^macroRecord;
@ -153,13 +154,15 @@ procedure NextToken;
{ Read the next token from the file. } { Read the next token from the file. }
procedure PutBackToken (var token: tokenType; expandEnabled: boolean); procedure PutBackToken (var token: tokenType; expandEnabled: boolean;
suppressPrint: boolean);
{ place a token into the token stream } { place a token into the token stream }
{ } { }
{ parameters: } { parameters: }
{ token - token to put back into the token stream } { token - token to put back into the token stream }
{ expandEnabled - can macro expansion be performed? } { expandEnabled - can macro expansion be performed? }
{ suppressPrint - suppress printing with #pragma expand? }
procedure TermScanner; procedure TermScanner;
@ -510,13 +513,15 @@ macroFound := mPtr;
end; {IsDefined} end; {IsDefined}
procedure PutBackToken {var token: tokenType; expandEnabled: boolean}; procedure PutBackToken {var token: tokenType; expandEnabled: boolean;
suppressPrint: boolean};
{ place a token into the token stream } { place a token into the token stream }
{ } { }
{ parameters: } { parameters: }
{ token - token to put back into the token stream } { token - token to put back into the token stream }
{ expandEnabled - can macro expansion be performed? } { expandEnabled - can macro expansion be performed? }
{ suppressPrint - suppress printing with #pragma expand? }
var var
tPtr: tokenListRecordPtr; {work pointer} tPtr: tokenListRecordPtr; {work pointer}
@ -527,6 +532,7 @@ tPtr^.next := tokenList;
tokenList := tPtr; tokenList := tPtr;
tPtr^.token := token; tPtr^.token := token;
tPtr^.expandEnabled := expandEnabled; tPtr^.expandEnabled := expandEnabled;
tPtr^.suppressPrint := suppressPrint;
tPtr^.tokenStart := tokenStart; tPtr^.tokenStart := tokenStart;
tPtr^.tokenEnd := tokenEnd; tPtr^.tokenEnd := tokenEnd;
end; {PutBackToken} end; {PutBackToken}
@ -1628,7 +1634,7 @@ else
end; {for} end; {for}
token.sval^.str[len+1] := chr(0); token.sval^.str[len+1] := chr(0);
token.sval^.length := len+1; token.sval^.length := len+1;
PutBackToken(token, true); PutBackToken(token, true, false);
end; {BuildStringToken} end; {BuildStringToken}
@ -1917,7 +1923,7 @@ if macro^.parameters >= 0 then begin {find the values of the parameters}
Error(14); Error(14);
if token.kind <> rparench then begin {insist on a closing ')'} if token.kind <> rparench then begin {insist on a closing ')'}
if not gettingFileName then {put back the source stream token} if not gettingFileName then {put back the source stream token}
PutBackToken(token, true); PutBackToken(token, true, false);
Error(12); Error(12);
end; {if} end; {if}
preprocessing := lPreprocessing; preprocessing := lPreprocessing;
@ -1925,7 +1931,7 @@ if macro^.parameters >= 0 then begin {find the values of the parameters}
else begin else begin
Error(13); Error(13);
if not gettingFileName then {put back the source stream token} if not gettingFileName then {put back the source stream token}
PutBackToken(token, true); PutBackToken(token, true, false);
end; {else} end; {else}
end; {if} end; {if}
if macro^.readOnly then begin {handle special macros} if macro^.readOnly then begin {handle special macros}
@ -2040,7 +2046,7 @@ if macro^.readOnly then begin {handle special macros}
end; {case} end; {case}
if macro^.algorithm <> 8 then {if not _Pragma} if macro^.algorithm <> 8 then {if not _Pragma}
PutBackToken(token, true); PutBackToken(token, true, false);
end {if} end {if}
else begin else begin
@ -2128,11 +2134,11 @@ else begin
if expandEnabled then if expandEnabled then
if tcPtr^.token.name^ = macro^.name^ then if tcPtr^.token.name^ = macro^.name^ then
expandEnabled := false; expandEnabled := false;
PutBackToken(tcPtr^.token, expandEnabled); PutBackToken(tcPtr^.token, expandEnabled, false);
end; {else} end; {else}
end {if} end {if}
else else
PutBackToken(tcPtr^.token, true); PutBackToken(tcPtr^.token, true, false);
tcPtr := tcPtr^.next; tcPtr := tcPtr^.next;
end; {while} end; {while}
end; {else} end; {else}
@ -2147,7 +2153,7 @@ else begin
expandEnabled := false; expandEnabled := false;
tokenStart := tlPtr^.tokenStart; tokenStart := tlPtr^.tokenStart;
tokenEnd := tlPtr^.tokenEnd; tokenEnd := tlPtr^.tokenEnd;
PutBackToken(tlPtr^.token, expandEnabled); PutBackToken(tlPtr^.token, expandEnabled, false);
end; {else} end; {else}
lastPtr := tlPtr; lastPtr := tlPtr;
tlPtr := tlPtr^.next; tlPtr := tlPtr^.next;
@ -3438,7 +3444,7 @@ if ch in ['a','d','e','i','l','p','u','w'] then begin
end; {if} end; {if}
if token.name^ <> 'STDC' then begin if token.name^ <> 'STDC' then begin
{Allow macro expansion, other than for STDC } {Allow macro expansion, other than for STDC }
PutBackToken(token, true); PutBackToken(token, true, false);
NextToken; NextToken;
end; {if} end; {if}
if token.name^ = 'keep' then if token.name^ = 'keep' then
@ -4903,6 +4909,7 @@ var
tPtr: tokenListRecordPtr; {for removing tokens from putback buffer} tPtr: tokenListRecordPtr; {for removing tokens from putback buffer}
tToken: tokenType; {for merging tokens} tToken: tokenType; {for merging tokens}
sPtr,tsPtr: gstringPtr; {for forming string constants} sPtr,tsPtr: gstringPtr; {for forming string constants}
suppressPrint: boolean; {suppress printing the token?}
lLastWasReturn: boolean; {local copy of lastWasReturn} lLastWasReturn: boolean; {local copy of lastWasReturn}
codePoint: longint; {Unicode character value} codePoint: longint; {Unicode character value}
chFromUCN: integer; {character given by UCN (converted)} chFromUCN: integer; {character given by UCN (converted)}
@ -5166,6 +5173,7 @@ if tokenList <> nil then begin {get a token put back by a macro}
tokenList := tPtr^.next; tokenList := tPtr^.next;
expandEnabled := tPtr^.expandEnabled; expandEnabled := tPtr^.expandEnabled;
tokenExpandEnabled := expandEnabled; tokenExpandEnabled := expandEnabled;
suppressPrint := tPtr^.suppressPrint;
token := tPtr^.token; token := tPtr^.token;
tokenStart := tPtr^.tokenStart; tokenStart := tPtr^.tokenStart;
tokenEnd := tPtr^.tokenEnd; tokenEnd := tPtr^.tokenEnd;
@ -5226,7 +5234,9 @@ if tokenList <> nil then begin {get a token put back by a macro}
expandMacros := lExpandMacros; expandMacros := lExpandMacros;
end; {if} end; {if}
goto 2; goto 2;
end; {if} end {if}
else
suppressPrint := false;
5: {skip white space} 5: {skip white space}
while charKinds[ord(ch)] in [illegal,ch_white,ch_eol,ch_pound] do begin while charKinds[ord(ch)] in [illegal,ch_white,ch_eol,ch_pound] do begin
if charKinds[ord(ch)] = ch_pound then begin if charKinds[ord(ch)] = ch_pound then begin
@ -5740,7 +5750,7 @@ if (token.kind = stringconst) and not mergingStrings {handle adjacent strings}
done := false; done := false;
end {if} end {if}
else begin else begin
PutBackToken(token, tokenExpandEnabled); PutBackToken(token, tokenExpandEnabled, false);
done := true; done := true;
end; {else} end; {else}
token := tToken; token := tToken;
@ -5755,7 +5765,9 @@ if doingPPExpression then begin
if token.kind = typedef then if token.kind = typedef then
token.kind := ident; token.kind := ident;
end; {if} end; {if}
if printMacroExpansions and not suppressMacroExpansions then if printMacroExpansions then
if not suppressMacroExpansions then
if not suppressPrint then
PrintToken(token); {print the token stream} PrintToken(token); {print the token stream}
if token.kind = otherch then if token.kind = otherch then
if not (skipping or preprocessing or suppressMacroExpansions) if not (skipping or preprocessing or suppressMacroExpansions)