summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wreturn-local-addr-7.c
blob: ac1fb769ba8dccbf32a7dea2d1ccc3eae1cf308e (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
/* Test to verify that a PHI with a COND_EXPR argument in a return
   statement is handled correctly.
  { dg-do compile }
  { dg-options "-O2 -Wall" } */

extern struct S s;

void* f (int n)
{
  void *p;
  int x = 0;

  for (int i = n; i >= 0; i--)
    {
      p = &s;
      if (p == (void*)-1)
         x = 1;
      else if (p)
         return p;
    }

  /* The return statement below ends up with the following IL:
     <bb 6> [local count: 59055800]:
     # x_10 = PHI <1(5), 0(2)>
     _5 = x_10 != 0 ? -1B : 0B;

     <bb 7> [local count: 114863532]:
     # _3 = PHI <&s(4), _5(6), &s(3)>
     return _3;  */
  return x ? (void*)-1 : 0;
}

void* g (int n)
{
  void *p;
  int x = 0;                  /* { dg-message "declared here" } */

  for (int i = n; i >= 0; i--)
    {
      p = &s;
      if (p == (void*)-1)
         x = 1;
      else if (p)
         return p;
    }

  /* The return statement below does not reference a COND_EXPR argument.  */
  return x ? &x : 0;          /* { dg-warning "may return address of local variable" "missing location" { xfail *-*-* } } */
  /* { dg-warning "may return address of local variable" "pr90735" { target *-*-* } 0 } */
}