Debian Codegraph @

gcc/testsuite/g++.dg/contracts/contracts-assume2.C

raw
1 // ensure that assert contracts can be turned into compile time assumptions
2 // and that they can be used for optimization.
3 //
4 // Even though x == -1, the assert contract tells the compiler that it is
5 // safe to assume the x <= 0 branch is never taken fun can be transformed into
6 // just
7 // printf("%d: test x>0\n", x);
8 // return 0;
9 // we ensure this by matching on the output and expecting a 0 return code from
10 // main -- unlike contracts-ignore2 which expects a failing return code
11 // { dg-do run }
12 // { dg-options "-std=c++2a -fcontracts -fcontract-role=default:never,assume,ignore -O1" }
13 // { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
14 #include <cstdio>
15
16 int fun(int x) {
17 [[assert audit: x > 0]];
18 if(x <= 0)
19 {
20 printf("%d: test x<=0 opt out\n", x);
21 return -1;
22 }
23 else
24 {
25 printf("%d: test x>0\n", x);
26 return 0;
27 }
28 }
29
30 int main(int, char**) {
31 volatile int x = -1;
32 return fun(x);
33 }
34
35 // { dg-output "-1: test x>0(\n|\r\n|\r)" }