-
-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
deinitializeDMD
function to deinitialize the front end
This is a start of being able to restore the global state initialized by `initDMD` to its original state. It only deinitializes state initialized by `initDMD`, state store directly inside functions are not restored. This is useful to invoke the compiler multiple times within the same application. This allows to write more fine grained tests for the compiler, more of a unit test style compared to the current end to end tests.
- Loading branch information
1 parent
6cc806c
commit 16ec85c
Showing
11 changed files
with
296 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
module deinitialization; | ||
|
||
@("global.deinitialize") | ||
unittest | ||
{ | ||
import dmd.globals : global; | ||
|
||
static void assertStructsEqual(T)(const ref T a, const ref T b) @nogc pure nothrow | ||
if (is(T == struct)) | ||
{ | ||
foreach (i, _; typeof(a.tupleof)) | ||
{ | ||
enum name = __traits(identifier, a.tupleof[i]); | ||
|
||
static if (is(typeof(a.tupleof[i]) == const(char*))) | ||
assert(a.tupleof[i].fromStringz == b.tupleof[i].fromStringz, name); | ||
else | ||
assert(a.tupleof[i] == b.tupleof[i], name); | ||
} | ||
} | ||
|
||
immutable init = global.init; | ||
assertStructsEqual(global, init); | ||
|
||
global._init(); | ||
global.deinitialize(); | ||
|
||
assert(global == global.init); | ||
} | ||
|
||
@("Type.deinitialize") | ||
unittest | ||
{ | ||
import dmd.mars : addDefaultVersionIdentifiers, setTarget; | ||
import dmd.mtype : Type; | ||
import dmd.globals : global; | ||
|
||
assert(Type.stringtable == Type.stringtable.init); | ||
|
||
global._init(); | ||
setTarget(global.params); | ||
|
||
Type._init(); | ||
Type.deinitialize(); | ||
|
||
global.deinitialize(); | ||
|
||
assert(Type.stringtable == Type.stringtable.init); | ||
} | ||
|
||
@("Id.deinitialize") | ||
unittest | ||
{ | ||
import dmd.id : Id; | ||
|
||
static void assertInitialState() | ||
{ | ||
foreach (e ; __traits(allMembers, Id)) | ||
{ | ||
static if (!__traits(isStaticFunction, mixin("Id." ~ e))) | ||
assert(__traits(getMember, Id, e) is null); | ||
} | ||
} | ||
|
||
assertInitialState(); | ||
|
||
Id.initialize(); | ||
Id.deinitialize(); | ||
|
||
assertInitialState(); | ||
} | ||
|
||
@("Module.deinitialize") | ||
unittest | ||
{ | ||
import dmd.dmodule : Module; | ||
|
||
assert(Module.modules is Module.modules.init); | ||
|
||
Module._init(); | ||
Module.deinitialize(); | ||
|
||
assert(Module.modules is Module.modules.init); | ||
} | ||
|
||
@("Target.deinitialize") | ||
unittest | ||
{ | ||
import dmd.globals : Param; | ||
import dmd.mars : setTarget; | ||
import dmd.target : target, Target; | ||
|
||
static bool isFPTypeProperties(T)() | ||
{ | ||
return is(T == const(typeof(Target.FloatProperties))) || | ||
is (T == const(typeof(Target.DoubleProperties))) || | ||
is (T == const(typeof(Target.RealProperties))); | ||
} | ||
|
||
static void assertStructsEqual(T)(const ref T a, const ref T b) @nogc pure nothrow | ||
if (is(T == struct)) | ||
{ | ||
foreach (i, _; typeof(a.tupleof)) | ||
{ | ||
alias Type = typeof(a.tupleof[i]); | ||
enum name = __traits(identifier, a.tupleof[i]); | ||
|
||
static if (!isFPTypeProperties!Type) | ||
assert(a.tupleof[i] == b.tupleof[i], name); | ||
} | ||
} | ||
|
||
const init = target.init; | ||
assertStructsEqual(target, init); | ||
|
||
Param params; | ||
setTarget(params); | ||
|
||
target._init(params); | ||
target.deinitialize(); | ||
|
||
assertStructsEqual(target, init); | ||
} | ||
|
||
@("Expression.deinitialize") | ||
unittest | ||
{ | ||
import dmd.ctfeexpr : CTFEExp; | ||
import dmd.expression : Expression; | ||
|
||
static void assertInitialState() | ||
{ | ||
assert(CTFEExp.cantexp is null); | ||
assert(CTFEExp.voidexp is null); | ||
assert(CTFEExp.breakexp is null); | ||
assert(CTFEExp.continueexp is null); | ||
assert(CTFEExp.gotoexp is null); | ||
assert(CTFEExp.showcontext is null); | ||
} | ||
|
||
assertInitialState(); | ||
|
||
Expression._init(); | ||
Expression.deinitialize(); | ||
|
||
assertInitialState(); | ||
} | ||
|
||
@("Objc.deinitialize") | ||
unittest | ||
{ | ||
import dmd.objc : Objc, objc; | ||
|
||
assert(objc is null); | ||
|
||
Objc._init(); | ||
Objc.deinitialize(); | ||
|
||
assert(objc is null); | ||
} | ||
|
||
private inout(char)[] fromStringz(inout(char)* cString) @nogc @system pure nothrow | ||
{ | ||
import core.stdc.string : strlen; | ||
return cString ? cString[0 .. strlen(cString)] : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters