Skip to content

Commit

Permalink
make deinitializeDMD reset onlyOneMain check (#17080)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator authored Dec 4, 2024
1 parent b076573 commit f28b1a0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
2 changes: 2 additions & 0 deletions compiler/src/dmd/frontend.d
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ void deinitializeDMD()
import dmd.dsymbol : Dsymbol;
import dmd.escape : EscapeState;
import dmd.expression : Expression;
import dmd.func : FuncDeclaration;
import dmd.globals : global;
import dmd.id : Id;
import dmd.mtype : Type;
Expand All @@ -184,6 +185,7 @@ void deinitializeDMD()

diagnosticHandler = null;
fatalErrorHandler = null;
FuncDeclaration.lastMain = null;

global.deinitialize();

Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -3663,6 +3663,7 @@ class FuncDeclaration : public Declaration
bool requiresClosure;
Array<VarDeclaration* > closureVars;
Array<VarDeclaration* > outerVars;
static FuncDeclaration* lastMain;
Array<FuncDeclaration* > siblingCallers;
Array<FuncDeclaration* >* inlinedNestedCallees;
AttributeViolation* safetyViolation;
Expand Down Expand Up @@ -7393,6 +7394,8 @@ class NrvoWalker final : public StatementRewriteWalker
void visit(TryFinallyStatement* s) override;
};

extern bool onlyOneMain(FuncDeclaration* fd);

class NOGCVisitor final : public StoppableVisitor
{
public:
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dmd/func.d
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ extern (C++) class FuncDeclaration : Declaration
*/
VarDeclarations outerVars;

// Most recent encountered `main` (`WinMain` or `DllMain`) function.
// Track it to give error messages for multiple entrypoints
__gshared FuncDeclaration lastMain;

/// Sibling nested functions which called this one
FuncDeclarations siblingCallers;

Expand Down
22 changes: 22 additions & 0 deletions compiler/src/dmd/funcsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ public:
}
}

/****************************************
* Only one entry point function is allowed. Print error if more than one.
* Params:
* fd = a "main" function
* Returns:
* true if haven't seen "main" before
*/
extern (C++) bool onlyOneMain(FuncDeclaration fd)
{
if (auto lastMain = FuncDeclaration.lastMain)
{
const format = (target.os == Target.OS.Windows)
? "only one entry point `main`, `WinMain` or `DllMain` is allowed"
: "only one entry point `main` is allowed";
error(fd.loc, format.ptr);
errorSupplemental(lastMain.loc, "previously found `%s` here", lastMain.toFullSignature());
return false;
}
FuncDeclaration.lastMain = fd;
return true;
}

/**********************************
* Main semantic routine for functions.
*/
Expand Down
24 changes: 1 addition & 23 deletions compiler/src/dmd/glue.d
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import dmd.e2ir;
import dmd.errors;
import dmd.expression;
import dmd.func;
import dmd.funcsem : onlyOneMain;
import dmd.globals;
import dmd.identifier;
import dmd.id;
Expand Down Expand Up @@ -1623,29 +1624,6 @@ private bool entryPointFunctions(Obj objmod, FuncDeclaration fd)
return false;
}

/****************************************
* Only one entry point function is allowed. Print error if more than one.
* Params:
* fd = a "main" function
* Returns:
* true if haven't seen "main" before
*/
private bool onlyOneMain(FuncDeclaration fd)
{
__gshared FuncDeclaration lastMain;
if (lastMain)
{
const format = (target.os == Target.OS.Windows)
? "only one entry point `main`, `WinMain` or `DllMain` is allowed"
: "only one entry point `main` is allowed";
error(fd.loc, format.ptr);
errorSupplemental(lastMain.loc, "previously found `%s` here", lastMain.toFullSignature());
return false;
}
lastMain = fd;
return true;
}

/* ================================================================== */

/**************************************
Expand Down

0 comments on commit f28b1a0

Please sign in to comment.