Skip to content

Commit 6b48a40

Browse files
committed
Add new flag -unittest-roots
1 parent 6d6d83a commit 6b48a40

File tree

12 files changed

+36
-13
lines changed

12 files changed

+36
-13
lines changed

compiler/src/dmd/cli.d

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,11 @@ dmd -cov -unittest myprog.d
913913
`Compile in $(LINK2 spec/unittest.html, unittest) code, turns on asserts, and sets the
914914
$(D unittest) $(LINK2 spec/version.html#PredefinedVersions, version identifier)`,
915915
),
916+
Option("unittest-roots",
917+
"compile in unit tests for root modules only",
918+
`Compile in $(LINK2 spec/unittest.html, unittest) code, turns on asserts, and sets the
919+
$(D unittest) $(LINK2 spec/version.html#PredefinedVersions, version identifier) only for root modules whose sources files are explicitly passed as arguments to compiler`,
920+
),
916921
Option("v",
917922
"verbose",
918923
`Enable verbose output for each compiler pass`,

compiler/src/dmd/dmodule.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ extern (C++) final class Module : Package
767767
}
768768
else
769769
{
770-
const bool doUnittests = global.params.parsingUnittestsRequired();
770+
const bool doUnittests = global.params.parsingUnittestsRequired(this.isRoot);
771771
scope p = new Parser!AST(this, buf, cast(bool) docfile, global.errorSink, &global.compileEnv, doUnittests);
772772
p.nextToken();
773773
p.parseModuleDeclaration();

compiler/src/dmd/dsymbolsem.d

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3764,8 +3764,9 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
37643764
const len = buf.length;
37653765
buf.writeByte(0);
37663766
const str = buf.extractSlice()[0 .. len];
3767-
const bool doUnittests = global.params.parsingUnittestsRequired();
3768-
scope p = new Parser!ASTCodegen(sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
3767+
auto mod = sc._module;
3768+
const bool doUnittests = global.params.parsingUnittestsRequired(mod.isRoot);
3769+
scope p = new Parser!ASTCodegen(mod, str, false, global.errorSink, &global.compileEnv, doUnittests);
37693770
adjustLocForMixin(str, cd.loc, *p.baseLoc, global.params.mixinOut);
37703771
p.linnum = p.baseLoc.startLine;
37713772
p.nextToken();
@@ -4719,7 +4720,14 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
47194720
return;
47204721
}
47214722

4722-
if (global.params.useUnitTests)
4723+
auto use = global.params.useUnitTests;
4724+
if (use && global.params.useUnitTestsRootOnly)
4725+
{
4726+
auto m = sc._module;
4727+
if (m && !m.isRoot())
4728+
use = false;
4729+
}
4730+
if (use)
47234731
{
47244732
if (!utd.type)
47254733
utd.type = new TypeFunction(ParameterList(), Type.tvoid, LINK.d, utd.storage_class);

compiler/src/dmd/expressionsem.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9345,7 +9345,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
93459345
const errors = global.errors;
93469346
const len = buf.length;
93479347
const str = buf.extractChars()[0 .. len];
9348-
const bool doUnittests = global.params.parsingUnittestsRequired();
9348+
const bool doUnittests = global.params.parsingUnittestsRequired(sc._module.isRoot);
93499349
scope p = new Parser!ASTCodegen(sc._module, str, false, global.errorSink, &global.compileEnv, doUnittests);
93509350
adjustLocForMixin(str, exp.loc, *p.baseLoc, global.params.mixinOut);
93519351
p.linnum = p.baseLoc.startLine;

compiler/src/dmd/frontend.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8167,6 +8167,7 @@ struct Param final
81678167
bool vcg_ast;
81688168
DiagnosticReporting useDeprecated;
81698169
bool useUnitTests;
8170+
bool useUnitTestsRootOnly;
81708171
bool useInline;
81718172
bool release;
81728173
bool preservePaths;
@@ -8250,7 +8251,7 @@ struct Param final
82508251
bool timeTrace;
82518252
uint32_t timeTraceGranularityUs;
82528253
const char* timeTraceFile;
8253-
bool parsingUnittestsRequired();
8254+
bool parsingUnittestsRequired(bool isRoot);
82548255
Param() :
82558256
obj(true),
82568257
readStdin(),
@@ -8260,6 +8261,7 @@ struct Param final
82608261
vcg_ast(),
82618262
useDeprecated((DiagnosticReporting)1u),
82628263
useUnitTests(),
8264+
useUnitTestsRootOnly(),
82638265
useInline(false),
82648266
release(),
82658267
preservePaths(),
@@ -8335,7 +8337,7 @@ struct Param final
83358337
timeTraceFile()
83368338
{
83378339
}
8338-
Param(bool obj, bool readStdin = false, bool multiobj = false, bool trace = false, bool tracegc = false, bool vcg_ast = false, DiagnosticReporting useDeprecated = (DiagnosticReporting)1u, bool useUnitTests = false, bool useInline = false, bool release = false, bool preservePaths = false, DiagnosticReporting useWarnings = (DiagnosticReporting)2u, bool cov = false, uint8_t covPercent = 0u, bool ctfe_cov = false, bool ignoreUnsupportedPragmas = true, bool useModuleInfo = true, bool useTypeInfo = true, bool useExceptions = true, bool useGC = true, bool betterC = false, bool addMain = false, bool allInst = false, bool bitfields = true, bool rewriteNoExceptionToSeq = false, CppStdRevision cplusplus = (CppStdRevision)201103u, Help help = Help(), Verbose v = Verbose(), Edition edition = (Edition)2023u, void* editionFiles = nullptr, FeatureState useDIP25 = (FeatureState)2u, FeatureState useDIP1000 = (FeatureState)0u, bool ehnogc = false, bool useDIP1021 = false, FeatureState fieldwise = (FeatureState)0u, bool fixAliasThis = false, FeatureState rvalueRefParam = (FeatureState)0u, FeatureState safer = (FeatureState)0u, FeatureState noSharedAccess = (FeatureState)0u, bool previewIn = false, bool inclusiveInContracts = false, bool shortenedMethods = true, bool fixImmutableConv = false, bool fix16997 = true, FeatureState dtorFields = (FeatureState)0u, FeatureState systemVariables = (FeatureState)0u, bool useFastDFA = false, CHECKENABLE useInvariants = (CHECKENABLE)0u, CHECKENABLE useIn = (CHECKENABLE)0u, CHECKENABLE useOut = (CHECKENABLE)0u, CHECKENABLE useArrayBounds = (CHECKENABLE)0u, CHECKENABLE useNullCheck = (CHECKENABLE)0u, CHECKENABLE useAssert = (CHECKENABLE)0u, CHECKENABLE useSwitchError = (CHECKENABLE)0u, CHECKENABLE boundscheck = (CHECKENABLE)0u, CHECKACTION checkAction = (CHECKACTION)0u, CLIIdentifierTable dIdentifierTable = (CLIIdentifierTable)0u, CLIIdentifierTable cIdentifierTable = (CLIIdentifierTable)0u, _d_dynamicArray< const char > argv0 = {}, Array<const char* > modFileAliasStrings = Array<const char* >(), Array<ImportPathInfo > imppath = Array<ImportPathInfo >(), Array<const char* > fileImppath = Array<const char* >(), _d_dynamicArray< const char > objdir = {}, _d_dynamicArray< const char > objname = {}, _d_dynamicArray< const char > libname = {}, Output ddoc = Output(), Output dihdr = Output(), Output cxxhdr = Output(), Output json = Output(), JsonFieldFlags jsonFieldFlags = (JsonFieldFlags)0u, Output makeDeps = Output(), Output mixinOut = Output(), Output moduleDeps = Output(), bool debugEnabled = false, bool run = false, Array<const char* > runargs = Array<const char* >(), Array<const char* > cppswitches = Array<const char* >(), const char* cpp = nullptr, Array<const char* > objfiles = Array<const char* >(), Array<const char* > linkswitches = Array<const char* >(), Array<bool > linkswitchIsForCC = Array<bool >(), Array<const char* > libfiles = Array<const char* >(), Array<const char* > dllfiles = Array<const char* >(), _d_dynamicArray< const char > deffile = {}, _d_dynamicArray< const char > resfile = {}, _d_dynamicArray< const char > exefile = {}, _d_dynamicArray< const char > mapfile = {}, bool fullyQualifiedObjectFiles = false, bool timeTrace = false, uint32_t timeTraceGranularityUs = 500u, const char* timeTraceFile = nullptr) :
8340+
Param(bool obj, bool readStdin = false, bool multiobj = false, bool trace = false, bool tracegc = false, bool vcg_ast = false, DiagnosticReporting useDeprecated = (DiagnosticReporting)1u, bool useUnitTests = false, bool useUnitTestsRootOnly = false, bool useInline = false, bool release = false, bool preservePaths = false, DiagnosticReporting useWarnings = (DiagnosticReporting)2u, bool cov = false, uint8_t covPercent = 0u, bool ctfe_cov = false, bool ignoreUnsupportedPragmas = true, bool useModuleInfo = true, bool useTypeInfo = true, bool useExceptions = true, bool useGC = true, bool betterC = false, bool addMain = false, bool allInst = false, bool bitfields = true, bool rewriteNoExceptionToSeq = false, CppStdRevision cplusplus = (CppStdRevision)201103u, Help help = Help(), Verbose v = Verbose(), Edition edition = (Edition)2023u, void* editionFiles = nullptr, FeatureState useDIP25 = (FeatureState)2u, FeatureState useDIP1000 = (FeatureState)0u, bool ehnogc = false, bool useDIP1021 = false, FeatureState fieldwise = (FeatureState)0u, bool fixAliasThis = false, FeatureState rvalueRefParam = (FeatureState)0u, FeatureState safer = (FeatureState)0u, FeatureState noSharedAccess = (FeatureState)0u, bool previewIn = false, bool inclusiveInContracts = false, bool shortenedMethods = true, bool fixImmutableConv = false, bool fix16997 = true, FeatureState dtorFields = (FeatureState)0u, FeatureState systemVariables = (FeatureState)0u, bool useFastDFA = false, CHECKENABLE useInvariants = (CHECKENABLE)0u, CHECKENABLE useIn = (CHECKENABLE)0u, CHECKENABLE useOut = (CHECKENABLE)0u, CHECKENABLE useArrayBounds = (CHECKENABLE)0u, CHECKENABLE useNullCheck = (CHECKENABLE)0u, CHECKENABLE useAssert = (CHECKENABLE)0u, CHECKENABLE useSwitchError = (CHECKENABLE)0u, CHECKENABLE boundscheck = (CHECKENABLE)0u, CHECKACTION checkAction = (CHECKACTION)0u, CLIIdentifierTable dIdentifierTable = (CLIIdentifierTable)0u, CLIIdentifierTable cIdentifierTable = (CLIIdentifierTable)0u, _d_dynamicArray< const char > argv0 = {}, Array<const char* > modFileAliasStrings = Array<const char* >(), Array<ImportPathInfo > imppath = Array<ImportPathInfo >(), Array<const char* > fileImppath = Array<const char* >(), _d_dynamicArray< const char > objdir = {}, _d_dynamicArray< const char > objname = {}, _d_dynamicArray< const char > libname = {}, Output ddoc = Output(), Output dihdr = Output(), Output cxxhdr = Output(), Output json = Output(), JsonFieldFlags jsonFieldFlags = (JsonFieldFlags)0u, Output makeDeps = Output(), Output mixinOut = Output(), Output moduleDeps = Output(), bool debugEnabled = false, bool run = false, Array<const char* > runargs = Array<const char* >(), Array<const char* > cppswitches = Array<const char* >(), const char* cpp = nullptr, Array<const char* > objfiles = Array<const char* >(), Array<const char* > linkswitches = Array<const char* >(), Array<bool > linkswitchIsForCC = Array<bool >(), Array<const char* > libfiles = Array<const char* >(), Array<const char* > dllfiles = Array<const char* >(), _d_dynamicArray< const char > deffile = {}, _d_dynamicArray< const char > resfile = {}, _d_dynamicArray< const char > exefile = {}, _d_dynamicArray< const char > mapfile = {}, bool fullyQualifiedObjectFiles = false, bool timeTrace = false, uint32_t timeTraceGranularityUs = 500u, const char* timeTraceFile = nullptr) :
83398341
obj(obj),
83408342
readStdin(readStdin),
83418343
multiobj(multiobj),
@@ -8344,6 +8346,7 @@ struct Param final
83448346
vcg_ast(vcg_ast),
83458347
useDeprecated(useDeprecated),
83468348
useUnitTests(useUnitTests),
8349+
useUnitTestsRootOnly(useUnitTestsRootOnly),
83478350
useInline(useInline),
83488351
release(release),
83498352
preservePaths(preservePaths),

compiler/src/dmd/globals.d

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ extern (C++) struct Param
165165
bool vcg_ast; // write-out codegen-ast
166166
DiagnosticReporting useDeprecated = DiagnosticReporting.inform; // how use of deprecated features are handled
167167
bool useUnitTests; // generate unittest code
168+
bool useUnitTestsRootOnly; // generate unittest code for root modules only
168169
bool useInline = false; // inline expand functions
169170
bool release; // build release version
170171
bool preservePaths; // true means don't strip path from source file
@@ -277,9 +278,9 @@ extern (C++) struct Param
277278
const(char)* timeTraceFile; /// File path of output file
278279

279280
///
280-
bool parsingUnittestsRequired() @safe
281+
bool parsingUnittestsRequired(bool isRoot) @safe
281282
{
282-
return useUnitTests || ddoc.doOutput || dihdr.doOutput;
283+
return (useUnitTests && (!useUnitTestsRootOnly || isRoot)) || ddoc.doOutput || dihdr.doOutput;
283284
}
284285
}
285286

compiler/src/dmd/globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct Param
189189
d_bool vcg_ast; // write-out codegen-ast
190190
Diagnostic useDeprecated;
191191
d_bool useUnitTests; // generate unittest code
192+
d_bool useUnitTestsRootOnly; // generate unittest code for root modules only
192193
d_bool useInline; // inline expand functions
193194
d_bool release; // build release version
194195
d_bool preservePaths; // true means don't strip path from source file

compiler/src/dmd/iasm/dmdaarch64.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Statement inlineAsmAArch64Semantic(InlineAsmStatement s, Scope* sc)
7373
}
7474
}
7575

76-
const bool doUnittests = global.params.parsingUnittestsRequired();
76+
const bool doUnittests = global.params.parsingUnittestsRequired(sc._module.isRoot);
7777
scope p = new Parser!ASTCodegen(sc._module, "", false, global.errorSink, &global.compileEnv, doUnittests);
7878

7979
/* Set list of tokens that will be the input to the parser, and starting line number to use.

compiler/src/dmd/iasm/gcc.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import dmd.typesem;
4343
public Statement gccAsmSemantic(GccAsmStatement s, Scope* sc)
4444
{
4545
//printf("GccAsmStatement.semantic()\n");
46-
const bool doUnittests = global.params.parsingUnittestsRequired();
46+
const bool doUnittests = global.params.parsingUnittestsRequired(sc._module.isRoot);
4747
scope p = (sc && sc.inCfile)
4848
? new CParser!ASTCodegen(sc._module, "", false, global.errorSink, target.c, null, &global.compileEnv)
4949
: new Parser!ASTCodegen(sc._module, "", false, global.errorSink, &global.compileEnv, doUnittests);

compiler/src/dmd/mars.d

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,11 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, out Param
16591659
}
16601660
else if (arg == "-unittest")
16611661
params.useUnitTests = true;
1662+
else if (arg == "-unittest-roots")
1663+
{
1664+
params.useUnitTests = true;
1665+
params.useUnitTestsRootOnly = true;
1666+
}
16621667
else if (p[1] == 'I') // https://dlang.org/dmd.html#switch-I
16631668
{
16641669
params.imppath.push(ImportPathInfo(p + 2 + (p[2] == '=')));

0 commit comments

Comments
 (0)