text
stringlengths
0
601k
let tab = String . make indent_width ' '
type source_code_t = Lines of string list | Inline of string
let strings_of_source_code bc = begin match bc with | Lines l -> l | Inline i -> [ i ] end
let string_of_source_code bc = begin match bc with | Lines l -> String . concat " \ n " l | Inline i -> i end
let concat_source_code a b = begin match a , b with | Lines l , Lines l2 -> Lines ( l @ l2 ) | Lines l , Inline i -> Lines ( l [ @ i ] ) | Inline i , Lines l -> Lines ( i :: l ) | Inline i , Inline i2 -> Inline ( i ^ i2 ) end
let concat_many_source_code = List . fold_left concat_source_code
let empty_source_code bc = bc = Lines ( [ ] ) || bc = Inline ( " " )
let delim_source_code ( ? preserve = false ) delim bc = begin match bc with | Lines ( l ) -> let x = List . rev l in Lines ( List . rev ( ( ( List . hd x ) ^ delim ) ( :: List . tl x ) ) ) | Inline ( i ) -> if delim = stmt_delimiter && ( not preserve ) then Lines [ i ^ delim ] else Inline ( i ^ delim ) end
let concat_and_delim_source_code delim a b = concat_source_code ( delim_source_code delim a ) b
let concat_and_delim_source_code_list ( ? final = false ) ( ? delim " " ) = bcl = if bcl = [ ] then Inline ( " " ) else let r = let ne = List . filter ( fun x -> not ( empty_source_code x ) ) bcl in match ne with | [ ] -> Inline ( " " ) | [ x ] -> x | h :: t -> List . fold_left ( concat_and_delim_source_code delim ) h ...
let indent_source_code s bc = if empty_source_code bc then bc else begin match bc with | Lines l -> Lines ( List . map ( fun a -> s ^ a ) l ) | Inline i -> Inline ( s ^ i ) end
let unary_op_source_code op bc = match bc with | Lines ( [ ] ) -> Inline ( op ) | Lines ( l :: rest ) -> if ( String . length op ) > ( indent_width * 2 ) then concat_source_code ( Inline ( op ) ) ( indent_source_code tab bc ) else concat_source_code ( Inline ( op ^ l ) ) ( indent_source_code ( String . make ( String . ...
let binary_op_source_code op bc_left bc_right = match ( bc_left , bc_right ) with | ( ( Lines ( [ ] ) ) , _ ) -> unary_op_source_code op bc_right | ( ( Lines ( l ) ) , ( Lines ( [ ] ) ) ) -> Lines ( l [ @ op ] ) | ( ( Inline ( l ) ) , ( Lines ( [ ] ) ) ) -> Inline ( l ^ op ) | ( ( Lines ( ll ) ) , ( Lines ( rl ) ) ) ->...
let parenthesize_source_code lparen rparen bc = match ( unary_op_source_code lparen bc ) with | Lines ( l ) -> Lines ( l [ @ rparen ] ) | Inline ( i ) -> Inline ( i ^ rparen )
let print_mapping ( sm : Source_map . t ) = let sources = Array . of_list sm . sources in let _names = Array . of_list sm . names in List . iter sm . mappings ~ f ( : fun ( m : Source_map . map ) -> let file = function | - 1 -> " null " | n -> normalize_path sources . ( n ) in Printf . printf " % s :% d :% d -> % d :% ...
module T = struct type t = | Attach | NamedSource of string | ParametricSource of { source_name : string ; subkind : string ; } | Transform of { local : TaintTransforms . t ; global : TaintTransforms . t ; base : t ; } [ @@ deriving compare , eq ] let rec pp formatter = function | Attach -> Format . fprintf formatter "...
let ignore_kind_at_call = function | Attach -> true | _ -> false
let apply_call = function | Transform { local ; global ; base } -> Transform { local = TaintTransforms . empty ; global = TaintTransforms . merge ~ local ~ global ; base } | source -> source
module Set = struct include Stdlib . Set . Make ( struct include T end ) let show set = set |> elements |> List . map ~ f : T . show |> String . concat ~ sep " , : " |> Format . asprintf " [ % s ] " let pp format set = Format . fprintf format " % s " ( show set ) let to_sanitize_transforms_exn set = let to_transform = ...
module Map = struct include Stdlib . Map . Make ( struct include T end ) let of_alist_exn = let add map ( key , data ) = update key ( function | None -> Some data | Some _ -> failwith " key already exists " ) map in List . fold ~ init : empty ~ f : add let to_alist map = let gather key data sofar = ( key , data ) :: so...
let discard_subkind = function | ParametricSource { source_name ; _ } -> NamedSource source_name | source -> source
let discard_transforms = function | Transform { base ; _ } -> base | source -> source
let discard_sanitize_transforms = function | Transform { base ; local ; global } -> let local = TaintTransforms . discard_sanitize_transforms local in let global = TaintTransforms . discard_sanitize_transforms global in if TaintTransforms . is_empty local && TaintTransforms . is_empty global then base else Transform { ...
let extract_sanitized_sources_from_transforms transforms = let extract ( SanitizeTransform . Source . Named name ) sources = Set . add ( NamedSource name ) sources in SanitizeTransform . SourceSet . fold extract transforms Set . empty
let extract_sanitize_transforms = function | Transform { local ; global ; _ } -> TaintTransforms . merge ~ local ~ global |> TaintTransforms . get_sanitize_transforms | _ -> SanitizeTransformSet . empty
let rec base_as_sanitizer = function | NamedSource name | ParametricSource { source_name = name ; _ } -> Some ( SanitizeTransform . Source ( SanitizeTransform . Source . Named name ) ) | Transform { base ; _ } -> base_as_sanitizer base | Attach -> None
let apply_sanitize_transforms transforms source = match source with | Attach -> Some Attach | NamedSource _ | ParametricSource _ -> ( match TaintTransforms . of_sanitize_transforms ~ preserve_sanitize_sources : false ~ preserve_sanitize_sinks : true ~ base ( : base_as_sanitizer source ) transforms with | None -> None |...
let apply_transforms transforms order source = match source with | Attach -> Some Attach | NamedSource _ | ParametricSource _ -> ( match TaintTransforms . add_transforms ~ preserve_sanitize_sources : false ~ preserve_sanitize_sinks : true ~ base ( : base_as_sanitizer source ) ~ local : TaintTransforms . empty ~ global ...
let get_named_transforms = function | Transform { local ; global ; _ } -> TaintTransforms . merge ~ local ~ global |> TaintTransforms . get_named_transforms | _ -> [ ]
let contains_sanitize_transforms source sanitize_transforms = SanitizeTransformSet . subset sanitize_transforms ( extract_sanitize_transforms source )
let create_ignore ignored_line codes kind start_line start_column end_line end_column = let location = let start = { Location . line = start_line ; column = start_column } in let stop = { Location . line = end_line ; column = end_column } in { Location . start ; stop } in create ~ ignored_line ~ codes ~ kind ~ location
let test_parse _ = let qualifier = " !& test " in let assert_mode line expected_mode = let { Source . TypecheckFlags . local_mode ; _ } = Source . TypecheckFlags . parse ~ qualifier [ line ] in let show_local_mode = function | Some { Node . value = mode ; _ } -> Source . show_local_mode mode | None -> " None " in asser...
let test_mode _ = let assert_mode ~ configuration local_mode expected_mode = let actual_mode = Source . mode ~ configuration ~ local_mode in assert_equal actual_mode expected_mode in let configuration = Configuration . Analysis . create ~ source_paths [ ] : ( ) in assert_mode ~ configuration None Source . Unsafe ; asse...
let test_qualifier _ = let qualifier = Reference . create_from_list in assert_equal ( ModulePath . qualifier_of_relative " module . py " ) ( qualifier [ " module " ] ) ; assert_equal ( ModulePath . qualifier_of_relative " module / submodule . py " ) ( qualifier [ " module " ; " submodule " ] ) ; assert_equal ( ModulePa...
let test_extension_suffix _ = let root = PyrePath . create_absolute " / root " in let assert_qualifier_equal ~ configuration ~ path expected = let actual_qualifier = match ModulePath . create ~ configuration ( Test . relative_artifact_path ~ root ~ relative : path ) with | Some { ModulePath . qualifier ; _ } -> qualifi...
let test_expand_relative_import _ = let assert_export ~ relative ~ from ~ expected = let from = match parse_single_statement ( " from " ^ from ^ " import something " ) with | { Node . value = Import { Import . from = Some from ; _ } ; _ } -> from | _ -> failwith " Could not parse import " in let source_path = Source . ...
let test_ignored_lines _ = let assert_ignores source expected = assert_equal ~ cmp [ :% compare . equal : Ignore . t list ] ~ printer [ :% show : Ignore . t list ] expected ( Source . ignored_lines_including_format_strings ~ collect_format_strings_with_ignores : Visit . collect_format_strings_with_ignores ( parse sourc...
let ( ) = " metadata " >::: [ " parse " >:: test_parse ; " mode " >:: test_mode ] |> Test . run ; " source " >::: [ " qualifier " >:: test_qualifier ; " extension_suffix " >:: test_extension_suffix ; " expand_relative_import " >:: test_expand_relative_import ; " ignored_lines " >:: test_ignored_lines ; ] |> Test . run
module T = struct type t = Caml . Lexing . position = { pos_fname : string ; pos_lnum : int ; pos_bol : int ; pos_cnum : int } [ @@ deriving_inline compare , hash , sexp_of ] let compare = ( fun a__001_ b__002_ -> if Ppx_compare_lib . phys_equal a__001_ b__002_ then 0 else ( match compare_string a__001_ . pos_fname b__...
let make_location_string ~ pos_fname ~ pos_lnum ~ pos_cnum ~ pos_bol = String . concat [ pos_fname ; " " ; : Int . to_string pos_lnum ; " " ; : Int . to_string ( pos_cnum - pos_bol ) ] ; ;
let to_string { Caml . Lexing . pos_fname ; pos_lnum ; pos_cnum ; pos_bol } = make_location_string ~ pos_fname ~ pos_lnum ~ pos_cnum ~ pos_bol ; ;
let sexp_of_t t = Sexp . Atom ( to_string t )
type t = { range : Position . range ; extra : int ; number_width : int ; pos : Position . t ; line : string ; doc : Print . doc ; }
let of_range ( extra : int ) ( range : Position . range ) : t = assert ( 0 <= extra ) ; assert ( Position . is_valid_range range ) ; let number_width = ( Position . line ( snd range ) + 1 ) |> string_of_int |> String . length in { range ; extra ; number_width ; pos = Position . start ; line = " " ; doc = Pretty . empty...
let of_position ( extra : int ) ( pos : Position . t ) : t = of_range extra ( pos , pos )
let needs_more ( ext : t ) : bool = let _ , p2 = ext . range in Position . ( line ext . pos <= line p2 )
let is_in_range ( p : t ) : bool = let open Position in let pos1 , pos2 = p . range in line pos1 <= line p . pos + p . extra && line p . pos <= line pos2
let is_start_line ( p : t ) : bool = let open Position in let pos1 , pos2 = p . range in line p . pos = line pos1 && line pos1 < line pos2
let is_end_line ( p : t ) : bool = let open Position in let pos1 , pos2 = p . range in line pos1 < line p . pos && line p . pos = line pos2
let is_one_line ( p : t ) : bool = let open Position in let pos1 , pos2 = p . range in line p . pos = line pos1 && line pos1 = line pos2
let source_separator : string = " | "
let source_indent ( p : t ) : int = p . number_width + String . length source_separator
let source_line ( p : t ) : Pretty . doc = let str = Position . line p . pos |> string_of_int in let n = p . number_width - String . length str in assert ( 0 <= n ) ; Pretty . ( fill n ' ' <+> ( Position . line p . pos + 1 |> string_of_int |> text ) <+> text source_separator <+> text p . line <+> cut )
let start_line_marker ( p : t ) : Pretty . doc = let col = Position . column ( fst p . range ) in Pretty . ( fill ( source_indent p + col ) ' ' <+> char ' v ' <+> fill 10 ' ' - <+> cut )
let end_line_marker ( p : t ) : Pretty . doc = let col = Position . column ( snd p . range ) and ind = source_indent p in Pretty . ( fill ind ' ' <+> fill ( col - ind ) ' ' - <+> char ' ' ^ <+> cut )
let one_line_marker ( p : t ) : Pretty . doc = let open Position in let pos1 , pos2 = p . range in let c1 = Position . column pos1 and c2 = Position . column pos2 in assert ( line pos1 = line pos2 ) ; assert ( c1 <= c2 ) ; let len = c2 - c1 in let len = max len 1 in Pretty . ( fill ( source_indent p + c1 ) ' ' <+> fill...
let put ( c : char ) ( p : t ) : t = let pos = Position . next c p . pos in if c <> ' \ n ' then { p with pos ; line = p . line ^ String . make 1 c ; } else if is_in_range p then let open Pretty in let doc = if is_start_line p then start_line_marker p <+> source_line p else if is_one_line p then source_line p <+> one_l...
let put_end ( p : t ) : t = put ' \ n ' p
let document ( p : t ) : Pretty . doc = p . doc
let run_on_stream ( str : char Stream . t ) ( p : t ) : t = let rec run p = if needs_more p then try put ( Stream . next str ) p |> run with Stream . Failure -> put_end p else p in run p
let run_on_string ( str : string ) ( ext : t ) : t = run_on_stream ( Stream . of_string str ) ext
let run_on_channel ( ic : in_channel ) ( ext : t ) : t = run_on_stream ( Stream . of_channel ic ) ext
type map = { gen_line : int ; gen_col : int ; ori_source : int ; ori_line : int ; ori_col : int ; ori_name : int option }
type mapping = map list
type t = { version : int ; file : string ; sourceroot : string option ; mutable sources : string list ; mutable sources_content : string option list option ; mutable names : string list ; mutable mappings : mapping }
let map_line_number ~ f = let f i = if i < 0 then i else f i in fun m -> { m with ori_line = f m . ori_line ; gen_line = f m . gen_line }
let string_of_mapping mapping = let mapping = List . map mapping ~ f ( : map_line_number ~ f : pred ) in let a = Array . of_list mapping in let len = Array . length a in Array . stable_sort ~ cmp ( : fun t1 t2 -> match compare t1 . gen_line t2 . gen_line with | 0 -> compare t1 . gen_col t2 . gen_col | n -> n ) a ; let ...
let mapping_of_string str = let total_len = String . length str in let gen_col = ref 0 in let ori_source = ref 0 in let ori_line = ref 0 in let ori_col = ref 0 in let ori_name = ref 0 in let rec readline line pos acc = if pos >= total_len then List . rev_map acc ~ f ( : map_line_number ~ f : succ ) else let last = try ...
let merge_sources_content a b = match a , b with | Some a , Some b -> Some ( a @ b ) | _ -> None
let maps ~ gen_line_offset ~ sources_offset ~ names_offset x = let gen_line = x . gen_line + gen_line_offset in let ori_source = x . ori_source + sources_offset in let ori_name = match x . ori_name with | None -> None | Some ori_name -> Some ( ori_name + names_offset ) in { x with gen_line ; ori_source ; ori_name }
let merge = function | [ ] -> None | ( gen_line_offset , _file , x ) :: rest -> let rec loop acc ~ sources_offset ~ names_offset l = match l with | [ ] -> acc | ( gen_line_offset , _ , sm ) :: rest -> let acc = { acc with sources = acc . sources @ sm . sources ; names = acc . names @ sm . names ; sources_content = merg...
let empty = { version = 3 ; file = " file " ; sourceroot = None ; sources = [ ] ; sources_content = None ; names = [ ] ; mappings = [ ] }
let json t = let rewrite_path path = if Filename . is_relative path then path else match Build_path_prefix_map . get_build_path_prefix_map ( ) with | Some map -> Build_path_prefix_map . rewrite map path | None -> path in ` Assoc [ " version " , ` Float ( float_of_int t . version ) ; " file " , ` String ( rewrite_path t...
let invalid ( ) = invalid_arg " Source_map . of_json "
let string name rest = try match List . assoc name rest with | ` String s -> Some s | ` Null -> None | _ -> invalid ( ) with Not_found -> None
let list_string name rest = try match List . assoc name rest with | ` List l -> Some ( List . map ( function | ` String s -> s | _ -> invalid ( ) ) l ) | _ -> invalid ( ) with Not_found -> None
let list_string_opt name rest = try match List . assoc name rest with | ` List l -> Some ( List . map ( function | ` String s -> Some s | ` Null -> None | _ -> invalid ( ) ) l ) | _ -> invalid ( ) with Not_found -> None
let of_json json = match json with | ` Assoc ( ( " version " , ` Float version ) :: rest ) when int_of_float version = 3 -> let def v d = match v with | None -> d | Some v -> v in let file = string " file " rest in let sourceroot = string " sourceRoot " rest in let names = list_string " names " rest in let sources = li...
let of_string s = of_json ( Yojson . Basic . from_string s )
let to_string m = Yojson . Basic . to_string ( json m )
let to_file m file = Yojson . Basic . to_file file ( json m )
type tag = | Ml of string | Mli of string | Re of string | Rei of string | Other of ( Target . t * string * string )
let pp ppf = function | Ml file -> Format . fprintf ppf " Ml % s " file | Mli file -> Format . fprintf ppf " Mli % s " file | Re file -> Format . fprintf ppf " Re % s " file | Rei file -> Format . fprintf ppf " Rei % s " file | Other _ -> Format . fprintf ppf " Other "
let tag target filename = match target with | Erlang | Core_erlang -> ( match Filename . extension filename with | " . ml " -> Ml filename | " . mli " -> Mli filename | " . re " -> Re filename | " . rei " -> Rei filename | ext -> Other ( target , filename , ext ) )
let assert_all_sources_are_supported sources = List . iter ( function | Other ( tgt , file , ext ) -> Format . fprintf Format . std_formatter " Attempted to compile % s , but % s files are not supported with the \ target flag : -- target =% s " %! file ext ( Target . to_string tgt ) ; exit 1 | _ -> ( ) ) sources
let prepare ~ sources ~ target = let tagged_sources = List . map ( tag target ) sources in assert_all_sources_are_supported tagged_sources ; let ml_sources = tagged_sources |> List . filter_map ( function | Ml f | Mli f | Re f | Rei f -> Some f | Other _ -> None ) |> List . map ( tag target ) in ml_sources
let ( ) = let verbose = true in let to_list src = List . rev ( Source . fold ( fun xs x -> x :: xs ) [ ] src ) in let t = T . test T . int ~ verbose in T . group " Source . empty " [ t " empty len " ~ actual ( : Source . len Source . empty ) ~ expected : 0 ; ] ; let t = T . test T . ( list int ) ~ verbose in T . group ...
module File = struct module T = struct type t = { ino : int ; dev : int } let to_dyn { ino ; dev } = let open Dyn in record [ ( " ino " , Int . to_dyn ino ) ; ( " dev " , Int . to_dyn dev ) ] let compare { ino ; dev } t = let open Ordering . O in let = ( ) = Int . compare ino t . ino in Int . compare dev t . dev end in...
module Dune_file = struct module Plain = struct type t = { contents : Sub_dirs . Dir_map . per_dir ; for_subdirs : Sub_dirs . Dir_map . t } end let fname = " dune " let alternative_fname = " dune - file " type kind = | Plain | Ocaml_script type t = { path : Path . Source . t ; kind : kind ; plain : Plain . t } let get_...
let filter_source_files = let is_temp_file fn = String . is_prefix fn ~ prefix " . " :# || String . is_suffix fn ~ suffix " . : swp " || String . is_suffix fn ~ suffix " " :~ in ref ( fun _ -> Memo . return ( fun _dir fn -> not ( is_temp_file fn ) ) )
module Readdir : sig type t = private { path : Path . Source . t ; files : String . Set . t ; dirs : ( string * Path . Source . t * File . t ) list } val empty : Path . Source . t -> t val filter_files : t -> Dune_project . t -> t Memo . t val of_source_path : Path . Source . t -> ( t , Unix_error . Detailed . t ) Resu...
module Dirs_visited : sig type t val singleton : Path . Source . t -> File . t -> t module Per_fn : sig type t type dirs_visited val init : t val find : t -> Path . Source . t -> dirs_visited val add : t -> dirs_visited -> string * Path . Source . t * File . t -> t end with type dirs_visited := t type t = Path . Source...
module Output = struct type ' a t = { dir : ' a ; visited : Dirs_visited . Per_fn . t } end
module Dir0 = struct type vcs = | Ancestor_vcs | This of Vcs . t type t = { path : Path . Source . t ; status : Sub_dirs . Status . t ; contents : contents ; project : Dune_project . t ; vcs : vcs } and contents = { files : String . Set . t ; sub_dirs : sub_dir String . Map . t ; dune_file : Dune_file . t option } and ...
let ancestor_vcs = Memo . lazy_ ~ name " : ancestor_vcs " ( fun ( ) -> if Config . inside_dune then Memo . return None else let rec loop dir = if Fpath . is_root dir then None else let dir = Filename . dirname dir in match Sys . readdir dir |> Array . to_list |> String . Set . of_list |> Vcs . Kind . of_dir_contents wi...
module rec Memoized : sig val root : unit -> Dir0 . t Memo . t val find_dir_raw : Path . Source . t -> ( Path . Source . t , Dir0 . t Output . t option ) Memo . Cell . t val find_dir : Path . Source . t -> Dir0 . t option Memo . t open Memoized module Get_subdir : sig val all : dirs_visited : Dirs_visited . t -> dirs (...
let root ( ) = Memoized . root ( )
let find_dir path = Memoized . find_dir path
let rec nearest_dir t = function | [ ] -> Memo . return t | comp :: components -> ( match String . Map . find ( Dir0 . sub_dirs t ) comp with | None -> Memo . return t | Some sub_dir -> let * sub_dir = Dir0 . sub_dir_as_t sub_dir in nearest_dir sub_dir components )
let nearest_dir path = let components = Path . Source . explode path in let * root = root ( ) in nearest_dir root components