File: Wrestrict-26.c

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (114 lines) | stat: -rw-r--r-- 4,242 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
105
106
107
108
109
110
111
112
113
114
/* Verify that sprintf calls with arrays or struct of arrays don't
   cause -Wrestrict false positives.
   { dg-do compile }
   { dg-options "-O2 -Wall -Wrestrict -ftrack-macro-expansion=0" } */

#define sprintf(d, f, ...) (sprintf (d, f, __VA_ARGS__), sink (d))

extern void sink (void*, ...);
extern int (sprintf) (char*, const char*, ...);

extern char ca[][2][8];

void test_array_of_arrays (void)
{
  sprintf (ca[0][0], "%s", ca[0][0]);     // { dg-warning "-Wrestrict" }
  sprintf (ca[0][0], "%s", ca[0][1]);
  sprintf (ca[0][0], "%s", ca[1][0]);
  sprintf (ca[0][0], "%s", ca[1][1]);

  sprintf (ca[0][1], "%s", ca[0][0]);
  sprintf (ca[0][1], "%s", ca[0][1]);     // { dg-warning "-Wrestrict" }
  sprintf (ca[0][1], "%s", ca[1][0]);
  sprintf (ca[0][1], "%s", ca[1][1]);

  sprintf (ca[1][0], "%s", ca[0][0]);
  sprintf (ca[1][0], "%s", ca[0][1]);
  sprintf (ca[1][0], "%s", ca[1][0]);     // { dg-warning "-Wrestrict" }
  sprintf (ca[1][0], "%s", ca[1][1]);

  sprintf (ca[1][1], "%s", ca[0][0]);
  sprintf (ca[1][1], "%s", ca[0][1]);
  sprintf (ca[1][1], "%s", ca[1][0]);
  sprintf (ca[1][1], "%s", ca[1][1]);     // { dg-warning "-Wrestrict" }
}


struct A
{
  char a[2][2][8];
  char b[2][2][8];
  char c[2][2][8];
};

extern struct A aa[][2];

void test_array_of_structs (void)
{
  // Verify that calls with the same elements of the same array trigger
  // warnings as expected.
  sprintf (aa[0][0].a[0][0], "%s", aa[0][0].a[0][0]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[0][0].a[0][1], "%s", aa[0][0].a[0][1]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[0][0].a[1][0], "%s", aa[0][0].a[1][0]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[0][0].a[1][1], "%s", aa[0][0].a[1][1]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[0][1].a[0][0], "%s", aa[0][1].a[0][0]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[0][1].a[0][1], "%s", aa[0][1].a[0][1]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[0][1].a[1][0], "%s", aa[0][1].a[1][0]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[0][1].a[1][1], "%s", aa[0][1].a[1][1]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[1][0].a[0][0], "%s", aa[1][0].a[0][0]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[1][0].a[0][1], "%s", aa[1][0].a[0][1]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[1][0].a[1][0], "%s", aa[1][0].a[1][0]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[1][0].a[1][1], "%s", aa[1][0].a[1][1]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[1][1].a[0][0], "%s", aa[1][1].a[0][0]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[1][1].a[0][1], "%s", aa[1][1].a[0][1]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[1][1].a[1][0], "%s", aa[1][1].a[1][0]);     // { dg-warning "-Wrestrict" }
  sprintf (aa[1][1].a[1][1], "%s", aa[1][1].a[1][1]);     // { dg-warning "-Wrestrict" }

#define NOWARN()

  // Exhaustively verify that calls with different elements of the same
  // array don't cause false positives.
#undef NOWARN
#define NOWARN(D, S)				\
  sprintf (D[0][0], "%s", S[0][0]);		\
  sprintf (D[0][0], "%s", S[0][1]);		\
  sprintf (D[0][0], "%s", S[1][0]);		\
  sprintf (D[0][0], "%s", S[1][1]);		\
  sprintf (D[0][1], "%s", S[0][0]);		\
  sprintf (D[0][1], "%s", S[0][1]);		\
  sprintf (D[0][1], "%s", S[1][0]);		\
  sprintf (D[0][1], "%s", S[1][1]);		\
  sprintf (D[1][0], "%s", S[0][0]);		\
  sprintf (D[1][0], "%s", S[0][1]);		\
  sprintf (D[1][0], "%s", S[1][0]);		\
  sprintf (D[1][0], "%s", S[1][1]);		\
  sprintf (D[1][1], "%s", S[0][0]);		\
  sprintf (D[1][1], "%s", S[0][1]);		\
  sprintf (D[1][1], "%s", S[1][0]);		\
  sprintf (D[1][1], "%s", S[1][1])

  NOWARN (aa[0][0].a, aa[0][1].a);
  NOWARN (aa[0][0].a, aa[1][0].a);
  NOWARN (aa[0][0].a, aa[1][1].a);

  NOWARN (aa[0][1].a, aa[0][0].a);
  NOWARN (aa[0][1].a, aa[1][0].a);
  NOWARN (aa[0][1].a, aa[1][1].a);

  NOWARN (aa[1][0].a, aa[0][0].a);
  NOWARN (aa[1][0].a, aa[0][1].a);
  NOWARN (aa[1][0].a, aa[1][1].a);

#define NOWARN_MEM(M1, M2)			\
  NOWARN (aa[0][0].M1, aa[0][0].M2);		\
  NOWARN (aa[0][0].M1, aa[0][1].M2);		\
  NOWARN (aa[0][0].M1, aa[1][0].M2);		\
  NOWARN (aa[0][0].M1, aa[1][1].M2)

  NOWARN_MEM (a, b);
  NOWARN_MEM (a, c);
  NOWARN_MEM (b, a);
  NOWARN_MEM (b, c);
  NOWARN_MEM (c, a);
  NOWARN_MEM (c, b);
}