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.
This commit is contained in:
Iain Buclaw 2025-04-12 11:13:50 +02:00
parent 8a03d014ec
commit 9a7b6668f8
7 changed files with 76 additions and 2 deletions

View 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;
}

View 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. */

View 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

View 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.

View 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)

View file

@ -0,0 +1,3 @@
module imports.pr109023;
void f109023() { }

View file

@ -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;