aboutsummaryrefslogtreecommitdiff
path: root/gcc/m2
diff options
context:
space:
mode:
authorGaius Mulley <gaiusmod2@gmail.com>2023-01-27 22:18:46 +0000
committerGaius Mulley <gaiusmod2@gmail.com>2023-01-27 22:18:46 +0000
commit97bf709f35af45f9cf2902904d82493efed0d2ec (patch)
treef53f7c974d358568b6a416c63a816287cd8df5dc /gcc/m2
parent84eb39556cc8449e04b5f48bd5c131941a7a2529 (diff)
downloadgcc-97bf709f35af45f9cf2902904d82493efed0d2ec.zip
gcc-97bf709f35af45f9cf2902904d82493efed0d2ec.tar.gz
gcc-97bf709f35af45f9cf2902904d82493efed0d2ec.tar.bz2
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 <gaiusmod2@gmail.com>
Diffstat (limited to 'gcc/m2')
-rw-r--r--gcc/m2/gm2-compiler/M2Comp.mod21
-rw-r--r--gcc/m2/gm2-compiler/M2LexBuf.mod39
2 files changed, 45 insertions, 15 deletions
diff --git a/gcc/m2/gm2-compiler/M2Comp.mod b/gcc/m2/gm2-compiler/M2Comp.mod
index 05eaacc..3c2c364 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 a557f4e..ac496f2 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 ;
@@ -606,6 +610,27 @@ 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<ListOfTokens.tail^.len
THEN
UpdateFromBucket (ListOfTokens.tail,
CurrentTokNo-ListOfTokens.LastBucketOffset)
ELSE
- (* call the lexical phase to place a new token into the last bucket *)
- m2flex.GetToken () ;
- GetToken ; (* and call ourselves again to collect the token from bucket *)
- RETURN
+ (* and call ourselves again to collect the token from bucket *)
+ GetNonEofToken (TRUE)
END
ELSE
t := CurrentTokNo ;
@@ -1175,6 +1198,10 @@ PROCEDURE AddTok (t: toktype) ;
VAR
s: String ;
BEGIN
+ IF t = eoftok
+ THEN
+ SeenEof := TRUE
+ END ;
IF NOT ((t=eoftok) AND IsLastTokenEof())
THEN
AddTokToList(t, NulName, 0,