123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161(* Yoann Padioleau
*
* Copyright (C) 2012 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.
*)openCommon(*****************************************************************************)(* Prelude *)(*****************************************************************************)(*
* It is often useful to skip certain parts of a codebase. Large codebase
* often contains special code that can not be parsed, that contains
* dependencies that should not exist, old code that we don't want
* to analyze, etc.
*
* todo: simplify interface in skip_list.txt file? can infer
* dir or file, and maybe sometimes instead of skip we would like
* to specify the opposite, what we want to keep, so maybe a simple
* +/- syntax would be better.
*)(*****************************************************************************)(* Types *)(*****************************************************************************)(* the filename are in readable path format *)typeskip=|DirofCommon.dirname|FileofCommon.filename|DirElementofCommon.dirname|SkipErrorsDirofCommon.dirname(*****************************************************************************)(* IO *)(*****************************************************************************)letloadfile=Common.catfile|>Common.exclude(funs->s=~"#.*"||s=~"^[ \t]*$")|>List.map(funs->matchswith|_whens=~"^dir:[ ]*\\([^ ]+\\)"->Dir(Common.matched1s)|_whens=~"^skip_errors_dir:[ ]*\\([^ ]+\\)"->SkipErrorsDir(Common.matched1s)|_whens=~"^file:[ ]*\\([^ ]+\\)"->File(Common.matched1s)|_whens=~"^dir_element:[ ]*\\([^ ]+\\)"->DirElement(Common.matched1s)|_->failwith("wrong line format in skip file: "^s))(*****************************************************************************)(* Main entry point *)(*****************************************************************************)(* less: say when skipped stuff? *)letfilter_filesskip_listrootxs=letskip_files=skip_list|>Common.map_filter(function|Files->Somes|_->None)|>Common.hashset_of_listinletskip_dirs=skip_list|>Common.map_filter(function|Dirs->Somes|_->None)inletskip_dir_elements=skip_list|>Common.map_filter(function|DirElements->Somes|_->None)inxs|>Common.exclude(funfile->letreadable=Common.readable~rootfilein(Hashtbl.memskip_filesreadable)||(skip_dirs|>List.exists(fundir->readable=~(dir^".*")))||(skip_dir_elements|>List.exists(fundir->readable=~(".*/"^dir^"/.*"))))(* copy paste of h_version_control/git.ml *)letfind_vcs_root_from_absolute_pathfile=letxs=Common.split"/"(Common2.dirnamefile)inletxxs=Common2.initsxsinxxs|>List.rev|>Common.find_some(funxs->letdir="/"^Common.join"/"xsinifSys.file_exists(Filename.concatdir".git")||Sys.file_exists(Filename.concatdir".hg")||falsethenSomedirelseNone)letfind_skip_file_from_rootroot=letcandidates=["skip_list.txt";(* fbobjc specific *)"Configurations/Sgrep/skip_list.txt";(* www specific *)"conf/codegraph/skip_list.txt";]incandidates|>Common.find_some(funf->letfull=Filename.concatrootfinifSys.file_existsfullthenSomefullelseNone)letfilter_files_if_skip_listxs=matchxswith|[]->[]|x::_->tryletroot=find_vcs_root_from_absolute_pathxinletskip_file=find_skip_file_from_rootrootinletskip_list=loadskip_fileinpr2(spf"using skip list in %s"skip_file);filter_filesskip_listrootxswithNot_found->xs(*****************************************************************************)(* Helpers *)(*****************************************************************************)letbuild_filter_errors_fileskip_list=letskip_dirs=skip_list|>Common.map_filter(function|SkipErrorsDirdir->Somedir|_->None)in(funreadable->skip_dirs|>List.exists(fundir->readable=~("^"^dir)))letreorder_files_skip_errors_lastskip_listrootxs=letis_file_want_to_skip_error=build_filter_errors_fileskip_listinlet(skip_errors,ok)=xs|>List.partition(funfile->letreadable=Common.readable~rootfileinis_file_want_to_skip_errorreadable)inok@skip_errors