Add -vet-extra (checks for unneeded casts and transmutes)

This commit is contained in:
gingerBill
2021-03-03 14:17:48 +00:00
parent c2794b62a9
commit 75f127af7c
4 changed files with 28 additions and 3 deletions

View File

@@ -190,6 +190,7 @@ struct BuildContext {
bool no_entry_point;
bool use_lld;
bool vet;
bool vet_extra;
bool cross_compiling;
bool different_os;
bool keep_object_files;

View File

@@ -2388,6 +2388,14 @@ void check_cast(CheckerContext *c, Operand *x, Type *type) {
update_expr_type(c, x->expr, final_type, true);
}
if (build_context.vet_extra) {
if (are_types_identical(x->type, type)) {
gbString str = type_to_string(type);
warning(x->expr, "Unneeded cast to the same type '%s'", str);
gb_string_free(str);
}
}
x->type = type;
}
@@ -2429,6 +2437,14 @@ bool check_transmute(CheckerContext *c, Ast *node, Operand *o, Type *t) {
return false;
}
if (build_context.vet_extra) {
if (are_types_identical(o->type, t)) {
gbString str = type_to_string(t);
warning(o->expr, "Unneeded transmute to the same type '%s'", str);
gb_string_free(str);
}
}
o->mode = Addressing_Value;
o->type = t;
return true;

View File

@@ -61,9 +61,6 @@ struct lbAddr {
Type *type;
Type *result;
} map;
struct {
i32 value_index;
} bit_field;
struct {
Selection sel;
} ctx;

View File

@@ -593,6 +593,7 @@ enum BuildFlagKind {
BuildFlag_NoEntryPoint,
BuildFlag_UseLLD,
BuildFlag_Vet,
BuildFlag_VetExtra,
BuildFlag_UseLLVMApi,
BuildFlag_IgnoreUnknownAttributes,
BuildFlag_ExtraLinkerFlags,
@@ -706,6 +707,7 @@ bool parse_build_flags(Array<String> args) {
add_flag(&build_flags, BuildFlag_NoEntryPoint, str_lit("no-entry-point"), BuildFlagParam_None, Command__does_check &~ Command_test);
add_flag(&build_flags, BuildFlag_UseLLD, str_lit("lld"), BuildFlagParam_None, Command__does_build);
add_flag(&build_flags, BuildFlag_Vet, str_lit("vet"), BuildFlagParam_None, Command__does_check);
add_flag(&build_flags, BuildFlag_VetExtra, str_lit("vet-extra"), BuildFlagParam_None, Command__does_check);
add_flag(&build_flags, BuildFlag_UseLLVMApi, str_lit("llvm-api"), BuildFlagParam_None, Command__does_build);
add_flag(&build_flags, BuildFlag_IgnoreUnknownAttributes, str_lit("ignore-unknown-attributes"), BuildFlagParam_None, Command__does_check);
add_flag(&build_flags, BuildFlag_ExtraLinkerFlags, str_lit("extra-linker-flags"), BuildFlagParam_String, Command__does_build);
@@ -1151,6 +1153,10 @@ bool parse_build_flags(Array<String> args) {
case BuildFlag_Vet:
build_context.vet = true;
break;
case BuildFlag_VetExtra:
build_context.vet = true;
build_context.vet_extra = true;
break;
case BuildFlag_UseLLVMApi:
build_context.use_llvm_api = true;
@@ -1694,6 +1700,11 @@ void print_show_help(String const arg0, String const &command) {
print_usage_line(3, "Unused declarations");
print_usage_line(0, "");
print_usage_line(1, "-vet-extra");
print_usage_line(2, "Do even more checks than standard vet on the code");
print_usage_line(2, "To treat the extra warnings as errors, use -warnings-as-errors");
print_usage_line(0, "");
print_usage_line(1, "-ignore-unknown-attributes");
print_usage_line(2, "Ignores unknown attributes");
print_usage_line(2, "This can be used with metaprogramming tools");