From 00344e1323dc6d9baf09c26f31c409f26a0a1cca Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 18 Mar 2024 16:56:01 +0000 Subject: [PATCH 1/4] Add check to people trying to `foreign import` C files. --- src/checker.cpp | 16 ++++++++++++++++ src/string.cpp | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/src/checker.cpp b/src/checker.cpp index 72c0ae574..fb7d401ab 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -4806,6 +4806,22 @@ gb_internal void check_add_foreign_import_decl(CheckerContext *ctx, Ast *decl) { return; } + for (String const &path : fl->fullpaths) { + String ext = path_extension(path); + if (str_eq_ignore_case(ext, ".c") || + str_eq_ignore_case(ext, ".cpp") || + str_eq_ignore_case(ext, ".cxx") || + str_eq_ignore_case(ext, ".h") || + str_eq_ignore_case(ext, ".hpp") || + str_eq_ignore_case(ext, ".hxx") || + false + ) { + error(fl->token, "With 'foreign import', you cannot import a %.*s file directory, you must precompile the library and link against that", LIT(ext)); + break; + } + } + + // if (fl->collection_name != "system") { // char *c_str = gb_alloc_array(heap_allocator(), char, fullpath.len+1); // defer (gb_free(heap_allocator(), c_str)); diff --git a/src/string.cpp b/src/string.cpp index bd703b2a6..f762dca40 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -104,6 +104,13 @@ gb_internal gb_inline bool str_eq_ignore_case(String const &a, String const &b) return false; } +template +gb_internal gb_inline bool str_eq_ignore_case(String const &a, char const (&b_)[N]) { + String b = {cast(u8 *)b_, N-1}; + return str_eq_ignore_case(a, b); +} + + gb_internal void string_to_lower(String *s) { for (isize i = 0; i < s->len; i++) { s->text[i] = gb_char_to_lower(s->text[i]); From 97be7feb99d4ff2b26cde0426c619e50bb0d758a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 18 Mar 2024 17:32:26 +0000 Subject: [PATCH 2/4] Add list of C identifier suggestions (types and keywords) --- src/check_expr.cpp | 68 ++++++++++++++++++++++++++++++++++++---------- src/string.cpp | 3 ++ 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/check_expr.cpp b/src/check_expr.cpp index b58006427..7da113455 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -1528,6 +1528,55 @@ gb_internal bool check_cycle(CheckerContext *c, Entity *curr, bool report) { return false; } +struct CIdentSuggestion { + String name; + String msg; +}; + +// NOTE(bill): this linear look-up table might be slow but because it's an error case, it should be fine +gb_internal CIdentSuggestion const c_ident_suggestions[] = { + {str_lit("while"), str_lit("Did you mean 'for'? Odin only has one loop construct: 'for'")}, + + {str_lit("sizeof"), str_lit("Did you mean 'size_of'?")}, + {str_lit("alignof"), str_lit("Did you mean 'align_of'?")}, + {str_lit("offsetof"), str_lit("Did you mean 'offset_of'?")}, + + {str_lit("_Bool"), str_lit("Did you mean 'bool'?")}, + + {str_lit("char"), str_lit("Did you mean 'u8', 'i8', or 'c.char' (which is part of 'core:c')?")}, + {str_lit("short"), str_lit("Did you mean 'i16' or 'c.short' (which is part of 'core:c')?")}, + {str_lit("long"), str_lit("Did you mean 'c.long' (which is part of 'core:c')?")}, + {str_lit("float"), str_lit("Did you mean 'f32'?")}, + {str_lit("double"), str_lit("Did you mean 'f64'?")}, + {str_lit("unsigned"), str_lit("Did you mean 'c.uint' (which is part of 'core:c')?")}, + {str_lit("signed"), str_lit("Did you mean 'c.int' (which is part of 'core:c')?")}, + + {str_lit("size_t"), str_lit("Did you mean 'uint', or 'c.size_t' (which is part of 'core:c')?")}, + {str_lit("ssize_t"), str_lit("Did you mean 'int', or 'c.ssize_t' (which is part of 'core:c')?")}, + + {str_lit("uintptr_t"), str_lit("Did you mean 'uintptr'?")}, + {str_lit("intptr_t"), str_lit("Did you mean 'uintptr' or `int` or something else?")}, + {str_lit("ptrdiff_t"), str_lit("Did you mean 'int' or 'c.ptrdiff_t' (which is part of 'core:c')?")}, + {str_lit("intmax_t"), str_lit("Dit you mean 'c.intmax_t' (which is part of 'core:c')?")}, + {str_lit("uintmax_t"), str_lit("Dit you mean 'c.uintmax_t' (which is part of 'core:c')?")}, + + {str_lit("uint8_t"), str_lit("Did you mean 'u8'?")}, + {str_lit("int8_t"), str_lit("Did you mean 'i8'?")}, + {str_lit("uint16_t"), str_lit("Did you mean 'u16'?")}, + {str_lit("int16_t"), str_lit("Did you mean 'i16'?")}, + {str_lit("uint32_t"), str_lit("Did you mean 'u32'?")}, + {str_lit("int32_t"), str_lit("Did you mean 'i32'?")}, + {str_lit("uint64_t"), str_lit("Did you mean 'u64'?")}, + {str_lit("int64_t"), str_lit("Did you mean 'i64'?")}, + {str_lit("uint128_t"), str_lit("Did you mean 'u128'?")}, + {str_lit("int128_t"), str_lit("Did you mean 'i128'?")}, + + {str_lit("float32"), str_lit("Did you mean 'f32'?")}, + {str_lit("float64"), str_lit("Did you mean 'f64'?")}, + {str_lit("float32_t"), str_lit("Did you mean 'f32'?")}, + {str_lit("float64_t"), str_lit("Did you mean 'f64'?")}, +}; + gb_internal Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name) { GB_ASSERT(n->kind == Ast_Ident); o->mode = Addressing_Invalid; @@ -1543,20 +1592,11 @@ gb_internal Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *nam error(n, "Undeclared name: %.*s", LIT(name)); // NOTE(bill): Loads of checks for C programmers - if (name == "float") { - error_line("\tSuggestion: Did you mean 'f32'?\n"); - } else if (name == "double") { - error_line("\tSuggestion: Did you mean 'f64'?\n"); - } else if (name == "short") { - error_line("\tSuggestion: Did you mean 'i16' or 'c.short' (which is part of 'core:c')?\n"); - } else if (name == "long") { - error_line("\tSuggestion: Did you mean 'c.long' (which is part of 'core:c')?\n"); - } else if (name == "unsigned") { - error_line("\tSuggestion: Did you mean 'c.uint' (which is part of 'core:c')?\n"); - } else if (name == "char") { - error_line("\tSuggestion: Did you mean 'u8', 'i8' or 'c.char' (which is part of 'core:c')?\n"); - } else if (name == "while") { - error_line("\tSuggestion: Did you mean 'for'? Odin only has one loop construct: 'for'\n"); + + for (CIdentSuggestion const &suggestion : c_ident_suggestions) { + if (name == suggestion.name) { + error_line("\tSuggestion: %s\n", LIT(suggestion.msg)); + } } } o->type = t_invalid; diff --git a/src/string.cpp b/src/string.cpp index f762dca40..8be40ec3c 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -106,6 +106,9 @@ gb_internal gb_inline bool str_eq_ignore_case(String const &a, String const &b) template gb_internal gb_inline bool str_eq_ignore_case(String const &a, char const (&b_)[N]) { + if (a.len != N-1) { + return false; + } String b = {cast(u8 *)b_, N-1}; return str_eq_ignore_case(a, b); } From ec9ac593232d8e201639615ba075717dab066752 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 18 Mar 2024 21:37:40 +0000 Subject: [PATCH 3/4] Unify "Did you mean" strings --- src/check_expr.cpp | 68 +++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 7da113455..bb31a1646 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -1535,46 +1535,46 @@ struct CIdentSuggestion { // NOTE(bill): this linear look-up table might be slow but because it's an error case, it should be fine gb_internal CIdentSuggestion const c_ident_suggestions[] = { - {str_lit("while"), str_lit("Did you mean 'for'? Odin only has one loop construct: 'for'")}, + {str_lit("while"), str_lit("'for'? Odin only has one loop construct: 'for'")}, - {str_lit("sizeof"), str_lit("Did you mean 'size_of'?")}, - {str_lit("alignof"), str_lit("Did you mean 'align_of'?")}, - {str_lit("offsetof"), str_lit("Did you mean 'offset_of'?")}, + {str_lit("sizeof"), str_lit("'size_of'?")}, + {str_lit("alignof"), str_lit("'align_of'?")}, + {str_lit("offsetof"), str_lit("'offset_of'?")}, - {str_lit("_Bool"), str_lit("Did you mean 'bool'?")}, + {str_lit("_Bool"), str_lit("'bool'?")}, - {str_lit("char"), str_lit("Did you mean 'u8', 'i8', or 'c.char' (which is part of 'core:c')?")}, - {str_lit("short"), str_lit("Did you mean 'i16' or 'c.short' (which is part of 'core:c')?")}, - {str_lit("long"), str_lit("Did you mean 'c.long' (which is part of 'core:c')?")}, - {str_lit("float"), str_lit("Did you mean 'f32'?")}, - {str_lit("double"), str_lit("Did you mean 'f64'?")}, - {str_lit("unsigned"), str_lit("Did you mean 'c.uint' (which is part of 'core:c')?")}, - {str_lit("signed"), str_lit("Did you mean 'c.int' (which is part of 'core:c')?")}, + {str_lit("char"), str_lit("'u8', 'i8', or 'c.char' (which is part of 'core:c')?")}, + {str_lit("short"), str_lit("'i16' or 'c.short' (which is part of 'core:c')?")}, + {str_lit("long"), str_lit("'c.long' (which is part of 'core:c')?")}, + {str_lit("float"), str_lit("'f32'?")}, + {str_lit("double"), str_lit("'f64'?")}, + {str_lit("unsigned"), str_lit("'c.uint' (which is part of 'core:c')?")}, + {str_lit("signed"), str_lit("'c.int' (which is part of 'core:c')?")}, - {str_lit("size_t"), str_lit("Did you mean 'uint', or 'c.size_t' (which is part of 'core:c')?")}, - {str_lit("ssize_t"), str_lit("Did you mean 'int', or 'c.ssize_t' (which is part of 'core:c')?")}, + {str_lit("size_t"), str_lit("'uint', or 'c.size_t' (which is part of 'core:c')?")}, + {str_lit("ssize_t"), str_lit("'int', or 'c.ssize_t' (which is part of 'core:c')?")}, - {str_lit("uintptr_t"), str_lit("Did you mean 'uintptr'?")}, - {str_lit("intptr_t"), str_lit("Did you mean 'uintptr' or `int` or something else?")}, - {str_lit("ptrdiff_t"), str_lit("Did you mean 'int' or 'c.ptrdiff_t' (which is part of 'core:c')?")}, - {str_lit("intmax_t"), str_lit("Dit you mean 'c.intmax_t' (which is part of 'core:c')?")}, - {str_lit("uintmax_t"), str_lit("Dit you mean 'c.uintmax_t' (which is part of 'core:c')?")}, + {str_lit("uintptr_t"), str_lit("'uintptr'?")}, + {str_lit("intptr_t"), str_lit("'uintptr' or `int` or something else?")}, + {str_lit("ptrdiff_t"), str_lit("'int' or 'c.ptrdiff_t' (which is part of 'core:c')?")}, + {str_lit("intmax_t"), str_lit("'c.intmax_t' (which is part of 'core:c')?")}, + {str_lit("uintmax_t"), str_lit("'c.uintmax_t' (which is part of 'core:c')?")}, - {str_lit("uint8_t"), str_lit("Did you mean 'u8'?")}, - {str_lit("int8_t"), str_lit("Did you mean 'i8'?")}, - {str_lit("uint16_t"), str_lit("Did you mean 'u16'?")}, - {str_lit("int16_t"), str_lit("Did you mean 'i16'?")}, - {str_lit("uint32_t"), str_lit("Did you mean 'u32'?")}, - {str_lit("int32_t"), str_lit("Did you mean 'i32'?")}, - {str_lit("uint64_t"), str_lit("Did you mean 'u64'?")}, - {str_lit("int64_t"), str_lit("Did you mean 'i64'?")}, - {str_lit("uint128_t"), str_lit("Did you mean 'u128'?")}, - {str_lit("int128_t"), str_lit("Did you mean 'i128'?")}, + {str_lit("uint8_t"), str_lit("'u8'?")}, + {str_lit("int8_t"), str_lit("'i8'?")}, + {str_lit("uint16_t"), str_lit("'u16'?")}, + {str_lit("int16_t"), str_lit("'i16'?")}, + {str_lit("uint32_t"), str_lit("'u32'?")}, + {str_lit("int32_t"), str_lit("'i32'?")}, + {str_lit("uint64_t"), str_lit("'u64'?")}, + {str_lit("int64_t"), str_lit("'i64'?")}, + {str_lit("uint128_t"), str_lit("'u128'?")}, + {str_lit("int128_t"), str_lit("'i128'?")}, - {str_lit("float32"), str_lit("Did you mean 'f32'?")}, - {str_lit("float64"), str_lit("Did you mean 'f64'?")}, - {str_lit("float32_t"), str_lit("Did you mean 'f32'?")}, - {str_lit("float64_t"), str_lit("Did you mean 'f64'?")}, + {str_lit("float32"), str_lit("'f32'?")}, + {str_lit("float64"), str_lit("'f64'?")}, + {str_lit("float32_t"), str_lit("'f32'?")}, + {str_lit("float64_t"), str_lit("'f64'?")}, }; gb_internal Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *named_type, Type *type_hint, bool allow_import_name) { @@ -1595,7 +1595,7 @@ gb_internal Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *nam for (CIdentSuggestion const &suggestion : c_ident_suggestions) { if (name == suggestion.name) { - error_line("\tSuggestion: %s\n", LIT(suggestion.msg)); + error_line("\tSuggestion: Did you mean %s\n", LIT(suggestion.msg)); } } } From 89315986d4e4fb0977d98c2cfb8003763f5b93cf Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 19 Mar 2024 12:37:11 +0000 Subject: [PATCH 4/4] Add suggestion when mistyping an array backwards e.g. `T[]` --- src/check_type.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/check_type.cpp b/src/check_type.cpp index e71b35809..da4479f6e 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -3344,8 +3344,25 @@ gb_internal Type *check_type_expr(CheckerContext *ctx, Ast *e, Type *named_type) if (!ok) { gbString err_str = expr_to_string(e); + defer (gb_string_free(err_str)); + + ERROR_BLOCK(); error(e, "'%s' is not a type", err_str); - gb_string_free(err_str); + + Ast *node = unparen_expr(e); + if (node && node->kind == Ast_IndexExpr) { + gbString index_str = nullptr; + if (node->IndexExpr.index) { + index_str = expr_to_string(node->IndexExpr.index); + } + defer (gb_string_free(index_str)); + + gbString type_str = expr_to_string(node->IndexExpr.expr); + defer (gb_string_free(type_str)); + + error_line("\tSuggestion: Did you mean '[%s]%s'?", index_str ? index_str : "", type_str); + } + type = t_invalid; }