Coccinelle archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] [PATCH 0/2] cocci: Support user comments attached to identifiers
@ 2020-01-10 17:11 Jaskaran Singh
  2020-01-10 17:11 ` [Cocci] [PATCH 1/2] parsing_c: Support user comments attached to identifier Jaskaran Singh
  2020-01-10 17:11 ` [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to ident Jaskaran Singh
  0 siblings, 2 replies; 5+ messages in thread
From: Jaskaran Singh @ 2020-01-10 17:11 UTC (permalink / raw)
  To: cocci; +Cc: linux-kernel-mentees, skhan

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Cocci] [PATCH 1/2] parsing_c: Support user comments attached to identifier
  2020-01-10 17:11 [Cocci] [PATCH 0/2] cocci: Support user comments attached to identifiers Jaskaran Singh
@ 2020-01-10 17:11 ` Jaskaran Singh
  2020-01-10 17:11 ` [Cocci] [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: linux-kernel-mentees, skhan

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

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to ident
  2020-01-10 17:11 [Cocci] [PATCH 0/2] cocci: Support user comments attached to identifiers Jaskaran Singh
  2020-01-10 17:11 ` [Cocci] [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   ` [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to identifiers Markus Elfring
  2020-01-11  9:18   ` [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: linux-kernel-mentees, skhan

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>
---
 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 00000000..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 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;
+}
diff --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

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to identifiers
  2020-01-10 17:11 ` [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to ident Jaskaran Singh
@ 2020-01-10 18:39   ` Markus Elfring
  2020-01-11  9:18   ` [Cocci] [2/2] " Markus Elfring
  1 sibling, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2020-01-10 18:39 UTC (permalink / raw)
  To: Jaskaran Singh, cocci; +Cc: linux-kernel-mentees, Shuah Khan

> Add a test case for both of the erroneous cases.

Thanks for your contribution.

Will a slightly different subject be more appropriate for this patch?


> +@ r0 @
> +type t;
> +position p;

I suggest to omit this variable for the shown simple SmPL script.


> +@ script:python r1 @
> +id;
> +@@
> +coccinelle.id = "id/* user comment */"
> +
> +@ r2 @
> +identifier r1.id;
> +type r0.t;
> +@@
> +foo() {
> +...
> +++ t id;
> +}

This variable assignment and the addition of commented variable declarations
looks interesting.

* Can such a change be achieved also directly without using an extra
  Python (or OCaml) script?

* I would usually prefer to avoid the generation of duplicate source code.
  The corresponding example might demonstrate other test goals.

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Cocci] [2/2] tests: Add test case for user comments attached to identifiers
  2020-01-10 17:11 ` [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to ident Jaskaran Singh
  2020-01-10 18:39   ` [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to identifiers Markus Elfring
@ 2020-01-11  9:18   ` " Markus Elfring
  1 sibling, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2020-01-11  9:18 UTC (permalink / raw)
  To: Jaskaran Singh, cocci; +Cc: linux-kernel-mentees, Shuah Khan

> +@ script:python r1 @
> +id;
> +@@
> +coccinelle.id = "id/* user comment */"

I am still looking for further clarification around this programming interface.

See also topics like the following:
* Propagating values back from Python script to SmPL rule with other metavariable
  type than “identifier”
  https://github.com/coccinelle/coccinelle/issues/86

* Reconsider programming interfaces for script-constructed metavariables
  https://github.com/coccinelle/coccinelle/issues/132

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-10 17:11 [Cocci] [PATCH 0/2] cocci: Support user comments attached to identifiers Jaskaran Singh
2020-01-10 17:11 ` [Cocci] [PATCH 1/2] parsing_c: Support user comments attached to identifier Jaskaran Singh
2020-01-10 17:11 ` [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to ident Jaskaran Singh
2020-01-10 18:39   ` [Cocci] [PATCH 2/2] tests: Add test case for user comments attached to identifiers Markus Elfring
2020-01-11  9:18   ` [Cocci] [2/2] " Markus Elfring

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr
	public-inbox-index cocci

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git