https://gcc.gnu.org/g:9a7b6668f8f79be8fa73982b8b0bde33c1d8c61f

commit r15-9403-g9a7b6668f8f79be8fa73982b8b0bde33c1d8c61f
Author: Iain Buclaw <ibuc...@gdcproject.org>
Date:   Sat Apr 12 11:13:50 2025 +0200

    d: Add option to include imported modules in the compilation [PR109023]
    
    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.

Diff:
---
 gcc/d/d-compiler.cc                             | 37 +++++++++++++++++++++++--
 gcc/d/d-lang.cc                                 | 19 +++++++++++++
 gcc/d/gdc.texi                                  |  6 ++++
 gcc/d/lang.opt                                  |  4 +++
 gcc/d/lang.opt.urls                             |  3 ++
 gcc/testsuite/gdc.dg/torture/imports/pr109023.d |  3 ++
 gcc/testsuite/gdc.dg/torture/pr109023.d         |  6 ++++
 7 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/gcc/d/d-compiler.cc b/gcc/d/d-compiler.cc
index 160539d08ae0..e18f5d33e6aa 100644
--- a/gcc/d/d-compiler.cc
+++ b/gcc/d/d-compiler.cc
@@ -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;
 }
diff --git a/gcc/d/d-lang.cc b/gcc/d/d-lang.cc
index 0dab76bbfbd5..ec2ea59938c4 100644
--- a/gcc/d/d-lang.cc
+++ b/gcc/d/d-lang.cc
@@ -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.  */
diff --git a/gcc/d/gdc.texi b/gcc/d/gdc.texi
index 2cb0c4a62676..3a8bea01050f 100644
--- a/gcc/d/gdc.texi
+++ b/gcc/d/gdc.texi
@@ -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
diff --git a/gcc/d/lang.opt b/gcc/d/lang.opt
index 50c6f2f4da4d..298ff5843ef2 100644
--- a/gcc/d/lang.opt
+++ b/gcc/d/lang.opt
@@ -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.
diff --git a/gcc/d/lang.opt.urls b/gcc/d/lang.opt.urls
index fa311d408a72..b4886bf18ad4 100644
--- a/gcc/d/lang.opt.urls
+++ b/gcc/d/lang.opt.urls
@@ -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
index 000000000000..e85e0ed961e8
--- /dev/null
+++ b/gcc/testsuite/gdc.dg/torture/imports/pr109023.d
@@ -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
index 000000000000..30604462c78b
--- /dev/null
+++ b/gcc/testsuite/gdc.dg/torture/pr109023.d
@@ -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;

Reply via email to