Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/dmd/astbase.d
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,7 @@ struct ASTBase

extern (D) this(Loc loc, Loc endloc, StorageClass stc, char* codedoc)
{
OutBuffer buf;
buf.printf("__unittestL%u_", loc.linnum);
super(loc, endloc, Identifier.generateId(buf.peekString()), stc, null);
super(loc, endloc, Identifier.generateIdWithLoc("__unittest", loc), stc, null);
this.codedoc = codedoc;
}

Expand Down Expand Up @@ -914,11 +912,11 @@ struct ASTBase
{
final extern (D) this(Loc loc, Loc endloc, StorageClass stc)
{
super(loc, endloc, Identifier.generateId("_staticCtor"), STC.static_ | stc, null);
super(loc, endloc, Identifier.generateIdWithLoc("_staticCtor", loc), STC.static_ | stc, null);
}
final extern (D) this(Loc loc, Loc endloc, const(char)* name, StorageClass stc)
final extern (D) this(Loc loc, Loc endloc, string name, StorageClass stc)
{
super(loc, endloc, Identifier.generateId(name), STC.static_ | stc, null);
super(loc, endloc, Identifier.generateIdWithLoc(name, loc), STC.static_ | stc, null);
}

override void accept(Visitor v)
Expand All @@ -931,11 +929,11 @@ struct ASTBase
{
final extern (D) this()(Loc loc, Loc endloc, StorageClass stc)
{
super(loc, endloc, Identifier.generateId("__staticDtor"), STC.static_ | stc, null);
super(loc, endloc, Identifier.generateIdWithLoc("__staticDtor", loc), STC.static_ | stc, null);
}
final extern (D) this(Loc loc, Loc endloc, const(char)* name, StorageClass stc)
final extern (D) this(Loc loc, Loc endloc, string name, StorageClass stc)
{
super(loc, endloc, Identifier.generateId(name), STC.static_ | stc, null);
super(loc, endloc, Identifier.generateIdWithLoc(name, loc), STC.static_ | stc, null);
}

override void accept(Visitor v)
Expand Down
25 changes: 7 additions & 18 deletions src/dmd/func.d
Original file line number Diff line number Diff line change
Expand Up @@ -3406,12 +3406,12 @@ extern (C++) class StaticCtorDeclaration : FuncDeclaration
{
extern (D) this(const ref Loc loc, const ref Loc endloc, StorageClass stc)
{
super(loc, endloc, Identifier.generateId("_staticCtor"), STC.static_ | stc, null);
super(loc, endloc, Identifier.generateIdWithLoc("_staticCtor", loc), STC.static_ | stc, null);
}

extern (D) this(const ref Loc loc, const ref Loc endloc, const(char)* name, StorageClass stc)
extern (D) this(const ref Loc loc, const ref Loc endloc, string name, StorageClass stc)
{
super(loc, endloc, Identifier.generateId(name), STC.static_ | stc, null);
super(loc, endloc, Identifier.generateIdWithLoc(name, loc), STC.static_ | stc, null);
}

override Dsymbol syntaxCopy(Dsymbol s)
Expand Down Expand Up @@ -3492,12 +3492,12 @@ extern (C++) class StaticDtorDeclaration : FuncDeclaration

extern (D) this(const ref Loc loc, const ref Loc endloc, StorageClass stc)
{
super(loc, endloc, Identifier.generateId("_staticDtor"), STC.static_ | stc, null);
super(loc, endloc, Identifier.generateIdWithLoc("_staticDtor", loc), STC.static_ | stc, null);
}

extern (D) this(const ref Loc loc, const ref Loc endloc, const(char)* name, StorageClass stc)
extern (D) this(const ref Loc loc, const ref Loc endloc, string name, StorageClass stc)
{
super(loc, endloc, Identifier.generateId(name), STC.static_ | stc, null);
super(loc, endloc, Identifier.generateIdWithLoc(name, loc), STC.static_ | stc, null);
}

override Dsymbol syntaxCopy(Dsymbol s)
Expand Down Expand Up @@ -3625,7 +3625,7 @@ extern (C++) final class UnitTestDeclaration : FuncDeclaration

extern (D) this(const ref Loc loc, const ref Loc endloc, StorageClass stc, char* codedoc)
{
super(loc, endloc, createIdentifier(loc), stc, null);
super(loc, endloc, Identifier.generateIdWithLoc("__unittest", loc), stc, null);
this.codedoc = codedoc;
}

Expand All @@ -3636,17 +3636,6 @@ extern (C++) final class UnitTestDeclaration : FuncDeclaration
return FuncDeclaration.syntaxCopy(utd);
}

/***********************************************************
* Generate unique unittest function Id so we can have multiple
* instances per module.
*/
private static Identifier createIdentifier(const ref Loc loc)
{
OutBuffer buf;
buf.printf("__unittest_L%u_C%u", loc.linnum, loc.charnum);
return Identifier.idPool(buf.peekSlice());
}

override inout(AggregateDeclaration) isThis() inout
{
return null;
Expand Down
34 changes: 34 additions & 0 deletions src/dmd/identifier.d
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,40 @@ nothrow:
return idPool(buf.peekSlice());
}

/***************************************
* Generate deterministic named identifier based on a source location,
* such that the name is consistent across multiple compilations.
* A new unique name is generated. If the prefix+location is already in
* the stringtable, an extra suffix is added (starting the count at "_1").
*
* Params:
* prefix = first part of the identifier name.
* loc = source location to use in the identifier name.
* Returns:
* Identifier (inside Identifier.idPool) with deterministic name based
* on the source location.
*/
extern (D) static Identifier generateIdWithLoc(string prefix, const ref Loc loc)
{
OutBuffer buf;
buf.writestring(prefix);
buf.writestring("_L");
buf.print(loc.linnum);
buf.writestring("_C");
buf.print(loc.charnum);
auto basesize = buf.peekSlice().length;
uint counter = 1;
while (stringtable.lookup(buf.peekSlice().ptr, buf.peekSlice().length) !is null)
{
// Strip the extra suffix
buf.setsize(basesize);
// Add new suffix with increased counter
buf.writestring("_");
buf.print(counter++);
}
return idPool(buf.peekSlice());
}

/********************************************
* Create an identifier in the string table.
*/
Expand Down
4 changes: 2 additions & 2 deletions test/compilable/extra-files/json.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"endline": 8,
"kind": "function",
"line": 8,
"name": "_staticCtor1",
"name": "_staticCtor_L8_C1",
"storageClass": [
"static"
]
Expand All @@ -22,7 +22,7 @@
"endline": 10,
"kind": "function",
"line": 10,
"name": "_staticDtor2",
"name": "_staticDtor_L10_C1",
"storageClass": [
"static"
]
Expand Down
3 changes: 3 additions & 0 deletions test/runnable/extra-files/test18868_a.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
shared static this() { }
import imports.test18868_fls;
alias floop = FLS!(int);
3 changes: 3 additions & 0 deletions test/runnable/extra-files/test18868_b.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import imports.test18868_fls;
alias floop = FLS!(int);
void main() {}
33 changes: 33 additions & 0 deletions test/runnable/imports/test18868_fls.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module imports.test18868_fls;

template FLS(T)
{
int ctorcount = 0;
int dtorcount = 0;
int sharedctorcount = 0;
int shareddtorcount = 0;

static this()
{
assert(ctorcount == 0);
ctorcount += 1;
}

static ~this()
{
assert(dtorcount == 0);
dtorcount += 1;
}

shared static this()
{
assert(sharedctorcount == 0);
sharedctorcount += 1;
}

shared static ~this()
{
assert(shareddtorcount == 0);
shareddtorcount += 1;
}
}
13 changes: 13 additions & 0 deletions test/runnable/test18868.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -e

$DMD -m${MODEL} -I${TEST_DIR} -od${RESULTS_TEST_DIR} -c ${EXTRA_FILES}/test18868_a.d

$DMD -m${MODEL} -I${TEST_DIR} -od${RESULTS_TEST_DIR} -c ${EXTRA_FILES}/test18868_b.d

$DMD -m${MODEL} -of${OUTPUT_BASE}${EXE} ${TEST_DIR}/imports/test18868_fls.d ${OUTPUT_BASE}_a${OBJ} ${OUTPUT_BASE}_b${OBJ}

${OUTPUT_BASE}${EXE}

rm ${OUTPUT_BASE}{${OBJ},_a${OBJ},_b${OBJ},${EXE}}
13 changes: 13 additions & 0 deletions test/runnable/test18868_2.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mixin(genCtor("666")); mixin(genCtor("777"));

int i;

string genCtor(string a)
{
return "static this() { i += " ~ a ~ "; }";
}

void main()
{
assert(i == 0 + 666 + 777);
}
16 changes: 16 additions & 0 deletions test/runnable/test18868_3.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
static foreach(s; ["666", "777", "888"])
{
mixin(genCtor(s));
}

int i;

string genCtor(string a)
{
return "static this() { i += " ~ a ~ "; }";
}

void main()
{
assert(i == 0 + 666 + 777 + 888);
}
20 changes: 20 additions & 0 deletions test/runnable/test18880.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* REQUIRED_ARGS: -unittest
PERMUTE_ARGS:
*/

static foreach(s; ["666", "777", "888"])
{
mixin(genTest(s));
}

int i;

string genTest(string a)
{
return "unittest { i += " ~ a ~ "; }";
}

void main()
{
assert(i == 0 + 666 + 777 + 888);
}