]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
d: Add option to include imported modules in the compilation [PR109023]
authorIain Buclaw <ibuclaw@gdcproject.org>
Sat, 12 Apr 2025 09:13:50 +0000 (11:13 +0200)
committerIain Buclaw <ibuclaw@gdcproject.org>
Sat, 12 Apr 2025 20:02:26 +0000 (22:02 +0200)
Adds the ability to include imported modules in the compilation, as if
they were given on the command line.  When this option is enabled, all
imported modules are compiled except those that are part of libphobos.

PR d/109023

gcc/d/ChangeLog:

* d-compiler.cc: Include dmd/errors.h.
(Compiler::onImport): Implement.
* d-lang.cc (d_handle_option): Handle -finclude-imports.
(d_parse_file): Run semantic on included imports.
* gdc.texi: Document -finclude-imports.
* lang.opt: Add finclude-imports.
* lang.opt.urls: Regenerate.

gcc/testsuite/ChangeLog:

* gdc.dg/torture/imports/pr109023.d: New test.
* gdc.dg/torture/pr109023.d: New test.

gcc/d/d-compiler.cc
gcc/d/d-lang.cc
gcc/d/gdc.texi
gcc/d/lang.opt
gcc/d/lang.opt.urls
gcc/testsuite/gdc.dg/torture/imports/pr109023.d [new file with mode: 0644]
gcc/testsuite/gdc.dg/torture/pr109023.d [new file with mode: 0644]

index 160539d08ae0fd18054137f2e5adcaea38a225ed..e18f5d33e6aa62a7ede0a1c45333eb29afb5d838 100644 (file)
@@ -20,6 +20,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "coretypes.h"
 
 #include "dmd/compiler.h"
+#include "dmd/errors.h"
 #include "dmd/expression.h"
 #include "dmd/identifier.h"
 #include "dmd/module.h"
@@ -164,7 +165,39 @@ Compiler::onParseModule (Module *m)
    driver intends on compiling the import.  */
 
 bool
-Compiler::onImport (Module *)
+Compiler::onImport (Module *m)
 {
-  return false;
+  if (!includeImports)
+    return false;
+
+  if (m->filetype != FileType::d && m->filetype != FileType::c)
+    return false;
+
+  /* All imports modules are included except those in the runtime library.  */
+  ModuleDeclaration *md = m->md;
+  if (md && md->id)
+    {
+      if (md->packages.length >= 1)
+       {
+         if (!strcmp (md->packages.ptr[0]->toChars (), "core")
+             || !strcmp (md->packages.ptr[0]->toChars (), "std")
+             || !strcmp (md->packages.ptr[0]->toChars (), "gcc")
+             || !strcmp (md->packages.ptr[0]->toChars (), "etc"))
+           return false;
+       }
+      else if (!strcmp (md->id->toChars (), "object"))
+       return false;
+    }
+  else if (m->ident)
+    {
+      if (!strcmp (m->ident->toChars (), "object"))
+       return false;
+    }
+
+  /* This import will be compiled.  */
+  if (global.params.v.verbose)
+    message ("compileimport (%s)", m->srcfile.toChars ());
+
+  compiledImports.push (m);
+  return true;
 }
index 0dab76bbfbd5b3d9a15e738c72469744f66ea988..ec2ea59938c48a351241b1953c9a6416809d0333 100644 (file)
@@ -523,6 +523,10 @@ d_handle_option (size_t scode, const char *arg, HOST_WIDE_INT value,
       global.params.ignoreUnsupportedPragmas = value;
       break;
 
+    case OPT_finclude_imports:
+      includeImports = true;
+      break;
+
     case OPT_finvariants:
       global.params.useInvariants = value ? CHECKENABLEon : CHECKENABLEoff;
       break;
@@ -1309,6 +1313,21 @@ d_parse_file (void)
       dmd::semantic3 (m, NULL);
     }
 
+  if (includeImports)
+    {
+      for (size_t i = 0; i < compiledImports.length; i++)
+       {
+         Module *m = compiledImports[i];
+         gcc_assert (m->isRoot ());
+
+         if (global.params.v.verbose)
+           message ("semantic3 %s", m->toChars ());
+
+         dmd::semantic3 (m, NULL);
+         modules.push (m);
+       }
+    }
+
   Module::runDeferredSemantic3 ();
 
   /* Check again, incase semantic3 pass loaded any more modules.  */
index 2cb0c4a62676a7535fc42ddbd4546fdb74f19ccb..3a8bea01050fa8167bde4ce612da73eb888b3a30 100644 (file)
@@ -277,6 +277,12 @@ Sets @code{__traits(getTargetInfo, "cppStd")} to @code{202002}.
 Sets @code{__traits(getTargetInfo, "cppStd")} to @code{202302}.
 @end table
 
+@opindex finclude-imports
+@item -finclude-imports
+Include imported modules in the compilation, as if they were given on the
+command line.  When this option is enabled, all imported modules are compiled
+except those that are part of libphobos.
+
 @opindex finvariants
 @opindex fno-invariants
 @item -fno-invariants
index 50c6f2f4da4d9c8b81cb421ee4a526f0f53b74a5..298ff5843ef270461a31e9d743caf2707dc28259 100644 (file)
@@ -327,6 +327,10 @@ fignore-unknown-pragmas
 D
 Ignore unsupported pragmas.
 
+finclude-imports
+D RejectNegative
+Include imported modules in the compilation.
+
 finvariants
 D Var(flag_invariants)
 Generate code for class invariant contracts.
index fa311d408a7201703d171a602d09bd12dd991f31..b4886bf18ad4088d9e35fb970663c99e25f30e31 100644 (file)
@@ -155,6 +155,9 @@ LangUrlSuffix_D(gdc/Runtime-Options.html#index-fextern-std)
 fignore-unknown-pragmas
 LangUrlSuffix_D(gdc/Warnings.html#index-fignore-unknown-pragmas)
 
+finclude-imports
+LangUrlSuffix_D(gdc/Runtime-Options.html#index-finclude-imports)
+
 finvariants
 LangUrlSuffix_D(gdc/Runtime-Options.html#index-finvariants)
 
diff --git a/gcc/testsuite/gdc.dg/torture/imports/pr109023.d b/gcc/testsuite/gdc.dg/torture/imports/pr109023.d
new file mode 100644 (file)
index 0000000..e85e0ed
--- /dev/null
@@ -0,0 +1,3 @@
+module imports.pr109023;
+
+void f109023() { }
diff --git a/gcc/testsuite/gdc.dg/torture/pr109023.d b/gcc/testsuite/gdc.dg/torture/pr109023.d
new file mode 100644 (file)
index 0000000..3060446
--- /dev/null
@@ -0,0 +1,6 @@
+// { dg-do "compile" }
+// { dg-additional-options "-I[srcdir] -finclude-imports" }
+// { dg-additional-files "imports/pr109023.d" }
+// { dg-final { scan-assembler "_D7imports8pr1090237f109023FZv" } }
+module pr109023;
+import imports.pr109023;