(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.misc-tests/
gcov-4.c
       1  /* Check that execution counts for various C constructs are reported
       2     correctly by gcov. */
       3  
       4  /* { dg-options "-fprofile-arcs -ftest-coverage" } */
       5  /* { dg-do run { target native } } */
       6  
       7  extern void abort (void);
       8  
       9  int do_something (int i)
      10  {
      11    return i;
      12  }
      13  
      14  /* Check static inline functions. */
      15  
      16  int unref_val;
      17  
      18  static inline int
      19  unreferenced (int i, int j)
      20  {
      21    return i - j;
      22  }
      23  
      24  static inline int
      25  uncalled (int i, int j)
      26  {
      27    return i * j;
      28  }
      29  
      30  static inline int
      31  called (int i, int j)
      32  {
      33      return i + j;			/* count(1) */
      34  }
      35  
      36  void
      37  call_unref ()
      38  {
      39    if (unref_val)			/* count(1) */
      40      unref_val = uncalled (1, 2);
      41    unref_val = called (unref_val, 4);	/* count(1) */
      42  }
      43  
      44  
      45  /* Check for loops. */
      46  
      47  int for_val1;
      48  int for_val2;
      49  int for_temp;
      50  
      51  int
      52  test_for1 (int n)
      53  {
      54    int i;
      55    for_temp = 1;				/* count(3) */
      56    for (i = 0; i < n; i++)
      57      for_temp++;				/* count(9) */
      58    return for_temp;			/* count(3) */
      59  }
      60  
      61  int
      62  test_for2 (int m, int n, int o)
      63  {
      64    int i, j, k;
      65    for_temp = 1;				/* count(6) */
      66    for (i = 0; i < n; i++)
      67      for (j = 0; j < m; j++)
      68        for (k = 0; k < o; k++)
      69  	for_temp++;			/* count(81) */
      70    return for_temp;			/* count(6) */
      71  }
      72  
      73  void
      74  call_for ()
      75  {
      76    for_val1 += test_for1 (0);
      77    for_val1 += test_for1 (2);
      78    for_val1 += test_for1 (7);
      79  
      80    for_val2 += test_for2 (0, 0, 0);
      81    for_val2 += test_for2 (1, 0, 0);
      82    for_val2 += test_for2 (1, 3, 0);
      83    for_val2 += test_for2 (1, 3, 1);
      84    for_val2 += test_for2 (3, 1, 5);
      85    for_val2 += test_for2 (3, 7, 3);
      86  }
      87  
      88  /* Check the use of goto. */
      89  
      90  int goto_val;
      91  
      92  int
      93  test_goto1 (int f)
      94  {
      95    if (f)				/* count(2) */
      96      goto lab1;				/* count(1) */
      97    return 1;				/* count(1) */
      98  lab1:
      99    return 2;				/* count(1) */
     100  }
     101  
     102  int
     103  test_goto2 (int f)
     104  {
     105    int i;
     106    for (i = 0; i < 10; i++)		/* count(15) */
     107      if (i == f) goto lab2;		/* count(14) */
     108    return 4;				/* count(1) */
     109  lab2:
     110    return 8;				/* count(1) */
     111  }
     112  
     113  int
     114  test_goto3 (int i, int j)
     115  {
     116      if (j) goto else_;		/* count(1) */
     117  
     118  top:
     119      if (i)			/* count(1) */
     120        {
     121  	i = do_something (i);
     122        }
     123      else
     124        {
     125  else_:				/* count(1) */
     126  	j = do_something (j);	/* count(2) */
     127  	if (j)			/* count(2) */
     128  	  {
     129  	    j = 0;		/* count(1) */
     130  	    goto top;		/* count(1) */
     131  	  }
     132        }
     133      return 16;
     134  }
     135  
     136  void
     137  call_goto ()
     138  {
     139    goto_val += test_goto1 (0);
     140    goto_val += test_goto1 (1);
     141    goto_val += test_goto2 (3);
     142    goto_val += test_goto2 (30);
     143    goto_val += test_goto3 (0, 1);
     144  }
     145  
     146  /* Check nested if-then-else statements. */
     147  
     148  int ifelse_val1;
     149  int ifelse_val2;
     150  int ifelse_val3;
     151  
     152  int
     153  test_ifelse1 (int i, int j)
     154  {
     155    int result = 0;
     156    if (i)				/* count(5) */
     157      if (j)				/* count(3) */
     158        result = do_something (4);	/* count(3) */
     159      else
     160        result = do_something (1024);
     161    else
     162      if (j)				/* count(2) */
     163        result = do_something (1);	/* count(1) */
     164      else
     165        result = do_something (2);	/* count(1) */
     166    if (i > j)				/* count(5) */
     167      result = do_something (result*2);	/* count(1) */
     168    if (i > 10)				/* count(5) */
     169      if (j > 10)				/* count(1) */
     170        result = do_something (result*4);	/* count(1) */
     171    return result;			/* count(5) */
     172  }
     173  
     174  int
     175  test_ifelse2 (int i)
     176  {
     177    int result = 0;
     178    if (!i)				/* count(6) */
     179      result = do_something (1);		/* count(1) */
     180    if (i == 1)				/* count(6) */
     181      result = do_something (1024);
     182    if (i == 2)				/* count(6) */
     183      result = do_something (2);		/* count(3) */
     184    if (i == 3)				/* count(6) */
     185      return do_something (8);		/* count(2) */
     186    if (i == 4)				/* count(4) */
     187      return do_something (2048);
     188    return result;			/* count(4) */
     189  }
     190  
     191  int
     192  test_ifelse3 (int i, int j)
     193  {
     194    int result = 1;
     195    if (i > 10 && j > i && j < 20)	/* count(11) */
     196      result = do_something (16);		/* count(1) */
     197    if (i > 20)				/* count(11) */
     198      if (j > i)				/* count(5) */
     199        if (j < 30)			/* count(2) */
     200  	result = do_something (32);	/* count(1) */
     201    if (i == 3 || j == 47 || i == j)	/* count(11) */
     202      result = do_something (64);		/* count(3) */
     203    return result;			/* count(11) */
     204  }
     205  
     206  void
     207  call_ifelse ()
     208  {
     209    ifelse_val1 += test_ifelse1 (0, 2);
     210    ifelse_val1 += test_ifelse1 (0, 0);
     211    ifelse_val1 += test_ifelse1 (1, 2);
     212    ifelse_val1 += test_ifelse1 (10, 2);
     213    ifelse_val1 += test_ifelse1 (11, 11);
     214  
     215    ifelse_val2 += test_ifelse2 (0);
     216    ifelse_val2 += test_ifelse2 (2);
     217    ifelse_val2 += test_ifelse2 (2);
     218    ifelse_val2 += test_ifelse2 (2);
     219    ifelse_val2 += test_ifelse2 (3);
     220    ifelse_val2 += test_ifelse2 (3);
     221  
     222    ifelse_val3 += test_ifelse3 (11, 19);
     223    ifelse_val3 += test_ifelse3 (25, 27);
     224    ifelse_val3 += test_ifelse3 (11, 22);
     225    ifelse_val3 += test_ifelse3 (11, 10);
     226    ifelse_val3 += test_ifelse3 (21, 32);
     227    ifelse_val3 += test_ifelse3 (21, 20);
     228    ifelse_val3 += test_ifelse3 (1, 2);
     229    ifelse_val3 += test_ifelse3 (32, 31);
     230    ifelse_val3 += test_ifelse3 (3, 0);
     231    ifelse_val3 += test_ifelse3 (0, 47);
     232    ifelse_val3 += test_ifelse3 (65, 65);
     233  }
     234  
     235  /* Check switch statements. */
     236  
     237  int switch_val, switch_m;
     238  
     239  int
     240  test_switch (int i, int j)
     241  {
     242    int result = 0;			/* count(5) */
     243  
     244    switch (i)				/* count(5) */
     245      {
     246        case 1:
     247          result = do_something (2);	/* count(1) */
     248          break;				/* count(1) */
     249        case 2:
     250          result = do_something (1024);
     251          break;
     252        case 3:
     253        case 4:
     254          if (j == 2)			/* count(3) */
     255            return do_something (4);	/* count(1) */
     256          result = do_something (8);	/* count(2) */
     257          break;				/* count(2) */
     258        default:
     259  	result = do_something (32);	/* count(1) */
     260  	switch_m++;			/* count(1) */
     261          break;
     262      }
     263    return result;			/* count(4) */
     264  }
     265  
     266  void
     267  call_switch ()
     268  {
     269    switch_val += test_switch (1, 0);
     270    switch_val += test_switch (3, 0);
     271    switch_val += test_switch (3, 2);
     272    switch_val += test_switch (4, 0);
     273    switch_val += test_switch (16, 0);	
     274    switch_val += switch_m;
     275  }
     276  
     277  int
     278  main()
     279  {
     280    call_for ();
     281    call_goto ();
     282    call_ifelse ();
     283    call_switch ();
     284    call_unref ();
     285    if ((for_val1 != 12)
     286        || (for_val2 != 87)
     287        || (goto_val != 31)
     288        || (ifelse_val1 != 31)
     289        || (ifelse_val2 != 23)
     290        || (ifelse_val3 != 246)
     291        || (switch_val != 55)
     292        || (unref_val != 4))
     293      abort ();
     294    return 0;
     295  }
     296  
     297  /* { dg-final { run-gcov gcov-4.c } } */