Skip to content

Commit

Permalink
minor bug fixes so the tool doesnt crash during analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0mz committed Sep 8, 2024
1 parent 6edfd61 commit b809f4e
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 81 deletions.
11 changes: 8 additions & 3 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let setup_node (mode : string) : string =
script


let main (filename : string) (output_path : string) (config_path : string) (mode : string) (generate_mdg : bool) (verbose : bool) : int =
let main (filename : string) (output_path : string) (config_path : string) (mode : string) (generate_mdg : bool) (no_dot : bool) (verbose : bool) : int =

(* SETUP *)
let script = setup_node mode in
Expand Down Expand Up @@ -107,7 +107,8 @@ let main (filename : string) (output_path : string) (config_path : string) (mode
if generate_mdg then (
let main = DependencyTree.get_main dep_tree in
let graph = ModuleGraphs.get module_graphs main in
Mdg.Pp.Dot.output graph_dir graph;
if not no_dot then
Mdg.Pp.Dot.output graph_dir graph;
Mdg.Pp.CSV.output graph_dir graph;
);

Expand All @@ -134,6 +135,10 @@ let mdg : bool Term.t =
let doc = "Generates Multiversion Dependency Graph." in
Arg.(value & flag & info ["mdg"] ~doc)

let no_dot : bool Term.t =
let doc = "Dont generate .dot and .svg graph representation." in
Arg.(value & flag & info ["noDot"] ~doc)

let output_path : string Term.t =
let doc = "Path to store all output files." in
let default_path = "graphjs-results" in
Expand All @@ -149,7 +154,7 @@ let verbose : bool Term.t =
Arg.(value & flag & info ["v"; "verbose"] ~doc)

let cli =
let cmd = Term.(const main $ input_file $ output_path $ config_path $ mode $ mdg $ verbose) in
let cmd = Term.(const main $ input_file $ output_path $ config_path $ mode $ mdg $ no_dot $ verbose) in
let info = Cmd.info "ast_gen" in
Cmd.v info cmd

Expand Down
58 changes: 38 additions & 20 deletions lib/ast/normalize.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ and normalize_statement (context : context) (stmt : ('M, 'T) Ast'.Statement.t) :
let loc_f = Location.convert_flow_loc !file_path in

let nec = normalize_expression in

match stmt with
(* --------- B L O C K --------- *)
| _, Ast'.Statement.Block {body; _} ->
Expand Down Expand Up @@ -196,9 +196,7 @@ and normalize_statement (context : context) (stmt : ('M, 'T) Ast'.Statement.t) :

(* --------- V A R I A B L E D E C L A R A T I O N --------- *)
| loc, Ast'.Statement.VariableDeclaration {kind; declarations; _} ->
let kind' : Statement.VarDecl.kind = match kind with
| Var -> _var | Let -> _let | Const -> _const
in
let kind' = translate_kind kind in

let new_context = {context with is_declaration = true} in
let assign_stmts, ids = List.split (List.map
Expand Down Expand Up @@ -455,10 +453,19 @@ and normalize_expression (context : context) (expr : ('M, 'T) Ast'.Expression.t)

(* --------- Y I E L D --------- *)
| loc, Ast'.Expression.Yield {argument; delegate; _} ->
let loc = loc_f loc in
let id = if not context.has_op then get_identifier loc context.identifier else Identifier.build_random loc in

let arg_stmts, arg_expr = map_default ne ([], None) argument in
let yield = Statement.Yield.build (loc_f loc) arg_expr delegate in
let yield = Statement.AssignYield.build loc id arg_expr delegate in

(* check if yield is done as a statement or an expression *)
if not context.is_assignment || context.has_op then
let _, decl = createVariableDeclaration None loc ~objId:(Id id) in
(decl @ arg_stmts @ [yield] , Some (Identifier.to_expression id))
else
arg_stmts @ [yield], Some (Identifier.to_expression id)

arg_stmts @ [yield], None

(* --------- C O N D I T I O N A L --------- *)
| loc, Ast'.Expression.Conditional {test; consequent; alternate; _} ->
Expand Down Expand Up @@ -486,17 +493,15 @@ and normalize_expression (context : context) (expr : ('M, 'T) Ast'.Expression.t)

(* --------- A S S I G N S I M P L E --------- *)
| _, Ast'.Expression.Assignment {operator; left; right; _} ->

let operator' = Option.map Operator.Assignment.translate operator in
let assign_stmts, _ = normalize_assignment context left operator' right in
let assign_stmts, _ = normalize_assignment {context with is_statement = false} left operator' right in

(* check if the assignment is done as a statement or an expression *)
let stmts, expr = if not context.is_statement
then get_pattern_expr left
else [], None
in

assign_stmts @ stmts, expr
if context.is_statement
then assign_stmts, None
else
let norm_stmts, norm_expr = get_pattern_expr left in
assign_stmts @ norm_stmts, norm_expr

(* --------- A S S I G N A R R A Y ---------*)
| loc, Ast'.Expression.Array {elements; _} ->
Expand Down Expand Up @@ -748,7 +753,7 @@ and get_pattern_expr (pattern : ('M, 'T) Ast'.Pattern.t) : norm_expr_t =
| _, Identifier {name; _} ->
[], Some ((Identifier.to_expression << normalize_identifier) name)

| _, Expression (loc, Member {_object; property; _}) ->
| _, Expression (loc, Member {_object; property; _}) ->
let loc = loc_f loc in
let id, decl = createVariableDeclaration None loc in

Expand Down Expand Up @@ -850,12 +855,17 @@ and normalize_imp_specifiers (loc : m) (source : string) (specifier : ('M, 'T) A
[import]

and normalize_for_left (left : ('M, 'T) generic_left) : norm_stmt_t * m Statement.VarDecl.t =
let ns = normalize_statement empty_context in
match left with
| LeftDeclaration (loc, decl) ->
let decl_stmts = ns (loc, Ast'.Statement.VariableDeclaration decl) in
[], to_var_decl (List.hd decl_stmts)

| LeftDeclaration (_, { Ast'.Statement.VariableDeclaration.kind; declarations; _}) ->
let kind' = translate_kind kind in
let declaration = List.hd declarations in
let pattern = match declaration with _, {id; _} -> id in

let id, decl_stmts = createVariableDeclaration None (Location.empty ()) ~kind:kind' in
let stmts, _ = normalize_pattern (Identifier.to_expression id) pattern None false in
stmts, to_var_decl (List.hd decl_stmts)


| LeftPattern pattern ->
let id, decl_stmts = createVariableDeclaration None (Location.empty ()) in
let stmts, _ = normalize_pattern (Identifier.to_expression id) pattern None false in
Expand Down Expand Up @@ -1141,6 +1151,8 @@ and is_special_assignment ((_, expr) : ('M, 'T) Ast'.Expression.t) : bool =
| Ast'.Expression.Logical _
| Ast'.Expression.Update _
| Ast'.Expression.Unary _
(* -- ASSIGN YIELD -- *)
| Ast'.Expression.Yield _
(* -- ASSIGN NEW -- *)
| Ast'.Expression.New _
(* -- ASSIGN CALL -- *)
Expand Down Expand Up @@ -1175,6 +1187,12 @@ and change_kind (kind' : Statement.VarDecl.kind) ((loc, stmt) : m Statement.t) :
| VarDecl decl -> let decl' = Statement.VarDecl {decl with kind = kind'} in
(loc, decl')
| _ -> failwith "[ERROR] Tried to change the kind of non-declaration statement"

and translate_kind (kind : Ast'.Variable.kind): Statement.VarDecl.kind =
match kind with
| Var -> _var
| Let -> _let
| Const -> _const

and is_declaration ((_, stmt) : m Statement.t) : bool =
match stmt with
Expand Down
9 changes: 5 additions & 4 deletions lib/ast/pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ module Js = struct
let label' = map_default ((^) " " << print_identifier) "" label in
identation_str ^ "break" ^ label' ^ ";\n"

| _, Yield {argument; _ } ->
let argument' = map_default ((^) " " << print_expr) "" argument in
identation_str ^ "yield" ^ argument' ^ ";\n"

| _, Continue {label} ->
let label' = map_default ((^) " " << print_identifier) "" label in
identation_str ^ "continue" ^ label' ^ ";\n"
Expand Down Expand Up @@ -162,6 +158,11 @@ module Js = struct
let argument' = print_expr argument in
identation_str ^ left' ^ " = " ^ operator' ^ argument' ^ ";\n"

| _, AssignYield {left; argument; _ } ->
let left' = print_identifier left in
let argument' = map_default ((^) " " << print_expr) "" argument in
identation_str ^ left' ^ " = yield" ^ argument' ^ ";\n"

| _, AssignArray {left; _} ->
let left' = print_identifier left in
identation_str ^ left' ^ " = [];\n"
Expand Down
68 changes: 34 additions & 34 deletions lib/ast/structures/grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,6 @@ and Statement : sig
val build : 'M -> 'M Identifier.t option -> 'M Statement.t
end

module Yield : sig
type 'M t = {
argument : 'M Expression.t option;
delegate : bool
}

val build : 'M -> 'M Expression.t option -> bool -> 'M Statement.t
end

module Continue : sig
type 'M t = { label : 'M Identifier.t option }
val build : 'M -> 'M Identifier.t option -> 'M Statement.t
Expand Down Expand Up @@ -379,6 +370,18 @@ and Statement : sig
val build : 'M -> 'M Identifier.t -> Operator.Unary.t -> 'M Expression.t -> 'M Statement.t
end

module AssignYield : sig
type 'M t = {
id : int;
left : 'M Identifier.t;
(* -- right -- *)
argument : 'M Expression.t option;
delegate : bool
}

val build : 'M -> 'M Identifier.t -> 'M Expression.t option -> bool -> 'M Statement.t
end

module AssignArray : sig
type 'M t = {
id : int;
Expand Down Expand Up @@ -542,7 +545,6 @@ and Statement : sig
| Return of 'M Return.t
| Throw of 'M Throw.t
| Break of 'M Break.t
| Yield of 'M Yield.t
| Continue of 'M Continue.t
| Debugger of Debugger.t

Expand All @@ -555,6 +557,7 @@ and Statement : sig
| AssignSimple of 'M AssignSimple.t
| AssignBinary of 'M AssignBinary.t
| AssignUnary of 'M AssignUnary.t
| AssignYield of 'M AssignYield.t
| AssignArray of 'M AssignArray.t
| AssignObject of 'M AssignObject.t
| StaticUpdate of 'M StaticUpdate.t
Expand All @@ -567,8 +570,7 @@ and Statement : sig
| AssignMetCallDynmic of 'M AssignMetCallDynmic.t
| AssignFunction of 'M AssignFunction.t

type 'M t = 'M * 'M t'

type 'M t = 'M * 'M t'

end = struct

Expand Down Expand Up @@ -790,20 +792,6 @@ end = struct
(metadata, break_info)
end

module Yield = struct
type 'M t = {
argument : 'M Expression.t option;
delegate : bool
}

let build (metadata : 'M) (argument' : 'M Expression.t option) (delegate': bool) : 'M Statement.t =
let yield_info = Statement.Yield {
argument = argument';
delegate = delegate'
} in
(metadata, yield_info)
end

module Continue = struct
type 'M t = { label : 'M Identifier.t option }

Expand Down Expand Up @@ -949,6 +937,25 @@ end = struct
(metadata, assign_info)
end

module AssignYield = struct
type 'M t = {
id : int;
left : 'M Identifier.t;
(* -- right -- *)
argument : 'M Expression.t option;
delegate : bool
}

let build (metadata : 'M) (left' : 'M Identifier.t) (argument' : 'M Expression.t option) (delegate': bool) : 'M Statement.t =
let yield_info = Statement.AssignYield {
id = get_id ();
left = left';
argument = argument';
delegate = delegate'
} in
(metadata, yield_info)
end

module AssignObject = struct

type 'M t = {
Expand Down Expand Up @@ -1188,7 +1195,6 @@ end = struct
| Return of 'M Return.t
| Throw of 'M Throw.t
| Break of 'M Break.t
| Yield of 'M Yield.t
| Continue of 'M Continue.t
| Debugger of Debugger.t

Expand All @@ -1201,6 +1207,7 @@ end = struct
| AssignSimple of 'M AssignSimple.t
| AssignBinary of 'M AssignBinary.t
| AssignUnary of 'M AssignUnary.t
| AssignYield of 'M AssignYield.t
| AssignArray of 'M AssignArray.t
| AssignObject of 'M AssignObject.t
| StaticUpdate of 'M StaticUpdate.t
Expand Down Expand Up @@ -1266,7 +1273,6 @@ and Expression : sig
val build : 'M -> 'M Expression.t
end

val get_id : 'M Expression.t -> string
val get_id_opt : 'M Expression.t -> string option

type 'M t' =
Expand Down Expand Up @@ -1340,12 +1346,6 @@ end = struct
let build (metadata: 'M) : 'M Expression.t =
(metadata, Expression.This ())
end

let get_id (expr : 'M Expression.t) : string =
match expr with
| _, Identifier {name; _} -> name
| _, This _ -> "this"
| _ -> failwith "[ERROR] Expression cannot be converted into an id"

let get_id_opt (expr : 'M Expression.t) : string option =
match expr with
Expand Down
8 changes: 7 additions & 1 deletion lib/auxiliary/functions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ let option_may f x : unit =
let hd_opt (lst : 'a list) : 'a option =
match lst with
| [] -> None
| fst::_ -> Some fst
| fst::_ -> Some fst

let split3 (lst : ('a * 'b * 'c) list) : 'a list * 'b list * 'c list =
let rec aux lst (xs, ys, zs) = match lst with
| [] -> (xs, ys, zs)
| (x, y, z)::tail -> aux tail (x::xs, y::ys, z::zs)
in aux lst ([], [], [])
Loading

0 comments on commit b809f4e

Please sign in to comment.