Skip to content

Commit

Permalink
Add -w (#10612)
Browse files Browse the repository at this point in the history
* let's go

* this isn't overengineered

* maybe like this

* increase global warning

* deal with all warning situations

* fail nicer
  • Loading branch information
Simn authored Mar 7, 2022
1 parent 0c9d011 commit b687030
Show file tree
Hide file tree
Showing 33 changed files with 369 additions and 144 deletions.
6 changes: 6 additions & 0 deletions src-json/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,12 @@
"targets": ["TClassField"],
"internal": true
},
{
"name": "HaxeWarning",
"metadata": ":haxe.warning",
"doc": "Modifies warning options, equivalent to the -w CLI argument",
"targets": ["TClass","TClassField"]
},
{
"name": "Void",
"metadata": ":void",
Expand Down
21 changes: 10 additions & 11 deletions src/codegen/gencommon/castDetect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,6 @@ let choose_ctor gen cl tparams etl maybe_empty_t p =
unify et t;
check_arg arglist elist
with Unify_error el ->
(* List.iter (fun el -> gen.gcon.warning (Error.unify_error_msg (print_context()) el) p) el; *)
false
)
| _ ->
Expand Down Expand Up @@ -768,12 +767,12 @@ let handle_type_parameter gen e e1 ef ~clean_ef ~overloads_cast_to_base f elist
| [Cannot_unify (b, TAbstract(a,params))] ->
let a = apply_params a.a_params params a.a_this in
if not (shallow_eq a b) then
gen.gcon.warning ("This expression may be invalid") pos
gen.gwarning WGencommon ("This expression may be invalid") pos
| _ ->
gen.gcon.warning ("This expression may be invalid") pos
gen.gwarning WGencommon ("This expression may be invalid") pos
)
| Invalid_argument _ ->
gen.gcon.warning ("This expression may be invalid") pos
gen.gwarning WGencommon ("This expression may be invalid") pos
);

List.map (fun t ->
Expand Down Expand Up @@ -825,7 +824,7 @@ let handle_type_parameter gen e e1 ef ~clean_ef ~overloads_cast_to_base f elist
(* f,f.cf_type, false *)
select_overload gen e1.etype ((f.cf_type,f) :: List.map (fun f -> f.cf_type,f) f.cf_overloads) [] [], true
| _ ->
gen.gcon.warning "Overloaded classfield typed as anonymous" ecall.epos;
gen.gwarning WGencommon "Overloaded classfield typed as anonymous" ecall.epos;
(cf, actual_t, true), true
in

Expand All @@ -848,7 +847,7 @@ let handle_type_parameter gen e e1 ef ~clean_ef ~overloads_cast_to_base f elist
end;
{ cf_orig with cf_name = cf.cf_name },actual_t,false
| None ->
gen.gcon.warning "Cannot find matching overload" ecall.epos;
gen.gwarning WGencommon "Cannot find matching overload" ecall.epos;
cf, actual_t, true
else
cf,actual_t,error
Expand Down Expand Up @@ -930,7 +929,7 @@ let handle_type_parameter gen e e1 ef ~clean_ef ~overloads_cast_to_base f elist
elist);
}, elist
with Invalid_argument _ ->
gen.gcon.warning ("This expression may be invalid" ) ecall.epos;
gen.gwarning WGencommon ("This expression may be invalid" ) ecall.epos;
{ ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist) }, elist
in
let new_ecall = if fparams <> [] then gen.gparam_func_call new_ecall { e1 with eexpr = TField(!ef, f) } fparams elist else new_ecall in
Expand Down Expand Up @@ -960,7 +959,7 @@ let handle_type_parameter gen e e1 ef ~clean_ef ~overloads_cast_to_base f elist
*)
| _ ->
let pt = match e with | None -> real_type | Some _ -> snd (get_fun e1.etype) in
let _params = match follow pt with | TEnum(_, p) -> p | _ -> gen.gcon.warning (debug_expr e1) e1.epos; die "" __LOC__ in
let _params = match follow pt with | TEnum(_, p) -> p | _ -> gen.gwarning WGencommon (debug_expr e1) e1.epos; die "" __LOC__ in
let args, ret = get_fun efield.ef_type in
let actual_t = TFun(List.map (fun (n,o,t) -> (n,o,gen.greal_type t)) args, gen.greal_type ret) in
(*
Expand Down Expand Up @@ -1144,7 +1143,7 @@ let configure gen ?(overloads_cast_to_base = false) maybe_empty_t calls_paramete
let base_type = match follow et with
| TInst({ cl_path = ([], "Array") } as cl, bt) -> gen.greal_type_param (TClassDecl cl) bt
| _ ->
gen.gcon.warning (debug_type et) e.epos;
gen.gwarning WGencommon (debug_type et) e.epos;
(match gen.gcurrent_class with
| Some cl -> print_endline (s_type_path cl.cl_path)
| _ -> ());
Expand Down Expand Up @@ -1188,7 +1187,7 @@ let configure gen ?(overloads_cast_to_base = false) maybe_empty_t calls_paramete
) (wrap_rest_args gen (TFun (args,rt)) eparams e.epos) args in
{ e with eexpr = TCall(ef, eparams) }
with | Not_found ->
gen.gcon.warning "No overload found for this constructor call" e.epos;
gen.gwarning WGencommon "No overload found for this constructor call" e.epos;
{ e with eexpr = TCall(ef, List.map run eparams) })
| TCall (ef, eparams) ->
(match ef.etype with
Expand Down Expand Up @@ -1216,7 +1215,7 @@ let configure gen ?(overloads_cast_to_base = false) maybe_empty_t calls_paramete
) (wrap_rest_args gen (TFun (args,rt)) eparams e.epos) args in
{ e with eexpr = TNew(cl, tparams, eparams) }
with | Not_found ->
gen.gcon.warning "No overload found for this constructor call" e.epos;
gen.gwarning WGencommon "No overload found for this constructor call" e.epos;
{ e with eexpr = TNew(cl, tparams, List.map run eparams) })
| TUnop((Increment | Decrement) as op, flag, ({ eexpr = TArray (arr, idx) } as e2))
when (match follow arr.etype with TInst({ cl_path = ["cs"],"NativeArray" },_) -> true | _ -> false) ->
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/gencommon/closuresToClass.ml
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ let configure gen ft =
with
| Not_found ->
if in_tparam then begin
gen.gcon.warning "This expression may be invalid" e.epos;
gen.gwarning WGencommon "This expression may be invalid" e.epos;
e
end else
(* It is possible that we are recursively calling a function
Expand All @@ -642,8 +642,8 @@ let configure gen ft =
(Meta.Custom(":tparamcall"), [], e.epos), e
) }
| Unify_error el ->
List.iter (fun el -> gen.gcon.warning (Error.unify_error_msg (print_context()) el) e.epos) el;
gen.gcon.warning "This expression may be invalid" e.epos;
List.iter (fun el -> gen.gwarning WGencommon (Error.unify_error_msg (print_context()) el) e.epos) el;
gen.gwarning WGencommon "This expression may be invalid" e.epos;
e
)
(* (handle_anon_func:texpr->tfunc->texpr) (dynamic_func_call:texpr->texpr->texpr list->texpr) *)
Expand Down
9 changes: 8 additions & 1 deletion src/codegen/gencommon/gencommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ type generator_ctx =

gtools : gen_tools;

gwarning : Warning.warning -> string -> pos -> unit;

(*
module filters run before module filters and they should generate valid haxe syntax as a result.
Module filters shouldn't go through the expressions as it adds an unnecessary burden to the GC,
Expand Down Expand Up @@ -573,6 +575,11 @@ let new_ctx con =

let rec gen = {
gcon = con;
gwarning = (fun w msg p ->
let options = Option.map_default (fun c -> Warning.from_meta c.cl_meta) [] gen.gcurrent_class in
let options = options @ Option.map_default (fun cf -> Warning.from_meta cf.cf_meta) [] gen.gcurrent_classfield in
con.warning w options msg p
);
gentry_point = get_entry_point con;
gclasses = {
cl_reflect = get_cl (get_type ([], "Reflect"));
Expand Down Expand Up @@ -612,7 +619,7 @@ let new_ctx con =

greal_field_types = Hashtbl.create 0;
ghandle_cast = (fun to_t from_t e -> mk_cast to_t e);
gon_unsafe_cast = (fun t t2 pos -> (gen.gcon.warning ("Type " ^ (debug_type t2) ^ " is being cast to the unrelated type " ^ (s_type (print_context()) t)) pos));
gon_unsafe_cast = (fun t t2 pos -> (gen.gwarning WGencommon ("Type " ^ (debug_type t2) ^ " is being cast to the unrelated type " ^ (s_type (print_context()) t)) pos));
gneeds_box = (fun t -> false);
gspecial_needs_cast = (fun to_t from_t -> false);
gsupported_conversions = Hashtbl.create 0;
Expand Down
17 changes: 9 additions & 8 deletions src/codegen/gencommon/initFunction.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,19 @@ let handle_override_dynfun acc e this field =
| None -> e :: acc
| Some add_expr -> add_expr :: e :: acc

let handle_class com cl =
let handle_class gen cl =
let com = gen.gcon in
let init = match cl.cl_init with
| None -> []
| Some i -> [i]
in
let init = List.fold_left (fun acc cf ->
match cf.cf_kind with
| Var v when Meta.has Meta.ReadOnly cf.cf_meta ->
if v.v_write <> AccNever && not (Meta.has Meta.CoreApi cl.cl_meta) then com.warning "@:readOnly variable declared without `never` setter modifier" cf.cf_pos;
if v.v_write <> AccNever && not (Meta.has Meta.CoreApi cl.cl_meta) then gen.gwarning WGencommon "@:readOnly variable declared without `never` setter modifier" cf.cf_pos;
(match cf.cf_expr with
| None -> com.warning "Uninitialized readonly variable" cf.cf_pos
| Some e -> ensure_simple_expr com e);
| None -> gen.gwarning WGencommon "Uninitialized readonly variable" cf.cf_pos
| Some e -> ensure_simple_expr gen.gcon e);
acc
| Var _
| Method MethDynamic when Type.is_physical_field cf ->
Expand Down Expand Up @@ -115,7 +116,7 @@ let handle_class com cl =
let vars, funs = List.fold_left (fun (acc_vars,acc_funs) cf ->
match cf.cf_kind with
| Var v when Meta.has Meta.ReadOnly cf.cf_meta ->
if v.v_write <> AccNever && not (Meta.has Meta.CoreApi cl.cl_meta) then com.warning "@:readOnly variable declared without `never` setter modifier" cf.cf_pos;
if v.v_write <> AccNever && not (Meta.has Meta.CoreApi cl.cl_meta) then gen.gwarning WGencommon "@:readOnly variable declared without `never` setter modifier" cf.cf_pos;
Option.may (ensure_simple_expr com) cf.cf_expr;
(acc_vars,acc_funs)
| Var _
Expand Down Expand Up @@ -224,15 +225,15 @@ let handle_class com cl =
List.iter process (ctors :: ctors.cf_overloads)
)

let mod_filter com md =
let mod_filter gen md =
match md with
| TClassDecl cl when not (has_class_flag cl CExtern) ->
handle_class com cl
handle_class gen cl
| _ -> ()

let name = "init_funcs"
let priority = solve_deps name [DBefore OverloadingConstructor.priority]

let configure gen =
let run = (fun md -> mod_filter gen.gcon md; md) in
let run = (fun md -> mod_filter gen md; md) in
gen.gmodule_filters#add name (PCustom priority) run
6 changes: 3 additions & 3 deletions src/codegen/gencommon/unreachableCodeEliminationSynf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ let rec get_constant_expr e =
| TParenthesis(e) | TMeta(_,e) -> get_constant_expr e
| _ -> None

let init com java_mode =
let init gen java_mode =
let should_warn = false in

let do_warn =
if should_warn then com.warning "Unreachable code" else (fun pos -> ())
if should_warn then gen.gwarning WGencommon "Unreachable code" else (fun pos -> ())
in

let return_loop expr kind =
Expand Down Expand Up @@ -208,5 +208,5 @@ let init com java_mode =
let priority = min_dep -. 100.0

let configure gen java_mode =
let run = init gen.gcon java_mode in
let run = init gen java_mode in
gen.gsyntax_filters#add "unreachable_synf" (PCustom priority) run
29 changes: 26 additions & 3 deletions src/compiler/haxe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,13 @@ let setup_common_context ctx com =
Common.raw_define com "true";
Common.define_value com Define.Dce "std";
com.info <- (fun msg p -> message ctx (CMInfo(msg,p)));
com.warning <- (fun msg p -> message ctx (CMWarning(msg,p)));
com.warning <- (fun w options msg p ->
match Warning.get_mode w (com.warning_options @ options) with
| WMEnable ->
message ctx (CMWarning(msg,p))
| WMDisable ->
()
);
com.error <- error ctx;
let filter_messages = (fun keep_errors predicate -> (List.filter (fun msg ->
(match msg with
Expand Down Expand Up @@ -446,9 +452,21 @@ let process_display_configuration ctx =
if com.display.dms_kind <> DMNone then begin
com.warning <-
if com.display.dms_error_policy = EPCollect then
(fun s p -> add_diagnostics_message com s p DKCompilerError DisplayTypes.DiagnosticsSeverity.Warning)
(fun w options s p ->
match Warning.get_mode w (com.warning_options @ options) with
| WMEnable ->
add_diagnostics_message com s p DKCompilerError DisplayTypes.DiagnosticsSeverity.Warning
| WMDisable ->
()
)
else
(fun msg p -> message ctx (CMWarning(msg,p)));
(fun w options msg p ->
match Warning.get_mode w (com.warning_options @ options) with
| WMEnable ->
message ctx (CMWarning(msg,p))
| WMDisable ->
()
);
com.error <- error ctx;
end;
Lexer.old_format := Common.defined com Define.OldErrorFormat;
Expand Down Expand Up @@ -956,6 +974,11 @@ try
did_something := true;
),"<directory>","set current working directory");
("Compilation",["--haxelib-global"],[], Arg.Unit (fun () -> ()),"","pass --global argument to haxelib");
("Compilation",["-w"],[], Arg.String (fun s ->
let p = { pfile = "-w " ^ s; pmin = 0; pmax = 0 } in
let l = Warning.parse_options s p in
com.warning_options <- l :: com.warning_options
),"<warning list>","enable or disable specific warnings");
] in
let args_callback cl =
begin try
Expand Down
7 changes: 5 additions & 2 deletions src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ open Type
open Globals
open Define
open NativeLibraries
open Warning

type package_rule =
| Forbidden
Expand Down Expand Up @@ -307,7 +308,8 @@ type context = {
mutable package_rules : (string,package_rule) PMap.t;
mutable error : string -> pos -> unit;
mutable info : string -> pos -> unit;
mutable warning : string -> pos -> unit;
mutable warning : warning -> Warning.warning_option list list -> string -> pos -> unit;
mutable warning_options : Warning.warning_option list list;
mutable get_messages : unit -> compiler_message list;
mutable filter_messages : (compiler_message -> bool) -> unit;
mutable load_extern_type : (string * (path -> pos -> Ast.package option)) list; (* allow finding types which are not in sources *)
Expand Down Expand Up @@ -743,7 +745,8 @@ let create version args =
};
get_macros = (fun() -> None);
info = (fun _ _ -> die "" __LOC__);
warning = (fun _ _ -> die "" __LOC__);
warning = (fun _ _ _ -> die "" __LOC__);
warning_options = [];
error = (fun _ _ -> die "" __LOC__);
get_messages = (fun() -> []);
filter_messages = (fun _ -> ());
Expand Down
17 changes: 14 additions & 3 deletions src/context/display/deprecationCheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open Common
open Ast

let curclass = ref null_class
let curfield = ref null_field

let warned_positions = Hashtbl.create 0

Expand All @@ -12,8 +13,11 @@ let warn_deprecation com s p_usage =
if not (Hashtbl.mem warned_positions (pkey p_usage)) then begin
Hashtbl.add warned_positions (pkey p_usage) (s,p_usage);
match com.display.dms_kind with
| DMDiagnostics _ -> ()
| _ -> com.warning s p_usage;
| DMDiagnostics _ ->
()
| _ ->
let options = Warning.from_meta (!curclass.cl_meta @ !curfield.cf_meta) in
com.warning WDeprecated options s p_usage;
end

let print_deprecation_message com meta s p_usage =
Expand Down Expand Up @@ -81,7 +85,14 @@ let run_on_expr com e =
in
expr e

let run_on_field com cf = match cf.cf_expr with None -> () | Some e -> run_on_expr com e
let run_on_field com cf =
match cf.cf_expr with
| None ->
()
| Some e ->
curfield := cf;
run_on_expr com e;
curfield := null_field

let run com =
List.iter (fun t -> match t with
Expand Down
8 changes: 6 additions & 2 deletions src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ let display_error ctx msg p = match ctx.com.display.DisplayMode.dms_error_policy
| DisplayMode.EPShow | DisplayMode.EPIgnore -> ctx.on_error ctx msg p
| DisplayMode.EPCollect -> add_diagnostics_message ctx.com msg p DisplayTypes.DiagnosticsKind.DKCompilerError DisplayTypes.DiagnosticsSeverity.Error

let warning ctx w msg p =
let options = (Warning.from_meta ctx.curclass.cl_meta) @ (Warning.from_meta ctx.curfield.cf_meta) in
ctx.com.warning w options msg p

let make_call ctx e el t p = (!make_call_ref) ctx e el t p

let type_expr ?(mode=MGet) ctx e with_type = (!type_expr_ref) ~mode ctx e with_type
Expand Down Expand Up @@ -298,8 +302,8 @@ let add_local ctx k n t p =
let v' = PMap.find n ctx.locals in
(* ignore std lib *)
if not (List.exists (ExtLib.String.starts_with p.pfile) ctx.com.std_path) then begin
ctx.com.warning "This variable shadows a previously declared variable" p;
ctx.com.warning (compl_msg "Previous variable was here") v'.v_pos
warning ctx WVarShadow "This variable shadows a previously declared variable" p;
warning ctx WVarShadow (compl_msg "Previous variable was here") v'.v_pos
end
with Not_found ->
()
Expand Down
Loading

0 comments on commit b687030

Please sign in to comment.