123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281(* Yoann Padioleau
*
* Copyright (C) 2014 Facebook
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation, with the
* special exception on linking described in file license.txt.
*
* This library 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 file
* license.txt for more details.
*)openCommonmoduleE=Entity_codemodulePI=Parse_info(*****************************************************************************)(* Prelude *)(*****************************************************************************)(*
* Centralize errors report functions (they did the same in c--).
* Mostly a copy paste of error_php.ml
*
* history:
* - was in check_module.ml
* - was generalized for scheck php
* - introduced ranking via int (but mess)
* - introduced simplified ranking using intermediate rank type
* - fully generalize when introduced graph_code_checker.ml
* - added @Scheck annotation
* - added some false positive deadcode detection
*
* todo:
* - priority to errors, so dead code func more important than dead field
* - factorize code with errors_cpp.ml, errors_php.ml, error_php.ml
*)(*****************************************************************************)(* Globals *)(*****************************************************************************)(* see g_errors below *)(*****************************************************************************)(* Types *)(*****************************************************************************)typeerror={typ:error_kind;loc:Parse_info.token_location;sev:severity;}(* less: Advice | Noisy | Meticulous ? *)andseverity=Fatal|Warninganderror_kind=(* entities *)(* done while building the graph:
* - UndefinedEntity (UseOfUndefined)
* - MultiDefinedEntity (DupeEntity)
*)(* As done by my PHP global analysis checker.
* Never done by compilers, and unusual for linters to do that.
*
* note: OCaml 4.01 now does that partially by locally checking if
* an entity is unused and not exported (which does not require
* global analysis)
*)|Deadcodeofentity|UndefinedDefOfDeclofentity(* really a special case of Deadcode decl *)|UnusedExportofentity(* tge decl*)*Common.filename(* file of def *)(* call sites *)(* should be done by the compiler (ocaml does):
* - TooManyArguments, NotEnoughArguments
* - WrongKeywordArguments
* - ...
*)(* variables *)(* also done by some compilers (ocaml does):
* - UseOfUndefinedVariable
* - UnusedVariable
*)|UnusedVariableofstring*Scope_code.t(* classes *)(* files (include/import) *)(* bail-out constructs *)(* a proper language should not have that *)(* lint *)(* other *)(* todo: should be merged with Graph_code.entity or put in Database_code?*)andentity=(string*Entity_code.entity_kind)typerank=(* Too many FPs for now. Not applied even in strict mode. *)|Never(* Usually a few FPs or too many of them. Only applied in strict mode. *)|OnlyStrict|Less|Ok|Important|ReallyImportant(* @xxx to acknowledge or explain false positives *)typeannotation=|AtScheckofstring(* to detect false positives (we use the Hashtbl.find_all property) *)typeidentifier_index=(string,Parse_info.token_location)Hashtbl.t(*****************************************************************************)(* Pretty printers *)(*****************************************************************************)letstring_of_error_kinderror_kind=matcherror_kindwith|Deadcode(s,kind)->spf"dead %s, %s"(Entity_code.string_of_entity_kindkind)s|UndefinedDefOfDecl(s,kind)->spf"no def found for %s (%s)"s(Entity_code.string_of_entity_kindkind)|UnusedExport((s,kind),file_def)->spf"useless export of %s (%s) (consider forward decl in %s)"s(Entity_code.string_of_entity_kindkind)file_def|UnusedVariable(name,scope)->spf"Unused variable %s, scope = %s"name(Scope_code.string_of_scopescope)(*
let loc_of_node root n g =
try
let info = G.nodeinfo n g in
let pos = info.G.pos in
let file = Filename.concat root pos.PI.file in
spf "%s:%d" file pos.PI.line
with Not_found -> "NO LOCATION"
*)letstring_of_errorerr=letpos=err.locinspf"%s:%d: %s"pos.PI.filepos.PI.line(string_of_error_kinderr.typ)(*****************************************************************************)(* Main entry points *)(*****************************************************************************)letg_errors=ref[]letfatallocerr=Common.push{loc=loc;typ=err;sev=Fatal}g_errorsletwarninglocerr=Common.push{loc=loc;typ=err;sev=Warning}g_errors(*****************************************************************************)(* Ranking *)(*****************************************************************************)letscore_of_rank=function|Never->0|OnlyStrict->1|Less->2|Ok->3|Important->4|ReallyImportant->5letrank_of_errorerr=matcherr.typwith|Deadcode(_s,kind)->(matchkindwith|E.Function->Ok(* or enable when use propagate_uses_of_defs_to_decl in graph_code *)|E.GlobalExtern|E.Prototype->Less|_->Ok)(* probably defined in assembly code? *)|UndefinedDefOfDecl_->Important(* we want to simplify interfaces as much as possible! *)|UnusedExport_->ReallyImportant|UnusedVariable_->Lessletscore_of_errorerr=err+>rank_of_error+>score_of_rank(*****************************************************************************)(* False positives *)(*****************************************************************************)letadjust_errorsxs=xs+>Common.exclude(funerr->letfile=err.loc.PI.fileinmatcherr.typwith|Deadcode(s,kind)->(matchkindwith|E.Dir|E.File->true(* kencc *)|E.Prototypewhens="SET"||s="USED"->true(* FP in graph_code_clang for now *)|E.Typewhens=~"E__anon"->true|E.Typewhens=~"U__anon"->true|E.Typewhens=~"S__anon"->true|E.Typewhens=~"E__"->true|E.Typewhens=~"T__"->true(* FP in graph_code_c for now *)|E.Typewhens=~"U____anon"->true(* TODO: to remove, but too many for now *)|E.Constructor|E.Field->true(* hmm plan9 specific? being unused for one project does not mean
* it's not used by another one.
*)|_whenfile=~"^include/"->true|_whenfile=~"^EXTERNAL/"->true(* too many FP on dynamic lang like PHP *)|E.Method->true|_->false)(* kencc *)|UndefinedDefOfDecl(("SET"|"USED"),_)->true|UndefinedDefOfDecl_->(* hmm very plan9 specific *)file=~"^include/"||file="kernel/lib/lib.h"||file="kernel/network/ip/ip.h"||file=~"kernel/conf/"||false|_->false)(*****************************************************************************)(* Annotations *)(*****************************************************************************)letannotation_of_line_opts=ifs=~".*@\\([A-Za-z_]+\\):[ ]?\\([^@]*\\)"thenlet(kind,explain)=Common.matched2sinSome(matchkindwith|"Scheck"->AtScheckexplain|s->failwith("Bad annotation: "^s))elseNone(* The user can override the checks by adding special annotations
* in the code at the same line than the code it related to.
*)letannotation_at2loc=letfile=loc.PI.fileinletline=max(loc.PI.line-1)1inmatchCommon2.cat_excerptsfile[line]with|[s]->annotation_of_line_opts|_->failwith(spf"wrong line number %d in %s"linefile)letannotation_ata=Common.profile_code"Errors_code.annotation"(fun()->annotation_at2a)