123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
* Copyright (C) 2017- Hongbo Zhang, Authors of ReScript
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)openPpxlibmoduleWarnings=structtypet=|Unused_attributeofstring|Fragile_externalofstring|Redundant_mel_string|Deprecated_uncurry_attribute|Deprecated_attribute_namespace|Deprecated_valletkind=function|Unused_attribute_->"unused"|Fragile_external_->"fragile"|Redundant_mel_string->"redundant"|Deprecated_uncurry_attribute|Deprecated_attribute_namespace|Deprecated_val->"deprecated"letppfmtt=matchtwith|Unused_attributes->Format.fprintffmt"Unused attribute [%@%s]@\n\
This means such annotation is not annotated properly.@\n\
For example, some annotations are only meaningful in externals\n"s|Fragile_externals->Format.fprintffmt"%s : the external name is inferred from val name is unsafe from \
refactoring when changing value name"s|Redundant_mel_string->Format.fprintffmt"[@mel.string] is redundant here, you can safely remove it"|Deprecated_uncurry_attribute->Format.fprintffmt"The `[@bs]' uncurry attribute is deprecated and will be removed in \
the next release.@\n\
Use `[@u]' instead."|Deprecated_attribute_namespace->Format.fprintffmt"The `[@bs.*]' attributes are deprecated and will be removed in the\n\
next release.@\n\
Use `[@mel.*]' instead."|Deprecated_val->Format.fprintffmt"`[@mel.val]' attributes are redundant and will be removed in the \
next release.@\n\
Consider removing them from any external declarations."endletwarn~locmsg=letmoduleLocation=Ocaml_common.LocationinLocation.prerr_alertloc{Ocaml_common.Warnings.kind=Warnings.kindmsg;message=Format.asprintf"%a"Warnings.ppmsg;def=Location.none;use=loc;}(** Warning unused bs attributes
Note if we warn `deriving` too,
it may fail third party ppxes
*)letis_bs_attributetxt=letlen=String.lengthtxtin(len=1&&String.unsafe_gettxt0='u')||len>=2(*TODO: check the stringing padding rule, this predicate may not be needed *)&&String.unsafe_gettxt0='b'&&String.unsafe_gettxt1='s'&&(len=2||String.unsafe_gettxt2='.')||String.unsafe_gettxt0='m'&&String.unsafe_gettxt1='e'&&String.unsafe_gettxt2='l'&&String.unsafe_gettxt3='.'letused_attributes:stringAsttypes.locHash_set_poly.t=Hash_set_poly.create16(* #if true *)(* let dump_attribute fmt = (fun (sloc : string Asttypes.loc) -> *)(* Format.fprintf fmt "@[%s @]" sloc.txt (* (Printast.payload 0 ) payload *) *)(* ) *)(* let dump_used_attributes fmt = *)(* Format.fprintf fmt "Used attributes Listing Start:@."; *)(* Hash_set_poly.iter used_attributes (fun attr -> dump_attribute fmt attr) ; *)(* Format.fprintf fmt "Used attributes Listing End:@." *)(* #endif *)(* only mark non-ghost used bs attribute *)letmark_used_bs_attribute({attr_name=x;_}:Parsetree.attribute)=ifnotx.loc.loc_ghostthenHash_set_poly.addused_attributesxletwarn_unused_attribute({attr_name={txt;loc}assloc;_}:Parsetree.attribute):unit=ifis_bs_attributetxt&&(notloc.loc_ghost)&¬(Hash_set_poly.memused_attributessloc)then(* #if true *)(* (*COMMENT*) *)(* dump_used_attributes Format.err_formatter; *)(* dump_attribute Format.err_formatter sloc; *)(* #endif *)warn~loc(Unused_attributetxt)letwarn_discarded_unused_attributes(attrs:Parsetree.attributes)=ifattrs<>[]thenList.iterwarn_unused_attributeattrs(* Note we only used Bs_ast_iterator here, we can reuse compiler-libs instead
of rolling our own *)letemit_external_warnings:Ast_traverse.iter=object(_self)inheritAst_traverse.iterassupermethod!attributeattr=warn_unused_attributeattrmethod!label_declarationlbl=List.iter(funattr->matchattrwith|{attr_name={txt="mel.as"|"bs.as"|"as";_};_}->mark_used_bs_attributeattr|_->())lbl.pld_attributes;super#label_declarationlblendletemit_external_warnings_on_structure(stru:Parsetree.structure)=emit_external_warnings#structurestruletemit_external_warnings_on_signature(sigi:Parsetree.signature)=emit_external_warnings#signaturesigi