linux-kernel-mentees.lists.linuxfoundation.org archive mirror
 help / color / mirror / Atom feed
* [Linux-kernel-mentees] [PATCH] parsing_c: handle space and ctx newline around minus toks
@ 2019-11-19  9:34 Jaskaran Singh
  0 siblings, 0 replies; only message in thread
From: Jaskaran Singh @ 2019-11-19  9:34 UTC (permalink / raw)
  To: cocci; +Cc: julia.lawall, linux-kernel-mentees

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 <jaskaransingh7654321@gmail.com>
---
 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

_______________________________________________
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] only message in thread

only message in thread, other threads:[~2019-11-19  9:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-19  9:34 [Linux-kernel-mentees] [PATCH] parsing_c: handle space and ctx newline around minus toks Jaskaran Singh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).