public inbox for cocci@systeme.lip6.fr
 help / color / mirror / Atom feed
* [Cocci] Adding names to arguments in function prototypes
@ 2012-12-05  9:25 Håkon Løvdal
  2012-12-05  9:28 ` Julia Lawall
  0 siblings, 1 reply; 5+ messages in thread
From: Håkon Løvdal @ 2012-12-05  9:25 UTC (permalink / raw)
  To: cocci

Hi. I want to add names to arguments in function prototypes where
only the type is specified, e.g. adding 'i' below:

-int foo(int);
+int foo(int i);


I have gotten it almost working like I want, but got some problem beyond
running on the most basic input code. The following works fine:

---START---

$?more abc.* parameter_wo_name.cocci
::::::::::::::
abc.c
::::::::::::::

#include "abc.h"

int foo(int i)
{
        return i;
}

int bar(int i)
{
        return i;
}

int abc(int a, int b, int c)
{
        return a + b + c;
}

::::::::::::::
abc.h
::::::::::::::

int foo(int);
int bar(int i);
int abc(int, int, int);

::::::::::::::
parameter_wo_name.cocci
::::::::::::::

/* Match function prototypes without parameter names */
@rule1@
identifier f;
type T1, T2;
parameter list[n] ps;
position p1;
@@

T1 f at p1(ps, T2, ...);

/* Match the corresponding function declaration */
@rule2@
type rule1.T1;
type rule1.T2;
identifier rule1.f;
parameter list[n] rule1.ps;
identifier id;
@@

T1 f(ps, T2 id, ...) {...}

/* Combine information from the above two rules to add the parameter
name to the function prototype */
@rule3@
type rule1.T1;
type rule1.T2;
identifier rule1.f;
parameter list[n] rule1.ps;
position rule1.p1;
identifier rule2.id;
@@

T1 f at p1(ps, T2
+id
, ...);

$?spatch --sp-file parameter_wo_name.cocci --recursive-includes -I
/usr/include abc.c
init_defs_builtins: /usr/share/coccinelle/standard.h
HANDLING: abc.c
a header file was modified: abc.h
diff =
--- ./abc.h
+++ /tmp/cocci-output-27689-53d466-abc.h
@@ -1,5 +1,5 @@

-int foo(int);
+int foo(int i);
 int bar(int i);
-int abc(int, int, int);
+int abc(int a, int, int);

$

---END---


As can be seen only the first parameter in abc is named, but this is
just a matter of running the script multiple times, so this is not a
big problem (although if anyone have a solution to have it done in one
operation, feel free to provide a solution).

However, if I start using other types than int I get in trouble:


$?more xyz.*
::::::::::::::
xyz.c
::::::::::::::

#include "xyz.h"

int xyz(int x, uint8_t y, int z)
{
        return x + y + z;
}

::::::::::::::
xyz.h
::::::::::::::
#include <stdint.h>

int xyz(int, uint8_t, int);

$?spatch --sp-file parameter_wo_name.cocci --recursive-includes -I
/usr/include xyz.c
init_defs_builtins: /usr/share/coccinelle/standard.h
HANDLING: xyz.c
(ONCE) TYPE: header gnu/stubs-32.h not found
Fatal error: exception Failure("empty list, max_min_ii_by_pos")
$?mkdir gnu
$?touch gnu/stubs-32.h
$?spatch --sp-file parameter_wo_name.cocci --recursive-includes -I
/usr/include -I . xyz.c
init_defs_builtins: /usr/share/coccinelle/standard.h
HANDLING: xyz.c
Fatal error: exception Failure("empty list, max_min_ii_by_pos")
$



Why is coccinelle failing here and how do I solve this?

BR H?kon L?vdal

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

* [Cocci] Adding names to arguments in function prototypes
  2012-12-05  9:25 [Cocci] Adding names to arguments in function prototypes Håkon Løvdal
@ 2012-12-05  9:28 ` Julia Lawall
  2012-12-05  9:31   ` Håkon Løvdal
  0 siblings, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2012-12-05  9:28 UTC (permalink / raw)
  To: cocci

Looking quickly, I think you need to inherit the parameter list length n
from one rule to the next.  In the rules after the first one, put rule1.n,
not just n.

julia

On Wed, 5 Dec 2012, H?kon L?vdal wrote:

> Hi. I want to add names to arguments in function prototypes where
> only the type is specified, e.g. adding 'i' below:
>
> -int foo(int);
> +int foo(int i);
>
>
> I have gotten it almost working like I want, but got some problem beyond
> running on the most basic input code. The following works fine:
>
> ---START---
>
> $?more abc.* parameter_wo_name.cocci
> ::::::::::::::
> abc.c
> ::::::::::::::
>
> #include "abc.h"
>
> int foo(int i)
> {
>         return i;
> }
>
> int bar(int i)
> {
>         return i;
> }
>
> int abc(int a, int b, int c)
> {
>         return a + b + c;
> }
>
> ::::::::::::::
> abc.h
> ::::::::::::::
>
> int foo(int);
> int bar(int i);
> int abc(int, int, int);
>
> ::::::::::::::
> parameter_wo_name.cocci
> ::::::::::::::
>
> /* Match function prototypes without parameter names */
> @rule1@
> identifier f;
> type T1, T2;
> parameter list[n] ps;
> position p1;
> @@
>
> T1 f at p1(ps, T2, ...);
>
> /* Match the corresponding function declaration */
> @rule2@
> type rule1.T1;
> type rule1.T2;
> identifier rule1.f;
> parameter list[n] rule1.ps;
> identifier id;
> @@
>
> T1 f(ps, T2 id, ...) {...}
>
> /* Combine information from the above two rules to add the parameter
> name to the function prototype */
> @rule3@
> type rule1.T1;
> type rule1.T2;
> identifier rule1.f;
> parameter list[n] rule1.ps;
> position rule1.p1;
> identifier rule2.id;
> @@
>
> T1 f at p1(ps, T2
> +id
> , ...);
>
> $?spatch --sp-file parameter_wo_name.cocci --recursive-includes -I
> /usr/include abc.c
> init_defs_builtins: /usr/share/coccinelle/standard.h
> HANDLING: abc.c
> a header file was modified: abc.h
> diff =
> --- ./abc.h
> +++ /tmp/cocci-output-27689-53d466-abc.h
> @@ -1,5 +1,5 @@
>
> -int foo(int);
> +int foo(int i);
>  int bar(int i);
> -int abc(int, int, int);
> +int abc(int a, int, int);
>
> $
>
> ---END---
>
>
> As can be seen only the first parameter in abc is named, but this is
> just a matter of running the script multiple times, so this is not a
> big problem (although if anyone have a solution to have it done in one
> operation, feel free to provide a solution).
>
> However, if I start using other types than int I get in trouble:
>
>
> $?more xyz.*
> ::::::::::::::
> xyz.c
> ::::::::::::::
>
> #include "xyz.h"
>
> int xyz(int x, uint8_t y, int z)
> {
>         return x + y + z;
> }
>
> ::::::::::::::
> xyz.h
> ::::::::::::::
> #include <stdint.h>
>
> int xyz(int, uint8_t, int);
>
> $?spatch --sp-file parameter_wo_name.cocci --recursive-includes -I
> /usr/include xyz.c
> init_defs_builtins: /usr/share/coccinelle/standard.h
> HANDLING: xyz.c
> (ONCE) TYPE: header gnu/stubs-32.h not found
> Fatal error: exception Failure("empty list, max_min_ii_by_pos")
> $?mkdir gnu
> $?touch gnu/stubs-32.h
> $?spatch --sp-file parameter_wo_name.cocci --recursive-includes -I
> /usr/include -I . xyz.c
> init_defs_builtins: /usr/share/coccinelle/standard.h
> HANDLING: xyz.c
> Fatal error: exception Failure("empty list, max_min_ii_by_pos")
> $
>
>
>
> Why is coccinelle failing here and how do I solve this?
>
> BR H?kon L?vdal
> _______________________________________________
> Cocci mailing list
> Cocci at systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>

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

* [Cocci] Adding names to arguments in function prototypes
  2012-12-05  9:28 ` Julia Lawall
@ 2012-12-05  9:31   ` Håkon Løvdal
  2012-12-05  9:36     ` Julia Lawall
  0 siblings, 1 reply; 5+ messages in thread
From: Håkon Løvdal @ 2012-12-05  9:31 UTC (permalink / raw)
  To: cocci

On 5 December 2012 10:28, Julia Lawall <julia.lawall@lip6.fr> wrote:
> Looking quickly, I think you need to inherit the parameter list length n
> from one rule to the next.  In the rules after the first one, put rule1.n,
> not just n.

Thanks, that sounds reasonable. However it made no difference for
neither abc.c nor xyz.c.

BR H?kon L?vdal

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

* [Cocci] Adding names to arguments in function prototypes
  2012-12-05  9:31   ` Håkon Løvdal
@ 2012-12-05  9:36     ` Julia Lawall
  2012-12-06 17:24       ` Christian Clausen
  0 siblings, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2012-12-05  9:36 UTC (permalink / raw)
  To: cocci

On Wed, 5 Dec 2012, H?kon L?vdal wrote:

> On 5 December 2012 10:28, Julia Lawall <julia.lawall@lip6.fr> wrote:
> > Looking quickly, I think you need to inherit the parameter list length n
> > from one rule to the next.  In the rules after the first one, put rule1.n,
> > not just n.
>
> Thanks, that sounds reasonable. However it made no difference for
> neither abc.c nor xyz.c.

OK, thanks.  I will look into it.

julia

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

* [Cocci] Adding names to arguments in function prototypes
  2012-12-05  9:36     ` Julia Lawall
@ 2012-12-06 17:24       ` Christian Clausen
  0 siblings, 0 replies; 5+ messages in thread
From: Christian Clausen @ 2012-12-06 17:24 UTC (permalink / raw)
  To: cocci

Hi H?kon,

The problem is in rule2 where you are inheriting the ps from rule1. The 
only value ps can have in both rule1 and rule2 is the list of named 
variables, and further more they need to have the same names. The fix is 
to remove the rule1.ps and use a new ps, while still having it be the 
same length as in rule one. It should be:
parameter list[rule1.n] ps;

As for the other problem, here is a patch that should fix it.

Best regards

Christian Clausen


On 05-12-2012 10:36, Julia Lawall wrote:
> On Wed, 5 Dec 2012, H?kon L?vdal wrote:
>
>> On 5 December 2012 10:28, Julia Lawall <julia.lawall@lip6.fr> wrote:
>>> Looking quickly, I think you need to inherit the parameter list length n
>>> from one rule to the next.  In the rules after the first one, put rule1.n,
>>> not just n.
>> Thanks, that sounds reasonable. However it made no difference for
>> neither abc.c nor xyz.c.
> OK, thanks.  I will look into it.
>
> julia
>
>
> _______________________________________________
> Cocci mailing list
> Cocci at systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20121206/90dea800/attachment.html>
-------------- next part --------------
diff --git a/parsing_c/parsing_hacks.ml b/parsing_c/parsing_hacks.ml
index 9d05efe..bd61492 100755
--- a/parsing_c/parsing_hacks.ml
+++ b/parsing_c/parsing_hacks.ml
@@ -1695,6 +1695,36 @@ let ident = function
     TIdent _ -> true
   | _ -> false
 
+let is_type = function
+  | TypedefIdent _ 
+  | Tvoid _
+  | Tchar _
+  | Tfloat _
+  | Tdouble _
+
+  | Tsize_t _
+  | Tssize_t _
+  | Tptrdiff_t _
+
+  | Tint _
+  | Tlong _
+  | Tshort _ -> true
+  | _ -> false
+
+let is_cparen = function (TCPar _) -> true | _ -> false
+let is_oparen = function (TOPar _) -> true | _ -> false
+
+let rec not_has_type_before f xs =
+  match xs with
+  | [] -> raise Impossible
+  | x :: xs ->
+      if f x then
+	true
+      else if is_type x then
+	false
+      else
+	not_has_type_before f xs
+
 (* This function is inefficient, because it will look over a K&R header,
 or function prototype multiple times.  At least when we see a , and are in a
 parameter list, we know we will eventually see a close paren, and it
@@ -1776,9 +1806,11 @@ let lookahead2 ~pass next before =
 
   (* [,(] xx [,)] AND param decl *)
   | (TIdent (s, i1)::(((TComma _|TCPar _)::_) as rest) ,
-     (TComma _ |TOPar _)::_ )
+     ((TComma _ |TOPar _)::_ as bef))
     when not_struct_enum before && (LP.current_context() =*= LP.InParameter)
       && k_and_r rest
+      && not_has_type_before is_cparen rest 
+      && not_has_type_before is_oparen bef
       ->
 	TKRParam(s,i1)
 

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

end of thread, other threads:[~2012-12-06 17:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-05  9:25 [Cocci] Adding names to arguments in function prototypes Håkon Løvdal
2012-12-05  9:28 ` Julia Lawall
2012-12-05  9:31   ` Håkon Løvdal
2012-12-05  9:36     ` Julia Lawall
2012-12-06 17:24       ` Christian Clausen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox