Skip to content

Commit 6839c3d

Browse files
committed
clean up unused attributes warning handling
1 parent 429192b commit 6839c3d

File tree

4 files changed

+47
-20
lines changed

4 files changed

+47
-20
lines changed

jscomp/core/lam_compile_const.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ and translate (x : Lam_constant.t ) : J.expression =
8484
E.str i
8585
| Const_unicode i ->
8686
E.unicode i
87-
(* E.str i ~delimiter:Literals.escaped_j_delimiter *)
87+
8888

8989
| Const_pointer (c,pointer_info) ->
9090
E.int ?comment:(Lam_compile_util.comment_of_pointer_info pointer_info)

jscomp/syntax/bs_ast_invariant.ml

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,39 @@ let is_bs_attribute txt =
3535

3636
let used_attributes : Parsetree.attribute Hash_set_poly.t = Hash_set_poly.create 16
3737

38+
let dump_attribute fmt = (fun ( (sloc : string Asttypes.loc),payload) ->
39+
Format.fprintf fmt "@[%s %a@]" sloc.txt (Printast.payload 0 ) payload
40+
)
41+
42+
let dump_used_attributes fmt =
43+
Format.fprintf fmt "Used attributes Listing Start:@.";
44+
Hash_set_poly.iter (fun attr -> dump_attribute fmt attr) used_attributes;
45+
Format.fprintf fmt "Used attributes Listing End:@."
46+
47+
3848
let mark_used_bs_attribute (x : Parsetree.attribute) =
3949
Hash_set_poly.add used_attributes x
4050

41-
let warn_unused_attributes attrs =
51+
let dummy_unused_attribute : Warnings.t = (Bs_unused_attribute "")
52+
53+
54+
55+
let warn_unused_attribute
56+
(({txt; loc}, _) as attr : Parsetree.attribute) =
57+
if is_bs_attribute txt &&
58+
not (Hash_set_poly.mem used_attributes attr) then
59+
begin
60+
#if BS_DEBUG then (*COMMENT*)
61+
dump_used_attributes Format.err_formatter;
62+
dump_attribute Format.err_formatter attr ;
63+
#end
64+
Location.prerr_warning loc (Bs_unused_attribute txt)
65+
end
66+
67+
let warn_unused_attributes (attrs : Parsetree.attributes) =
4268
if attrs <> [] then
43-
List.iter (fun (({txt; loc}, _) as a : Parsetree.attribute) ->
44-
if is_bs_attribute txt &&
45-
not (Hash_set_poly.mem used_attributes a) then
46-
Location.prerr_warning loc (Warnings.Bs_unused_attribute txt)
47-
) attrs
69+
Ext_list.iter attrs warn_unused_attribute
70+
4871
#if OCAML_VERSION =~ ">4.03.0" then
4972
type iterator = Ast_iterator.iterator
5073
let default_iterator = Ast_iterator.default_iterator
@@ -57,13 +80,7 @@ let default_iterator = Bs_ast_iterator.default_iterator
5780
let emit_external_warnings : iterator=
5881
{
5982
default_iterator with
60-
attribute = (fun _ a ->
61-
match a with
62-
| {txt ; loc}, _ ->
63-
if is_bs_attribute txt &&
64-
not (Hash_set_poly.mem used_attributes a) then
65-
Location.prerr_warning loc (Bs_unused_attribute txt)
66-
);
83+
attribute = (fun _ attr -> warn_unused_attribute attr);
6784
expr = (fun self a ->
6885
match a.Parsetree.pexp_desc with
6986
| Pexp_constant (
@@ -97,3 +114,11 @@ let emit_external_warnings : iterator=
97114
default_iterator.value_description self v
98115
)
99116
}
117+
118+
let emit_external_warnings_on_structure (stru : Parsetree.structure) =
119+
if Warnings.is_active dummy_unused_attribute then
120+
emit_external_warnings.structure emit_external_warnings stru
121+
122+
let emit_external_warnings_on_signature (sigi : Parsetree.signature) =
123+
if Warnings.is_active dummy_unused_attribute then
124+
emit_external_warnings.signature emit_external_warnings sigi

jscomp/syntax/bs_ast_invariant.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ val mark_used_bs_attribute :
3636
val warn_unused_attributes :
3737
Parsetree.attributes -> unit
3838
(** Ast invariant checking for detecting errors *)
39-
val emit_external_warnings : iterator
39+
40+
val emit_external_warnings_on_structure:
41+
Parsetree.structure -> unit
42+
43+
val emit_external_warnings_on_signature:
44+
Parsetree.signature -> unit

jscomp/syntax/ppx_entry.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ let signature_config_table :
285285
(Parsetree.expression option -> unit) String_map.t=
286286
String_map.of_list common_actions_table
287287

288-
let dummy_unused_attribute : Warnings.t = (Bs_unused_attribute "")
289288

290289
let rewrite_signature :
291290
(Parsetree.signature -> Parsetree.signature) ref =
@@ -303,8 +302,7 @@ let rewrite_signature :
303302
unsafe_mapper.signature unsafe_mapper x in
304303
reset ();
305304
(* Keep this check, since the check is not inexpensive*)
306-
if Warnings.is_active dummy_unused_attribute then
307-
Bs_ast_invariant.emit_external_warnings.signature Bs_ast_invariant.emit_external_warnings result ;
305+
Bs_ast_invariant.emit_external_warnings_on_signature result;
308306
result
309307
)
310308

@@ -331,8 +329,7 @@ let rewrite_implementation : (Parsetree.structure -> Parsetree.structure) ref =
331329
unsafe_mapper.structure unsafe_mapper x in
332330
reset ();
333331
(* Keep this check since it is not inexpensive*)
334-
(if Warnings.is_active dummy_unused_attribute then
335-
Bs_ast_invariant.emit_external_warnings.structure Bs_ast_invariant.emit_external_warnings result);
332+
Bs_ast_invariant.emit_external_warnings_on_structure result;
336333
result
337334
)
338335

0 commit comments

Comments
 (0)