Skip to content

Commit 7cf333e

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

File tree

12 files changed

+33
-12
lines changed

12 files changed

+33
-12
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: 2 additions & 1 deletion
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(),

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)