mirror of
https://github.com/dlang/dmd.git
synced 2025-04-26 13:10:12 +03:00
ImportC: improve error message for size_t (#21187)
Resolves: https://github.com/dlang/dmd/issues/20414 Add some hints for common missing includes.
This commit is contained in:
parent
ca2f90d1fc
commit
74cdfed9d6
5 changed files with 83 additions and 21 deletions
|
@ -4116,16 +4116,28 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
|
|||
|
||||
/* Look for what user might have meant
|
||||
*/
|
||||
if (const n = importHint(exp.ident.toString()))
|
||||
error(exp.loc, "`%s` is not defined, perhaps `import %.*s;` is needed?", exp.ident.toChars(), cast(int)n.length, n.ptr);
|
||||
else if (auto s2 = sc.search_correct(exp.ident))
|
||||
error(exp.loc, "undefined identifier `%s`, did you mean %s `%s`?", exp.ident.toChars(), s2.kind(), s2.toChars());
|
||||
else if (const p = Scope.search_correct_C(exp.ident))
|
||||
error(exp.loc, "undefined identifier `%s`, did you mean `%s`?", exp.ident.toChars(), p);
|
||||
else if (exp.ident == Id.dollar)
|
||||
error(exp.loc, "undefined identifier `$`");
|
||||
if (!(sc && sc.inCfile))
|
||||
{
|
||||
if (const n = importHint(exp.ident.toString()))
|
||||
error(exp.loc, "`%s` is not defined, perhaps `import %.*s;` is needed?", exp.ident.toChars(), cast(int)n.length, n.ptr);
|
||||
else if (auto s2 = sc.search_correct(exp.ident))
|
||||
error(exp.loc, "undefined identifier `%s`, did you mean %s `%s`?", exp.ident.toChars(), s2.kind(), s2.toChars());
|
||||
else if (const p = Scope.search_correct_C(exp.ident))
|
||||
error(exp.loc, "undefined identifier `%s`, did you mean `%s`?", exp.ident.toChars(), p);
|
||||
else if (exp.ident == Id.dollar)
|
||||
error(exp.loc, "undefined identifier `$`");
|
||||
else
|
||||
error(exp.loc, "undefined identifier `%s`", exp.ident.toChars());
|
||||
}
|
||||
else
|
||||
error(exp.loc, "undefined identifier `%s`", exp.ident.toChars());
|
||||
{
|
||||
if (const n = cIncludeHint(exp.ident.toString()))
|
||||
error(exp.loc, "`%s` is not defined, perhaps `#include %.*s` is needed?", exp.ident.toChars(), cast(int)n.length, n.ptr);
|
||||
else if (auto s2 = sc.search_correct(exp.ident))
|
||||
error(exp.loc, "undefined identifier `%s`, did you mean %s `%s`?", exp.ident.toChars(), s2.kind(), s2.toChars());
|
||||
else
|
||||
error(exp.loc, "undefined identifier `%s`", exp.ident.toChars());
|
||||
}
|
||||
|
||||
result = ErrorExp.get();
|
||||
}
|
||||
|
|
|
@ -26,8 +26,15 @@ const(char)[] importHint(const(char)[] s) @safe
|
|||
return *entry;
|
||||
return null;
|
||||
}
|
||||
const(char)[] cIncludeHint(const(char)[] s) @safe
|
||||
{
|
||||
if (auto entry = s in cHints)
|
||||
return *entry;
|
||||
return null;
|
||||
}
|
||||
|
||||
private immutable string[string] hints;
|
||||
private immutable string[string] cHints;
|
||||
|
||||
shared static this()
|
||||
{
|
||||
|
@ -83,6 +90,23 @@ shared static this()
|
|||
"InterpolationHeader": "core.interpolation",
|
||||
"InterpolationFooter": "core.interpolation",
|
||||
];
|
||||
cHints = [
|
||||
"NULL": "<stddef.h>",
|
||||
"calloc": "<stdlib.h>",
|
||||
"fopen": "<stdio.h>",
|
||||
"fprintf": "<stdio.h>",
|
||||
"free": "<stdlib.h>",
|
||||
"malloc": "<stdlib.h>",
|
||||
"memcpy": "<string.h>",
|
||||
"memmove": "<string.h>",
|
||||
"memset": "<string.h>",
|
||||
"printf": "<stdio.h>",
|
||||
"ptrdiff_t": "<stddef.h>",
|
||||
"size_t": "<stddef.h>",
|
||||
"stderr": "<stdio.h>",
|
||||
"stdin": "<stdio.h>",
|
||||
"stdout": "<stdio.h>",
|
||||
];
|
||||
}
|
||||
|
||||
unittest
|
||||
|
|
|
@ -169,17 +169,28 @@ private void resolveHelper(TypeQualified mt, Loc loc, Scope* sc, Dsymbol s, Dsym
|
|||
*/
|
||||
const p = mt.mutableOf().unSharedOf().toChars();
|
||||
auto id = Identifier.idPool(p[0 .. strlen(p)]);
|
||||
if (const n = importHint(id.toString()))
|
||||
error(loc, "`%s` is not defined, perhaps `import %.*s;` ?", p, cast(int)n.length, n.ptr);
|
||||
else if (auto s2 = sc.search_correct(id))
|
||||
error(loc, "undefined identifier `%s`, did you mean %s `%s`?", p, s2.kind(), s2.toChars());
|
||||
else if (const q = Scope.search_correct_C(id))
|
||||
error(loc, "undefined identifier `%s`, did you mean `%s`?", p, q);
|
||||
else if ((id == Id.This && sc.getStructClassScope()) ||
|
||||
(id == Id._super && sc.getClassScope()))
|
||||
error(loc, "undefined identifier `%s`, did you mean `typeof(%s)`?", p, p);
|
||||
else
|
||||
error(loc, "undefined identifier `%s`", p);
|
||||
if (!(sc && sc.inCfile))
|
||||
{
|
||||
if (const n = importHint(id.toString()))
|
||||
error(loc, "`%s` is not defined, perhaps `import %.*s;` ?", p, cast(int)n.length, n.ptr);
|
||||
else if (auto s2 = sc.search_correct(id))
|
||||
error(loc, "undefined identifier `%s`, did you mean %s `%s`?", p, s2.kind(), s2.toChars());
|
||||
else if (const q = Scope.search_correct_C(id))
|
||||
error(loc, "undefined identifier `%s`, did you mean `%s`?", p, q);
|
||||
else if ((id == Id.This && sc.getStructClassScope()) ||
|
||||
(id == Id._super && sc.getClassScope()))
|
||||
error(loc, "undefined identifier `%s`, did you mean `typeof(%s)`?", p, p);
|
||||
else
|
||||
error(loc, "undefined identifier `%s`", p);
|
||||
}
|
||||
else {
|
||||
if (const n = cIncludeHint(id.toString()))
|
||||
error(loc, "`%s` is not defined, perhaps `#include %.*s` ?", p, cast(int)n.length, n.ptr);
|
||||
else if (auto s2 = sc.search_correct(id))
|
||||
error(loc, "undefined identifier `%s`, did you mean %s `%s`?", p, s2.kind(), s2.toChars());
|
||||
else
|
||||
error(loc, "undefined identifier `%s`", p);
|
||||
}
|
||||
|
||||
pt = Type.terror;
|
||||
return;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue