From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1499) id F1F273858D20; Fri, 27 Jan 2023 22:19:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F1F273858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674857951; bh=rH+E+PCcLpg8teT75R5Ds0T/Zsac9/Qw1o2bno7nptw=; h=From:To:Subject:Date:From; b=M1iVJBfVpW2Kn2MtHKDhZhzK0k0jmBjTr6vOsvDSAv5IdQJg3nkNxywtdakr54AlX fRnAeBfLQSNt4Q5nz0eYLTyZxGnPS5SQeqHzO+/IyMQpK1BvWsp6ZA+8OJLfZ+C/J1 TJanGB38/m8bzum4vF9yvzuroZlhYvyXsRMyZLS0= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Gaius Mulley To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5463] PR-108557 Stuck compilation for empty file X-Act-Checkin: gcc X-Git-Author: Gaius Mulley X-Git-Refname: refs/heads/master X-Git-Oldrev: 84eb39556cc8449e04b5f48bd5c131941a7a2529 X-Git-Newrev: 97bf709f35af45f9cf2902904d82493efed0d2ec Message-Id: <20230127221911.F1F273858D20@sourceware.org> Date: Fri, 27 Jan 2023 22:19:11 +0000 (GMT) List-Id: https://gcc.gnu.org/g:97bf709f35af45f9cf2902904d82493efed0d2ec commit r13-5463-g97bf709f35af45f9cf2902904d82493efed0d2ec Author: Gaius Mulley Date: Fri Jan 27 22:18:46 2023 +0000 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: --- gcc/m2/gm2-compiler/M2Comp.mod | 21 ++++++++++--------- gcc/m2/gm2-compiler/M2LexBuf.mod | 39 ++++++++++++++++++++++++++++++------ gcc/testsuite/gm2/pim/fail/empty.mod | 1 + 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/gcc/m2/gm2-compiler/M2Comp.mod b/gcc/m2/gm2-compiler/M2Comp.mod index 05eaaccb554..3c2c3643b18 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 a557f4ebeae..ac496f2d932 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