PR tree-optimization/81303
[official-gcc.git] / gcc / testsuite / gcc.dg / const-1.c
bloba5b2b1677283bdddf0c44b167f3c5469e9e653f7
1 /* { dg-do compile { target nonpic } } */
2 /* { dg-options "-O2 -Wsuggest-attribute=const" } */
4 extern int extern_const(int a) __attribute__ ((const));
6 /* Trivial. */
7 int
8 foo1(int a) /* { dg-bogus "normally" "detect const candidate" } */
9 { /* { dg-warning "const" "detect const candidate" { target *-*-* } "8" } */
10 return extern_const (a);
13 /* Loops known to be normally and extern const calls should be safe. */
15 int __attribute__ ((noinline))
16 foo2(int n) /* { dg-bogus "normally" "detect const candidate" } */
17 { /* { dg-warning "const" "detect const candidate" { target *-*-* } "16" } */
18 int ret = 0;
19 int i;
20 for (i=0; i<n; i++)
21 ret+=extern_const (i);
22 return ret;
25 /* No warning here; we can work it by ourselves. */
26 static int __attribute__ ((noinline))
27 foo2b(int n)
29 int ret = 0;
30 int i;
31 for (i=0; i<n; i++)
32 ret+=extern_const (i);
33 return ret;
36 /* Unbounded loops are not safe. */
37 static int __attribute__ ((noinline))
38 foo3(unsigned int n) /* { dg-warning "const\[^\n\]* normally" "detect const candidate" } */
40 int ret = 0;
41 unsigned int i;
42 for (i=0; extern_const (i+n); n++)
43 ret+=extern_const (i);
44 return ret;
47 int
48 foo4(int n) /* { dg-warning "const\[^\n\]* normally" "detect const candidate" } */
50 return foo3(n) + foo2b(n);
53 int
54 foo5(int n) /* { dg-bogus "normally" "detect const candidate" } */
55 { /* { dg-warning "const" "detect const candidate" { target *-*-* } "54" } */
56 return foo2(n);