Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make deinitializeDMD reset onlyOneMain check #17080

Merged
merged 1 commit into from
Dec 4, 2024
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
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 @@ -3668,6 +3668,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 @@ -7406,6 +7407,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 @@
}
}

/****************************************
* 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure where in the headers this should go, or for that matter where in DMD. This code is duplicated by LDC and GDC, hence the extern(C++).

{
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;

Check warning on line 173 in compiler/src/dmd/funcsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/funcsem.d#L168-L173

Added lines #L168 - L173 were not covered by tests
}
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
Loading