[Linux-kernel-mentees] [PATCH 1/2] parsing_c: Support user comments attached to identifier

Jaskaran Singh jaskaransingh7654321 at gmail.com
Fri Jan 10 17:11:49 UTC 2020


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 at 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



More information about the Linux-kernel-mentees mailing list