* [Linux-kernel-mentees] [PATCH 1/2] parsing_c: Support user comments attached to identifier
2020-01-10 17:11 [Linux-kernel-mentees] [PATCH 0/2] cocci: Support user comments attached to identifiers Jaskaran Singh
@ 2020-01-10 17:11 ` Jaskaran Singh
2020-01-10 17:11 ` [Linux-kernel-mentees] [PATCH 2/2] tests: Add test case for user comments attached to ident Jaskaran Singh
1 sibling, 0 replies; 5+ messages in thread
From: Jaskaran Singh @ 2020-01-10 17:11 UTC (permalink / raw)
To: cocci; +Cc: julia.lawall, jaskaransingh7654321, linux-kernel-mentees
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 <jaskaransingh7654321@gmail.com>
---
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
_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [Linux-kernel-mentees] [PATCH 2/2] tests: Add test case for user comments attached to ident
2020-01-10 17:11 [Linux-kernel-mentees] [PATCH 0/2] cocci: Support user comments attached to identifiers Jaskaran Singh
2020-01-10 17:11 ` [Linux-kernel-mentees] [PATCH 1/2] parsing_c: Support user comments attached to identifier Jaskaran Singh
@ 2020-01-10 17:11 ` Jaskaran Singh
2020-01-10 18:39 ` [Linux-kernel-mentees] [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to identifiers Markus Elfring
2020-01-11 9:18 ` [Linux-kernel-mentees] [Cocci] [2/2] " Markus Elfring
1 sibling, 2 replies; 5+ messages in thread
From: Jaskaran Singh @ 2020-01-10 17:11 UTC (permalink / raw)
To: cocci; +Cc: julia.lawall, jaskaransingh7654321, linux-kernel-mentees
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 <jaskaransingh7654321@gmail.com>
---
| 4 ++++
| 19 +++++++++++++++++++
| 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
--git a/tests/id_comments.c b/tests/id_comments.c
new file mode 100644
index 00000000..b92f346b
--- /dev/null
+++ b/tests/id_comments.c
@@ -0,0 +1,4 @@
+foo () {
+ const void * const id;
+ pgd_t *__meminit id;
+}
--git a/tests/id_comments.cocci b/tests/id_comments.cocci
new file mode 100644
index 00000000..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;
+}
--git a/tests/id_comments.res b/tests/id_comments.res
new file mode 100644
index 00000000..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
_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
^ permalink raw reply related [flat|nested] 5+ messages in thread