]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
a68: emit proper error for empty source file
authorJose E. Marchesi <jose.marchesi@oracle.com>
Fri, 12 Dec 2025 02:43:38 +0000 (03:43 +0100)
committerJose E. Marchesi <jose.marchesi@oracle.com>
Fri, 12 Dec 2025 02:49:54 +0000 (03:49 +0100)
Ok this is an embarrassing one.  A source file that is either empty or
that contains just blanks, characters and/or pragmats, is not a proper
particular program nor a prelude packet.  This patch makes ga68 to
emit an error mesage rather than ICEing.

Signed-off-by: Jose E. Marchesi <jemarch@gnu.org>
gcc/algol68/ChangeLog

* a68-parser-scanner.cc (a68_lexical_analyser): New argument
empty_program.
* a68.h: Update prototype of a68_lexical_analyser.
* a68-parser.cc (a68_parser): Emit error for empty input files.

gcc/algol68/a68-parser-scanner.cc
gcc/algol68/a68-parser.cc
gcc/algol68/a68.h

index 966ebdf6ed1d1245d61a54cc9a403555ca9d0416..8c0694de9ba26b60464ca622f7fa12edeb74eb44 100644 (file)
@@ -328,12 +328,7 @@ read_source_file (const char *filename)
     fatal_error (UNKNOWN_LOCATION, "specified file %s is a directory",
                 filename);
 
-  if ((source_file_size = get_source_size ()) == 0)
-    {
-      /* The source file is empty.  */
-      ret = false;
-      goto done;
-    }
+  source_file_size = get_source_size ();
 
   /* Allocate A68_PARSER (scan_buf), which is an auxiliary buffer used by the
      scanner known to be big enough to hold any string contained in the source
@@ -395,7 +390,6 @@ read_source_file (const char *filename)
   /* Include files.  */
   include_files (TOP_LINE (&A68_JOB));
 
- done:
   if (fclose (FILE_SOURCE_FD (&A68_JOB)) != 0)
     gcc_unreachable ();
   return ret;
@@ -2279,7 +2273,7 @@ tokenise_source (NODE_T **root, int level, bool in_format,
 /* Tokenise source file, build initial syntax tree.  */
 
 bool
-a68_lexical_analyser (const char *filename)
+a68_lexical_analyser (const char *filename, bool *empty_program)
 {
   LINE_T *l = NO_LINE, *start_l = NO_LINE;
   char *s = NO_TEXT, *start_c = NO_TEXT;
@@ -2295,6 +2289,23 @@ a68_lexical_analyser (const char *filename)
     s = STRING (l);
   tokenise_source (&root, 0, false, &l, &s, &start_l, &start_c);
 
+  /* Detemine whether the actual file contents resulted in some token.  This is
+     used in order to provide better diagnostics for empty source files or
+     files containing only comments or pragmats.  These are not valid Algol 68
+     packets.  */
+
+  *empty_program = true;
+  for (NODE_T *p = TOP_NODE (&A68_JOB); p != NO_NODE; FORWARD (p))
+    {
+      LINE_T *l = LINE (INFO (p));
+      if (strcmp (FILENAME (l), "prelude") != 0
+         && strcmp (FILENAME (l), "postlude") != 0)
+       {
+         *empty_program = false;
+         break;
+       }
+    }
+
   /* If the source is a prelude packet then we should remove the prelude and
      postlude nodes from the token stream.  We distinguish these nodes by
      location.
index f01ecbee434bf44e287eadfa1abad96b47a06742..c43efe3ba193db9d80770923d4101f48689f2905 100644 (file)
@@ -454,17 +454,15 @@ a68_parser (const char *filename)
   /* Tokeniser.  */
   if (ERROR_COUNT (&A68_JOB) == 0)
     {
-      bool ok = a68_lexical_analyser (filename);
+      bool empty_program;
+      bool ok = a68_lexical_analyser (filename, &empty_program);
 
       if (!ok)
        return;
 
-      /* An empty file is not a valid program.  */
-      if (TOP_NODE (&A68_JOB) == NO_NODE)
-       {
-         a68_error (NO_NODE, "file is empty, expected a program");
-         return;
-       }
+      if (empty_program)
+       a68_error (NO_NODE,
+                  "particular program or prelude packet not found in source file");
 
       TREE_LISTING_SAFE (&A68_JOB) = true;
       renum = 0;
index 92dc28e222f2eedb7c65b54d846fdfa13f2b04ab..40ceec9ddc3ea873a146bc88df83cdb3d9a3bc83 100644 (file)
@@ -280,7 +280,7 @@ void a68_scan_error (LINE_T *u, char *v, const char *txt, ...);
 
 /* a68-parser-scanner.cc  */
 
-bool a68_lexical_analyser (const char *filename);
+bool a68_lexical_analyser (const char *filename, bool *empty_file);
 
 /* a68-parser.cc  */