PR-108557 Stuck compilation for empty file
authorGaius Mulley <gaiusmod2@gmail.com>
Fri, 27 Jan 2023 22:18:46 +0000 (22:18 +0000)
committerGaius Mulley <gaiusmod2@gmail.com>
Fri, 27 Jan 2023 22:18:46 +0000 (22:18 +0000)
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>
gcc/m2/gm2-compiler/M2Comp.mod
gcc/m2/gm2-compiler/M2LexBuf.mod
gcc/testsuite/gm2/pim/fail/empty.mod [new file with mode: 0644]

index 05eaacc..3c2c364 100644 (file)
@@ -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 ;
 
index a557f4e..ac496f2 100644 (file)
@@ -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,
diff --git a/gcc/testsuite/gm2/pim/fail/empty.mod b/gcc/testsuite/gm2/pim/fail/empty.mod
new file mode 100644 (file)
index 0000000..8b13789
--- /dev/null
@@ -0,0 +1 @@
+