Skip to contentSkip to conversation

parsing_c: Handle case of macro before typedef

PATCH In review Jaskaran Singh 1 participant 2 messages Jan 19, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 parsing_c: Handle case of macro before typedef

For the following case:

<macro> <typedef> <identifier>

A case in parsing_hacks.ml sometimes mislabels <macro> as a typedef ident.

If <typedef> is a known typedef (such as u8 or *_t), then label <macro> as a CppMacro. Subsequent cases will then label <typedef> correctly.

Following is a diff of --parse-c on the Linux Kernel v5.5-rc4:
Before
---
After

nb good = 18956150, nb passed = 134061 =========> 0.70% passed
---
nb good = 18956150, nb passed = 134062 =========> 0.70% passed

---
 parsing_c/parsing_hacks.ml | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
parsing_c/parsing_hacks.ml +33 -0
index 8374731b..685a4908 100644
@@ -2184,6 +2184,39 @@ let lookahead2 ~pass next before =
       && ok_typedef s && is_macro s2 && is_type type_
         ->
 	  TIdent (s, i1)
+
+  (* xx yy zz : xx is a macro *)
+  | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _)
+	when not_struct_enum before
+        && ok_typedef s2
+        && is_known_typdef s2
+        ->
+	  TCommentCpp(Token_c.CppMacro, i1)
+
+  (* xx yy zz : xx is a typedef ident *)
+  | (TIdent (s, i1)::TIdent (s2, i2)::TIdent(_,_)::_ , _)
+	when not_struct_enum before
+        && ok_typedef s
+        ->
+      msg_typedef s i1 2; LP.add_typedef_root s i1;
+      TypedefIdent (s, i1)
+
+  (* xx yy * zz : xx is a macro *)
+  | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _)
+    when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr
+	&& not_struct_enum before
+	&& ok_typedef s2 && is_known_typdef s2
+        ->
+	  TCommentCpp(Token_c.CppMacro, i1)
+
+  (* xx yy * zz : xx is a typedef ident *)
+  | (TIdent (s, i1)::TIdent (s2, i2)::ptr , _)
+    when pointer ~followed_by:(function TIdent _ -> true | _ -> false) ptr && not_struct_enum before
+	&& ok_typedef s
+        ->
+      msg_typedef s i1 2; LP.add_typedef_root s i1;
+      TypedefIdent (s, i1)
+
   (* xx yy *)
   | (TIdent (s, i1)::TIdent (s2, i2)::rest  , _) when not_struct_enum before
 	&& ok_typedef s && not (is_macro_paren s2 rest)
Signature · 6 lines
2.21.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#1 lore raw
Quoted 16 lines · Jaskaran Singh

For the following case:

<macro> <typedef> <identifier>

A case in parsing_hacks.ml sometimes mislabels <macro> as a typedef ident.

If <typedef> is a known typedef (such as u8 or *_t), then label <macro> as a CppMacro. Subsequent cases will then label <typedef> correctly.

Following is a diff of --parse-c on the Linux Kernel v5.5-rc4:
Before
---
After

Quoted 5 lines

nb good = 18956150, nb passed = 134061 =========> 0.70% passed
---
nb good = 18956150, nb passed = 134062 =========> 0.70% passed

Whoops, ignore this. The commit message comes out badly.

Thanks,
Jaskaran.

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci