Skip to contentSkip to conversation

cocci: Add ParenType/FunctionType to SmPL ASTs

PATCH Superseded Jaskaran Singh 2 participants 50 messages Mar 16, 2020 – Mar 19, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover cocci: Add ParenType/FunctionType to SmPL ASTs

The aim of this patch series is to add the types ParenType and FunctionType to the SmPL AST. These types are present in the C AST but not in the SmPL AST.

Preliminarily, a hack to resolve a reduce/reduce conflict with the funproto rule in the SmPL parser is implemented.

Upon this, rules similar to that of the C parser's direct declarator and abstract direct declarator rules are implemented, and used wherever suitable. These rules produce ParenType and FunctionType, similar to the C parser.

Cases for these types are added to various places in the codebase.

The FunctionPointer type is removed from the SmPL AST, since all the productions that produce FunctionPointers in the SmPL parser are replaced by those that produce ParenType. Any functions, constructs or cases of FunctionPointer are also removed from the codebase.

A test case to match an array of function pointers is included to demonstrate matching improvements.

[PATCH 01/26] parsing_cocci: Add Function Prototype token [PATCH 02/26] parsing_cocci: AST: Add ParenType and FunctionType to [PATCH 03/26] parsing_cocci: parser: Add
[PATCH 04/26] parsing_cocci: visitor_ast0: Add cases for [PATCH 05/26] parsing_cocci: visitor_ast0: Add cases for [PATCH 06/26] parsing_cocci: arity: Add cases for
[PATCH 07/26] parsing_cocci: index: Add cases for
[PATCH 08/26] parsing_cocci: context_neg: Add cases for [PATCH 09/26] parsing_cocci: unparse_ast0: Add cases for [PATCH 10/26] parsing_cocci: single_statement: Add cases for [PATCH 11/26] parsing_cocci: function_prototypes: Add cases for [PATCH 12/26] parsing_cocci: check_meta: Add cases for [PATCH 13/26] parsing_cocci: iso_pattern: Add cases for [PATCH 14/26] parsing_cocci: adjust_pragmas: Add cases for [PATCH 15/26] parsing_cocci: compute_lines: Add cases for [PATCH 16/26] parsing_cocci: ast0toast: Add cases for [PATCH 17/26] parsing_cocci: type_cocci: Add ParenType/FunctionType [PATCH 18/26] parsing_cocci: unify_ast: Add cases for [PATCH 19/26] parsing_cocci: disjdistr: Add cases for [PATCH 20/26] parsing_cocci: ast_cocci: Add cases for [PATCH 21/26] parsing_cocci: pretty_print_cocci: Print [PATCH 22/26] parsing_c: unparse_cocci: Print ParenType/FunctionType [PATCH 23/26] engine: Match A.ParenType and A.FunctionType [PATCH 24/26] tools: spgen: Add cases for ParenType/FunctionType [PATCH 25/26] cocci: Remove Ast_cocci.FunctionPointer [PATCH 26/26] tests: Add test case for array of function pointers

 engine/check_exhaustive_pattern.ml       |    3 
 engine/cocci_vs_c.ml                     |   82 +++-------
 ocaml/coccilib.mli                       |   10 -
 parsing_c/unparse_cocci.ml               |   74 +++++++--
 parsing_cocci/adjust_pragmas.ml          |    7 
 parsing_cocci/arity.ml                   |   25 ++-
 parsing_cocci/ast0_cocci.ml              |    7 
 parsing_cocci/ast0_cocci.mli             |    6 
 parsing_cocci/ast0toast.ml               |   11 -
 parsing_cocci/ast_cocci.ml               |   21 +-
 parsing_cocci/ast_cocci.mli              |    6 
 parsing_cocci/check_meta.ml              |    8 -
 parsing_cocci/compute_lines.ml           |   21 +-
 parsing_cocci/context_neg.ml             |    4 
 parsing_cocci/disjdistr.ml               |   16 +-
 parsing_cocci/function_prototypes.ml     |    7 
 parsing_cocci/get_constants.ml           |    2 
 parsing_cocci/index.ml                   |    3 
 parsing_cocci/iso_pattern.ml             |   17 --
 parsing_cocci/parse_cocci.ml             |   72 +++++++--
 parsing_cocci/parser_cocci_menhir.mly    |  248 +++++++++++++++----------------
 parsing_cocci/pretty_print_cocci.ml      |   71 +++++++-
 parsing_cocci/single_statement.ml        |    3 
 parsing_cocci/type_cocci.mli             |    3 
 parsing_cocci/type_infer.ml              |   25 +--
 parsing_cocci/unify_ast.ml               |   16 +-
 parsing_cocci/unparse_ast0.ml            |   56 +++++--
 parsing_cocci/visitor_ast.ml             |  101 +++++++++---
 parsing_cocci/visitor_ast0.ml            |  214 ++++++++++++++++++++------
 tests/funptr_array.c                     |    1 
 tests/funptr_array.cocci                 |    9 +
 tests/funptr_array.res                   |    1 
 tools/spgen/source/meta_variable.ml      |    1 
 tools/spgen/source/position_generator.ml |    9 -
 34 files changed, 754 insertions(+), 406 deletions(-)

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#1 lore raw

PATCH 1/26 parsing_cocci: Add Function Prototype token

To add the types ParenType and FunctionType to the SmPL AST, a reduce/reduce conflict with the funproto rule of the SmPL parser must be resolved. This requires explicitly identifying a function prototype by use of a token (TFunProto).

While the correct method of identifying a function prototype would be to check if an identifier is preceded by a return type, it is challenging to implement. This is because implementing an OCaml function, to correctly determine a C type in SmPL, without the aid of Yacc, would have to handle a number of cases (disjunctions, typeof expressions, etc.).

Thus, a slightly hacky approach is taken to determine a function prototype with not the best certainty but what works for most cases in SmPL. If the identifier is preceded by any token that does not seem to be part of a type, then it is not identified as a function prototype. Else, it is.

---
 parsing_cocci/parse_cocci.ml          | 72 +++++++++++++++++++++++----
 parsing_cocci/parser_cocci_menhir.mly |  9 ++--
 2 files changed, 67 insertions(+), 14 deletions(-)
parsing_cocci/parse_cocci.ml +61 -11
index 679d213a..4b2cb7e4 100644
@@ -295,6 +295,7 @@ let token2c (tok,_) add_clt =
   | PC.TLineEnd(clt) -> "line end"
   | PC.TInvalid -> "invalid"
   | PC.TFunDecl(clt) -> "fundecl"
+  | PC.TFunProto(clt) -> "funproto"
 
   | PC.TIso -> "<=>"
   | PC.TRightIso -> "=>"
@@ -480,7 +481,7 @@ let get_clt (tok,_) =
 
   | PC.TOPar0(_,clt) | PC.TMid0(_,clt) | PC.TAnd0(_,clt) | PC.TCPar0(_,clt)
   | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
-  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt)
+  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) | PC.TFunProto(clt)
   | PC.TFunDecl(clt) | PC.TDirective(_,clt) | PC.TAttr_(clt)
   | PC.TLineEnd(clt) -> clt
   | PC.TVAEllipsis(clt) -> clt
@@ -718,6 +719,7 @@ let update_clt (tok,x) clt =
 
   | PC.TLineEnd(_) -> (PC.TLineEnd(clt),x)
   | PC.TFunDecl(_) -> (PC.TFunDecl(clt),x)
+  | PC.TFunProto(_) -> (PC.TFunProto(clt),x)
   | PC.TTildeExclEq(_) -> (PC.TTildeExclEq(clt),x)
   | PC.TDirective(a,_) -> (PC.TDirective(a,clt),x)
   | PC.TAttr_(_) -> (PC.TAttr_(clt),x)
@@ -925,7 +927,7 @@ let split_token ((tok,_) as t) =
   | PC.TInitialize | PC.TFinalize -> ([t],[t])
   | PC.TPArob clt | PC.TMetaPos(_,_,_,clt) | PC.TMetaCom(_,_,clt) -> split t clt
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
   | PC.TEllipsis(clt)
@@ -1006,7 +1008,8 @@ let find_function_names l =
     | _ -> false in
   let rec split acc = function
       [] | [_] -> raise Irrelevant
-    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) :: rest ->
+    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) :: rest
+    | ((PC.TCPar(_),_) as t1) :: ((PC.TPtVirg(_),_) as t2) :: rest ->
 	(List.rev (t1::acc),(t2::rest))
     | x::xs -> split (x::acc) xs in
   let rec balanced_name level = function
@@ -1037,7 +1040,48 @@ let find_function_names l =
     | (PC.TArobArob,_)::_ | (PC.TArob,_)::_ | (PC.EOF,_)::_ ->
 	raise Irrelevant
     | t::rest -> balanced_args level rest in
-  let rec loop = function
+  let rec is_permissible_proto = function
+      [] -> false
+    | (PC.TCPar0(_),_)::
+      ((PC.TMid0(_),_) | (PC.TAnd0(_),_))::
+      (PC.TOPar0(_),_)::_ -> false
+    | (PC.TOPar0(_),_)::rest
+    | (PC.TCPar0(_),_)::rest -> is_permissible_proto rest
+    | x::rest when is_mid x ->
+        let rec loop = function
+          [] -> false
+        | (PC.TOPar0(_),_)::xs -> is_permissible_proto xs
+        | x::xs -> loop xs in
+        loop rest
+    | _::((PC.TEq(_),_) | (PC.TNotEq(_),_))::(PC.TWhen(_),_)::_
+    | _::(PC.TWhen(_),_)::_
+    | (PC.TComma(_),_)::_
+    | (PC.TDirective(_),_)::_
+    | (PC.TElse(_),_)::_
+    | (PC.TReturn(_),_)::_
+    | (PC.TMetaStm(_),_)::_
+    | (PC.TMetaExp(_),_)::_
+    | (PC.TMetaId(_),_)::_
+    | (PC.TMetaLocalIdExp(_),_)::_
+    | (PC.TEq(_),_)::_
+    | (PC.TEllipsis(_),_)::_
+    | (PC.TOEllipsis(_),_)::_
+    | (PC.TCEllipsis(_),_)::_
+    | (PC.TPOEllipsis(_),_)::_
+    | (PC.TPCEllipsis(_),_)::_
+    | (PC.TPtVirg(_),_)::_
+    | (PC.TOBrace(_),_)::_
+    | (PC.TCBrace(_),_)::_
+    | (PC.TOPar(_),_)::_
+    | (PC.TCPar(_),_)::_
+    | (PC.TIdent(_),_)::_ -> false
+    | _ -> true in
+  let decl_or_proto clt info bef aft =
+    match aft with
+      (PC.TOBrace(_),_)::_ -> (((PC.TFunDecl(clt),info) :: bef), aft)
+    | (PC.TPtVirg(_),_)::_ -> (((PC.TFunProto(clt),info) :: bef), aft)
+    | _ -> raise Irrelevant in
+  let rec loop acc = function
       [] -> []
     | t :: rest ->
 	if is_par t || is_mid t || is_ident t
@@ -1046,26 +1090,30 @@ let find_function_names l =
 	    try
 	      let (bef,aft) = split [] (t::rest) in
 	      let rest = balanced_name 0 bef in
+              (match aft with
+                (PC.TPtVirg(_),_)::_
+                 when not(is_permissible_proto acc) -> raise Irrelevant
+              | _ ->
 	      (match rest with
 		(PC.TOPar(_),_)::_ ->
 		  (match balanced_args 0 rest with
 		    [] ->
 		      let (_,info) as h = List.hd bef in
 		      let clt = get_clt h in
-		      (((PC.TFunDecl(clt),info) :: bef), aft)
+                      decl_or_proto clt info bef aft
 		  | (PC.TAttr_(_),_)::rest ->
 		      (match balanced_args 0 rest with
 			[] ->
 			  let (_,info) as h = List.hd bef in
 			  let clt = get_clt h in
-			  (((PC.TFunDecl(clt),info) :: bef), aft)
+                          decl_or_proto clt info bef aft
 		      | _ -> raise Irrelevant)
 		  | _ -> raise Irrelevant)
-	      | _ -> raise Irrelevant)
+	      | _ -> raise Irrelevant))
 	    with Irrelevant -> ([t],rest) in
-	  t @ (loop rest)
-	else t :: (loop rest) in
-  loop l
+          t @ (loop (t @ acc) rest)
+        else t :: (loop (t :: acc) rest) in
+  loop [] l
 
 (* ----------------------------------------------------------------------- *)
 (* an attribute is an identifier that precedes another identifier and
@@ -1168,6 +1216,8 @@ let detect_types in_meta_decls l =
 	delim::newid::id::(loop false infn (ident::type_names) rest)
     | ((PC.TFunDecl(_),_) as fn)::rest ->
 	fn::(loop false 1 type_names rest)
+    | ((PC.TFunProto(_),_) as fn)::rest ->
+	fn::(loop false 1 type_names rest)
     | ((PC.TOPar(_),_) as lp)::rest when infn > 0 ->
 	lp::(loop false (infn + 1) type_names rest)
     | ((PC.TCPar(_),_) as rp)::rest when infn > 0 ->
@@ -1252,7 +1302,7 @@ let token2line (tok,_) =
   | PC.TMetaDParamList(_,_,_,_,clt) | PC.TMetaFunc(_,_,_,clt)
   | PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt) | PC.TMetaCom(_,_,clt)
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
 
parsing_cocci/parser_cocci_menhir.mly +6 -3
index 9e6c8a08..26958f63 100644
@@ -239,7 +239,8 @@ let inline_id aft = function
 
 %token <Data.clt> TVAEllipsis
 %token <Data.clt> TIf TElse TWhile TFor TDo TSwitch TCase TDefault TReturn
-%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl Tdecimal Texec
+%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl TFunProto
+%token <Data.clt> Tdecimal Texec
 %token <string * Data.clt> TIdent TTypeId TDeclarerId TIteratorId TSymId
 %token <Ast_cocci.added_string * Data.clt> TDirective
 %token <Data.clt> TAttr_
@@ -1439,7 +1440,8 @@ reverse_separated_nonempty_llist(separator, X):
 
 funproto:
   s=ioption(storage) i=ioption(Tinline) t=ctype
-  id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = match s with None -> [] | Some s -> [Ast0.FStorage s] in
         let i =
 	  match i with
@@ -1453,7 +1455,8 @@ funproto:
 	      P.clt2mcode "(" lp, args, vararg, P.clt2mcode ")" rp,
 	      P.clt2mcode ";" pt)) }
 | i=Tinline s=storage t=ctype
-  id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = [Ast0.FStorage s] in
         let i = [Ast0.FInline (P.clt2mcode "inline" i)] in
 	let t = [Ast0.FType t] in
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#2 lore raw

Re: [Cocci] [PATCH 01/26] parsing_cocci: Add Function Prototype token

Quoted 12 lines · Jaskaran Singh

To add the types ParenType and FunctionType to the SmPL AST, a reduce/reduce conflict with the funproto rule of the SmPL parser must be resolved. This requires explicitly identifying a function prototype by use of a token (TFunProto).

While the correct method of identifying a function prototype would be to check if an identifier is preceded by a return type, it is challenging to implement. This is because implementing an OCaml function, to correctly determine a C type in SmPL, without the aid of Yacc, would have to handle a number of cases (disjunctions, typeof expressions, etc.).

Quoted 5 lines

Thus, a slightly hacky approach is taken to determine a function prototype with not the best certainty but what works for most cases in SmPL. If the identifier is preceded by any token that does not seem to be part of a type, then it is not identified as a function prototype. Else, it is.

This sacrifices the test case tests/p1p2.cocci:

@@
typedef Error;
type T;
identifier FN;
parameter P1;
@@
T
+__attribute__((nonnull(1)))
FN(P1, Error **errp);

@@
typedef Error;
type T;
identifier FN;
parameter P1;
parameter P2;
@@
T
+__attribute__((nonnull(2)))
FN(P1, P2, Error **errp);

Normally, the only way that you can have a ) before a function call is when there is a cast. But hopefully in that case there would not be two )) before the function call. Can that get around the problem?

julia

Quoted 195 lines
---
 parsing_cocci/parse_cocci.ml          | 72 +++++++++++++++++++++++----
 parsing_cocci/parser_cocci_menhir.mly |  9 ++--
 2 files changed, 67 insertions(+), 14 deletions(-)
parsing_cocci/parse_cocci.ml +61 -11
index 679d213a..4b2cb7e4 100644
@@ -295,6 +295,7 @@ let token2c (tok,_) add_clt =
   | PC.TLineEnd(clt) -> "line end"
   | PC.TInvalid -> "invalid"
   | PC.TFunDecl(clt) -> "fundecl"
+  | PC.TFunProto(clt) -> "funproto"
 
   | PC.TIso -> "<=>"
   | PC.TRightIso -> "=>"
@@ -480,7 +481,7 @@ let get_clt (tok,_) =
 
   | PC.TOPar0(_,clt) | PC.TMid0(_,clt) | PC.TAnd0(_,clt) | PC.TCPar0(_,clt)
   | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
-  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt)
+  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) | PC.TFunProto(clt)
   | PC.TFunDecl(clt) | PC.TDirective(_,clt) | PC.TAttr_(clt)
   | PC.TLineEnd(clt) -> clt
   | PC.TVAEllipsis(clt) -> clt
@@ -718,6 +719,7 @@ let update_clt (tok,x) clt =
 
   | PC.TLineEnd(_) -> (PC.TLineEnd(clt),x)
   | PC.TFunDecl(_) -> (PC.TFunDecl(clt),x)
+  | PC.TFunProto(_) -> (PC.TFunProto(clt),x)
   | PC.TTildeExclEq(_) -> (PC.TTildeExclEq(clt),x)
   | PC.TDirective(a,_) -> (PC.TDirective(a,clt),x)
   | PC.TAttr_(_) -> (PC.TAttr_(clt),x)
@@ -925,7 +927,7 @@ let split_token ((tok,_) as t) =
   | PC.TInitialize | PC.TFinalize -> ([t],[t])
   | PC.TPArob clt | PC.TMetaPos(_,_,_,clt) | PC.TMetaCom(_,_,clt) -> split t clt
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
   | PC.TEllipsis(clt)
@@ -1006,7 +1008,8 @@ let find_function_names l =
     | _ -> false in
   let rec split acc = function
       [] | [_] -> raise Irrelevant
-    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) :: rest ->
+    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) :: rest
+    | ((PC.TCPar(_),_) as t1) :: ((PC.TPtVirg(_),_) as t2) :: rest ->
 	(List.rev (t1::acc),(t2::rest))
     | x::xs -> split (x::acc) xs in
   let rec balanced_name level = function
@@ -1037,7 +1040,48 @@ let find_function_names l =
     | (PC.TArobArob,_)::_ | (PC.TArob,_)::_ | (PC.EOF,_)::_ ->
 	raise Irrelevant
     | t::rest -> balanced_args level rest in
-  let rec loop = function
+  let rec is_permissible_proto = function
+      [] -> false
+    | (PC.TCPar0(_),_)::
+      ((PC.TMid0(_),_) | (PC.TAnd0(_),_))::
+      (PC.TOPar0(_),_)::_ -> false
+    | (PC.TOPar0(_),_)::rest
+    | (PC.TCPar0(_),_)::rest -> is_permissible_proto rest
+    | x::rest when is_mid x ->
+        let rec loop = function
+          [] -> false
+        | (PC.TOPar0(_),_)::xs -> is_permissible_proto xs
+        | x::xs -> loop xs in
+        loop rest
+    | _::((PC.TEq(_),_) | (PC.TNotEq(_),_))::(PC.TWhen(_),_)::_
+    | _::(PC.TWhen(_),_)::_
+    | (PC.TComma(_),_)::_
+    | (PC.TDirective(_),_)::_
+    | (PC.TElse(_),_)::_
+    | (PC.TReturn(_),_)::_
+    | (PC.TMetaStm(_),_)::_
+    | (PC.TMetaExp(_),_)::_
+    | (PC.TMetaId(_),_)::_
+    | (PC.TMetaLocalIdExp(_),_)::_
+    | (PC.TEq(_),_)::_
+    | (PC.TEllipsis(_),_)::_
+    | (PC.TOEllipsis(_),_)::_
+    | (PC.TCEllipsis(_),_)::_
+    | (PC.TPOEllipsis(_),_)::_
+    | (PC.TPCEllipsis(_),_)::_
+    | (PC.TPtVirg(_),_)::_
+    | (PC.TOBrace(_),_)::_
+    | (PC.TCBrace(_),_)::_
+    | (PC.TOPar(_),_)::_
+    | (PC.TCPar(_),_)::_
+    | (PC.TIdent(_),_)::_ -> false
+    | _ -> true in
+  let decl_or_proto clt info bef aft =
+    match aft with
+      (PC.TOBrace(_),_)::_ -> (((PC.TFunDecl(clt),info) :: bef), aft)
+    | (PC.TPtVirg(_),_)::_ -> (((PC.TFunProto(clt),info) :: bef), aft)
+    | _ -> raise Irrelevant in
+  let rec loop acc = function
       [] -> []
     | t :: rest ->
 	if is_par t || is_mid t || is_ident t
@@ -1046,26 +1090,30 @@ let find_function_names l =
 	    try
 	      let (bef,aft) = split [] (t::rest) in
 	      let rest = balanced_name 0 bef in
+              (match aft with
+                (PC.TPtVirg(_),_)::_
+                 when not(is_permissible_proto acc) -> raise Irrelevant
+              | _ ->
 	      (match rest with
 		(PC.TOPar(_),_)::_ ->
 		  (match balanced_args 0 rest with
 		    [] ->
 		      let (_,info) as h = List.hd bef in
 		      let clt = get_clt h in
-		      (((PC.TFunDecl(clt),info) :: bef), aft)
+                      decl_or_proto clt info bef aft
 		  | (PC.TAttr_(_),_)::rest ->
 		      (match balanced_args 0 rest with
 			[] ->
 			  let (_,info) as h = List.hd bef in
 			  let clt = get_clt h in
-			  (((PC.TFunDecl(clt),info) :: bef), aft)
+                          decl_or_proto clt info bef aft
 		      | _ -> raise Irrelevant)
 		  | _ -> raise Irrelevant)
-	      | _ -> raise Irrelevant)
+	      | _ -> raise Irrelevant))
 	    with Irrelevant -> ([t],rest) in
-	  t @ (loop rest)
-	else t :: (loop rest) in
-  loop l
+          t @ (loop (t @ acc) rest)
+        else t :: (loop (t :: acc) rest) in
+  loop [] l
 
 (* ----------------------------------------------------------------------- *)
 (* an attribute is an identifier that precedes another identifier and
@@ -1168,6 +1216,8 @@ let detect_types in_meta_decls l =
 	delim::newid::id::(loop false infn (ident::type_names) rest)
     | ((PC.TFunDecl(_),_) as fn)::rest ->
 	fn::(loop false 1 type_names rest)
+    | ((PC.TFunProto(_),_) as fn)::rest ->
+	fn::(loop false 1 type_names rest)
     | ((PC.TOPar(_),_) as lp)::rest when infn > 0 ->
 	lp::(loop false (infn + 1) type_names rest)
     | ((PC.TCPar(_),_) as rp)::rest when infn > 0 ->
@@ -1252,7 +1302,7 @@ let token2line (tok,_) =
   | PC.TMetaDParamList(_,_,_,_,clt) | PC.TMetaFunc(_,_,_,clt)
   | PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt) | PC.TMetaCom(_,_,clt)
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
 
parsing_cocci/parser_cocci_menhir.mly +6 -4
index 9e6c8a08..26958f63 100644
@@ -239,7 +239,8 @@ let inline_id aft = function
 
 %token <Data.clt> TVAEllipsis
 %token <Data.clt> TIf TElse TWhile TFor TDo TSwitch TCase TDefault TReturn
-%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl Tdecimal Texec
+%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl TFunProto
+%token <Data.clt> Tdecimal Texec
 %token <string * Data.clt> TIdent TTypeId TDeclarerId TIteratorId TSymId
 %token <Ast_cocci.added_string * Data.clt> TDirective
 %token <Data.clt> TAttr_
@@ -1439,7 +1440,8 @@ reverse_separated_nonempty_llist(separator, X):
 
 funproto:
   s=ioption(storage) i=ioption(Tinline) t=ctype
-  id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = match s with None -> [] | Some s -> [Ast0.FStorage s] in
         let i =
 	  match i with
@@ -1453,7 +1455,8 @@ funproto:
 	      P.clt2mcode "(" lp, args, vararg, P.clt2mcode ")" rp,
 	      P.clt2mcode ";" pt)) }
 | i=Tinline s=storage t=ctype
-  id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = [Ast0.FStorage s] in
         let i = [Ast0.FInline (P.clt2mcode "inline" i)] in
 	let t = [Ast0.FType t] in
--
Quoted 3 lines

2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#3 lore raw

Re: [Cocci] [PATCH 01/26] parsing_cocci: Add Function Prototype token

Quoted 18 lines · Julia Lawall
Quoted 15 lines · Jaskaran Singh

To add the types ParenType and FunctionType to the SmPL AST, a reduce/reduce conflict with the funproto rule of the SmPL parser must be resolved. This requires explicitly identifying a function prototype by use of a token (TFunProto).

While the correct method of identifying a function prototype would be to
check if an identifier is preceded by a return type, it is challenging
to implement. This is because implementing an OCaml function, to correctly determine a C type in SmPL, without the aid of Yacc, would
have to handle a number of cases (disjunctions, typeof expressions, etc.).

Quoted 8 lines
Quoted 5 lines

Thus, a slightly hacky approach is taken to determine a function prototype with not the best certainty but what works for most cases in SmPL. If the identifier is preceded by any token that does not seem to be part of a type, then it is not identified as a function prototype. Else, it is.

This sacrifices the test case tests/p1p2.cocci:

What do you mean by sacrifice?

The test case seems to work, but not with just 01 applied. Here's what the debugger tells me about the AST:

Ast_cocci.FunProto
([Ast_cocci.FType
{Ast_cocci.node =

     Ast_cocci.Type (false, None,
      {Ast_cocci.node =
        Ast_cocci.MetaType
         ((("rule starting on line 11", "T"),
           {Ast_cocci.line = 18; column = 1;
            strbef = []; straft = [];
            whitespace = " "},
           Ast_cocci.CONTEXT (Ast_cocci.NoPos,
            Ast_cocci.AFTER
             ([[Ast_cocci.Directive
                 [Ast_cocci.Space
                   "__attribute__((nonnull(2)))"]]],
             Ast_cocci.ONE)),
           []),

Maybe after the grammar changes, Menhir does some kind of lookahead? I'm not sure.

Cheers,
Jaskaran.

Quoted 242 lines

@@
typedef Error;
type T;
identifier FN;
parameter P1;
@@
T
+__attribute__((nonnull(1)))
FN(P1, Error **errp);

@@
typedef Error;
type T;
identifier FN;
parameter P1;
parameter P2;
@@
T
+__attribute__((nonnull(2)))
FN(P1, P2, Error **errp);

Normally, the only way that you can have a ) before a function call is
when there is a cast. But hopefully in that case there would not be two
)) before the function call. Can that get around the problem?

julia

Quoted 210 lines
---
 parsing_cocci/parse_cocci.ml          | 72
+++++++++++++++++++++++----
 parsing_cocci/parser_cocci_menhir.mly |  9 ++--
 2 files changed, 67 insertions(+), 14 deletions(-)
parsing_cocci/parse_cocci.ml +2 -1
index 679d213a..4b2cb7e4 100644
@@ -295,6 +295,7 @@ let token2c (tok,_) add_clt =
   | PC.TLineEnd(clt) -> "line end"
   | PC.TInvalid -> "invalid"
   | PC.TFunDecl(clt) -> "fundecl"
+  | PC.TFunProto(clt) -> "funproto"
 
   | PC.TIso -> "<=>"
   | PC.TRightIso -> "=>"
@@ -480,7 +481,7 @@ let get_clt (tok,_) =
 
   | PC.TOPar0(_,clt) | PC.TMid0(_,clt) | PC.TAnd0(_,clt) |
 PC.TCPar0(_,clt)
   | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
-  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt)
+  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) | PC.TFunProto(clt)
   | PC.TFunDecl(clt) | PC.TDirective(_,clt) | PC.TAttr_(clt)
   | PC.TLineEnd(clt) -> clt
   | PC.TVAEllipsis(clt) -> clt
parsing_cocci/parse_cocci.ml +46 -6
@@ -718,6 +719,7 @@ let update_clt (tok,x) clt =
 
   | PC.TLineEnd(_) -> (PC.TLineEnd(clt),x)
   | PC.TFunDecl(_) -> (PC.TFunDecl(clt),x)
+  | PC.TFunProto(_) -> (PC.TFunProto(clt),x)
   | PC.TTildeExclEq(_) -> (PC.TTildeExclEq(clt),x)
   | PC.TDirective(a,_) -> (PC.TDirective(a,clt),x)
   | PC.TAttr_(_) -> (PC.TAttr_(clt),x)
@@ -925,7 +927,7 @@ let split_token ((tok,_) as t) =
   | PC.TInitialize | PC.TFinalize -> ([t],[t])
   | PC.TPArob clt | PC.TMetaPos(_,_,_,clt) | PC.TMetaCom(_,_,clt)
-> split t clt
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
   | PC.TEllipsis(clt)
@@ -1006,7 +1008,8 @@ let find_function_names l =
     | _ -> false in
   let rec split acc = function
       [] | [_] -> raise Irrelevant
-    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) :: rest
->
+    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) :: rest
+    | ((PC.TCPar(_),_) as t1) :: ((PC.TPtVirg(_),_) as t2) :: rest
->
 	(List.rev (t1::acc),(t2::rest))
     | x::xs -> split (x::acc) xs in
   let rec balanced_name level = function
@@ -1037,7 +1040,48 @@ let find_function_names l =
     | (PC.TArobArob,_)::_ | (PC.TArob,_)::_ | (PC.EOF,_)::_ ->
 	raise Irrelevant
     | t::rest -> balanced_args level rest in
-  let rec loop = function
+  let rec is_permissible_proto = function
+      [] -> false
+    | (PC.TCPar0(_),_)::
+      ((PC.TMid0(_),_) | (PC.TAnd0(_),_))::
+      (PC.TOPar0(_),_)::_ -> false
+    | (PC.TOPar0(_),_)::rest
+    | (PC.TCPar0(_),_)::rest -> is_permissible_proto rest
+    | x::rest when is_mid x ->
+        let rec loop = function
+          [] -> false
+        | (PC.TOPar0(_),_)::xs -> is_permissible_proto xs
+        | x::xs -> loop xs in
+        loop rest
+    | _::((PC.TEq(_),_) | (PC.TNotEq(_),_))::(PC.TWhen(_),_)::_
+    | _::(PC.TWhen(_),_)::_
+    | (PC.TComma(_),_)::_
+    | (PC.TDirective(_),_)::_
+    | (PC.TElse(_),_)::_
+    | (PC.TReturn(_),_)::_
+    | (PC.TMetaStm(_),_)::_
+    | (PC.TMetaExp(_),_)::_
+    | (PC.TMetaId(_),_)::_
+    | (PC.TMetaLocalIdExp(_),_)::_
+    | (PC.TEq(_),_)::_
+    | (PC.TEllipsis(_),_)::_
+    | (PC.TOEllipsis(_),_)::_
+    | (PC.TCEllipsis(_),_)::_
+    | (PC.TPOEllipsis(_),_)::_
+    | (PC.TPCEllipsis(_),_)::_
+    | (PC.TPtVirg(_),_)::_
+    | (PC.TOBrace(_),_)::_
+    | (PC.TCBrace(_),_)::_
+    | (PC.TOPar(_),_)::_
+    | (PC.TCPar(_),_)::_
+    | (PC.TIdent(_),_)::_ -> false
+    | _ -> true in
+  let decl_or_proto clt info bef aft =
+    match aft with
+      (PC.TOBrace(_),_)::_ -> (((PC.TFunDecl(clt),info) :: bef),
 aft)
+    | (PC.TPtVirg(_),_)::_ -> (((PC.TFunProto(clt),info) :: bef),
 aft)
+    | _ -> raise Irrelevant in
+  let rec loop acc = function
       [] -> []
     | t :: rest ->
parsing_cocci/parse_cocci.ml +3 -0
 	if is_par t || is_mid t || is_ident t
@@ -1046,26 +1090,30 @@ let find_function_names l =
 	    try
 	      let (bef,aft) = split [] (t::rest) in
 	      let rest = balanced_name 0 bef in
+              (match aft with
+                (PC.TPtVirg(_),_)::_
+                 when not(is_permissible_proto acc) -> raise

Irrelevant

parsing_cocci/parse_cocci.ml +7 -7
+              | _ ->
 	      (match rest with
 		(PC.TOPar(_),_)::_ ->
 		  (match balanced_args 0 rest with
 		    [] ->
 		      let (_,info) as h = List.hd bef in
 		      let clt = get_clt h in
-		      (((PC.TFunDecl(clt),info) :: bef), aft)
+                      decl_or_proto clt info bef aft
 		  | (PC.TAttr_(_),_)::rest ->
 		      (match balanced_args 0 rest with
 			[] ->
 			  let (_,info) as h = List.hd bef in
 			  let clt = get_clt h in
-			  (((PC.TFunDecl(clt),info) :: bef), aft)
+                          decl_or_proto clt info bef aft
 		      | _ -> raise Irrelevant)
 		  | _ -> raise Irrelevant)
-	      | _ -> raise Irrelevant)
+	      | _ -> raise Irrelevant))
 	    with Irrelevant -> ([t],rest) in
-	  t @ (loop rest)
-	else t :: (loop rest) in
-  loop l
+          t @ (loop (t @ acc) rest)
+        else t :: (loop (t :: acc) rest) in
+  loop [] l
 
 (* -------------------------------------------------------------
---------- *)
 (* an attribute is an identifier that precedes another identifier

and

parsing_cocci/parse_cocci.ml +3 -1
@@ -1168,6 +1216,8 @@ let detect_types in_meta_decls l =
 	delim::newid::id::(loop false infn (ident::type_names) rest)
     | ((PC.TFunDecl(_),_) as fn)::rest ->
 	fn::(loop false 1 type_names rest)
+    | ((PC.TFunProto(_),_) as fn)::rest ->
+	fn::(loop false 1 type_names rest)
     | ((PC.TOPar(_),_) as lp)::rest when infn > 0 ->
 	lp::(loop false (infn + 1) type_names rest)
     | ((PC.TCPar(_),_) as rp)::rest when infn > 0 ->
@@ -1252,7 +1302,7 @@ let token2line (tok,_) =
   | PC.TMetaDParamList(_,_,_,_,clt) | PC.TMetaFunc(_,_,_,clt)
   | PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt) |
 PC.TMetaCom(_,_,clt)
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
parsing_cocci/parser_cocci_menhir.mly +0 -0
index 9e6c8a08..26958f63 100644
@@ -239,7 +239,8 @@ let inline_id aft = function
 
 %token <Data.clt> TVAEllipsis
 %token <Data.clt> TIf TElse TWhile TFor TDo TSwitch TCase TDefault

TReturn
-%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl Tdecimal Texec
+%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl TFunProto
+%token <Data.clt> Tdecimal Texec
%token <string * Data.clt> TIdent TTypeId TDeclarerId TIteratorId
TSymId
%token <Ast_cocci.added_string * Data.clt> TDirective %token <Data.clt> TAttr_

parsing_cocci/parser_cocci_menhir.mly +0 -0
@@ -1439,7 +1440,8 @@ reverse_separated_nonempty_llist(separator,

X):

funproto:
s=ioption(storage) i=ioption(Tinline) t=ctype
- id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = match s with None -> [] | Some s -> [Ast0.FStorage

s] in
let i =

parsing_cocci/parser_cocci_menhir.mly +0 -1
 	  match i with
@@ -1453,7 +1455,8 @@ funproto:
 	      P.clt2mcode "(" lp, args, vararg, P.clt2mcode ")" rp,
 	      P.clt2mcode ";" pt)) }
 | i=Tinline s=storage t=ctype
-  id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar

pt=TPtVirg
+ TFunProto id=fn_ident

Quoted 8 lines
Quoted 8 lines
parsing_cocci/parser_cocci_menhir.mly +1 -1
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = [Ast0.FStorage s] in
         let i = [Ast0.FInline (P.clt2mcode "inline" i)] in
 	let t = [Ast0.FType t] in
--

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#4 lore raw

Re: [Cocci] [PATCH 01/26] parsing_cocci: Add Function Prototype token

Quoted 25 lines · Jaskaran Singh
Quoted 24 lines · Julia Lawall
Quoted 20 lines · Jaskaran Singh

To add the types ParenType and FunctionType to the SmPL AST, a reduce/reduce conflict with the funproto rule of the SmPL parser must be resolved. This requires explicitly identifying a function prototype by use of a token (TFunProto).

While the correct method of identifying a function prototype would be to
check if an identifier is preceded by a return type, it is challenging
to implement. This is because implementing an OCaml function, to correctly determine a C type in SmPL, without the aid of Yacc, would
have to handle a number of cases (disjunctions, typeof expressions, etc.).

Thus, a slightly hacky approach is taken to determine a function prototype with not the best certainty but what works for most cases in SmPL. If the identifier is preceded by any token that does not seem to be part of a type, then it is not identified as a function prototype. Else, it is.

Quoted 4 lines
Quoted 2 lines

This sacrifices the test case tests/p1p2.cocci:

What do you mean by sacrifice?

That it doesn't work after applying 01.

Does something that comes later make it work again?

julia

Quoted 269 lines

The test case seems to work, but not with just 01 applied. Here's what the debugger tells me about the AST:

Ast_cocci.FunProto
([Ast_cocci.FType
{Ast_cocci.node =

     Ast_cocci.Type (false, None,
      {Ast_cocci.node =
        Ast_cocci.MetaType
         ((("rule starting on line 11", "T"),
           {Ast_cocci.line = 18; column = 1;
            strbef = []; straft = [];
            whitespace = " "},
           Ast_cocci.CONTEXT (Ast_cocci.NoPos,
            Ast_cocci.AFTER
             ([[Ast_cocci.Directive
                 [Ast_cocci.Space
                   "__attribute__((nonnull(2)))"]]],
             Ast_cocci.ONE)),
           []),

Maybe after the grammar changes, Menhir does some kind of lookahead? I'm not sure.

Cheers,
Jaskaran.

Quoted 242 lines

@@
typedef Error;
type T;
identifier FN;
parameter P1;
@@
T
+__attribute__((nonnull(1)))
FN(P1, Error **errp);

@@
typedef Error;
type T;
identifier FN;
parameter P1;
parameter P2;
@@
T
+__attribute__((nonnull(2)))
FN(P1, P2, Error **errp);

Normally, the only way that you can have a ) before a function call is
when there is a cast. But hopefully in that case there would not be two
)) before the function call. Can that get around the problem?

julia

Quoted 210 lines
---
 parsing_cocci/parse_cocci.ml          | 72
+++++++++++++++++++++++----
 parsing_cocci/parser_cocci_menhir.mly |  9 ++--
 2 files changed, 67 insertions(+), 14 deletions(-)
parsing_cocci/parse_cocci.ml +2 -1
index 679d213a..4b2cb7e4 100644
@@ -295,6 +295,7 @@ let token2c (tok,_) add_clt =
   | PC.TLineEnd(clt) -> "line end"
   | PC.TInvalid -> "invalid"
   | PC.TFunDecl(clt) -> "fundecl"
+  | PC.TFunProto(clt) -> "funproto"
 
   | PC.TIso -> "<=>"
   | PC.TRightIso -> "=>"
@@ -480,7 +481,7 @@ let get_clt (tok,_) =
 
   | PC.TOPar0(_,clt) | PC.TMid0(_,clt) | PC.TAnd0(_,clt) |
 PC.TCPar0(_,clt)
   | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
-  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt)
+  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) | PC.TFunProto(clt)
   | PC.TFunDecl(clt) | PC.TDirective(_,clt) | PC.TAttr_(clt)
   | PC.TLineEnd(clt) -> clt
   | PC.TVAEllipsis(clt) -> clt
parsing_cocci/parse_cocci.ml +46 -6
@@ -718,6 +719,7 @@ let update_clt (tok,x) clt =
 
   | PC.TLineEnd(_) -> (PC.TLineEnd(clt),x)
   | PC.TFunDecl(_) -> (PC.TFunDecl(clt),x)
+  | PC.TFunProto(_) -> (PC.TFunProto(clt),x)
   | PC.TTildeExclEq(_) -> (PC.TTildeExclEq(clt),x)
   | PC.TDirective(a,_) -> (PC.TDirective(a,clt),x)
   | PC.TAttr_(_) -> (PC.TAttr_(clt),x)
@@ -925,7 +927,7 @@ let split_token ((tok,_) as t) =
   | PC.TInitialize | PC.TFinalize -> ([t],[t])
   | PC.TPArob clt | PC.TMetaPos(_,_,_,clt) | PC.TMetaCom(_,_,clt)
-> split t clt
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
   | PC.TEllipsis(clt)
@@ -1006,7 +1008,8 @@ let find_function_names l =
     | _ -> false in
   let rec split acc = function
       [] | [_] -> raise Irrelevant
-    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) :: rest
->
+    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) :: rest
+    | ((PC.TCPar(_),_) as t1) :: ((PC.TPtVirg(_),_) as t2) :: rest
->
 	(List.rev (t1::acc),(t2::rest))
     | x::xs -> split (x::acc) xs in
   let rec balanced_name level = function
@@ -1037,7 +1040,48 @@ let find_function_names l =
     | (PC.TArobArob,_)::_ | (PC.TArob,_)::_ | (PC.EOF,_)::_ ->
 	raise Irrelevant
     | t::rest -> balanced_args level rest in
-  let rec loop = function
+  let rec is_permissible_proto = function
+      [] -> false
+    | (PC.TCPar0(_),_)::
+      ((PC.TMid0(_),_) | (PC.TAnd0(_),_))::
+      (PC.TOPar0(_),_)::_ -> false
+    | (PC.TOPar0(_),_)::rest
+    | (PC.TCPar0(_),_)::rest -> is_permissible_proto rest
+    | x::rest when is_mid x ->
+        let rec loop = function
+          [] -> false
+        | (PC.TOPar0(_),_)::xs -> is_permissible_proto xs
+        | x::xs -> loop xs in
+        loop rest
+    | _::((PC.TEq(_),_) | (PC.TNotEq(_),_))::(PC.TWhen(_),_)::_
+    | _::(PC.TWhen(_),_)::_
+    | (PC.TComma(_),_)::_
+    | (PC.TDirective(_),_)::_
+    | (PC.TElse(_),_)::_
+    | (PC.TReturn(_),_)::_
+    | (PC.TMetaStm(_),_)::_
+    | (PC.TMetaExp(_),_)::_
+    | (PC.TMetaId(_),_)::_
+    | (PC.TMetaLocalIdExp(_),_)::_
+    | (PC.TEq(_),_)::_
+    | (PC.TEllipsis(_),_)::_
+    | (PC.TOEllipsis(_),_)::_
+    | (PC.TCEllipsis(_),_)::_
+    | (PC.TPOEllipsis(_),_)::_
+    | (PC.TPCEllipsis(_),_)::_
+    | (PC.TPtVirg(_),_)::_
+    | (PC.TOBrace(_),_)::_
+    | (PC.TCBrace(_),_)::_
+    | (PC.TOPar(_),_)::_
+    | (PC.TCPar(_),_)::_
+    | (PC.TIdent(_),_)::_ -> false
+    | _ -> true in
+  let decl_or_proto clt info bef aft =
+    match aft with
+      (PC.TOBrace(_),_)::_ -> (((PC.TFunDecl(clt),info) :: bef),
 aft)
+    | (PC.TPtVirg(_),_)::_ -> (((PC.TFunProto(clt),info) :: bef),
 aft)
+    | _ -> raise Irrelevant in
+  let rec loop acc = function
       [] -> []
     | t :: rest ->
parsing_cocci/parse_cocci.ml +3 -0
 	if is_par t || is_mid t || is_ident t
@@ -1046,26 +1090,30 @@ let find_function_names l =
 	    try
 	      let (bef,aft) = split [] (t::rest) in
 	      let rest = balanced_name 0 bef in
+              (match aft with
+                (PC.TPtVirg(_),_)::_
+                 when not(is_permissible_proto acc) -> raise

Irrelevant

parsing_cocci/parse_cocci.ml +7 -7
+              | _ ->
 	      (match rest with
 		(PC.TOPar(_),_)::_ ->
 		  (match balanced_args 0 rest with
 		    [] ->
 		      let (_,info) as h = List.hd bef in
 		      let clt = get_clt h in
-		      (((PC.TFunDecl(clt),info) :: bef), aft)
+                      decl_or_proto clt info bef aft
 		  | (PC.TAttr_(_),_)::rest ->
 		      (match balanced_args 0 rest with
 			[] ->
 			  let (_,info) as h = List.hd bef in
 			  let clt = get_clt h in
-			  (((PC.TFunDecl(clt),info) :: bef), aft)
+                          decl_or_proto clt info bef aft
 		      | _ -> raise Irrelevant)
 		  | _ -> raise Irrelevant)
-	      | _ -> raise Irrelevant)
+	      | _ -> raise Irrelevant))
 	    with Irrelevant -> ([t],rest) in
-	  t @ (loop rest)
-	else t :: (loop rest) in
-  loop l
+          t @ (loop (t @ acc) rest)
+        else t :: (loop (t :: acc) rest) in
+  loop [] l
 
 (* -------------------------------------------------------------
---------- *)
 (* an attribute is an identifier that precedes another identifier

and

parsing_cocci/parse_cocci.ml +3 -1
@@ -1168,6 +1216,8 @@ let detect_types in_meta_decls l =
 	delim::newid::id::(loop false infn (ident::type_names) rest)
     | ((PC.TFunDecl(_),_) as fn)::rest ->
 	fn::(loop false 1 type_names rest)
+    | ((PC.TFunProto(_),_) as fn)::rest ->
+	fn::(loop false 1 type_names rest)
     | ((PC.TOPar(_),_) as lp)::rest when infn > 0 ->
 	lp::(loop false (infn + 1) type_names rest)
     | ((PC.TCPar(_),_) as rp)::rest when infn > 0 ->
@@ -1252,7 +1302,7 @@ let token2line (tok,_) =
   | PC.TMetaDParamList(_,_,_,_,clt) | PC.TMetaFunc(_,_,_,clt)
   | PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt) |
 PC.TMetaCom(_,_,clt)
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
parsing_cocci/parser_cocci_menhir.mly +0 -0
index 9e6c8a08..26958f63 100644
@@ -239,7 +239,8 @@ let inline_id aft = function
 
 %token <Data.clt> TVAEllipsis
 %token <Data.clt> TIf TElse TWhile TFor TDo TSwitch TCase TDefault

TReturn
-%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl Tdecimal Texec
+%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl TFunProto
+%token <Data.clt> Tdecimal Texec
%token <string * Data.clt> TIdent TTypeId TDeclarerId TIteratorId
TSymId
%token <Ast_cocci.added_string * Data.clt> TDirective %token <Data.clt> TAttr_

parsing_cocci/parser_cocci_menhir.mly +0 -0
@@ -1439,7 +1440,8 @@ reverse_separated_nonempty_llist(separator,

X):

funproto:
s=ioption(storage) i=ioption(Tinline) t=ctype
- id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = match s with None -> [] | Some s -> [Ast0.FStorage

s] in
let i =

parsing_cocci/parser_cocci_menhir.mly +0 -1
 	  match i with
@@ -1453,7 +1455,8 @@ funproto:
 	      P.clt2mcode "(" lp, args, vararg, P.clt2mcode ")" rp,
 	      P.clt2mcode ";" pt)) }
 | i=Tinline s=storage t=ctype
-  id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar

pt=TPtVirg
+ TFunProto id=fn_ident

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines
parsing_cocci/parser_cocci_menhir.mly +1 -1
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = [Ast0.FStorage s] in
         let i = [Ast0.FInline (P.clt2mcode "inline" i)] in
 	let t = [Ast0.FType t] in
--

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#5 lore raw

Re: [Cocci] [PATCH 01/26] parsing_cocci: Add Function Prototype token

Quoted 36 lines · Julia Lawall
Quoted 33 lines · Jaskaran Singh
Quoted 32 lines · Julia Lawall
Quoted 29 lines · Jaskaran Singh

To add the types ParenType and FunctionType to the SmPL AST, a reduce/reduce conflict with the funproto rule of the SmPL parser
must be resolved. This requires explicitly identifying a function
prototype by use of a token (TFunProto).

While the correct method of identifying a function prototype would
be to
check if an identifier is preceded by a return type, it is challenging
to implement. This is because implementing an OCaml function, to
correctly determine a C type in SmPL, without the aid of Yacc, would
have to handle a number of cases (disjunctions, typeof expressions,
etc.).

Thus, a slightly hacky approach is taken to determine a function
prototype with not the best certainty but what works for most cases
in SmPL. If the identifier is preceded by any token that does not
seem to be part of a type, then it is not identified as a function
prototype. Else, it is.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

This sacrifices the test case tests/p1p2.cocci:

What do you mean by sacrifice?

That it doesn't work after applying 01.

Does something that comes later make it work again?

Yes. I applied the series till 16/25 (Got a fatal error before that), where the test worked again.

Cheers,
Jaskaran.

Quoted 296 lines

julia

Quoted 294 lines

The test case seems to work, but not with just 01 applied. Here's what
the debugger tells me about the AST:

Ast_cocci.FunProto
([Ast_cocci.FType
{Ast_cocci.node =

     Ast_cocci.Type (false, None,
      {Ast_cocci.node =
        Ast_cocci.MetaType
         ((("rule starting on line 11", "T"),
           {Ast_cocci.line = 18; column = 1;
            strbef = []; straft = [];
            whitespace = " "},
           Ast_cocci.CONTEXT (Ast_cocci.NoPos,
            Ast_cocci.AFTER
             ([[Ast_cocci.Directive
                 [Ast_cocci.Space
                   "__attribute__((nonnull(2)))"]]],
             Ast_cocci.ONE)),
           []),

Maybe after the grammar changes, Menhir does some kind of lookahead?
I'm not sure.

Cheers,
Jaskaran.

Quoted 265 lines

@@
typedef Error;
type T;
identifier FN;
parameter P1;
@@
T
+__attribute__((nonnull(1)))
FN(P1, Error **errp);

@@
typedef Error;
type T;
identifier FN;
parameter P1;
parameter P2;
@@
T
+__attribute__((nonnull(2)))
FN(P1, P2, Error **errp);

Normally, the only way that you can have a ) before a function call
is
when there is a cast. But hopefully in that case there would not be
two
)) before the function call. Can that get around the problem?

julia

Quoted 231 lines
---
 parsing_cocci/parse_cocci.ml          | 72
+++++++++++++++++++++++----
 parsing_cocci/parser_cocci_menhir.mly |  9 ++--
 2 files changed, 67 insertions(+), 14 deletions(-)
parsing_cocci/parse_cocci.ml +2 -1
index 679d213a..4b2cb7e4 100644
@@ -295,6 +295,7 @@ let token2c (tok,_) add_clt =
   | PC.TLineEnd(clt) -> "line end"
   | PC.TInvalid -> "invalid"
   | PC.TFunDecl(clt) -> "fundecl"
+  | PC.TFunProto(clt) -> "funproto"
 
   | PC.TIso -> "<=>"
   | PC.TRightIso -> "=>"
@@ -480,7 +481,7 @@ let get_clt (tok,_) =
 
   | PC.TOPar0(_,clt) | PC.TMid0(_,clt) | PC.TAnd0(_,clt) |
 PC.TCPar0(_,clt)
   | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
-  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt)
+  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) |
 PC.TFunProto(clt)
   | PC.TFunDecl(clt) | PC.TDirective(_,clt) | PC.TAttr_(clt)
   | PC.TLineEnd(clt) -> clt
   | PC.TVAEllipsis(clt) -> clt
parsing_cocci/parse_cocci.ml +2 -3
@@ -718,6 +719,7 @@ let update_clt (tok,x) clt =
 
   | PC.TLineEnd(_) -> (PC.TLineEnd(clt),x)
   | PC.TFunDecl(_) -> (PC.TFunDecl(clt),x)
+  | PC.TFunProto(_) -> (PC.TFunProto(clt),x)
   | PC.TTildeExclEq(_) -> (PC.TTildeExclEq(clt),x)
   | PC.TDirective(a,_) -> (PC.TDirective(a,clt),x)
   | PC.TAttr_(_) -> (PC.TAttr_(clt),x)
@@ -925,7 +927,7 @@ let split_token ((tok,_) as t) =
   | PC.TInitialize | PC.TFinalize -> ([t],[t])
   | PC.TPArob clt | PC.TMetaPos(_,_,_,clt) |
 PC.TMetaCom(_,_,clt)
-> split t clt
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
   | PC.TEllipsis(clt)
@@ -1006,7 +1008,8 @@ let find_function_names l =
     | _ -> false in
   let rec split acc = function
       [] | [_] -> raise Irrelevant
-    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) ::

rest
->
+ | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) ::
rest
+ | ((PC.TCPar(_),_) as t1) :: ((PC.TPtVirg(_),_) as t2) ::
rest
->

parsing_cocci/parse_cocci.ml +14 -1
 	(List.rev (t1::acc),(t2::rest))
     | x::xs -> split (x::acc) xs in
   let rec balanced_name level = function
@@ -1037,7 +1040,48 @@ let find_function_names l =
     | (PC.TArobArob,_)::_ | (PC.TArob,_)::_ | (PC.EOF,_)::_ ->
 	raise Irrelevant
     | t::rest -> balanced_args level rest in
-  let rec loop = function
+  let rec is_permissible_proto = function
+      [] -> false
+    | (PC.TCPar0(_),_)::
+      ((PC.TMid0(_),_) | (PC.TAnd0(_),_))::
+      (PC.TOPar0(_),_)::_ -> false
+    | (PC.TOPar0(_),_)::rest
+    | (PC.TCPar0(_),_)::rest -> is_permissible_proto rest
+    | x::rest when is_mid x ->
+        let rec loop = function
+          [] -> false
+        | (PC.TOPar0(_),_)::xs -> is_permissible_proto xs
+        | x::xs -> loop xs in
+        loop rest
+    | _::((PC.TEq(_),_) |

(PC.TNotEq(_),_))::(PC.TWhen(_),_)::_

parsing_cocci/parse_cocci.ml +25 -0
+    | _::(PC.TWhen(_),_)::_
+    | (PC.TComma(_),_)::_
+    | (PC.TDirective(_),_)::_
+    | (PC.TElse(_),_)::_
+    | (PC.TReturn(_),_)::_
+    | (PC.TMetaStm(_),_)::_
+    | (PC.TMetaExp(_),_)::_
+    | (PC.TMetaId(_),_)::_
+    | (PC.TMetaLocalIdExp(_),_)::_
+    | (PC.TEq(_),_)::_
+    | (PC.TEllipsis(_),_)::_
+    | (PC.TOEllipsis(_),_)::_
+    | (PC.TCEllipsis(_),_)::_
+    | (PC.TPOEllipsis(_),_)::_
+    | (PC.TPCEllipsis(_),_)::_
+    | (PC.TPtVirg(_),_)::_
+    | (PC.TOBrace(_),_)::_
+    | (PC.TCBrace(_),_)::_
+    | (PC.TOPar(_),_)::_
+    | (PC.TCPar(_),_)::_
+    | (PC.TIdent(_),_)::_ -> false
+    | _ -> true in
+  let decl_or_proto clt info bef aft =
+    match aft with
+      (PC.TOBrace(_),_)::_ -> (((PC.TFunDecl(clt),info) ::

bef),
aft)
+ | (PC.TPtVirg(_),_)::_ -> (((PC.TFunProto(clt),info) ::
bef),
aft)

parsing_cocci/parse_cocci.ml +5 -0
+    | _ -> raise Irrelevant in
+  let rec loop acc = function
       [] -> []
     | t :: rest ->
 	if is_par t || is_mid t || is_ident t
@@ -1046,26 +1090,30 @@ let find_function_names l =
 	    try
 	      let (bef,aft) = split [] (t::rest) in
 	      let rest = balanced_name 0 bef in
+              (match aft with
+                (PC.TPtVirg(_),_)::_
+                 when not(is_permissible_proto acc) -> raise

Irrelevant

parsing_cocci/parse_cocci.ml +2 -2
+              | _ ->
 	      (match rest with
 		(PC.TOPar(_),_)::_ ->
 		  (match balanced_args 0 rest with
 		    [] ->
 		      let (_,info) as h = List.hd bef in
 		      let clt = get_clt h in
-		      (((PC.TFunDecl(clt),info) :: bef), aft)
+                      decl_or_proto clt info bef aft
 		  | (PC.TAttr_(_),_)::rest ->
 		      (match balanced_args 0 rest with
 			[] ->
 			  let (_,info) as h = List.hd bef in
 			  let clt = get_clt h in
-			  (((PC.TFunDecl(clt),info) :: bef),

aft)

parsing_cocci/parse_cocci.ml +5 -6
+                          decl_or_proto clt info bef aft
 		      | _ -> raise Irrelevant)
 		  | _ -> raise Irrelevant)
-	      | _ -> raise Irrelevant)
+	      | _ -> raise Irrelevant))
 	    with Irrelevant -> ([t],rest) in
-	  t @ (loop rest)
-	else t :: (loop rest) in
-  loop l
+          t @ (loop (t @ acc) rest)
+        else t :: (loop (t :: acc) rest) in
+  loop [] l
 
 (* ---------------------------------------------------------
----
---------- *)
 (* an attribute is an identifier that precedes another

identifier
and

parsing_cocci/parse_cocci.ml +2 -0
@@ -1168,6 +1216,8 @@ let detect_types in_meta_decls l =
 	delim::newid::id::(loop false infn (ident::type_names)
 rest)
     | ((PC.TFunDecl(_),_) as fn)::rest ->
 	fn::(loop false 1 type_names rest)
+    | ((PC.TFunProto(_),_) as fn)::rest ->
+	fn::(loop false 1 type_names rest)
     | ((PC.TOPar(_),_) as lp)::rest when infn > 0 ->
 	lp::(loop false (infn + 1) type_names rest)
     | ((PC.TCPar(_),_) as rp)::rest when infn > 0 ->
parsing_cocci/parse_cocci.ml +1 -1
@@ -1252,7 +1302,7 @@ let token2line (tok,_) =
   | PC.TMetaDParamList(_,_,_,_,clt) | PC.TMetaFunc(_,_,_,clt)
   | PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt) |
 PC.TMetaCom(_,_,clt)
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
parsing_cocci/parser_cocci_menhir.mly +0 -0
index 9e6c8a08..26958f63 100644
@@ -239,7 +239,8 @@ let inline_id aft = function
 
 %token <Data.clt> TVAEllipsis
 %token <Data.clt> TIf TElse TWhile TFor TDo TSwitch TCase

TDefault
TReturn
-%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl
Tdecimal Texec
+%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl
TFunProto
+%token <Data.clt> Tdecimal Texec
%token <string * Data.clt> TIdent TTypeId TDeclarerId
TIteratorId
TSymId
%token <Ast_cocci.added_string * Data.clt> TDirective %token <Data.clt> TAttr_

parsing_cocci/parser_cocci_menhir.mly +0 -0
@@ -1439,7 +1440,8 @@
 reverse_separated_nonempty_llist(separator,

X):

funproto:
s=ioption(storage) i=ioption(Tinline) t=ctype
- id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar
pt=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = match s with None -> [] | Some s ->

[Ast0.FStorage
s] in
let i =

parsing_cocci/parser_cocci_menhir.mly +0 -1
 	  match i with
@@ -1453,7 +1455,8 @@ funproto:
 	      P.clt2mcode "(" lp, args, vararg, P.clt2mcode ")"
 rp,
 	      P.clt2mcode ";" pt)) }
 | i=Tinline s=storage t=ctype
-  id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl)

rp=TCPar
pt=TPtVirg
+ TFunProto id=fn_ident

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
parsing_cocci/parser_cocci_menhir.mly +1 -1
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = [Ast0.FStorage s] in
         let i = [Ast0.FInline (P.clt2mcode "inline" i)] in
 	let t = [Ast0.FType t] in
--

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#6 lore raw

Re: [Cocci] [PATCH 01/26] parsing_cocci: Add Function Prototype token

Quoted 41 lines · Jaskaran Singh
Quoted 40 lines · Julia Lawall
Quoted 38 lines · Jaskaran Singh
Quoted 36 lines · Julia Lawall
Quoted 22 lines · Jaskaran Singh

To add the types ParenType and FunctionType to the SmPL AST, a
reduce/reduce conflict with the funproto rule of the SmPL parser
must be resolved. This requires explicitly identifying a function
prototype by use of a token (TFunProto).

While the correct method of identifying a function prototype would
be to
check if an identifier is preceded by a return type, it is challenging
to implement. This is because implementing an OCaml function, to
correctly determine a C type in SmPL, without the aid of Yacc,
would
have to handle a number of cases (disjunctions, typeof expressions,
etc.).

Quoted 9 lines

Thus, a slightly hacky approach is taken to determine a function
prototype with not the best certainty but what works for most cases
in SmPL. If the identifier is preceded by any token that does not
seem to be part of a type, then it is not identified as a function
prototype. Else, it is.

This sacrifices the test case tests/p1p2.cocci:

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

What do you mean by sacrifice?

That it doesn't work after applying 01.

Does something that comes later make it work again?

Yes. I applied the series till 16/25

Correction: 16/26*

Cheers,
Jaskaran.

Quoted 311 lines

(Got a fatal error before that),
where the test worked again.

Cheers,
Jaskaran.

Quoted 305 lines

julia

Quoted 303 lines

The test case seems to work, but not with just 01 applied. Here's what
the debugger tells me about the AST:

Ast_cocci.FunProto
([Ast_cocci.FType
{Ast_cocci.node =

     Ast_cocci.Type (false, None,
      {Ast_cocci.node =
        Ast_cocci.MetaType
         ((("rule starting on line 11", "T"),
           {Ast_cocci.line = 18; column = 1;
            strbef = []; straft = [];
            whitespace = " "},
           Ast_cocci.CONTEXT (Ast_cocci.NoPos,
            Ast_cocci.AFTER
             ([[Ast_cocci.Directive
                 [Ast_cocci.Space
                   "__attribute__((nonnull(2)))"]]],
             Ast_cocci.ONE)),
           []),

Maybe after the grammar changes, Menhir does some kind of lookahead?
I'm not sure.

Cheers,
Jaskaran.

Quoted 274 lines

@@
typedef Error;
type T;
identifier FN;
parameter P1;
@@
T
+__attribute__((nonnull(1)))
FN(P1, Error **errp);

@@
typedef Error;
type T;
identifier FN;
parameter P1;
parameter P2;
@@
T
+__attribute__((nonnull(2)))
FN(P1, P2, Error **errp);

Normally, the only way that you can have a ) before a function call
is
when there is a cast. But hopefully in that case there would not
be
two
)) before the function call. Can that get around the problem?

julia

Quoted 239 lines
Quoted 1 line
---
 parsing_cocci/parse_cocci.ml          | 72
+++++++++++++++++++++++----
 parsing_cocci/parser_cocci_menhir.mly |  9 ++--
 2 files changed, 67 insertions(+), 14 deletions(-)
parsing_cocci/parse_cocci.ml +2 -1
index 679d213a..4b2cb7e4 100644
@@ -295,6 +295,7 @@ let token2c (tok,_) add_clt =
   | PC.TLineEnd(clt) -> "line end"
   | PC.TInvalid -> "invalid"
   | PC.TFunDecl(clt) -> "fundecl"
+  | PC.TFunProto(clt) -> "funproto"
 
   | PC.TIso -> "<=>"
   | PC.TRightIso -> "=>"
@@ -480,7 +481,7 @@ let get_clt (tok,_) =
 
   | PC.TOPar0(_,clt) | PC.TMid0(_,clt) | PC.TAnd0(_,clt) |
 PC.TCPar0(_,clt)
   | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
-  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt)
+  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) |
 PC.TFunProto(clt)
   | PC.TFunDecl(clt) | PC.TDirective(_,clt) | PC.TAttr_(clt)
   | PC.TLineEnd(clt) -> clt
   | PC.TVAEllipsis(clt) -> clt
parsing_cocci/parse_cocci.ml +2 -3
@@ -718,6 +719,7 @@ let update_clt (tok,x) clt =
 
   | PC.TLineEnd(_) -> (PC.TLineEnd(clt),x)
   | PC.TFunDecl(_) -> (PC.TFunDecl(clt),x)
+  | PC.TFunProto(_) -> (PC.TFunProto(clt),x)
   | PC.TTildeExclEq(_) -> (PC.TTildeExclEq(clt),x)
   | PC.TDirective(a,_) -> (PC.TDirective(a,clt),x)
   | PC.TAttr_(_) -> (PC.TAttr_(clt),x)
@@ -925,7 +927,7 @@ let split_token ((tok,_) as t) =
   | PC.TInitialize | PC.TFinalize -> ([t],[t])
   | PC.TPArob clt | PC.TMetaPos(_,_,_,clt) |
 PC.TMetaCom(_,_,clt)
-> split t clt
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
   | PC.TEllipsis(clt)
@@ -1006,7 +1008,8 @@ let find_function_names l =
     | _ -> false in
   let rec split acc = function
       [] | [_] -> raise Irrelevant
-    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2)

::
rest
->
+ | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2)
::
rest
+ | ((PC.TCPar(_),_) as t1) :: ((PC.TPtVirg(_),_) as t2)
::
rest
->

parsing_cocci/parse_cocci.ml +14 -2
 	(List.rev (t1::acc),(t2::rest))
     | x::xs -> split (x::acc) xs in
   let rec balanced_name level = function
@@ -1037,7 +1040,48 @@ let find_function_names l =
     | (PC.TArobArob,_)::_ | (PC.TArob,_)::_ | (PC.EOF,_)::_
->
 	raise Irrelevant
     | t::rest -> balanced_args level rest in
-  let rec loop = function
+  let rec is_permissible_proto = function
+      [] -> false
+    | (PC.TCPar0(_),_)::
+      ((PC.TMid0(_),_) | (PC.TAnd0(_),_))::
+      (PC.TOPar0(_),_)::_ -> false
+    | (PC.TOPar0(_),_)::rest
+    | (PC.TCPar0(_),_)::rest -> is_permissible_proto rest
+    | x::rest when is_mid x ->
+        let rec loop = function
+          [] -> false
+        | (PC.TOPar0(_),_)::xs -> is_permissible_proto xs
+        | x::xs -> loop xs in
+        loop rest
+    | _::((PC.TEq(_),_) |

(PC.TNotEq(_),_))::(PC.TWhen(_),_)::_

parsing_cocci/parse_cocci.ml +25 -0
+    | _::(PC.TWhen(_),_)::_
+    | (PC.TComma(_),_)::_
+    | (PC.TDirective(_),_)::_
+    | (PC.TElse(_),_)::_
+    | (PC.TReturn(_),_)::_
+    | (PC.TMetaStm(_),_)::_
+    | (PC.TMetaExp(_),_)::_
+    | (PC.TMetaId(_),_)::_
+    | (PC.TMetaLocalIdExp(_),_)::_
+    | (PC.TEq(_),_)::_
+    | (PC.TEllipsis(_),_)::_
+    | (PC.TOEllipsis(_),_)::_
+    | (PC.TCEllipsis(_),_)::_
+    | (PC.TPOEllipsis(_),_)::_
+    | (PC.TPCEllipsis(_),_)::_
+    | (PC.TPtVirg(_),_)::_
+    | (PC.TOBrace(_),_)::_
+    | (PC.TCBrace(_),_)::_
+    | (PC.TOPar(_),_)::_
+    | (PC.TCPar(_),_)::_
+    | (PC.TIdent(_),_)::_ -> false
+    | _ -> true in
+  let decl_or_proto clt info bef aft =
+    match aft with
+      (PC.TOBrace(_),_)::_ -> (((PC.TFunDecl(clt),info) ::

bef),
aft)
+ | (PC.TPtVirg(_),_)::_ -> (((PC.TFunProto(clt),info) ::
bef),
aft)

parsing_cocci/parse_cocci.ml +5 -0
+    | _ -> raise Irrelevant in
+  let rec loop acc = function
       [] -> []
     | t :: rest ->
 	if is_par t || is_mid t || is_ident t
@@ -1046,26 +1090,30 @@ let find_function_names l =
 	    try
 	      let (bef,aft) = split [] (t::rest) in
 	      let rest = balanced_name 0 bef in
+              (match aft with
+                (PC.TPtVirg(_),_)::_
+                 when not(is_permissible_proto acc) -> raise

Irrelevant

parsing_cocci/parse_cocci.ml +2 -2
+              | _ ->
 	      (match rest with
 		(PC.TOPar(_),_)::_ ->
 		  (match balanced_args 0 rest with
 		    [] ->
 		      let (_,info) as h = List.hd bef in
 		      let clt = get_clt h in
-		      (((PC.TFunDecl(clt),info) :: bef), aft)
+                      decl_or_proto clt info bef aft
 		  | (PC.TAttr_(_),_)::rest ->
 		      (match balanced_args 0 rest with
 			[] ->
 			  let (_,info) as h = List.hd bef in
 			  let clt = get_clt h in
-			  (((PC.TFunDecl(clt),info) :: bef),

aft)

parsing_cocci/parse_cocci.ml +5 -6
+                          decl_or_proto clt info bef aft
 		      | _ -> raise Irrelevant)
 		  | _ -> raise Irrelevant)
-	      | _ -> raise Irrelevant)
+	      | _ -> raise Irrelevant))
 	    with Irrelevant -> ([t],rest) in
-	  t @ (loop rest)
-	else t :: (loop rest) in
-  loop l
+          t @ (loop (t @ acc) rest)
+        else t :: (loop (t :: acc) rest) in
+  loop [] l
 
 (* ---------------------------------------------------------
----
---------- *)
 (* an attribute is an identifier that precedes another

identifier
and

parsing_cocci/parse_cocci.ml +2 -0
@@ -1168,6 +1216,8 @@ let detect_types in_meta_decls l =
 	delim::newid::id::(loop false infn (ident::type_names)
 rest)
     | ((PC.TFunDecl(_),_) as fn)::rest ->
 	fn::(loop false 1 type_names rest)
+    | ((PC.TFunProto(_),_) as fn)::rest ->
+	fn::(loop false 1 type_names rest)
     | ((PC.TOPar(_),_) as lp)::rest when infn > 0 ->
 	lp::(loop false (infn + 1) type_names rest)
     | ((PC.TCPar(_),_) as rp)::rest when infn > 0 ->
parsing_cocci/parse_cocci.ml +1 -1
@@ -1252,7 +1302,7 @@ let token2line (tok,_) =
   | PC.TMetaDParamList(_,_,_,_,clt) |
 PC.TMetaFunc(_,_,_,clt)
   | PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt) |
 PC.TMetaCom(_,_,clt)
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)

| PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)

parsing_cocci/parser_cocci_menhir.mly +0 -0
index 9e6c8a08..26958f63 100644
@@ -239,7 +239,8 @@ let inline_id aft = function
 
 %token <Data.clt> TVAEllipsis
 %token <Data.clt> TIf TElse TWhile TFor TDo TSwitch TCase

TDefault
TReturn
-%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl
Tdecimal Texec
+%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl
TFunProto
+%token <Data.clt> Tdecimal Texec
%token <string * Data.clt> TIdent TTypeId TDeclarerId
TIteratorId
TSymId
%token <Ast_cocci.added_string * Data.clt> TDirective %token <Data.clt> TAttr_

parsing_cocci/parser_cocci_menhir.mly +0 -0
@@ -1439,7 +1440,8 @@
 reverse_separated_nonempty_llist(separator,

X):

funproto:
s=ioption(storage) i=ioption(Tinline) t=ctype
- id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar
pt=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar

pt=TPtVirg

       { let s = match s with None -> [] | Some s ->

[Ast0.FStorage
s] in
let i =

parsing_cocci/parser_cocci_menhir.mly +0 -1
 	  match i with
@@ -1453,7 +1455,8 @@ funproto:
 	      P.clt2mcode "(" lp, args, vararg, P.clt2mcode ")"
 rp,
 	      P.clt2mcode ";" pt)) }
 | i=Tinline s=storage t=ctype
-  id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl)

rp=TCPar
pt=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

pt=TPtVirg
{ let s = [Ast0.FStorage s] in
let i = [Ast0.FInline (P.clt2mcode "inline" i)] in
let t = [Ast0.FType t] in
--
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#7 lore raw

Re: [Cocci] [PATCH 01/26] parsing_cocci: Add Function Prototype token

In reply to Jaskaran Singh
Quoted 42 lines · Jaskaran Singh
Quoted 41 lines · Julia Lawall
Quoted 37 lines · Jaskaran Singh
Quoted 32 lines · Julia Lawall
Quoted 20 lines · Jaskaran Singh

To add the types ParenType and FunctionType to the SmPL AST, a reduce/reduce conflict with the funproto rule of the SmPL parser
must be resolved. This requires explicitly identifying a function
prototype by use of a token (TFunProto).

While the correct method of identifying a function prototype would
be to
check if an identifier is preceded by a return type, it is challenging
to implement. This is because implementing an OCaml function, to
correctly determine a C type in SmPL, without the aid of Yacc, would
have to handle a number of cases (disjunctions, typeof expressions,
etc.).

Quoted 9 lines

Thus, a slightly hacky approach is taken to determine a function
prototype with not the best certainty but what works for most cases
in SmPL. If the identifier is preceded by any token that does not
seem to be part of a type, then it is not identified as a function
prototype. Else, it is.

Quoted 2 lines

This sacrifices the test case tests/p1p2.cocci:

What do you mean by sacrifice?

Quoted 7 lines
Quoted 4 lines

That it doesn't work after applying 01.

Does something that comes later make it work again?

Yes. I applied the series till 16/25 (Got a fatal error before that), where the test worked again.

I can see how 16 would fix some fatal errors, but I don't see why it would fix the error in p1p2, which I see as:

plus: parse error:
File "tests/p1p2.cocci", line 9, column 3, charpos = 93 around = '(',
whole content = FN(P1, Error **errp);

This means that it failed in parsing the plus slice of the code, ie the prototype with the attribute included.

julia

Quoted 300 lines

Cheers,
Jaskaran.

Quoted 296 lines

julia

Quoted 294 lines

The test case seems to work, but not with just 01 applied. Here's what
the debugger tells me about the AST:

Ast_cocci.FunProto
([Ast_cocci.FType
{Ast_cocci.node =

     Ast_cocci.Type (false, None,
      {Ast_cocci.node =
        Ast_cocci.MetaType
         ((("rule starting on line 11", "T"),
           {Ast_cocci.line = 18; column = 1;
            strbef = []; straft = [];
            whitespace = " "},
           Ast_cocci.CONTEXT (Ast_cocci.NoPos,
            Ast_cocci.AFTER
             ([[Ast_cocci.Directive
                 [Ast_cocci.Space
                   "__attribute__((nonnull(2)))"]]],
             Ast_cocci.ONE)),
           []),

Maybe after the grammar changes, Menhir does some kind of lookahead?
I'm not sure.

Cheers,
Jaskaran.

Quoted 265 lines

@@
typedef Error;
type T;
identifier FN;
parameter P1;
@@
T
+__attribute__((nonnull(1)))
FN(P1, Error **errp);

@@
typedef Error;
type T;
identifier FN;
parameter P1;
parameter P2;
@@
T
+__attribute__((nonnull(2)))
FN(P1, P2, Error **errp);

Normally, the only way that you can have a ) before a function call
is
when there is a cast. But hopefully in that case there would not be
two
)) before the function call. Can that get around the problem?

julia

Quoted 231 lines
---
 parsing_cocci/parse_cocci.ml          | 72
+++++++++++++++++++++++----
 parsing_cocci/parser_cocci_menhir.mly |  9 ++--
 2 files changed, 67 insertions(+), 14 deletions(-)
parsing_cocci/parse_cocci.ml +2 -1
index 679d213a..4b2cb7e4 100644
@@ -295,6 +295,7 @@ let token2c (tok,_) add_clt =
   | PC.TLineEnd(clt) -> "line end"
   | PC.TInvalid -> "invalid"
   | PC.TFunDecl(clt) -> "fundecl"
+  | PC.TFunProto(clt) -> "funproto"
 
   | PC.TIso -> "<=>"
   | PC.TRightIso -> "=>"
@@ -480,7 +481,7 @@ let get_clt (tok,_) =
 
   | PC.TOPar0(_,clt) | PC.TMid0(_,clt) | PC.TAnd0(_,clt) |
 PC.TCPar0(_,clt)
   | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
-  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt)
+  | PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) |
 PC.TFunProto(clt)
   | PC.TFunDecl(clt) | PC.TDirective(_,clt) | PC.TAttr_(clt)
   | PC.TLineEnd(clt) -> clt
   | PC.TVAEllipsis(clt) -> clt
parsing_cocci/parse_cocci.ml +2 -3
@@ -718,6 +719,7 @@ let update_clt (tok,x) clt =
 
   | PC.TLineEnd(_) -> (PC.TLineEnd(clt),x)
   | PC.TFunDecl(_) -> (PC.TFunDecl(clt),x)
+  | PC.TFunProto(_) -> (PC.TFunProto(clt),x)
   | PC.TTildeExclEq(_) -> (PC.TTildeExclEq(clt),x)
   | PC.TDirective(a,_) -> (PC.TDirective(a,clt),x)
   | PC.TAttr_(_) -> (PC.TAttr_(clt),x)
@@ -925,7 +927,7 @@ let split_token ((tok,_) as t) =
   | PC.TInitialize | PC.TFinalize -> ([t],[t])
   | PC.TPArob clt | PC.TMetaPos(_,_,_,clt) |
 PC.TMetaCom(_,_,clt)
-> split t clt
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
   | PC.TEllipsis(clt)
@@ -1006,7 +1008,8 @@ let find_function_names l =
     | _ -> false in
   let rec split acc = function
       [] | [_] -> raise Irrelevant
-    | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) ::

rest
->
+ | ((PC.TCPar(_),_) as t1) :: ((PC.TOBrace(_),_) as t2) ::
rest
+ | ((PC.TCPar(_),_) as t1) :: ((PC.TPtVirg(_),_) as t2) ::
rest
->

parsing_cocci/parse_cocci.ml +14 -1
 	(List.rev (t1::acc),(t2::rest))
     | x::xs -> split (x::acc) xs in
   let rec balanced_name level = function
@@ -1037,7 +1040,48 @@ let find_function_names l =
     | (PC.TArobArob,_)::_ | (PC.TArob,_)::_ | (PC.EOF,_)::_ ->
 	raise Irrelevant
     | t::rest -> balanced_args level rest in
-  let rec loop = function
+  let rec is_permissible_proto = function
+      [] -> false
+    | (PC.TCPar0(_),_)::
+      ((PC.TMid0(_),_) | (PC.TAnd0(_),_))::
+      (PC.TOPar0(_),_)::_ -> false
+    | (PC.TOPar0(_),_)::rest
+    | (PC.TCPar0(_),_)::rest -> is_permissible_proto rest
+    | x::rest when is_mid x ->
+        let rec loop = function
+          [] -> false
+        | (PC.TOPar0(_),_)::xs -> is_permissible_proto xs
+        | x::xs -> loop xs in
+        loop rest
+    | _::((PC.TEq(_),_) |

(PC.TNotEq(_),_))::(PC.TWhen(_),_)::_

parsing_cocci/parse_cocci.ml +25 -0
+    | _::(PC.TWhen(_),_)::_
+    | (PC.TComma(_),_)::_
+    | (PC.TDirective(_),_)::_
+    | (PC.TElse(_),_)::_
+    | (PC.TReturn(_),_)::_
+    | (PC.TMetaStm(_),_)::_
+    | (PC.TMetaExp(_),_)::_
+    | (PC.TMetaId(_),_)::_
+    | (PC.TMetaLocalIdExp(_),_)::_
+    | (PC.TEq(_),_)::_
+    | (PC.TEllipsis(_),_)::_
+    | (PC.TOEllipsis(_),_)::_
+    | (PC.TCEllipsis(_),_)::_
+    | (PC.TPOEllipsis(_),_)::_
+    | (PC.TPCEllipsis(_),_)::_
+    | (PC.TPtVirg(_),_)::_
+    | (PC.TOBrace(_),_)::_
+    | (PC.TCBrace(_),_)::_
+    | (PC.TOPar(_),_)::_
+    | (PC.TCPar(_),_)::_
+    | (PC.TIdent(_),_)::_ -> false
+    | _ -> true in
+  let decl_or_proto clt info bef aft =
+    match aft with
+      (PC.TOBrace(_),_)::_ -> (((PC.TFunDecl(clt),info) ::

bef),
aft)
+ | (PC.TPtVirg(_),_)::_ -> (((PC.TFunProto(clt),info) ::
bef),
aft)

parsing_cocci/parse_cocci.ml +5 -0
+    | _ -> raise Irrelevant in
+  let rec loop acc = function
       [] -> []
     | t :: rest ->
 	if is_par t || is_mid t || is_ident t
@@ -1046,26 +1090,30 @@ let find_function_names l =
 	    try
 	      let (bef,aft) = split [] (t::rest) in
 	      let rest = balanced_name 0 bef in
+              (match aft with
+                (PC.TPtVirg(_),_)::_
+                 when not(is_permissible_proto acc) -> raise

Irrelevant

parsing_cocci/parse_cocci.ml +2 -2
+              | _ ->
 	      (match rest with
 		(PC.TOPar(_),_)::_ ->
 		  (match balanced_args 0 rest with
 		    [] ->
 		      let (_,info) as h = List.hd bef in
 		      let clt = get_clt h in
-		      (((PC.TFunDecl(clt),info) :: bef), aft)
+                      decl_or_proto clt info bef aft
 		  | (PC.TAttr_(_),_)::rest ->
 		      (match balanced_args 0 rest with
 			[] ->
 			  let (_,info) as h = List.hd bef in
 			  let clt = get_clt h in
-			  (((PC.TFunDecl(clt),info) :: bef),

aft)

parsing_cocci/parse_cocci.ml +5 -6
+                          decl_or_proto clt info bef aft
 		      | _ -> raise Irrelevant)
 		  | _ -> raise Irrelevant)
-	      | _ -> raise Irrelevant)
+	      | _ -> raise Irrelevant))
 	    with Irrelevant -> ([t],rest) in
-	  t @ (loop rest)
-	else t :: (loop rest) in
-  loop l
+          t @ (loop (t @ acc) rest)
+        else t :: (loop (t :: acc) rest) in
+  loop [] l
 
 (* ---------------------------------------------------------
----
---------- *)
 (* an attribute is an identifier that precedes another

identifier
and

parsing_cocci/parse_cocci.ml +2 -0
@@ -1168,6 +1216,8 @@ let detect_types in_meta_decls l =
 	delim::newid::id::(loop false infn (ident::type_names)
 rest)
     | ((PC.TFunDecl(_),_) as fn)::rest ->
 	fn::(loop false 1 type_names rest)
+    | ((PC.TFunProto(_),_) as fn)::rest ->
+	fn::(loop false 1 type_names rest)
     | ((PC.TOPar(_),_) as lp)::rest when infn > 0 ->
 	lp::(loop false (infn + 1) type_names rest)
     | ((PC.TCPar(_),_) as rp)::rest when infn > 0 ->
parsing_cocci/parse_cocci.ml +1 -1
@@ -1252,7 +1302,7 @@ let token2line (tok,_) =
   | PC.TMetaDParamList(_,_,_,_,clt) | PC.TMetaFunc(_,_,_,clt)
   | PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt) |
 PC.TMetaCom(_,_,clt)
 
-  | PC.TFunDecl(clt)
+  | PC.TFunDecl(clt) | PC.TFunProto(clt)
   | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
   | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
parsing_cocci/parser_cocci_menhir.mly +0 -0
index 9e6c8a08..26958f63 100644
@@ -239,7 +239,8 @@ let inline_id aft = function
 
 %token <Data.clt> TVAEllipsis
 %token <Data.clt> TIf TElse TWhile TFor TDo TSwitch TCase

TDefault
TReturn
-%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl
Tdecimal Texec
+%token <Data.clt> TBreak TContinue TGoto TSizeof TTypeof TFunDecl
TFunProto
+%token <Data.clt> Tdecimal Texec
%token <string * Data.clt> TIdent TTypeId TDeclarerId
TIteratorId
TSymId
%token <Ast_cocci.added_string * Data.clt> TDirective %token <Data.clt> TAttr_

parsing_cocci/parser_cocci_menhir.mly +0 -0
@@ -1439,7 +1440,8 @@
 reverse_separated_nonempty_llist(separator,

X):

funproto:
s=ioption(storage) i=ioption(Tinline) t=ctype
- id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar
pt=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  TFunProto id=fn_ident
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = match s with None -> [] | Some s ->

[Ast0.FStorage
s] in
let i =

parsing_cocci/parser_cocci_menhir.mly +0 -1
 	  match i with
@@ -1453,7 +1455,8 @@ funproto:
 	      P.clt2mcode "(" lp, args, vararg, P.clt2mcode ")"
 rp,
 	      P.clt2mcode ";" pt)) }
 | i=Tinline s=storage t=ctype
-  id=fn_ident lp=TOPar arglist=arg_list(name_opt_decl)

rp=TCPar
pt=TPtVirg
+ TFunProto id=fn_ident

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
parsing_cocci/parser_cocci_menhir.mly +1 -1
+  lp=TOPar arglist=arg_list(name_opt_decl) rp=TCPar pt=TPtVirg
       { let s = [Ast0.FStorage s] in
         let i = [Ast0.FInline (P.clt2mcode "inline" i)] in
 	let t = [Ast0.FType t] in
--

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#8 lore raw

PATCH 2/26 parsing_cocci: AST: Add ParenType and FunctionType to SmPL ASTs

In reply to Jaskaran Singh

ParenType and FunctionType are types present in the C AST that are not present in the SmPL AST. In the pursuit of aligning both the C and SmPL ASTs, add these types to the SmPL ASTs.

---
 ocaml/coccilib.mli           | 6 ++++++
 parsing_cocci/ast0_cocci.ml  | 3 +++
 parsing_cocci/ast0_cocci.mli | 3 +++
 parsing_cocci/ast_cocci.ml   | 3 +++
 parsing_cocci/ast_cocci.mli  | 3 +++
 5 files changed, 18 insertions(+)
ocaml/coccilib.mli +6 -0
index 5a913099..e5409a97 100644
@@ -2709,6 +2709,9 @@ module Ast_cocci :
       | Pointer of fullType * string mcode
       | FunctionPointer of fullType * string mcode * string mcode *
           string mcode * string mcode * parameter_list * string mcode
+      | ParenType of string mcode (* ( *) * fullType * string mcode (* ) *)
+      | FunctionType of fullType *
+          string mcode (* ( *) * parameter_list * string mcode (* ) *)
       | Array of fullType * string mcode * expression option * string mcode
       | Decimal of string mcode * string mcode * expression *
           string mcode option * expression option * string mcode
@@ -3353,6 +3356,9 @@ module Ast0_cocci :
       | Pointer of typeC * string mcode
       | FunctionPointer of typeC * string mcode * string mcode *
           string mcode * string mcode * parameter_list * string mcode
+      | ParenType of string mcode * typeC * string mcode
+      | FunctionType of typeC *
+          string mcode * parameter_list * string mcode
       | Array of typeC * string mcode * expression option * string mcode
       | Decimal of string mcode * string mcode * expression *
           string mcode option * expression option * string mcode
parsing_cocci/ast0_cocci.ml +3 -0
index 77dc46f0..0acbeaa4 100644
@@ -203,6 +203,9 @@ and base_typeC =
   | FunctionPointer of typeC *
 	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
                   string mcode (* ( *)*parameter_list*string mcode(* ) *)
+  | ParenType       of string mcode (* ( *) * typeC * string mcode (* ) *)
+  | FunctionType    of typeC *
+                  string mcode (* ( *) * parameter_list * string mcode (* ) *)
   | Array           of typeC * string mcode (* [ *) *
 	               expression option * string mcode (* ] *)
   | Decimal         of string mcode (* decimal *) * string mcode (* ( *) *
parsing_cocci/ast0_cocci.mli +3 -0
index 274c6bc2..33bd12b2 100644
@@ -194,6 +194,9 @@ and base_typeC =
   | FunctionPointer of typeC *
 	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
                   string mcode (* ( *)*parameter_list*string mcode(* ) *)
+  | ParenType       of string mcode (* ( *) * typeC * string mcode (* ) *)
+  | FunctionType    of typeC *
+                  string mcode (* ( *) * parameter_list * string mcode (* ) *)
   | Array           of typeC * string mcode (* [ *) *
 	               expression option * string mcode (* ] *)
   | Decimal         of string mcode (* decimal *) * string mcode (* ( *) *
parsing_cocci/ast_cocci.ml +3 -0
index ba6ec29e..f8e6dee6 100644
@@ -343,6 +343,9 @@ and base_typeC =
   | FunctionPointer of fullType *
 	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
                   string mcode (* ( *)*parameter_list*string mcode(* ) *)
+  | ParenType       of string mcode (* ( *) * fullType * string mcode (* ) *)
+  | FunctionType    of fullType *
+                  string mcode (* ( *) * parameter_list * string mcode (* ) *)
   | Array           of fullType * string mcode (* [ *) *
 	               expression option * string mcode (* ] *)
   | Decimal         of string mcode (* decimal *) * string mcode (* ( *) *
parsing_cocci/ast_cocci.mli +3 -0
index 5f21664b..7fb54e4d 100644
@@ -325,6 +325,9 @@ and base_typeC =
   | FunctionPointer of fullType *
 	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
                   string mcode (* ( *)*parameter_list*string mcode(* ) *)
+  | ParenType       of string mcode (* ( *) * fullType * string mcode (* ) *)
+  | FunctionType    of fullType *
+                  string mcode (* ( *) * parameter_list * string mcode (* ) *)
   | Array           of fullType * string mcode (* [ *) *
 	               expression option * string mcode (* ] *)
   | Decimal         of string mcode (* decimal *) * string mcode (* ( *) *
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#9 lore raw

Re: [Cocci] [PATCH 02/26] parsing_cocci: AST: Add ParenType and FunctionType to SmPL ASTs

Quoted 3 lines · Jaskaran Singh

ParenType and FunctionType are types present in the C AST that are not present in the SmPL AST. In the pursuit of aligning both the C and SmPL ASTs, add these types to the SmPL ASTs.

It would be nice to extend the log message to give an example of how these constructors would be used.

julia

Quoted 91 lines
---
 ocaml/coccilib.mli           | 6 ++++++
 parsing_cocci/ast0_cocci.ml  | 3 +++
 parsing_cocci/ast0_cocci.mli | 3 +++
 parsing_cocci/ast_cocci.ml   | 3 +++
 parsing_cocci/ast_cocci.mli  | 3 +++
 5 files changed, 18 insertions(+)
ocaml/coccilib.mli +6 -0
index 5a913099..e5409a97 100644
@@ -2709,6 +2709,9 @@ module Ast_cocci :
       | Pointer of fullType * string mcode
       | FunctionPointer of fullType * string mcode * string mcode *
           string mcode * string mcode * parameter_list * string mcode
+      | ParenType of string mcode (* ( *) * fullType * string mcode (* ) *)
+      | FunctionType of fullType *
+          string mcode (* ( *) * parameter_list * string mcode (* ) *)
       | Array of fullType * string mcode * expression option * string mcode
       | Decimal of string mcode * string mcode * expression *
           string mcode option * expression option * string mcode
@@ -3353,6 +3356,9 @@ module Ast0_cocci :
       | Pointer of typeC * string mcode
       | FunctionPointer of typeC * string mcode * string mcode *
           string mcode * string mcode * parameter_list * string mcode
+      | ParenType of string mcode * typeC * string mcode
+      | FunctionType of typeC *
+          string mcode * parameter_list * string mcode
       | Array of typeC * string mcode * expression option * string mcode
       | Decimal of string mcode * string mcode * expression *
           string mcode option * expression option * string mcode
parsing_cocci/ast0_cocci.ml +3 -0
index 77dc46f0..0acbeaa4 100644
@@ -203,6 +203,9 @@ and base_typeC =
   | FunctionPointer of typeC *
 	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
                   string mcode (* ( *)*parameter_list*string mcode(* ) *)
+  | ParenType       of string mcode (* ( *) * typeC * string mcode (* ) *)
+  | FunctionType    of typeC *
+                  string mcode (* ( *) * parameter_list * string mcode (* ) *)
   | Array           of typeC * string mcode (* [ *) *
 	               expression option * string mcode (* ] *)
   | Decimal         of string mcode (* decimal *) * string mcode (* ( *) *
parsing_cocci/ast0_cocci.mli +3 -0
index 274c6bc2..33bd12b2 100644
@@ -194,6 +194,9 @@ and base_typeC =
   | FunctionPointer of typeC *
 	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
                   string mcode (* ( *)*parameter_list*string mcode(* ) *)
+  | ParenType       of string mcode (* ( *) * typeC * string mcode (* ) *)
+  | FunctionType    of typeC *
+                  string mcode (* ( *) * parameter_list * string mcode (* ) *)
   | Array           of typeC * string mcode (* [ *) *
 	               expression option * string mcode (* ] *)
   | Decimal         of string mcode (* decimal *) * string mcode (* ( *) *
parsing_cocci/ast_cocci.ml +3 -0
index ba6ec29e..f8e6dee6 100644
@@ -343,6 +343,9 @@ and base_typeC =
   | FunctionPointer of fullType *
 	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
                   string mcode (* ( *)*parameter_list*string mcode(* ) *)
+  | ParenType       of string mcode (* ( *) * fullType * string mcode (* ) *)
+  | FunctionType    of fullType *
+                  string mcode (* ( *) * parameter_list * string mcode (* ) *)
   | Array           of fullType * string mcode (* [ *) *
 	               expression option * string mcode (* ] *)
   | Decimal         of string mcode (* decimal *) * string mcode (* ( *) *
parsing_cocci/ast_cocci.mli +3 -1
index 5f21664b..7fb54e4d 100644
@@ -325,6 +325,9 @@ and base_typeC =
   | FunctionPointer of fullType *
 	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
                   string mcode (* ( *)*parameter_list*string mcode(* ) *)
+  | ParenType       of string mcode (* ( *) * fullType * string mcode (* ) *)
+  | FunctionType    of fullType *
+                  string mcode (* ( *) * parameter_list * string mcode (* ) *)
   | Array           of fullType * string mcode (* [ *) *
 	               expression option * string mcode (* ] *)
   | Decimal         of string mcode (* decimal *) * string mcode (* ( *) *
--
Quoted 3 lines

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#10 lore raw

PATCH 3/26 parsing_cocci: parser: Add direct_declarator/direct_abstract_d rules

In reply to Jaskaran Singh

The direct_declarator rule and the direct_abstract_d rule are present in the C parser. Add similar rules to the SmPL parser so that declarators are parsed as they are in the C parser.

For the type ParenType, direct_declarator and direct_abstract_d only allow the following productions:

	( * id  [ .* ] ) ( params )

i.e. a function pointer or an array of function pointers. The compromise is flexibility in the range of productions, mainly because collateral evolutions needed by having a flexible rule are very large and distasteful.

Replace usage of the older d_ident rule in the SmPL parser with the above mentioned rules. All usages of d_ident, however, have not been removed due to reduce/reduce conflicts.

Remove rules/productions that parse function pointers with usage of direct_declarator and direct_abstract_d.

---
 parsing_cocci/parser_cocci_menhir.mly | 239 +++++++++++++-------------
 1 file changed, 116 insertions(+), 123 deletions(-)
parsing_cocci/parser_cocci_menhir.mly +116 -123
index 26958f63..fade830f 100644
@@ -1116,33 +1116,17 @@ struct_decl_one:
 	{ let (mids,code) = t in
 	Ast0.wrap
 	  (Ast0.ConjField(P.id2mcode lp,code,mids, P.id2mcode rp)) }
-    | t=ctype d=d_ident_option bf=struct_bitfield? pv=TPtVirg
+    | t=ctype d=direct_decl_option(disj_ident) bf=struct_bitfield? pv=TPtVirg
 	 { let (id,fn) = d in
 	 Ast0.wrap(Ast0.Field(fn t,id,bf,P.clt2mcode ";" pv)) }
-    | t=ctype lp1=TOPar st=TMul d=d_ident_option rp1=TCPar
-	lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-	bf=struct_bitfield? pv=TPtVirg
-        { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        Ast0.wrap(Ast0.Field(fn t,id,bf,P.clt2mcode ";" pv)) }
-     | cv=ioption(const_vol) i=pure_ident_or_symbol d=d_ident_option
+    | cv=ioption(const_vol) i=pure_ident_or_symbol
+      d=direct_decl_option(disj_ident)
 	 bf=struct_bitfield?
 	 pv=TPtVirg
 	 { let (id,fn) = d in
 	 let idtype = P.make_cv cv (Ast0.wrap (Ast0.TypeName(P.id2mcode i))) in
 	 Ast0.wrap(Ast0.Field(fn idtype,id,bf,P.clt2mcode ";" pv)) }
 
-d_ident_option:
-	 { None, (fun x -> x) }
-     | d=d_ident {
-       let (id, fn) = d in
-       (Some id, fn)
-    }
-
 struct_bitfield:
    c=TDotDot e=expr { (P.clt2mcode ":" c, e) }
 
@@ -1529,17 +1513,9 @@ storage:
        | s=Tregister    { P.clt2mcode Ast.Register s }
        | s=Textern      { P.clt2mcode Ast.Extern s }
 
-decl: t=ctype i=disj_ident a=list(array_dec)
-	{ let t = P.arrayify t a in Ast0.wrap(Ast0.Param(t, Some i)) }
+decl: t=ctype d=direct_declarator(disj_ident)
+	{ let (i,fn) = d in Ast0.wrap(Ast0.Param(fn t, Some i)) }
     | t=ctype { (*verify in FunDecl*) Ast0.wrap(Ast0.Param(t, None)) }
-    | t=ctype lp=TOPar s=TMul i=disj_ident rp=TCPar
-	lp1=TOPar d=decl_list(name_opt_decl) rp1=TCPar
-        { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp,P.clt2mcode "*" s,P.clt2mcode ")" rp,
-		P.clt2mcode "(" lp1,d,P.clt2mcode ")" rp1)) in
-	Ast0.wrap(Ast0.Param(fnptr, Some i)) }
     | TMetaParam
 	{ let (nm,cstr,pure,clt) = $1 in
 	Ast0.wrap(Ast0.MetaParam(P.clt2mcode nm clt,cstr,pure)) }
@@ -1547,14 +1523,6 @@ decl: t=ctype i=disj_ident a=list(array_dec)
 
 name_opt_decl:
       decl  { $1 }
-    | t=ctype lp=TOPar s=TMul rp=TCPar
-	lp1=TOPar d=decl_list(name_opt_decl) rp1=TCPar
-        { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp,P.clt2mcode "*" s,P.clt2mcode ")" rp,
-		P.clt2mcode "(" lp1,d,P.clt2mcode ")" rp1)) in
-	Ast0.wrap(Ast0.Param(fnptr, None)) }
 
 const_vol:
       Tconst       { P.clt2mcode Ast.Const $1 }
@@ -1684,13 +1652,15 @@ decl_var:
     t=ctype pv=TPtVirg
       { [Ast0.wrap(Ast0.TyDecl(t,P.clt2mcode ";" pv))] }
   | TMetaDecl { [P.meta_decl $1] }
-  | s=ioption(storage) t=ctype d=comma_list(d_ident) pv=TPtVirg
+  | s=ioption(storage) t=ctype
+      d=comma_list(direct_declarator(disj_ident)) pv=TPtVirg
       { List.map
 	  (function (id,fn) ->
 	    Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv)))
 	  d }
   | f=funproto { [f] }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list q=TEq e=initialize
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+    a=attr_list q=TEq e=initialize
       pv=TPtVirg
       {let (id,fn) = d in
       [Ast0.wrap
@@ -1711,18 +1681,6 @@ decl_var:
       let idtype = P.make_cv cv (Ast0.wrap (Ast0.TypeName(P.id2mcode i))) in
       [Ast0.wrap(Ast0.Init(s,fn idtype,id,a,P.clt2mcode "=" q,e,
 			   P.clt2mcode ";" pv))] }
-  /* function pointer type */
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        [Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv))] }
   | s=ioption(storage) d=decl_ident o=TOPar e=eexpr_list_option c=TCPar
       p=TPtVirg
       { [Ast0.wrap(Ast0.MacroDecl(s,d,P.clt2mcode "(" o,e,
@@ -1735,56 +1693,25 @@ decl_var:
 	       (s,d,P.clt2mcode "(" o,e,
 		P.clt2mcode ")" c,P.clt2mcode "=" q,i,
 		P.clt2mcode ";" p))] }
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    q=TEq e=initialize pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-      [Ast0.wrap
-	  (Ast0.Init(s,fn t,id,[],P.clt2mcode "=" q,e,P.clt2mcode ";" pv))]}
-  | s=Ttypedef t=typedef_ctype id=comma_list(typedef_ident) pv=TPtVirg
+  | s=Ttypedef t=typedef_ctype
+      d=comma_list(direct_declarator(typedef_ident)) pv=TPtVirg
       { let s = P.clt2mcode "typedef" s in
         List.map
-	  (function id ->
-	    Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv)))
-	  id }
-  | s=Ttypedef t=typedef_ctype id=typedef_ident
-      l=TOCro i=eexpr r=TCCro pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t = P.arrayify t [(l,Some i,r)] in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))] }
-  | s=Ttypedef t=typedef_ctype id=typedef_ident
-      l=TOCro i=eexpr r=TCCro
-      l2=TOCro i2=eexpr r2=TCCro pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t = P.arrayify t [(l,Some i,r)] in
-        let t = P.arrayify t [(l2,Some i2,r2)] in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))] }
-  | s=Ttypedef
-    t=typedef_ctype lp1=TOPar st=TMul id=typedef_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))]}
+	  (function (id,fn) ->
+	    Ast0.wrap(Ast0.Typedef(s,fn t,id,P.clt2mcode ";" pv)))
+	  d }
 
 one_decl_var:
     t=ctype pv=TPtVirg
       { Ast0.wrap(Ast0.TyDecl(t,P.clt2mcode ";" pv)) }
   | TMetaDecl { P.meta_decl $1 }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list pv=TPtVirg
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+      a=attr_list pv=TPtVirg
       { let (id,fn) = d in
         Ast0.wrap(Ast0.UnInit(s,fn t,id,a,P.clt2mcode ";" pv)) }
   | f=funproto { f }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list q=TEq e=initialize
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+      a=attr_list q=TEq e=initialize
       pv=TPtVirg
       { let (id,fn) = d in
       Ast0.wrap
@@ -1802,18 +1729,6 @@ one_decl_var:
       let idtype = P.make_cv cv (Ast0.wrap (Ast0.TypeName(P.id2mcode i))) in
       Ast0.wrap(Ast0.Init(s,fn idtype,id,a,P.clt2mcode "=" q,e,
 			   P.clt2mcode ";" pv)) }
-  /* function pointer type */
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv)) }
   | s=ioption(storage) d=decl_ident o=TOPar e=eexpr_list_option c=TCPar
       p=TPtVirg
       { Ast0.wrap(Ast0.MacroDecl(s,d,P.clt2mcode "(" o,e,
@@ -1826,19 +1741,103 @@ one_decl_var:
                (s,d,P.clt2mcode "(" o,e,
                 P.clt2mcode ")" c,P.clt2mcode "=" q,i,
                 P.clt2mcode ";" p)) }
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar a=attr_list
-    q=TEq e=initialize pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-      Ast0.wrap(Ast0.Init(s,fn t,id,a,P.clt2mcode "=" q,e,P.clt2mcode ";" pv))}
 
 
+direct_declarator(ident_type):
+    ident_type
+      { ($1, function x -> x) }
+  | o=TOPar m=list(TMul) d=direct_declarator(ident_type) c=TCPar
+      { let (id,fn) = d in
+        (id,
+         function t ->
+          let ty =  fn (P.pointerify t m) in
+          let _ =
+           match Ast0.unwrap ty with
+            Ast0.Pointer(ty1,_) ->
+             (match Ast0.unwrap ty1 with
+               Ast0.FunctionType(ty2,_,_,_) -> ()
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function pointer"))
+           | Ast0.Array(ty1,_,_,_) ->
+              (match Ast0.unwrap ty1 with
+                Ast0.Pointer(ty2,_) ->(
+                 match Ast0.unwrap ty2 with
+                  Ast0.FunctionType(ty3,_,_,_) -> ()
+                 | _ ->
+                  raise (Semantic_cocci.Semantic "expected function pointer"))
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function pointer"))
+           | _ ->
+            raise (Semantic_cocci.Semantic "expected function pointer") in
+          Ast0.wrap
+            (Ast0.ParenType
+               (P.clt2mcode "(" o,
+                ty,
+                P.clt2mcode ")" c))) }
+  | d=direct_declarator(ident_type) a=array_dec
+      { let (id,fn) = d in
+        (id, function t -> let r = P.arrayify t [a] in fn r) }
+  | d=direct_declarator(ident_type)
+      o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { let (id,fn) = d in
+        (id, function t ->
+              let r =
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c)) in fn r) }
+
+
+direct_abstract_d:
+    o=TOPar m=list(TMul) d=direct_abstract_d c=TCPar
+      { function t ->
+          let ty = d (P.pointerify t m) in
+          let _ =
+           match Ast0.unwrap ty with
+            Ast0.Pointer(ty1,_) ->
+             (match Ast0.unwrap ty1 with
+               Ast0.FunctionType(ty2,_,_,_) ->()
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function pointer"))
+           | Ast0.Array(ty1,_,_,_) ->
+              (match Ast0.unwrap ty1 with
+                Ast0.Pointer(ty2,_) ->(
+                 match Ast0.unwrap ty2 with
+                  Ast0.FunctionType(ty3,_,_,_) -> ()
+                 | _ ->
+                  raise (Semantic_cocci.Semantic "expected function pointer"))
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function pointer"))
+           | _ ->
+            raise (Semantic_cocci.Semantic "expected function pointer") in
+          Ast0.wrap
+            (Ast0.ParenType
+               (P.clt2mcode "(" o,
+                ty,
+                P.clt2mcode ")" c))
+      }
+  | a=array_dec
+      { function t -> P.arrayify t [a] }
+  | d=direct_abstract_d a=array_dec
+      { function t -> let r = P.arrayify t [a] in d r }
+  | o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { function t ->
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c)) }
+  | d=direct_abstract_d o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { function t ->
+              let r =
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c)) in d r }
+
+
+direct_decl_option(ident_type):
+      { (None, function x -> x) }
+  | d=direct_declarator(ident_type)
+      { let (id,fn) = d in
+        (Some id, fn) }
+
 d_ident:
     disj_ident list(array_dec)
       { ($1, function t -> P.arrayify t $2) }
@@ -2151,16 +2150,10 @@ cast_expr(r,pe):
     unary_expr(r,pe)                      { $1 }
   | lp=TOPar t=ctype rp=TCPar e=cast_expr(r,pe)
       { Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, t,
+                             P.clt2mcode ")" rp, e)) }
+  | lp=TOPar t=ctype d=direct_abstract_d rp=TCPar e=cast_expr(r,pe)
+      { Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, d t,
 			     P.clt2mcode ")" rp, e)) }
-  | lp=TOPar t=ctype lp1=TOPar s=TMul rp1=TCPar
-      lp2=TOPar d=decl_list(name_opt_decl) rp2=TCPar rp=TCPar
-      e=cast_expr(r,pe)
-      { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" s,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,d,P.clt2mcode ")" rp2)) in
-      Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, fnptr, P.clt2mcode ")" rp, e)) }
 
 unary_expr(r,pe):
     postfix_expr(r,pe)                   { $1 }
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#11 lore raw

Re: [Cocci] [PATCH 03/26] parsing_cocci: parser: Add direct_declarator/direct_abstract_d rules

Quoted 7 lines · Jaskaran Singh

The direct_declarator rule and the direct_abstract_d rule are present in the C parser. Add similar rules to the SmPL parser so that declarators are parsed as they are in the C parser.

For the type ParenType, direct_declarator and direct_abstract_d only allow the following productions:

Quoted 6 lines
	( * id  [ .* ] ) ( params )

i.e. a function pointer or an array of function pointers. The compromise is flexibility in the range of productions, mainly because collateral evolutions needed by having a flexible rule are very large and distasteful.

It is not clear what is meant by flexible. Can you give an example of what will not be supported?

julia

Quoted 346 lines

Replace usage of the older d_ident rule in the SmPL parser with the above mentioned rules. All usages of d_ident, however, have not been removed due to reduce/reduce conflicts.

Remove rules/productions that parse function pointers with usage of direct_declarator and direct_abstract_d.

---
 parsing_cocci/parser_cocci_menhir.mly | 239 +++++++++++++-------------
 1 file changed, 116 insertions(+), 123 deletions(-)
parsing_cocci/parser_cocci_menhir.mly +116 -124
index 26958f63..fade830f 100644
@@ -1116,33 +1116,17 @@ struct_decl_one:
 	{ let (mids,code) = t in
 	Ast0.wrap
 	  (Ast0.ConjField(P.id2mcode lp,code,mids, P.id2mcode rp)) }
-    | t=ctype d=d_ident_option bf=struct_bitfield? pv=TPtVirg
+    | t=ctype d=direct_decl_option(disj_ident) bf=struct_bitfield? pv=TPtVirg
 	 { let (id,fn) = d in
 	 Ast0.wrap(Ast0.Field(fn t,id,bf,P.clt2mcode ";" pv)) }
-    | t=ctype lp1=TOPar st=TMul d=d_ident_option rp1=TCPar
-	lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-	bf=struct_bitfield? pv=TPtVirg
-        { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        Ast0.wrap(Ast0.Field(fn t,id,bf,P.clt2mcode ";" pv)) }
-     | cv=ioption(const_vol) i=pure_ident_or_symbol d=d_ident_option
+    | cv=ioption(const_vol) i=pure_ident_or_symbol
+      d=direct_decl_option(disj_ident)
 	 bf=struct_bitfield?
 	 pv=TPtVirg
 	 { let (id,fn) = d in
 	 let idtype = P.make_cv cv (Ast0.wrap (Ast0.TypeName(P.id2mcode i))) in
 	 Ast0.wrap(Ast0.Field(fn idtype,id,bf,P.clt2mcode ";" pv)) }
 
-d_ident_option:
-	 { None, (fun x -> x) }
-     | d=d_ident {
-       let (id, fn) = d in
-       (Some id, fn)
-    }
-
 struct_bitfield:
    c=TDotDot e=expr { (P.clt2mcode ":" c, e) }
 
@@ -1529,17 +1513,9 @@ storage:
        | s=Tregister    { P.clt2mcode Ast.Register s }
        | s=Textern      { P.clt2mcode Ast.Extern s }
 
-decl: t=ctype i=disj_ident a=list(array_dec)
-	{ let t = P.arrayify t a in Ast0.wrap(Ast0.Param(t, Some i)) }
+decl: t=ctype d=direct_declarator(disj_ident)
+	{ let (i,fn) = d in Ast0.wrap(Ast0.Param(fn t, Some i)) }
     | t=ctype { (*verify in FunDecl*) Ast0.wrap(Ast0.Param(t, None)) }
-    | t=ctype lp=TOPar s=TMul i=disj_ident rp=TCPar
-	lp1=TOPar d=decl_list(name_opt_decl) rp1=TCPar
-        { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp,P.clt2mcode "*" s,P.clt2mcode ")" rp,
-		P.clt2mcode "(" lp1,d,P.clt2mcode ")" rp1)) in
-	Ast0.wrap(Ast0.Param(fnptr, Some i)) }
     | TMetaParam
 	{ let (nm,cstr,pure,clt) = $1 in
 	Ast0.wrap(Ast0.MetaParam(P.clt2mcode nm clt,cstr,pure)) }
@@ -1547,14 +1523,6 @@ decl: t=ctype i=disj_ident a=list(array_dec)
 
 name_opt_decl:
       decl  { $1 }
-    | t=ctype lp=TOPar s=TMul rp=TCPar
-	lp1=TOPar d=decl_list(name_opt_decl) rp1=TCPar
-        { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp,P.clt2mcode "*" s,P.clt2mcode ")" rp,
-		P.clt2mcode "(" lp1,d,P.clt2mcode ")" rp1)) in
-	Ast0.wrap(Ast0.Param(fnptr, None)) }
 
 const_vol:
       Tconst       { P.clt2mcode Ast.Const $1 }
@@ -1684,13 +1652,15 @@ decl_var:
     t=ctype pv=TPtVirg
       { [Ast0.wrap(Ast0.TyDecl(t,P.clt2mcode ";" pv))] }
   | TMetaDecl { [P.meta_decl $1] }
-  | s=ioption(storage) t=ctype d=comma_list(d_ident) pv=TPtVirg
+  | s=ioption(storage) t=ctype
+      d=comma_list(direct_declarator(disj_ident)) pv=TPtVirg
       { List.map
 	  (function (id,fn) ->
 	    Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv)))
 	  d }
   | f=funproto { [f] }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list q=TEq e=initialize
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+    a=attr_list q=TEq e=initialize
       pv=TPtVirg
       {let (id,fn) = d in
       [Ast0.wrap
@@ -1711,18 +1681,6 @@ decl_var:
       let idtype = P.make_cv cv (Ast0.wrap (Ast0.TypeName(P.id2mcode i))) in
       [Ast0.wrap(Ast0.Init(s,fn idtype,id,a,P.clt2mcode "=" q,e,
 			   P.clt2mcode ";" pv))] }
-  /* function pointer type */
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        [Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv))] }
   | s=ioption(storage) d=decl_ident o=TOPar e=eexpr_list_option c=TCPar
       p=TPtVirg
       { [Ast0.wrap(Ast0.MacroDecl(s,d,P.clt2mcode "(" o,e,
@@ -1735,56 +1693,25 @@ decl_var:
 	       (s,d,P.clt2mcode "(" o,e,
 		P.clt2mcode ")" c,P.clt2mcode "=" q,i,
 		P.clt2mcode ";" p))] }
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    q=TEq e=initialize pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-      [Ast0.wrap
-	  (Ast0.Init(s,fn t,id,[],P.clt2mcode "=" q,e,P.clt2mcode ";" pv))]}
-  | s=Ttypedef t=typedef_ctype id=comma_list(typedef_ident) pv=TPtVirg
+  | s=Ttypedef t=typedef_ctype
+      d=comma_list(direct_declarator(typedef_ident)) pv=TPtVirg
       { let s = P.clt2mcode "typedef" s in
         List.map
-	  (function id ->
-	    Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv)))
-	  id }
-  | s=Ttypedef t=typedef_ctype id=typedef_ident
-      l=TOCro i=eexpr r=TCCro pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t = P.arrayify t [(l,Some i,r)] in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))] }
-  | s=Ttypedef t=typedef_ctype id=typedef_ident
-      l=TOCro i=eexpr r=TCCro
-      l2=TOCro i2=eexpr r2=TCCro pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t = P.arrayify t [(l,Some i,r)] in
-        let t = P.arrayify t [(l2,Some i2,r2)] in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))] }
-  | s=Ttypedef
-    t=typedef_ctype lp1=TOPar st=TMul id=typedef_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))]}
+	  (function (id,fn) ->
+	    Ast0.wrap(Ast0.Typedef(s,fn t,id,P.clt2mcode ";" pv)))
+	  d }
 
 one_decl_var:
     t=ctype pv=TPtVirg
       { Ast0.wrap(Ast0.TyDecl(t,P.clt2mcode ";" pv)) }
   | TMetaDecl { P.meta_decl $1 }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list pv=TPtVirg
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+      a=attr_list pv=TPtVirg
       { let (id,fn) = d in
         Ast0.wrap(Ast0.UnInit(s,fn t,id,a,P.clt2mcode ";" pv)) }
   | f=funproto { f }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list q=TEq e=initialize
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+      a=attr_list q=TEq e=initialize
       pv=TPtVirg
       { let (id,fn) = d in
       Ast0.wrap
@@ -1802,18 +1729,6 @@ one_decl_var:
       let idtype = P.make_cv cv (Ast0.wrap (Ast0.TypeName(P.id2mcode i))) in
       Ast0.wrap(Ast0.Init(s,fn idtype,id,a,P.clt2mcode "=" q,e,
 			   P.clt2mcode ";" pv)) }
-  /* function pointer type */
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv)) }
   | s=ioption(storage) d=decl_ident o=TOPar e=eexpr_list_option c=TCPar
       p=TPtVirg
       { Ast0.wrap(Ast0.MacroDecl(s,d,P.clt2mcode "(" o,e,
@@ -1826,19 +1741,103 @@ one_decl_var:
                (s,d,P.clt2mcode "(" o,e,
                 P.clt2mcode ")" c,P.clt2mcode "=" q,i,
                 P.clt2mcode ";" p)) }
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar a=attr_list
-    q=TEq e=initialize pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-      Ast0.wrap(Ast0.Init(s,fn t,id,a,P.clt2mcode "=" q,e,P.clt2mcode ";" pv))}
 
 
+direct_declarator(ident_type):
+    ident_type
+      { ($1, function x -> x) }
+  | o=TOPar m=list(TMul) d=direct_declarator(ident_type) c=TCPar
+      { let (id,fn) = d in
+        (id,
+         function t ->
+          let ty =  fn (P.pointerify t m) in
+          let _ =
+           match Ast0.unwrap ty with
+            Ast0.Pointer(ty1,_) ->
+             (match Ast0.unwrap ty1 with
+               Ast0.FunctionType(ty2,_,_,_) -> ()
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function pointer"))
+           | Ast0.Array(ty1,_,_,_) ->
+              (match Ast0.unwrap ty1 with
+                Ast0.Pointer(ty2,_) ->(
+                 match Ast0.unwrap ty2 with
+                  Ast0.FunctionType(ty3,_,_,_) -> ()
+                 | _ ->
+                  raise (Semantic_cocci.Semantic "expected function pointer"))
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function pointer"))
+           | _ ->
+            raise (Semantic_cocci.Semantic "expected function pointer") in
+          Ast0.wrap
+            (Ast0.ParenType
+               (P.clt2mcode "(" o,
+                ty,
+                P.clt2mcode ")" c))) }
+  | d=direct_declarator(ident_type) a=array_dec
+      { let (id,fn) = d in
+        (id, function t -> let r = P.arrayify t [a] in fn r) }
+  | d=direct_declarator(ident_type)
+      o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { let (id,fn) = d in
+        (id, function t ->
+              let r =
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c)) in fn r) }
+
+
+direct_abstract_d:
+    o=TOPar m=list(TMul) d=direct_abstract_d c=TCPar
+      { function t ->
+          let ty = d (P.pointerify t m) in
+          let _ =
+           match Ast0.unwrap ty with
+            Ast0.Pointer(ty1,_) ->
+             (match Ast0.unwrap ty1 with
+               Ast0.FunctionType(ty2,_,_,_) ->()
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function pointer"))
+           | Ast0.Array(ty1,_,_,_) ->
+              (match Ast0.unwrap ty1 with
+                Ast0.Pointer(ty2,_) ->(
+                 match Ast0.unwrap ty2 with
+                  Ast0.FunctionType(ty3,_,_,_) -> ()
+                 | _ ->
+                  raise (Semantic_cocci.Semantic "expected function pointer"))
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function pointer"))
+           | _ ->
+            raise (Semantic_cocci.Semantic "expected function pointer") in
+          Ast0.wrap
+            (Ast0.ParenType
+               (P.clt2mcode "(" o,
+                ty,
+                P.clt2mcode ")" c))
+      }
+  | a=array_dec
+      { function t -> P.arrayify t [a] }
+  | d=direct_abstract_d a=array_dec
+      { function t -> let r = P.arrayify t [a] in d r }
+  | o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { function t ->
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c)) }
+  | d=direct_abstract_d o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { function t ->
+              let r =
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c)) in d r }
+
+
+direct_decl_option(ident_type):
+      { (None, function x -> x) }
+  | d=direct_declarator(ident_type)
+      { let (id,fn) = d in
+        (Some id, fn) }
+
 d_ident:
     disj_ident list(array_dec)
       { ($1, function t -> P.arrayify t $2) }
@@ -2151,16 +2150,10 @@ cast_expr(r,pe):
     unary_expr(r,pe)                      { $1 }
   | lp=TOPar t=ctype rp=TCPar e=cast_expr(r,pe)
       { Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, t,
+                             P.clt2mcode ")" rp, e)) }
+  | lp=TOPar t=ctype d=direct_abstract_d rp=TCPar e=cast_expr(r,pe)
+      { Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, d t,
 			     P.clt2mcode ")" rp, e)) }
-  | lp=TOPar t=ctype lp1=TOPar s=TMul rp1=TCPar
-      lp2=TOPar d=decl_list(name_opt_decl) rp2=TCPar rp=TCPar
-      e=cast_expr(r,pe)
-      { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" s,P.clt2mcode ")" rp1,
-		P.clt2mcode "(" lp2,d,P.clt2mcode ")" rp2)) in
-      Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, fnptr, P.clt2mcode ")" rp, e)) }
 
 unary_expr(r,pe):
     postfix_expr(r,pe)                   { $1 }
--
Quoted 3 lines

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#12 lore raw

Re: [Cocci] [PATCH 03/26] parsing_cocci: parser: Add direct_declarator/direct_abstract_d rules

Quoted 21 lines · Julia Lawall
Quoted 9 lines · Jaskaran Singh

The direct_declarator rule and the direct_abstract_d rule are present in the C parser. Add similar rules to the SmPL parser so that
declarators are parsed as they are in the C parser.

For the type ParenType, direct_declarator and direct_abstract_d only
allow the following productions:

Quoted 8 lines
	( * id  [ .* ] ) ( params )

i.e. a function pointer or an array of function pointers. The compromise
is flexibility in the range of productions, mainly because collateral
evolutions needed by having a flexible rule are very large and distasteful.

Quoted 4 lines

It is not clear what is meant by flexible. Can you give an example of
what will not be supported?

Well, it's mainly anything that uses ParenType for anything other than a function pointer or an array of function pointers. Examples of productions that aren't supported:

int (**x[])(params); // array of pointer to function pointer

int (*x[][])(params); // array of array of function pointers

int ((*x))(params); // ident wrapped in double parantheses

To be honest, I think the first two and similar ones (i.e. array of array of ... pointer to pointer to ....) can be supported by implementing some kind of loop, but don't know about other productions.

Cheers,
Jaskaran.

Quoted 385 lines

julia

Quoted 383 lines

Replace usage of the older d_ident rule in the SmPL parser with the above mentioned rules. All usages of d_ident, however, have not been
removed due to reduce/reduce conflicts.

Remove rules/productions that parse function pointers with usage of direct_declarator and direct_abstract_d.

---
 parsing_cocci/parser_cocci_menhir.mly | 239 +++++++++++++---------
----
 1 file changed, 116 insertions(+), 123 deletions(-)
parsing_cocci/parser_cocci_menhir.mly +1 -1
index 26958f63..fade830f 100644
@@ -1116,33 +1116,17 @@ struct_decl_one:
 	{ let (mids,code) = t in
 	Ast0.wrap
 	  (Ast0.ConjField(P.id2mcode lp,code,mids, P.id2mcode rp)) }
-    | t=ctype d=d_ident_option bf=struct_bitfield? pv=TPtVirg
+    | t=ctype d=direct_decl_option(disj_ident) bf=struct_bitfield?

pv=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +0 -8
 	 { let (id,fn) = d in
 	 Ast0.wrap(Ast0.Field(fn t,id,bf,P.clt2mcode ";" pv)) }
-    | t=ctype lp1=TOPar st=TMul d=d_ident_option rp1=TCPar
-	lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-	bf=struct_bitfield? pv=TPtVirg
-        { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode

")" rp1,

parsing_cocci/parser_cocci_menhir.mly +0 -3
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        Ast0.wrap(Ast0.Field(fn t,id,bf,P.clt2mcode ";" pv)) }
-     | cv=ioption(const_vol) i=pure_ident_or_symbol

d=d_ident_option

parsing_cocci/parser_cocci_menhir.mly +2 -0
+    | cv=ioption(const_vol) i=pure_ident_or_symbol
+      d=direct_decl_option(disj_ident)
 	 bf=struct_bitfield?
 	 pv=TPtVirg
 	 { let (id,fn) = d in
 	 let idtype = P.make_cv cv (Ast0.wrap (Ast0.TypeName(P.id2mcode

i))) in

parsing_cocci/parser_cocci_menhir.mly +2 -17
 	 Ast0.wrap(Ast0.Field(fn idtype,id,bf,P.clt2mcode ";" pv)) }
 
-d_ident_option:
-	 { None, (fun x -> x) }
-     | d=d_ident {
-       let (id, fn) = d in
-       (Some id, fn)
-    }
-
 struct_bitfield:
    c=TDotDot e=expr { (P.clt2mcode ":" c, e) }
@@ -1529,17 +1513,9 @@ storage:
        | s=Tregister    { P.clt2mcode Ast.Register s }
        | s=Textern      { P.clt2mcode Ast.Extern s }
 
-decl: t=ctype i=disj_ident a=list(array_dec)
-	{ let t = P.arrayify t a in Ast0.wrap(Ast0.Param(t, Some i)) }
+decl: t=ctype d=direct_declarator(disj_ident)
+	{ let (i,fn) = d in Ast0.wrap(Ast0.Param(fn t, Some i)) }
     | t=ctype { (*verify in FunDecl*) Ast0.wrap(Ast0.Param(t,
 None)) }
-    | t=ctype lp=TOPar s=TMul i=disj_ident rp=TCPar
-	lp1=TOPar d=decl_list(name_opt_decl) rp1=TCPar
-        { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp,P.clt2mcode "*" s,P.clt2mcode ")"
 rp,
-		P.clt2mcode "(" lp1,d,P.clt2mcode ")" rp1)) in
-	Ast0.wrap(Ast0.Param(fnptr, Some i)) }
     | TMetaParam
parsing_cocci/parser_cocci_menhir.mly +0 -8
 	{ let (nm,cstr,pure,clt) = $1 in
 	Ast0.wrap(Ast0.MetaParam(P.clt2mcode nm clt,cstr,pure)) }
@@ -1547,14 +1523,6 @@ decl: t=ctype i=disj_ident a=list(array_dec)
 
 name_opt_decl:
       decl  { $1 }
-    | t=ctype lp=TOPar s=TMul rp=TCPar
-	lp1=TOPar d=decl_list(name_opt_decl) rp1=TCPar
-        { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp,P.clt2mcode "*" s,P.clt2mcode ")"
 rp,
-		P.clt2mcode "(" lp1,d,P.clt2mcode ")" rp1)) in
-	Ast0.wrap(Ast0.Param(fnptr, None)) }
 
 const_vol:
       Tconst       { P.clt2mcode Ast.Const $1 }
parsing_cocci/parser_cocci_menhir.mly +2 -2
@@ -1684,13 +1652,15 @@ decl_var:
     t=ctype pv=TPtVirg
       { [Ast0.wrap(Ast0.TyDecl(t,P.clt2mcode ";" pv))] }
   | TMetaDecl { [P.meta_decl $1] }
-  | s=ioption(storage) t=ctype d=comma_list(d_ident) pv=TPtVirg
+  | s=ioption(storage) t=ctype
+      d=comma_list(direct_declarator(disj_ident)) pv=TPtVirg
       { List.map
 	  (function (id,fn) ->
 	    Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv)))
 	  d }
   | f=funproto { [f] }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list q=TEq

e=initialize

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+    a=attr_list q=TEq e=initialize
       pv=TPtVirg
       {let (id,fn) = d in
       [Ast0.wrap
@@ -1711,18 +1681,6 @@ decl_var:
       let idtype = P.make_cv cv (Ast0.wrap

(Ast0.TypeName(P.id2mcode i))) in

       [Ast0.wrap(Ast0.Init(s,fn idtype,id,a,P.clt2mcode "=" q,e,
parsing_cocci/parser_cocci_menhir.mly +0 -10
 			   P.clt2mcode ";" pv))] }
-  /* function pointer type */
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode

")" rp1,

parsing_cocci/parser_cocci_menhir.mly +0 -2
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        [Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv))]

}
| s=ioption(storage) d=decl_ident o=TOPar e=eexpr_list_option
c=TCPar

       p=TPtVirg
       { [Ast0.wrap(Ast0.MacroDecl(s,d,P.clt2mcode "(" o,e,
parsing_cocci/parser_cocci_menhir.mly +0 -13
@@ -1735,56 +1693,25 @@ decl_var:
 	       (s,d,P.clt2mcode "(" o,e,
 		P.clt2mcode ")" c,P.clt2mcode "=" q,i,
 		P.clt2mcode ";" p))] }
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    q=TEq e=initialize pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode
 ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-      [Ast0.wrap
-	  (Ast0.Init(s,fn t,id,[],P.clt2mcode "=" q,e,P.clt2mcode ";"
 pv))]}
-  | s=Ttypedef t=typedef_ctype id=comma_list(typedef_ident)

pv=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +2 -23
+  | s=Ttypedef t=typedef_ctype
+      d=comma_list(direct_declarator(typedef_ident)) pv=TPtVirg
       { let s = P.clt2mcode "typedef" s in
         List.map
-	  (function id ->
-	    Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv)))
-	  id }
-  | s=Ttypedef t=typedef_ctype id=typedef_ident
-      l=TOCro i=eexpr r=TCCro pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t = P.arrayify t [(l,Some i,r)] in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))] }
-  | s=Ttypedef t=typedef_ctype id=typedef_ident
-      l=TOCro i=eexpr r=TCCro
-      l2=TOCro i2=eexpr r2=TCCro pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t = P.arrayify t [(l,Some i,r)] in
-        let t = P.arrayify t [(l2,Some i2,r2)] in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))] }
-  | s=Ttypedef
-    t=typedef_ctype lp1=TOPar st=TMul id=typedef_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode

")" rp1,

parsing_cocci/parser_cocci_menhir.mly +5 -4
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))]}
+	  (function (id,fn) ->
+	    Ast0.wrap(Ast0.Typedef(s,fn t,id,P.clt2mcode ";" pv)))
+	  d }
 
 one_decl_var:
     t=ctype pv=TPtVirg
       { Ast0.wrap(Ast0.TyDecl(t,P.clt2mcode ";" pv)) }
   | TMetaDecl { P.meta_decl $1 }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list pv=TPtVirg
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+      a=attr_list pv=TPtVirg
       { let (id,fn) = d in
         Ast0.wrap(Ast0.UnInit(s,fn t,id,a,P.clt2mcode ";" pv)) }
   | f=funproto { f }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list q=TEq

e=initialize

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+      a=attr_list q=TEq e=initialize
       pv=TPtVirg
       { let (id,fn) = d in
       Ast0.wrap
@@ -1802,18 +1729,6 @@ one_decl_var:
       let idtype = P.make_cv cv (Ast0.wrap

(Ast0.TypeName(P.id2mcode i))) in

       Ast0.wrap(Ast0.Init(s,fn idtype,id,a,P.clt2mcode "=" q,e,
parsing_cocci/parser_cocci_menhir.mly +0 -10
 			   P.clt2mcode ";" pv)) }
-  /* function pointer type */
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode

")" rp1,

parsing_cocci/parser_cocci_menhir.mly +0 -2
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv)) }
   | s=ioption(storage) d=decl_ident o=TOPar e=eexpr_list_option

c=TCPar

       p=TPtVirg
       { Ast0.wrap(Ast0.MacroDecl(s,d,P.clt2mcode "(" o,e,
parsing_cocci/parser_cocci_menhir.mly +22 -11
@@ -1826,19 +1741,103 @@ one_decl_var:
                (s,d,P.clt2mcode "(" o,e,
                 P.clt2mcode ")" c,P.clt2mcode "=" q,i,
                 P.clt2mcode ";" p)) }
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar a=attr_list
-    q=TEq e=initialize pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode
 ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-      Ast0.wrap(Ast0.Init(s,fn t,id,a,P.clt2mcode "="
 q,e,P.clt2mcode ";" pv))}
 
 
+direct_declarator(ident_type):
+    ident_type
+      { ($1, function x -> x) }
+  | o=TOPar m=list(TMul) d=direct_declarator(ident_type) c=TCPar
+      { let (id,fn) = d in
+        (id,
+         function t ->
+          let ty =  fn (P.pointerify t m) in
+          let _ =
+           match Ast0.unwrap ty with
+            Ast0.Pointer(ty1,_) ->
+             (match Ast0.unwrap ty1 with
+               Ast0.FunctionType(ty2,_,_,_) -> ()
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function
 pointer"))
+           | Ast0.Array(ty1,_,_,_) ->
+              (match Ast0.unwrap ty1 with
+                Ast0.Pointer(ty2,_) ->(
+                 match Ast0.unwrap ty2 with
+                  Ast0.FunctionType(ty3,_,_,_) -> ()
+                 | _ ->
+                  raise (Semantic_cocci.Semantic "expected

function pointer"))

parsing_cocci/parser_cocci_menhir.mly +2 -0
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function

pointer"))

parsing_cocci/parser_cocci_menhir.mly +2 -0
+           | _ ->
+            raise (Semantic_cocci.Semantic "expected function

pointer") in

parsing_cocci/parser_cocci_menhir.mly +16 -0
+          Ast0.wrap
+            (Ast0.ParenType
+               (P.clt2mcode "(" o,
+                ty,
+                P.clt2mcode ")" c))) }
+  | d=direct_declarator(ident_type) a=array_dec
+      { let (id,fn) = d in
+        (id, function t -> let r = P.arrayify t [a] in fn r) }
+  | d=direct_declarator(ident_type)
+      o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { let (id,fn) = d in
+        (id, function t ->
+              let r =
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c))

in fn r) }

parsing_cocci/parser_cocci_menhir.mly +13 -0
+
+
+direct_abstract_d:
+    o=TOPar m=list(TMul) d=direct_abstract_d c=TCPar
+      { function t ->
+          let ty = d (P.pointerify t m) in
+          let _ =
+           match Ast0.unwrap ty with
+            Ast0.Pointer(ty1,_) ->
+             (match Ast0.unwrap ty1 with
+               Ast0.FunctionType(ty2,_,_,_) ->()
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function

pointer"))

parsing_cocci/parser_cocci_menhir.mly +7 -0
+           | Ast0.Array(ty1,_,_,_) ->
+              (match Ast0.unwrap ty1 with
+                Ast0.Pointer(ty2,_) ->(
+                 match Ast0.unwrap ty2 with
+                  Ast0.FunctionType(ty3,_,_,_) -> ()
+                 | _ ->
+                  raise (Semantic_cocci.Semantic "expected

function pointer"))

parsing_cocci/parser_cocci_menhir.mly +2 -0
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function

pointer"))

parsing_cocci/parser_cocci_menhir.mly +2 -0
+           | _ ->
+            raise (Semantic_cocci.Semantic "expected function

pointer") in

parsing_cocci/parser_cocci_menhir.mly +15 -0
+          Ast0.wrap
+            (Ast0.ParenType
+               (P.clt2mcode "(" o,
+                ty,
+                P.clt2mcode ")" c))
+      }
+  | a=array_dec
+      { function t -> P.arrayify t [a] }
+  | d=direct_abstract_d a=array_dec
+      { function t -> let r = P.arrayify t [a] in d r }
+  | o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { function t ->
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c))

}
+ | d=direct_abstract_d o=TOPar arglist=decl_list(name_opt_decl) c=TCPar

parsing_cocci/parser_cocci_menhir.mly +5 -0
+      { function t ->
+              let r =
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c))

in d r }

parsing_cocci/parser_cocci_menhir.mly +11 -9
+
+
+direct_decl_option(ident_type):
+      { (None, function x -> x) }
+  | d=direct_declarator(ident_type)
+      { let (id,fn) = d in
+        (Some id, fn) }
+
 d_ident:
     disj_ident list(array_dec)
       { ($1, function t -> P.arrayify t $2) }
@@ -2151,16 +2150,10 @@ cast_expr(r,pe):
     unary_expr(r,pe)                      { $1 }
   | lp=TOPar t=ctype rp=TCPar e=cast_expr(r,pe)
       { Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, t,
+                             P.clt2mcode ")" rp, e)) }
+  | lp=TOPar t=ctype d=direct_abstract_d rp=TCPar
 e=cast_expr(r,pe)
+      { Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, d t,
 			     P.clt2mcode ")" rp, e)) }
-  | lp=TOPar t=ctype lp1=TOPar s=TMul rp1=TCPar
-      lp2=TOPar d=decl_list(name_opt_decl) rp2=TCPar rp=TCPar
-      e=cast_expr(r,pe)
-      { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" s,P.clt2mcode ")"
 rp1,
-		P.clt2mcode "(" lp2,d,P.clt2mcode ")" rp2)) in
-      Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, fnptr, P.clt2mcode
 ")" rp, e)) }
Quoted 6 lines
Quoted 6 lines

unary_expr(r,pe):

     postfix_expr(r,pe)                   { $1 }

--
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#13 lore raw

Re: [Cocci] [PATCH 03/26] parsing_cocci: parser: Add direct_declarator/direct_abstract_d rules

Quoted 33 lines · Jaskaran Singh
Quoted 21 lines · Julia Lawall
Quoted 9 lines · Jaskaran Singh

The direct_declarator rule and the direct_abstract_d rule are present in the C parser. Add similar rules to the SmPL parser so that
declarators are parsed as they are in the C parser.

For the type ParenType, direct_declarator and direct_abstract_d only
allow the following productions:

Quoted 8 lines
	( * id  [ .* ] ) ( params )

i.e. a function pointer or an array of function pointers. The compromise
is flexibility in the range of productions, mainly because collateral
evolutions needed by having a flexible rule are very large and distasteful.

Quoted 4 lines

It is not clear what is meant by flexible. Can you give an example of
what will not be supported?

Well, it's mainly anything that uses ParenType for anything other than a function pointer or an array of function pointers. Examples of productions that aren't supported:

int (**x[])(params); // array of pointer to function pointer

Quoted 8 lines

int (*x[][])(params); // array of array of function pointers

int ((*x))(params); // ident wrapped in double parantheses

To be honest, I think the first two and similar ones (i.e. array of array of ... pointer to pointer to ....) can be supported by implementing some kind of loop, but don't know about other productions.

OK. You could add these to the log message for future reference. The double parentheses seem like a low priority. Perhaps the first two could be useful at some point.

thanks,
julia

Quoted 389 lines

Cheers,
Jaskaran.

Quoted 385 lines

julia

Quoted 383 lines

Replace usage of the older d_ident rule in the SmPL parser with the above mentioned rules. All usages of d_ident, however, have not been
removed due to reduce/reduce conflicts.

Remove rules/productions that parse function pointers with usage of direct_declarator and direct_abstract_d.

---
 parsing_cocci/parser_cocci_menhir.mly | 239 +++++++++++++---------
----
 1 file changed, 116 insertions(+), 123 deletions(-)
parsing_cocci/parser_cocci_menhir.mly +1 -1
index 26958f63..fade830f 100644
@@ -1116,33 +1116,17 @@ struct_decl_one:
 	{ let (mids,code) = t in
 	Ast0.wrap
 	  (Ast0.ConjField(P.id2mcode lp,code,mids, P.id2mcode rp)) }
-    | t=ctype d=d_ident_option bf=struct_bitfield? pv=TPtVirg
+    | t=ctype d=direct_decl_option(disj_ident) bf=struct_bitfield?

pv=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +0 -8
 	 { let (id,fn) = d in
 	 Ast0.wrap(Ast0.Field(fn t,id,bf,P.clt2mcode ";" pv)) }
-    | t=ctype lp1=TOPar st=TMul d=d_ident_option rp1=TCPar
-	lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-	bf=struct_bitfield? pv=TPtVirg
-        { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode

")" rp1,

parsing_cocci/parser_cocci_menhir.mly +0 -3
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        Ast0.wrap(Ast0.Field(fn t,id,bf,P.clt2mcode ";" pv)) }
-     | cv=ioption(const_vol) i=pure_ident_or_symbol

d=d_ident_option

parsing_cocci/parser_cocci_menhir.mly +2 -0
+    | cv=ioption(const_vol) i=pure_ident_or_symbol
+      d=direct_decl_option(disj_ident)
 	 bf=struct_bitfield?
 	 pv=TPtVirg
 	 { let (id,fn) = d in
 	 let idtype = P.make_cv cv (Ast0.wrap (Ast0.TypeName(P.id2mcode

i))) in

parsing_cocci/parser_cocci_menhir.mly +2 -17
 	 Ast0.wrap(Ast0.Field(fn idtype,id,bf,P.clt2mcode ";" pv)) }
 
-d_ident_option:
-	 { None, (fun x -> x) }
-     | d=d_ident {
-       let (id, fn) = d in
-       (Some id, fn)
-    }
-
 struct_bitfield:
    c=TDotDot e=expr { (P.clt2mcode ":" c, e) }
@@ -1529,17 +1513,9 @@ storage:
        | s=Tregister    { P.clt2mcode Ast.Register s }
        | s=Textern      { P.clt2mcode Ast.Extern s }
 
-decl: t=ctype i=disj_ident a=list(array_dec)
-	{ let t = P.arrayify t a in Ast0.wrap(Ast0.Param(t, Some i)) }
+decl: t=ctype d=direct_declarator(disj_ident)
+	{ let (i,fn) = d in Ast0.wrap(Ast0.Param(fn t, Some i)) }
     | t=ctype { (*verify in FunDecl*) Ast0.wrap(Ast0.Param(t,
 None)) }
-    | t=ctype lp=TOPar s=TMul i=disj_ident rp=TCPar
-	lp1=TOPar d=decl_list(name_opt_decl) rp1=TCPar
-        { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp,P.clt2mcode "*" s,P.clt2mcode ")"
 rp,
-		P.clt2mcode "(" lp1,d,P.clt2mcode ")" rp1)) in
-	Ast0.wrap(Ast0.Param(fnptr, Some i)) }
     | TMetaParam
parsing_cocci/parser_cocci_menhir.mly +0 -8
 	{ let (nm,cstr,pure,clt) = $1 in
 	Ast0.wrap(Ast0.MetaParam(P.clt2mcode nm clt,cstr,pure)) }
@@ -1547,14 +1523,6 @@ decl: t=ctype i=disj_ident a=list(array_dec)
 
 name_opt_decl:
       decl  { $1 }
-    | t=ctype lp=TOPar s=TMul rp=TCPar
-	lp1=TOPar d=decl_list(name_opt_decl) rp1=TCPar
-        { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp,P.clt2mcode "*" s,P.clt2mcode ")"
 rp,
-		P.clt2mcode "(" lp1,d,P.clt2mcode ")" rp1)) in
-	Ast0.wrap(Ast0.Param(fnptr, None)) }
 
 const_vol:
       Tconst       { P.clt2mcode Ast.Const $1 }
parsing_cocci/parser_cocci_menhir.mly +2 -2
@@ -1684,13 +1652,15 @@ decl_var:
     t=ctype pv=TPtVirg
       { [Ast0.wrap(Ast0.TyDecl(t,P.clt2mcode ";" pv))] }
   | TMetaDecl { [P.meta_decl $1] }
-  | s=ioption(storage) t=ctype d=comma_list(d_ident) pv=TPtVirg
+  | s=ioption(storage) t=ctype
+      d=comma_list(direct_declarator(disj_ident)) pv=TPtVirg
       { List.map
 	  (function (id,fn) ->
 	    Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv)))
 	  d }
   | f=funproto { [f] }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list q=TEq

e=initialize

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+    a=attr_list q=TEq e=initialize
       pv=TPtVirg
       {let (id,fn) = d in
       [Ast0.wrap
@@ -1711,18 +1681,6 @@ decl_var:
       let idtype = P.make_cv cv (Ast0.wrap

(Ast0.TypeName(P.id2mcode i))) in

       [Ast0.wrap(Ast0.Init(s,fn idtype,id,a,P.clt2mcode "=" q,e,
parsing_cocci/parser_cocci_menhir.mly +0 -10
 			   P.clt2mcode ";" pv))] }
-  /* function pointer type */
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode

")" rp1,

parsing_cocci/parser_cocci_menhir.mly +0 -2
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        [Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv))]

}
| s=ioption(storage) d=decl_ident o=TOPar e=eexpr_list_option
c=TCPar

       p=TPtVirg
       { [Ast0.wrap(Ast0.MacroDecl(s,d,P.clt2mcode "(" o,e,
parsing_cocci/parser_cocci_menhir.mly +0 -13
@@ -1735,56 +1693,25 @@ decl_var:
 	       (s,d,P.clt2mcode "(" o,e,
 		P.clt2mcode ")" c,P.clt2mcode "=" q,i,
 		P.clt2mcode ";" p))] }
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    q=TEq e=initialize pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode
 ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-      [Ast0.wrap
-	  (Ast0.Init(s,fn t,id,[],P.clt2mcode "=" q,e,P.clt2mcode ";"
 pv))]}
-  | s=Ttypedef t=typedef_ctype id=comma_list(typedef_ident)

pv=TPtVirg

parsing_cocci/parser_cocci_menhir.mly +2 -23
+  | s=Ttypedef t=typedef_ctype
+      d=comma_list(direct_declarator(typedef_ident)) pv=TPtVirg
       { let s = P.clt2mcode "typedef" s in
         List.map
-	  (function id ->
-	    Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv)))
-	  id }
-  | s=Ttypedef t=typedef_ctype id=typedef_ident
-      l=TOCro i=eexpr r=TCCro pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t = P.arrayify t [(l,Some i,r)] in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))] }
-  | s=Ttypedef t=typedef_ctype id=typedef_ident
-      l=TOCro i=eexpr r=TCCro
-      l2=TOCro i2=eexpr r2=TCCro pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t = P.arrayify t [(l,Some i,r)] in
-        let t = P.arrayify t [(l2,Some i2,r2)] in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))] }
-  | s=Ttypedef
-    t=typedef_ctype lp1=TOPar st=TMul id=typedef_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar pv=TPtVirg
-      { let s = P.clt2mcode "typedef" s in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode

")" rp1,

parsing_cocci/parser_cocci_menhir.mly +5 -4
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-	[Ast0.wrap(Ast0.Typedef(s,t,id,P.clt2mcode ";" pv))]}
+	  (function (id,fn) ->
+	    Ast0.wrap(Ast0.Typedef(s,fn t,id,P.clt2mcode ";" pv)))
+	  d }
 
 one_decl_var:
     t=ctype pv=TPtVirg
       { Ast0.wrap(Ast0.TyDecl(t,P.clt2mcode ";" pv)) }
   | TMetaDecl { P.meta_decl $1 }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list pv=TPtVirg
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+      a=attr_list pv=TPtVirg
       { let (id,fn) = d in
         Ast0.wrap(Ast0.UnInit(s,fn t,id,a,P.clt2mcode ";" pv)) }
   | f=funproto { f }
-  | s=ioption(storage) t=ctype d=d_ident a=attr_list q=TEq

e=initialize

parsing_cocci/parser_cocci_menhir.mly +2 -0
+  | s=ioption(storage) t=ctype d=direct_declarator(disj_ident)
+      a=attr_list q=TEq e=initialize
       pv=TPtVirg
       { let (id,fn) = d in
       Ast0.wrap
@@ -1802,18 +1729,6 @@ one_decl_var:
       let idtype = P.make_cv cv (Ast0.wrap

(Ast0.TypeName(P.id2mcode i))) in

       Ast0.wrap(Ast0.Init(s,fn idtype,id,a,P.clt2mcode "=" q,e,
parsing_cocci/parser_cocci_menhir.mly +0 -10
 			   P.clt2mcode ";" pv)) }
-  /* function pointer type */
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar
-    pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode

")" rp1,

parsing_cocci/parser_cocci_menhir.mly +0 -2
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-        Ast0.wrap(Ast0.UnInit(s,fn t,id,[],P.clt2mcode ";" pv)) }
   | s=ioption(storage) d=decl_ident o=TOPar e=eexpr_list_option

c=TCPar

       p=TPtVirg
       { Ast0.wrap(Ast0.MacroDecl(s,d,P.clt2mcode "(" o,e,
parsing_cocci/parser_cocci_menhir.mly +22 -11
@@ -1826,19 +1741,103 @@ one_decl_var:
                (s,d,P.clt2mcode "(" o,e,
                 P.clt2mcode ")" c,P.clt2mcode "=" q,i,
                 P.clt2mcode ";" p)) }
-  | s=ioption(storage)
-    t=ctype lp1=TOPar st=TMul d=d_ident rp1=TCPar
-    lp2=TOPar p=decl_list(name_opt_decl) rp2=TCPar a=attr_list
-    q=TEq e=initialize pv=TPtVirg
-      { let (id,fn) = d in
-        let t =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" st,P.clt2mcode
 ")" rp1,
-		P.clt2mcode "(" lp2,p,P.clt2mcode ")" rp2)) in
-      Ast0.wrap(Ast0.Init(s,fn t,id,a,P.clt2mcode "="
 q,e,P.clt2mcode ";" pv))}
 
 
+direct_declarator(ident_type):
+    ident_type
+      { ($1, function x -> x) }
+  | o=TOPar m=list(TMul) d=direct_declarator(ident_type) c=TCPar
+      { let (id,fn) = d in
+        (id,
+         function t ->
+          let ty =  fn (P.pointerify t m) in
+          let _ =
+           match Ast0.unwrap ty with
+            Ast0.Pointer(ty1,_) ->
+             (match Ast0.unwrap ty1 with
+               Ast0.FunctionType(ty2,_,_,_) -> ()
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function
 pointer"))
+           | Ast0.Array(ty1,_,_,_) ->
+              (match Ast0.unwrap ty1 with
+                Ast0.Pointer(ty2,_) ->(
+                 match Ast0.unwrap ty2 with
+                  Ast0.FunctionType(ty3,_,_,_) -> ()
+                 | _ ->
+                  raise (Semantic_cocci.Semantic "expected

function pointer"))

parsing_cocci/parser_cocci_menhir.mly +2 -0
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function

pointer"))

parsing_cocci/parser_cocci_menhir.mly +2 -0
+           | _ ->
+            raise (Semantic_cocci.Semantic "expected function

pointer") in

parsing_cocci/parser_cocci_menhir.mly +16 -0
+          Ast0.wrap
+            (Ast0.ParenType
+               (P.clt2mcode "(" o,
+                ty,
+                P.clt2mcode ")" c))) }
+  | d=direct_declarator(ident_type) a=array_dec
+      { let (id,fn) = d in
+        (id, function t -> let r = P.arrayify t [a] in fn r) }
+  | d=direct_declarator(ident_type)
+      o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { let (id,fn) = d in
+        (id, function t ->
+              let r =
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c))

in fn r) }

parsing_cocci/parser_cocci_menhir.mly +13 -0
+
+
+direct_abstract_d:
+    o=TOPar m=list(TMul) d=direct_abstract_d c=TCPar
+      { function t ->
+          let ty = d (P.pointerify t m) in
+          let _ =
+           match Ast0.unwrap ty with
+            Ast0.Pointer(ty1,_) ->
+             (match Ast0.unwrap ty1 with
+               Ast0.FunctionType(ty2,_,_,_) ->()
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function

pointer"))

parsing_cocci/parser_cocci_menhir.mly +7 -0
+           | Ast0.Array(ty1,_,_,_) ->
+              (match Ast0.unwrap ty1 with
+                Ast0.Pointer(ty2,_) ->(
+                 match Ast0.unwrap ty2 with
+                  Ast0.FunctionType(ty3,_,_,_) -> ()
+                 | _ ->
+                  raise (Semantic_cocci.Semantic "expected

function pointer"))

parsing_cocci/parser_cocci_menhir.mly +2 -0
+             | _ ->
+              raise (Semantic_cocci.Semantic "expected function

pointer"))

parsing_cocci/parser_cocci_menhir.mly +2 -0
+           | _ ->
+            raise (Semantic_cocci.Semantic "expected function

pointer") in

parsing_cocci/parser_cocci_menhir.mly +15 -0
+          Ast0.wrap
+            (Ast0.ParenType
+               (P.clt2mcode "(" o,
+                ty,
+                P.clt2mcode ")" c))
+      }
+  | a=array_dec
+      { function t -> P.arrayify t [a] }
+  | d=direct_abstract_d a=array_dec
+      { function t -> let r = P.arrayify t [a] in d r }
+  | o=TOPar arglist=decl_list(name_opt_decl) c=TCPar
+      { function t ->
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c))

}
+ | d=direct_abstract_d o=TOPar arglist=decl_list(name_opt_decl) c=TCPar

parsing_cocci/parser_cocci_menhir.mly +5 -0
+      { function t ->
+              let r =
+               Ast0.wrap
+                (Ast0.FunctionType
+                  (t,P.clt2mcode "(" o,arglist,P.clt2mcode ")" c))

in d r }

parsing_cocci/parser_cocci_menhir.mly +11 -9
+
+
+direct_decl_option(ident_type):
+      { (None, function x -> x) }
+  | d=direct_declarator(ident_type)
+      { let (id,fn) = d in
+        (Some id, fn) }
+
 d_ident:
     disj_ident list(array_dec)
       { ($1, function t -> P.arrayify t $2) }
@@ -2151,16 +2150,10 @@ cast_expr(r,pe):
     unary_expr(r,pe)                      { $1 }
   | lp=TOPar t=ctype rp=TCPar e=cast_expr(r,pe)
       { Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, t,
+                             P.clt2mcode ")" rp, e)) }
+  | lp=TOPar t=ctype d=direct_abstract_d rp=TCPar
 e=cast_expr(r,pe)
+      { Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, d t,
 			     P.clt2mcode ")" rp, e)) }
-  | lp=TOPar t=ctype lp1=TOPar s=TMul rp1=TCPar
-      lp2=TOPar d=decl_list(name_opt_decl) rp2=TCPar rp=TCPar
-      e=cast_expr(r,pe)
-      { let fnptr =
-	  Ast0.wrap
-	    (Ast0.FunctionPointer
-	       (t,P.clt2mcode "(" lp1,P.clt2mcode "*" s,P.clt2mcode ")"
 rp1,
-		P.clt2mcode "(" lp2,d,P.clt2mcode ")" rp2)) in
-      Ast0.wrap(Ast0.Cast (P.clt2mcode "(" lp, fnptr, P.clt2mcode
 ")" rp, e)) }
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

unary_expr(r,pe):

     postfix_expr(r,pe)                   { $1 }

--
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#14 lore raw

PATCH 4/26 parsing_cocci: visitor_ast0: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

	<type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
 parsing_cocci/visitor_ast0.ml | 163 ++++++++++++++++++++++++++++++++++
 1 file changed, 163 insertions(+)
parsing_cocci/visitor_ast0.ml +163 -0
index c282e1f8..c56cd7a7 100644
@@ -339,6 +339,12 @@ let visitor mode bind option_default
 	| Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	    let (t,id) =
               function_pointer (ty,lp1,star,None,rp1,lp2,params,rp2) in t
+        | Ast0.ParenType(lp,ty,rp) ->
+	    let (t,id) =
+              parentype_type (lp,ty,None,rp) in t
+        | Ast0.FunctionType(ty,lp,params,rp) ->
+	    let (t,id) =
+              functiontype_type (ty,None,lp,params,rp) in t
 	| Ast0.Array(ty,lb,size,rb) ->
             let (t,id) = array_type (ty,None,lb,size,rb) in t
 	| Ast0.Decimal(dec,lp,length,comma,precision_opt,rp) ->
@@ -435,6 +441,76 @@ let visitor mode bind option_default
     ((multibind ([ty_n] @ idl @ [lb_n;size_n;rb_n]),
      Ast0.Array(ty,lb,size,rb)), idu)
 
+  and parentype_type (lp,ty,(id : Ast0.ident option),rp) =
+    match Ast0.unwrap ty with
+      Ast0.Pointer(ty1,star) ->
+        (match Ast0.unwrap ty1 with
+          Ast0.FunctionType(ty2,lp2,params,rp2) ->
+            let (ty_n,typ) = typeC ty2 in
+            let (lp_n,lp) = string_mcode lp in
+            let (star_n,star) = string_mcode star in
+            let (idl,idu) = (match id with
+              | Some a -> let (b,c) = ident a in ([b],Some c)
+              | None -> ([],None)) in
+            let (rp_n,rp) = string_mcode rp in
+            let (lp2_n,lp2) = string_mcode lp2 in
+            let (params_n,params) = parameter_dots params in
+            let (rp2_n,rp2) = string_mcode rp2 in
+            ((multibind ([ty_n;lp_n;star_n] @ idl @
+              [rp_n;lp2_n;params_n;rp2_n]),
+               Ast0.ParenType
+                 (lp,
+                  Ast0.rewrap ty1 (Ast0.Pointer
+                   (Ast0.rewrap ty2 (Ast0.FunctionType
+                     (typ,lp2,params,rp2)),star)),rp)), idu)
+	| _ -> failwith "ParenType Visitor_ast0")
+    | Ast0.Array(ty1,lb1,size1,rb1) ->
+        (match Ast0.unwrap ty1 with
+          Ast0.Pointer(ty2,star) ->
+            (match Ast0.unwrap ty2 with
+              Ast0.FunctionType(ty3,lp3,params,rp3) ->
+                let (ty_n,typ) = typeC ty3 in
+                let (lp_n,lp) = string_mcode lp in
+                let (star_n,star) = string_mcode star in
+                let (idl,idu) = (match id with
+                  | Some a -> let (b,c) = ident a in ([b],Some c)
+                  | None -> ([],None)) in
+                let (lb1_n,lb1) = string_mcode lb1 in
+                let (size_n,size1) = get_option expression size1 in
+                let (rb1_n,rb1) = string_mcode rb1 in
+                let (rp_n,rp) = string_mcode rp in
+                let (lp3_n,lp3) = string_mcode lp3 in
+                let (params_n,params) = parameter_dots params in
+                let (rp3_n,rp3) = string_mcode rp3 in
+                ((multibind ([ty_n;lp_n;star_n] @ idl @
+                  [lb1_n;size_n;rb1_n;rp_n;lp3_n;params_n;rp3_n]),
+                   Ast0.ParenType
+                     (lp,
+                      Ast0.rewrap ty1
+                       (Ast0.Array
+                         (Ast0.rewrap ty2
+                           (Ast0.Pointer
+                             (Ast0.rewrap ty3
+                               (Ast0.FunctionType(typ,lp3,params,rp3)),
+                              star)),
+                        lb1,size1,rb1)),
+                   rp)),
+                 idu)
+    	    | _ -> failwith "ParenType Visitor_ast0")
+        | _ -> failwith "ParenType Visitor_ast0")
+    | _ -> failwith "ParenType Visitor_ast0"
+
+  and functiontype_type (ty,(id : Ast0.ident option),lp,params,rp) =
+    let (ty_n,ty) = typeC ty in
+    let (idl,idu) = (match id with
+      | Some a -> let (b,c) = ident a in ([b],Some c)
+      | None -> ([],None)) in
+    let (lp_n,lp) = string_mcode lp in
+    let (params_n,params) = parameter_dots params in
+    let (rp_n,rp) = string_mcode rp in
+    ((multibind ([ty_n] @ idl @ [lp_n; params_n; rp_n]),
+     Ast0.FunctionType(ty,lp,params,rp)), idu)
+
   and named_type ty id =
     match Ast0.unwrap ty with
       Ast0.FunctionPointer(rty,lp1,star,rp1,lp2,params,rp2) ->
@@ -446,6 +522,14 @@ let visitor mode bind option_default
 	let (tyres, idn) = array_type (rty,Some id,lb,size,rb) in
         let idn = match idn with Some i -> i | None -> failwith "Impossible" in
 	(rewrap ty tyres, idn)
+    | Ast0.ParenType(lp,rty,rp) ->
+	let (tyres, idn) = parentype_type (lp,rty,Some id,rp) in
+        let idn = match idn with Some i -> i | None -> failwith "Impossible" in
+	(rewrap ty tyres, idn)
+    | Ast0.FunctionType(rty,lp,params,rp) ->
+	let (tyres, idn) = functiontype_type (rty,Some id,lp,params,rp) in
+        let idn = match idn with Some i -> i | None -> failwith "Impossible" in
+	(rewrap ty tyres, idn)
     | _ -> let (ty_n,ty) = typeC ty in
            let (id_n,id) = ident id in
              ((bind ty_n id_n, ty), id)
@@ -479,6 +563,77 @@ let visitor mode bind option_default
     ((multibind ([ty_n] @ idl @ [lb_n;size_n;rb_n]),
      Ast0.Array(ty,lb,size,rb)), idu)
 
+  (* returns ((bind value,original value),id) since id may have been updated*)
+  and parentype_typedef (lp,ty,id,rp) =
+    match Ast0.unwrap ty with
+      Ast0.Pointer(ty1,star) ->
+        (match Ast0.unwrap ty1 with
+          Ast0.FunctionType(ty2,lp2,params,rp2) ->
+            let (ty_n,typ) = typeC ty2 in
+            let (lp_n,lp) = string_mcode lp in
+            let (star_n,star) = string_mcode star in
+            let (idl,idu) = (match id with
+              | Some a -> let (b,c) = typeC a in ([b],Some c)
+              | None -> ([],None)) in
+            let (rp_n,rp) = string_mcode rp in
+            let (lp2_n,lp2) = string_mcode lp2 in
+            let (params_n,params) = parameter_dots params in
+            let (rp2_n,rp2) = string_mcode rp2 in
+            ((multibind ([ty_n;lp_n;star_n] @ idl @
+              [rp_n;lp2_n;params_n;rp2_n]),
+               Ast0.ParenType
+                 (lp,
+                  Ast0.rewrap ty1 (Ast0.Pointer
+                   (Ast0.rewrap ty2 (Ast0.FunctionType
+                     (typ,lp2,params,rp2)),star)),rp)), idu)
+	| _ -> failwith "ParenType Visitor_ast0")
+    | Ast0.Array(ty1,lb1,size1,rb1) ->
+        (match Ast0.unwrap ty with
+          Ast0.Pointer(ty2,star) ->
+            (match Ast0.unwrap ty1 with
+              Ast0.FunctionType(ty3,lp3,params,rp3) ->
+                let (ty_n,typ) = typeC ty3 in
+                let (lp_n,lp) = string_mcode lp in
+                let (star_n,star) = string_mcode star in
+                let (idl,idu) = (match id with
+                  | Some a -> let (b,c) = typeC a in ([b],Some c)
+                  | None -> ([],None)) in
+                let (lb1_n,lb1) = string_mcode lb1 in
+                let (size_n,size1) = get_option expression size1 in
+                let (rb1_n,rb1) = string_mcode rb1 in
+                let (rp_n,rp) = string_mcode rp in
+                let (lp3_n,lp3) = string_mcode lp3 in
+                let (params_n,params) = parameter_dots params in
+                let (rp3_n,rp3) = string_mcode rp3 in
+                ((multibind ([ty_n;lp_n;star_n] @ idl @
+                  [lb1_n;size_n;rb1_n;rp_n;lp3_n;params_n;rp3_n]),
+                   Ast0.ParenType
+                     (lp,
+                      Ast0.rewrap ty1
+                       (Ast0.Array
+                         (Ast0.rewrap ty2 
+                           (Ast0.Pointer
+                             (Ast0.rewrap ty3
+                               (Ast0.FunctionType(typ,lp3,params,rp3)),
+                              star)),
+                        lb1,size1,rb1)),
+                   rp)),
+                 idu)
+    	    | _ -> failwith "ParenType Visitor_ast0")
+        | _ -> failwith "ParenType Visitor_ast0")
+    | _ -> failwith "ParenType Visitor_ast0"
+
+  and functiontype_typedef (ty,id,lp,params,rp) =
+    let (ty_n,ty) = typeC ty in
+    let (idl,idu) = (match id with
+      | Some a -> let (b,c) = typeC a in ([b],Some c)
+      | None -> ([],None)) in
+    let (lp_n,lp) = string_mcode lp in
+    let (params_n,params) = parameter_dots params in
+    let (rp_n,rp) = string_mcode rp in
+    ((multibind ([ty_n] @ idl @ [lp_n; params_n; rp_n]),
+     Ast0.FunctionType(ty,lp,params,rp)), idu)
+
   and named_type_typedef ty id =
     match Ast0.unwrap ty with
       Ast0.FunctionPointer(rty,lp1,star,rp1,lp2,params,rp2) ->
@@ -490,6 +645,14 @@ let visitor mode bind option_default
 	let (tyres, idn) = array_type_typedef (rty,Some id,lb,size,rb) in
         let idn = match idn with Some i -> i | None -> failwith "Impossible" in
 	(rewrap ty tyres, idn)
+    | Ast0.ParenType(lp,rty,rp) ->
+	let (tyres, idn) = parentype_typedef (lp,rty,Some id,rp) in
+        let idn = match idn with Some i -> i | None -> failwith "Impossible" in
+	(rewrap ty tyres, idn)
+    | Ast0.FunctionType(rty,lp,params,rp) ->
+	let (tyres, idn) = functiontype_typedef (rty,Some id,lp,params,rp) in
+        let idn = match idn with Some i -> i | None -> failwith "Impossible" in
+	(rewrap ty tyres, idn)
     | _ -> let (ty_n,ty) = typeC ty in
            let (id_n,id) = typeC id in
              ((bind ty_n id_n, ty), id)
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#15 lore raw

Re: [Cocci] [PATCH 04/26] parsing_cocci: visitor_ast0: Add cases for ParenType/FunctionType

Quoted 34 lines · Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

	<type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
 parsing_cocci/visitor_ast0.ml | 163 ++++++++++++++++++++++++++++++++++
 1 file changed, 163 insertions(+)
parsing_cocci/visitor_ast0.ml +6 -0
index c282e1f8..c56cd7a7 100644
@@ -339,6 +339,12 @@ let visitor mode bind option_default
 	| Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	    let (t,id) =
               function_pointer (ty,lp1,star,None,rp1,lp2,params,rp2) in t
+        | Ast0.ParenType(lp,ty,rp) ->
+	    let (t,id) =
+              parentype_type (lp,ty,None,rp) in t
+        | Ast0.FunctionType(ty,lp,params,rp) ->
+	    let (t,id) =
+              functiontype_type (ty,None,lp,params,rp) in t
 	| Ast0.Array(ty,lb,size,rb) ->
             let (t,id) = array_type (ty,None,lb,size,rb) in t
 	| Ast0.Decimal(dec,lp,length,comma,precision_opt,rp) ->
@@ -435,6 +441,76 @@ let visitor mode bind option_default
     ((multibind ([ty_n] @ idl @ [lb_n;size_n;rb_n]),
      Ast0.Array(ty,lb,size,rb)), idu)

parsing_cocci/visitor_ast0.ml let visitor mode bind option_default · line 452

Quoted 10 lines
parsing_cocci/visitor_ast0.ml +9 -0
 
+  and parentype_type (lp,ty,(id : Ast0.ident option),rp) =
+    match Ast0.unwrap ty with
+      Ast0.Pointer(ty1,star) ->
+        (match Ast0.unwrap ty1 with
+          Ast0.FunctionType(ty2,lp2,params,rp2) ->
+            let (ty_n,typ) = typeC ty2 in
+            let (lp_n,lp) = string_mcode lp in
+            let (star_n,star) = string_mcode star in
+            let (idl,idu) = (match id with

I would prefer the match on a line by itself. It doesn't need parentheses around it. (Even though I see that some other code has done it the way you do it here).

Quoted 39 lines
parsing_cocci/visitor_ast0.ml +39 -0
+              | Some a -> let (b,c) = ident a in ([b],Some c)
+              | None -> ([],None)) in
+            let (rp_n,rp) = string_mcode rp in
+            let (lp2_n,lp2) = string_mcode lp2 in
+            let (params_n,params) = parameter_dots params in
+            let (rp2_n,rp2) = string_mcode rp2 in
+            ((multibind ([ty_n;lp_n;star_n] @ idl @
+              [rp_n;lp2_n;params_n;rp2_n]),
+               Ast0.ParenType
+                 (lp,
+                  Ast0.rewrap ty1 (Ast0.Pointer
+                   (Ast0.rewrap ty2 (Ast0.FunctionType
+                     (typ,lp2,params,rp2)),star)),rp)), idu)
+	| _ -> failwith "ParenType Visitor_ast0")
+    | Ast0.Array(ty1,lb1,size1,rb1) ->
+        (match Ast0.unwrap ty1 with
+          Ast0.Pointer(ty2,star) ->
+            (match Ast0.unwrap ty2 with
+              Ast0.FunctionType(ty3,lp3,params,rp3) ->
+                let (ty_n,typ) = typeC ty3 in
+                let (lp_n,lp) = string_mcode lp in
+                let (star_n,star) = string_mcode star in
+                let (idl,idu) = (match id with
+                  | Some a -> let (b,c) = ident a in ([b],Some c)
+                  | None -> ([],None)) in
+                let (lb1_n,lb1) = string_mcode lb1 in
+                let (size_n,size1) = get_option expression size1 in
+                let (rb1_n,rb1) = string_mcode rb1 in
+                let (rp_n,rp) = string_mcode rp in
+                let (lp3_n,lp3) = string_mcode lp3 in
+                let (params_n,params) = parameter_dots params in
+                let (rp3_n,rp3) = string_mcode rp3 in
+                ((multibind ([ty_n;lp_n;star_n] @ idl @
+                  [lb1_n;size_n;rb1_n;rp_n;lp3_n;params_n;rp3_n]),
+                   Ast0.ParenType
+                     (lp,
+                      Ast0.rewrap ty1
+                       (Ast0.Array
+                         (Ast0.rewrap ty2

parsing_cocci/visitor_ast0.ml let visitor mode bind option_default · line 501

Quoted 10 lines
parsing_cocci/visitor_ast0.ml +10 -0
+                           (Ast0.Pointer
+                             (Ast0.rewrap ty3
+                               (Ast0.FunctionType(typ,lp3,params,rp3)),
+                              star)),
+                        lb1,size1,rb1)),
+                   rp)),
+                 idu)
+    	    | _ -> failwith "ParenType Visitor_ast0")
+        | _ -> failwith "ParenType Visitor_ast0")
+    | _ -> failwith "ParenType Visitor_ast0"

Can some code be shared between the above two cases?

Quoted 16 lines
parsing_cocci/visitor_ast0.ml +11 -0
+  and functiontype_type (ty,(id : Ast0.ident option),lp,params,rp) =
+    let (ty_n,ty) = typeC ty in
+    let (idl,idu) = (match id with
+      | Some a -> let (b,c) = ident a in ([b],Some c)
+      | None -> ([],None)) in
+    let (lp_n,lp) = string_mcode lp in
+    let (params_n,params) = parameter_dots params in
+    let (rp_n,rp) = string_mcode rp in
+    ((multibind ([ty_n] @ idl @ [lp_n; params_n; rp_n]),
+     Ast0.FunctionType(ty,lp,params,rp)), idu)
+
   and named_type ty id =
     match Ast0.unwrap ty with
       Ast0.FunctionPointer(rty,lp1,star,rp1,lp2,params,rp2) ->
@@ -446,6 +522,14 @@ let visitor mode bind option_default
 	let (tyres, idn) = array_type (rty,Some id,lb,size,rb) in

parsing_cocci/visitor_ast0.ml let visitor mode bind option_default · line 532

Quoted 10 lines
parsing_cocci/visitor_ast0.ml +8 -0
         let idn = match idn with Some i -> i | None -> failwith "Impossible" in
 	(rewrap ty tyres, idn)
+    | Ast0.ParenType(lp,rty,rp) ->
+	let (tyres, idn) = parentype_type (lp,rty,Some id,rp) in
+        let idn = match idn with Some i -> i | None -> failwith "Impossible" in
+	(rewrap ty tyres, idn)
+    | Ast0.FunctionType(rty,lp,params,rp) ->
+	let (tyres, idn) = functiontype_type (rty,Some id,lp,params,rp) in
+        let idn = match idn with Some i -> i | None -> failwith "Impossible" in
+	(rewrap ty tyres, idn)

The second let is using spaces instead of a tab in each case. The code above has the same problem. No need to modify the code above, but the new code could use the tabs.

Quoted 56 lines
parsing_cocci/visitor_ast0.ml +49 -0
     | _ -> let (ty_n,ty) = typeC ty in
            let (id_n,id) = ident id in
              ((bind ty_n id_n, ty), id)
@@ -479,6 +563,77 @@ let visitor mode bind option_default
     ((multibind ([ty_n] @ idl @ [lb_n;size_n;rb_n]),
      Ast0.Array(ty,lb,size,rb)), idu)
 
+  (* returns ((bind value,original value),id) since id may have been updated*)
+  and parentype_typedef (lp,ty,id,rp) =
+    match Ast0.unwrap ty with
+      Ast0.Pointer(ty1,star) ->
+        (match Ast0.unwrap ty1 with
+          Ast0.FunctionType(ty2,lp2,params,rp2) ->
+            let (ty_n,typ) = typeC ty2 in
+            let (lp_n,lp) = string_mcode lp in
+            let (star_n,star) = string_mcode star in
+            let (idl,idu) = (match id with
+              | Some a -> let (b,c) = typeC a in ([b],Some c)
+              | None -> ([],None)) in
+            let (rp_n,rp) = string_mcode rp in
+            let (lp2_n,lp2) = string_mcode lp2 in
+            let (params_n,params) = parameter_dots params in
+            let (rp2_n,rp2) = string_mcode rp2 in
+            ((multibind ([ty_n;lp_n;star_n] @ idl @
+              [rp_n;lp2_n;params_n;rp2_n]),
+               Ast0.ParenType
+                 (lp,
+                  Ast0.rewrap ty1 (Ast0.Pointer
+                   (Ast0.rewrap ty2 (Ast0.FunctionType
+                     (typ,lp2,params,rp2)),star)),rp)), idu)
+	| _ -> failwith "ParenType Visitor_ast0")
+    | Ast0.Array(ty1,lb1,size1,rb1) ->
+        (match Ast0.unwrap ty with
+          Ast0.Pointer(ty2,star) ->
+            (match Ast0.unwrap ty1 with
+              Ast0.FunctionType(ty3,lp3,params,rp3) ->
+                let (ty_n,typ) = typeC ty3 in
+                let (lp_n,lp) = string_mcode lp in
+                let (star_n,star) = string_mcode star in
+                let (idl,idu) = (match id with
+                  | Some a -> let (b,c) = typeC a in ([b],Some c)
+                  | None -> ([],None)) in
+                let (lb1_n,lb1) = string_mcode lb1 in
+                let (size_n,size1) = get_option expression size1 in
+                let (rb1_n,rb1) = string_mcode rb1 in
+                let (rp_n,rp) = string_mcode rp in
+                let (lp3_n,lp3) = string_mcode lp3 in
+                let (params_n,params) = parameter_dots params in
+                let (rp3_n,rp3) = string_mcode rp3 in
+                ((multibind ([ty_n;lp_n;star_n] @ idl @
+                  [lb1_n;size_n;rb1_n;rp_n;lp3_n;params_n;rp3_n]),
+                   Ast0.ParenType
+                     (lp,
+                      Ast0.rewrap ty1
+                       (Ast0.Array
+                         (Ast0.rewrap ty2

parsing_cocci/visitor_ast0.ml let visitor mode bind option_default · line 624

Quoted 10 lines
parsing_cocci/visitor_ast0.ml +10 -0
+                           (Ast0.Pointer
+                             (Ast0.rewrap ty3
+                               (Ast0.FunctionType(typ,lp3,params,rp3)),
+                              star)),
+                        lb1,size1,rb1)),
+                   rp)),
+                 idu)
+    	    | _ -> failwith "ParenType Visitor_ast0")
+        | _ -> failwith "ParenType Visitor_ast0")
+    | _ -> failwith "ParenType Visitor_ast0"

Again, can some code be shared?

julia

Quoted 31 lines
parsing_cocci/visitor_ast0.ml +20 -1
+
+  and functiontype_typedef (ty,id,lp,params,rp) =
+    let (ty_n,ty) = typeC ty in
+    let (idl,idu) = (match id with
+      | Some a -> let (b,c) = typeC a in ([b],Some c)
+      | None -> ([],None)) in
+    let (lp_n,lp) = string_mcode lp in
+    let (params_n,params) = parameter_dots params in
+    let (rp_n,rp) = string_mcode rp in
+    ((multibind ([ty_n] @ idl @ [lp_n; params_n; rp_n]),
+     Ast0.FunctionType(ty,lp,params,rp)), idu)
+
   and named_type_typedef ty id =
     match Ast0.unwrap ty with
       Ast0.FunctionPointer(rty,lp1,star,rp1,lp2,params,rp2) ->
@@ -490,6 +645,14 @@ let visitor mode bind option_default
 	let (tyres, idn) = array_type_typedef (rty,Some id,lb,size,rb) in
         let idn = match idn with Some i -> i | None -> failwith "Impossible" in
 	(rewrap ty tyres, idn)
+    | Ast0.ParenType(lp,rty,rp) ->
+	let (tyres, idn) = parentype_typedef (lp,rty,Some id,rp) in
+        let idn = match idn with Some i -> i | None -> failwith "Impossible" in
+	(rewrap ty tyres, idn)
+    | Ast0.FunctionType(rty,lp,params,rp) ->
+	let (tyres, idn) = functiontype_typedef (rty,Some id,lp,params,rp) in
+        let idn = match idn with Some i -> i | None -> failwith "Impossible" in
+	(rewrap ty tyres, idn)
     | _ -> let (ty_n,ty) = typeC ty in
            let (id_n,id) = typeC id in
              ((bind ty_n id_n, ty), id)
--
Quoted 3 lines

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#16 lore raw

PATCH 5/26 parsing_cocci: visitor_ast0: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

	<type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
 parsing_cocci/visitor_ast.ml | 75 ++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
parsing_cocci/visitor_ast.ml +75 -0
index 3d5de794..99af7652 100644
@@ -318,6 +318,61 @@ let combiner bind option_default
     let lrb = string_mcode rb in
     multibind ([lty] @ lid @ [lb; lsize; lrb])
 
+  and parentype_type (lp, ty, (id : Ast.ident option), rp) =
+    match Ast.unwrap ty with
+     Ast.Type(_,_,fty1) ->
+      (match Ast.unwrap fty1 with
+         Ast.Pointer(ty1,star) ->
+          (match Ast.unwrap ty1 with
+             Ast.Type(_,_,fty2) ->
+              (match Ast.unwrap fty2 with
+                Ast.FunctionType(ty2,lp2,params,rp2) ->
+                  let typ = fullType ty2 in
+                  let lp = string_mcode lp in
+                  let star = string_mcode star in
+                  let idl = match id with
+                    | Some idd -> [ident idd]
+                    | None -> [] in
+                  let rp = string_mcode rp in
+                  let lp2 = string_mcode lp2 in
+                  let params = parameter_dots params in
+                  let rp2 = string_mcode rp2 in
+                  multibind
+                    ([typ;lp;star] @ idl @ [rp;lp2;params;rp2])
+                | _ -> failwith "ParenType Visitor_ast")
+           | _ -> failwith "ParenType Visitor_ast")
+       | Ast.Array(ty1,lb1,size1,rb1) ->
+          (match Ast.unwrap ty1 with
+            Ast.Type(_,_,fty2) ->
+             (match Ast.unwrap fty2 with
+               Ast.Pointer(ty2,star) ->
+                (match Ast.unwrap ty2 with
+                  Ast.Type(_,_,fty3) ->
+                   (match Ast.unwrap fty3 with
+                     Ast.FunctionType(ty3,lp3,params,rp3) ->
+                     let typ = fullType ty3 in
+                     let lp = string_mcode lp in
+                     let star = string_mcode star in
+                     let idl = match id with
+                       | Some idd -> [ident idd]
+                       | None -> [] in
+                     let lb1 = string_mcode lb1 in
+                     let size1 = get_option expression size1 in
+                     let rb1 = string_mcode rb1 in
+                     let rp = string_mcode rp in
+                     let lp3 = string_mcode lp3 in
+                     let params = parameter_dots params in
+                     let rp3 = string_mcode rp3 in
+                     multibind
+                       ([typ;lp;star] @ idl @
+                        [lb1;size1;rb1;rp;lp3;params;rp3])
+   	           | _ -> failwith "ParenType Visitor_ast")
+                | _ -> failwith "ParenType Visitor_ast")
+             | _ -> failwith "ParenType Visitor_ast")
+          | _ -> failwith "ParenType Visitor_ast")
+       | _ -> failwith "ParenType Visitor_ast")
+    | _ -> failwith "ParenType Visitor_ast"
+
   and typeC ty =
     let k ty =
       match Ast.unwrap ty with
@@ -332,6 +387,14 @@ let combiner bind option_default
 	  bind lty lstar
       | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	  function_pointer (ty,lp1,star,None,rp1,lp2,params,rp2)
+      | Ast.ParenType(lp,ty,rp) ->
+          parentype_type (lp,ty,None,rp)
+      | Ast.FunctionType(ty,lp,params,rp) ->
+          let lty = fullType ty in
+          let llp = string_mcode lp in
+          let lparams = parameter_dots params in
+          let lrp = string_mcode rp in
+          multibind [lty; llp; lparams; lrp]
       | Ast.Array(ty,lb,size,rb) -> array_type (ty,None,lb,size,rb)
       | Ast.Decimal(dec,lp,length,comma,precision_opt,rp) ->
 	  let ldec = string_mcode dec in
@@ -384,6 +447,7 @@ let combiner bind option_default
 	  Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	    function_pointer (ty, lp1, star, Some id, rp1, lp2, params, rp2)
 	| Ast.Array(ty,lb,size,rb) -> array_type (ty, Some id, lb, size, rb)
+        | Ast.ParenType(lp,ty,rp) -> parentype_type (lp, ty, Some id, rp)
 	| _ -> let lty = fullType ty in
 	       let lid = ident id in
 	       bind lty lid)
@@ -1253,6 +1317,17 @@ let rebuilder
 	    let lparams = parameter_dots params in
 	    let lrp2 = string_mcode rp2 in
 	    Ast.FunctionPointer(lty, llp1, lstar, lrp1, llp2, lparams, lrp2)
+        | Ast.ParenType(lp,ty,rp) ->
+            let llp = string_mcode lp in
+            let lty = fullType ty in
+            let lrp = string_mcode rp in
+            Ast.ParenType(llp,lty,lrp)
+        | Ast.FunctionType(ty,lp,params,rp) ->
+            let lty = fullType ty in
+            let llp = string_mcode lp in
+            let lparams = parameter_dots params in
+            let lrp = string_mcode rp in
+            Ast.FunctionType(lty,llp,lparams,lrp)
 	| Ast.Array(ty,lb,size,rb) ->
 	    let lty = fullType ty in
 	    let llb = string_mcode lb in
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#17 lore raw

PATCH 5/26 parsing_cocci: visitor_ast: Add cases for ParenType/FunctionType

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

	<type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
 parsing_cocci/visitor_ast.ml | 75 ++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
parsing_cocci/visitor_ast.ml +75 -0
index 3d5de794..99af7652 100644
@@ -318,6 +318,61 @@ let combiner bind option_default
     let lrb = string_mcode rb in
     multibind ([lty] @ lid @ [lb; lsize; lrb])
 
+  and parentype_type (lp, ty, (id : Ast.ident option), rp) =
+    match Ast.unwrap ty with
+     Ast.Type(_,_,fty1) ->
+      (match Ast.unwrap fty1 with
+         Ast.Pointer(ty1,star) ->
+          (match Ast.unwrap ty1 with
+             Ast.Type(_,_,fty2) ->
+              (match Ast.unwrap fty2 with
+                Ast.FunctionType(ty2,lp2,params,rp2) ->
+                  let typ = fullType ty2 in
+                  let lp = string_mcode lp in
+                  let star = string_mcode star in
+                  let idl = match id with
+                    | Some idd -> [ident idd]
+                    | None -> [] in
+                  let rp = string_mcode rp in
+                  let lp2 = string_mcode lp2 in
+                  let params = parameter_dots params in
+                  let rp2 = string_mcode rp2 in
+                  multibind
+                    ([typ;lp;star] @ idl @ [rp;lp2;params;rp2])
+                | _ -> failwith "ParenType Visitor_ast")
+           | _ -> failwith "ParenType Visitor_ast")
+       | Ast.Array(ty1,lb1,size1,rb1) ->
+          (match Ast.unwrap ty1 with
+            Ast.Type(_,_,fty2) ->
+             (match Ast.unwrap fty2 with
+               Ast.Pointer(ty2,star) ->
+                (match Ast.unwrap ty2 with
+                  Ast.Type(_,_,fty3) ->
+                   (match Ast.unwrap fty3 with
+                     Ast.FunctionType(ty3,lp3,params,rp3) ->
+                     let typ = fullType ty3 in
+                     let lp = string_mcode lp in
+                     let star = string_mcode star in
+                     let idl = match id with
+                       | Some idd -> [ident idd]
+                       | None -> [] in
+                     let lb1 = string_mcode lb1 in
+                     let size1 = get_option expression size1 in
+                     let rb1 = string_mcode rb1 in
+                     let rp = string_mcode rp in
+                     let lp3 = string_mcode lp3 in
+                     let params = parameter_dots params in
+                     let rp3 = string_mcode rp3 in
+                     multibind
+                       ([typ;lp;star] @ idl @
+                        [lb1;size1;rb1;rp;lp3;params;rp3])
+   	           | _ -> failwith "ParenType Visitor_ast")
+                | _ -> failwith "ParenType Visitor_ast")
+             | _ -> failwith "ParenType Visitor_ast")
+          | _ -> failwith "ParenType Visitor_ast")
+       | _ -> failwith "ParenType Visitor_ast")
+    | _ -> failwith "ParenType Visitor_ast"
+
   and typeC ty =
     let k ty =
       match Ast.unwrap ty with
@@ -332,6 +387,14 @@ let combiner bind option_default
 	  bind lty lstar
       | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	  function_pointer (ty,lp1,star,None,rp1,lp2,params,rp2)
+      | Ast.ParenType(lp,ty,rp) ->
+          parentype_type (lp,ty,None,rp)
+      | Ast.FunctionType(ty,lp,params,rp) ->
+          let lty = fullType ty in
+          let llp = string_mcode lp in
+          let lparams = parameter_dots params in
+          let lrp = string_mcode rp in
+          multibind [lty; llp; lparams; lrp]
       | Ast.Array(ty,lb,size,rb) -> array_type (ty,None,lb,size,rb)
       | Ast.Decimal(dec,lp,length,comma,precision_opt,rp) ->
 	  let ldec = string_mcode dec in
@@ -384,6 +447,7 @@ let combiner bind option_default
 	  Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	    function_pointer (ty, lp1, star, Some id, rp1, lp2, params, rp2)
 	| Ast.Array(ty,lb,size,rb) -> array_type (ty, Some id, lb, size, rb)
+        | Ast.ParenType(lp,ty,rp) -> parentype_type (lp, ty, Some id, rp)
 	| _ -> let lty = fullType ty in
 	       let lid = ident id in
 	       bind lty lid)
@@ -1253,6 +1317,17 @@ let rebuilder
 	    let lparams = parameter_dots params in
 	    let lrp2 = string_mcode rp2 in
 	    Ast.FunctionPointer(lty, llp1, lstar, lrp1, llp2, lparams, lrp2)
+        | Ast.ParenType(lp,ty,rp) ->
+            let llp = string_mcode lp in
+            let lty = fullType ty in
+            let lrp = string_mcode rp in
+            Ast.ParenType(llp,lty,lrp)
+        | Ast.FunctionType(ty,lp,params,rp) ->
+            let lty = fullType ty in
+            let llp = string_mcode lp in
+            let lparams = parameter_dots params in
+            let lrp = string_mcode rp in
+            Ast.FunctionType(lty,llp,lparams,lrp)
 	| Ast.Array(ty,lb,size,rb) ->
 	    let lty = fullType ty in
 	    let llb = string_mcode lb in
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#18 lore raw

Re: [Cocci] [PATCH 05/26] parsing_cocci: visitor_ast: Add cases for ParenType/FunctionType

Quoted 8 lines · Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

	<type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

Did you change this? It should be v2, and then the reason for the change should be described under the ---

julia

Quoted 114 lines
---
 parsing_cocci/visitor_ast.ml | 75 ++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
parsing_cocci/visitor_ast.ml +75 -1
index 3d5de794..99af7652 100644
@@ -318,6 +318,61 @@ let combiner bind option_default
     let lrb = string_mcode rb in
     multibind ([lty] @ lid @ [lb; lsize; lrb])
 
+  and parentype_type (lp, ty, (id : Ast.ident option), rp) =
+    match Ast.unwrap ty with
+     Ast.Type(_,_,fty1) ->
+      (match Ast.unwrap fty1 with
+         Ast.Pointer(ty1,star) ->
+          (match Ast.unwrap ty1 with
+             Ast.Type(_,_,fty2) ->
+              (match Ast.unwrap fty2 with
+                Ast.FunctionType(ty2,lp2,params,rp2) ->
+                  let typ = fullType ty2 in
+                  let lp = string_mcode lp in
+                  let star = string_mcode star in
+                  let idl = match id with
+                    | Some idd -> [ident idd]
+                    | None -> [] in
+                  let rp = string_mcode rp in
+                  let lp2 = string_mcode lp2 in
+                  let params = parameter_dots params in
+                  let rp2 = string_mcode rp2 in
+                  multibind
+                    ([typ;lp;star] @ idl @ [rp;lp2;params;rp2])
+                | _ -> failwith "ParenType Visitor_ast")
+           | _ -> failwith "ParenType Visitor_ast")
+       | Ast.Array(ty1,lb1,size1,rb1) ->
+          (match Ast.unwrap ty1 with
+            Ast.Type(_,_,fty2) ->
+             (match Ast.unwrap fty2 with
+               Ast.Pointer(ty2,star) ->
+                (match Ast.unwrap ty2 with
+                  Ast.Type(_,_,fty3) ->
+                   (match Ast.unwrap fty3 with
+                     Ast.FunctionType(ty3,lp3,params,rp3) ->
+                     let typ = fullType ty3 in
+                     let lp = string_mcode lp in
+                     let star = string_mcode star in
+                     let idl = match id with
+                       | Some idd -> [ident idd]
+                       | None -> [] in
+                     let lb1 = string_mcode lb1 in
+                     let size1 = get_option expression size1 in
+                     let rb1 = string_mcode rb1 in
+                     let rp = string_mcode rp in
+                     let lp3 = string_mcode lp3 in
+                     let params = parameter_dots params in
+                     let rp3 = string_mcode rp3 in
+                     multibind
+                       ([typ;lp;star] @ idl @
+                        [lb1;size1;rb1;rp;lp3;params;rp3])
+   	           | _ -> failwith "ParenType Visitor_ast")
+                | _ -> failwith "ParenType Visitor_ast")
+             | _ -> failwith "ParenType Visitor_ast")
+          | _ -> failwith "ParenType Visitor_ast")
+       | _ -> failwith "ParenType Visitor_ast")
+    | _ -> failwith "ParenType Visitor_ast"
+
   and typeC ty =
     let k ty =
       match Ast.unwrap ty with
@@ -332,6 +387,14 @@ let combiner bind option_default
 	  bind lty lstar
       | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	  function_pointer (ty,lp1,star,None,rp1,lp2,params,rp2)
+      | Ast.ParenType(lp,ty,rp) ->
+          parentype_type (lp,ty,None,rp)
+      | Ast.FunctionType(ty,lp,params,rp) ->
+          let lty = fullType ty in
+          let llp = string_mcode lp in
+          let lparams = parameter_dots params in
+          let lrp = string_mcode rp in
+          multibind [lty; llp; lparams; lrp]
       | Ast.Array(ty,lb,size,rb) -> array_type (ty,None,lb,size,rb)
       | Ast.Decimal(dec,lp,length,comma,precision_opt,rp) ->
 	  let ldec = string_mcode dec in
@@ -384,6 +447,7 @@ let combiner bind option_default
 	  Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	    function_pointer (ty, lp1, star, Some id, rp1, lp2, params, rp2)
 	| Ast.Array(ty,lb,size,rb) -> array_type (ty, Some id, lb, size, rb)
+        | Ast.ParenType(lp,ty,rp) -> parentype_type (lp, ty, Some id, rp)
 	| _ -> let lty = fullType ty in
 	       let lid = ident id in
 	       bind lty lid)
@@ -1253,6 +1317,17 @@ let rebuilder
 	    let lparams = parameter_dots params in
 	    let lrp2 = string_mcode rp2 in
 	    Ast.FunctionPointer(lty, llp1, lstar, lrp1, llp2, lparams, lrp2)
+        | Ast.ParenType(lp,ty,rp) ->
+            let llp = string_mcode lp in
+            let lty = fullType ty in
+            let lrp = string_mcode rp in
+            Ast.ParenType(llp,lty,lrp)
+        | Ast.FunctionType(ty,lp,params,rp) ->
+            let lty = fullType ty in
+            let llp = string_mcode lp in
+            let lparams = parameter_dots params in
+            let lrp = string_mcode rp in
+            Ast.FunctionType(lty,llp,lparams,lrp)
 	| Ast.Array(ty,lb,size,rb) ->
 	    let lty = fullType ty in
 	    let llb = string_mcode lb in
--
Quoted 3 lines

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#19 lore raw

PATCH v2 5/26 parsing_cocci: visitor_ast: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

	<type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
Changes in v2:
-------------
- Change the "visitor_ast0" in patch subject to "visitor_ast"

 parsing_cocci/visitor_ast.ml | 75 ++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
parsing_cocci/visitor_ast.ml +75 -0
index 3d5de794..99af7652 100644
@@ -318,6 +318,61 @@ let combiner bind option_default
     let lrb = string_mcode rb in
     multibind ([lty] @ lid @ [lb; lsize; lrb])
 
+  and parentype_type (lp, ty, (id : Ast.ident option), rp) =
+    match Ast.unwrap ty with
+     Ast.Type(_,_,fty1) ->
+      (match Ast.unwrap fty1 with
+         Ast.Pointer(ty1,star) ->
+          (match Ast.unwrap ty1 with
+             Ast.Type(_,_,fty2) ->
+              (match Ast.unwrap fty2 with
+                Ast.FunctionType(ty2,lp2,params,rp2) ->
+                  let typ = fullType ty2 in
+                  let lp = string_mcode lp in
+                  let star = string_mcode star in
+                  let idl = match id with
+                    | Some idd -> [ident idd]
+                    | None -> [] in
+                  let rp = string_mcode rp in
+                  let lp2 = string_mcode lp2 in
+                  let params = parameter_dots params in
+                  let rp2 = string_mcode rp2 in
+                  multibind
+                    ([typ;lp;star] @ idl @ [rp;lp2;params;rp2])
+                | _ -> failwith "ParenType Visitor_ast")
+           | _ -> failwith "ParenType Visitor_ast")
+       | Ast.Array(ty1,lb1,size1,rb1) ->
+          (match Ast.unwrap ty1 with
+            Ast.Type(_,_,fty2) ->
+             (match Ast.unwrap fty2 with
+               Ast.Pointer(ty2,star) ->
+                (match Ast.unwrap ty2 with
+                  Ast.Type(_,_,fty3) ->
+                   (match Ast.unwrap fty3 with
+                     Ast.FunctionType(ty3,lp3,params,rp3) ->
+                     let typ = fullType ty3 in
+                     let lp = string_mcode lp in
+                     let star = string_mcode star in
+                     let idl = match id with
+                       | Some idd -> [ident idd]
+                       | None -> [] in
+                     let lb1 = string_mcode lb1 in
+                     let size1 = get_option expression size1 in
+                     let rb1 = string_mcode rb1 in
+                     let rp = string_mcode rp in
+                     let lp3 = string_mcode lp3 in
+                     let params = parameter_dots params in
+                     let rp3 = string_mcode rp3 in
+                     multibind
+                       ([typ;lp;star] @ idl @
+                        [lb1;size1;rb1;rp;lp3;params;rp3])
+   	           | _ -> failwith "ParenType Visitor_ast")
+                | _ -> failwith "ParenType Visitor_ast")
+             | _ -> failwith "ParenType Visitor_ast")
+          | _ -> failwith "ParenType Visitor_ast")
+       | _ -> failwith "ParenType Visitor_ast")
+    | _ -> failwith "ParenType Visitor_ast"
+
   and typeC ty =
     let k ty =
       match Ast.unwrap ty with
@@ -332,6 +387,14 @@ let combiner bind option_default
 	  bind lty lstar
       | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	  function_pointer (ty,lp1,star,None,rp1,lp2,params,rp2)
+      | Ast.ParenType(lp,ty,rp) ->
+          parentype_type (lp,ty,None,rp)
+      | Ast.FunctionType(ty,lp,params,rp) ->
+          let lty = fullType ty in
+          let llp = string_mcode lp in
+          let lparams = parameter_dots params in
+          let lrp = string_mcode rp in
+          multibind [lty; llp; lparams; lrp]
       | Ast.Array(ty,lb,size,rb) -> array_type (ty,None,lb,size,rb)
       | Ast.Decimal(dec,lp,length,comma,precision_opt,rp) ->
 	  let ldec = string_mcode dec in
@@ -384,6 +447,7 @@ let combiner bind option_default
 	  Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	    function_pointer (ty, lp1, star, Some id, rp1, lp2, params, rp2)
 	| Ast.Array(ty,lb,size,rb) -> array_type (ty, Some id, lb, size, rb)
+        | Ast.ParenType(lp,ty,rp) -> parentype_type (lp, ty, Some id, rp)
 	| _ -> let lty = fullType ty in
 	       let lid = ident id in
 	       bind lty lid)
@@ -1253,6 +1317,17 @@ let rebuilder
 	    let lparams = parameter_dots params in
 	    let lrp2 = string_mcode rp2 in
 	    Ast.FunctionPointer(lty, llp1, lstar, lrp1, llp2, lparams, lrp2)
+        | Ast.ParenType(lp,ty,rp) ->
+            let llp = string_mcode lp in
+            let lty = fullType ty in
+            let lrp = string_mcode rp in
+            Ast.ParenType(llp,lty,lrp)
+        | Ast.FunctionType(ty,lp,params,rp) ->
+            let lty = fullType ty in
+            let llp = string_mcode lp in
+            let lparams = parameter_dots params in
+            let lrp = string_mcode rp in
+            Ast.FunctionType(lty,llp,lparams,lrp)
 	| Ast.Array(ty,lb,size,rb) ->
 	    let lty = fullType ty in
 	    let llb = string_mcode lb in
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#20 lore raw

Re: [Cocci] [PATCH v2 05/26] parsing_cocci: visitor_ast: Add cases for ParenType/FunctionType

Quoted 70 lines · Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

	<type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
Changes in v2:
-------------
- Change the "visitor_ast0" in patch subject to "visitor_ast"

 parsing_cocci/visitor_ast.ml | 75 ++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
parsing_cocci/visitor_ast.ml +44 -0
index 3d5de794..99af7652 100644
@@ -318,6 +318,61 @@ let combiner bind option_default
     let lrb = string_mcode rb in
     multibind ([lty] @ lid @ [lb; lsize; lrb])
 
+  and parentype_type (lp, ty, (id : Ast.ident option), rp) =
+    match Ast.unwrap ty with
+     Ast.Type(_,_,fty1) ->
+      (match Ast.unwrap fty1 with
+         Ast.Pointer(ty1,star) ->
+          (match Ast.unwrap ty1 with
+             Ast.Type(_,_,fty2) ->
+              (match Ast.unwrap fty2 with
+                Ast.FunctionType(ty2,lp2,params,rp2) ->
+                  let typ = fullType ty2 in
+                  let lp = string_mcode lp in
+                  let star = string_mcode star in
+                  let idl = match id with
+                    | Some idd -> [ident idd]
+                    | None -> [] in
+                  let rp = string_mcode rp in
+                  let lp2 = string_mcode lp2 in
+                  let params = parameter_dots params in
+                  let rp2 = string_mcode rp2 in
+                  multibind
+                    ([typ;lp;star] @ idl @ [rp;lp2;params;rp2])
+                | _ -> failwith "ParenType Visitor_ast")
+           | _ -> failwith "ParenType Visitor_ast")
+       | Ast.Array(ty1,lb1,size1,rb1) ->
+          (match Ast.unwrap ty1 with
+            Ast.Type(_,_,fty2) ->
+             (match Ast.unwrap fty2 with
+               Ast.Pointer(ty2,star) ->
+                (match Ast.unwrap ty2 with
+                  Ast.Type(_,_,fty3) ->
+                   (match Ast.unwrap fty3 with
+                     Ast.FunctionType(ty3,lp3,params,rp3) ->
+                     let typ = fullType ty3 in
+                     let lp = string_mcode lp in
+                     let star = string_mcode star in
+                     let idl = match id with
+                       | Some idd -> [ident idd]
+                       | None -> [] in
+                     let lb1 = string_mcode lb1 in
+                     let size1 = get_option expression size1 in
+                     let rb1 = string_mcode rb1 in
+                     let rp = string_mcode rp in
+                     let lp3 = string_mcode lp3 in
+                     let params = parameter_dots params in

parsing_cocci/visitor_ast.ml let combiner bind option_default · line 374

Quoted 10 lines
parsing_cocci/visitor_ast.ml +10 -0
+                     let rp3 = string_mcode rp3 in
+                     multibind
+                       ([typ;lp;star] @ idl @
+                        [lb1;size1;rb1;rp;lp3;params;rp3])
+   	           | _ -> failwith "ParenType Visitor_ast")
+                | _ -> failwith "ParenType Visitor_ast")
+             | _ -> failwith "ParenType Visitor_ast")
+          | _ -> failwith "ParenType Visitor_ast")
+       | _ -> failwith "ParenType Visitor_ast")
+    | _ -> failwith "ParenType Visitor_ast"

Same comments as in the ast0 case: the match can go on a line below and I wonder if some of the code can be shared.

julia

Quoted 46 lines
parsing_cocci/visitor_ast.ml +21 -1
+
   and typeC ty =
     let k ty =
       match Ast.unwrap ty with
@@ -332,6 +387,14 @@ let combiner bind option_default
 	  bind lty lstar
       | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	  function_pointer (ty,lp1,star,None,rp1,lp2,params,rp2)
+      | Ast.ParenType(lp,ty,rp) ->
+          parentype_type (lp,ty,None,rp)
+      | Ast.FunctionType(ty,lp,params,rp) ->
+          let lty = fullType ty in
+          let llp = string_mcode lp in
+          let lparams = parameter_dots params in
+          let lrp = string_mcode rp in
+          multibind [lty; llp; lparams; lrp]
       | Ast.Array(ty,lb,size,rb) -> array_type (ty,None,lb,size,rb)
       | Ast.Decimal(dec,lp,length,comma,precision_opt,rp) ->
 	  let ldec = string_mcode dec in
@@ -384,6 +447,7 @@ let combiner bind option_default
 	  Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	    function_pointer (ty, lp1, star, Some id, rp1, lp2, params, rp2)
 	| Ast.Array(ty,lb,size,rb) -> array_type (ty, Some id, lb, size, rb)
+        | Ast.ParenType(lp,ty,rp) -> parentype_type (lp, ty, Some id, rp)
 	| _ -> let lty = fullType ty in
 	       let lid = ident id in
 	       bind lty lid)
@@ -1253,6 +1317,17 @@ let rebuilder
 	    let lparams = parameter_dots params in
 	    let lrp2 = string_mcode rp2 in
 	    Ast.FunctionPointer(lty, llp1, lstar, lrp1, llp2, lparams, lrp2)
+        | Ast.ParenType(lp,ty,rp) ->
+            let llp = string_mcode lp in
+            let lty = fullType ty in
+            let lrp = string_mcode rp in
+            Ast.ParenType(llp,lty,lrp)
+        | Ast.FunctionType(ty,lp,params,rp) ->
+            let lty = fullType ty in
+            let llp = string_mcode lp in
+            let lparams = parameter_dots params in
+            let lrp = string_mcode rp in
+            Ast.FunctionType(lty,llp,lparams,lrp)
 	| Ast.Array(ty,lb,size,rb) ->
 	    let lty = fullType ty in
 	    let llb = string_mcode lb in
--
Quoted 3 lines

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#21 lore raw

PATCH 6/26 parsing_cocci: arity: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are added to the SmPL ASTs. Add cases for these types in arity.ml.

---
 parsing_cocci/arity.ml | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
parsing_cocci/arity.ml +17 -0
index f29b86eb..3b408554 100644
@@ -419,6 +419,23 @@ and top_typeC tgt opt_allowed typ =
       let params = parameter_list tgt params in
       make_typeC typ tgt arity
 	(Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2))
+  | Ast0.ParenType(lp,ty,rp) ->
+      let arity =
+        all_same opt_allowed tgt (mcode2line lp)
+        [mcode2arity lp; mcode2arity rp] in
+      let lp = mcode lp in
+      let ty = typeC arity ty in
+      let rp = mcode rp in
+      make_typeC typ tgt arity (Ast0.ParenType(lp,ty,rp))
+  | Ast0.FunctionType(ty,lp,params,rp) ->
+      let arity =
+        all_same opt_allowed tgt (mcode2line lp)
+        [mcode2arity lp; mcode2arity rp] in
+      let ty = typeC arity ty in
+      let lp = mcode lp in
+      let params = parameter_list tgt params in
+      let rp = mcode rp in
+      make_typeC typ tgt arity (Ast0.FunctionType(ty,lp,params,rp))
   | Ast0.Array(ty,lb,size,rb) ->
       let arity =
 	all_same opt_allowed tgt (mcode2line lb)
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#22 lore raw

PATCH 7/26 parsing_cocci: index: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in index.ml.

---
 parsing_cocci/index.ml | 2 ++
 1 file changed, 2 insertions(+)
parsing_cocci/index.ml +2 -0
index a1103503..5c91f620 100644
@@ -93,6 +93,8 @@ let typeC t =
   | Ast0.Signed(sign,ty) -> [129]
   | Ast0.Pointer(ty,star) -> [49]
   | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> [131]
+  | Ast0.ParenType(lp,ty,rp) -> [138]
+  | Ast0.FunctionType(ty,lp,params,rp) -> [139]
   | Ast0.Array(ty,lb,size,rb) -> [50]
   | Ast0.Decimal(dec,lp,length,comma,precision_opt,rp) -> [160]
   | Ast0.EnumName(kind,name) -> [146]
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#23 lore raw

Re: [Cocci] [PATCH 07/26] parsing_cocci: index: Add cases for ParenType/FunctionType

Quoted 13 lines · Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in index.ml.

---
 parsing_cocci/index.ml | 2 ++
 1 file changed, 2 insertions(+)
parsing_cocci/index.ml +0 -0
index a1103503..5c91f620 100644

parsing_cocci/index.ml let typeC t = · line 97

Quoted 5 lines
parsing_cocci/index.ml +2 -0
@@ -93,6 +93,8 @@ let typeC t =
   | Ast0.Signed(sign,ty) -> [129]
   | Ast0.Pointer(ty,star) -> [49]
   | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> [131]
+  | Ast0.ParenType(lp,ty,rp) -> [138]
+  | Ast0.FunctionType(ty,lp,params,rp) -> [139]

You should indicate at the top of the file that these numbers aren't available any more.

julia

Quoted 7 lines
parsing_cocci/index.ml +0 -1
   | Ast0.Array(ty,lb,size,rb) -> [50]
   | Ast0.Decimal(dec,lp,length,comma,precision_opt,rp) -> [160]
   | Ast0.EnumName(kind,name) -> [146]
--

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#24 lore raw

PATCH 8/26 parsing_cocci: context_neg: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in context_neg.ml.

---
 parsing_cocci/context_neg.ml | 4 ++++
 1 file changed, 4 insertions(+)
parsing_cocci/context_neg.ml +4 -0
index 7bbf743d..b3a52daf 100644
@@ -663,6 +663,10 @@ let equal_typeC t1 t2 =
       equal_mcode sign1 sign2
   | (Ast0.Pointer(_,star1),Ast0.Pointer(_,star2)) ->
       equal_mcode star1 star2
+  | (Ast0.ParenType(lp1,_,rp1),Ast0.ParenType(lp2,_,rp2)) ->
+      equal_mcode lp1 lp2 && equal_mcode rp1 rp2
+  | (Ast0.FunctionType(_,lp1,_,rp1),Ast0.FunctionType(_,lp2,_,rp2)) ->
+      equal_mcode lp1 lp2 && equal_mcode rp1 rp2
   | (Ast0.Array(_,lb1,_,rb1),Ast0.Array(_,lb2,_,rb2)) ->
       equal_mcode lb1 lb2 && equal_mcode rb1 rb2
   | (Ast0.Decimal(dec1,lp1,_,comma1,_,rp1),
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#25 lore raw

PATCH 9/26 parsing_cocci: unparse_ast0: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType/FunctionType are now types in the SmPL ASTs. Add cases for these types in unparse_ast0.ml.

---
 parsing_cocci/unparse_ast0.ml | 43 +++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
parsing_cocci/unparse_ast0.ml +43 -0
index 5d450e0e..f8684dd2 100644
@@ -292,6 +292,40 @@ and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
   mcode print_string rp1; mcode print_string lp2;
   parameter_list params; mcode print_string rp2
 
+and print_parentype (lp,ty,rp) fn =
+ match Ast0.unwrap ty with
+   Ast0.Pointer(ty1,star) ->
+     (match Ast0.unwrap ty1 with
+       Ast0.FunctionType(ty2,lp2,params,rp2) ->
+         typeC ty2;
+         mcode print_string lp;
+         mcode print_string star;
+         fn();
+         mcode print_string rp;
+         mcode print_string lp2;
+         parameter_list params;
+         mcode print_string rp2;
+       | _ -> failwith "ParenType Unparse_ast0")
+ | Ast0.Array(ty1,lb1,size1,rb1) ->
+     (match Ast0.unwrap ty1 with
+       Ast0.Pointer(ty2,star) ->
+         (match Ast0.unwrap ty2 with
+           Ast0.FunctionType(ty3,lp3,params,rp3) ->
+             typeC ty3;
+             mcode print_string lp;
+             mcode print_string star;
+             fn();
+             mcode print_string lb1;
+             print_option expression size1;
+             mcode print_string rb1;
+             mcode print_string rp;
+             mcode print_string lp3;
+             parameter_list params;
+             mcode print_string rp3;
+ 	| _ -> failwith "ParenType Unparse_ast0")
+     | _ -> failwith "ParenType Unparse_ast0")
+ | _ -> failwith "ParenType Unparse_ast0"
+
 and typeC t =
   print_context t
     (function _ ->
@@ -306,6 +340,13 @@ and typeC t =
       | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	  print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
 	    (function _ -> ())
+      | Ast0.ParenType(lp,ty,rp) ->
+          print_parentype (lp,ty,rp) (function _ -> ())
+      | Ast0.FunctionType(ty,lp,params,rp) ->
+          typeC ty;
+          mcode print_string lp;
+          parameter_list params;
+          mcode print_string rp
       | Ast0.Array(ty,lb,size,rb) ->
 	  typeC ty; mcode print_string lb; print_option expression size;
 	  mcode print_string rb
@@ -367,6 +408,8 @@ and print_named_type ty id =
 		mcode print_string rb)
 	| _ -> typeC ty; ident id; k () in
       loop ty (function _ -> ())
+  | Ast0.ParenType(lp,ty,rp) ->
+      print_parentype (lp,ty,rp) (function _ -> ident id)
   | _ -> typeC ty; ident id
 
 and declaration d =
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#26 lore raw

Re: [Cocci] [PATCH 09/26] parsing_cocci: unparse_ast0: Add cases for ParenType/FunctionType

Quoted 36 lines · Jaskaran Singh

ParenType/FunctionType are now types in the SmPL ASTs. Add cases for these types in unparse_ast0.ml.

---
 parsing_cocci/unparse_ast0.ml | 43 +++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
parsing_cocci/unparse_ast0.ml +20 -0
index 5d450e0e..f8684dd2 100644
@@ -292,6 +292,40 @@ and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
   mcode print_string rp1; mcode print_string lp2;
   parameter_list params; mcode print_string rp2
 
+and print_parentype (lp,ty,rp) fn =
+ match Ast0.unwrap ty with
+   Ast0.Pointer(ty1,star) ->
+     (match Ast0.unwrap ty1 with
+       Ast0.FunctionType(ty2,lp2,params,rp2) ->
+         typeC ty2;
+         mcode print_string lp;
+         mcode print_string star;
+         fn();
+         mcode print_string rp;
+         mcode print_string lp2;
+         parameter_list params;
+         mcode print_string rp2;
+       | _ -> failwith "ParenType Unparse_ast0")
+ | Ast0.Array(ty1,lb1,size1,rb1) ->
+     (match Ast0.unwrap ty1 with
+       Ast0.Pointer(ty2,star) ->
+         (match Ast0.unwrap ty2 with
+           Ast0.FunctionType(ty3,lp3,params,rp3) ->
+             typeC ty3;

parsing_cocci/unparse_ast0.ml and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn = · line 324

Quoted 10 lines
parsing_cocci/unparse_ast0.ml +10 -0
+             mcode print_string lp;
+             mcode print_string star;
+             fn();
+             mcode print_string lb1;
+             print_option expression size1;
+             mcode print_string rb1;
+             mcode print_string rp;
+             mcode print_string lp3;
+             parameter_list params;
+             mcode print_string rp3;

It should be possible to share some code here.

julia

Quoted 31 lines
parsing_cocci/unparse_ast0.ml +13 -1
+ 	| _ -> failwith "ParenType Unparse_ast0")
+     | _ -> failwith "ParenType Unparse_ast0")
+ | _ -> failwith "ParenType Unparse_ast0"
+
 and typeC t =
   print_context t
     (function _ ->
@@ -306,6 +340,13 @@ and typeC t =
       | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
 	  print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
 	    (function _ -> ())
+      | Ast0.ParenType(lp,ty,rp) ->
+          print_parentype (lp,ty,rp) (function _ -> ())
+      | Ast0.FunctionType(ty,lp,params,rp) ->
+          typeC ty;
+          mcode print_string lp;
+          parameter_list params;
+          mcode print_string rp
       | Ast0.Array(ty,lb,size,rb) ->
 	  typeC ty; mcode print_string lb; print_option expression size;
 	  mcode print_string rb
@@ -367,6 +408,8 @@ and print_named_type ty id =
 		mcode print_string rb)
 	| _ -> typeC ty; ident id; k () in
       loop ty (function _ -> ())
+  | Ast0.ParenType(lp,ty,rp) ->
+      print_parentype (lp,ty,rp) (function _ -> ident id)
   | _ -> typeC ty; ident id
 
 and declaration d =
--
Quoted 3 lines

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#27 lore raw

PATCH 10/26 parsing_cocci: single_statement: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType/FunctionType are now types in the SmPL ASTs. Add cases for these types in single_statement.ml.

---
 parsing_cocci/single_statement.ml | 2 ++
 1 file changed, 2 insertions(+)
parsing_cocci/single_statement.ml +2 -0
index 46408b73..48d77b5a 100644
@@ -166,6 +166,8 @@ and left_typeC t =
   | Ast0.Signed(sgn,ty) -> modif_before_mcode sgn
   | Ast0.Pointer(ty,star) -> left_typeC ty
   | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> left_typeC ty
+  | Ast0.ParenType(lp,ty,rp) -> left_typeC ty
+  | Ast0.FunctionType(ty,lp,params,rp) -> left_typeC ty
   | Ast0.Array(ty,lb,size,rb) -> left_typeC ty
   | Ast0.Decimal(dec,lp,length,comma,precision_opt,rp) ->
       modif_before_mcode dec
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#28 lore raw

Re: [Cocci] [PATCH 10/26] parsing_cocci: single_statement: Add cases for ParenType/FunctionType

Quoted 13 lines · Jaskaran Singh

ParenType/FunctionType are now types in the SmPL ASTs. Add cases for these types in single_statement.ml.

---
 parsing_cocci/single_statement.ml | 2 ++
 1 file changed, 2 insertions(+)
parsing_cocci/single_statement.ml +0 -0
index 46408b73..48d77b5a 100644

parsing_cocci/single_statement.ml and left_typeC t = · line 169

Quoted 4 lines
parsing_cocci/single_statement.ml +1 -0
@@ -166,6 +166,8 @@ and left_typeC t =
   | Ast0.Signed(sgn,ty) -> modif_before_mcode sgn
   | Ast0.Pointer(ty,star) -> left_typeC ty
   | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> left_typeC ty
+  | Ast0.ParenType(lp,ty,rp) -> left_typeC ty

This doesn't look correct. ty is not the leftmost thing.

julia

Quoted 8 lines
parsing_cocci/single_statement.ml +1 -1
+  | Ast0.FunctionType(ty,lp,params,rp) -> left_typeC ty
   | Ast0.Array(ty,lb,size,rb) -> left_typeC ty
   | Ast0.Decimal(dec,lp,length,comma,precision_opt,rp) ->
       modif_before_mcode dec
--

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#29 lore raw

PATCH 11/26 parsing_cocci: function_prototypes: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in function_prototypes.ml.

---
 parsing_cocci/function_prototypes.ml | 4 ++++
 1 file changed, 4 insertions(+)
parsing_cocci/function_prototypes.ml +4 -0
index 2e6eea43..fd36f31b 100644
@@ -229,6 +229,10 @@ let rec attach_right strings ty =
     | Ast0.FunctionPointer(ty,lp,star,rp,lp1,ps,rp1) ->
 	Ast0.FunctionPointer(ty,lp,star,rp,lp1,ps,
 			     right_attach_mcode strings rp1)
+    | Ast0.ParenType(lp,ty,rp) ->
+	Ast0.ParenType(lp,ty,right_attach_mcode strings rp)
+    | Ast0.FunctionType(ty,lp,ps,rp) ->
+	Ast0.FunctionType(ty,lp,ps,right_attach_mcode strings rp)
     | Ast0.Array(ty,lb,e,rb) ->
 	Ast0.Array(ty,lb,e,right_attach_mcode strings rb)
     | Ast0.Decimal(dec,lp,e1,comma,e2,rp) ->
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#30 lore raw

PATCH 12/26 parsing_cocci: check_meta: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in check_meta.ml.

---
 parsing_cocci/check_meta.ml | 5 +++++
 1 file changed, 5 insertions(+)
parsing_cocci/check_meta.ml +5 -0
index 02b0fd9c..7b10bd53 100644
@@ -224,6 +224,11 @@ and typeC old_metas table minus t =
   | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
       typeC old_metas table minus ty;
       parameter_list old_metas table minus params
+  | Ast0.ParenType(lp,ty,rp) ->
+      typeC old_metas table minus ty
+  | Ast0.FunctionType(ty,lp,params,rp) ->
+      typeC old_metas table minus ty;
+      parameter_list old_metas table minus params
   | Ast0.Array(ty,lb,size,rb) ->
       typeC old_metas table minus ty;
       get_opt (expression ID old_metas table minus) size
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#31 lore raw

PATCH 13/26 parsing_cocci: iso_pattern: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in iso_pattern.ml.

---
 parsing_cocci/iso_pattern.ml | 5 +++++
 1 file changed, 5 insertions(+)
parsing_cocci/iso_pattern.ml +5 -0
index 5ceb2bdb..6d51b072 100644
@@ -1818,6 +1818,11 @@ let instantiate bindings mv_bindings model =
                           Ast0.rewrap ty (
                             Ast0.FunctionPointer(
                               renamer ty', s0, s1, s2, s3, p, s4))
+                      | Ast0.ParenType(s0, ty', s1) ->
+                          Ast0.rewrap ty (Ast0.ParenType(s0, renamer ty', s1))
+                      | Ast0.FunctionType(ty', s0, s1, s2) ->
+                          Ast0.rewrap ty (
+                            Ast0.FunctionType(renamer ty', s0, s1, s2))
                       | Ast0.Array(ty', s0, e, s1) ->
                           Ast0.rewrap ty (Ast0.Array(renamer ty', s0, e, s1))
                       | Ast0.Signed(s, ty') ->
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#32 lore raw

PATCH 14/26 parsing_cocci: adjust_pragmas: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in adjust_pragmas.ml.

---
 parsing_cocci/adjust_pragmas.ml | 4 ++++
 1 file changed, 4 insertions(+)
parsing_cocci/adjust_pragmas.ml +4 -0
index 73b5ff5f..537150e7 100644
@@ -171,6 +171,10 @@ let rec left_ty t =
   | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
       call_right left_ty ty t
 	(function ty -> Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2))
+  | Ast0.ParenType(lp,ty,rp) ->
+      call_right left_ty ty t (function ty -> Ast0.ParenType(lp,ty,rp))
+  | Ast0.FunctionType(ty,lp,params,rp) ->
+      call_right left_ty ty t (function ty -> Ast0.FunctionType(ty,lp,params,rp))
   | Ast0.Array(ty,lb,size,rb) ->
       call_right left_ty ty t (function ty -> Ast0.Array(ty,lb,size,rb))
   | Ast0.Decimal(dec,lp,length,comma,precision_opt,rp) ->
Signature · 6 lines
2.21.1

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#33 lore raw

Re: [Cocci] [PATCH 14/26] parsing_cocci: adjust_pragmas: Add cases for ParenType/FunctionType

Quoted 13 lines · Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in adjust_pragmas.ml.

---
 parsing_cocci/adjust_pragmas.ml | 4 ++++
 1 file changed, 4 insertions(+)
parsing_cocci/adjust_pragmas.ml +0 -0
index 73b5ff5f..537150e7 100644

parsing_cocci/adjust_pragmas.ml let rec left_ty t = · line 175

Quoted 5 lines
parsing_cocci/adjust_pragmas.ml +2 -0
@@ -171,6 +171,10 @@ let rec left_ty t =
   | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
       call_right left_ty ty t
 	(function ty -> Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2))
+  | Ast0.ParenType(lp,ty,rp) ->
+      call_right left_ty ty t (function ty -> Ast0.ParenType(lp,ty,rp))

This doesn't look right, because the type is not the leftmost thing.

julia

Quoted 9 lines
parsing_cocci/adjust_pragmas.ml +2 -1
+  | Ast0.FunctionType(ty,lp,params,rp) ->
+      call_right left_ty ty t (function ty -> Ast0.FunctionType(ty,lp,params,rp))
   | Ast0.Array(ty,lb,size,rb) ->
       call_right left_ty ty t (function ty -> Ast0.Array(ty,lb,size,rb))
   | Ast0.Decimal(dec,lp,length,comma,precision_opt,rp) ->
--

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#34 lore raw

PATCH 15/26 parsing_cocci: compute_lines: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in compute_lines.ml.

---
 parsing_cocci/compute_lines.ml | 11 +++++++++++
 1 file changed, 11 insertions(+)
parsing_cocci/compute_lines.ml +11 -0
index f4b6f4d8..8231a0e7 100644
@@ -574,6 +574,17 @@ and typeC t =
       let rp2 = normal_mcode rp2 in
       mkres t (Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2))
 	ty (promote_mcode rp2)
+  | Ast0.ParenType(lp,ty,rp) ->
+      let lp = normal_mcode lp in
+      let rp = normal_mcode rp in
+      let ty = typeC ty in
+      mkres t (Ast0.ParenType(lp,ty,rp)) ty (promote_mcode rp)
+  | Ast0.FunctionType(ty,lp,params,rp) ->
+      let ty = typeC ty in
+      let lp = normal_mcode lp in
+      let params = parameter_list (Some(promote_mcode lp)) params in
+      let rp = normal_mcode rp in
+      mkres t (Ast0.FunctionType(ty,lp,params,rp)) ty (promote_mcode rp)
   | Ast0.Array(ty,lb,size,rb) ->
       let ty = typeC ty in
       let lb = normal_mcode lb in
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#35 lore raw

Re: [Cocci] [PATCH 15/26] parsing_cocci: compute_lines: Add cases for ParenType/FunctionType

Quoted 13 lines · Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in compute_lines.ml.

---
 parsing_cocci/compute_lines.ml | 11 +++++++++++
 1 file changed, 11 insertions(+)
parsing_cocci/compute_lines.ml +0 -0
index f4b6f4d8..8231a0e7 100644

parsing_cocci/compute_lines.ml and typeC t = · line 581

Quoted 8 lines
parsing_cocci/compute_lines.ml +5 -0
@@ -574,6 +574,17 @@ and typeC t =
       let rp2 = normal_mcode rp2 in
       mkres t (Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2))
 	ty (promote_mcode rp2)
+  | Ast0.ParenType(lp,ty,rp) ->
+      let lp = normal_mcode lp in
+      let rp = normal_mcode rp in
+      let ty = typeC ty in
+      mkres t (Ast0.ParenType(lp,ty,rp)) ty (promote_mcode rp)

The type is not the leftmost thing. So the third argument should be (promote_mcode lp).

julia

Quoted 10 lines
parsing_cocci/compute_lines.ml +6 -1
+  | Ast0.FunctionType(ty,lp,params,rp) ->
+      let ty = typeC ty in
+      let lp = normal_mcode lp in
+      let params = parameter_list (Some(promote_mcode lp)) params in
+      let rp = normal_mcode rp in
+      mkres t (Ast0.FunctionType(ty,lp,params,rp)) ty (promote_mcode rp)
   | Ast0.Array(ty,lb,size,rb) ->
       let ty = typeC ty in
       let lb = normal_mcode lb in
--
Quoted 3 lines

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#36 lore raw

PATCH 16/26 parsing_cocci: ast0toast: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in ast0toast.ml.

---
 parsing_cocci/ast0toast.ml | 6 ++++++
 1 file changed, 6 insertions(+)
parsing_cocci/ast0toast.ml +6 -0
index f1bbde0b..dc7b30f7 100644
@@ -562,6 +562,7 @@ and typeC allminus t =
 	| types -> Ast.DisjType(List.map (rewrap t no_isos) types))
     | Ast0.BaseType(_) | Ast0.Signed(_,_) | Ast0.Pointer(_,_)
     | Ast0.FunctionPointer(_,_,_,_,_,_,_)
+    | Ast0.ParenType(_,_,_) | Ast0.FunctionType(_,_,_,_)
     | Ast0.Array(_,_,_,_) | Ast0.Decimal(_,_,_,_,_,_)
     | Ast0.EnumName(_,_) | Ast0.StructUnionName(_,_)
     | Ast0.StructUnionDef(_,_,_,_) | Ast0.EnumDef(_,_,_,_)
@@ -588,6 +589,11 @@ and base_typeC allminus t =
       Ast.FunctionPointer
 	(typeC allminus ty,mcode lp1,mcode star,mcode rp1,
 	 mcode lp2,parameter_list params,mcode rp2)
+  | Ast0.ParenType(lp,ty,rp) ->
+      Ast.ParenType(mcode lp,typeC allminus ty,mcode rp)
+  | Ast0.FunctionType(ty,lp,params,rp) ->
+      Ast.FunctionType
+        (typeC allminus ty,mcode lp,parameter_list params,mcode rp)
   | Ast0.Array(ty,lb,size,rb) ->
       Ast.Array(typeC allminus ty,mcode lb,get_option expression size,
 		mcode rb)
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#37 lore raw

PATCH 17/26 parsing_cocci: type_cocci: Add ParenType/FunctionType to types

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in type_cocci.ml.

---
 parsing_cocci/type_cocci.mli | 2 ++
 1 file changed, 2 insertions(+)
parsing_cocci/type_cocci.mli +2 -0
index c1ccb58f..6f24adf7 100644
@@ -16,6 +16,8 @@ type typeC =
   | SignedT         of sign * typeC option
   | Pointer         of typeC
   | FunctionPointer of typeC (* only return type *)
+  | ParenType       of typeC (* only return type *)
+  | FunctionType    of typeC (* only return type *)
   | Array           of typeC (* drop size info *)
   | Decimal         of name * name
   | EnumName        of name
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#38 lore raw

PATCH 18/26 parsing_cocci: unify_ast: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in unify_ast.ml.

---
 parsing_cocci/unify_ast.ml | 8 ++++++++
 1 file changed, 8 insertions(+)
parsing_cocci/unify_ast.ml +8 -0
index d4ad3030..de86b1d3 100644
@@ -336,6 +336,14 @@ and unify_typeC t1 t2 =
 	 unify_fullType tya tyb &&
 	 unify_dots unify_parameterTypeDef pdots paramsa paramsb
        else false
+  | (Ast.ParenType(lpa,tya,rpa),Ast.ParenType(lpb,tyb,rpb)) ->
+      unify_fullType tya tyb && unify_mcode lpa lpb && unify_mcode rpa rpb
+  | (Ast.FunctionType(tya,lpa,paramsa,rpa),
+     Ast.FunctionType(tyb,lpb,paramsb,rpb)) ->
+      unify_fullType tya tyb &&
+      unify_mcode lpa lpb &&
+      unify_dots unify_parameterTypeDef pdots paramsa paramsb &&
+      unify_mcode rpa rpb
   | (Ast.Array(ty1,lb1,e1,rb1),Ast.Array(ty2,lb2,e2,rb2)) ->
       unify_fullType ty1 ty2 && unify_option unify_expression e1 e2
   | (Ast.Decimal(dec1,lp1,len1,comma1,prec_opt1,rp1),
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#39 lore raw

PATCH 19/26 parsing_cocci: disjdistr: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in disjdistr.ml.

---
 parsing_cocci/disjdistr.ml | 10 ++++++++++
 1 file changed, 10 insertions(+)
parsing_cocci/disjdistr.ml +10 -0
index 668a8810..91d7f1b8 100644
@@ -73,6 +73,16 @@ and disjtypeC bty =
 	(function ty ->
 	  Ast.rewrap bty (Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2)))
 	ty
+  | Ast.ParenType(lp,ty,rp) ->
+      let ty = disjty ty in
+      List.map
+        (function ty ->
+          Ast.rewrap bty (Ast.ParenType(lp,ty,rp))) ty
+  | Ast.FunctionType(ty,lp,params,rp) ->
+      let ty = disjty ty in
+      List.map
+        (function ty ->
+          Ast.rewrap bty (Ast.FunctionType(ty,lp,params,rp))) ty
   | Ast.Array(ty,lb,size,rb) ->
       disjmult2 (disjty ty) (disjoption disjexp size)
 	(function ty -> function size ->
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#40 lore raw

PATCH 20/26 parsing_cocci: ast_cocci: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in ast_cocci.ml.

---
 parsing_cocci/ast_cocci.ml | 10 ++++++++++
 1 file changed, 10 insertions(+)
parsing_cocci/ast_cocci.ml +10 -0
index f8e6dee6..4fea9794 100644
@@ -1110,6 +1110,10 @@ let rec string_of_typeC ty =
       string_of_fullType ty' ^ "*"
   | FunctionPointer (ty', _, _, _, _, _, _) ->
       string_of_fullType ty' ^ "(*)()"
+  | ParenType (_ , ty', _) ->
+      "(" ^ string_of_fullType ty' ^ ")"
+  | FunctionType (ty' , _, _, _) ->
+      string_of_fullType ty' ^ "()"
   | Array (ty', _, _, _) ->
       string_of_fullType ty' ^ "[]"
   | Decimal(_, _, e0, _, e1, _) ->
@@ -1191,6 +1195,10 @@ and typeC_map tr ty =
   | Pointer (ty', s) -> rewrap ty (Pointer (fullType_map tr ty', s))
   | FunctionPointer (ty, s0, s1, s2, s3, s4, s5) ->
       rewrap ty (FunctionPointer (fullType_map tr ty, s0, s1, s2, s3, s4, s5))
+  | ParenType (s0, ty', s1) ->
+      rewrap ty (ParenType (s0, fullType_map tr ty', s1))
+  | FunctionType (ty', s0, s1, s2) ->
+      rewrap ty (FunctionType (fullType_map tr ty', s0, s1, s2))
   | Array (ty', s0, s1, s2) ->
       rewrap ty (Array (fullType_map tr ty', s0, s1, s2))
   | EnumName (s0, ident) ->
@@ -1250,6 +1258,8 @@ and typeC_fold tr ty v =
   | SignedT (_, Some ty') -> typeC_fold tr ty' v
   | Pointer (ty', _)
   | FunctionPointer (ty', _, _, _, _, _, _)
+  | ParenType (_, ty', _)
+  | FunctionType (ty', _, _, _)
   | Array (ty', _, _, _)
   | EnumDef (ty', _, _, _)
   | StructUnionDef (ty', _, _, _) -> fullType_fold tr ty' v
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#41 lore raw

PATCH 21/26 parsing_cocci: pretty_print_cocci: Print ParenType/FunctionType

In reply to Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

        <type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
 parsing_cocci/pretty_print_cocci.ml | 57 +++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
parsing_cocci/pretty_print_cocci.ml +57 -0
index 6338e464..ef60106c 100644
@@ -403,6 +403,54 @@ and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
   mcode print_string rp1; mcode print_string lp1;
   parameter_list params; mcode print_string rp2
 
+and print_parentype (lp,ty,rp) fn =
+  match Ast.unwrap ty with
+   Ast.Type(_,_,fty1) ->
+    (match Ast.unwrap fty1 with
+      Ast.Pointer(ty1,star) ->
+       (match Ast.unwrap ty1 with
+         Ast.Type(_,_,fty2) ->
+          (match Ast.unwrap fty2 with
+            Ast.FunctionType(ty2,lp2,params,rp2) ->
+             fullType ty2;
+             print_space();
+             mcode print_string lp;
+             mcode print_string star;
+             fn();
+             mcode print_string rp;
+             mcode print_string lp2;
+             parameter_list params;
+             mcode print_string rp2
+         | _ -> failwith "ParenType Pretty_print_cocci")
+       | _ -> failwith "ParenType Pretty_print_cocci")
+    | Ast.Array(ty1,lb1,size1,rb1) ->
+       (match Ast.unwrap ty1 with
+         Ast.Type(_,_,fty2) ->
+          (match Ast.unwrap fty2 with
+            Ast.Pointer(ty2,star) ->
+             (match Ast.unwrap ty2 with
+               Ast.Type(_,_,fty3) ->
+                (match Ast.unwrap fty3 with
+                  Ast.FunctionType(ty3,lp3,params,rp3) ->
+                  fullType ty3;
+                  print_space();
+                  mcode print_string lp;
+                  mcode print_string star;
+                  fn();
+                  mcode print_string lb1;
+                  print_option expression size1;
+                  mcode print_string rb1;
+                  mcode print_string rp;
+                  mcode print_string lp3;
+                  parameter_list params;
+                  mcode print_string rp3
+                | _ -> failwith "ParenType Pretty_print_cocci")
+             | _ -> failwith "ParenType Pretty_print_cocci")
+          | _ -> failwith "ParenType Pretty_print_cocci")
+       | _ -> failwith "ParenType Pretty_print_cocci")
+    | _ -> failwith "ParenType Pretty_print_cocci")
+  | _ -> failwith "ParenType Pretty_print_cocci"
+
 and varargs = function
   | None -> ()
   | Some (comma, ellipsis) ->
@@ -424,6 +472,13 @@ and typeC ty =
   | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
       print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
 	(function _ -> ())
+  | Ast.ParenType(lp,ty,rp) ->
+      print_parentype (lp,ty,rp) (function _ -> ())
+  | Ast.FunctionType(ty,lp,params,rp) ->
+      fullType ty;
+      mcode print_string lp;
+      parameter_list params;
+      mcode print_string rp
   | Ast.Array(ty,lb,size,rb) ->
       fullType ty; mcode print_string lb; print_option expression size;
       mcode print_string rb
@@ -498,6 +553,8 @@ and print_named_type ty id =
 		| _ -> failwith "complex array types not supported")
 	    | _ -> typeC ty; id(); k () in
 	  loop ty1 (function _ -> ())
+      | Ast.ParenType(lp,ty,rp) ->
+          print_parentype (lp,ty,rp) (function _ -> id())
       | _ -> fullType ty; id())
   | _ -> fullType ty; id()
 
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#42 lore raw

Re: [Cocci] [PATCH 21/26] parsing_cocci: pretty_print_cocci: Print ParenType/FunctionType

Quoted 59 lines · Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

        <type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
 parsing_cocci/pretty_print_cocci.ml | 57 +++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
parsing_cocci/pretty_print_cocci.ml +37 -0
index 6338e464..ef60106c 100644
@@ -403,6 +403,54 @@ and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
   mcode print_string rp1; mcode print_string lp1;
   parameter_list params; mcode print_string rp2
 
+and print_parentype (lp,ty,rp) fn =
+  match Ast.unwrap ty with
+   Ast.Type(_,_,fty1) ->
+    (match Ast.unwrap fty1 with
+      Ast.Pointer(ty1,star) ->
+       (match Ast.unwrap ty1 with
+         Ast.Type(_,_,fty2) ->
+          (match Ast.unwrap fty2 with
+            Ast.FunctionType(ty2,lp2,params,rp2) ->
+             fullType ty2;
+             print_space();
+             mcode print_string lp;
+             mcode print_string star;
+             fn();
+             mcode print_string rp;
+             mcode print_string lp2;
+             parameter_list params;
+             mcode print_string rp2
+         | _ -> failwith "ParenType Pretty_print_cocci")
+       | _ -> failwith "ParenType Pretty_print_cocci")
+    | Ast.Array(ty1,lb1,size1,rb1) ->
+       (match Ast.unwrap ty1 with
+         Ast.Type(_,_,fty2) ->
+          (match Ast.unwrap fty2 with
+            Ast.Pointer(ty2,star) ->
+             (match Ast.unwrap ty2 with
+               Ast.Type(_,_,fty3) ->
+                (match Ast.unwrap fty3 with
+                  Ast.FunctionType(ty3,lp3,params,rp3) ->
+                  fullType ty3;
+                  print_space();
+                  mcode print_string lp;
+                  mcode print_string star;
+                  fn();
+                  mcode print_string lb1;
+                  print_option expression size1;
+                  mcode print_string rb1;

parsing_cocci/pretty_print_cocci.ml and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn = · line 452

Quoted 10 lines
parsing_cocci/pretty_print_cocci.ml +10 -0
+                  mcode print_string rp;
+                  mcode print_string lp3;
+                  parameter_list params;
+                  mcode print_string rp3
+                | _ -> failwith "ParenType Pretty_print_cocci")
+             | _ -> failwith "ParenType Pretty_print_cocci")
+          | _ -> failwith "ParenType Pretty_print_cocci")
+       | _ -> failwith "ParenType Pretty_print_cocci")
+    | _ -> failwith "ParenType Pretty_print_cocci")
+  | _ -> failwith "ParenType Pretty_print_cocci"

Can some code be shared here?

julia

Quoted 28 lines
parsing_cocci/pretty_print_cocci.ml +10 -1
+
 and varargs = function
   | None -> ()
   | Some (comma, ellipsis) ->
@@ -424,6 +472,13 @@ and typeC ty =
   | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
       print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
 	(function _ -> ())
+  | Ast.ParenType(lp,ty,rp) ->
+      print_parentype (lp,ty,rp) (function _ -> ())
+  | Ast.FunctionType(ty,lp,params,rp) ->
+      fullType ty;
+      mcode print_string lp;
+      parameter_list params;
+      mcode print_string rp
   | Ast.Array(ty,lb,size,rb) ->
       fullType ty; mcode print_string lb; print_option expression size;
       mcode print_string rb
@@ -498,6 +553,8 @@ and print_named_type ty id =
 		| _ -> failwith "complex array types not supported")
 	    | _ -> typeC ty; id(); k () in
 	  loop ty1 (function _ -> ())
+      | Ast.ParenType(lp,ty,rp) ->
+          print_parentype (lp,ty,rp) (function _ -> id())
       | _ -> fullType ty; id())
   | _ -> fullType ty; id()
 
--
Quoted 3 lines

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#43 lore raw

PATCH 22/26 parsing_c: unparse_cocci: Print ParenType/FunctionType

In reply to Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

        <type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
 parsing_c/unparse_cocci.ml | 60 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
parsing_c/unparse_cocci.ml +60 -0
index 30e755e9..9dd84821 100644
@@ -722,6 +722,13 @@ and typeC ty =
   | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
       print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
 	(function _ -> ())
+  | Ast.ParenType(lp,ty,rp) ->
+      print_parentype (lp,ty,rp) (function _ -> ())
+  | Ast.FunctionType(ty,lp,params,rp) ->
+      fullType ty;
+      mcode print_string lp;
+      parameter_list params;
+      mcode print_string rp
   | Ast.Array(ty,lb,size,rb) ->
       fullType ty; mcode print_string lb; print_option expression size;
       mcode print_string rb
@@ -786,6 +793,57 @@ and storage = function
   | Ast.Register -> print_string "register"
   | Ast.Extern -> print_string "extern"
 
+(* --------------------------------------------------------------------- *)
+(* ParenType *)
+
+and print_parentype (lp,ty,rp) fn =
+  match Ast.unwrap ty with
+   Ast.Type(_,_,fty1) ->
+    (match Ast.unwrap fty1 with
+      Ast.Pointer(ty1,star) ->
+       (match Ast.unwrap ty1 with
+         Ast.Type(_,_,fty2) ->
+          (match Ast.unwrap fty2 with
+            Ast.FunctionType(ty2,lp2,params,rp2) ->
+             fullType ty2;
+             pr_space();
+             mcode print_string lp;
+             mcode print_string star;
+             fn();
+             mcode print_string rp;
+             mcode print_string lp2;
+             parameter_list params;
+             mcode print_string rp2
+         | _ -> failwith "ParenType Unparse_cocci")
+       | _ -> failwith "ParenType Unparse_cocci")
+    | Ast.Array(ty1,lb1,size1,rb1) ->
+       (match Ast.unwrap ty1 with
+         Ast.Type(_,_,fty2) ->
+          (match Ast.unwrap fty2 with
+            Ast.Pointer(ty2,star) ->
+             (match Ast.unwrap ty2 with
+               Ast.Type(_,_,fty3) ->
+                (match Ast.unwrap fty3 with
+                  Ast.FunctionType(ty3,lp3,params,rp3) ->
+                  fullType ty3;
+                  pr_space();
+                  mcode print_string lp;
+                  mcode print_string star;
+                  fn();
+                  mcode print_string lb1;
+                  print_option expression size1;
+                  mcode print_string rb1;
+                  mcode print_string rp;
+                  mcode print_string lp3;
+                  parameter_list params;
+                  mcode print_string rp3
+                | _ -> failwith "ParenType Unparse_cocci")
+             | _ -> failwith "ParenType Unparse_cocci")
+          | _ -> failwith "ParenType Unparse_cocci")
+       | _ -> failwith "ParenType Unparse_cocci")
+    | _ -> failwith "ParenType Unparse_cocci")
+  | _ -> failwith "ParenType Unparse_cocci"
+
 (* --------------------------------------------------------------------- *)
 (* Variable declaration *)
 
@@ -818,6 +876,8 @@ and print_named_type ty id =
 		pretty_print_c.Pretty_print_c.type_with_ident ty
 		  (function _ -> id())
             | _ -> error name ty "type value expected")
+      | Ast.ParenType(lp,ty,rp) ->
+          print_parentype (lp,ty,rp) (function _ -> id())
     (*| should have a case here for pointer to array or function type
         that would put ( * ) around the variable.  This makes one wonder
         why we really need a special case for function pointer *)
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#44 lore raw

Re: [Cocci] [PATCH 22/26] parsing_c: unparse_cocci: Print ParenType/FunctionType

Quoted 76 lines · Jaskaran Singh

The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following:

        <type> ( * id [ .* ] ) ( params )

i.e., a function pointer or an array of function pointers, and will fail for any other cases. This is similar to the function used to print ParenType in Pretty_print_c.

---
 parsing_c/unparse_cocci.ml | 60 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
parsing_c/unparse_cocci.ml +47 -0
index 30e755e9..9dd84821 100644
@@ -722,6 +722,13 @@ and typeC ty =
   | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
       print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
 	(function _ -> ())
+  | Ast.ParenType(lp,ty,rp) ->
+      print_parentype (lp,ty,rp) (function _ -> ())
+  | Ast.FunctionType(ty,lp,params,rp) ->
+      fullType ty;
+      mcode print_string lp;
+      parameter_list params;
+      mcode print_string rp
   | Ast.Array(ty,lb,size,rb) ->
       fullType ty; mcode print_string lb; print_option expression size;
       mcode print_string rb
@@ -786,6 +793,57 @@ and storage = function
   | Ast.Register -> print_string "register"
   | Ast.Extern -> print_string "extern"
 
+(* --------------------------------------------------------------------- *)
+(* ParenType *)
+
+and print_parentype (lp,ty,rp) fn =
+  match Ast.unwrap ty with
+   Ast.Type(_,_,fty1) ->
+    (match Ast.unwrap fty1 with
+      Ast.Pointer(ty1,star) ->
+       (match Ast.unwrap ty1 with
+         Ast.Type(_,_,fty2) ->
+          (match Ast.unwrap fty2 with
+            Ast.FunctionType(ty2,lp2,params,rp2) ->
+             fullType ty2;
+             pr_space();
+             mcode print_string lp;
+             mcode print_string star;
+             fn();
+             mcode print_string rp;
+             mcode print_string lp2;
+             parameter_list params;
+             mcode print_string rp2
+         | _ -> failwith "ParenType Unparse_cocci")
+       | _ -> failwith "ParenType Unparse_cocci")
+    | Ast.Array(ty1,lb1,size1,rb1) ->
+       (match Ast.unwrap ty1 with
+         Ast.Type(_,_,fty2) ->
+          (match Ast.unwrap fty2 with
+            Ast.Pointer(ty2,star) ->
+             (match Ast.unwrap ty2 with
+               Ast.Type(_,_,fty3) ->
+                (match Ast.unwrap fty3 with
+                  Ast.FunctionType(ty3,lp3,params,rp3) ->
+                  fullType ty3;
+                  pr_space();
+                  mcode print_string lp;
+                  mcode print_string star;
+                  fn();
+                  mcode print_string lb1;
+                  print_option expression size1;
+                  mcode print_string rb1;

parsing_c/unparse_cocci.ml and storage = function · line 845

Quoted 10 lines
parsing_c/unparse_cocci.ml +10 -0
+                  mcode print_string rp;
+                  mcode print_string lp3;
+                  parameter_list params;
+                  mcode print_string rp3
+                | _ -> failwith "ParenType Unparse_cocci")
+             | _ -> failwith "ParenType Unparse_cocci")
+          | _ -> failwith "ParenType Unparse_cocci")
+       | _ -> failwith "ParenType Unparse_cocci")
+    | _ -> failwith "ParenType Unparse_cocci")
+  | _ -> failwith "ParenType Unparse_cocci"

Can some code be shared?

parsing_c/unparse_cocci.ml and print_named_type ty id = · line 880

Quoted 10 lines
parsing_c/unparse_cocci.ml +3 -0
+
 (* --------------------------------------------------------------------- *)
 (* Variable declaration *)
 
@@ -818,6 +876,8 @@ and print_named_type ty id =
 		pretty_print_c.Pretty_print_c.type_with_ident ty
 		  (function _ -> id())
             | _ -> error name ty "type value expected")
+      | Ast.ParenType(lp,ty,rp) ->
+          print_parentype (lp,ty,rp) (function _ -> id())

Is it normal that there is no FunctionType case here?

julia

Quoted 7 lines
parsing_c/unparse_cocci.ml +0 -1
     (*| should have a case here for pointer to array or function type
         that would put ( * ) around the variable.  This makes one wonder
         why we really need a special case for function pointer *)
--

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#45 lore raw

PATCH 23/26 engine: Match A.ParenType and A.FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are added to the SmPL AST as types. Match these types correctly.

---
 engine/check_exhaustive_pattern.ml |  2 ++
 engine/cocci_vs_c.ml               | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
engine/check_exhaustive_pattern.ml +2 -0
index 5a047f73..872c060c 100644
@@ -149,6 +149,8 @@ let dumb_astcocci_type = function
  | A.SignedT (signa,tya) -> ()
  | A.Pointer (typa, _) -> ()
  | A.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> ()
+ | A.ParenType(lp,ty,rp) -> ()
+ | A.FunctionType(ty,lp,params,rp) -> ()
  | A.Array (typa, _, eaopt, _) -> ()
  | A.Decimal(_, _, _, _, _, _) -> ()
  | A.EnumName(en, ena) -> ()
engine/cocci_vs_c.ml +27 -0
index a4a0e53b..3e0eb50e 100644
@@ -3785,6 +3785,29 @@ and (typeC: (A.typeC, Ast_c.typeC) matcher) =
         | _ -> fail
         )
 
+    | A.ParenType (lpa, typa, rpa), (B.ParenType typb, ii) ->
+        let (lpb, rpb) = tuple_of_list2 ii in
+        fullType typa typb >>= (fun typa typb ->
+        tokenf lpa lpb >>= (fun lpa lpb ->
+        tokenf rpa rpb >>= (fun rpa rpb ->
+          return (
+            (A.ParenType (lpa, typa, rpa)) +> A.rewrap ta,
+            (B.ParenType (typb), [lpb;rpb])
+          ))))
+
+    | A.FunctionType (typa, lpa, paramsa, rpa),
+        (B.FunctionType (typb, (paramsb, (isvaargs, iidotsb))), ii) ->
+        let (lpb, rpb) = tuple_of_list2 ii in
+        fullType typa typb >>= (fun typa typb ->
+        tokenf lpa lpb >>= (fun lpa lpb ->
+        tokenf rpa rpb >>= (fun rpa rpb ->
+        parameters (seqstyle paramsa) (A.unwrap paramsa) paramsb >>=
+        (fun paramsaunwrap paramsb ->
+          let paramsa = A.rewrap paramsa paramsaunwrap in
+          return (
+            (A.FunctionType (typa, lpa, paramsa, rpa)) +> A.rewrap ta,
+            (B.FunctionType (typb, (paramsb, (isvaargs, iidotsb))), [lpb;rpb])
+          )))))
 
 
     (* todo: handle the iso on optional size specification ? *)
@@ -4361,6 +4384,10 @@ and compatible_typeC a (b,local) =
     | A.FunctionPointer (a, _, _, _, _, _, _), _ ->
 	failwith
 	  "TODO: function pointer type doesn't store enough information to determine compatibility"
+    | A.ParenType (_, a, _), (qub, (B.ParenType b, ii)) ->
+	compatible_type a (b, local)
+    | A.FunctionType (a, _, _, _), (qub, (B.FunctionType (b,_), ii)) ->
+	compatible_type a (b, local)
     | A.Array (a, _, _, _), (qub, (B.Array (eopt, b),ii)) ->
       (* no size info for cocci *)
 	compatible_type a (b, local)
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#46 lore raw

PATCH 24/26 tools: spgen: Add cases for ParenType/FunctionType

In reply to Jaskaran Singh

ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in position_generator.ml.

---
 tools/spgen/source/position_generator.ml | 6 ++++++
 1 file changed, 6 insertions(+)
tools/spgen/source/position_generator.ml +6 -0
index 31d6cb5e..2fa630fb 100644
@@ -195,6 +195,12 @@ let rec type_pos t snp
   | Ast0.FunctionPointer(t,lp,star,rp,lp2,params,rp2) ->
       let constructor ~mc = Ast0.FunctionPointer(t,lp,star,rp,lp2,params,mc) in
       mcode_wrap ~mc:rp2 ~constructor snp
+  | Ast0.ParenType(lp,t,rp) ->
+      let constructor ~mc = Ast0.ParenType(lp,t,rp) in
+      mcode_wrap ~mc:rp ~constructor snp
+  | Ast0.FunctionType(t,lp,params,rp) ->
+      let constructor ~mc = Ast0.FunctionType(t,lp,params,rp) in
+      mcode_wrap ~mc:rp ~constructor snp
   | Ast0.Array(t,lb,expr,rb) ->
       let constructor ~mc = Ast0.Array(t,lb,expr,mc) in
       mcode_wrap ~mc:rb ~constructor snp
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#47 lore raw

PATCH 25/26 cocci: Remove Ast_cocci.FunctionPointer

In reply to Jaskaran Singh

ParenType and FunctionType are now in the SmPL AST, and these types have replaced all productions related to the FunctionPointer type in the SmPL parser.

Remove FunctionPointer from the SmPL ASTs, its cases and any functions or constructs related to it from the codebase.

---
 engine/check_exhaustive_pattern.ml       |  1 -
 engine/cocci_vs_c.ml                     | 55 ------------------------
 ocaml/coccilib.mli                       |  4 --
 parsing_c/unparse_cocci.ml               | 14 +-----
 parsing_cocci/adjust_pragmas.ml          |  3 --
 parsing_cocci/arity.ml                   |  8 ----
 parsing_cocci/ast0_cocci.ml              |  4 --
 parsing_cocci/ast0_cocci.mli             |  3 --
 parsing_cocci/ast0toast.ml               |  5 ---
 parsing_cocci/ast_cocci.ml               |  8 ----
 parsing_cocci/ast_cocci.mli              |  3 --
 parsing_cocci/check_meta.ml              |  3 --
 parsing_cocci/compute_lines.ml           | 10 -----
 parsing_cocci/disjdistr.ml               |  6 ---
 parsing_cocci/function_prototypes.ml     |  3 --
 parsing_cocci/get_constants.ml           |  2 +-
 parsing_cocci/index.ml                   |  1 -
 parsing_cocci/iso_pattern.ml             | 12 ------
 parsing_cocci/pretty_print_cocci.ml      | 14 +-----
 parsing_cocci/single_statement.ml        |  1 -
 parsing_cocci/type_cocci.mli             |  1 -
 parsing_cocci/type_infer.ml              | 25 +++++------
 parsing_cocci/unify_ast.ml               |  8 ----
 parsing_cocci/unparse_ast0.ml            | 13 +-----
 parsing_cocci/visitor_ast.ml             | 26 -----------
 parsing_cocci/visitor_ast0.ml            | 51 +---------------------
 tools/spgen/source/meta_variable.ml      |  1 -
 tools/spgen/source/position_generator.ml |  3 --
 28 files changed, 19 insertions(+), 269 deletions(-)
engine/check_exhaustive_pattern.ml +0 -1
index 872c060c..fe0b2c78 100644
@@ -148,7 +148,6 @@ let dumb_astcocci_type = function
  | A.BaseType (basea,strings) -> ()
  | A.SignedT (signa,tya) -> ()
  | A.Pointer (typa, _) -> ()
- | A.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> ()
  | A.ParenType(lp,ty,rp) -> ()
  | A.FunctionType(ty,lp,params,rp) -> ()
  | A.Array (typa, _, eaopt, _) -> ()
engine/cocci_vs_c.ml +0 -55

67 lines not on this page · raw message

ocaml/coccilib.mli +0 -4
index e5409a97..c54607d5 100644
@@ -2707,8 +2707,6 @@ module Ast_cocci :
         BaseType of baseType * string mcode list
       | SignedT of sign mcode * typeC option
       | Pointer of fullType * string mcode
-      | FunctionPointer of fullType * string mcode * string mcode *
-          string mcode * string mcode * parameter_list * string mcode
       | ParenType of string mcode (* ( *) * fullType * string mcode (* ) *)
       | FunctionType of fullType *
           string mcode (* ( *) * parameter_list * string mcode (* ) *)
@@ -3354,8 +3352,6 @@ module Ast0_cocci :
       | BaseType of Ast_cocci.baseType * string mcode list
       | Signed of Ast_cocci.sign mcode * typeC option
       | Pointer of typeC * string mcode
-      | FunctionPointer of typeC * string mcode * string mcode *
-          string mcode * string mcode * parameter_list * string mcode
       | ParenType of string mcode * typeC * string mcode
       | FunctionType of typeC *
           string mcode * parameter_list * string mcode
parsing_c/unparse_cocci.ml +1 -13
index 9dd84821..6949f9b2 100644
@@ -700,12 +700,6 @@ and fullType ft =
   | Ast.DisjType _ | Ast.ConjType _ -> raise CantBeInPlus
   | Ast.OptType(_) -> raise CantBeInPlus
 
-and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
-  fullType ty; pr_space();
-  mcode print_string lp1; mcode print_string star; fn();
-  mcode print_string rp1; mcode print_string lp1;
-  parameter_list params; mcode print_string rp2
-
 and print_fninfo = function
     Ast.FStorage(stg) -> mcode storage stg
   | Ast.FType(ty) -> fullType ty
@@ -719,9 +713,6 @@ and typeC ty =
   | Ast.SignedT(sgn,ty) -> mcode sign sgn; print_option_prespace typeC ty
   | Ast.Pointer(ty,star) ->
       fullType ty; ft_space ty; mcode print_string star; eatspace()
-  | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-      print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
-	(function _ -> ())
   | Ast.ParenType(lp,ty,rp) ->
       print_parentype (lp,ty,rp) (function _ -> ())
   | Ast.FunctionType(ty,lp,params,rp) ->
@@ -851,10 +842,7 @@ and print_named_type ty id =
   match Ast.unwrap ty with
     Ast.Type(_,None,ty1) ->
       (match Ast.unwrap ty1 with
-	Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-	  print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
-	    (function _ -> id())
-      | Ast.Array(_,_,_,_) ->
+        Ast.Array(_,_,_,_) ->
 	  let rec loop ty k =
 	    match Ast.unwrap ty with
 	      Ast.Array(ty,lb,size,rb) ->
parsing_cocci/adjust_pragmas.ml +0 -3
index 537150e7..a7f681f0 100644
@@ -168,9 +168,6 @@ let rec left_ty t =
       call_right left_mcode sign t (function sign -> Ast0.Signed(sign,ty))
   | Ast0.Pointer(ty,star) ->
       call_right left_ty ty t (function ty -> Ast0.Pointer(ty,star))
-  | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-      call_right left_ty ty t
-	(function ty -> Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2))
   | Ast0.ParenType(lp,ty,rp) ->
       call_right left_ty ty t (function ty -> Ast0.ParenType(lp,ty,rp))
   | Ast0.FunctionType(ty,lp,params,rp) ->
parsing_cocci/arity.ml +0 -8
index 3b408554..e1515873 100644
@@ -411,14 +411,6 @@ and top_typeC tgt opt_allowed typ =
       let ty = typeC arity ty in
       let star = mcode star in
       make_typeC typ tgt arity (Ast0.Pointer(ty,star))
-  | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-      let arity =
-	all_same opt_allowed tgt (mcode2line lp1)
-	  (List.map mcode2arity [lp1;star;rp1;lp2;rp2]) in
-      let ty = typeC arity ty in
-      let params = parameter_list tgt params in
-      make_typeC typ tgt arity
-	(Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2))
   | Ast0.ParenType(lp,ty,rp) ->
       let arity =
         all_same opt_allowed tgt (mcode2line lp)
parsing_cocci/ast0_cocci.ml +0 -4
index 0acbeaa4..1b4087c3 100644
@@ -200,9 +200,6 @@ and base_typeC =
   | BaseType        of Ast.baseType * string mcode list
   | Signed          of Ast.sign mcode * typeC option
   | Pointer         of typeC * string mcode (* * *)
-  | FunctionPointer of typeC *
-	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
-                  string mcode (* ( *)*parameter_list*string mcode(* ) *)
   | ParenType       of string mcode (* ( *) * typeC * string mcode (* ) *)
   | FunctionType    of typeC *
                   string mcode (* ( *) * parameter_list * string mcode (* ) *)
@@ -737,7 +734,6 @@ let rec meta_names_of_typeC ty =
     ConstVol (_, ty)
   | Signed (_, Some ty)
   | Pointer (ty, _)
-  | FunctionPointer (ty, _, _, _, _, _, _)
   | Array (ty, _, _, _) -> meta_names_of_typeC ty
   | EnumName (_, Some ident)
   | StructUnionName(_, Some ident) -> meta_names_of_ident ident
parsing_cocci/ast0_cocci.mli +0 -3
index 33bd12b2..c1bc561b 100644
@@ -191,9 +191,6 @@ and base_typeC =
   | BaseType        of Ast_cocci.baseType * string mcode list
   | Signed          of Ast_cocci.sign mcode * typeC option
   | Pointer         of typeC * string mcode (* * *)
-  | FunctionPointer of typeC *
-	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
-                  string mcode (* ( *)*parameter_list*string mcode(* ) *)
   | ParenType       of string mcode (* ( *) * typeC * string mcode (* ) *)
   | FunctionType    of typeC *
                   string mcode (* ( *) * parameter_list * string mcode (* ) *)
parsing_cocci/ast0toast.ml +0 -5
index dc7b30f7..375b2292 100644
@@ -561,7 +561,6 @@ and typeC allminus t =
 	  [ty] -> ty
 	| types -> Ast.DisjType(List.map (rewrap t no_isos) types))
     | Ast0.BaseType(_) | Ast0.Signed(_,_) | Ast0.Pointer(_,_)
-    | Ast0.FunctionPointer(_,_,_,_,_,_,_)
     | Ast0.ParenType(_,_,_) | Ast0.FunctionType(_,_,_,_)
     | Ast0.Array(_,_,_,_) | Ast0.Decimal(_,_,_,_,_,_)
     | Ast0.EnumName(_,_) | Ast0.StructUnionName(_,_)
@@ -585,10 +584,6 @@ and base_typeC allminus t =
 	(mcode sgn,
 	 get_option (function x -> rewrap_iso x (base_typeC allminus x)) ty)
   | Ast0.Pointer(ty,star) -> Ast.Pointer(typeC allminus ty,mcode star)
-  | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-      Ast.FunctionPointer
-	(typeC allminus ty,mcode lp1,mcode star,mcode rp1,
-	 mcode lp2,parameter_list params,mcode rp2)
   | Ast0.ParenType(lp,ty,rp) ->
       Ast.ParenType(mcode lp,typeC allminus ty,mcode rp)
   | Ast0.FunctionType(ty,lp,params,rp) ->
parsing_cocci/ast_cocci.ml +0 -8
index 4fea9794..b1639ba6 100644
@@ -340,9 +340,6 @@ and base_typeC =
     BaseType        of baseType * string mcode list (* Yoann style *)
   | SignedT         of sign mcode * typeC option
   | Pointer         of fullType * string mcode (* * *)
-  | FunctionPointer of fullType *
-	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
-                  string mcode (* ( *)*parameter_list*string mcode(* ) *)
   | ParenType       of string mcode (* ( *) * fullType * string mcode (* ) *)
   | FunctionType    of fullType *
                   string mcode (* ( *) * parameter_list * string mcode (* ) *)
@@ -1108,8 +1105,6 @@ let rec string_of_typeC ty =
       ssign ^ " " ^ Common.default "" string_of_typeC ty'
   | Pointer (ty', _) ->
       string_of_fullType ty' ^ "*"
-  | FunctionPointer (ty', _, _, _, _, _, _) ->
-      string_of_fullType ty' ^ "(*)()"
   | ParenType (_ , ty', _) ->
       "(" ^ string_of_fullType ty' ^ ")"
   | FunctionType (ty' , _, _, _) ->
@@ -1193,8 +1188,6 @@ and typeC_map tr ty =
         | Some f -> rewrap ty (f ty' s)
       end
   | Pointer (ty', s) -> rewrap ty (Pointer (fullType_map tr ty', s))
-  | FunctionPointer (ty, s0, s1, s2, s3, s4, s5) ->
-      rewrap ty (FunctionPointer (fullType_map tr ty, s0, s1, s2, s3, s4, s5))
   | ParenType (s0, ty', s1) ->
       rewrap ty (ParenType (s0, fullType_map tr ty', s1))
   | FunctionType (ty', s0, s1, s2) ->
@@ -1257,7 +1250,6 @@ and typeC_fold tr ty v =
   | SignedT (_, None) -> v
   | SignedT (_, Some ty') -> typeC_fold tr ty' v
   | Pointer (ty', _)
-  | FunctionPointer (ty', _, _, _, _, _, _)
   | ParenType (_, ty', _)
   | FunctionType (ty', _, _, _)
   | Array (ty', _, _, _)
parsing_cocci/ast_cocci.mli +0 -3
index 7fb54e4d..6bc1d652 100644
@@ -322,9 +322,6 @@ and base_typeC =
     BaseType        of baseType * string mcode list (* Yoann style *)
   | SignedT         of sign mcode * typeC option
   | Pointer         of fullType * string mcode (* * *)
-  | FunctionPointer of fullType *
-	          string mcode(* ( *)*string mcode(* * *)*string mcode(* ) *)*
-                  string mcode (* ( *)*parameter_list*string mcode(* ) *)
   | ParenType       of string mcode (* ( *) * fullType * string mcode (* ) *)
   | FunctionType    of fullType *
                   string mcode (* ( *) * parameter_list * string mcode (* ) *)
parsing_cocci/check_meta.ml +0 -3
index 7b10bd53..fc6766ae 100644
@@ -221,9 +221,6 @@ and typeC old_metas table minus t =
   | Ast0.Signed(sgn,ty) ->
       get_opt (typeC old_metas table minus) ty
   | Ast0.Pointer(ty,star) -> typeC old_metas table minus ty
-  | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-      typeC old_metas table minus ty;
-      parameter_list old_metas table minus params
   | Ast0.ParenType(lp,ty,rp) ->
       typeC old_metas table minus ty
   | Ast0.FunctionType(ty,lp,params,rp) ->
parsing_cocci/compute_lines.ml +0 -10
index 8231a0e7..fb093299 100644
@@ -564,16 +564,6 @@ and typeC t =
       let ty = typeC ty in
       let star = normal_mcode star in
       mkres t (Ast0.Pointer(ty,star)) ty (promote_mcode star)
-  | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-      let ty = typeC ty in
-      let lp1 = normal_mcode lp1 in
-      let star = normal_mcode star in
-      let rp1 = normal_mcode rp1 in
-      let lp2 = normal_mcode lp2 in
-      let params = parameter_list (Some(promote_mcode lp2)) params in
-      let rp2 = normal_mcode rp2 in
-      mkres t (Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2))
-	ty (promote_mcode rp2)
   | Ast0.ParenType(lp,ty,rp) ->
       let lp = normal_mcode lp in
       let rp = normal_mcode rp in
parsing_cocci/disjdistr.ml +0 -6
index 91d7f1b8..ff49057c 100644
@@ -67,12 +67,6 @@ and disjtypeC bty =
   | Ast.Pointer(ty,star) ->
       let ty = disjty ty in
       List.map (function ty -> Ast.rewrap bty (Ast.Pointer(ty,star))) ty
-  | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-      let ty = disjty ty in
-      List.map
-	(function ty ->
-	  Ast.rewrap bty (Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2)))
-	ty
   | Ast.ParenType(lp,ty,rp) ->
       let ty = disjty ty in
       List.map
parsing_cocci/function_prototypes.ml +0 -3
index fd36f31b..b31178d2 100644
@@ -226,9 +226,6 @@ let rec attach_right strings ty =
     | Ast0.Signed(sgn,Some ty) ->
 	Ast0.Signed(sgn,Some (attach_right strings ty))
     | Ast0.Pointer(ty,star) -> Ast0.Pointer(ty,right_attach_mcode strings star)
-    | Ast0.FunctionPointer(ty,lp,star,rp,lp1,ps,rp1) ->
-	Ast0.FunctionPointer(ty,lp,star,rp,lp1,ps,
-			     right_attach_mcode strings rp1)
     | Ast0.ParenType(lp,ty,rp) ->
 	Ast0.ParenType(lp,ty,right_attach_mcode strings rp)
     | Ast0.FunctionType(ty,lp,ps,rp) ->
parsing_cocci/get_constants.ml +1 -1
index 65c72641..a23f2e4f 100644
@@ -181,7 +181,7 @@ let check_inherited nm =
     | _ -> k i in
 
   let rec type_collect res = function
-      TC.ConstVol(_,ty) | TC.Pointer(ty) | TC.FunctionPointer(ty)
+      TC.ConstVol(_,ty) | TC.Pointer(ty)
     | TC.Array(ty) -> type_collect res ty
     | TC.MetaType(tyname,_,_) ->
 	inherited tyname
parsing_cocci/index.ml +0 -1
index 5c91f620..9143203b 100644
@@ -92,7 +92,6 @@ let typeC t =
   | Ast0.BaseType(ty,strings) -> [48]
   | Ast0.Signed(sign,ty) -> [129]
   | Ast0.Pointer(ty,star) -> [49]
-  | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> [131]
   | Ast0.ParenType(lp,ty,rp) -> [138]
   | Ast0.FunctionType(ty,lp,params,rp) -> [139]
   | Ast0.Array(ty,lb,size,rb) -> [50]
parsing_cocci/iso_pattern.ml +0 -12
index 6d51b072..71640044 100644
@@ -863,14 +863,6 @@ let match_maker checks_needed context_required whencode_allowed =
 	      else return false
 	  | (Ast0.Pointer(tya,star1),Ast0.Pointer(tyb,star)) ->
 	      conjunct_bindings (check_mcode star1 star) (match_typeC tya tyb)
-	  | (Ast0.FunctionPointer(tya,lp1a,stara,rp1a,lp2a,paramsa,rp2a),
-	     Ast0.FunctionPointer(tyb,lp1b,starb,rp1b,lp2b,paramsb,rp2b)) ->
-	       conjunct_many_bindings
-		 [check_mcode stara starb; check_mcode lp1a lp1b;
-		   check_mcode rp1a rp1b; check_mcode lp2a lp2b;
-		   check_mcode rp2a rp2b; match_typeC tya tyb;
-		   match_dots match_param is_plist_matcher
-		     do_plist_match paramsa paramsb]
 	  | (Ast0.Array(tya,lb1,sizea,rb1),Ast0.Array(tyb,lb,sizeb,rb)) ->
 	      conjunct_many_bindings
 		[check_mcode lb1 lb; check_mcode rb1 rb;
@@ -1814,10 +1806,6 @@ let instantiate bindings mv_bindings model =
                           Ast0.rewrap ty (Ast0.ConstVol(cv,renamer ty'))
                       | Ast0.Pointer(ty', s) ->
                           Ast0.rewrap ty (Ast0.Pointer(renamer ty', s))
-                      | Ast0.FunctionPointer(ty', s0, s1, s2, s3, p, s4) ->
-                          Ast0.rewrap ty (
-                            Ast0.FunctionPointer(
-                              renamer ty', s0, s1, s2, s3, p, s4))
                       | Ast0.ParenType(s0, ty', s1) ->
                           Ast0.rewrap ty (Ast0.ParenType(s0, renamer ty', s1))
                       | Ast0.FunctionType(ty', s0, s1, s2) ->
parsing_cocci/pretty_print_cocci.ml +1 -13
index ef60106c..213994fc 100644
@@ -397,12 +397,6 @@ and fullType ft =
   | Ast.ConjType(decls) -> print_disj_list fullType decls "&"
   | Ast.OptType(ty) -> print_string "?"; fullType ty
 
-and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
-  fullType ty; print_string " ";
-  mcode print_string lp1; mcode print_string star; fn();
-  mcode print_string rp1; mcode print_string lp1;
-  parameter_list params; mcode print_string rp2
-
 and print_parentype (lp,ty,rp) fn =
   match Ast.unwrap ty with
    Ast.Type(_,_,fty1) ->
@@ -469,9 +463,6 @@ and typeC ty =
       List.iter (function s -> mcode print_string s; print_string " ") strings
   | Ast.SignedT(sgn,ty) -> mcode sign sgn; print_option typeC ty
   | Ast.Pointer(ty,star) -> fullType ty; mcode print_string star
-  | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-      print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
-	(function _ -> ())
   | Ast.ParenType(lp,ty,rp) ->
       print_parentype (lp,ty,rp) (function _ -> ())
   | Ast.FunctionType(ty,lp,params,rp) ->
@@ -532,10 +523,7 @@ and print_named_type ty id =
   match Ast.unwrap ty with
     Ast.Type(_,None,ty1) ->
       (match Ast.unwrap ty1 with
-	Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-	  print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
-	    (function _ -> id())
-      | Ast.Array(ty,lb,size,rb) ->
+        Ast.Array(ty,lb,size,rb) ->
 	  let rec loop ty k =
 	    match Ast.unwrap ty with
 	      Ast.Array(ty,lb,size,rb) ->
parsing_cocci/single_statement.ml +0 -1
index 48d77b5a..c1266980 100644
@@ -165,7 +165,6 @@ and left_typeC t =
   | Ast0.BaseType(ty,strings) -> modif_before_mcode (List.hd strings)
   | Ast0.Signed(sgn,ty) -> modif_before_mcode sgn
   | Ast0.Pointer(ty,star) -> left_typeC ty
-  | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) -> left_typeC ty
   | Ast0.ParenType(lp,ty,rp) -> left_typeC ty
   | Ast0.FunctionType(ty,lp,params,rp) -> left_typeC ty
   | Ast0.Array(ty,lb,size,rb) -> left_typeC ty
parsing_cocci/type_cocci.mli +0 -1
index 6f24adf7..5b0734d2 100644
@@ -15,7 +15,6 @@ type typeC =
   | BaseType        of baseType
   | SignedT         of sign * typeC option
   | Pointer         of typeC
-  | FunctionPointer of typeC (* only return type *)
   | ParenType       of typeC (* only return type *)
   | FunctionType    of typeC (* only return type *)
   | Array           of typeC (* drop size info *)
parsing_cocci/type_infer.ml +13 -12
index b4c0b55e..345bc129 100644
@@ -161,18 +161,19 @@ let rec propagate_types env =
               Ast0.rewrap e (
                 Ast0.Array(Ast0.rewrap e char_type, dummy, None, dummy)))
 	| Ast0.FunCall(fn,lp,args,rp) ->
-	    (match Common.map_option Ast0.unwrap (Ast0.get_type fn) with
-		 Some (Ast0.FunctionPointer(ty, _, _, _, _, _, _)) -> Some ty
-	       |  _ ->
-		    (match Ast0.unwrap fn with
-			 Ast0.Ident(id) ->
-			   (match Ast0.unwrap id with
-				Ast0.Id(id) ->
-				  if List.mem (Ast0.unwrap_mcode id) bool_functions
-				  then Some(Ast0.rewrap e bool_type)
-				  else None
-			      | _ -> None)
-		       |	_ -> None))
+            let fna = Common.map_option Ast0.unwrap (Ast0.get_type fn) in
+            (match fna with
+                 Some _ ->
+                 (match Ast0.unwrap fn with
+                    Ast0.Ident(id) ->
+                      (match Ast0.unwrap id with
+                         Ast0.Id(id) ->
+                           if List.mem (Ast0.unwrap_mcode id) bool_functions
+                           then Some(Ast0.rewrap e bool_type)
+                           else None
+                       | _ -> None)
+                  | _ -> None)
+            | _ -> None)
 	| Ast0.Assignment(exp1,op,exp2,_) ->
 	    let ty = lub_type (Ast0.get_type exp1) (Ast0.get_type exp2) in
 	      Ast0.set_type exp1 ty; Ast0.set_type exp2 ty; ty
parsing_cocci/unify_ast.ml +0 -8
index de86b1d3..47a4dfe6 100644
@@ -328,14 +328,6 @@ and unify_typeC t1 t2 =
       then unify_option unify_typeC ty1 ty2
       else false
   | (Ast.Pointer(ty1,s1),Ast.Pointer(ty2,s2)) -> unify_fullType ty1 ty2
-  | (Ast.FunctionPointer(tya,lp1a,stara,rp1a,lp2a,paramsa,rp2a),
-     Ast.FunctionPointer(tyb,lp1b,starb,rp1b,lp2b,paramsb,rp2b)) ->
-       if List.for_all2 unify_mcode
-	   [lp1a;stara;rp1a;lp2a;rp2a] [lp1b;starb;rp1b;lp2b;rp2b]
-       then
-	 unify_fullType tya tyb &&
-	 unify_dots unify_parameterTypeDef pdots paramsa paramsb
-       else false
   | (Ast.ParenType(lpa,tya,rpa),Ast.ParenType(lpb,tyb,rpb)) ->
       unify_fullType tya tyb && unify_mcode lpa lpb && unify_mcode rpa rpb
   | (Ast.FunctionType(tya,lpa,paramsa,rpa),
parsing_cocci/unparse_ast0.ml +1 -12
index f8684dd2..7e108232 100644
@@ -287,11 +287,6 @@ and string_format e =
 (* --------------------------------------------------------------------- *)
 (* Types *)
 
-and print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2) fn =
-  typeC ty; mcode print_string lp1; mcode print_string star; fn();
-  mcode print_string rp1; mcode print_string lp2;
-  parameter_list params; mcode print_string rp2
-
 and print_parentype (lp,ty,rp) fn =
  match Ast0.unwrap ty with
    Ast0.Pointer(ty1,star) ->
@@ -337,9 +332,6 @@ and typeC t =
 	    strings
       |	Ast0.Signed(sgn,ty) -> mcode U.sign sgn; print_option typeC ty
       | Ast0.Pointer(ty,star) -> typeC ty; mcode print_string star
-      | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-	  print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
-	    (function _ -> ())
       | Ast0.ParenType(lp,ty,rp) ->
           print_parentype (lp,ty,rp) (function _ -> ())
       | Ast0.FunctionType(ty,lp,params,rp) ->
@@ -393,10 +385,7 @@ and typeC t =
 
 and print_named_type ty id =
   match Ast0.unwrap ty with
-    Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-      print_function_pointer (ty,lp1,star,rp1,lp2,params,rp2)
-	(function _ -> print_string " "; ident id)
-  | Ast0.Array(ty,lb,size,rb) ->
+    Ast0.Array(ty,lb,size,rb) ->
       let rec loop ty k =
 	match Ast0.unwrap ty with
 	  Ast0.Array(ty,lb,size,rb) ->
parsing_cocci/visitor_ast.ml +0 -26
index 99af7652..a6bd59a3 100644
@@ -297,19 +297,6 @@ let combiner bind option_default
       | Ast.OptType(ty) -> fullType ty in
     ftfn all_functions k ft
 
-  and function_pointer
-	(ty, lp1, star, (id : Ast.ident option), rp1, lp2, params, rp2) =
-    (* have to put the treatment of the identifier into the right position *)
-    let lty = fullType ty in
-    let llp1 = string_mcode lp1 in
-    let lstar = string_mcode star in
-    let lid = match id with Some idd -> [ident idd] | None -> [] in
-    let lrp1 = string_mcode rp1 in
-    let llp2 = string_mcode lp2 in
-    let lparams = parameter_dots params in
-    let lrp2 = string_mcode rp2 in
-    multibind ([lty; llp1; lstar] @ lid @ [lrp1; llp2; lparams; lrp2])
-
   and array_type (ty,(id : Ast.ident option),lb,size,rb) =
     let lty = fullType ty in
     let lid = match id with Some idd -> [ident idd] | None -> [] in
@@ -385,8 +372,6 @@ let combiner bind option_default
 	  let lty = fullType ty in
 	  let lstar = string_mcode star in
 	  bind lty lstar
-      | Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-	  function_pointer (ty,lp1,star,None,rp1,lp2,params,rp2)
       | Ast.ParenType(lp,ty,rp) ->
           parentype_type (lp,ty,None,rp)
       | Ast.FunctionType(ty,lp,params,rp) ->
@@ -444,8 +429,6 @@ let combiner bind option_default
     match Ast.unwrap ty with
       Ast.Type(_,None,ty1) ->
 	(match Ast.unwrap ty1 with
-	  Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-	    function_pointer (ty, lp1, star, Some id, rp1, lp2, params, rp2)
 	| Ast.Array(ty,lb,size,rb) -> array_type (ty, Some id, lb, size, rb)
         | Ast.ParenType(lp,ty,rp) -> parentype_type (lp, ty, Some id, rp)
 	| _ -> let lty = fullType ty in
@@ -1308,15 +1291,6 @@ let rebuilder
 	    let lty = fullType ty in
 	    let lstar = string_mcode star in
 	    Ast.Pointer (lty, lstar)
-	| Ast.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
-	    let lty = fullType ty in
-	    let llp1 = string_mcode lp1 in
-	    let lstar = string_mcode star in
-	    let lrp1 = string_mcode rp1 in
-	    let llp2 = string_mcode lp2 in
-	    let lparams = parameter_dots params in
-	    let lrp2 = string_mcode rp2 in
-	    Ast.FunctionPointer(lty, llp1, lstar, lrp1, llp2, lparams, lrp2)
         | Ast.ParenType(lp,ty,rp) ->
             let llp = string_mcode lp in
             let lty = fullType ty in
parsing_cocci/visitor_ast0.ml +2 -49

81 lines not on this page · raw message

tools/spgen/source/meta_variable.ml +0 -1
index 7adfacad..6a3c2089 100644
@@ -273,7 +273,6 @@ let types ~rn = function
         | Ast.StructUnionName(_, Some n) -> get_meta_id acc n
         | Ast.SignedT (_, Some t) -> get_meta_type acc t
         | Ast.Pointer (t, _)
-        | Ast.FunctionPointer (t, _, _, _, _, _, _)
         | Ast.Array (t, _, _, _) -> get_meta_type_full acc t
 	| Ast.TypeOfExpr(_,_,exp,_) -> acc (* not sure *)
 	| Ast.TypeOfType(_,_,ty,_) -> get_meta_type_full acc ty
tools/spgen/source/position_generator.ml +0 -3
index 2fa630fb..602e768e 100644
@@ -192,9 +192,6 @@ let rec type_pos t snp
   | Ast0.Pointer(t,star) ->
       let constructor ~mc = Ast0.Pointer(t,mc) in
       mcode_wrap ~mc:star ~constructor snp
-  | Ast0.FunctionPointer(t,lp,star,rp,lp2,params,rp2) ->
-      let constructor ~mc = Ast0.FunctionPointer(t,lp,star,rp,lp2,params,mc) in
-      mcode_wrap ~mc:rp2 ~constructor snp
   | Ast0.ParenType(lp,t,rp) ->
       let constructor ~mc = Ast0.ParenType(lp,t,rp) in
       mcode_wrap ~mc:rp ~constructor snp
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#48 lore raw

PATCH 26/26 tests: Add test case for array of function pointers

In reply to Jaskaran Singh

Add a test case to match against an array of function pointers. This would previously not work due to differences in the C and SmPL ASTs.

---
 tests/funptr_array.c     | 1 +
 tests/funptr_array.cocci | 9 +++++++++
 tests/funptr_array.res   | 1 +
 3 files changed, 11 insertions(+)
 create mode 100644 tests/funptr_array.c
 create mode 100644 tests/funptr_array.cocci
 create mode 100644 tests/funptr_array.res
tests/funptr_array.c new file +1 -0
new file mode 100644
index 00000000..dac29ac6
@@ -0,0 +1 @@
+int (*x[2])(int x);
tests/funptr_array.cocci new file +9 -0
new file mode 100644
index 00000000..8027bcf4
@@ -0,0 +1,9 @@
+@@
+type T;
+identifier x;
+@@
+
+T (*x[2])(
+- int
++ char
+  x);
tests/funptr_array.res new file +1 -0
new file mode 100644
index 00000000..72e1a14a
@@ -0,0 +1 @@
+int (*x[2])(char x);
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#49 lore raw

Re: [Cocci] [PATCH 26/26] tests: Add test case for array of function pointers

Quoted 4 lines · Jaskaran Singh

Add a test case to match against an array of function pointers. This would previously not work due to differences in the C and SmPL ASTs.

Thanks for all of the changes in this series!

julia

Quoted 40 lines
---
 tests/funptr_array.c     | 1 +
 tests/funptr_array.cocci | 9 +++++++++
 tests/funptr_array.res   | 1 +
 3 files changed, 11 insertions(+)
 create mode 100644 tests/funptr_array.c
 create mode 100644 tests/funptr_array.cocci
 create mode 100644 tests/funptr_array.res
tests/funptr_array.c new file +1 -0
new file mode 100644
index 00000000..dac29ac6
@@ -0,0 +1 @@
+int (*x[2])(int x);
tests/funptr_array.cocci new file +9 -0
new file mode 100644
index 00000000..8027bcf4
@@ -0,0 +1,9 @@
+@@
+type T;
+identifier x;
+@@
+
+T (*x[2])(
+- int
++ char
+  x);
tests/funptr_array.res new file +1 -1
new file mode 100644
index 00000000..72e1a14a
@@ -0,0 +1 @@
+int (*x[2])(char x);
--
Quoted 3 lines

2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees