cocci: Align the C AST and Cocci AST for pointer
cover 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(-)
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
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.
--- parsing_c/parser_c.mly | 4 ++-- parsing_c/pretty_print_c.ml | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-)
parsing_c/parser_c.mly +2 -2
@@ -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 }
parsing_c/pretty_print_c.ml +4 -2
@@ -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
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH 2/2 tests: Add test case for matching ptr to const ptr and variants
In reply to Jaskaran SinghThese types would not match previously. Add a test case for matching a pointer to a const pointer and its variants.
--- 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
tests/constptr.c new file +7 -0
@@ -0,0 +1,7 @@ +int main () +{ +const char *const * id; +const char * *const * id; +const char *const * * id; +const char *const id; +}
tests/constptr.cocci new file +19 -0
@@ -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 +;
tests/constptr.res new file +7 -0
@@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1; + const char * * const * id2; + const char * const * * id3; + const char * const id4; +}
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
v2 cover cocci: Align the C AST and Cocci AST for pointer
In reply to Jaskaran SinghThis 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(-)
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
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.
--- parsing_c/parser_c.mly | 4 ++-- parsing_c/pretty_print_c.ml | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-)
parsing_c/parser_c.mly +2 -2
@@ -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 }
parsing_c/pretty_print_c.ml +4 -2
@@ -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
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Re: [Cocci] [PATCH v2 1/2] parsing_c: Align C AST and Cocci AST for pointer
There is a problem with the pretty printing. For the following semantic patch:
@@
type t;
symbol x;
@@
void main() {
- t x;
+ t y;
}
and the following C code:
void main() {
const char * const * x;
}The output is:
void main() {
- const char * const * x;
+ const char *const *y;
}
There should be a space between * and const on the + line.
julia
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v2 2/2 tests: Add test case to match const pointer variants
In reply to Jaskaran SinghPointer to const pointer and its variants would not match previously. Add a test case for matching these types.
--- 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
tests/constptr.c new file +7 -0
@@ -0,0 +1,7 @@ +int main () +{ +const char *const * id; +const char * *const * id; +const char *const * * id; +const char *const id; +}
tests/constptr.cocci new file +19 -0
@@ -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 +;
tests/constptr.res new file +7 -0
@@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1; + const char * * const * id2; + const char * const * * id3; + const char * const id4; +}
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Re: [Cocci] [PATCH v2 2/2] tests: Add test case to match const pointer variants
Quoted 2 lines · Jaskaran Singh
Pointer to const pointer and its variants would not match previously. Add a test case for matching these types.
I don't get any matches for this test. If I split the semantic patch into four separate rules, then I get the change for id2 and id4, but not for the other two.
julia
Quoted 63 lines
Signed-off-by Jaskaran Singh <[email protected]>--- 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.restests/constptr.c new file +7
-0@@ -0,0 +1,7 @@ +int main () +{ +const char *const * id; +const char * *const * id; +const char *const * * id; +const char *const id; +}tests/constptr.cocci new file +19
-0@@ -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 +;tests/constptr.res new file +7
-1@@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1; + const char * * const * id2; + const char * const * * id3; + const char * const id4; +} --
Quoted 3 lines
2.21.1
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Re: [Cocci] [PATCH v2 2/2] tests: Add test case to match const pointer variants
Quoted 6 lines · Julia Lawall
Quoted 3 lines · Jaskaran Singh
Pointer to const pointer and its variants would not match previously.
Add a test case for matching these types.
Quoted 6 lines
I don't get any matches for this test. If I split the semantic patch into
four separate rules, then I get the change for id2 and id4, but not for
the other two.
That's weird, it works fine for me. I get this on both bytecode and native builds.
tmp/cocci-output-31326-084eed-constptr.c renamed from constptr.c +4 -4
@@ -1,7 +1,7 @@int main () { -const char *const * id; -const char * *const * id; -const char *const * * id; -const char *const id; +const char *const * id1; +const char * *const * id2; +const char *const * * id3; +const char *const id4; }
On the other hand, I get the id2 and id4 result with upstream.
tmp/cocci-output-5387-a9b63b-constptr.c renamed from constptr.c +2 -2
@@ -1,7 +1,7 @@int main () {const char *const * id; -const char * *const * id; +const char * *const * id2;const char *const * * id; -const char *const id; +const char *const id4; }
Could you check if you've applied 1/2 first?
Cheers,
Jaskaran.
Quoted 59 lines
julia
Quoted 57 lines
Signed-off-by Jaskaran Singh <[email protected]>--- 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.restests/constptr.c new file +7
-0@@ -0,0 +1,7 @@ +int main () +{ +const char *const * id; +const char * *const * id; +const char *const * * id; +const char *const id; +}tests/constptr.cocci new file +19
-0@@ -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 +;tests/constptr.res new file +3
-0@@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1;
Quoted 8 lines
Quoted 8 lines
tests/constptr.res new file +4
-1+ const char * * const * id2; + const char * const * * id3; + const char * const id4; +} --2.21.1
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Re: [Cocci] [PATCH v2 2/2] tests: Add test case to match const pointer variants
Quoted 14 lines · Jaskaran Singh
Quoted 6 lines · Julia Lawall
Quoted 3 lines · Jaskaran Singh
Pointer to const pointer and its variants would not match previously.
Add a test case for matching these types.Quoted 6 lines
I don't get any matches for this test. If I split the semantic patch into
four separate rules, then I get the change for id2 and id4, but not for
the other two.
Quoted 2 lines
That's weird, it works fine for me. I get this on both bytecode and native builds.
OK, you're right, it's OK.
Can you fix the *const problem? I still see that one.
thanks,
julia
Quoted 96 lines
tmp/cocci-output-31326-084eed-constptr.c renamed from constptr.c +4
-4@@ -1,7 +1,7 @@int main () { -const char *const * id; -const char * *const * id; -const char *const * * id; -const char *const id; +const char *const * id1; +const char * *const * id2; +const char *const * * id3; +const char *const id4; }On the other hand, I get the id2 and id4 result with upstream.
tmp/cocci-output-5387-a9b63b-constptr.c renamed from constptr.c +2
-2@@ -1,7 +1,7 @@int main () {const char *const * id; -const char * *const * id; +const char * *const * id2;const char *const * * id; -const char *const id; +const char *const id4; }Could you check if you've applied 1/2 first?
Cheers,
Jaskaran.Quoted 59 lines
julia
Quoted 57 lines
Signed-off-by Jaskaran Singh <[email protected]>--- 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.restests/constptr.c new file +7
-0@@ -0,0 +1,7 @@ +int main () +{ +const char *const * id; +const char * *const * id; +const char *const * * id; +const char *const id; +}tests/constptr.cocci new file +19
-0@@ -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 +;tests/constptr.res new file +3
-0@@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1;
Quoted 10 lines
Quoted 8 lines
Quoted 8 lines
tests/constptr.res new file +4
-1+ const char * * const * id2; + const char * const * * id3; + const char * const id4; +} --2.21.1
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Re: [Cocci] [PATCH v2 2/2] tests: Add test case to match const pointer variants
Quoted 18 lines · Julia Lawall
Quoted 16 lines · Jaskaran Singh
Quoted 14 lines · Julia Lawall
Quoted 3 lines · Jaskaran Singh
Pointer to const pointer and its variants would not match previously.
Add a test case for matching these types.I don't get any matches for this test. If I split the semantic patch
into
four separate rules, then I get the change for id2 and id4, but not
for
the other two.
Quoted 7 lines
Quoted 2 lines
That's weird, it works fine for me. I get this on both bytecode and native builds.
OK, you're right, it's OK.
Can you fix the *const problem? I still see that one.
Sure, will send v3.
Cheers,
Jaskaran.
Quoted 98 lines
thanks,
juliaQuoted 95 lines
tmp/cocci-output-31326-084eed-constptr.c renamed from constptr.c +4
-4@@ -1,7 +1,7 @@int main () { -const char *const * id; -const char * *const * id; -const char *const * * id; -const char *const id; +const char *const * id1; +const char * *const * id2; +const char *const * * id3; +const char *const id4; }On the other hand, I get the id2 and id4 result with upstream.
tmp/cocci-output-5387-a9b63b-constptr.c renamed from constptr.c +2
-2@@ -1,7 +1,7 @@int main () {const char *const * id; -const char * *const * id; +const char * *const * id2;const char *const * * id; -const char *const id; +const char *const id4; }Could you check if you've applied 1/2 first?
Cheers,
Jaskaran.Quoted 59 lines
julia
Quoted 57 lines
Signed-off-by Jaskaran Singh <[email protected]>--- 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.restests/constptr.c new file +7
-0@@ -0,0 +1,7 @@ +int main () +{ +const char *const * id; +const char * *const * id; +const char *const * * id; +const char *const id; +}tests/constptr.cocci new file +19
-0@@ -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 +;tests/constptr.res new file +3
-0@@ -0,0 +1,7 @@ +int main() +{ + const char * const * id1;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
tests/constptr.res new file +4
-1+ const char * * const * id2; + const char * const * * id3; + const char * const id4; +} --2.21.1
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees