1  /* PR c/93812 - ICE on redeclaration of an attribute format function without
       2     protoype
       3     It's not clear that attribute format should be accepted on functions
       4     without a prototype.  If it's decided that it shouldn't be the tests
       5     here will need to be adjusted.
       6     { dg-do compile }
       7     { dg-options "-Wall" } */
       8  
       9  #define FMT(n1, n2) __attribute__((__format__(__printf__, n1, n2)))
      10  
      11  // Exercise function declarations.
      12  FMT (1, 2) void print1 ();
      13  
      14  FMT (2, 3) void print2 ();
      15             void print2 ();
      16  
      17  FMT (3, 4) void print3 ();
      18  FMT (3, 4) void print3 ();
      19  
      20  FMT (1, 2) void print4 ();
      21             void print4 (void);              // { dg-warning "'format' attribute cannot be applied to a function that does not take variable arguments" }
      22  
      23             void print5 ();
      24  FMT (1, 2) void print5 (void);              // { dg-warning "\\\[-Wattributes" }
      25  
      26  FMT (1, 2) void print6 ();
      27            void print6 (const char*, ...);   // { dg-error "conflicting types" }
      28  
      29             void print7 (const char*, ...);
      30  FMT (1, 2) void print7 ();                  // { dg-error "conflicting types" }
      31  
      32  
      33  // Exercise function calls.
      34  void test_print (void)
      35  {
      36    print1 ("%i %s", 123, "");
      37    print1 ("%s %i", 123, 123);               // { dg-warning "\\\[-Wformat" }
      38  
      39    print2 (0, "%s %i", "", 123);
      40    print2 (1, "%i %s", "", 123);             // { dg-warning "\\\[-Wformat" }
      41  
      42    print3 (0, 1, "%s %i", "", 123);
      43    print3 (1, 2, "%i %s", "", 123);          // { dg-warning "\\\[-Wformat" }
      44  
      45    // Just verify there's no ICE.
      46    print4 ();
      47    print5 ();
      48    print6 ("%i %s", 123, "");
      49  }
      50  
      51  
      52  // Exercise declarations of pointers to functions.
      53  FMT (1, 2) void (*pfprint1)();
      54  
      55  FMT (2, 3) void (*pfprint2)();
      56             void (*pfprint2)();
      57  
      58  FMT (3, 4) void (*pfprint3)();
      59  FMT (3, 4) void (*pfprint3)();
      60  
      61  FMT (1, 2) void (*pfprint4)();
      62             void (*pfprint4)(void);              // { dg-warning "'format' attribute cannot be applied to a function that does not take variable arguments" }
      63  
      64             void (*pfprint5)();
      65  FMT (1, 2) void (*pfprint5)(void);              // { dg-warning "\\\[-Wattributes" }
      66  
      67  FMT (1, 2) void (*pfprint6)();
      68             void (*pfprint6)(const char*, ...);   // { dg-error "conflicting types" }
      69  
      70             void (*pfprint7)(const char*, ...);
      71  FMT (1, 2) void (*pfprint7)();                  // { dg-error "conflicting types" }
      72  
      73  // Exercise calls via function pointers.
      74  void test_pfprint (void)
      75  {
      76    pfprint1 ("%i %s", 123, "");
      77    pfprint1 ("%s %i", 123, 123);             // { dg-warning "\\\[-Wformat" }
      78  
      79    pfprint2 (0, "%s %i", "", 123);
      80    pfprint2 (1, "%i %s", "", 123);           // { dg-warning "\\\[-Wformat" }
      81  
      82    pfprint3 (0, 1, "%s %i", "", 123);
      83    pfprint3 (1, 2, "%i %s", "", 123);        // { dg-warning "\\\[-Wformat" }
      84  
      85    // Just verify there's no ICE.
      86    pfprint4 ();
      87    pfprint5 ();
      88    pfprint6 ("%i %s", 123, "");
      89  }