aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/plugin/known-fns-1.c
blob: 5fa49f6a93fab8df65708ce485ceb920b458e3cf (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
/* { dg-do compile } */
/* { dg-options "-fanalyzer" } */
/* { dg-require-effective-target analyzer } */

#include "../analyzer/analyzer-decls.h"

/* Basic example of known fn behavior.  */

extern int returns_42 (void);

void test_1 (void)
{
  int val = returns_42 ();
  __analyzer_eval (val == 42); /* { dg-warning "TRUE" } */
}

/* Example of bifurcation, with a copy that can fail.  */

extern int
attempt_to_copy (void *to, const void *from, int sz);

void test_copy_success (void *to, const void *from, int sz)
{
  if (!attempt_to_copy (to, from, sz))
    {
      /* Success */
    }
}

void test_copy_failure (void *to, const void *from, int sz)
{
  if (attempt_to_copy (to, from, sz)) /* { dg-message "when 'attempt_to_copy' fails" } */
    __analyzer_dump_path (); /* { dg-message "path" } */
}

struct coord
{
  int x;
  int y;
  int z;
};

void test_copy_2 (void)
{
  struct coord to = {1, 2, 3};
  struct coord from = {4, 5, 6};
  if (attempt_to_copy (&to, &from, sizeof (struct coord)))
    {
      /* Failure.  */
      __analyzer_eval (to.x == 1); /* { dg-warning "TRUE" } */
      __analyzer_eval (to.y == 2); /* { dg-warning "TRUE" } */
      __analyzer_eval (to.z == 3); /* { dg-warning "TRUE" } */
    }
  else
    {
      /* Success.  */
      __analyzer_eval (to.x == 4); /* { dg-warning "TRUE" } */
      __analyzer_eval (to.y == 5); /* { dg-warning "TRUE" } */
      __analyzer_eval (to.z == 6); /* { dg-warning "TRUE" } */
    }
}