aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wstring-compare-3.c
blob: 6d0d5be9d779a770cd07d2f25e6138986d371a86 (plain)
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
/* PR middle-end/95673 - missing -Wstring-compare for an impossible strncmp test
   { dg-do compile }
   { dg-options "-O2 -Wall -Wstring-compare -Wno-stringop-overread -ftrack-macro-expansion=0" } */

typedef __SIZE_TYPE__ size_t;

extern int strcmp (const char*, const char*);
extern int strncmp (const char*, const char*, size_t);

void sink (int, ...);

extern char a3[3];

int nowarn_strcmp_one_use_ltz (int c)
{
  const char *s = c ? "1234" : a3;
  int n = strcmp (s, "123");
  return n < 0;
}


int nowarn_strcmp_one_use_eqnz (int c)
{
  const char *s = c ? "12345" : a3;
  int n = strcmp (s, "123");
  return n == 1;
}


int warn_strcmp_one_use_eqz (int c)
{
  const char *s = c ? "123456" : a3;
  int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
  return n == 0;                // { dg-message "in this expression" }
}


int warn_strcmp_one_use_bang (int c)
{
  const char *s = c ? "1234567" : a3;
  int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
  return !n;                    // { dg-message "in this expression" }
}


int warn_strcmp_one_use_bang_bang (int c)
{
  const char *s = c ? "12345678" : a3;
  int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
  return !!n;                   // { dg-message "in this expression" }
}


_Bool warn_one_use_bool (int c)
{
  const char *s = c ? "123456789" : a3;
  int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
  return (_Bool)n;              // { dg-message "in this expression" }
}


int warn_strcmp_one_use_cond (int c)
{
  const char *s = c ? "1234567890" : a3;
  int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
  return n ? 3 : 5;             // { dg-message "in this expression" }
}


int nowarn_strcmp_multiple_uses (int c)
{
  const char *s = c ? "1234" : a3;
  int n = strcmp (s, "123");
  sink (n < 0);
  sink (n > 0);
  sink (n <= 0);
  sink (n >= 0);
  sink (n + 1);
  return n;
}


int warn_strcmp_multiple_uses (int c)
{
  const char *s = c ? "12345" : a3;
  int n = strcmp (s, "123");    // { dg-warning "'strcmp' of a string of length 3 and an array of size 3 evaluates to nonzero" }
  sink (n < 0);
  sink (n > 0);
  sink (n <= 0);
  sink (n >= 0);
  sink (n == 0);                // { dg-message "in this expression" }
  return n;
}


int warn_strncmp_multiple_uses (int c)
{
  const char *s = a3;
  int n = strncmp (s, "1234", 4); // { dg-warning "'strncmp' of a string of length 4, an array of size 3 and bound of 4 evaluates to nonzero" }
  sink (n < 0);
  sink (n > 0);
  sink (n <= 0);
  sink (n >= 0);
  sink (n == 0);                // { dg-message "in this expression" }
  return n;
}