Skip to content

Commit

Permalink
frontend: propagate error as null instead of exit(1)
Browse files Browse the repository at this point in the history
This patch propagates null when Module AST node can't be constructed correctly
instead of doing fatal(), which terminates the program. This is useful if users
use DMD frontend as a library.

Signed-off-by: Luís Ferreira <contact@lsferreira.net>
  • Loading branch information
ljmf00 committed Feb 8, 2022
1 parent cbd0b47 commit 7e016e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/dmd/dmodule.d
Expand Up @@ -524,7 +524,7 @@ extern (C++) final class Module : Package
buf.printf("%s\t(%s)", ident.toChars(), m.srcfile.toChars());
message("import %s", buf.peekChars());
}
m = m.parse();
if((m = m.parse()) is null) return null;

// Call onImport here because if the module is going to be compiled then we
// need to determine it early because it affects semantic analysis. This is
Expand Down Expand Up @@ -727,7 +727,7 @@ extern (C++) final class Module : Package
if (buf.length & 3)
{
error("odd length of UTF-32 char source %llu", cast(ulong) buf.length);
fatal();
return null;
}

const (uint)[] eBuf = cast(const(uint)[])buf;
Expand All @@ -743,7 +743,7 @@ extern (C++) final class Module : Package
if (u > 0x10FFFF)
{
error("UTF-32 value %08x greater than 0x10FFFF", u);
fatal();
return null;
}
dbuf.writeUTF8(u);
}
Expand Down Expand Up @@ -773,7 +773,7 @@ extern (C++) final class Module : Package
if (buf.length & 1)
{
error("odd length of UTF-16 char source %llu", cast(ulong) buf.length);
fatal();
return null;
}

const (ushort)[] eBuf = cast(const(ushort)[])buf;
Expand All @@ -793,26 +793,26 @@ extern (C++) final class Module : Package
if (i >= eBuf.length)
{
error("surrogate UTF-16 high value %04x at end of file", u);
fatal();
return null;
}
const u2 = readNext(&eBuf[i]);
if (u2 < 0xDC00 || 0xE000 <= u2)
{
error("surrogate UTF-16 low value %04x out of range", u2);
fatal();
return null;
}
u = (u - 0xD7C0) << 10;
u |= (u2 - 0xDC00);
}
else if (u >= 0xDC00 && u <= 0xDFFF)
{
error("unpaired surrogate UTF-16 value %04x", u);
fatal();
return null;
}
else if (u == 0xFFFE || u == 0xFFFF)
{
error("illegal UTF-16 value %04x", u);
fatal();
return null;
}
dbuf.writeUTF8(u);
}
Expand Down Expand Up @@ -899,7 +899,7 @@ extern (C++) final class Module : Package
if (buf[0] >= 0x80)
{
error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]);
fatal();
return null;
}
}
}
Expand Down Expand Up @@ -929,6 +929,8 @@ extern (C++) final class Module : Package
? UTF32ToUTF8!(Endian.little)(buf)
: UTF32ToUTF8!(Endian.big)(buf);
}
// an error happened on UTF conversion
if (buf is null) return null;
}

/* If it starts with the string "Ddoc", then it's a documentation
Expand Down
2 changes: 1 addition & 1 deletion src/dmd/frontend.d
Expand Up @@ -407,7 +407,7 @@ Tuple!(Module, "module_", Diagnostics, "diagnostics") parseModule(AST = ASTCodeg
}
}

m.parseModule!AST();
m = m.parseModule!AST();

Diagnostics diagnostics = {
errors: global.errors,
Expand Down

0 comments on commit 7e016e7

Please sign in to comment.