mirror of
https://github.com/dlang/dmd.git
synced 2025-04-28 06:00:13 +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
|
/* Look for what user might have meant
|
||||||
*/
|
*/
|
||||||
if (const n = importHint(exp.ident.toString()))
|
if (!(sc && sc.inCfile))
|
||||||
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))
|
if (const n = importHint(exp.ident.toString()))
|
||||||
error(exp.loc, "undefined identifier `%s`, did you mean %s `%s`?", exp.ident.toChars(), s2.kind(), s2.toChars());
|
error(exp.loc, "`%s` is not defined, perhaps `import %.*s;` is needed?", exp.ident.toChars(), cast(int)n.length, n.ptr);
|
||||||
else if (const p = Scope.search_correct_C(exp.ident))
|
else if (auto s2 = sc.search_correct(exp.ident))
|
||||||
error(exp.loc, "undefined identifier `%s`, did you mean `%s`?", exp.ident.toChars(), p);
|
error(exp.loc, "undefined identifier `%s`, did you mean %s `%s`?", exp.ident.toChars(), s2.kind(), s2.toChars());
|
||||||
else if (exp.ident == Id.dollar)
|
else if (const p = Scope.search_correct_C(exp.ident))
|
||||||
error(exp.loc, "undefined identifier `$`");
|
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
|
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();
|
result = ErrorExp.get();
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,15 @@ const(char)[] importHint(const(char)[] s) @safe
|
||||||
return *entry;
|
return *entry;
|
||||||
return null;
|
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] hints;
|
||||||
|
private immutable string[string] cHints;
|
||||||
|
|
||||||
shared static this()
|
shared static this()
|
||||||
{
|
{
|
||||||
|
@ -83,6 +90,23 @@ shared static this()
|
||||||
"InterpolationHeader": "core.interpolation",
|
"InterpolationHeader": "core.interpolation",
|
||||||
"InterpolationFooter": "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
|
unittest
|
||||||
|
|
|
@ -169,17 +169,28 @@ private void resolveHelper(TypeQualified mt, Loc loc, Scope* sc, Dsymbol s, Dsym
|
||||||
*/
|
*/
|
||||||
const p = mt.mutableOf().unSharedOf().toChars();
|
const p = mt.mutableOf().unSharedOf().toChars();
|
||||||
auto id = Identifier.idPool(p[0 .. strlen(p)]);
|
auto id = Identifier.idPool(p[0 .. strlen(p)]);
|
||||||
if (const n = importHint(id.toString()))
|
if (!(sc && sc.inCfile))
|
||||||
error(loc, "`%s` is not defined, perhaps `import %.*s;` ?", p, cast(int)n.length, n.ptr);
|
{
|
||||||
else if (auto s2 = sc.search_correct(id))
|
if (const n = importHint(id.toString()))
|
||||||
error(loc, "undefined identifier `%s`, did you mean %s `%s`?", p, s2.kind(), s2.toChars());
|
error(loc, "`%s` is not defined, perhaps `import %.*s;` ?", p, cast(int)n.length, n.ptr);
|
||||||
else if (const q = Scope.search_correct_C(id))
|
else if (auto s2 = sc.search_correct(id))
|
||||||
error(loc, "undefined identifier `%s`, did you mean `%s`?", p, q);
|
error(loc, "undefined identifier `%s`, did you mean %s `%s`?", p, s2.kind(), s2.toChars());
|
||||||
else if ((id == Id.This && sc.getStructClassScope()) ||
|
else if (const q = Scope.search_correct_C(id))
|
||||||
(id == Id._super && sc.getClassScope()))
|
error(loc, "undefined identifier `%s`, did you mean `%s`?", p, q);
|
||||||
error(loc, "undefined identifier `%s`, did you mean `typeof(%s)`?", p, p);
|
else if ((id == Id.This && sc.getStructClassScope()) ||
|
||||||
else
|
(id == Id._super && sc.getClassScope()))
|
||||||
error(loc, "undefined identifier `%s`", p);
|
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;
|
pt = Type.terror;
|
||||||
return;
|
return;
|
||||||
|
|
15
compiler/test/fail_compilation/test20414.c
Normal file
15
compiler/test/fail_compilation/test20414.c
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
/* TEST_OUTPUT:
|
||||||
|
---
|
||||||
|
fail_compilation/test20414.c(1): Error: `size_t` is not defined, perhaps `#include <stddef.h>` ?
|
||||||
|
fail_compilation/test20414.c(2): Error: `ptrdiff_t` is not defined, perhaps `#include <stddef.h>` ?
|
||||||
|
fail_compilation/test20414.c(3): Error: `NULL` is not defined, perhaps `#include <stddef.h>` is needed?
|
||||||
|
fail_compilation/test20414.c(5): Error: undefined identifier `fooo`, did you mean function `foo`?
|
||||||
|
---
|
||||||
|
*/
|
||||||
|
|
||||||
|
#line 1
|
||||||
|
size_t x;
|
||||||
|
ptrdiff_t pd;
|
||||||
|
void *p = NULL;
|
||||||
|
void foo(void);
|
||||||
|
void (*fp)(void) = &fooo;
|
|
@ -1,6 +1,6 @@
|
||||||
/* TEST_OUTPUT:
|
/* TEST_OUTPUT:
|
||||||
---
|
---
|
||||||
fail_compilation/test23003.c(101): Error: undefined identifier `size_t`
|
fail_compilation/test23003.c(101): Error: `size_t` is not defined, perhaps `#include <stddef.h>` ?
|
||||||
fail_compilation/test23003.c(102): Error: undefined identifier `object`
|
fail_compilation/test23003.c(102): Error: undefined identifier `object`
|
||||||
---
|
---
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue