Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Record Java extension and accent #2944

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions semgrep-core/src/api/AST_generic_to_v1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,10 @@ and
cimplements = v_cimplements;
cbody = v_cbody;
cmixins = v_cmixins;
cparams = v_cparams;
} =
let v_cbody = map_bracket (map_of_list map_field) v_cbody in
let v_cparams = map_parameters v_cparams in
let v_cmixins = map_of_list map_type_ v_cmixins in
let v_cimplements = map_of_list map_type_ v_cimplements in
let v_cextends = map_of_list map_type_ v_cextends in
Expand All @@ -935,6 +937,7 @@ and
cimplements = v_cimplements;
cbody = v_cbody;
cmixins = v_cmixins;
cparams = v_cparams;
}

and map_class_kind = function
Expand All @@ -943,6 +946,7 @@ and map_class_kind = function
| Trait -> `Trait
| Object -> `Object
| AtInterface -> `AtInterface
| RecordClass -> `RecordClass

and map_directive =
function
Expand Down
4 changes: 4 additions & 0 deletions semgrep-core/src/api/AST_generic_v1.atd
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,8 @@ type class_definition = {
(* class_kind in type_ is usually a Trait *)
cmixins: type_ list; (* PHP 'uses' *)

(* for Java Record or Scala Classes; we could transpile them into fields *)
cparams: parameters;
(* newscope: note: this can be an empty fake bracket when used in Partial.*)
cbody: field list bracket;
}
Expand All @@ -944,6 +946,8 @@ type class_kind = [
| Trait
(* Kotlin, Scala *)
| Object
(* Java 'record', Scala 'case class' *)
| RecordClass
(* java: *)
| AtInterface (* @interface, a.k.a annotation type declaration *)
]
Expand Down
4 changes: 4 additions & 0 deletions semgrep-core/src/core/ast/AST_generic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,8 @@ and class_definition = {
(* class_kind in type_ is usually a Trait *)
cmixins: type_ list; (* PHP 'uses' *)

(* for Java Record or Scala Classes; we could transpile them into fields *)
cparams: parameters;
(* newscope:
* note: this can be an empty fake bracket when used in Partial.
* TODO? use an option here?
Expand All @@ -1515,6 +1517,8 @@ and class_kind =
| Trait
(* Kotlin, Scala *)
| Object
(* Java 'record', Scala 'case class' *)
| RecordClass
(* java: *)
| AtInterface (* @interface, a.k.a annotation type declaration *)
(*e: type [[AST_generic.class_kind]] *)
Expand Down
3 changes: 3 additions & 0 deletions semgrep-core/src/core/ast/Map_AST.ml
Original file line number Diff line number Diff line change
Expand Up @@ -853,18 +853,21 @@ let (mk_visitor: visitor_in -> visitor_out) = fun vin ->
cimplements = v_cimplements;
cbody = v_cbody;
cmixins = v_cmixins;
cparams;
} =
let v_cbody = map_bracket (map_of_list map_field) v_cbody in
let v_cmixins = map_of_list map_type_ v_cmixins in
let v_cimplements = map_of_list map_type_ v_cimplements in
let v_cextends = map_of_list map_type_ v_cextends in
let v_ckind = map_class_kind v_ckind in
let cparams = map_parameters cparams in
{
ckind = v_ckind;
cextends = v_cextends;
cimplements = v_cimplements;
cbody = v_cbody;
cmixins = v_cmixins;
cparams;
}

and map_class_kind (x,t) =
Expand Down
5 changes: 5 additions & 0 deletions semgrep-core/src/core/ast/Meta_AST.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1139,11 +1139,15 @@ and
cimplements = v_cimplements;
cbody = v_cbody;
cmixins = v_cmixins;
cparams;
} =
let bnds = [] in
let arg = vof_bracket (OCaml.vof_list vof_field) v_cbody in
let bnd = ("cbody", arg) in
let bnds = bnd :: bnds in
let arg = vof_parameters cparams in
let bnd = ("cparams", arg) in
let bnds = bnd :: bnds in
let arg = OCaml.vof_list vof_type_ v_cmixins in
let bnd = ("cmixins", arg) in
let bnds = bnd :: bnds in
Expand All @@ -1163,6 +1167,7 @@ and vof_class_kind_bis =
| Trait -> OCaml.VSum ("Trait", [])
| AtInterface -> OCaml.VSum ("AtInterface", [])
| Object -> OCaml.VSum ("Object", [])
| RecordClass -> OCaml.VSum ("RecordClass", [])
and vof_ident_and_id_info (v1, v2) =
let v1 = vof_ident v1 in
let v2 = vof_id_info v2 in
Expand Down
2 changes: 2 additions & 0 deletions semgrep-core/src/core/ast/Visitor_AST.ml
Original file line number Diff line number Diff line change
Expand Up @@ -880,11 +880,13 @@ let (mk_visitor: visitor_in -> visitor_out) = fun vin ->
cimplements = v_cimplements;
cmixins = v_mixins;
cbody = v_cbody;
cparams;
} =
let arg = v_class_kind v_ckind in
let arg = v_list v_type_ v_cextends in
let arg = v_list v_type_ v_cimplements in
let arg = v_list v_type_ v_mixins in
v_parameters cparams;
let arg = v_bracket (v_list v_field) v_cbody in
()
in
Expand Down
16 changes: 9 additions & 7 deletions semgrep-core/src/matching/Generic_vs_generic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2553,19 +2553,20 @@ and m_list__m_type_any_order (xsa: A.type_ list) (xsb: A.type_ list) =
(*s: function [[Generic_vs_generic.m_class_definition]] *)
and m_class_definition a b =
match a, b with
{ A. ckind = a1; cextends = a2; cimplements = a3; cbody = a4;
cmixins = a5;
{ A. ckind = a1; cextends = a2; cimplements = a3; cmixins = a5;
cbody = a4; cparams = a6;
},
{ B. ckind = b1; cextends = b2; cimplements = b3; cbody = b4;
cmixins = b5;
{ B. ckind = b1; cextends = b2; cimplements = b3; cmixins = b5;
cbody = b4; cparams = b6;
} ->
m_class_kind a1 b1 >>= (fun () ->
(* TODO: use also m_list_in_any_order? regressions for python? *)
(m_list__m_type_) a2 b2 >>= (fun () ->
(m_list__m_type_any_order) a3 b3 >>= (fun () ->
(m_list__m_type_any_order) a5 b5 >>= (fun () ->
m_bracket (m_fields) a4 b4
))))
m_parameters a6 b6 >>= (fun () ->
m_bracket (m_fields) a4 b4
)))))
(*e: function [[Generic_vs_generic.m_class_definition]] *)

(*s: function [[Generic_vs_generic.m_class_kind]] *)
Expand All @@ -2577,9 +2578,10 @@ and m_class_kind_bis a b =
| A.Trait, B.Trait
| A.AtInterface, B.AtInterface
| A.Object, B.Object
| A.RecordClass, B.RecordClass
-> return ()
| A.Class, _ | A.Interface, _ | A.Trait, _
| A.AtInterface, _ | A.Object, _
| A.AtInterface, _ | A.Object, _ | A.RecordClass, _
-> fail ()
(*e: function [[Generic_vs_generic.m_class_kind]] *)

Expand Down
3 changes: 1 addition & 2 deletions semgrep-core/src/parsing/Parse_target.ml
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,8 @@ let just_parse_with_lang lang file =
* an invoke because of a segfault/memory-leak), but when both parsers
* fail, it's better to give the tree-sitter parsing error now.
*)
Pfff (throw_tokens Parse_java.parse);
(* TODO: move before, but need fix tree-sitter-java first *)
TreeSitter Parse_java_tree_sitter.parse;
Pfff (throw_tokens Parse_java.parse);
]
Java_to_generic.program
| Lang.Go ->
Expand Down
2 changes: 1 addition & 1 deletion semgrep-core/src/parsing/pfff/Python_to_generic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ and stmt_aux x =
in
let ent = G.basic_entity v1 v4 in
let def = { G.ckind = (G.Class, v0); cextends = v2;
cimplements = []; cmixins = [];
cimplements = []; cmixins = []; cparams = [];
cbody = fb (v3 |> List.map(fun x ->G.FieldStmt x);)
} in
[G.DefStmt (ent, G.ClassDef def) |> G.s]
Expand Down
25 changes: 15 additions & 10 deletions semgrep-core/src/parsing/pfff/java_to_generic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,13 @@ and expr e =
(match v3 with
| None -> G.Call (G.IdSpecial (G.New, v0), (lp,(G.ArgType v1)::v2,rp))
| Some decls ->
let anonclass = G.AnonClass { G.
ckind = (G.Class, v0);
cextends = [v1];
cimplements = []; cmixins = [];
cbody = decls |> bracket (List.map (fun x -> G.FieldStmt x))
}
let anonclass =
G.AnonClass { G.
ckind = (G.Class, v0);
cextends = [v1];
cimplements = []; cmixins = []; cparams = [];
cbody = decls |> bracket (List.map (fun x -> G.FieldStmt x))
}
in
G.Call (G.IdSpecial (G.New, v0), (lp,(G.Arg anonclass)::v2,rp))
)
Expand Down Expand Up @@ -315,7 +316,7 @@ and expr e =
let v2 = typ v2 in
G.TypedMetavar (v1, Parse_info.fake_info " ", v2)
| Lambda (v1, t, v2) ->
let v1 = params v1 in
let v1 = parameters v1 in
let v2 = stmt v2 in
G.Lambda { G.fparams = v1; frettype = None; fbody = v2;
fkind = (G.Arrow, t); }
Expand Down Expand Up @@ -455,7 +456,7 @@ and init =
| ArrayInit v1 -> let v1 = bracket (list init) v1 in
G.Container (G.Array, v1)

and params v = List.map parameter_binding v
and parameters v = List.map parameter_binding v
and parameter_binding = function
| ParamClassic v | ParamReceiver v ->
let (ent, t) = var v in
Expand All @@ -474,7 +475,7 @@ and
m_body = m_body
} =
let ent, rett = var m_var in
let v2 = params m_formals in
let v2 = parameters m_formals in
let v3 = list typ m_throws in
let v4 = stmt m_body in
let throws = v3 |> List.map (fun t ->
Expand Down Expand Up @@ -516,14 +517,16 @@ and class_decl {
cl_mods = cl_mods;
cl_extends = cl_extends;
cl_impls = cl_impls;
cl_body = cl_body
cl_body = cl_body;
cl_formals;
} =
let v1 = ident cl_name in
let v2 = class_kind cl_kind in
let v3 = list type_parameter cl_tparams in
let v4 = modifiers cl_mods in
let v5 = option typ cl_extends in
let v6 = list ref_type cl_impls in
let cparams = parameters cl_formals in
let v7 = class_body cl_body in
let fields = v7 |> bracket (List.map (fun x -> G.FieldStmt x)) in
let ent = { (G.basic_entity v1 v4) with
Expand All @@ -533,6 +536,7 @@ and class_decl {
cextends = Common.opt_to_list v5;
cimplements = v6;
cmixins = [];
cparams;
cbody = fields;
} in
ent, cdef
Expand All @@ -544,6 +548,7 @@ and class_kind (x, t) =
| ClassRegular -> G.Class
| Interface -> G.Interface
| AtInterface -> G.AtInterface
| Record -> G.RecordClass
), t

and decl decl =
Expand Down
2 changes: 1 addition & 1 deletion semgrep-core/src/parsing/pfff/js_to_generic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ and class_ { c_extends; c_implements; c_body; c_kind; c_attrs; } =
let attrs = list attribute c_attrs in
let cimplements = list type_ c_implements in
{ G.ckind = H.conv_class_kind c_kind; cextends; cimplements;
cmixins = []; cbody = v2;}, attrs
cmixins = []; cparams = []; cbody = v2;}, attrs


and field_classic {fld_name=v1; fld_attrs = v2; fld_type = vt; fld_body = v3} =
Expand Down
1 change: 1 addition & 0 deletions semgrep-core/src/parsing/pfff/php_to_generic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ and class_def {
cextends = extends |> Common.opt_to_list;
cimplements = implements;
cmixins = uses;
cparams = [];
cbody = t1,
fields |> List.map (fun def ->
G.FieldStmt (G.DefStmt def |> G.s)
Expand Down
2 changes: 1 addition & 1 deletion semgrep-core/src/parsing/pfff/ruby_to_generic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ and definition def =
in
let def = { G.ckind = (G.Class, t); cextends = extends;
(* TODO: this is done by special include/require builtins *)
cimplements = []; cmixins = [];
cimplements = []; cmixins = []; cparams = [];
cbody = fb ([G.FieldStmt body]);
} in
G.DefStmt (ent, G.ClassDef def) |> G.s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ and expression (env : env) (x : CST.expression) : AST.expr =
let v5 = token env v5 (* "}" *) in
AnonClass {
ckind = (Class, v1);
cextends = []; cimplements = []; cmixins = [];
cextends = []; cimplements = []; cmixins = []; cparams = [];
cbody = (v2, v3, v5);
}
| `Array_crea_exp (v1, v2, v3) ->
Expand Down Expand Up @@ -2496,8 +2496,7 @@ and class_interface_struct (env : env) class_kind (v1, v2, v3, v4, v5, v6, v7, v
AST.DefStmt (ent, AST.ClassDef {
ckind = (class_kind, v3);
cextends = v6;
cimplements = [];
cmixins = [];
cimplements = []; cmixins = []; cparams = [];
cbody = (open_bra, fields, close_bra);
}) |> AST.s

Expand Down
20 changes: 17 additions & 3 deletions semgrep-core/src/parsing/tree_sitter/Parse_java_tree_sitter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1283,8 +1283,22 @@ and enum_body (env : env) ((v1, v2, v3, v4, v5) : CST.enum_body) =
let _v5 = token env v5 (* "}" *) in
v2, v4

and record_declaration (env : env) ((v1, v2, v3, v4, v5) : CST.record_declaration) : class_decl =
let v1 =
(match v1 with
| Some x -> modifiers env x
| None -> [])
in
let v2 = token env v2 (* "record" *) in
let v3 = identifier env v3 (* pattern [a-zA-Z_]\w* *) in
let v4 = formal_parameters env v4 in
let v5 = class_body env v5 in
{ cl_name = v3; cl_kind = (Record, v2); cl_tparams = [];
cl_mods = v1; cl_extends = None; cl_impls = []; cl_formals = v4;
cl_body = v5 }

and class_body_decl env = function
| `Record_decl x -> [Class (record_declaration env x)]
| `Field_decl x -> field_declaration env x
| `Meth_decl x -> [Method (method_declaration env x)]
| `Class_decl x -> [Class (class_declaration env x)]
Expand Down Expand Up @@ -1349,7 +1363,7 @@ and class_declaration (env : env) ((v1, v2, v3, v4, v5, v6, v7) : CST.class_decl
in
let v7 = class_body env v7 in
{ cl_name = v3; cl_kind = (ClassRegular, v2); cl_tparams = v4;
cl_mods = v1; cl_extends = v5; cl_impls = v6;
cl_mods = v1; cl_extends = v5; cl_impls = v6; cl_formals = [];
cl_body = v7 }


Expand Down Expand Up @@ -1550,7 +1564,7 @@ and annotation_type_declaration (env : env) ((v1, v2, v3, v4) : CST.annotation_t
let v3 = identifier env v3 (* pattern [a-zA-Z_]\w* *) in
let v4 = annotation_type_body env v4 in
{ cl_mods = v1; cl_name = v3; cl_body = v4; cl_kind = (AtInterface, v2);
cl_tparams = []; cl_extends = None; cl_impls = [];
cl_tparams = []; cl_extends = None; cl_impls = []; cl_formals = [];
}


Expand Down Expand Up @@ -1622,7 +1636,7 @@ and interface_declaration (env : env) ((v1, v2, v3, v4, v5, v6) : CST.interface_
in
let v6 = interface_body env v6 in
{ cl_name = v3; cl_kind = (Interface, v2); cl_tparams = v4;
cl_mods = v1; cl_extends = None; cl_impls = v5;
cl_mods = v1; cl_extends = None; cl_impls = v5; cl_formals = [];
cl_body = v6 }


Expand Down