]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR modula2/109336 - The -fmod= and -fdef= options do not work
authorGaius Mulley <gaiusmod2@gmail.com>
Wed, 29 Mar 2023 16:38:22 +0000 (17:38 +0100)
committerGaius Mulley <gaiusmod2@gmail.com>
Wed, 29 Mar 2023 16:38:22 +0000 (17:38 +0100)
The -fmod= and -fdef= options do not work.  After the linking
re-implementation and subsequent restructuring the -fmod= amd -fdef= are
now broken.  This patch adds -fmod= and -fdef= processing into gm2spec.cc.
It also reduces the complexity of extension handling within M2Search
by storing the preceeding "." in the extension.

gcc/m2/ChangeLog:

PR modula2/109336
PR modula2/109315
* gm2-compiler/M2FileName.mod (CalculateFileName): Simplified by
ensuring the extension contains the ".".
(CalculateStemName): Re-formatted.
(ExtractExtension): Re-formatted.
(ExtractModule): Re-formatted.
* gm2-compiler/M2Options.def (setdefextension): Add block comment.
(setmodextension): Add block comment.  Re-formatted.
* gm2-compiler/M2Options.mod (setdefextension): Add block comment.
(setmodextension): Add block comment.  Re-formatted.
* gm2-compiler/M2Search.mod (FindSourceDefFile): Use
DefaultDefExt.
(DefaultDefExt): New constant.
(DefaultModExt): New constant.
(FindSourceModFile): Use DefaultModExt.
* gm2-gcc/m2decl.cc (m2decl_DeclareKnownVariable): Correct
spelling.
* gm2spec.cc (M2SOURCE): New constant.
(LANGSPEC): New value.
(MATHLIB): New value.
(WITHLIBC): New value.
(SKIPOPT): New value.
(lang_specific_driver): Replace seen_module_extension bool with
module_extension char *.  Detect -fmod= and remember extension.
Use the extension to detect modula-2 source and mark it as such.

gcc/testsuite/ChangeLog:

PR modula2/109336
* gm2/link/nondefaultext/pass/hello.md: New test.
* gm2/link/nondefaultext/pass/liba.dm: New test.
* gm2/link/nondefaultext/pass/liba.md: New test.
* gm2/link/nondefaultext/pass/link-nondefaultext-pass.exp: New test.

Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
gcc/m2/gm2-compiler/M2FileName.mod
gcc/m2/gm2-compiler/M2Options.def
gcc/m2/gm2-compiler/M2Options.mod
gcc/m2/gm2-compiler/M2Search.mod
gcc/m2/gm2-gcc/m2decl.cc
gcc/m2/gm2spec.cc
gcc/testsuite/gm2/link/nondefaultext/pass/hello.md [new file with mode: 0644]
gcc/testsuite/gm2/link/nondefaultext/pass/liba.dm [new file with mode: 0644]
gcc/testsuite/gm2/link/nondefaultext/pass/liba.md [new file with mode: 0644]
gcc/testsuite/gm2/link/nondefaultext/pass/link-nondefaultext-pass.exp [new file with mode: 0755]

index 45a77f8be7ad2cf606cbdb80c96c3c876165a947..e52df96bdaeb5ab04e609e98d595e10853c65c08 100644 (file)
@@ -49,9 +49,9 @@ PROCEDURE CalculateFileName (Module, Extension: String) : String ;
 BEGIN
    IF MaxFileName=0
    THEN
-      RETURN( ConCat(ConCatChar(Slice(Module, 0, MaxFileName), '.'), Extension) )
+      RETURN ConCat (Slice (Module, 0, MaxFileName), Extension)
    ELSE
-      RETURN( ConCat(ConCatChar(Slice(Module, 0, MaxFileName-Length(Extension)-1), '.'), Extension) )
+      RETURN ConCat (Slice (Module, 0, MaxFileName-Length (Extension)), Extension)
    END
 END CalculateFileName ;
 
@@ -64,7 +64,7 @@ END CalculateFileName ;
 
 PROCEDURE CalculateStemName (Module: String) : String ;
 BEGIN
-   RETURN( Slice(Module, 0, MaxStemName) )
+   RETURN Slice (Module, 0, MaxStemName)
 END CalculateStemName ;
 
 
@@ -75,11 +75,11 @@ END CalculateStemName ;
 
 PROCEDURE ExtractExtension (filename, ext: String) : String ;
 BEGIN
-   IF Equal(ext, Mark(Slice(filename, -Length(ext), 0)))
+   IF Equal (ext, Mark (Slice (filename, -Length (ext), 0)))
    THEN
-      RETURN( Slice(filename, 0, -Length(ext)) )
+      RETURN Slice (filename, 0, -Length (ext))
    ELSE
-      RETURN( filename )
+      RETURN filename
    END
 END ExtractExtension ;
 
@@ -93,12 +93,12 @@ PROCEDURE ExtractModule (filename: String) : String ;
 VAR
    i: INTEGER ;
 BEGIN
-   i := Index(filename, Directory, 0) ;
+   i := Index (filename, Directory, 0) ;
    IF i=-1
    THEN
-      RETURN( Dup(filename) )
+      RETURN Dup (filename)
    ELSE
-      RETURN( Slice(filename, i+1, 0) )
+      RETURN Slice (filename, i+1, 0)
    END
 END ExtractModule ;
 
index 2be92d594b7048fe885276adc864200d291c44c6..7e4d2aa6b41a8c972a33f1a9a2d0887eeb32ff47 100644 (file)
@@ -730,14 +730,16 @@ PROCEDURE SetStrictTypeChecking (value: BOOLEAN) ;
 
 
 (*
-   setdefextension -
+   setdefextension - set the source file definition module extension to arg.
+                     This should include the . and by default it is set to .def.
 *)
 
 PROCEDURE setdefextension (arg: ADDRESS) ;
 
 
 (*
-   setmodextension -
+   setmodextension - set the source file module extension to arg.
+                     This should include the . and by default it is set to .mod.
 *)
 
 PROCEDURE setmodextension (arg: ADDRESS) ;
index 2b3ed81f95a21b7426390fadd0487135cbb7ce65..7cacee26bcfd48161366f6dbf80abf1532d3c544 100644 (file)
@@ -959,30 +959,32 @@ END SetSearchPath ;
 
 
 (*
-   setdefextension -
+   setdefextension - set the source file definition module extension to arg.
+                     This should include the . and by default it is set to .def.
 *)
 
 PROCEDURE setdefextension (arg: ADDRESS) ;
 VAR
    s: String ;
 BEGIN
-   s := InitStringCharStar(arg) ;
-   SetDefExtension(s) ;
-   s := KillString(s)
+   s := InitStringCharStar (arg) ;
+   SetDefExtension (s) ;
+   s := KillString (s)
 END setdefextension ;
 
 
 (*
-   setmodextension -
+   setmodextension - set the source file module extension to arg.
+                     This should include the . and by default it is set to .mod.
 *)
 
 PROCEDURE setmodextension (arg: ADDRESS) ;
 VAR
    s: String ;
 BEGIN
-   s := InitStringCharStar(arg) ;
-   SetModExtension(s) ;
-   s := KillString(s)
+   s := InitStringCharStar (arg) ;
+   SetModExtension (s) ;
+   s := KillString (s)
 END setmodextension ;
 
 
index 80806bf8084f63997b889ffb992c3b36323256c9..5e48f6b5ff5fad0073074507e28ebf596dca1c02 100644 (file)
@@ -36,6 +36,8 @@ FROM DynamicStrings IMPORT InitString, InitStringChar,
 
 CONST
    GarbageDebugging = FALSE ;
+   DefaultDefExt    = '.def' ;
+   DefaultModExt    = '.mod' ;
 
 VAR
    Def, Mod: String ;
@@ -156,7 +158,7 @@ BEGIN
       f := KillString (f)
    END ;
    (* Try the GNU Modula-2 default extension.  *)
-   f := CalculateFileName (Stem, Mark (InitString ('def'))) ;
+   f := CalculateFileName (Stem, Mark (InitString (DefaultDefExt))) ;
    RETURN FindSourceFile (f, FullPath, named)
 END FindSourceDefFile ;
 
@@ -182,7 +184,7 @@ BEGIN
       f := KillString (f)
    END ;
    (* Try the GNU Modula-2 default extension.  *)
-   f := CalculateFileName (Stem, Mark (InitString ('mod'))) ;
+   f := CalculateFileName (Stem, Mark (InitString (DefaultModExt))) ;
    RETURN FindSourceFile (f, FullPath, named)
 END FindSourceModFile ;
 
index 4fb646e2dab966deb398214231f7e51ec33b349d..6dde7a22ea5cdab72d44e89979c46289ff3e4ac7 100644 (file)
@@ -122,7 +122,7 @@ m2decl_DeclareKnownVariable (location_t location, const char *name, tree type,
     error ("storage size of %qD has not been resolved", decl);
 
   if ((TREE_PUBLIC (decl) == 0) && DECL_EXTERNAL (decl))
-    internal_error ("inconsistant because %qs",
+    internal_error ("inconsistent because %qs",
                    "PUBLIC_DECL(decl) == 0 && DECL_EXTERNAL(decl) == 1");
 
   m2block_addDeclExpr (build_stmt (location, DECL_EXPR, decl));
index 3b2652cd92a1880b2591dbf4fb56debe387547db..8b4010838ec57044fc2aa574636e2bb8b09377da 100644 (file)
@@ -50,14 +50,16 @@ along with GNU Modula-2; see the file COPYING3.  If not see
 #endif
 #endif
 
+/* This bit is set if the arguments is a M2 source file.  */
+#define M2SOURCE       (1<<1)
 /* This bit is set if we saw a `-xfoo' language specification.  */
-#define LANGSPEC       (1<<1)
+#define LANGSPEC       (1<<2)
 /* This bit is set if they did `-lm' or `-lmath'.  */
-#define MATHLIB                (1<<2)
+#define MATHLIB                (1<<3)
 /* This bit is set if they did `-lc'.  */
-#define WITHLIBC       (1<<3)
+#define WITHLIBC       (1<<4)
 /* Skip this option.  */
-#define SKIPOPT                (1<<4)
+#define SKIPOPT                (1<<5)
 
 #ifndef MATH_LIBRARY
 #define MATH_LIBRARY "m"
@@ -431,7 +433,7 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
   int *args;
 
   /* Have we seen -fmod=?  */
-  bool seen_module_extension = false;
+  char *module_extension = NULL;
 
   /* Should the driver perform a link?  */
   bool linking = true;
@@ -530,8 +532,11 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
          args[i] |= SKIPOPT; /* We will add the option if it is needed.  */
          break;
        case OPT_fmod_:
-         seen_module_extension = true;
+         module_extension = xstrdup (arg);
          args[i] |= SKIPOPT; /* We will add the option if it is needed.  */
+#if defined(DEBUG_ARG)
+         printf ("seen -fmod=%s\n", module_extension);
+#endif
          break;
         case OPT_fpthread:
           need_pthread = decoded_options[i].value;
@@ -674,6 +679,34 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
          which_library = (stdcxxlib_kind) decoded_options[i].value;
          break;
 
+       case OPT_SPECIAL_input_file:
+         {
+           const char *source_file = decoded_options[i].orig_option_with_args_text;
+#if defined(DEBUG_ARG)
+           printf ("seen OPT_SPECIAL_input_file: %s\n", source_file);
+#endif
+           if (source_file != NULL)
+             {
+               /* Record that this is a Modula-2 source file.  */
+               const char *suffix = strrchr (source_file, '.');
+#if defined(DEBUG_ARG)
+               printf ("ext = %s\n", suffix);
+#endif
+               if ((suffix != NULL)
+                   && ((strcmp (suffix, ".mod") == 0)
+                       || ((module_extension != NULL)
+                           && (strcmp (suffix, module_extension) == 0))))
+                 {
+#if defined(DEBUG_ARG)
+                   printf ("modula-2 source file detected: %s\n", source_file);
+#endif
+                   args[i] |= M2SOURCE;
+                   // args[i] = 0;
+                 }
+             }
+         }
+         break;
+
        default:
          break;
        }
@@ -719,19 +752,49 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
   for (i = 1; i < argc; ++i)
     {
 #if defined(DEBUG_ARG)
-      printf ("2nd pass: %s\n",
+      printf ("2nd pass: %s",
              decoded_options[i].orig_option_with_args_text);
+      if ((args[i] & SKIPOPT) != 0)
+       printf (" skipped");
+      if ((args[i] & M2SOURCE) != 0)
+       printf (" m2 source");
+      printf ("\n");
 #endif
       if ((args[i] & SKIPOPT) == 0)
        {
-         append_arg (&decoded_options[i]);
-         /* Make sure -lstdc++ is before the math library, since libstdc++
-            itself uses those math routines.  */
-         if (!saw_math && (args[i] & MATHLIB) && library > 0)
-           saw_math = &decoded_options[i];
-
-         if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
-           saw_libc = &decoded_options[i];
+         if ((args[i] & M2SOURCE) == 0)
+           {
+             append_arg (&decoded_options[i]);
+             /* Make sure -lstdc++ is before the math library, since libstdc++
+                itself uses those math routines.  */
+             if (!saw_math && (args[i] & MATHLIB) && library > 0)
+               saw_math = &decoded_options[i];
+
+             if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
+               saw_libc = &decoded_options[i];
+           }
+         else
+           {
+             if ((! seen_x_flag) && module_extension)
+               {
+#if defined(DEBUG_ARG)
+                 printf (" adding: -x modula-2 ");
+#endif
+                 append_option (OPT_x, "modula-2", 1);
+               }
+             append_arg (&decoded_options[i]);
+#if defined(DEBUG_ARG)
+             printf (" adding: %s\n",
+                     decoded_options[i].orig_option_with_args_text);
+#endif
+             if ((! seen_x_flag) && module_extension)
+               {
+#if defined(DEBUG_ARG)
+                 printf (" adding: -x none ");
+#endif
+                 append_option (OPT_x, "none", 1);
+               }
+           }
        }
 #if defined(DEBUG_ARG)
       else
@@ -789,9 +852,6 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
   else
     append_option (OPT_flibs_, xstrdup ("-"), 0); /* no system libs.  */
 
-  if ((! seen_x_flag) && seen_module_extension)
-    append_option (OPT_x, "modula-2", 1);
-
   if (need_plugin)
     append_option (OPT_fplugin_, "m2rte", 1);
 
diff --git a/gcc/testsuite/gm2/link/nondefaultext/pass/hello.md b/gcc/testsuite/gm2/link/nondefaultext/pass/hello.md
new file mode 100644 (file)
index 0000000..c2f2dcb
--- /dev/null
@@ -0,0 +1,9 @@
+MODULE hello ;
+
+FROM liba IMPORT bar ;
+FROM libc IMPORT printf ;
+
+BEGIN
+   bar ;
+   printf ("hello world\n")
+END hello.
diff --git a/gcc/testsuite/gm2/link/nondefaultext/pass/liba.dm b/gcc/testsuite/gm2/link/nondefaultext/pass/liba.dm
new file mode 100644 (file)
index 0000000..e86d9e3
--- /dev/null
@@ -0,0 +1,8 @@
+DEFINITION MODULE liba ;
+
+CONST
+   max = 12 ;
+
+PROCEDURE bar ;
+
+END liba.
diff --git a/gcc/testsuite/gm2/link/nondefaultext/pass/liba.md b/gcc/testsuite/gm2/link/nondefaultext/pass/liba.md
new file mode 100644 (file)
index 0000000..20d1206
--- /dev/null
@@ -0,0 +1,6 @@
+IMPLEMENTATION MODULE liba ;
+
+PROCEDURE bar ;
+END bar ;
+
+END liba.
diff --git a/gcc/testsuite/gm2/link/nondefaultext/pass/link-nondefaultext-pass.exp b/gcc/testsuite/gm2/link/nondefaultext/pass/link-nondefaultext-pass.exp
new file mode 100755 (executable)
index 0000000..e9e38f7
--- /dev/null
@@ -0,0 +1,38 @@
+# Expect driver script for GCC Regression Tests
+# Copyright (C) 2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# This file was written by Gaius Mulley (gaius.mulley@southwales.ac.uk)
+# for GNU Modula-2.
+
+if $tracelevel then {
+    strace $tracelevel
+}
+
+# load support procs
+load_lib gm2-torture.exp
+
+gm2_init_pim "${srcdir}/gm2/link/nondefaultext/pass"
+gm2_link_obj "liba.o"
+
+foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/hello.md]] {
+    # If we're only testing specific files and this isn't one of them, skip it.
+    if ![runtest_file_p $runtests $testcase] then {
+       continue
+    }
+    gm2_target_compile $srcdir/$subdir/liba.md liba.o object "-g -fdef=.dm -fmod=.md"
+    gm2-torture $testcase "-g -fdef=.dm -fmod=.md"
+}