Add warning to variables which may overflow the stack on declaration; #Fix 661

This commit is contained in:
gingerBill
2021-06-06 12:42:39 +01:00
parent 785c27daa7
commit 84b851f578
+13 -4
View File
@@ -561,10 +561,6 @@ bool check_vet_unused(Checker *c, Entity *e, VettedEntity *ve) {
} }
void check_scope_usage(Checker *c, Scope *scope) { void check_scope_usage(Checker *c, Scope *scope) {
if (!build_context.vet) {
return;
}
bool vet_unused = true; bool vet_unused = true;
bool vet_shadowing = true; bool vet_shadowing = true;
@@ -591,6 +587,7 @@ void check_scope_usage(Checker *c, Scope *scope) {
Entity *other = ve.other; Entity *other = ve.other;
String name = e->token.string; String name = e->token.string;
if (build_context.vet) {
switch (ve.kind) { switch (ve.kind) {
case VettedEntity_Unused: case VettedEntity_Unused:
error(e->token, "'%.*s' declared but not used", LIT(name)); error(e->token, "'%.*s' declared but not used", LIT(name));
@@ -607,6 +604,18 @@ void check_scope_usage(Checker *c, Scope *scope) {
} }
} }
if (e->kind == Entity_Variable && (e->flags & (EntityFlag_Param|EntityFlag_Using)) == 0) {
i64 sz = type_size_of(e->type);
// TODO(bill): When is a good size warn?
// Is 128 KiB good enough?
if (sz >= 1ll<<17) {
gbString type_str = type_to_string(e->type);
warning(e->token, "Declaration of '%.*s' may cause a stack overflow due to its type '%s' having a size of %lld bytes", LIT(name), type_str, cast(long long)sz);
gb_string_free(type_str);
}
}
}
array_free(&vetted_entities); array_free(&vetted_entities);
for (Scope *child = scope->first_child; for (Scope *child = scope->first_child;