File: const-assignments.c

package info (click to toggle)
frama-c 20220511-manganese-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 66,492 kB
  • sloc: ml: 278,834; ansic: 47,093; sh: 4,823; makefile: 3,613; javascript: 2,436; python: 1,919; perl: 897; lisp: 293; xml: 62
file content (104 lines) | stat: -rw-r--r-- 1,404 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* run.config
   STDOPT:
 EXIT:1
   STDOPT: +" -cpp-extra-args=-DT0"
   STDOPT: +" -cpp-extra-args=-DT1"
   STDOPT: +" -cpp-extra-args=-DT2"
   STDOPT: +" -cpp-extra-args=-DT3"
   STDOPT: +" -cpp-extra-args=-DT4"
   STDOPT: +" -cpp-extra-args=-DT5"
 EXIT:0
   STDOPT: +" -cpp-extra-args=-DT6"
   STDOPT: +" -cpp-extra-args=-DT7"
 EXIT:1
   STDOPT: +" -cpp-extra-args=-DT8"
*/
/* The first run is correct. The others should fail, as they include invalid
   assignments to const lvalues. */
const int x = 1;
#ifdef T0
void f() {
  x = 42;
}
#endif

#ifdef T1
void f() {
  x++;
}
#endif

#ifdef T2
void f() {
  --x;
}
#endif

#ifdef T3
void f() {
  x += 3;
}
#endif

#ifdef T4
void f() {
  const int x = 2;
  x *= 2;
}
#endif

#ifdef T5
void f(const int* x) {
  *x = 1;
}
#endif

extern void g(int *p);

#ifdef T6
void f() {
  g(&x);
}
#endif

#ifdef T7
void f(const int* x) {
  g(x);
}
#endif

void h(const int* x) {
  int* y = (int *)x;
  *y = 1;
  g(y);
}

typedef struct {
 __attribute__((__fc_mutable)) int x;
 const int y;
} S;

void build_S(
  __attribute__((__fc_initialized_object)) const S* s, int x, int y)
{
  s->x = x;
  s->y=y;
}

void mutable_test(const S* s) {
  s->x = 42;
  s->x++;
  s->x += 2;
}

#ifdef T8

typedef struct {
__attribute__((__fc_mutable)) S s;
} T;

void mutable_test_ko(const T* t) {
  t->s.y = 32; // KO: although t->s could be modified, t->s.y is still const
}

#endif