File: strlenopt-36.c

package info (click to toggle)
gcc-riscv64-unknown-elf 8.3.0.2019.08%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 680,956 kB
  • sloc: ansic: 3,237,715; cpp: 896,882; ada: 772,854; f90: 144,254; asm: 68,788; makefile: 67,456; sh: 29,743; exp: 28,045; objc: 15,273; fortran: 11,885; python: 7,369; pascal: 5,375; awk: 3,725; perl: 2,872; yacc: 316; xml: 311; ml: 285; lex: 198; haskell: 122
file content (86 lines) | stat: -rw-r--r-- 2,460 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
/* PR tree-optimization/78450 - strlen(s) return value can be assumed
   to be less than the size of s
   { dg-do compile }
   { dg-options "-O2 -fdump-tree-optimized" } */

#include "strlenopt.h"

extern char a7[7], a6[6], a5[5], a4[4], a3[3], a2[2], a1[1];
extern char a0[0];   /* Intentionally not tested here.  */
extern char ax[];    /* Same.  */

struct MemArrays {
  char a7[7], a6[6], a5[5], a4[4], a3[3], a2[2], a1[1];
  char a0[0];   /* Not tested here.  */
};

struct NestedMemArrays {
  struct { char a7[7]; } ma7;
  struct { char a6[6]; } ma6;
  struct { char a5[5]; } ma5;
  struct { char a4[4]; } ma4;
  struct { char a3[3]; } ma3;
  struct { char a2[2]; } ma2;
  struct { char a1[1]; } ma1;
  struct { char a0[0]; } ma0;
  char last;
};

extern void failure_on_line (int);

#define TEST_FAIL(line)					\
  do {							\
    failure_on_line (line);				\
  } while (0)

#define T(expr)						\
  if (!(expr)) TEST_FAIL (__LINE__); else (void)0


void test_array (void)
{
  T (strlen (a7) < sizeof a7);
  T (strlen (a6) < sizeof a6);
  T (strlen (a5) < sizeof a5);
  T (strlen (a4) < sizeof a4);
  T (strlen (a3) < sizeof a3);

  /* The following two calls are folded too early which defeats
     the strlen() optimization.
    T (strlen (a2) == 1);
    T (strlen (a1) == 0);  */
}

void test_memarray (struct MemArrays *ma)
{
  T (strlen (ma->a7) < sizeof ma->a7);
  T (strlen (ma->a6) < sizeof ma->a6);
  T (strlen (ma->a5) < sizeof ma->a5);
  T (strlen (ma->a4) < sizeof ma->a4);
  T (strlen (ma->a3) < sizeof ma->a3);

  /* The following two calls are folded too early which defeats
     the strlen() optimization.
  T (strlen (ma->a2) == 1);
  T (strlen (ma->a1) == 0);  */
}

/* Verify that the range of strlen(A) of a last struct member is
   set even when the array is the sole member of a struct as long
   as the struct itself is a member of another struct.  The converse
   is tested in stlenopt-37.c.  */
void test_nested_memarray (struct NestedMemArrays *ma)
{
  T (strlen (ma->ma7.a7) < sizeof ma->ma7.a7);
  T (strlen (ma->ma6.a6) < sizeof ma->ma6.a6);
  T (strlen (ma->ma5.a5) < sizeof ma->ma5.a5);
  T (strlen (ma->ma4.a4) < sizeof ma->ma4.a4);
  T (strlen (ma->ma3.a3) < sizeof ma->ma3.a3);

  /* The following two calls are folded too early which defeats
     the strlen() optimization.
  T (strlen (ma->ma2.a2) == 1);
  T (strlen (ma->ma1.a1) == 0);  */
}

/* { dg-final { scan-tree-dump-not "failure_on_line" "optimized" } } */