(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strlenopt-59.c
       1  /* Verify that strlen() calls with constant conditional expressions are
       2     eliminated as expected.
       3  
       4     { dg-do compile }
       5     { dg-options "-O1 -fdump-tree-optimized" }  */
       6  
       7  extern void abort (void);
       8  extern __SIZE_TYPE__ strlen (const char*);
       9  
      10  
      11  #define CAT(x, y) x ## y
      12  #define CONCAT(x, y) CAT (x, y)
      13  #define FAILNAME(name) CONCAT (call_ ## name ##_on_line_, __LINE__)
      14  
      15  #define FAIL(name) do {				\
      16      extern void FAILNAME (name) (void);		\
      17      FAILNAME (name)();				\
      18    } while (0)
      19  
      20  /* Macros to emit a call to funcation named
      21       call_failed_to_be_eliminated_on_line_NNN()
      22     for each call that's expected to be eliminated.  The dg-final
      23     scan-tree-dump-time directive at the bottom of the test verifies
      24     that no such call appears in output.  */
      25  #define ELIM(expr) \
      26    if ((expr)) FAIL (test_not_eliminated); else (void)0
      27  
      28  extern char a3[3];
      29  extern char a7[7];
      30  
      31  struct MemArrays { char a[7], b[9]; };
      32  
      33  struct MemArrays ma;
      34  
      35  void test_elim_condexpr (int i)
      36  {
      37    ELIM (6 < strlen (i ? "" : "123456"));
      38    ELIM (6 < strlen (i ? "123456" : ""));
      39  
      40    ELIM (4 < strlen (i < 1 ? "a" : i == 1 ? "ab" : "abc"));
      41  
      42    ELIM (3 < strlen (i ? "" : a3));
      43    ELIM (3 < strlen (i ? a3 : "1"));
      44  
      45    ELIM (6 < strlen (i ? "12" : a7));
      46    ELIM (6 < strlen (i ? a7 : "123"));
      47  
      48    ELIM (6 < strlen (i ? "1234" : a7));
      49    ELIM (7 < strlen (i ? a7 : "1234567"));
      50  
      51    ELIM (3 < strlen (i < 1 ? "a" : i == 1 ? "ab" : a3));
      52    ELIM (3 < strlen (i < 1 ? "a" : i == 1 ? a3 : "abc"));
      53    ELIM (3 < strlen (i < 1 ? a3 : i == 1 ? "a" : "abc"));
      54  
      55    ELIM (6 < strlen (i < 1 ? "a" : i == 1 ? "ab" : a7));
      56    ELIM (6 < strlen (i < 1 ? "a" : i == 1 ? a7 : "abc"));
      57    ELIM (6 < strlen (i < 1 ? a7 : i == 1 ? "a" : "abc"));
      58  
      59    ELIM (6 < strlen (i < 1 ? "a" : i == 1 ? a7 : a3));
      60    ELIM (6 < strlen (i < 1 ? a7 : i == 1 ? "a" : a3));
      61  
      62    {
      63      enum { maxlen = sizeof ma - 1 };
      64      ELIM (maxlen < strlen (ma.a));
      65    }
      66  
      67    {
      68      enum { maxlen = sizeof ma - __builtin_offsetof (struct MemArrays, b) - 1 };
      69      ELIM (maxlen < strlen (ma.b));
      70    }
      71  }
      72  
      73  /* { dg-final { scan-tree-dump-times "test_not_eliminated_" 0 "optimized" } } */