Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mem leak
[simgrid.git] / src / smpi / replace_globals.cocci
1 // FIXME: seems like cocci has problems manipulating the declarations, at least
2 // when there is more than one on the same line. We already need perl to split
3 // up the declarations after the fact, is there another tool we can use to patch
4 // up and match the declarations? In that case we could consider dropping cocci,
5 // or just using it to alter global variable accesses.
6 //
7 // FIXME: problems
8 //   - array declarations not properly matched...can fix, but then can't have
9 //   multiple declarations on one line
10 //   - does not match array initializers
11 //   - probably won't fix structure declarations with initialization either
12
13 // Function prototype looks like variable dec, but has parentheses
14 @funcproto@
15 type T;
16 identifier func;
17 position p;
18 @@
19 T@p func(...);
20
21 // define a local variable declaration as one at some level of nesting
22 @localvardecl@
23 type T;
24 identifier var;
25 position p;
26 expression E;
27 @@
28 <...
29 ( // default case
30 T@p
31 var
32 ;
33 | // variable has initializer
34 T@p
35 var = E
36 ;
37 )
38 ...>
39
40 // define a global variable declaration as one that is neither a function
41 // prototype nor a local variable declaration
42 @globalvardecl@
43 type T;
44 identifier var;
45 position p != { funcproto.p, localvardecl.p };
46 expression value;
47 // expression size;
48 @@
49 ( // default case
50 T@p 
51 - var
52 + *var = SMPI_VARINIT_GLOBAL(var, T)
53 ;
54 | // variable has initializer (not a struct or array)
55 T@p
56 - var = value 
57 + *var = SMPI_VARINIT_GLOBAL_AND_SET(var, T, value)
58 ;
59 //| // array of specified size
60 //T@p // FIXME: matches, but complains if more than one decl on a line...
61 //- var[size]
62 //+ *var[size] = SMPI_VARINIT_GLOBAL_ARRAY(T, size)
63 //;
64 //| // array of specified size with initializer
65 //T@p // FIXME: how to match initializer?
66 //- var[size] = { ... }
67 //+ *var[] = SMPI_VARINIT_GLOBAL_ARRAY_AND_SET(T, size, { ... })
68 //;
69 //| // array without specified size, but with initializer
70 //T@p // FIXME: how to match initializer? how to figure out size?
71 //- var[] = { ... }
72 //+ *var[] = SMPI_VARINIT_GLOBAL_ARRAY_AND_SET(T, size, { ... }) // size = ?
73 //;
74 //| struct with initializer?
75 )
76
77 // rewrite access to global variables based on name, but avoid the declaration
78 // and local variables that might have the same name
79 @rewriteglobalaccess@
80 type T;
81 local idexpression lvar;
82 identifier globalvardecl.var;
83 @@
84 <...
85 ( // local variable
86 lvar
87 | // rewrite access
88 +SMPI_VARGET_GLOBAL(
89 var
90 +)
91 )
92 ...>
93
94 // define a local static variable declaration as one at some level of nesting
95 // starting with the word static (exceptions?)
96 @staticvardecl@
97 type T;
98 identifier func, var;
99 expression value;
100 @@
101 func(...) {
102 ...
103 ( // default case
104 static T
105 - var
106 + *var = SMPI_VARINIT_STATIC(T, var)
107 ;
108 | // variable has initializer (not a struct or array)
109 T
110 - var = value 
111 + *var = SMPI_VARINIT_STATIC_AND_SET(var, T, value)
112 ;
113 )
114 ...
115 }
116
117 // 
118 @rewritestaticaccess@
119 type T;
120 identifier staticvardecl.func, staticvardecl.var;
121 @@
122 func(...) {
123 <...
124 ( // declaration
125 T
126 var
127 ;
128 | // rewrite access
129 +SMPI_VARGET_STATIC(
130 var
131 +)
132 )
133 ...>
134 }