[Cocci] [PATCH] parsing_c: handle space and ctx newline around minus toks
Handle the following case: space + minus + context newline In this case, the space should be dropped except if it is followed by any newline. This should be done just before the minus tokens are removed from the token stream. This is useful for situations like the following. Here, the space next to the right paranthesis should be dropped: if(a) -{ -result = b; goto c; -} Signed-off-by: Jaskaran Singh --- parsing_c/unparse_c.ml | 33 - 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/parsing_c/unparse_c.ml b/parsing_c/unparse_c.ml index 778a59ab..c6478c5a 100644 --- a/parsing_c/unparse_c.ml +++ b/parsing_c/unparse_c.ml @@ -1300,21 +1300,28 @@ let rec drop_space_at_endline = function drop_space_at_endline rest | ((T2(Parser_c.TCommentSpace _,Ctx,_i,_h)) as a)::rest -> let (outer_spaces,rest) = span is_space rest in -let minus_or_comment_or_space_nocpp = function - | (T2(Parser_c.TCommentNewline _,_,_i,_)) -> false - | T2(_,Min adj,_,_) -> true - | (T2(Parser_c.TCommentSpace _,Ctx,_i,_)) -> true +let match_till_context_nl = function + | (T2(Parser_c.TCommentNewline _,_,_i,_) :: _) -> false + | (T2(_,Min adj,_,_) :: _) -> true | x -> false in -let (minus,rest) = span minus_or_comment_or_space_nocpp rest in -let fail _ = a :: outer_spaces @ minus @ (drop_space_at_endline rest) in -if List.exists is_minus minus +if match_till_context_nl rest then - match rest with - | ((T2(Parser_c.TCommentNewline _,Ctx,_i,_h)) as a)::rest -> -(* drop trailing spaces *) -minus @ a :: (drop_space_at_endline rest) - | _ -> fail () -else fail () + let minus_or_comment_or_space_nocpp = function +| (T2(Parser_c.TCommentNewline _,Ctx,_i,_)) -> false +| T2(_,Min adj,_,_) -> true +| (T2(Parser_c.TCommentSpace _,Ctx,_i,_)) -> true +| x -> false in + let (minus,rest) = span minus_or_comment_or_space_nocpp rest in + let fail _ = a :: outer_spaces @ minus @ (drop_space_at_endline rest) in + if List.exists is_minus minus + then +match rest with +| ((T2(Parser_c.TCommentNewline _,Ctx,_i,_h)) as a)::rest -> + (* drop trailing spaces *) + minus @ a :: (drop_space_at_endline rest) +| _ -> fail () + else fail () +else a :: outer_spaces @ (drop_space_at_endline rest) | a :: rest -> a :: drop_space_at_endline rest -- 2.21.0 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 2/2] tests: Add test case for statement removed and added
This is in response to the following "continue statement of death" report: https://www.mail-archive.com/cocci@systeme.lip6.fr/msg06199.html Coccinelle would crash in similar cases, so add a test case for it. Signed-off-by: Jaskaran Singh --- tests/stmt_removed_and_added.c | 8 tests/stmt_removed_and_added.cocci | 7 +++ tests/stmt_removed_and_added.res | 8 3 files changed, 23 insertions(+) create mode 100644 tests/stmt_removed_and_added.c create mode 100644 tests/stmt_removed_and_added.cocci create mode 100644 tests/stmt_removed_and_added.res diff --git a/tests/stmt_removed_and_added.c b/tests/stmt_removed_and_added.c new file mode 100644 index ..cbc64f42 --- /dev/null +++ b/tests/stmt_removed_and_added.c @@ -0,0 +1,8 @@ +void main() +{ + for(i; j; k) { + if (1) + continue; + c++; + } +} diff --git a/tests/stmt_removed_and_added.cocci b/tests/stmt_removed_and_added.cocci new file mode 100644 index ..93558a19 --- /dev/null +++ b/tests/stmt_removed_and_added.cocci @@ -0,0 +1,7 @@ +@@ +expression I, J, K; +statement S; +@@ + +- for (I; J; K) S ++ while(1) S diff --git a/tests/stmt_removed_and_added.res b/tests/stmt_removed_and_added.res new file mode 100644 index ..4b20027a --- /dev/null +++ b/tests/stmt_removed_and_added.res @@ -0,0 +1,8 @@ +void main() +{ + while (1) { + if (1) + continue; + c++; + } +} -- 2.21.0 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 1/2] engine: remove circular references in MINUS/NOREPLACEMENT case
The cocci info tag can sometimes have circular references in the MINUS/NOREPLACEMENT case in the transformation. Pass the binding info through clean_env to remove circular references. Signed-off-by: Jaskaran Singh --- engine/transformation_c.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/transformation_c.ml b/engine/transformation_c.ml index bcde08b8..9f0b0ab2 100644 --- a/engine/transformation_c.ml +++ b/engine/transformation_c.ml @@ -380,7 +380,7 @@ module XTRANS = struct (Ast_cocci.MINUS (old_pos,Common.union_set old_inst new_inst,old_adj, Ast_cocci.NOREPLACEMENT), - [tin.binding]); + [clean_env tin.binding]); (if !Flag_matcher.show_misc then pr2_once "already tagged but only removed, so safe") -- 2.21.0 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 0/2] cocci: Fix continue statement of death and similar cases
The following patch series is for fixing the "continue statement of death" report here: https://www.mail-archive.com/cocci@systeme.lip6.fr/msg06199.html Changes include removing circular references that cause the crash and adding a corresponding test case. engine/transformation_c.ml |2 +- tests/stmt_removed_and_added.c |8 tests/stmt_removed_and_added.cocci |7 +++ tests/stmt_removed_and_added.res |8 4 files changed, 24 insertions(+), 1 deletion(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v3 3/3] tests: Add test case to match const pointer variants
Pointer to const pointer and its variants would not match previously. Add a test case for matching these types. Signed-off-by: Jaskaran Singh --- tests/constptr.c | 7 +++ tests/constptr.cocci | 19 +++ tests/constptr.res | 7 +++ 3 files changed, 33 insertions(+) create mode 100644 tests/constptr.c create mode 100644 tests/constptr.cocci create mode 100644 tests/constptr.res diff --git a/tests/constptr.c b/tests/constptr.c new file mode 100644 index ..13fe064c --- /dev/null +++ b/tests/constptr.c @@ -0,0 +1,7 @@ +int main() +{ + const char * const * id; + const char * * const * id; + const char * const * * id; + const char * const id; +} diff --git a/tests/constptr.cocci b/tests/constptr.cocci new file mode 100644 index ..29f0aa96 --- /dev/null +++ b/tests/constptr.cocci @@ -0,0 +1,19 @@ +@ r0 @ +identifier id; +@@ +const char * const * +- id ++ id1 +; +const char * * const * +- id ++ id2 +; +const char * const * * +- id ++ id3 +; +const char * const +- id ++ id4 +; diff --git a/tests/constptr.res b/tests/constptr.res new file mode 100644 index ..0af4de9a --- /dev/null +++ b/tests/constptr.res @@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1; + const char * * const * id2; + const char * const * * id3; + const char * const id4; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v3 1/3] parsing_c: Align C AST and Cocci AST for pointer
For a pointer, the C parser constructed an AST dissimilar from that of the Cocci AST. This caused failures in matching with certain pointer types. For example, for the following case: char *1 const *2 id; The C AST constructed would be: const Pointer1 -> Pointer2 -> char The Cocci AST constructed would be: Pointer2 -> const Pointer1 -> char Change the pointer rule in the C parser so that an AST similar to the Cocci AST is constructed. Make necessary changes in the C pretty printer so that the pointer type is printed correctly. Signed-off-by: Jaskaran Singh --- parsing_c/parser_c.mly | 4 ++-- parsing_c/pretty_print_c.ml | 12 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly index 8d7b761e..4c74f15a 100644 --- a/parsing_c/parser_c.mly +++ b/parsing_c/parser_c.mly @@ -1333,14 +1333,14 @@ pointer: | tmul { (Ast_c.noattr,fun x -> mk_ty (Pointer x) [$1]) } | tmul pointer { let (attr,ptr) = $2 in - (attr,fun x -> mk_ty (Pointer (ptr x)) [$1]) } + (attr,fun x -> ptr (mk_ty (Pointer x) [$1])) } | tmul type_qualif_list { let (attr,tq) = $2 in (attr,fun x -> (tq.qualifD, mk_tybis (Pointer x) [$1]))} | tmul type_qualif_list pointer { let (attr1,tq) = $2 in let (attr2,ptr) = $3 in - (attr1@attr2,fun x -> (tq.qualifD, mk_tybis (Pointer (ptr x)) [$1])) } + (attr1@attr2,fun x -> ptr (tq.qualifD, mk_tybis (Pointer x) [$1])) } tmul: TMul { $1 } diff --git a/parsing_c/pretty_print_c.ml b/parsing_c/pretty_print_c.ml index a2e35588..ae02b513 100644 --- a/parsing_c/pretty_print_c.ml +++ b/parsing_c/pretty_print_c.ml @@ -804,11 +804,13 @@ and pp_string_format (e,ii) = (FunctionType (return=void, params=int i) *) (*WRONG I THINK, use left & right function *) (* bug: pp_type_with_ident_rest None t; print_ident ident *) + pp_type_left t; pr_elem i; - iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + iiqu +> List.iter (function x -> + (pr_space(); pr_elem x));(* le const est forcement apres le '*' *) if iiqu <> [] || get_comments_after i <> [] then pr_space(); - pp_type_with_ident_rest ident t attrs Ast_c.noattr; + print_ident ident (* ugly special case ... todo? maybe sufficient in practice *) | (ParenType ttop, [i1;i2]) -> @@ -885,11 +887,13 @@ and pp_string_format (e,ii) = match ty, iity with (NoType,_) -> failwith "pp_type_left: unexpected NoType" | (Pointer t, [i]) -> + pp_type_left t; pr_elem i; - iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + iiqu +> List.iter (function x -> + (pr_space(); pr_elem x));(* le const est forcement apres le '*' *) if iiqu <> [] || get_comments_after i <> [] then pr_space(); - pp_type_left t + () | (Array (eopt, t), [i1;i2]) -> pp_type_left t | (FunctionType (returnt, paramst), [i1;i2]) -> pp_type_left returnt -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v3 2/3] tests: Add space between * and const in ptrconstptr.res
A space is now added between the * and qualifier. Reflect this change in the ptrconstptr test case. Signed-off-by: Jaskaran Singh --- tests/ptrconstptr.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ptrconstptr.res b/tests/ptrconstptr.res index f5b2210c..1d0c3c1d 100644 --- a/tests/ptrconstptr.res +++ b/tests/ptrconstptr.res @@ -1,3 +1,3 @@ void main() { - const char *const *y; + const char * const *y; } -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v3 0/3] cocci: Align the C AST and Cocci AST for pointer
This series is to address the type matching problem in Coccinelle. Patch 1/3 is for aligning the C and Cocci AST so that pointer to const pointer and its variants can be matched. Patch 2/3 reflects the pretty printing changes made in 1/3 in the ptrconstptr test case. A space is added between the * and const. Patch 3/3 consists of a test case for matching a pointer to const pointer and its variants. Changes in v3: -- - Add a space between the * and qualifier in Patch 1/3. - Add Patch 2/3 to add a space between * and const in the test case ptrconstptr. Changes in v2: -- - Change body and subject of Patch 2/2 as per suggestion of Markus Elfring. [PATCH v3 1/3] parsing_c: Align C AST and Cocci AST for pointer [PATCH v3 2/3] tests: Add space between * and const in [PATCH v3 3/3] tests: Add test case to match const pointer variants parsing_c/parser_c.mly |4 ++-- parsing_c/pretty_print_c.ml | 12 tests/constptr.c|7 +++ tests/constptr.cocci| 19 +++ tests/constptr.res |7 +++ tests/ptrconstptr.res |2 +- 6 files changed, 44 insertions(+), 7 deletions(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH] parsing_hacks: Add bool to list of known typedefs
On Tue, 2020-01-21 at 06:20 +, Julia Lawall wrote: > > On Sun, 19 Jan 2020, Jaskaran Singh wrote: > > > bool is widely used in the Linux kernel. Certain cases of > > parsing_hacks.ml would mislabel bool. > > > > Add bool to the list of known typedefs. > > > > Stats of --parse-c on Linux v5.5-rc4 are as follows: > > > > Before: > > > > nb good = 18956150, nb passed = 134062 => 0.70% passed > > > > After: > > > > nb good = 18956150, nb passed = 134073 => 0.70% passed > > Why did nb passed increase? These are tokens that will not be able > to be > transformed by a semantic patch. > I suspect it's instances of nokprobe_inline in linux/kernel/trace/trace_kprobe.c. They're marked as CppMacro. Here's results from my diff: 20402,20406c20288,20294 < passed:bool < passed:bool < passed:bool < passed:bool < passed:bool --- > passed:nokprobe_inline > passed:nokprobe_inline > passed:nokprobe_inline > passed:nokprobe_inline > passed:nokprobe_inline > passed:nokprobe_inline > passed:nokprobe_inline 20407a20296,20297 > passed:nokprobe_inline > passed:nokprobe_inline 20408a20299,20300 > passed:nokprobe_inline > passed:nokprobe_inline 20409a20302,20303 > passed:nokprobe_inline > passed:nokprobe_inline 20410a20305,20306 > passed:nokprobe_inline > passed:nokprobe_inline Cheers, Jaskaran. > julia > > > Signed-off-by: Jaskaran Singh > > --- > > The above diff has been obtained after applying > > https://www.mail-archive.com/cocci@systeme.lip6.fr/msg06706.html > > > > parsing_c/parsing_hacks.ml | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/parsing_c/parsing_hacks.ml > > b/parsing_c/parsing_hacks.ml > > index 685a4908..5e3301a0 100644 > > --- a/parsing_c/parsing_hacks.ml > > +++ b/parsing_c/parsing_hacks.ml > > @@ -61,7 +61,7 @@ let is_known_typdef = > >| "u_char" | "u_short" | "u_int" | "u_long" > >| "u8" | "u16" | "u32" | "u64" > >| "s8" | "s16" | "s32" | "s64" > > - | "__u8" | "__u16" | "__u32" | "__u64" > > + | "__u8" | "__u16" | "__u32" | "__u64" | "bool" > > -> true > > > >| "acpi_handle" > > -- > > 2.21.1 > > > > ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH] parsing_hacks: Add bool to list of known typedefs
On Tue, 2020-01-21 at 06:23 +, Julia Lawall wrote: > > On Sun, 19 Jan 2020, Jaskaran Singh wrote: > > > bool is widely used in the Linux kernel. Certain cases of > > parsing_hacks.ml would mislabel bool. > > > > Add bool to the list of known typedefs. > > As far as I can see, this function is only used for avoiding to print > some > warning messages. So I'm not sure why it would have any impact on > the > parsing at all? > Sorry, It should've been clear in the patch. This patch will only make any difference if it's applied on top of https://www.mail-archive.com/cocci@systeme.lip6.fr/msg06706.html Cheers, Jaskaran. > julia > > > Stats of --parse-c on Linux v5.5-rc4 are as follows: > > > > Before: > > > > nb good = 18956150, nb passed = 134062 => 0.70% passed > > > > After: > > > > nb good = 18956150, nb passed = 134073 => 0.70% passed > > > > Signed-off-by: Jaskaran Singh > > --- > > The above diff has been obtained after applying > > https://www.mail-archive.com/cocci@systeme.lip6.fr/msg06706.html > > > > parsing_c/parsing_hacks.ml | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/parsing_c/parsing_hacks.ml > > b/parsing_c/parsing_hacks.ml > > index 685a4908..5e3301a0 100644 > > --- a/parsing_c/parsing_hacks.ml > > +++ b/parsing_c/parsing_hacks.ml > > @@ -61,7 +61,7 @@ let is_known_typdef = > >| "u_char" | "u_short" | "u_int" | "u_long" > >| "u8" | "u16" | "u32" | "u64" > >| "s8" | "s16" | "s32" | "s64" > > - | "__u8" | "__u16" | "__u32" | "__u64" > > + | "__u8" | "__u16" | "__u32" | "__u64" | "bool" > > -> true > > > >| "acpi_handle" > > -- > > 2.21.1 > > > > ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v2 2/3] parsing_hacks: Add bool to list of known typedefs
bool is widely used in the Linux kernel. Certain cases of parsing_hacks.ml would mislabel bool. Add bool to the list of known typedefs. Stats of --parse-c on Linux v5.5-rc4 are as follows: Before: nb good = 18956150, nb passed = 134062 => 0.70% passed After: nb good = 18956150, nb passed = 134073 => 0.70% passed The increase in passed tokens is due to functions in kernel/trace/trace_kprobe.c using nokprobe_inline. For instances of nokprobe_inline bool, nokprobe_inline is labeled as a CppMacro. Examples of this in the --parse-c diff are as follows: Before: passed:bool passed:bool passed:bool passed:bool passed:bool After: passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline Signed-off-by: Jaskaran Singh --- parsing_c/parsing_hacks.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml index 23d675cf..42ad9ccc 100644 --- a/parsing_c/parsing_hacks.ml +++ b/parsing_c/parsing_hacks.ml @@ -61,7 +61,7 @@ let is_known_typdef = | "u_char" | "u_short" | "u_int" | "u_long" | "u8" | "u16" | "u32" | "u64" | "s8" | "s16" | "s32" | "s64" - | "__u8" | "__u16" | "__u32" | "__u64" + | "__u8" | "__u16" | "__u32" | "__u64" | "bool" -> true | "acpi_handle" -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v2 1/3] parsing_c: Handle case of macro before typedef
For the following case: A case in parsing_hacks.ml sometimes mislabels as a typedef ident. If typedef is a known typedef (such as u8 or *_t), then label as a CppMacro. Subsequent cases will then label correctly. Following are results of --parse-c on the Linux Kernel v5.5-rc4: Before: nb good = 18956150, nb passed = 134061 => 0.70% passed After: nb good = 18956150, nb passed = 134062 => 0.70% passed Signed-off-by: Jaskaran Singh --- parsing_c/parsing_hacks.ml | 35 +++ 1 file changed, 35 insertions(+) diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml index 8374731b..23d675cf 100644 --- a/parsing_c/parsing_hacks.ml +++ b/parsing_c/parsing_hacks.ml @@ -2184,6 +2184,41 @@ let lookahead2 ~pass next before = && ok_typedef s && is_macro s2 && is_type type_ -> TIdent (s, i1) + + (* xx yy zz : xx is a macro *) + | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _) +when not_struct_enum before + && ok_typedef s2 + && is_known_typdef s2 +-> + TCommentCpp(Token_c.CppMacro, i1) + + (* xx yy zz : xx is a typedef ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _) +when not_struct_enum before + && ok_typedef s +-> + msg_typedef s i1 2; LP.add_typedef_root s i1; + TypedefIdent (s, i1) + + (* xx yy * zz : xx is a macro *) + | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _) +when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr + && not_struct_enum before + && ok_typedef s2 + && is_known_typdef s2 +-> + TCommentCpp(Token_c.CppMacro, i1) + + (* xx yy * zz : xx is a typedef ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _) +when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr + && not_struct_enum before + && ok_typedef s +-> + msg_typedef s i1 2; LP.add_typedef_root s i1; + TypedefIdent (s, i1) + (* xx yy *) | (TIdent (s, i1)::TIdent (s2, i2)::rest , _) when not_struct_enum before && ok_typedef s && not (is_macro_paren s2 rest) -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v2 3/3] tests: Add test case for bool
This would previously yield pretty printing errors, i.e. bool would be printed on the next line, followed by ret on the next to next line. The metatype should be only bool and not bool. Signed-off-by: Jaskaran Singh --- tests/macro_before_bool.c | 4 tests/macro_before_bool.cocci | 9 + tests/macro_before_bool.res | 5 + 3 files changed, 18 insertions(+) create mode 100644 tests/macro_before_bool.c create mode 100644 tests/macro_before_bool.cocci create mode 100644 tests/macro_before_bool.res diff --git a/tests/macro_before_bool.c b/tests/macro_before_bool.c new file mode 100644 index ..a59cba5a --- /dev/null +++ b/tests/macro_before_bool.c @@ -0,0 +1,4 @@ +static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk) +{ + return false; +} diff --git a/tests/macro_before_bool.cocci b/tests/macro_before_bool.cocci new file mode 100644 index ..53b2fd2e --- /dev/null +++ b/tests/macro_before_bool.cocci @@ -0,0 +1,9 @@ +@@ +type t; +identifier x; +@@ + +t x(...) { ++ t ret; + return false; +} diff --git a/tests/macro_before_bool.res b/tests/macro_before_bool.res new file mode 100644 index ..1b0ec319 --- /dev/null +++ b/tests/macro_before_bool.res @@ -0,0 +1,5 @@ +static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk) +{ + bool ret; + return false; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v2 0/3] cocci: Improve management of macros before typedefs
This patch series is for improving the management of the following case: If is a known typedef (such as u8 or *_t) then label as a CppMacro. Subsequent cases will label correctly as a typedef ident. Also add bool to the list of known typedefs so that cases of bool are handled correctly just like any known typedef will be. Changes in v2: -- - Group these patches as a series for clarity - Add a test case for bool (Patch 3/3) - Explain what has been improved with Patch 2/3 parsing_c/parsing_hacks.ml| 37 - tests/macro_before_bool.c |4 tests/macro_before_bool.cocci |9 + tests/macro_before_bool.res |5 + 4 files changed, 54 insertions(+), 1 deletion(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH v2 2/3] parsing_hacks: Add bool to list of known typedefs
On Tue, 2020-01-21 at 11:10 +0100, Markus Elfring wrote: > > Before: > > passed:bool > > I wonder if subsequent duplicate information can really help to > achieve a better understanding > by the suggested description of a software situation. > Hm, I thought it would but you're probably right. Should I just remove the second Before/After? The text above it about trace_kprobe.c explains things anyway. Cheers, Jaskaran. > Regards, > Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v3 0/3] cocci: Improve management of macros before typedefs
This patch series is for improving the management of the following case: If is a known typedef (such as u8 or *_t) then label as a CppMacro. Subsequent cases will continue to label correctly as a typedef identifier. Also add bool to the list of known typedefs so that cases of bool are handled correctly just like any known typedef will be. Changes in v3: -- - Change "subsequent cases will label" to "subsequent cases will continue to label" in commit messages of Patch 0/3 and Patch 1/3. - Change the term "typedef ident" to "typedef identifier" wherever suitable. - Add metavariable symbol false to tests/macro_before_bool.cocci to suppress warning. Changes in v2: -- - Group these patches as a series for clarity - Add a test case for bool (Patch 3/3) - Explain what has been improved with Patch 2/3 parsing_c/parsing_hacks.ml| 37 - tests/macro_before_bool.c |4 tests/macro_before_bool.cocci | 10 ++ tests/macro_before_bool.res |5 + 4 files changed, 55 insertions(+), 1 deletion(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v3 3/3] tests: Add test case for bool
This would previously yield pretty printing errors, i.e. bool would be printed on the next line, followed by ret on the next to next line. The metatype should be only bool and not bool. Signed-off-by: Jaskaran Singh --- tests/macro_before_bool.c | 4 tests/macro_before_bool.cocci | 10 ++ tests/macro_before_bool.res | 5 + 3 files changed, 19 insertions(+) create mode 100644 tests/macro_before_bool.c create mode 100644 tests/macro_before_bool.cocci create mode 100644 tests/macro_before_bool.res diff --git a/tests/macro_before_bool.c b/tests/macro_before_bool.c new file mode 100644 index ..a59cba5a --- /dev/null +++ b/tests/macro_before_bool.c @@ -0,0 +1,4 @@ +static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk) +{ + return false; +} diff --git a/tests/macro_before_bool.cocci b/tests/macro_before_bool.cocci new file mode 100644 index ..667e4662 --- /dev/null +++ b/tests/macro_before_bool.cocci @@ -0,0 +1,10 @@ +@@ +type t; +identifier x; +symbol false; +@@ + +t x(...) { ++ t ret; + return false; +} diff --git a/tests/macro_before_bool.res b/tests/macro_before_bool.res new file mode 100644 index ..1b0ec319 --- /dev/null +++ b/tests/macro_before_bool.res @@ -0,0 +1,5 @@ +static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk) +{ + bool ret; + return false; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v3 2/3] parsing_hacks: Add bool to list of known typedefs
bool is widely used in the Linux kernel. Certain cases of parsing_hacks.ml would mislabel bool. Add bool to the list of known typedefs. Stats of --parse-c on Linux v5.5-rc4 are as follows: Before: nb good = 18956150, nb passed = 134062 => 0.70% passed After: nb good = 18956150, nb passed = 134073 => 0.70% passed The increase in passed tokens is due to functions in kernel/trace/trace_kprobe.c using nokprobe_inline. For instances of nokprobe_inline bool, nokprobe_inline is labeled as a CppMacro. Examples of this in the --parse-c diff are as follows: Before: passed:bool passed:bool passed:bool passed:bool passed:bool After: passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline passed:nokprobe_inline Signed-off-by: Jaskaran Singh --- parsing_c/parsing_hacks.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml index 23d675cf..42ad9ccc 100644 --- a/parsing_c/parsing_hacks.ml +++ b/parsing_c/parsing_hacks.ml @@ -61,7 +61,7 @@ let is_known_typdef = | "u_char" | "u_short" | "u_int" | "u_long" | "u8" | "u16" | "u32" | "u64" | "s8" | "s16" | "s32" | "s64" - | "__u8" | "__u16" | "__u32" | "__u64" + | "__u8" | "__u16" | "__u32" | "__u64" | "bool" -> true | "acpi_handle" -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v3 1/3] parsing_c: Handle case of macro before typedef
For the following case: A case in parsing_hacks.ml sometimes mislabels as a typedef identifier. If typedef is a known typedef (such as u8 or *_t), then label as a CppMacro. Subsequent cases will continue to label correctly as a typedef identifier. Following are results of --parse-c on the Linux Kernel v5.5-rc4: Before: nb good = 18956150, nb passed = 134061 => 0.70% passed After: nb good = 18956150, nb passed = 134062 => 0.70% passed Signed-off-by: Jaskaran Singh --- parsing_c/parsing_hacks.ml | 35 +++ 1 file changed, 35 insertions(+) diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml index 8374731b..23d675cf 100644 --- a/parsing_c/parsing_hacks.ml +++ b/parsing_c/parsing_hacks.ml @@ -2184,6 +2184,41 @@ let lookahead2 ~pass next before = && ok_typedef s && is_macro s2 && is_type type_ -> TIdent (s, i1) + + (* xx yy zz : xx is a macro *) + | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _) +when not_struct_enum before + && ok_typedef s2 + && is_known_typdef s2 +-> + TCommentCpp(Token_c.CppMacro, i1) + + (* xx yy zz : xx is a typedef ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _) +when not_struct_enum before + && ok_typedef s +-> + msg_typedef s i1 2; LP.add_typedef_root s i1; + TypedefIdent (s, i1) + + (* xx yy * zz : xx is a macro *) + | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _) +when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr + && not_struct_enum before + && ok_typedef s2 + && is_known_typdef s2 +-> + TCommentCpp(Token_c.CppMacro, i1) + + (* xx yy * zz : xx is a typedef ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _) +when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr + && not_struct_enum before + && ok_typedef s +-> + msg_typedef s i1 2; LP.add_typedef_root s i1; + TypedefIdent (s, i1) + (* xx yy *) | (TIdent (s, i1)::TIdent (s2, i2)::rest , _) when not_struct_enum before && ok_typedef s && not (is_macro_paren s2 rest) -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH 2/2] tests: Add test case for matching pointers to const pointers (and variants)
On Wed, 2020-02-05 at 19:55 +0100, Markus Elfring wrote: > Would you like to avoid any abbreviations in the patch subject? > I used abbreviations to keep the patch subject short but still clear. Would the following be a better subject? "tests: Add test case to match const pointer variants" Cheers, Jaskaran. > > > These types would not match previously. > > I imagine that this information can be improved for a nicer patch > description, > can't it? > > Regards, > Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH 1/2] parsing_c: Align C AST and Cocci AST for pointers
On Wed, 2020-02-05 at 19:36 +0100, Markus Elfring wrote: > … > > +++ b/parsing_c/pretty_print_c.ml > > @@ -804,11 +804,12 @@ and pp_string_format (e,ii) = > > (FunctionType (return=void, params=int i) *) > >(*WRONG I THINK, use left & right function *) > >(* bug: pp_type_with_ident_rest None t; print_ident > > ident *) > > + pp_type_left t; > >pr_elem i; > >iiqu +> List.iter pr_elem; (* le const est forcement > > apres le '*' *) > >if iiqu <> [] || get_comments_after i <> [] > >then pr_space(); > > - pp_type_with_ident_rest ident t attrs Ast_c.noattr; > > + print_ident ident > > > >(* ugly special case ... todo? maybe sufficient in practice > > *) > >| (ParenType ttop, [i1;i2]) -> > > Are the source code comments still appropriate for this patch hunk? > I know the "le const est forcement" comment is still appropriate. The bug comment above probably still is. Not so sure about the WRONG I THINK comment. I'm using pp_type_left here, but not pp_type_right. Of course, that is if the comment is in that context. Cheers, Jaskaran. > Regards, > Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v2 2/2] tests: Add test case to match const pointer variants
Pointer to const pointer and its variants would not match previously. Add a test case for matching these types. Signed-off-by: Jaskaran Singh --- tests/constptr.c | 7 +++ tests/constptr.cocci | 19 +++ tests/constptr.res | 7 +++ 3 files changed, 33 insertions(+) create mode 100644 tests/constptr.c create mode 100644 tests/constptr.cocci create mode 100644 tests/constptr.res diff --git a/tests/constptr.c b/tests/constptr.c new file mode 100644 index ..13fe064c --- /dev/null +++ b/tests/constptr.c @@ -0,0 +1,7 @@ +int main() +{ + const char * const * id; + const char * * const * id; + const char * const * * id; + const char * const id; +} diff --git a/tests/constptr.cocci b/tests/constptr.cocci new file mode 100644 index ..29f0aa96 --- /dev/null +++ b/tests/constptr.cocci @@ -0,0 +1,19 @@ +@ r0 @ +identifier id; +@@ +const char * const * +- id ++ id1 +; +const char * * const * +- id ++ id2 +; +const char * const * * +- id ++ id3 +; +const char * const +- id ++ id4 +; diff --git a/tests/constptr.res b/tests/constptr.res new file mode 100644 index ..0af4de9a --- /dev/null +++ b/tests/constptr.res @@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1; + const char * * const * id2; + const char * const * * id3; + const char * const id4; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v2 0/2] cocci: Align the C AST and Cocci AST for pointer
This series is to address the type matching problem in Coccinelle. Patch 1/2 is for aligning the C and Cocci AST so that pointer to const pointer and its variants can be matched. Patch 2/2 consists of a test case for matching a pointer to const pointer and its variants. Changes in v2: -- - Change body and subject of Patch 2/2 as per suggestion of Markus Elfring. parsing_c/parser_c.mly |4 ++-- parsing_c/pretty_print_c.ml |6 -- tests/constptr.c|7 +++ tests/constptr.cocci| 19 +++ tests/constptr.res |7 +++ 5 files changed, 39 insertions(+), 4 deletions(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v2 1/2] parsing_c: Align C AST and Cocci AST for pointer
For a pointer, the C parser constructed an AST dissimilar from that of the Cocci AST. This caused failures in matching with certain pointer types. For example, for the following case: char *1 const *2 id; The C AST constructed would be: const Pointer1 -> Pointer2 -> char The Cocci AST constructed would be: Pointer2 -> const Pointer1 -> char Change the pointer rule in the C parser so that an AST similar to the Cocci AST is constructed. Make necessary changes in the C pretty printer so that the pointer type is printed correctly. Signed-off-by: Jaskaran Singh --- parsing_c/parser_c.mly | 4 ++-- parsing_c/pretty_print_c.ml | 6 -- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly index 8d7b761e..4c74f15a 100644 --- a/parsing_c/parser_c.mly +++ b/parsing_c/parser_c.mly @@ -1333,14 +1333,14 @@ pointer: | tmul { (Ast_c.noattr,fun x -> mk_ty (Pointer x) [$1]) } | tmul pointer { let (attr,ptr) = $2 in - (attr,fun x -> mk_ty (Pointer (ptr x)) [$1]) } + (attr,fun x -> ptr (mk_ty (Pointer x) [$1])) } | tmul type_qualif_list { let (attr,tq) = $2 in (attr,fun x -> (tq.qualifD, mk_tybis (Pointer x) [$1]))} | tmul type_qualif_list pointer { let (attr1,tq) = $2 in let (attr2,ptr) = $3 in - (attr1@attr2,fun x -> (tq.qualifD, mk_tybis (Pointer (ptr x)) [$1])) } + (attr1@attr2,fun x -> ptr (tq.qualifD, mk_tybis (Pointer x) [$1])) } tmul: TMul { $1 } diff --git a/parsing_c/pretty_print_c.ml b/parsing_c/pretty_print_c.ml index a2e35588..40b89fe5 100644 --- a/parsing_c/pretty_print_c.ml +++ b/parsing_c/pretty_print_c.ml @@ -804,11 +804,12 @@ and pp_string_format (e,ii) = (FunctionType (return=void, params=int i) *) (*WRONG I THINK, use left & right function *) (* bug: pp_type_with_ident_rest None t; print_ident ident *) + pp_type_left t; pr_elem i; iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) if iiqu <> [] || get_comments_after i <> [] then pr_space(); - pp_type_with_ident_rest ident t attrs Ast_c.noattr; + print_ident ident (* ugly special case ... todo? maybe sufficient in practice *) | (ParenType ttop, [i1;i2]) -> @@ -885,11 +886,12 @@ and pp_string_format (e,ii) = match ty, iity with (NoType,_) -> failwith "pp_type_left: unexpected NoType" | (Pointer t, [i]) -> + pp_type_left t; pr_elem i; iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) if iiqu <> [] || get_comments_after i <> [] then pr_space(); - pp_type_left t + () | (Array (eopt, t), [i1;i2]) -> pp_type_left t | (FunctionType (returnt, paramst), [i1;i2]) -> pp_type_left returnt -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [2/2] tests: Add test case to match const pointer variants
On Fri, 2020-02-07 at 07:07 +0100, Markus Elfring wrote: > > Would the following be a better subject? > > The alternative is nicer. > > > Will other implementation details become also more interesting? > > The information “Align C AST and Cocci AST” was provided. > https://lore.kernel.org/cocci/20200205130327.6812-2-jaskaransingh7654...@gmail.com/ > https://systeme.lip6.fr/pipermail/cocci/2020-February/006840.html > > * Should the potential for differences in these “AST” be checked any > more? > An effort is already being made to do this by me and Julia :) > * How can such items be compared safely? > Not sure what you mean. If you mean how the two ASTs can be compared, it's just a process of data collection and debugging. Cheers, Jaskaran. > > Is there a need to extend the test format? > https://github.com/coccinelle/coccinelle/issues/134 > > Regards, > Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH 1/2] parsing_c: Align C AST and Cocci AST for const pointer variants
On Sat, 2020-02-08 at 09:30 +0100, Markus Elfring wrote: > > For a pointer, the C parser constructed an AST dissimilar from that > > of the Cocci AST. This caused failures in matching with certain > > pointer types. For example, for the following case: > > > > char *1 const *2 id; > > > > The C AST constructed would be: > > const Pointer1 -> Pointer2 -> char > > > > The Cocci AST constructed would be: > > Pointer2 -> const Pointer1 -> char > > Which software development tools support to check such information? > A little collection of scripts/tools I've written[1], and the invaluable OCaml debugger :) Cheers, Jaskaran. [1]http://github.com/jajajasalu2/cocci-type-test-suite > Regards, > Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 0/2] cocci: Align the C AST and Cocci AST for pointer
This series is to address the type matching problem in Coccinelle. Patch 1/2 is for aligning the C and Cocci AST so that pointer to const pointer and its variants can be matched. Patch 2/2 consists of a test case for matching a pointer to const pointer and its variants. parsing_c/parser_c.mly |4 ++-- parsing_c/pretty_print_c.ml |6 -- tests/constptr.c|7 +++ tests/constptr.cocci| 19 +++ tests/constptr.res |7 +++ 5 files changed, 39 insertions(+), 4 deletions(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 2/2] tests: Add test case for matching ptr to const ptr and variants
These types would not match previously. Add a test case for matching a pointer to a const pointer and its variants. Signed-off-by: Jaskaran Singh --- tests/constptr.c | 7 +++ tests/constptr.cocci | 19 +++ tests/constptr.res | 7 +++ 3 files changed, 33 insertions(+) create mode 100644 tests/constptr.c create mode 100644 tests/constptr.cocci create mode 100644 tests/constptr.res diff --git a/tests/constptr.c b/tests/constptr.c new file mode 100644 index ..13fe064c --- /dev/null +++ b/tests/constptr.c @@ -0,0 +1,7 @@ +int main() +{ + const char * const * id; + const char * * const * id; + const char * const * * id; + const char * const id; +} diff --git a/tests/constptr.cocci b/tests/constptr.cocci new file mode 100644 index ..29f0aa96 --- /dev/null +++ b/tests/constptr.cocci @@ -0,0 +1,19 @@ +@ r0 @ +identifier id; +@@ +const char * const * +- id ++ id1 +; +const char * * const * +- id ++ id2 +; +const char * const * * +- id ++ id3 +; +const char * const +- id ++ id4 +; diff --git a/tests/constptr.res b/tests/constptr.res new file mode 100644 index ..0af4de9a --- /dev/null +++ b/tests/constptr.res @@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1; + const char * * const * id2; + const char * const * * id3; + const char * const id4; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 1/2] parsing_c: Align C AST and Cocci AST for pointer
For a pointer, the C parser constructed an AST dissimilar from that of the Cocci AST. This caused failures in matching with certain pointer types. For example, for the following case: char *1 const *2 id; The C AST constructed would be: const Pointer1 -> Pointer2 -> char The Cocci AST constructed would be: Pointer2 -> const Pointer1 -> char Change the pointer rule in the C parser so that an AST similar to the Cocci AST is constructed. Make necessary changes in the C pretty printer so that the pointer type is printed correctly. Signed-off-by: Jaskaran Singh --- parsing_c/parser_c.mly | 4 ++-- parsing_c/pretty_print_c.ml | 6 -- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly index 8d7b761e..4c74f15a 100644 --- a/parsing_c/parser_c.mly +++ b/parsing_c/parser_c.mly @@ -1333,14 +1333,14 @@ pointer: | tmul { (Ast_c.noattr,fun x -> mk_ty (Pointer x) [$1]) } | tmul pointer { let (attr,ptr) = $2 in - (attr,fun x -> mk_ty (Pointer (ptr x)) [$1]) } + (attr,fun x -> ptr (mk_ty (Pointer x) [$1])) } | tmul type_qualif_list { let (attr,tq) = $2 in (attr,fun x -> (tq.qualifD, mk_tybis (Pointer x) [$1]))} | tmul type_qualif_list pointer { let (attr1,tq) = $2 in let (attr2,ptr) = $3 in - (attr1@attr2,fun x -> (tq.qualifD, mk_tybis (Pointer (ptr x)) [$1])) } + (attr1@attr2,fun x -> ptr (tq.qualifD, mk_tybis (Pointer x) [$1])) } tmul: TMul { $1 } diff --git a/parsing_c/pretty_print_c.ml b/parsing_c/pretty_print_c.ml index a2e35588..40b89fe5 100644 --- a/parsing_c/pretty_print_c.ml +++ b/parsing_c/pretty_print_c.ml @@ -804,11 +804,12 @@ and pp_string_format (e,ii) = (FunctionType (return=void, params=int i) *) (*WRONG I THINK, use left & right function *) (* bug: pp_type_with_ident_rest None t; print_ident ident *) + pp_type_left t; pr_elem i; iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) if iiqu <> [] || get_comments_after i <> [] then pr_space(); - pp_type_with_ident_rest ident t attrs Ast_c.noattr; + print_ident ident (* ugly special case ... todo? maybe sufficient in practice *) | (ParenType ttop, [i1;i2]) -> @@ -885,11 +886,12 @@ and pp_string_format (e,ii) = match ty, iity with (NoType,_) -> failwith "pp_type_left: unexpected NoType" | (Pointer t, [i]) -> + pp_type_left t; pr_elem i; iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) if iiqu <> [] || get_comments_after i <> [] then pr_space(); - pp_type_left t + () | (Array (eopt, t), [i1;i2]) -> pp_type_left t | (FunctionType (returnt, paramst), [i1;i2]) -> pp_type_left returnt -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH v4 3/3] tests: Add test case to match const pointer variants
On Sun, 2020-02-09 at 17:48 +0100, Julia Lawall wrote: > On Sun, 9 Feb 2020, Jaskaran Singh wrote: > > > Pointer to const pointer and its variants would not match > > previously. > > Add a test case for matching these types. > > > > Signed-off-by: Jaskaran Singh > > --- > > tests/constptr.c | 7 +++ > > tests/constptr.cocci | 19 +++ > > tests/constptr.res | 7 +++ > > 3 files changed, 33 insertions(+) > > create mode 100644 tests/constptr.c > > create mode 100644 tests/constptr.cocci > > create mode 100644 tests/constptr.res > > > > diff --git a/tests/constptr.c b/tests/constptr.c > > new file mode 100644 > > index ..13fe064c > > --- /dev/null > > +++ b/tests/constptr.c > > @@ -0,0 +1,7 @@ > > +int main() > > +{ > > + const char * const * id; > > + const char * * const * id; > > + const char * const * * id; > > + const char * const id; > > As far as I can see in the Linux kernel, there is no space between > two *s, > and there is no space before an identifier. > Well, the test isn't replacing the type here. It's just replacing the ident as per the matches. So I guess the spaces are preserved as in the C file. When replacing the type like say -t x; +t y; Spaces are not added between the stars or the ident. Sorry if I'm missing something. Do you mean that the C file/res file should conform to the Linux style? Cheers, Jaskaran. > julia > > > +} > > diff --git a/tests/constptr.cocci b/tests/constptr.cocci > > new file mode 100644 > > index ..29f0aa96 > > --- /dev/null > > +++ b/tests/constptr.cocci > > @@ -0,0 +1,19 @@ > > +@ r0 @ > > +identifier id; > > +@@ > > +const char * const * > > +- id > > ++ id1 > > +; > > +const char * * const * > > +- id > > ++ id2 > > +; > > +const char * const * * > > +- id > > ++ id3 > > +; > > +const char * const > > +- id > > ++ id4 > > +; > > diff --git a/tests/constptr.res b/tests/constptr.res > > new file mode 100644 > > index ..0af4de9a > > --- /dev/null > > +++ b/tests/constptr.res > > @@ -0,0 +1,7 @@ > > +int main() > > +{ > > + const char * const * id1; > > + const char * * const * id2; > > + const char * const * * id3; > > + const char * const id4; > > +} > > -- > > 2.21.1 > > > > ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v4 0/3] cocci: Align the C AST and Cocci AST for pointer
This series is to address the type matching problem in Coccinelle. Patch 1/3 is for aligning the C and Cocci AST so that pointer to const pointer and its variants can be matched. Patch 2/3 reflects the pretty printing changes made in 1/3 in the ptrconstptr test case. A space is added between the * and const. Patch 3/3 consists of a test case for matching a pointer to const pointer and its variants. Changes in v4: -- - Change coding style w/r/t placement of comment with List.iter and the qualifier printing function. Changes in v3: -- - Add a space between the * and qualifier in Patch 1/3. - Add Patch 2/3 to add a space between * and const in the test case ptrconstptr. Changes in v2: -- - Change body and subject of Patch 2/2 as per suggestion of Markus Elfring. [PATCH v4 1/3] parsing_c: Align C AST and Cocci AST for pointer [PATCH v4 2/3] tests: Add space between * and const in [PATCH v4 3/3] tests: Add test case to match const pointer variants parsing_c/parser_c.mly |4 ++-- parsing_c/pretty_print_c.ml | 14 ++ tests/constptr.c|7 +++ tests/constptr.cocci| 19 +++ tests/constptr.res |7 +++ tests/ptrconstptr.res |2 +- 6 files changed, 46 insertions(+), 7 deletions(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v4 2/3] tests: Add space between * and const in ptrconstptr.res
A space is now added between the * and qualifier. Reflect this change in the ptrconstptr test case. Signed-off-by: Jaskaran Singh --- tests/ptrconstptr.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ptrconstptr.res b/tests/ptrconstptr.res index f5b2210c..1d0c3c1d 100644 --- a/tests/ptrconstptr.res +++ b/tests/ptrconstptr.res @@ -1,3 +1,3 @@ void main() { - const char *const *y; + const char * const *y; } -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v4 3/3] tests: Add test case to match const pointer variants
Pointer to const pointer and its variants would not match previously. Add a test case for matching these types. Signed-off-by: Jaskaran Singh --- tests/constptr.c | 7 +++ tests/constptr.cocci | 19 +++ tests/constptr.res | 7 +++ 3 files changed, 33 insertions(+) create mode 100644 tests/constptr.c create mode 100644 tests/constptr.cocci create mode 100644 tests/constptr.res diff --git a/tests/constptr.c b/tests/constptr.c new file mode 100644 index ..13fe064c --- /dev/null +++ b/tests/constptr.c @@ -0,0 +1,7 @@ +int main() +{ + const char * const * id; + const char * * const * id; + const char * const * * id; + const char * const id; +} diff --git a/tests/constptr.cocci b/tests/constptr.cocci new file mode 100644 index ..29f0aa96 --- /dev/null +++ b/tests/constptr.cocci @@ -0,0 +1,19 @@ +@ r0 @ +identifier id; +@@ +const char * const * +- id ++ id1 +; +const char * * const * +- id ++ id2 +; +const char * const * * +- id ++ id3 +; +const char * const +- id ++ id4 +; diff --git a/tests/constptr.res b/tests/constptr.res new file mode 100644 index ..0af4de9a --- /dev/null +++ b/tests/constptr.res @@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1; + const char * * const * id2; + const char * const * * id3; + const char * const id4; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v4 1/3] parsing_c: Align C AST and Cocci AST for pointer
For a pointer, the C parser constructed an AST dissimilar from that of the Cocci AST. This caused failures in matching with certain pointer types. For example, for the following case: char *1 const *2 id; The C AST constructed would be: const Pointer1 -> Pointer2 -> char The Cocci AST constructed would be: Pointer2 -> const Pointer1 -> char Change the pointer rule in the C parser so that an AST similar to the Cocci AST is constructed. Make necessary changes in the C pretty printer so that the pointer type is printed correctly. Signed-off-by: Jaskaran Singh --- parsing_c/parser_c.mly | 4 ++-- parsing_c/pretty_print_c.ml | 14 ++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly index 8d7b761e..4c74f15a 100644 --- a/parsing_c/parser_c.mly +++ b/parsing_c/parser_c.mly @@ -1333,14 +1333,14 @@ pointer: | tmul { (Ast_c.noattr,fun x -> mk_ty (Pointer x) [$1]) } | tmul pointer { let (attr,ptr) = $2 in - (attr,fun x -> mk_ty (Pointer (ptr x)) [$1]) } + (attr,fun x -> ptr (mk_ty (Pointer x) [$1])) } | tmul type_qualif_list { let (attr,tq) = $2 in (attr,fun x -> (tq.qualifD, mk_tybis (Pointer x) [$1]))} | tmul type_qualif_list pointer { let (attr1,tq) = $2 in let (attr2,ptr) = $3 in - (attr1@attr2,fun x -> (tq.qualifD, mk_tybis (Pointer (ptr x)) [$1])) } + (attr1@attr2,fun x -> ptr (tq.qualifD, mk_tybis (Pointer x) [$1])) } tmul: TMul { $1 } diff --git a/parsing_c/pretty_print_c.ml b/parsing_c/pretty_print_c.ml index a2e35588..3aaa97ca 100644 --- a/parsing_c/pretty_print_c.ml +++ b/parsing_c/pretty_print_c.ml @@ -804,11 +804,14 @@ and pp_string_format (e,ii) = (FunctionType (return=void, params=int i) *) (*WRONG I THINK, use left & right function *) (* bug: pp_type_with_ident_rest None t; print_ident ident *) + pp_type_left t; pr_elem i; - iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + iiqu +> + List.iter (* le const est forcement apres le '*' *) +(function x -> (pr_space(); pr_elem x)); if iiqu <> [] || get_comments_after i <> [] then pr_space(); - pp_type_with_ident_rest ident t attrs Ast_c.noattr; + print_ident ident (* ugly special case ... todo? maybe sufficient in practice *) | (ParenType ttop, [i1;i2]) -> @@ -885,11 +888,14 @@ and pp_string_format (e,ii) = match ty, iity with (NoType,_) -> failwith "pp_type_left: unexpected NoType" | (Pointer t, [i]) -> + pp_type_left t; pr_elem i; - iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + iiqu +> + List.iter (* le const est forcement apres le '*' *) +(function x -> (pr_space(); pr_elem x)); if iiqu <> [] || get_comments_after i <> [] then pr_space(); - pp_type_left t + () | (Array (eopt, t), [i1;i2]) -> pp_type_left t | (FunctionType (returnt, paramst), [i1;i2]) -> pp_type_left returnt -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v5 1/3] parsing_c: Align C AST and Cocci AST for pointer
For a pointer, the C parser constructed an AST dissimilar from that of the Cocci AST. This caused failures in matching with certain pointer types. For example, for the following case: char *1 const *2 id; The C AST constructed would be: const Pointer1 -> Pointer2 -> char The Cocci AST constructed would be: Pointer2 -> const Pointer1 -> char Change the pointer rule in the C parser so that an AST similar to the Cocci AST is constructed. Make necessary changes in the C pretty printer so that the pointer type is printed correctly. Signed-off-by: Jaskaran Singh --- parsing_c/parser_c.mly | 4 ++-- parsing_c/pretty_print_c.ml | 15 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/parsing_c/parser_c.mly b/parsing_c/parser_c.mly index 8d7b761e..4c74f15a 100644 --- a/parsing_c/parser_c.mly +++ b/parsing_c/parser_c.mly @@ -1333,14 +1333,14 @@ pointer: | tmul { (Ast_c.noattr,fun x -> mk_ty (Pointer x) [$1]) } | tmul pointer { let (attr,ptr) = $2 in - (attr,fun x -> mk_ty (Pointer (ptr x)) [$1]) } + (attr,fun x -> ptr (mk_ty (Pointer x) [$1])) } | tmul type_qualif_list { let (attr,tq) = $2 in (attr,fun x -> (tq.qualifD, mk_tybis (Pointer x) [$1]))} | tmul type_qualif_list pointer { let (attr1,tq) = $2 in let (attr2,ptr) = $3 in - (attr1@attr2,fun x -> (tq.qualifD, mk_tybis (Pointer (ptr x)) [$1])) } + (attr1@attr2,fun x -> ptr (tq.qualifD, mk_tybis (Pointer x) [$1])) } tmul: TMul { $1 } diff --git a/parsing_c/pretty_print_c.ml b/parsing_c/pretty_print_c.ml index a2e35588..f4821d5c 100644 --- a/parsing_c/pretty_print_c.ml +++ b/parsing_c/pretty_print_c.ml @@ -804,11 +804,14 @@ and pp_string_format (e,ii) = (FunctionType (return=void, params=int i) *) (*WRONG I THINK, use left & right function *) (* bug: pp_type_with_ident_rest None t; print_ident ident *) + pp_type_left t; pr_elem i; - iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + iiqu +> + List.iter (* le const est forcement apres le '*' *) +(function x -> pr_space(); pr_elem x); if iiqu <> [] || get_comments_after i <> [] then pr_space(); - pp_type_with_ident_rest ident t attrs Ast_c.noattr; + print_ident ident (* ugly special case ... todo? maybe sufficient in practice *) | (ParenType ttop, [i1;i2]) -> @@ -885,11 +888,13 @@ and pp_string_format (e,ii) = match ty, iity with (NoType,_) -> failwith "pp_type_left: unexpected NoType" | (Pointer t, [i]) -> + pp_type_left t; pr_elem i; - iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + iiqu +> + List.iter (* le const est forcement apres le '*' *) +(function x -> pr_space(); pr_elem x); if iiqu <> [] || get_comments_after i <> [] - then pr_space(); - pp_type_left t + then pr_space() | (Array (eopt, t), [i1;i2]) -> pp_type_left t | (FunctionType (returnt, paramst), [i1;i2]) -> pp_type_left returnt -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v5 3/3] tests: Add test case to match const pointer variants
Pointer to const pointer and its variants would not match previously. Add a test case for matching these types. Signed-off-by: Jaskaran Singh --- tests/constptr.c | 7 +++ tests/constptr.cocci | 19 +++ tests/constptr.res | 7 +++ 3 files changed, 33 insertions(+) create mode 100644 tests/constptr.c create mode 100644 tests/constptr.cocci create mode 100644 tests/constptr.res diff --git a/tests/constptr.c b/tests/constptr.c new file mode 100644 index ..49f4af65 --- /dev/null +++ b/tests/constptr.c @@ -0,0 +1,7 @@ +int main() +{ + const char * const *id; + const char ** const *id; + const char * const **id; + const char * const id; +} diff --git a/tests/constptr.cocci b/tests/constptr.cocci new file mode 100644 index ..29f0aa96 --- /dev/null +++ b/tests/constptr.cocci @@ -0,0 +1,19 @@ +@ r0 @ +identifier id; +@@ +const char * const * +- id ++ id1 +; +const char * * const * +- id ++ id2 +; +const char * const * * +- id ++ id3 +; +const char * const +- id ++ id4 +; diff --git a/tests/constptr.res b/tests/constptr.res new file mode 100644 index ..62fe2513 --- /dev/null +++ b/tests/constptr.res @@ -0,0 +1,7 @@ +int main() +{ + const char * const *id1; + const char ** const *id2; + const char * const **id3; + const char * const id4; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v5 2/3] tests: Add space between * and const in ptrconstptr.res
A space is now added between the * and qualifier. Reflect this change in the ptrconstptr test case. Signed-off-by: Jaskaran Singh --- tests/ptrconstptr.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ptrconstptr.res b/tests/ptrconstptr.res index f5b2210c..1d0c3c1d 100644 --- a/tests/ptrconstptr.res +++ b/tests/ptrconstptr.res @@ -1,3 +1,3 @@ void main() { - const char *const *y; + const char * const *y; } -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH v5 0/3] cocci: Align the C AST and Cocci AST for pointer
This series is to address the type matching problem in Coccinelle. Patch 1/3 is for aligning the C and Cocci AST so that pointer to const pointer and its variants can be matched. Patch 2/3 reflects the pretty printing changes made in 1/3 in the ptrconstptr test case. A space is added between the * and const. Patch 3/3 consists of a test case for matching a pointer to const pointer and its variants. Changes in v5: -- - Remove unnecessary parantheses and return of unit in Patch 1/3. - Have the test case in Patch 3/3 conform more closely with the Linux kernel coding style. Changes in v4: -- - Fix coding style w/r/t placement of comment with List.iter and the qualifier printing function. Changes in v3: -- - Add a space between the * and qualifier in Patch 1/3. - Add Patch 2/3 to add a space between * and const in the test case ptrconstptr. Changes in v2: -- - Change body and subject of Patch 2/2 as per suggestion of Markus Elfring. [PATCH v5 1/3] parsing_c: Align C AST and Cocci AST for pointer [PATCH v5 2/3] tests: Add space between * and const in [PATCH v5 3/3] tests: Add test case to match const pointer variants parsing_c/parser_c.mly |4 ++-- parsing_c/pretty_print_c.ml | 15 ++- tests/constptr.c|7 +++ tests/constptr.cocci| 19 +++ tests/constptr.res |7 +++ tests/ptrconstptr.res |2 +- 6 files changed, 46 insertions(+), 8 deletions(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH] parsing_c: Add space for tokens after *
In certain cases, there is no space added after the Pointer type. Examples of such cases are * const, * __attr and * const *. In the Pointer case of pp_type_with_ident_rest and pp_type_left, if the * is followed by anything within the type, add a space after the printing. Signed-off-by: Jaskaran Singh --- parsing_c/pretty_print_c.ml | 4 1 file changed, 4 insertions(+) diff --git a/parsing_c/pretty_print_c.ml b/parsing_c/pretty_print_c.ml index ccf8214d..a2e35588 100644 --- a/parsing_c/pretty_print_c.ml +++ b/parsing_c/pretty_print_c.ml @@ -806,6 +806,8 @@ and pp_string_format (e,ii) = (* bug: pp_type_with_ident_rest None t; print_ident ident *) pr_elem i; iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + if iiqu <> [] || get_comments_after i <> [] + then pr_space(); pp_type_with_ident_rest ident t attrs Ast_c.noattr; (* ugly special case ... todo? maybe sufficient in practice *) @@ -885,6 +887,8 @@ and pp_string_format (e,ii) = | (Pointer t, [i]) -> pr_elem i; iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + if iiqu <> [] || get_comments_after i <> [] + then pr_space(); pp_type_left t | (Array (eopt, t), [i1;i2]) -> pp_type_left t -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH] parsing_c: Handle case of macro before typedef
For the following case: A case in parsing_hacks.ml sometimes mislabels as a typedef ident. If is a known typedef (such as u8 or *_t), then label as a CppMacro. Subsequent cases will then label correctly. Following is a diff of --parse-c on the Linux Kernel v5.5-rc4: Before --- After nb good = 18956150, nb passed = 134061 => 0.70% passed --- nb good = 18956150, nb passed = 134062 => 0.70% passed Signed-off-by: Jaskaran Singh --- parsing_c/parsing_hacks.ml | 33 + 1 file changed, 33 insertions(+) diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml index 8374731b..685a4908 100644 --- a/parsing_c/parsing_hacks.ml +++ b/parsing_c/parsing_hacks.ml @@ -2184,6 +2184,39 @@ let lookahead2 ~pass next before = && ok_typedef s && is_macro s2 && is_type type_ -> TIdent (s, i1) + + (* xx yy zz : xx is a macro *) + | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _) + when not_struct_enum before +&& ok_typedef s2 +&& is_known_typdef s2 +-> + TCommentCpp(Token_c.CppMacro, i1) + + (* xx yy zz : xx is a typedef ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _) + when not_struct_enum before +&& ok_typedef s +-> + msg_typedef s i1 2; LP.add_typedef_root s i1; + TypedefIdent (s, i1) + + (* xx yy * zz : xx is a macro *) + | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _) +when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr + && not_struct_enum before + && ok_typedef s2 && is_known_typdef s2 +-> + TCommentCpp(Token_c.CppMacro, i1) + + (* xx yy * zz : xx is a typedef ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _) +when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr && not_struct_enum before + && ok_typedef s +-> + msg_typedef s i1 2; LP.add_typedef_root s i1; + TypedefIdent (s, i1) + (* xx yy *) | (TIdent (s, i1)::TIdent (s2, i2)::rest , _) when not_struct_enum before && ok_typedef s && not (is_macro_paren s2 rest) -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH] parsing_c: Handle case of macro before typedef
On Sun, 2020-01-19 at 18:48 +0530, Jaskaran Singh wrote: > For the following case: > > > > A case in parsing_hacks.ml sometimes mislabels as a > typedef ident. > > If is a known typedef (such as u8 or *_t), then label > as a CppMacro. Subsequent cases will then label > correctly. > > Following is a diff of --parse-c on the Linux Kernel v5.5-rc4: > Before > --- > After > > nb good = 18956150, nb passed = 134061 => 0.70% passed > --- > nb good = 18956150, nb passed = 134062 => 0.70% passed > > Signed-off-by: Jaskaran Singh Whoops, ignore this. The commit message comes out badly. Thanks, Jaskaran. ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH] parsing_c: Handle case of macro before typedef
For the following case: A case in parsing_hacks.ml sometimes mislabels as a typedef ident. If typedef is a known typedef (such as u8 or *_t), then label as a CppMacro. Subsequent cases will then label correctly. Following are results of --parse-c on the Linux Kernel v5.5-rc4: Before: nb good = 18956150, nb passed = 134061 => 0.70% passed After: nb good = 18956150, nb passed = 134062 => 0.70% passed Signed-off-by: Jaskaran Singh --- parsing_c/parsing_hacks.ml | 35 +++ 1 file changed, 35 insertions(+) diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml index 8374731b..23d675cf 100644 --- a/parsing_c/parsing_hacks.ml +++ b/parsing_c/parsing_hacks.ml @@ -2184,6 +2184,41 @@ let lookahead2 ~pass next before = && ok_typedef s && is_macro s2 && is_type type_ -> TIdent (s, i1) + + (* xx yy zz : xx is a macro *) + | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _) +when not_struct_enum before + && ok_typedef s2 + && is_known_typdef s2 +-> + TCommentCpp(Token_c.CppMacro, i1) + + (* xx yy zz : xx is a typedef ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _) +when not_struct_enum before + && ok_typedef s +-> + msg_typedef s i1 2; LP.add_typedef_root s i1; + TypedefIdent (s, i1) + + (* xx yy * zz : xx is a macro *) + | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _) +when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr + && not_struct_enum before + && ok_typedef s2 + && is_known_typdef s2 +-> + TCommentCpp(Token_c.CppMacro, i1) + + (* xx yy * zz : xx is a typedef ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _) +when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr + && not_struct_enum before + && ok_typedef s +-> + msg_typedef s i1 2; LP.add_typedef_root s i1; + TypedefIdent (s, i1) + (* xx yy *) | (TIdent (s, i1)::TIdent (s2, i2)::rest , _) when not_struct_enum before && ok_typedef s && not (is_macro_paren s2 rest) -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH] parsing_hacks: Add bool to list of known typedefs
bool is widely used in the Linux kernel. Certain cases of parsing_hacks.ml would mislabel bool. Add bool to the list of known typedefs. Stats of --parse-c on Linux v5.5-rc4 are as follows: Before: nb good = 18956150, nb passed = 134062 => 0.70% passed After: nb good = 18956150, nb passed = 134073 => 0.70% passed Signed-off-by: Jaskaran Singh --- The above diff has been obtained after applying https://www.mail-archive.com/cocci@systeme.lip6.fr/msg06706.html parsing_c/parsing_hacks.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml index 685a4908..5e3301a0 100644 --- a/parsing_c/parsing_hacks.ml +++ b/parsing_c/parsing_hacks.ml @@ -61,7 +61,7 @@ let is_known_typdef = | "u_char" | "u_short" | "u_int" | "u_long" | "u8" | "u16" | "u32" | "u64" | "s8" | "s16" | "s32" | "s64" - | "__u8" | "__u16" | "__u32" | "__u64" + | "__u8" | "__u16" | "__u32" | "__u64" | "bool" -> true | "acpi_handle" -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 0/2] cocci: Add space for tokens after *
The Pointer cases in pp_type_with_ident_rest and pp_type_left of pretty_print_c.ml sometimes do not add a necessary space, if there exist tokens after the *. Create changes necessary for adding this space and add a test case for * const *. parsing_c/pretty_print_c.ml |4 tests/ptrconstptr.c |3 +++ tests/ptrconstptr.cocci |7 +++ tests/ptrconstptr.res |3 +++ 4 files changed, 17 insertions(+) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 1/2] parsing_c: Add space for tokens after *
In certain cases, there is no space added after the Pointer type. Examples of such cases are * const, * __attr and * const *. In the Pointer case of pp_type_with_ident_rest and pp_type_left, if the * is followed by anything within the type, add a space after the printing. Signed-off-by: Jaskaran Singh --- parsing_c/pretty_print_c.ml | 4 1 file changed, 4 insertions(+) diff --git a/parsing_c/pretty_print_c.ml b/parsing_c/pretty_print_c.ml index ccf8214d..a2e35588 100644 --- a/parsing_c/pretty_print_c.ml +++ b/parsing_c/pretty_print_c.ml @@ -806,6 +806,8 @@ and pp_string_format (e,ii) = (* bug: pp_type_with_ident_rest None t; print_ident ident *) pr_elem i; iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + if iiqu <> [] || get_comments_after i <> [] + then pr_space(); pp_type_with_ident_rest ident t attrs Ast_c.noattr; (* ugly special case ... todo? maybe sufficient in practice *) @@ -885,6 +887,8 @@ and pp_string_format (e,ii) = | (Pointer t, [i]) -> pr_elem i; iiqu +> List.iter pr_elem; (* le const est forcement apres le '*' *) + if iiqu <> [] || get_comments_after i <> [] + then pr_space(); pp_type_left t | (Array (eopt, t), [i1;i2]) -> pp_type_left t -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 2/2] tests: Add test case for * const *
Previously, a space would not be added after the * const. Add a test case for testing spacing with use of the type. Signed-off-by: Jaskaran Singh --- tests/ptrconstptr.c | 3 +++ tests/ptrconstptr.cocci | 7 +++ tests/ptrconstptr.res | 3 +++ 3 files changed, 13 insertions(+) create mode 100644 tests/ptrconstptr.c create mode 100644 tests/ptrconstptr.cocci create mode 100644 tests/ptrconstptr.res diff --git a/tests/ptrconstptr.c b/tests/ptrconstptr.c new file mode 100644 index ..a2a3cf71 --- /dev/null +++ b/tests/ptrconstptr.c @@ -0,0 +1,3 @@ +void main() { + const char * const * x; +} diff --git a/tests/ptrconstptr.cocci b/tests/ptrconstptr.cocci new file mode 100644 index ..030bc024 --- /dev/null +++ b/tests/ptrconstptr.cocci @@ -0,0 +1,7 @@ +@@ +type t; +@@ +void main() { +- t x; ++ t y; +} diff --git a/tests/ptrconstptr.res b/tests/ptrconstptr.res new file mode 100644 index ..f5b2210c --- /dev/null +++ b/tests/ptrconstptr.res @@ -0,0 +1,3 @@ +void main() { + const char *const *y; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH] parsing_c: Add space for tokens after *
On Thu, 2020-01-16 at 17:01 +0530, Jaskaran Singh wrote: > On Thu, 2020-01-16 at 11:59 +0100, Julia Lawall wrote: > > On Thu, 16 Jan 2020, Jaskaran Singh wrote: > > > > > On Thu, 2020-01-16 at 11:40 +0100, Markus Elfring wrote: > > > > > In certain cases, there is no space added after the Pointer > > > > > type. > > > > > > > > Will such a pretty-printing detail matter also for the > > > > clarification > > > > of > > > > a topic like “Make change influence configurable for coding > > > > style > > > > rules”? > > > > https://github.com/coccinelle/coccinelle/issues/37 > > > > > > > > > > Not sure what you mean. If you mean to say that the result should > > > conform to the Linux coding style (i.e. the result should be > > > "const > > > char * const * y" with spaces et al intact), that seems like > > > something > > > for a different patch, right? Whether you want Coccinelle to > > > conform to > > > Linux coding style or not, the space should be added either way. > > > > The option --smpl-spacing should address this issue, by preservign > > the > > spacing illustrated in the semantic patch. I don't know if it does > > that > > in this case. > > > > Hm, can't say that it does. Here's what I get with --smpl-spacing on > case https://www.mail-archive.com/cocci@systeme.lip6.fr/msg06696.html > Whoops, incorrect link https://www.mail-archive.com/cocci@systeme.lip6.fr/msg06695.html Cheers, Jaskaran. > before: > > void main() { > - const char * const * x; > + const char *const*y ; > } > > after: > > void main() { > - const char * const * x; > + const char *const *y ; > } > > Cheers, > Jaskaran. > > > julia > > > > > Cheers, > > > Jaskaran. > > > > > > > Regards, > > > > Markus > > > > > > ___ > > > Cocci mailing list > > > Cocci@systeme.lip6.fr > > > https://systeme.lip6.fr/mailman/listinfo/cocci ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH] parsing_c: Add space for tokens after *
On Thu, 2020-01-16 at 11:59 +0100, Julia Lawall wrote: > > On Thu, 16 Jan 2020, Jaskaran Singh wrote: > > > On Thu, 2020-01-16 at 11:40 +0100, Markus Elfring wrote: > > > > In certain cases, there is no space added after the Pointer > > > > type. > > > > > > Will such a pretty-printing detail matter also for the > > > clarification > > > of > > > a topic like “Make change influence configurable for coding style > > > rules”? > > > https://github.com/coccinelle/coccinelle/issues/37 > > > > > > > Not sure what you mean. If you mean to say that the result should > > conform to the Linux coding style (i.e. the result should be "const > > char * const * y" with spaces et al intact), that seems like > > something > > for a different patch, right? Whether you want Coccinelle to > > conform to > > Linux coding style or not, the space should be added either way. > > The option --smpl-spacing should address this issue, by preservign > the > spacing illustrated in the semantic patch. I don't know if it does > that > in this case. > Hm, can't say that it does. Here's what I get with --smpl-spacing on case https://www.mail-archive.com/cocci@systeme.lip6.fr/msg06696.html before: void main() { - const char * const * x; + const char *const*y ; } after: void main() { - const char * const * x; + const char *const *y ; } Cheers, Jaskaran. > julia > > > Cheers, > > Jaskaran. > > > > > Regards, > > > Markus > > > > ___ > > Cocci mailing list > > Cocci@systeme.lip6.fr > > https://systeme.lip6.fr/mailman/listinfo/cocci ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH] parsing_c: Handle case of annotation in function proto/def
For the following case: [const_vol] [ptr] The lookahead function in Parsing_hacks would mislabel as a CppDirective, causing pretty printing errors. Add a case in the lookahead function for handling this case. Signed-off-by: Jaskaran Singh --- parsing_c/parsing_hacks.ml | 14 ++ 1 file changed, 14 insertions(+) diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml index 43421647..3f031f21 100644 --- a/parsing_c/parsing_hacks.ml +++ b/parsing_c/parsing_hacks.ml @@ -2066,6 +2066,20 @@ let lookahead2 ~pass next before = && is_type type_ -> TCommentCpp (Token_c.CppDirective, i1) +(* tt xx yy ( : xx is an annot *) + | (TIdent (s, i1)::TIdent (s2, i2)::TOPar _::_, seen::_) +when LP.current_context () = LP.InTopLevel + && (is_struct_enum before || is_type seen) + && s ==~ regexp_annot -> + TCommentCpp (Token_c.CppMacro, i1) + +(* tt * xx yy ( : xx is an annot *) + | (TIdent (s, i1)::TIdent (s2, i2)::TOPar _::_, ptr) +when LP.current_context () = LP.InTopLevel + && pointer ptr + && s ==~ regexp_annot -> + TCommentCpp (Token_c.CppMacro, i1) + (* tt xx yy; : yy is an annot *) | (TIdent (s, i1)::(TPtVirg _|TEq _)::_, TIdent (s2, i2)::type_::rest) when (is_struct_enum (type_::rest) -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 2/2] tests: Add test case for user comments attached to ident
When comments were appended to an identifier passed from python to cocci or ocaml to cocci, two cases showed pretty printing errors. Add a test case for both of the erroneous cases. Signed-off-by: Jaskaran Singh --- tests/id_comments.c | 4 tests/id_comments.cocci | 19 +++ tests/id_comments.res | 6 ++ 3 files changed, 29 insertions(+) create mode 100644 tests/id_comments.c create mode 100644 tests/id_comments.cocci create mode 100644 tests/id_comments.res diff --git a/tests/id_comments.c b/tests/id_comments.c new file mode 100644 index ..b92f346b --- /dev/null +++ b/tests/id_comments.c @@ -0,0 +1,4 @@ +foo () { + const void * const id; + pgd_t *__meminit id; +} diff --git a/tests/id_comments.cocci b/tests/id_comments.cocci new file mode 100644 index ..971616a5 --- /dev/null +++ b/tests/id_comments.cocci @@ -0,0 +1,19 @@ +@ r0 @ +type t; +position p; +@@ +t@p + +@ script:python r1 @ +id; +@@ +coccinelle.id = "id/* user comment */" + +@ r2 @ +identifier r1.id; +type r0.t; +@@ +foo() { +... +++ t id; +} diff --git a/tests/id_comments.res b/tests/id_comments.res new file mode 100644 index ..b29ac113 --- /dev/null +++ b/tests/id_comments.res @@ -0,0 +1,6 @@ +foo() { + const void * const id; + pgd_t *__meminit id; + const void *const id/* user comment */; + pgd_t *__meminit id/* user comment */; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 1/2] parsing_c: Support user comments attached to identifier
Comments attached to identifiers via OCaml/Python bindings can be helpful in using Coccinelle for source code analysis. Users of SmPL can attach these comments to identifiers for denoting some information. In certain cases, attaching comments to an identifier via OCaml/Python bindings can lead to pretty printing errors. The reason for this is that cases in unparse_cocci.ml do not recognize the identifier and the comments as different tokens. Add a function to support user comments. Signed-off-by: Jaskaran Singh --- parsing_c/unparse_cocci.ml | 32 +--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/parsing_c/unparse_cocci.ml b/parsing_c/unparse_cocci.ml index 30e755e9..0388aa59 100644 --- a/parsing_c/unparse_cocci.ml +++ b/parsing_c/unparse_cocci.ml @@ -268,23 +268,49 @@ let print_disj_list fn l sep = (* - *) (* Identifier *) +let print_with_comments id lcol rcol = + let ident_re = Str.regexp +"^\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\(/\\*.*\\*/\\)*$" in + let pr_with_comments i c = +match c with + "" -> print_text i +| _ -> +print_text i; +pr_barrier lcol rcol; +print_text c in + let get_match i s = +try matched i s +with Not_found -> "" in + if id ==~ ident_re + then +let matched_id = get_match 1 id in +let comment = get_match 2 id in +pr_with_comments matched_id comment + else print_text id in + let rec ident i = match Ast.unwrap i with Ast.Id(name) -> mcode print_string name | Ast.MetaId(name,_,_,_) -> + let (_,_,_,lcol,rcol) = lookup_metavar name in handle_metavar name (function - | (Ast_c.MetaIdVal id) -> print_text id + | (Ast_c.MetaIdVal id) -> + print_with_comments id lcol rcol | _ -> error name i "identifier value expected" ) | Ast.MetaFunc(name,_,_,_) -> + let (_,_,_,lcol,rcol) = lookup_metavar name in handle_metavar name (function - | (Ast_c.MetaFuncVal id) -> print_text id + | (Ast_c.MetaFuncVal id) -> + print_with_comments id lcol rcol | _ -> error name i "function name value expected" ) | Ast.MetaLocalFunc(name,_,_,_) -> + let (_,_,_,lcol,rcol) = lookup_metavar name in handle_metavar name (function - | (Ast_c.MetaLocalFuncVal id) -> print_text id + | (Ast_c.MetaLocalFuncVal id) -> + print_with_comments id lcol rcol | _ -> error name i "local function name value expected" -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 0/2] cocci: Support user comments attached to identifiers
Comments attached to identifiers via OCaml/Python bindings can be helpful in using Coccinelle for source code analysis. Users of SmPL can attach these comments to identifiers for denoting some information. This patch series is for handling these comments and adding a corresponding test case for cases that previously caused pretty printing errors parsing_c/unparse_cocci.ml | 32 +--- tests/id_comments.c|4 tests/id_comments.cocci| 19 +++ tests/id_comments.res |6 ++ 4 files changed, 58 insertions(+), 3 deletions(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH] parsing_c: Handle case of annotation and ; or = after ident
For the following case: [const_vol] [ptr] The lookahead function in Parsing_hacks would mislabel as a CppDirective. Add cases in the lookahead function for handling this case. Signed-off-by: Jaskaran Singh --- parsing_c/parsing_hacks.ml | 31 +++ 1 file changed, 31 insertions(+) diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml index 56857f3c..43421647 100644 --- a/parsing_c/parsing_hacks.ml +++ b/parsing_c/parsing_hacks.ml @@ -1887,6 +1887,11 @@ let not_struct_enum = function (Parser_c.Tstruct _ | Parser_c.Tunion _ | Parser_c.Tenum _)::_ -> false | _ -> true +let is_struct_enum = function + | (Parser_c.TIdent _):: +(Parser_c.Tstruct _ | Parser_c.Tunion _ | Parser_c.Tenum _)::_ -> true + | _ -> false + let not_opar = function TOPar _ -> false | _ -> true @@ -2061,6 +2066,32 @@ let lookahead2 ~pass next before = && is_type type_ -> TCommentCpp (Token_c.CppDirective, i1) + (* tt xx yy; : yy is an annot *) + | (TIdent (s, i1)::(TPtVirg _|TEq _)::_, TIdent (s2, i2)::type_::rest) +when (is_struct_enum (type_::rest) + || is_type type_) + && s ==~ regexp_annot -> + TCommentCpp (Token_c.CppMacro, i1) + + (* tt * xx yy; : yy is an annot *) + | (TIdent (s, i1)::(TPtVirg _|TEq _)::_, TIdent (s2, i2)::ptr) +when pointer ptr + && s ==~ regexp_annot -> + TCommentCpp (Token_c.CppMacro, i1) + + (* tt xx yy; : yy is an annot, so xx is an ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::(TPtVirg _|TEq _)::_, seen::_) +when (is_struct_enum before + || is_type seen) + && s2 ==~ regexp_annot -> + TIdent (s, i1) + + (* tt * xx yy; : yy is an annot, so xx is an ident *) + | (TIdent (s, i1)::TIdent (s2, i2)::(TPtVirg _|TEq _)::_, ptr) +when pointer ptr + && s2 ==~ regexp_annot -> + TIdent (s, i1) + (* tt xx yy *) | (TIdent (s, i1)::TIdent (s2, i2)::_ , seen::_) when not_struct_enum before -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH 00/13] cocci: Align the C AST and SmPL AST for enum
On Mon, 2020-03-09 at 15:15 +0100, Julia Lawall wrote: > > On Sun, 8 Mar 2020, Jaskaran Singh wrote: > > > The C AST and SmPL AST differs with respect to the enum type. > > > > For an enumerator, the C AST is as follows: > > Enum -> list of (name, (info, expression)) > > > > For the same, the SmPL AST is as follows: > > EnumDef -> list of expression > > > > While the SmPL parser does make sure that enumerators are > > parsed as per C rules, the OCaml types for an enumerator themselves > > mismatch, due to their organization. This causes bugs/mismatches > > for > > cases where enums are in disjunctions. > > > > This patch series makes the enumerator type of the SmPL AST > > closer to that of the C AST. Various places in the codebase that > > handle an enum are also changed to match the new type, and > > collateral evolutions caused by changed in the SmPL visitors are > > handled as well. > > > > Changes are also made to Cocci_vs_c to correctly match two > > enumerators, and in Pretty_print_cocci and Unparse_cocci to > > correctly print an enumerator. > > I have applied all of the changes. In the end, I squashed all of the > commits together, to only commit something that compiles, but I > appreciated having the changes broken up into more understandable > units. > Sorry about that. Will be more careful about these big changes next time, and thanks for applying. Cheers, Jaskaran. > thanks, > julia > > > > [PATCH 01/13] parsing_cocci: Align C AST and SmPL AST for enum > > [PATCH 02/13] ocaml: coccilib: Reflect changes in SmPL AST for > > [PATCH 03/13] parsing_cocci: parser: Parse enumerators correctly > > [PATCH 04/13] parsing_cocci: Add EnumDeclTag and EnumDeclDotsTag to > > [PATCH 05/13] ocaml: coccilib: Reflect EnumDeclTag and > > [PATCH 06/13] parsing_cocci: visitor_ast0: Add visitor functions > > for > > [PATCH 07/13] parsing_cocci: Reflect visitor_ast0 changes in > > [PATCH 08/13] parsing_cocci: Add visitor functions for enum_decl in > > [PATCH 09/13] cocci: Reflect changes in SmPL visitor_ast in > > codebase > > [PATCH 10/13] engine: cocci_vs_c: Match enumerators properly as per > > [PATCH 11/13] cocci: pretty print EnumDef as per enum_decl type > > [PATCH 12/13] tests: Add test case for assigned enumerator > > [PATCH 13/13] tools: spgen: Reflect visitor changes > > > > cocci.ml |4 - > > engine/asttoctl2.ml | 21 +--- > > engine/asttomember.ml | 17 --- > > engine/cocci_vs_c.ml | 46 --- > > engine/transformation_c.ml|4 - > > ocaml/coccilib.mli| 22 - > > parsing_c/unparse_cocci.ml| 27 ++- > > parsing_c/unparse_hrule.ml|4 - > > parsing_cocci/arity.ml| 25 ++ > > parsing_cocci/ast0_cocci.ml | 15 +- > > parsing_cocci/ast0_cocci.mli | 14 + > > parsing_cocci/ast0toast.ml| 30 +++- > > parsing_cocci/ast0toast.mli |4 + > > parsing_cocci/ast_cocci.ml| 13 + > > parsing_cocci/ast_cocci.mli | 12 - > > parsing_cocci/check_meta.ml | 17 +-- > > parsing_cocci/cleanup_rules.ml|5 +- > > parsing_cocci/commas_on_lists.ml | 10 ++-- > > parsing_cocci/compute_lines.ml| 25 ++ > > parsing_cocci/context_neg.ml | 47 +-- > > parsing_cocci/disjdistr.ml| 29 +--- > > parsing_cocci/free_vars.ml| 27 +-- > > parsing_cocci/function_prototypes.ml |7 +- > > parsing_cocci/get_constants2.ml |7 +- > > parsing_cocci/index.ml|7 ++ > > parsing_cocci/index.mli |2 > > parsing_cocci/insert_plus.ml | 39 +--- > > parsing_cocci/iso_compile.ml |4 - > > parsing_cocci/iso_pattern.ml | 80 > > -- > > parsing_cocci/parse_aux.ml|5 ++ > > parsing_cocci/parse_aux.mli |9 +++ > > parsing_cocci/parse_cocci.ml |4 - > > parsing_cocci/parser_cocci_menhir.mly | 13 ++--- > > parsing_cocci/pretty_print_cocci.ml | 18 +++ > > parsing_cocci/re_constraints.ml | 10 ++-- > > parsing_cocci/safe_for_multi_decls.ml | 11 ++-- > > parsing_cocci/single_statement.ml |5 +- > > pars
[Cocci] [PATCH 26/26] tests: Add test case for array of function pointers
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. Signed-off-by: Jaskaran Singh --- 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 diff --git a/tests/funptr_array.c b/tests/funptr_array.c new file mode 100644 index ..dac29ac6 --- /dev/null +++ b/tests/funptr_array.c @@ -0,0 +1 @@ +int (*x[2])(int x); diff --git a/tests/funptr_array.cocci b/tests/funptr_array.cocci new file mode 100644 index ..8027bcf4 --- /dev/null +++ b/tests/funptr_array.cocci @@ -0,0 +1,9 @@ +@@ +type T; +identifier x; +@@ + +T (*x[2])( +- int ++ char + x); diff --git a/tests/funptr_array.res b/tests/funptr_array.res new file mode 100644 index ..72e1a14a --- /dev/null +++ b/tests/funptr_array.res @@ -0,0 +1 @@ +int (*x[2])(char x); -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 04/26] parsing_cocci: visitor_ast0: 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: ( * 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. Signed-off-by: Jaskaran Singh --- parsing_cocci/visitor_ast0.ml | 163 ++ 1 file changed, 163 insertions(+) diff --git a/parsing_cocci/visitor_ast0.ml b/parsing_cocci/visitor_ast0.ml index c282e1f8..c56cd7a7 100644 --- a/parsing_cocci/visitor_ast0.ml +++ b/parsing_cocci/visitor_ast0.ml @@ -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) =
[Cocci] [PATCH 00/26] 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 Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 17/26] parsing_cocci: type_cocci: Add ParenType/FunctionType to types
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in type_cocci.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/type_cocci.mli | 2 ++ 1 file changed, 2 insertions(+) diff --git a/parsing_cocci/type_cocci.mli b/parsing_cocci/type_cocci.mli index c1ccb58f..6f24adf7 100644 --- a/parsing_cocci/type_cocci.mli +++ b/parsing_cocci/type_cocci.mli @@ -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 *) + | FunctionTypeof typeC (* only return type *) | Array of typeC (* drop size info *) | Decimal of name * name | EnumNameof name -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 07/26] parsing_cocci: index: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in index.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/index.ml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/parsing_cocci/index.ml b/parsing_cocci/index.ml index a1103503..5c91f620 100644 --- a/parsing_cocci/index.ml +++ b/parsing_cocci/index.ml @@ -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] -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 03/26] parsing_cocci: parser: Add direct_declarator/direct_abstract_d rules
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. Signed-off-by: Jaskaran Singh --- parsing_cocci/parser_cocci_menhir.mly | 239 +- 1 file changed, 116 insertions(+), 123 deletions(-) diff --git a/parsing_cocci/parser_cocci_menhir.mly b/parsing_cocci/parser_cocci_menhir.mly index 26958f63..fade830f 100644 --- a/parsing_cocci/parser_cocci_menhir.mly +++ b/parsing_cocci/parser_cocci_menhir.mly @@ -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
[Cocci] [PATCH 02/26] parsing_cocci: AST: Add ParenType and FunctionType to SmPL ASTs
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. Signed-off-by: Jaskaran Singh --- 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(+) diff --git a/ocaml/coccilib.mli b/ocaml/coccilib.mli index 5a913099..e5409a97 100644 --- a/ocaml/coccilib.mli +++ b/ocaml/coccilib.mli @@ -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 diff --git a/parsing_cocci/ast0_cocci.ml b/parsing_cocci/ast0_cocci.ml index 77dc46f0..0acbeaa4 100644 --- a/parsing_cocci/ast0_cocci.ml +++ b/parsing_cocci/ast0_cocci.ml @@ -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 (* ) *) + | FunctionTypeof typeC * + string mcode (* ( *) * parameter_list * string mcode (* ) *) | Array of typeC * string mcode (* [ *) * expression option * string mcode (* ] *) | Decimal of string mcode (* decimal *) * string mcode (* ( *) * diff --git a/parsing_cocci/ast0_cocci.mli b/parsing_cocci/ast0_cocci.mli index 274c6bc2..33bd12b2 100644 --- a/parsing_cocci/ast0_cocci.mli +++ b/parsing_cocci/ast0_cocci.mli @@ -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 (* ) *) + | FunctionTypeof typeC * + string mcode (* ( *) * parameter_list * string mcode (* ) *) | Array of typeC * string mcode (* [ *) * expression option * string mcode (* ] *) | Decimal of string mcode (* decimal *) * string mcode (* ( *) * diff --git a/parsing_cocci/ast_cocci.ml b/parsing_cocci/ast_cocci.ml index ba6ec29e..f8e6dee6 100644 --- a/parsing_cocci/ast_cocci.ml +++ b/parsing_cocci/ast_cocci.ml @@ -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 (* ) *) + | FunctionTypeof fullType * + string mcode (* ( *) * parameter_list * string mcode (* ) *) | Array of fullType * string mcode (* [ *) * expression option * string mcode (* ] *) | Decimal of string mcode (* decimal *) * string mcode (* ( *) * diff --git a/parsing_cocci/ast_cocci.mli b/parsing_cocci/ast_cocci.mli index 5f21664b..7fb54e4d 100644 --- a/parsing_cocci/ast_cocci.mli +++ b/parsing_cocci/ast_cocci.mli @@ -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 (* ) *) + | FunctionTypeof fullType * + string mcode (* ( *) * parameter_list * string mcode (* ) *) | Array of fullType * string mcode (* [ *) * expression option * string mcode (* ] *) | Decimal of string mcode (* decimal *) * string mcode (* ( *) * -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6
[Cocci] [PATCH 01/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. Signed-off-by: Jaskaran Singh --- parsing_cocci/parse_cocci.ml | 72 +++ parsing_cocci/parser_cocci_menhir.mly | 9 ++-- 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/parsing_cocci/parse_cocci.ml b/parsing_cocci/parse_cocci.ml index 679d213a..4b2cb7e4 100644 --- a/parsing_cocci/parse_cocci.ml +++ b/parsing_cocci/parse_cocci.ml @@ -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,
[Cocci] [PATCH 10/26] parsing_cocci: single_statement: Add cases for ParenType/FunctionType
ParenType/FunctionType are now types in the SmPL ASTs. Add cases for these types in single_statement.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/single_statement.ml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/parsing_cocci/single_statement.ml b/parsing_cocci/single_statement.ml index 46408b73..48d77b5a 100644 --- a/parsing_cocci/single_statement.ml +++ b/parsing_cocci/single_statement.ml @@ -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 -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 14/26] parsing_cocci: adjust_pragmas: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in adjust_pragmas.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/adjust_pragmas.ml | 4 1 file changed, 4 insertions(+) diff --git a/parsing_cocci/adjust_pragmas.ml b/parsing_cocci/adjust_pragmas.ml index 73b5ff5f..537150e7 100644 --- a/parsing_cocci/adjust_pragmas.ml +++ b/parsing_cocci/adjust_pragmas.ml @@ -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) -> -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 13/26] parsing_cocci: iso_pattern: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in iso_pattern.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/iso_pattern.ml | 5 + 1 file changed, 5 insertions(+) diff --git a/parsing_cocci/iso_pattern.ml b/parsing_cocci/iso_pattern.ml index 5ceb2bdb..6d51b072 100644 --- a/parsing_cocci/iso_pattern.ml +++ b/parsing_cocci/iso_pattern.ml @@ -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') -> -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 12/26] parsing_cocci: check_meta: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in check_meta.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/check_meta.ml | 5 + 1 file changed, 5 insertions(+) diff --git a/parsing_cocci/check_meta.ml b/parsing_cocci/check_meta.ml index 02b0fd9c..7b10bd53 100644 --- a/parsing_cocci/check_meta.ml +++ b/parsing_cocci/check_meta.ml @@ -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 -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 22/26] parsing_c: unparse_cocci: Print ParenType/FunctionType
The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following: ( * 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. Signed-off-by: Jaskaran Singh --- parsing_c/unparse_cocci.ml | 60 ++ 1 file changed, 60 insertions(+) diff --git a/parsing_c/unparse_cocci.ml b/parsing_c/unparse_cocci.ml index 30e755e9..9dd84821 100644 --- a/parsing_c/unparse_cocci.ml +++ b/parsing_c/unparse_cocci.ml @@ -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 *) -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 06/26] parsing_cocci: arity: Add cases for ParenType/FunctionType
ParenType and FunctionType are added to the SmPL ASTs. Add cases for these types in arity.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/arity.ml | 17 + 1 file changed, 17 insertions(+) diff --git a/parsing_cocci/arity.ml b/parsing_cocci/arity.ml index f29b86eb..3b408554 100644 --- a/parsing_cocci/arity.ml +++ b/parsing_cocci/arity.ml @@ -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) -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 15/26] parsing_cocci: compute_lines: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in compute_lines.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/compute_lines.ml | 11 +++ 1 file changed, 11 insertions(+) diff --git a/parsing_cocci/compute_lines.ml b/parsing_cocci/compute_lines.ml index f4b6f4d8..8231a0e7 100644 --- a/parsing_cocci/compute_lines.ml +++ b/parsing_cocci/compute_lines.ml @@ -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 -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 11/26] parsing_cocci: function_prototypes: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in function_prototypes.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/function_prototypes.ml | 4 1 file changed, 4 insertions(+) diff --git a/parsing_cocci/function_prototypes.ml b/parsing_cocci/function_prototypes.ml index 2e6eea43..fd36f31b 100644 --- a/parsing_cocci/function_prototypes.ml +++ b/parsing_cocci/function_prototypes.ml @@ -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) -> -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 19/26] parsing_cocci: disjdistr: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in disjdistr.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/disjdistr.ml | 10 ++ 1 file changed, 10 insertions(+) diff --git a/parsing_cocci/disjdistr.ml b/parsing_cocci/disjdistr.ml index 668a8810..91d7f1b8 100644 --- a/parsing_cocci/disjdistr.ml +++ b/parsing_cocci/disjdistr.ml @@ -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 -> -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 21/26] parsing_cocci: pretty_print_cocci: Print ParenType/FunctionType
The order of the terms in ParenType require implementing a special case for ParenType. This case handles only the following: ( * 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. Signed-off-by: Jaskaran Singh --- parsing_cocci/pretty_print_cocci.ml | 57 + 1 file changed, 57 insertions(+) diff --git a/parsing_cocci/pretty_print_cocci.ml b/parsing_cocci/pretty_print_cocci.ml index 6338e464..ef60106c 100644 --- a/parsing_cocci/pretty_print_cocci.ml +++ b/parsing_cocci/pretty_print_cocci.ml @@ -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() -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 09/26] parsing_cocci: unparse_ast0: Add cases for ParenType/FunctionType
ParenType/FunctionType are now types in the SmPL ASTs. Add cases for these types in unparse_ast0.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/unparse_ast0.ml | 43 +++ 1 file changed, 43 insertions(+) diff --git a/parsing_cocci/unparse_ast0.ml b/parsing_cocci/unparse_ast0.ml index 5d450e0e..f8684dd2 100644 --- a/parsing_cocci/unparse_ast0.ml +++ b/parsing_cocci/unparse_ast0.ml @@ -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 = -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 05/26] parsing_cocci: visitor_ast0: 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: ( * 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. Signed-off-by: Jaskaran Singh --- parsing_cocci/visitor_ast.ml | 75 1 file changed, 75 insertions(+) diff --git a/parsing_cocci/visitor_ast.ml b/parsing_cocci/visitor_ast.ml index 3d5de794..99af7652 100644 --- a/parsing_cocci/visitor_ast.ml +++ b/parsing_cocci/visitor_ast.ml @@ -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,
[Cocci] [PATCH 08/26] parsing_cocci: context_neg: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in context_neg.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/context_neg.ml | 4 1 file changed, 4 insertions(+) diff --git a/parsing_cocci/context_neg.ml b/parsing_cocci/context_neg.ml index 7bbf743d..b3a52daf 100644 --- a/parsing_cocci/context_neg.ml +++ b/parsing_cocci/context_neg.ml @@ -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), -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 18/26] parsing_cocci: unify_ast: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in unify_ast.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/unify_ast.ml | 8 1 file changed, 8 insertions(+) diff --git a/parsing_cocci/unify_ast.ml b/parsing_cocci/unify_ast.ml index d4ad3030..de86b1d3 100644 --- a/parsing_cocci/unify_ast.ml +++ b/parsing_cocci/unify_ast.ml @@ -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), -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 25/26] cocci: Remove Ast_cocci.FunctionPointer
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. Signed-off-by: Jaskaran Singh --- 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(-) diff --git a/engine/check_exhaustive_pattern.ml b/engine/check_exhaustive_pattern.ml index 872c060c..fe0b2c78 100644 --- a/engine/check_exhaustive_pattern.ml +++ b/engine/check_exhaustive_pattern.ml @@ -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, _) -> () diff --git a/engine/cocci_vs_c.ml b/engine/cocci_vs_c.ml index 3e0eb50e..87b0ce70 100644 --- a/engine/cocci_vs_c.ml +++ b/engine/cocci_vs_c.ml @@ -3733,58 +3733,6 @@ and (typeC: (A.typeC, Ast_c.typeC) matcher) = (B.Pointer typb, [ibmult]) ))) -| A.FunctionPointer(tya,lp1a,stara,rp1a,lp2a,paramsa,rp2a), -(B.ParenType t1, ii) -> -let (lp1b, rp1b) = tuple_of_list2 ii in -let (qu1b, t1b) = t1 in -(match t1b with -| B.Pointer t2, ii -> -let (starb) = tuple_of_list1 ii in -let (qu2b, t2b) = t2 in -(match t2b with -| B.FunctionType (tyb, (paramsb, (isvaargs, iidotsb))), ii -> -let (lp2b, rp2b) = tuple_of_list2 ii in - -if isvaargs -then - pr2_once - ("Not handling well variable length arguments func. "^ -"You have been warned"); - -fullType tya tyb >>= (fun tya tyb -> -tokenf lp1a lp1b >>= (fun lp1a lp1b -> -tokenf rp1a rp1b >>= (fun rp1a rp1b -> -tokenf lp2a lp2b >>= (fun lp2a lp2b -> -tokenf rp2a rp2b >>= (fun rp2a rp2b -> -tokenf stara starb >>= (fun stara starb -> -parameters (seqstyle paramsa) (A.unwrap paramsa) paramsb >>= -(fun paramsaunwrap paramsb -> - let paramsa = A.rewrap paramsa paramsaunwrap in - - let t2 = -(qu2b, -(B.FunctionType (tyb, (paramsb, (isvaargs, iidotsb))), -[lp2b;rp2b])) - in - let t1 = -(qu1b, -(B.Pointer t2, [starb])) - in - - return ( -(A.FunctionPointer(tya,lp1a,stara,rp1a,lp2a,paramsa,rp2a)) -+> A.rewrap ta, -(B.ParenType t1, [lp1b;rp1b]) - ) -))) - - - -| _ -> fail -) -| _ -> fail -) - | A.ParenType (lpa, typa, rpa), (B.ParenType typb, ii) -> let (lpb, rpb) = tuple_of_list2 ii in fullType typa typb >>= (fun typa typb -> @@ -4381,9 +4329,6 @@ and compatible_typeC a (b,local) = | A.Pointer (a, _), (qub, (B.Pointer b, ii)) -> compatible_type a (b, local) -| A.FunctionPointer (a, _, _, _, _, _, _), _ -&
[Cocci] [PATCH 20/26] parsing_cocci: ast_cocci: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in ast_cocci.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/ast_cocci.ml | 10 ++ 1 file changed, 10 insertions(+) diff --git a/parsing_cocci/ast_cocci.ml b/parsing_cocci/ast_cocci.ml index f8e6dee6..4fea9794 100644 --- a/parsing_cocci/ast_cocci.ml +++ b/parsing_cocci/ast_cocci.ml @@ -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 -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 23/26] engine: Match A.ParenType and A.FunctionType
ParenType and FunctionType are added to the SmPL AST as types. Match these types correctly. Signed-off-by: Jaskaran Singh --- engine/check_exhaustive_pattern.ml | 2 ++ engine/cocci_vs_c.ml | 27 +++ 2 files changed, 29 insertions(+) diff --git a/engine/check_exhaustive_pattern.ml b/engine/check_exhaustive_pattern.ml index 5a047f73..872c060c 100644 --- a/engine/check_exhaustive_pattern.ml +++ b/engine/check_exhaustive_pattern.ml @@ -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) -> () diff --git a/engine/cocci_vs_c.ml b/engine/cocci_vs_c.ml index a4a0e53b..3e0eb50e 100644 --- a/engine/cocci_vs_c.ml +++ b/engine/cocci_vs_c.ml @@ -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) -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 24/26] tools: spgen: Add cases for ParenType/FunctionType
ParenType and FunctionType are now added to the SmPL ASTs. Add cases for these types in position_generator.ml. Signed-off-by: Jaskaran Singh --- tools/spgen/source/position_generator.ml | 6 ++ 1 file changed, 6 insertions(+) diff --git a/tools/spgen/source/position_generator.ml b/tools/spgen/source/position_generator.ml index 31d6cb5e..2fa630fb 100644 --- a/tools/spgen/source/position_generator.ml +++ b/tools/spgen/source/position_generator.ml @@ -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 -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [PATCH 22/23] tests: Add test case for removing parameter attributes
On Mon, 2020-04-27 at 19:31 +0200, Julia Lawall wrote: > > On Mon, 27 Apr 2020, Jaskaran Singh wrote: > > > Add a test case for removing Parameter attributes. The test case > > checks > > correct removal of the attribute when it is: > > > > - before the parameter type. > > - after the parameter type and before the parameter identifier. > > - after the parameter identifier. > > > > Signed-off-by: Jaskaran Singh > > --- > > tests/remove_param_attrs.c | 11 +++ > > tests/remove_param_attrs.cocci | 13 + > > tests/remove_param_attrs.res | 11 +++ > > 3 files changed, 35 insertions(+) > > create mode 100644 tests/remove_param_attrs.c > > create mode 100644 tests/remove_param_attrs.cocci > > create mode 100644 tests/remove_param_attrs.res > > > > diff --git a/tests/remove_param_attrs.c > > b/tests/remove_param_attrs.c > > new file mode 100644 > > index ..8ec1ffde > > --- /dev/null > > +++ b/tests/remove_param_attrs.c > > @@ -0,0 +1,11 @@ > > +int func1(int x, __nocast int y) { > > + return 0; > > +} > > + > > +int func2(int x, int __nocast y) { > > + return 0; > > +} > > + > > +int func3(int x, int y __nocast) { > > + return 0; > > +} > > diff --git a/tests/remove_param_attrs.cocci > > b/tests/remove_param_attrs.cocci > > new file mode 100644 > > index ..43f5a204 > > --- /dev/null > > +++ b/tests/remove_param_attrs.cocci > > @@ -0,0 +1,13 @@ > > +@@ > > +type T,U; > > +attribute name __nocast; > > +identifier x,y; > > +@@ > > + > > +T x( > > +..., > > +U y > > +- __nocast > > So the idea is that the attribute is removed no matter where it > occurs? > Yes, if the attribute occurs in the parameter. Cheers, Jaskaran. > julia > > > +, > > +... > > + ) {...} > > diff --git a/tests/remove_param_attrs.res > > b/tests/remove_param_attrs.res > > new file mode 100644 > > index ..3705e814 > > --- /dev/null > > +++ b/tests/remove_param_attrs.res > > @@ -0,0 +1,11 @@ > > +int func1(int x, int y) { > > + return 0; > > +} > > + > > +int func2(int x, int y) { > > + return 0; > > +} > > + > > +int func3(int x, int y) { > > + return 0; > > +} > > -- > > 2.21.1 > > > > ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] [RFC PATCH 00/25] cocci: Improve C parsing of attributes
On Fri, 2020-04-24 at 14:02 +0200, Markus Elfring wrote: > > This patch series aims to improve parsing of attributes in C by > > Coccinelle's C parser. > > How do you think about to use the wording “in C source code by”? > > > > These parsing errors were discovered by running a build of > > Coccinelle's > > Would you like to omit the word “These”? > Hi Markus, Could you please keep your feedback relevant to the actual patch itself (or the commit messages)? Maybe pull the patches, apply them, run them yourself, see if something breaks? Feedback like this really isn't relevant, unless these minor typos/grammartical errors cause a _lot_ of confusion. > > > Coccinelle currently manages attributes similar to comments, > > Will this aspect trigger further software development considerations? > I don't know. > > > so to explicity state what the attributes are to the C parser, > > a MACROANNOTATION hint was used in Coccinelle's standard.h file. > > I find such information suspicious at first glance. > Additional background information from an update step like > “[RFC PATCH 12/25] parsing_c: cpp_token_c: Introduce MACROANNOTATION > hint” > might make the proposed data processing approach more reasonable. > https://lore.kernel.org/cocci/20200424091801.13871-13-jaskaransingh7654...@gmail.com/ > https://systeme.lip6.fr/pipermail/cocci/2020-April/007217.html > > > > Separate patches will be sent for the above. > > I am curious how the software evolution will be continued here. > Good to know. Cheers, Jaskaran. > Regards, > Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 13/23] parsing_c: unparse_cocci: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Print these attributes correctly in unparse_cocci.ml. Signed-off-by: Jaskaran Singh --- parsing_c/unparse_cocci.ml | 16 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/parsing_c/unparse_cocci.ml b/parsing_c/unparse_cocci.ml index 395b3a3c..d3f97bf7 100644 --- a/parsing_c/unparse_cocci.ml +++ b/parsing_c/unparse_cocci.ml @@ -1089,10 +1089,18 @@ and designator = function and parameterTypeDef p = match Ast.unwrap p with -Ast.VoidParam(ty) -> fullType ty - | Ast.Param(ty,Some id) -> print_named_type ty (fun _ -> ident id) - | Ast.Param(ty,None) -> fullType ty - +Ast.VoidParam(ty,attr) -> + fullType ty; + (if not (attr = []) then pr_space()); + print_between pr_space (mcode print_string) attr; + | Ast.Param(ty,Some id,attr) -> + print_named_type ty (fun _ -> ident id); + (if not (attr = []) then pr_space()); + print_between pr_space (mcode print_string) attr; + | Ast.Param(ty,None,attr) -> + fullType ty; + (if not (attr = []) then pr_space()); + print_between pr_space (mcode print_string) attr; | Ast.MetaParam(name,_,_,_) -> handle_metavar name (function -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 14/23] parsing_cocci: ast_cocci: Add Parameter attributes
Add parameter attributes to the SmPL AST. This is a list of attributes in the VoidParam and Param types of the SmPL AST. Signed-off-by: Jaskaran Singh --- parsing_cocci/ast_cocci.ml | 4 ++-- parsing_cocci/ast_cocci.mli | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/parsing_cocci/ast_cocci.ml b/parsing_cocci/ast_cocci.ml index 9b147e5c..919d1dcc 100644 --- a/parsing_cocci/ast_cocci.ml +++ b/parsing_cocci/ast_cocci.ml @@ -487,8 +487,8 @@ and initialiser = base_initialiser wrap (* Parameter *) and base_parameterTypeDef = -VoidParam of fullType - | Param of fullType * ident option +VoidParam of fullType * attr list + | Param of fullType * ident option * attr list | MetaParam of meta_name mcode * constraints * keep_binding * inherited | MetaParamList of meta_name mcode * listlen * constraints * keep_binding * diff --git a/parsing_cocci/ast_cocci.mli b/parsing_cocci/ast_cocci.mli index c56bba88..4b6e7296 100644 --- a/parsing_cocci/ast_cocci.mli +++ b/parsing_cocci/ast_cocci.mli @@ -468,8 +468,8 @@ and initialiser = base_initialiser wrap (* Parameter *) and base_parameterTypeDef = -VoidParam of fullType - | Param of fullType * ident option +VoidParam of fullType * attr list + | Param of fullType * ident option * attr list | MetaParam of meta_name mcode * constraints * keep_binding * inherited | MetaParamList of meta_name mcode * listlen * constraints * keep_binding * -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 15/23] parsing_cocci: visitor_ast: Visit Parameter attributes
Parameter attributes are added to the SmPL AST. Visit these attributes in the AST visitor. Signed-off-by: Jaskaran Singh --- parsing_cocci/visitor_ast.ml | 22 +- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/parsing_cocci/visitor_ast.ml b/parsing_cocci/visitor_ast.ml index 8e530114..f1e78d8c 100644 --- a/parsing_cocci/visitor_ast.ml +++ b/parsing_cocci/visitor_ast.ml @@ -621,9 +621,18 @@ let combiner bind option_default and parameterTypeDef p = let k p = match Ast.unwrap p with - Ast.VoidParam(ty) -> fullType ty - | Ast.Param(ty,Some id) -> named_type ty id - | Ast.Param(ty,None) -> fullType ty + Ast.VoidParam(ty,attr) -> + let lty = fullType ty in + let lattr = multibind (List.map string_mcode attr) in + bind lty lattr + | Ast.Param(ty,Some id,attr) -> + let lid = named_type ty id in + let lattr = multibind (List.map string_mcode attr) in + bind lid lattr + | Ast.Param(ty,None,attr) -> + let lty = fullType ty in + let lattr = multibind (List.map string_mcode attr) in + bind lty lattr | Ast.MetaParam(name,_,_,_) -> meta_mcode name | Ast.MetaParamList(name,_,_,_,_) -> meta_mcode name |Ast.AsParam(p,asexp) -> @@ -1582,8 +1591,11 @@ let rebuilder let k p = Ast.rewrap p (match Ast.unwrap p with - Ast.VoidParam(ty) -> Ast.VoidParam(fullType ty) - | Ast.Param(ty,id) -> Ast.Param(fullType ty, get_option ident id) + Ast.VoidParam(ty,attr) -> +Ast.VoidParam(fullType ty,List.map string_mcode attr) + | Ast.Param(ty,id,attr) -> +Ast.Param + (fullType ty, get_option ident id,List.map string_mcode attr) | Ast.MetaParam(name,constraints,keep,inherited) -> Ast.MetaParam(meta_mcode name,constraints,keep,inherited) | Ast.MetaParamList(name,lenname_inh,constraints,keep,inherited) -> -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 10/23] parsing_cocci: iso_pattern: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in iso_pattern.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/iso_pattern.ml | 22 ++ 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/parsing_cocci/iso_pattern.ml b/parsing_cocci/iso_pattern.ml index 9df21fb9..09a39bdf 100644 --- a/parsing_cocci/iso_pattern.ml +++ b/parsing_cocci/iso_pattern.ml @@ -1165,10 +1165,24 @@ let match_maker checks_needed context_required whencode_allowed = if not(checks_needed) || not(context_required) || is_context p then match (up,Ast0.unwrap p) with - (Ast0.VoidParam(tya),Ast0.VoidParam(tyb)) -> match_typeC tya tyb - | (Ast0.Param(tya,ida),Ast0.Param(tyb,idb)) -> - conjunct_bindings (match_typeC tya tyb) - (match_option match_ident ida idb) + (Ast0.VoidParam(tya,attra),Ast0.VoidParam(tyb,attrb)) -> + if +(List.length attra = List.length attrb && + List.fold_left2 (fun p a b -> p && mcode_equal a b) true + attra attrb) + then + match_typeC tya tyb + else return false + | (Ast0.Param(tya,ida,attra),Ast0.Param(tyb,idb,attrb)) -> + if +(List.length attra = List.length attrb && + List.fold_left2 (fun p a b -> p && mcode_equal a b) true + attra attrb) + then + conjunct_bindings (match_typeC tya tyb) + (match_option match_ident ida idb) + else +return false | (Ast0.PComma(c1),Ast0.PComma(c)) -> check_mcode c1 c | (Ast0.Pdots(d1),Ast0.Pdots(d)) -> check_mcode d1 d | (Ast0.OptParam(parama),Ast0.OptParam(paramb)) -> -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 16/23] parsing_cocci: ast0toast: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in ast0toast.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/ast0toast.ml | 7 --- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/parsing_cocci/ast0toast.ml b/parsing_cocci/ast0toast.ml index 09c28c06..80d38138 100644 --- a/parsing_cocci/ast0toast.ml +++ b/parsing_cocci/ast0toast.ml @@ -857,10 +857,11 @@ and designator = function and parameterTypeDef p = rewrap p no_isos (match Ast0.unwrap p with - Ast0.VoidParam(ty) -> Ast.VoidParam(typeC false ty) -| Ast0.Param(ty,id) -> + Ast0.VoidParam(ty,attr) -> +Ast.VoidParam(typeC false ty,List.map mcode attr) +| Ast0.Param(ty,id,attr) -> let allminus = check_allminus.VT0.combiner_rec_parameter p in - Ast.Param(typeC allminus ty,get_option ident id) + Ast.Param(typeC allminus ty,get_option ident id,List.map mcode attr) | Ast0.MetaParam(name,cstr,_) -> Ast.MetaParam(mcode name,constraints cstr,unitary,false) | Ast0.MetaParamList(name,lenname,cstr,_) -> -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 23/23] tests: Add test case to detect parameter attributes
Add a test case to detect parameter attributes in C. The test case changes the type of the parameter if it has the attribute. The cases covered are for when the attribute is: - before the parameter type. - after the parameter type and before the parameter identifier. - after the parameter identifier. Signed-off-by: Jaskaran Singh --- tests/detect_param_attr.c | 3 +++ tests/detect_param_attr.cocci | 13 + tests/detect_param_attr.res | 3 +++ 3 files changed, 19 insertions(+) create mode 100644 tests/detect_param_attr.c create mode 100644 tests/detect_param_attr.cocci create mode 100644 tests/detect_param_attr.res diff --git a/tests/detect_param_attr.c b/tests/detect_param_attr.c new file mode 100644 index ..160d6af2 --- /dev/null +++ b/tests/detect_param_attr.c @@ -0,0 +1,3 @@ +int func1(int __nocast u, __nocast int z, int q __nocast, int w) { + return 0; +} diff --git a/tests/detect_param_attr.cocci b/tests/detect_param_attr.cocci new file mode 100644 index ..53b8668f --- /dev/null +++ b/tests/detect_param_attr.cocci @@ -0,0 +1,13 @@ +@@ +type T1, T2; +attribute name __nocast; +identifier x, y; +@@ + +T1 x( + ..., +- T2 ++ char + y __nocast, + ... + ) {...} diff --git a/tests/detect_param_attr.res b/tests/detect_param_attr.res new file mode 100644 index ..b3db290e --- /dev/null +++ b/tests/detect_param_attr.res @@ -0,0 +1,3 @@ +int func1(char __nocast u, __nocast char z, char q __nocast, int w) { + return 0; +} -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 12/23] parsing_cocci: unparse_ast0: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in unparse_ast0.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/unparse_ast0.ml | 15 --- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/parsing_cocci/unparse_ast0.ml b/parsing_cocci/unparse_ast0.ml index fdaf6cfa..738549fd 100644 --- a/parsing_cocci/unparse_ast0.ml +++ b/parsing_cocci/unparse_ast0.ml @@ -546,9 +546,18 @@ and parameterTypeDef p = print_context p (function _ -> match Ast0.unwrap p with - Ast0.VoidParam(ty) -> typeC ty - | Ast0.Param(ty,Some id) -> print_named_type ty id - |Ast0.Param(ty,None) -> typeC ty +Ast0.VoidParam(ty,attr) -> + typeC ty; + (if (attr = []) then print_string " "); + print_between (fun _ -> print_string " ") (mcode print_string) attr; + | Ast0.Param(ty,Some id,attr) -> + print_named_type ty id; + (if (attr = []) then print_string " "); + print_between (fun _ -> print_string " ") (mcode print_string) attr; + |Ast0.Param(ty,None,attr) -> + typeC ty; + (if (attr = []) then print_string " "); + print_between (fun _ -> print_string " ") (mcode print_string) attr; | Ast0.MetaParam(name,_,_) -> mcode print_meta name | Ast0.MetaParamList(name,_,_,_) -> mcode print_meta name | Ast0.PComma(cm) -> mcode print_string cm; print_space() -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 06/23] parsing_cocci: compute_lines: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in compute_lines.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/compute_lines.ml | 15 +-- 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/parsing_cocci/compute_lines.ml b/parsing_cocci/compute_lines.ml index 1361d842..9bf4ab06 100644 --- a/parsing_cocci/compute_lines.ml +++ b/parsing_cocci/compute_lines.ml @@ -914,13 +914,16 @@ and is_param_dots p = and parameterTypeDef p = match Ast0.unwrap p with -Ast0.VoidParam(ty) -> - let ty = typeC ty in mkres p (Ast0.VoidParam(ty)) ty ty - | Ast0.Param(ty,Some id) -> +Ast0.VoidParam(ty,attr) -> + let attr = List.map normal_mcode attr in + let ty = typeC ty in mkres p (Ast0.VoidParam(ty,attr)) ty ty + | Ast0.Param(ty,Some id,attr) -> let id = ident id in - let ty = typeC ty in mkres p (Ast0.Param(ty,Some id)) ty id - | Ast0.Param(ty,None) -> - let ty = typeC ty in mkres p (Ast0.Param(ty,None)) ty ty + let attr = List.map normal_mcode attr in + let ty = typeC ty in mkres p (Ast0.Param(ty,Some id,attr)) ty id + | Ast0.Param(ty,None,attr) -> + let attr = List.map normal_mcode attr in + let ty = typeC ty in mkres p (Ast0.Param(ty,None,attr)) ty ty | Ast0.MetaParam(name,a,b) -> let name = normal_mcode name in let ln = promote_mcode name in -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 00/23] cocci: Add parameter attributes to SmPL
This patch series aims to add parameter attributes to SmPL, and is a continuation of the series "cocci: Improve C parsing of attributes"[1]. In [1], parameter attributes were added to the C AST of Coccinelle, but not to SmPL. Two test cases are included: - detect_param_attr: Test case to detect a parameter attribute. - remove_param_attr: Test case to remove a parameter attribute as per the given SmPL. [1]https://www.mail-archive.com/cocci@systeme.lip6.fr/msg07133.html Jaskaran Singh (23): parsing_cocci: ast0_cocci: Add parameter attributes parsing_cocci: parser: Parse Parameter attributes parsing_cocci: visitor_ast0: Visit Parameter attributes parsing_cocci: arity: Reflect Parameter attributes parsing_cocci: check_meta: Reflect Parameter attributes parsing_cocci: compute_lines: Reflect Parameter attributes parsing_cocci: context_neg: Reflect Parameter attributes parsing_cocci: function_prototypes: Reflect Parameter attributes parsing_cocci: index: Reflect Parameter attributes parsing_cocci: iso_pattern: Reflect Parameter attributes parsing_cocci: type_infer: Reflect Parameter attributes parsing_cocci: unparse_ast0: Reflect Parameter attributes parsing_c: unparse_cocci: Reflect Parameter attributes parsing_cocci: ast_cocci: Add Parameter attributes parsing_cocci: visitor_ast: Visit Parameter attributes parsing_cocci: ast0toast: Reflect Parameter attributes parsing_cocci: disjdistr: Reflect Parameter attributes parsing_cocci: pretty_print_cocci: Reflect Parameter attributes parsing_cocci: unify_ast: Reflect Parameter attributes engine: cocci_vs_c: Match Parameter attributes ocaml: coccilib: Reflect Parameter attributes tests: Add test case for removing parameter attributes tests: Add test case to detect parameter attributes engine/cocci_vs_c.ml | 18 +- ocaml/coccilib.mli|8 parsing_c/unparse_cocci.ml| 16 parsing_cocci/arity.ml| 17 ++--- parsing_cocci/ast0_cocci.ml |4 ++-- parsing_cocci/ast0_cocci.mli |4 ++-- parsing_cocci/ast0toast.ml|7 --- parsing_cocci/ast_cocci.ml|4 ++-- parsing_cocci/ast_cocci.mli |4 ++-- parsing_cocci/check_meta.ml |2 +- parsing_cocci/compute_lines.ml| 15 +-- parsing_cocci/context_neg.ml | 10 ++ parsing_cocci/disjdistr.ml|6 +++--- parsing_cocci/function_prototypes.ml | 12 ++-- parsing_cocci/index.ml|4 ++-- parsing_cocci/iso_pattern.ml | 22 ++ parsing_cocci/parser_cocci_menhir.mly |7 --- parsing_cocci/pretty_print_cocci.ml | 15 --- parsing_cocci/type_infer.ml |2 +- parsing_cocci/unify_ast.ml| 14 ++ parsing_cocci/unparse_ast0.ml | 15 --- parsing_cocci/visitor_ast.ml | 22 +- parsing_cocci/visitor_ast0.ml | 16 ++-- tests/detect_param_attr.c |3 +++ tests/detect_param_attr.cocci | 13 + tests/detect_param_attr.res |3 +++ tests/remove_param_attrs.c| 11 +++ tests/remove_param_attrs.cocci| 13 + tests/remove_param_attrs.res | 11 +++ 29 files changed, 212 insertions(+), 86 deletions(-) ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 05/23] parsing_cocci: check_meta: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in check_meta.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/check_meta.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parsing_cocci/check_meta.ml b/parsing_cocci/check_meta.ml index 5a348ba3..ab1d357e 100644 --- a/parsing_cocci/check_meta.ml +++ b/parsing_cocci/check_meta.ml @@ -374,7 +374,7 @@ and initialiser_list old_metas table minus = and parameterTypeDef old_metas table minus param = match Ast0.unwrap param with -Ast0.Param(ty,id) -> +Ast0.Param(ty,id,attr) -> get_opt (ident ID old_metas table minus) id; typeC old_metas table minus ty | Ast0.MetaParam(name,_,_) -> -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 09/23] parsing_cocci: index: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in index.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/index.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parsing_cocci/index.ml b/parsing_cocci/index.ml index f7ae48b4..1baeb2bb 100644 --- a/parsing_cocci/index.ml +++ b/parsing_cocci/index.ml @@ -157,8 +157,8 @@ let initialiser i = let parameterTypeDef p = match Ast0.unwrap p with -Ast0.VoidParam(ty) -> [59] - | Ast0.Param(ty,id) -> [60] +Ast0.VoidParam(ty,attr) -> [59] + | Ast0.Param(ty,id,attr) -> [60] | Ast0.MetaParam(name,_,_) -> [61] | Ast0.MetaParamList(name,_,_,_) -> [62] | Ast0.PComma(cm) -> [63] -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 02/23] parsing_cocci: parser: Parse Parameter attributes
Parameter attributes are added to the SmPL AST. Parse these attributes correctly in the SmPL parser. The added production only supports attributes after the type or the type and identifier. Signed-off-by: Jaskaran Singh --- parsing_cocci/parser_cocci_menhir.mly | 7 --- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/parsing_cocci/parser_cocci_menhir.mly b/parsing_cocci/parser_cocci_menhir.mly index db5661bd..fe4ef322 100644 --- a/parsing_cocci/parser_cocci_menhir.mly +++ b/parsing_cocci/parser_cocci_menhir.mly @@ -1541,9 +1541,10 @@ storage: | s=Tregister{ P.clt2mcode Ast.Register s } | s=Textern { P.clt2mcode Ast.Extern s } -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)) } +decl: t=ctype d=direct_declarator(disj_ident) ar=attr_list + { let (i,fn) = d in Ast0.wrap(Ast0.Param(fn t, Some i, ar)) } +| t=ctype ar=attr_list +{ (*verify in FunDecl*) Ast0.wrap(Ast0.Param(t, None, ar)) } | TMetaParam { let (nm,cstr,pure,clt) = $1 in Ast0.wrap(Ast0.MetaParam(P.clt2mcode nm clt,cstr,pure)) } -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 08/23] parsing_cocci: function_prototypes: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in function_prototypes.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/function_prototypes.ml | 12 ++-- 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/parsing_cocci/function_prototypes.ml b/parsing_cocci/function_prototypes.ml index 71bf1a2a..e223013b 100644 --- a/parsing_cocci/function_prototypes.ml +++ b/parsing_cocci/function_prototypes.ml @@ -185,7 +185,7 @@ and changed_proto = function (* - *) (* make rules *) -let collect_ident_strings id = +let collect_id_strings id = let bind x y = x @ y in let option_default = [] in let donothing r k e = k e in @@ -261,10 +261,10 @@ let rec attach_right strings ty = let rec drop_param_name p = Ast0.rewrap p (match Ast0.unwrap p with - Ast0.Param(p,Some id) -> - let strings = collect_ident_strings id in + Ast0.Param(p,Some id,attr) -> + let strings = collect_id_strings id in let p = attach_right strings p in - Ast0.Param(p,None) + Ast0.Param(p,None,attr) | Ast0.OptParam(p) -> Ast0.OptParam(drop_param_name p) | p -> p) @@ -297,7 +297,7 @@ let new_iname name index = let rec rename_param old_name all param index = match Ast0.unwrap param with -Ast0.Param(ty,Some id) when all -> +Ast0.Param(ty,Some id,attr) when all -> (match Ast0.unwrap id with Ast0.MetaId (((_,name),arity,info,mcodekind,pos,adj),constraints,seed,pure) -> @@ -308,7 +308,7 @@ let rec rename_param old_name all param index = ((nm,arity,info,mcodekind,pos,adj),constraints,seed, Ast0.Pure)) in ([Ast.MetaIdDecl(Ast.NONE,nm)], - Ast0.rewrap param (Ast0.Param(ty,Some new_id))) + Ast0.rewrap param (Ast0.Param(ty,Some new_id,attr))) |_ -> ([],param)) | Ast0.Pdots(d) -> let nm = (old_name,new_iname "__P" index) in -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 21/23] ocaml: coccilib: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in coccilib.mli. Signed-off-by: Jaskaran Singh --- ocaml/coccilib.mli | 8 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ocaml/coccilib.mli b/ocaml/coccilib.mli index 0e807c9a..59c58aea 100644 --- a/ocaml/coccilib.mli +++ b/ocaml/coccilib.mli @@ -2825,8 +2825,8 @@ module Ast_cocci : and initialiser = base_initialiser wrap and base_parameterTypeDef = Ast_cocci.base_parameterTypeDef = -VoidParam of fullType - | Param of fullType * ident option +VoidParam of fullType * attr list + | Param of fullType * ident option * attr list | MetaParam of meta_name mcode * constraints * keep_binding * inherited | MetaParamList of meta_name mcode * listlen * constraints * keep_binding * inherited @@ -3455,8 +3455,8 @@ module Ast0_cocci : and initialiser_list = initialiser dots and base_parameterTypeDef = Ast0_cocci.base_parameterTypeDef = -VoidParam of typeC - | Param of typeC * ident option +VoidParam of typeC * attr list + | Param of typeC * ident option * attr list | MetaParam of Ast_cocci.meta_name mcode * constraints * pure | MetaParamList of Ast_cocci.meta_name mcode * listlen * constraints * pure -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 20/23] engine: cocci_vs_c: Match Parameter attributes
Parameter attributes are added to the C and SmPL AST. Match the attributes correctly in cocci_vs_c.ml. Signed-off-by: Jaskaran Singh --- engine/cocci_vs_c.ml | 18 +- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/engine/cocci_vs_c.ml b/engine/cocci_vs_c.ml index 317fa71e..66cb104a 100644 --- a/engine/cocci_vs_c.ml +++ b/engine/cocci_vs_c.ml @@ -2112,14 +2112,14 @@ and parameters_bis eas ebs = let special_cases ea eas ebs = (* a case where one smpl parameter matches a list of C parameters *) match A.unwrap ea,ebs with - A.VoidParam ta, ys -> + A.VoidParam (ta, attrsa), ys -> Some (match eas, ebs with | [], [Left eb] -> let {B.p_register=(hasreg,iihasreg); p_namei = idbopt; p_type=tb; -p_attr=attrs; } = eb in +p_attr=attrsb; } = eb in let attr_allminus = check_allminus.Visitor_ast.combiner_parameter ea in @@ -2128,10 +2128,10 @@ and parameters_bis eas ebs = match tb with | (qub, (B.BaseType B.Void,_)) -> fullType ta tb >>= (fun ta tb -> -attribute_list attr_allminus [] attrs >>= +attribute_list attr_allminus attrsa attrsb >>= (fun attrsa attrsb -> return ( - [(A.VoidParam ta) +> A.rewrap ea], + [(A.VoidParam (ta, attrsa)) +> A.rewrap ea], [Left {B.p_register=(hasreg, iihasreg); p_namei = idbopt; p_type = tb; @@ -2171,23 +2171,23 @@ and parameter = fun parama paramb -> return (A.MetaParam(ida,constraints,keep,inherited)+> A.rewrap parama,eb))) - | A.Param (typa, idaopt), eb -> + | A.Param (typa, idaopt, attrsa), eb -> let {B.p_register = (hasreg,iihasreg); p_namei = nameidbopt; p_type = typb; -p_attr = attrs;} = paramb in +p_attr = attrsb;} = paramb in let attr_allminus = check_allminus.Visitor_ast.combiner_parameter parama in fullType typa typb >>= (fun typa typb -> - attribute_list attr_allminus [] attrs >>= (fun attrsa attrsb -> + attribute_list attr_allminus attrsa attrsb >>= (fun attrsa attrsb -> match idaopt, nameidbopt with | Some ida, Some nameidb -> (* todo: if minus on ida, should also minus the iihasreg ? *) ident_cpp DontKnow ida nameidb >>= (fun ida nameidb -> return ( - A.Param (typa, Some ida)+> A.rewrap parama, + A.Param (typa, Some ida, attrsa)+> A.rewrap parama, {B.p_register = (hasreg, iihasreg); p_namei = Some (nameidb); p_type = typb; @@ -2196,7 +2196,7 @@ and parameter = fun parama paramb -> | None, None -> return ( -A.Param (typa, None)+> A.rewrap parama, +A.Param (typa, None, attrsa)+> A.rewrap parama, {B.p_register=(hasreg,iihasreg); p_namei = None; p_type = typb; -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 18/23] parsing_cocci: pretty_print_cocci: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Print these attributes correctly in pretty_print_cocci.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/pretty_print_cocci.ml | 15 --- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/parsing_cocci/pretty_print_cocci.ml b/parsing_cocci/pretty_print_cocci.ml index 7628882b..a9eb4a8b 100644 --- a/parsing_cocci/pretty_print_cocci.ml +++ b/parsing_cocci/pretty_print_cocci.ml @@ -688,9 +688,18 @@ and designator = function and parameterTypeDef p = match Ast.unwrap p with -Ast.VoidParam(ty) -> fullType ty - | Ast.Param(ty,Some id) -> print_named_type ty (fun _ -> ident id); - | Ast.Param(ty,None) -> fullType ty +Ast.VoidParam(ty,attr) -> + fullType ty; + (if not (attr = []) then print_string " "); + print_between print_space (mcode print_string) attr + | Ast.Param(ty,Some id,attr) -> + print_named_type ty (fun _ -> ident id); + (if not (attr = []) then print_string " "); + print_between print_space (mcode print_string) attr + | Ast.Param(ty,None,attr) -> + fullType ty; + (if not (attr = []) then print_string " "); + print_between print_space (mcode print_string) attr | Ast.MetaParam(name,_,_,_) -> mcode print_meta name | Ast.MetaParamList(name,_,_,_,_) -> mcode print_meta name | Ast.PComma(cm) -> mcode print_string cm; print_space() -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 19/23] parsing_cocci: unify_ast: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in unify_ast.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/unify_ast.ml | 14 ++ 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/parsing_cocci/unify_ast.ml b/parsing_cocci/unify_ast.ml index 68cb6613..e2b92c48 100644 --- a/parsing_cocci/unify_ast.ml +++ b/parsing_cocci/unify_ast.ml @@ -542,11 +542,17 @@ and unify_designator d1 d2 = and unify_parameterTypeDef p1 p2 = match (Ast.unwrap p1,Ast.unwrap p2) with -(Ast.VoidParam(ft1),Ast.VoidParam(ft2)) -> unify_fullType ft1 ft2 - | (Ast.Param(ft1,i1),Ast.Param(ft2,i2)) -> - unify_fullType ft1 ft2 && - unify_option unify_ident i1 i2 +(Ast.VoidParam(ft1,attr1),Ast.VoidParam(ft2,attr2)) -> + if List.for_all2 unify_mcode attr1 attr2 + then unify_fullType ft1 ft2 + else false + | (Ast.Param(ft1,i1,attr1),Ast.Param(ft2,i2,attr2)) -> + if List.for_all2 unify_mcode attr1 attr2 + then +unify_fullType ft1 ft2 && +unify_option unify_ident i1 i2 + else false | (Ast.MetaParam(_,_,_,_),_) | (Ast.MetaParamList(_,_,_,_,_),_) | (_,Ast.MetaParam(_,_,_,_)) -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
[Cocci] [PATCH 11/23] parsing_cocci: type_infer: Reflect Parameter attributes
Parameter attributes are added to the SmPL AST. Reflect these changes in type_infer.ml. Signed-off-by: Jaskaran Singh --- parsing_cocci/type_infer.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parsing_cocci/type_infer.ml b/parsing_cocci/type_infer.ml index e8aad810..495d7974 100644 --- a/parsing_cocci/type_infer.ml +++ b/parsing_cocci/type_infer.ml @@ -420,7 +420,7 @@ let rec propagate_types env = Ast0.FunDecl(_,fninfo,name,lp,params,va,rp,lbrace,body,rbrace,_) -> let rec get_binding p = match Ast0.unwrap p with - Ast0.Param(ty,Some id) -> + Ast0.Param(ty,Some id,attr) -> List.map (function i -> (i,ty)) (strip id) | Ast0.OptParam(param) -> get_binding param | Ast0.AsParam(param,e) -> get_binding param -- 2.21.1 ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci