warning diagnostics for unused locals

This commit is contained in:
2026-07-08 18:45:55 +02:00
parent f171a6579d
commit 5e18df9bc1
10 changed files with 378 additions and 112 deletions
+39 -14
View File
@@ -53,8 +53,14 @@ Store :: struct {
}
Diagnostic :: struct {
span: Span,
message: string,
span: Span,
message: string,
severity: Severity,
}
Severity :: enum u8 {
Error,
Warning,
}
Diagnostics :: struct {
@@ -66,8 +72,9 @@ Diagnostics :: struct {
}
Diagnostic_Key :: struct {
span: Span,
message: string,
span: Span,
message: string,
severity: Severity,
}
init_store :: proc(allocator := context.allocator) -> Store {
@@ -144,31 +151,47 @@ destroy_diagnostics :: proc(diagnostics: ^Diagnostics) {
delete(diagnostics.items)
}
add :: proc(diagnostics: ^Diagnostics, span: Span, message: string) -> Diagnostic_Id {
key := Diagnostic_Key{span=span, message=message}
add_with_severity :: proc(diagnostics: ^Diagnostics, span: Span, message: string, severity: Severity) -> Diagnostic_Id {
key := Diagnostic_Key{span=span, message=message, severity=severity}
if id, ok := diagnostics.index[key]; ok {
return id
}
id := diagnostic_id(len(diagnostics.items))
cloned := fmt.aprintf("%s", message, allocator=diagnostics.allocator)
append(&diagnostics.items, Diagnostic{span=span, message=cloned})
diagnostics.index[Diagnostic_Key{span=span, message=cloned}] = id
append(&diagnostics.items, Diagnostic{span=span, message=cloned, severity=severity})
diagnostics.index[Diagnostic_Key{span=span, message=cloned, severity=severity}] = id
return id
}
addf :: proc(diagnostics: ^Diagnostics, span: Span, format: string, args: ..any) -> Diagnostic_Id {
add :: proc(diagnostics: ^Diagnostics, span: Span, message: string) -> Diagnostic_Id {
return add_with_severity(diagnostics, span, message, .Error)
}
add_warning :: proc(diagnostics: ^Diagnostics, span: Span, message: string) -> Diagnostic_Id {
return add_with_severity(diagnostics, span, message, .Warning)
}
addf_with_severity :: proc(diagnostics: ^Diagnostics, span: Span, severity: Severity, format: string, args: ..any) -> Diagnostic_Id {
message := fmt.aprintf(format, ..args, allocator=diagnostics.allocator)
key := Diagnostic_Key{span=span, message=message}
key := Diagnostic_Key{span=span, message=message, severity=severity}
if id, ok := diagnostics.index[key]; ok {
delete(message, diagnostics.allocator)
return id
}
id := diagnostic_id(len(diagnostics.items))
append(&diagnostics.items, Diagnostic{span=span, message=message})
diagnostics.index[Diagnostic_Key{span=span, message=message}] = id
append(&diagnostics.items, Diagnostic{span=span, message=message, severity=severity})
diagnostics.index[Diagnostic_Key{span=span, message=message, severity=severity}] = id
return id
}
addf :: proc(diagnostics: ^Diagnostics, span: Span, format: string, args: ..any) -> Diagnostic_Id {
return addf_with_severity(diagnostics, span, .Error, format, ..args)
}
addf_warning :: proc(diagnostics: ^Diagnostics, span: Span, format: string, args: ..any) -> Diagnostic_Id {
return addf_with_severity(diagnostics, span, .Warning, format, ..args)
}
line_and_column :: proc(source_file: ^Source, offset: Offset) -> (line, column: int) {
if len(source_file.line_starts) > 0 {
limit := min(int(offset), len(source_file.text))
@@ -222,15 +245,17 @@ format :: proc(diagnostics: ^Diagnostics, id: Diagnostic_Id, allocator := contex
}
diagnostic := diagnostics.items[index]
source_file := source_for_span(diagnostics, diagnostic.span)
severity := "warning" if diagnostic.severity == .Warning else "error"
if source_file == nil {
return fmt.aprintf("<unknown>: error: %s", diagnostic.message, allocator=allocator)
return fmt.aprintf("<unknown>: %s: %s", severity, diagnostic.message, allocator=allocator)
}
line, column := line_and_column(source_file, diagnostic.span.start)
return fmt.aprintf(
"%s:%d:%d: error: %s",
"%s:%d:%d: %s: %s",
source_file.path,
line,
column,
severity,
diagnostic.message,
allocator=allocator,
)