Skip to contentSkip to conversation

How to remove unused global variables by using Coccinelle?

Cocci 杨已彪 2 participants 5 messages Oct 13, 2017

lore
Share & tools
lore

杨已彪

author
#0 lore raw

Hello,

Thanks for this awesome tool - Coccinelle.

Currently, I am trying to use Coccinelle to remove unused global variables in C source code. While applying the following remove.cocci to test.c , it does not work.

Can anyone help?

Thanks.
---
Yibiao

$ spatch --version
spatch version 1.0.6 compiled with OCaml version 4.02.3
Flags passed to the configure script: [none]
Python scripting support: yes
Syntax of regular expresssions: PCRE
$ spatch ?in-place -sp-file remove.cocci small.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
HANDLING: small.c
$ cat remove.cocci
@@
type T;
identifier i;
expression E;
@@
- static T i = E;
  ... when != I
$ cat small.c
static int g = 0;
int main()
{
    return 0;
}
#1 lore raw
Quoted 22 lines · ???

Hello,

Thanks for this awesome tool - Coccinelle.

Currently, I am trying to use Coccinelle to remove unused global variables in C source code. While applying the following remove.cocci to test.c , it does not work.

Can anyone help?

Thanks.
---
Yibiao

$ spatch --version
spatch version 1.0.6 compiled with OCaml version 4.02.3
Flags passed to the configure script: [none]
Python scripting support: yes
Syntax of regular expresssions: PCRE
$ spatch ?in-place -sp-file remove.cocci small.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
HANDLING: small.c
Quoted 8 lines
$ cat remove.cocci
@@
type T;
identifier i;
expression E;
@@
- static T i = E;
  ... when != I

This doesn't work for various reasons. First Coccinelle is case sensitive, so i is different than I. Also Coccinelle works on one function at a time. So you can't use ... to refer to the rest of the file. You want something like:

@r@
type T;
identifier i;
expression E;
@@
static T i = E;

@useful@
identifier r.i;
@@

i

@depends on !useful@
identifier r.i;
@@
- static T i = E;

julia

Quoted 12 lines
$ cat small.c
static int g = 0;
int main()
{
    return 0;
}
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
author
#2 lore raw

Thank you very much. It works.

Quoted 52 lines

? 2017?10?13????9:59?Julia Lawall <[email protected]> ???

Quoted 22 lines · ???

Hello,

Thanks for this awesome tool - Coccinelle.

Currently, I am trying to use Coccinelle to remove unused global variables in C source code. While applying the following remove.cocci to test.c , it does not work.

Can anyone help?

Thanks.
---
Yibiao

$ spatch --version
spatch version 1.0.6 compiled with OCaml version 4.02.3
Flags passed to the configure script: [none]
Python scripting support: yes
Syntax of regular expresssions: PCRE
$ spatch ?in-place -sp-file remove.cocci small.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
HANDLING: small.c
Quoted 8 lines
$ cat remove.cocci
@@
type T;
identifier i;
expression E;
@@
- static T i = E;
 ... when != I

This doesn't work for various reasons. First Coccinelle is case sensitive, so i is different than I. Also Coccinelle works on one function at a time. So you can't use ... to refer to the rest of the file. You want something like:

@r@
type T;
identifier i;
expression E;
@@
static T i = E;

@useful@
identifier r.i;
@@

Quoted 20 lines

i

@depends on !useful@
identifier r.i;
@@
- static T i = E;

julia

Quoted 11 lines
$ cat small.c
static int g = 0;
int main()
{
   return 0;
}
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr <mailto:[email protected]>
https://systeme.lip6.fr/mailman/listinfo/cocci <https://systeme.lip6.fr/mailman/listinfo/cocci>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20171013/0920e5b5/attachment-0001.html>

author
#3 lore raw
Quoted 77 lines

? 2017?10?13????10:09???? <[email protected]> ???

Thank you very much. It works.

Quoted 72 lines

? 2017?10?13????9:59?Julia Lawall <julia.lawall at lip6.fr <mailto:[email protected]>> ???

Quoted 22 lines · ???

Hello,

Thanks for this awesome tool - Coccinelle.

Currently, I am trying to use Coccinelle to remove unused global variables in C source code. While applying the following remove.cocci to test.c , it does not work.

Can anyone help?

Thanks.
---
Yibiao

$ spatch --version
spatch version 1.0.6 compiled with OCaml version 4.02.3
Flags passed to the configure script: [none]
Python scripting support: yes
Syntax of regular expresssions: PCRE
$ spatch ?in-place -sp-file remove.cocci small.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
HANDLING: small.c
Quoted 8 lines
$ cat remove.cocci
@@
type T;
identifier i;
expression E;
@@
- static T i = E;
 ... when != I

This doesn't work for various reasons. First Coccinelle is case sensitive, so i is different than I. Also Coccinelle works on one function at a time. So you can't use ... to refer to the rest of the file. You want something like:

@r@
type T;
identifier i;
expression E;
@@
static T i = E;

@useful@
identifier r.i;
@@

i

@depends on !useful@
identifier r.i;
@@
- static T i = E;

julia

Quoted 11 lines
$ cat small.c
static int g = 0;
int main()
{
   return 0;
}
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr <mailto:[email protected]>
https://systeme.lip6.fr/mailman/listinfo/cocci <https://systeme.lip6.fr/mailman/listinfo/cocci>

Btw, how to distinguish global and local variables?
In the following, the output include both global variables, functions names, and local variables.

What if I only want to output global variables? Thanks.

@g@
type T;
identifier i;
@@
T i;

@script:python depends on g@
i << g.i;
@@
print I

??
Yibiao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20171013/08137e33/attachment.html>

author
#4 lore raw
Quoted 93 lines

? 2017?10?13????11:12???? <[email protected]> ???

Quoted 76 lines

? 2017?10?13????10:09???? <yangyibiao at nju.edu.cn <mailto:[email protected]>> ???

Thank you very much. It works.

Quoted 72 lines

? 2017?10?13????9:59?Julia Lawall <julia.lawall at lip6.fr <mailto:[email protected]>> ???

Quoted 22 lines · ???

Hello,

Thanks for this awesome tool - Coccinelle.

Currently, I am trying to use Coccinelle to remove unused global variables in C source code. While applying the following remove.cocci to test.c , it does not work.

Can anyone help?

Thanks.
---
Yibiao

$ spatch --version
spatch version 1.0.6 compiled with OCaml version 4.02.3
Flags passed to the configure script: [none]
Python scripting support: yes
Syntax of regular expresssions: PCRE
$ spatch ?in-place -sp-file remove.cocci small.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
HANDLING: small.c
Quoted 8 lines
$ cat remove.cocci
@@
type T;
identifier i;
expression E;
@@
- static T i = E;
 ... when != I

This doesn't work for various reasons. First Coccinelle is case sensitive, so i is different than I. Also Coccinelle works on one function at a time. So you can't use ... to refer to the rest of the file. You want something like:

@r@
type T;
identifier i;
expression E;
@@
static T i = E;

@useful@
identifier r.i;
@@

i

@depends on !useful@
identifier r.i;
@@
- static T i = E;

julia

Quoted 11 lines
$ cat small.c
static int g = 0;
int main()
{
   return 0;
}
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr <mailto:[email protected]>
https://systeme.lip6.fr/mailman/listinfo/cocci <https://systeme.lip6.fr/mailman/listinfo/cocci>

Btw, how to distinguish global and local variables?
In the following, the output include both global variables, functions names, and local variables.

What if I only want to output global variables? Thanks.

@g@
type T;
identifier i;
@@
T i;

Quoted 8 lines

@script:python depends on g@
i << g.i;
@@
print I

??
Yibiao

Solved by using the global idexpression! Thank you all the same.

@g@
type T;
global idexpression x;
@@
x

@useful@
global idexpression g.x;
@@
x(...)

@script:python depends on g && !useful@
x << g.x;
@@
print x

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20171013/3c3c0ffe/attachment-0001.html>