aboutsummaryrefslogtreecommitdiff
path: root/src/checker.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2021-06-06 12:42:39 +0100
committergingerBill <bill@gingerbill.org>2021-06-06 12:42:39 +0100
commit84b851f5787b22fe38a748e3b32ca284aeb54920 (patch)
tree006b3a4e7751cdf66ee452dc05623c9b3eacef9e /src/checker.cpp
parent785c27daa7a84734c29dbbbb35d084dcdd8752f2 (diff)
Add warning to variables which may overflow the stack on declaration; #Fix 661
Diffstat (limited to 'src/checker.cpp')
-rw-r--r--src/checker.cpp41
1 files changed, 25 insertions, 16 deletions
diff --git a/src/checker.cpp b/src/checker.cpp
index 2792bdece..3f36b024c 100644
--- a/src/checker.cpp
+++ b/src/checker.cpp
@@ -561,10 +561,6 @@ bool check_vet_unused(Checker *c, Entity *e, VettedEntity *ve) {
}
void check_scope_usage(Checker *c, Scope *scope) {
- if (!build_context.vet) {
- return;
- }
-
bool vet_unused = true;
bool vet_shadowing = true;
@@ -591,19 +587,32 @@ void check_scope_usage(Checker *c, Scope *scope) {
Entity *other = ve.other;
String name = e->token.string;
- switch (ve.kind) {
- case VettedEntity_Unused:
- error(e->token, "'%.*s' declared but not used", LIT(name));
- break;
- case VettedEntity_Shadowed:
- if (e->flags&EntityFlag_Using) {
- error(e->token, "Declaration of '%.*s' from 'using' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line);
- } else {
- error(e->token, "Declaration of '%.*s' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line);
+ if (build_context.vet) {
+ switch (ve.kind) {
+ case VettedEntity_Unused:
+ error(e->token, "'%.*s' declared but not used", LIT(name));
+ break;
+ case VettedEntity_Shadowed:
+ if (e->flags&EntityFlag_Using) {
+ error(e->token, "Declaration of '%.*s' from 'using' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line);
+ } else {
+ error(e->token, "Declaration of '%.*s' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line);
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ 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);
}
- break;
- default:
- break;
}
}