From: Gaius Mulley Date: Fri, 27 Jan 2023 22:18:46 +0000 (+0000) Subject: PR-108557 Stuck compilation for empty file X-Git-Tag: basepoints/gcc-14~1744 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97bf709f35af45f9cf2902904d82493efed0d2ec;p=thirdparty%2Fgcc.git PR-108557 Stuck compilation for empty file Trying to compile an empty file causes cc1gm2 to hang. The bug occurs when M2LexBuf.mod calls m2flex.GetToken after an eof token has been seen which results in m2flex attempting to read from stdin. The bug fix detects eof per file and blocks subsequent calls to m2flex.GetToken. gcc/m2/ChangeLog: * gm2-compiler/M2Comp.mod: Import MetaString0. (ExamineCompilationUnit): New variable Message. Create and format error string. * gm2-compiler/M2LexBuf.mod: New variable SeenEof. (GetNonEofToken): New procedure. (Init): Set SeenEof to FALSE. (GetToken): Use GetNonEofToken instead of calls to m2flex.GetToken and GetToken. (AddTok): Detect eoftok and set SeenEof. gcc/testsuite/ChangeLog: * gm2/pim/fail/empty.mod: New test. Signed-off-by: Gaius Mulley --- diff --git a/gcc/m2/gm2-compiler/M2Comp.mod b/gcc/m2/gm2-compiler/M2Comp.mod index 05eaaccb5549..3c2c3643b186 100644 --- a/gcc/m2/gm2-compiler/M2Comp.mod +++ b/gcc/m2/gm2-compiler/M2Comp.mod @@ -39,7 +39,7 @@ FROM libc IMPORT exit ; FROM M2Error IMPORT ErrorStringAt, ErrorStringAt2, ErrorStringsAt2, WriteFormat0, FlushErrors, FlushWarnings, ResetErrorScope ; -FROM M2MetaError IMPORT MetaErrorString1, MetaError0, MetaError1 ; +FROM M2MetaError IMPORT MetaErrorString0, MetaErrorString1, MetaError0, MetaError1, MetaString0 ; FROM FormatStrings IMPORT Sprintf1 ; FROM P0SymBuild IMPORT P0Init, P1Init ; @@ -173,6 +173,8 @@ END compile ; *) PROCEDURE ExamineCompilationUnit (VAR name: ADDRESS; VAR isdefimp: BOOLEAN) ; +VAR + Message: String ; BEGIN isdefimp := FALSE ; (* default to program module *) (* stop if we see eof, ';' or '[' *) @@ -189,8 +191,9 @@ BEGIN END ; GetToken END ; - m2flex.M2Error(string(InitString('failed to find module name'))) ; - exit(1) + Message := MetaString0 (InitString ('no {%kMODULE} name found')) ; + m2flex.M2Error (string (Message)) ; + exit (1) END ExamineCompilationUnit ; @@ -204,20 +207,20 @@ VAR name : ADDRESS ; isdefimp: BOOLEAN ; BEGIN - IF OpenSource(s) + IF OpenSource (s) THEN - ExamineCompilationUnit(name, isdefimp) ; + ExamineCompilationUnit (name, isdefimp) ; IF isdefimp THEN - SetMainModule(MakeImplementationSource(GetTokenNo(), makekey(name))) + SetMainModule (MakeImplementationSource (GetTokenNo (), makekey (name))) ELSE - SetMainModule(MakeProgramSource(GetTokenNo(), makekey(name))) + SetMainModule (MakeProgramSource (GetTokenNo (), makekey (name))) END ; CloseSource ; ReInitialize ELSE - fprintf1(StdErr, 'failed to open %s\n', s) ; - exit(1) + fprintf1 (StdErr, 'failed to open %s\n', s) ; + exit (1) END END PeepInto ; diff --git a/gcc/m2/gm2-compiler/M2LexBuf.mod b/gcc/m2/gm2-compiler/M2LexBuf.mod index a557f4ebeaec..ac496f2d9327 100644 --- a/gcc/m2/gm2-compiler/M2LexBuf.mod +++ b/gcc/m2/gm2-compiler/M2LexBuf.mod @@ -82,6 +82,8 @@ VAR ListOfTokens : ListDesc ; CurrentTokNo : CARDINAL ; InsertionIndex : CARDINAL ; + SeenEof : BOOLEAN ; (* Have we seen eof since the last call + to OpenSource. *) (* @@ -122,6 +124,7 @@ END InitTokenList ; PROCEDURE Init ; BEGIN + SeenEof := FALSE ; InsertionIndex := 0 ; currenttoken := eoftok ; CurrentTokNo := InitialSourceToken ; @@ -337,6 +340,7 @@ END SetFile ; PROCEDURE OpenSource (s: String) : BOOLEAN ; BEGIN + SeenEof := FALSE ; IF UseBufferedTokens THEN GetToken ; @@ -605,6 +609,27 @@ BEGIN END DumpTokens ; +(* + GetNonEofToken - providing that we have not already seen an eof for this source + file call m2flex.GetToken and GetToken if requested. +*) + +PROCEDURE GetNonEofToken (callGetToken: BOOLEAN) ; +BEGIN + IF SeenEof + THEN + currenttoken := eoftok + ELSE + (* Call the lexical phase to place a new token into the last bucket. *) + m2flex.GetToken () ; + IF callGetToken + THEN + GetToken + END + END +END GetNonEofToken ; + + (* GetToken - gets the next token into currenttoken. *) @@ -622,7 +647,7 @@ BEGIN ELSE IF ListOfTokens.tail=NIL THEN - m2flex.GetToken () ; + GetNonEofToken (FALSE) ; IF ListOfTokens.tail=NIL THEN HALT @@ -630,16 +655,14 @@ BEGIN END ; IF CurrentTokNo>=ListOfTokens.LastBucketOffset THEN - (* CurrentTokNo is in the last bucket or needs to be read *) + (* CurrentTokNo is in the last bucket or needs to be read. *) IF CurrentTokNo-ListOfTokens.LastBucketOffset