(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
attr-copy-7.c
       1  /* PR middle-end/88546 - Copy attribute unusable for weakrefs
       2     Verify that attribute noreturn (represented as volatile on function
       3     decls) is interpreted correctly and doesn't affect variables.
       4     { dg-do compile }
       5     { dg-options "-O1 -Wall -fdump-tree-optimized" }*/
       6  
       7  #define ATTR(...)   __attribute__ ((__VA_ARGS__))
       8  #define ASRT(expr)   _Static_assert (expr, #expr)
       9  
      10  ATTR (noreturn) void fnoreturn (void);
      11  ATTR (copy (fnoreturn)) void fnoreturn_copy (void);
      12  ASRT (__builtin_has_attribute (fnoreturn_copy, noreturn));
      13  
      14  int call_fnoreturn_copy (void)
      15  {
      16    fnoreturn_copy ();
      17    fnoreturn_copy ();   // should be eliminated
      18  }
      19  
      20  // { dg-final { scan-tree-dump-times "fnoreturn_copy \\(\\);" 1 "optimized" } }
      21  
      22  
      23  _Noreturn void f_Noreturn (void);
      24  ATTR (copy (f_Noreturn)) void f_Noreturn_copy (void);
      25  ASRT (__builtin_has_attribute (f_Noreturn_copy, noreturn));
      26  
      27  int call_f_Noreturn_copy (void)
      28  {
      29    f_Noreturn_copy ();
      30    f_Noreturn_copy ();   // should be eliminated
      31  }
      32  
      33  // { dg-final { scan-tree-dump-times "f_Noreturn_copy \\(\\);" 1 "optimized" } }
      34  
      35  
      36  // Verify the combination of both is accepted and works too,
      37  // just for fun.
      38  ATTR (noreturn) _Noreturn void fnoreturn_Noreturn (void);
      39  ATTR (copy (fnoreturn_Noreturn)) void fnoreturn_Noreturn_copy (void);
      40  ASRT (__builtin_has_attribute (fnoreturn_Noreturn_copy, noreturn));
      41  
      42  int call_fnoreturn_Noreturn_copy (void)
      43  {
      44    fnoreturn_Noreturn_copy ();
      45    fnoreturn_Noreturn_copy ();   // should be eliminated
      46  }
      47  
      48  // { dg-final { scan-tree-dump-times "fnoreturn_Noreturn_copy \\(\\);" 1 "optimized" } }
      49  
      50  
      51  typedef void func_t (void);
      52  
      53  ATTR (noreturn) func_t func_noreturn;
      54  ATTR (copy (func_noreturn)) func_t func_noreturn_copy;
      55  ASRT (__builtin_has_attribute (func_noreturn_copy, noreturn));
      56  
      57  int call_func_noreturn_copy (void)
      58  {
      59    func_noreturn_copy ();
      60    func_noreturn_copy ();   // should be eliminated
      61  }
      62  
      63  // { dg-final { scan-tree-dump-times "func_noreturn_copy \\(\\);" 1 "optimized" } }
      64  
      65  
      66  // Finally, verify that the volatile bit isn't copied for variables.
      67  extern volatile int vi;
      68  
      69  int read_nonvolatile (void)
      70  {
      71    ATTR (copy (vi)) int i = 0;
      72  
      73    return i + i;   // should be folded to return 0;
      74  }
      75  
      76  // { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } }