Home | History | Annotate | Download | only in Sema
      1 // RUN: cp %s %t
      2 // RUN: %clang_cc1 -pedantic -Wall -fixit %t
      3 // RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Werror %t
      4 // RUN: %clang_cc1 -E -o - %t | FileCheck %s
      5 
      6 /* This is a test of the various code modification hints that are
      7    provided as part of warning or extension diagnostics. All of the
      8    warnings will be fixed by -fixit, and the resulting file should
      9    compile cleanly with -Werror -pedantic. */
     10 
     11 int printf(char const *, ...);
     12 
     13 typedef __SIZE_TYPE__ size_t;
     14 typedef __INTMAX_TYPE__ intmax_t;
     15 typedef __UINTMAX_TYPE__ uintmax_t;
     16 typedef __PTRDIFF_TYPE__ ptrdiff_t;
     17 
     18 void test() {
     19   // Basic types
     20   printf("%s", (int) 123);
     21   printf("abc%0f", "testing testing 123");
     22   printf("%u", (long) -12);
     23   printf("%p", 123);
     24   printf("%c\n", "x");
     25   printf("%c\n", 1.23);
     26 
     27   // Larger types
     28   printf("%+.2d", (unsigned long long) 123456);
     29   printf("%1d", (long double) 1.23);
     30 
     31   // Flag handling
     32   printf("%0+s", (unsigned) 31337); // 0 flag should stay
     33   printf("%#p", (void *) 0);
     34   printf("% +f", 1.23); // + flag should stay
     35   printf("%0-f", 1.23); // - flag should stay
     36 
     37   // Positional arguments
     38 #pragma clang diagnostic push // Don't warn about using positional arguments.
     39 #pragma clang diagnostic ignored "-Wformat-non-iso"
     40   printf("%1$f:%2$.*3$f:%4$.*3$f\n", 1, 2, 3, 4);
     41 #pragma clang diagnostic pop
     42 
     43   // Precision
     44   printf("%10.5d", 1l); // (bug 7394)
     45   printf("%.2c", 'a');
     46 
     47   // Ignored flags
     48   printf("%0-f", 1.23);
     49 
     50   // Bad length modifiers
     51   printf("%hhs", "foo");
     52 #pragma clang diagnostic push // Don't warn about using positional arguments.
     53 #pragma clang diagnostic ignored "-Wformat-non-iso"
     54   printf("%1$zp", (void *)0);
     55 #pragma clang diagnostic pop
     56 
     57   // Preserve the original formatting for unsigned integers.
     58   unsigned long val = 42;
     59   printf("%X", val);
     60 
     61   // size_t, etc.
     62   printf("%f", (size_t) 42);
     63   printf("%f", (intmax_t) 42);
     64   printf("%f", (uintmax_t) 42);
     65   printf("%f", (ptrdiff_t) 42);
     66 
     67   // Look beyond the first typedef.
     68   typedef size_t my_size_type;
     69   typedef intmax_t my_intmax_type;
     70   typedef uintmax_t my_uintmax_type;
     71   typedef ptrdiff_t my_ptrdiff_type;
     72   typedef int my_int_type;
     73   printf("%f", (my_size_type) 42);
     74   printf("%f", (my_intmax_type) 42);
     75   printf("%f", (my_uintmax_type) 42);
     76   printf("%f", (my_ptrdiff_type) 42);
     77   printf("%f", (my_int_type) 42);
     78 
     79   // string
     80   printf("%ld", "foo");
     81 
     82   // Preserve the original choice of conversion specifier.
     83   printf("%o", (long) 42);
     84   printf("%u", (long) 42);
     85   printf("%x", (long) 42);
     86   printf("%X", (long) 42);
     87   printf("%i", (unsigned long) 42);
     88   printf("%d", (unsigned long) 42);
     89   printf("%F", (long double) 42);
     90   printf("%e", (long double) 42);
     91   printf("%E", (long double) 42);
     92   printf("%g", (long double) 42);
     93   printf("%G", (long double) 42);
     94   printf("%a", (long double) 42);
     95   printf("%A", (long double) 42);
     96 }
     97 
     98 int scanf(char const *, ...);
     99 
    100 void test2() {
    101   char str[100];
    102   short shortVar;
    103   unsigned short uShortVar;
    104   int intVar;
    105   unsigned uIntVar;
    106   float floatVar;
    107   double doubleVar;
    108   long double longDoubleVar;
    109   long longVar;
    110   unsigned long uLongVar;
    111   long long longLongVar;
    112   unsigned long long uLongLongVar;
    113   size_t sizeVar;
    114   intmax_t intmaxVar;
    115   uintmax_t uIntmaxVar;
    116   ptrdiff_t ptrdiffVar;
    117 
    118   scanf("%lf", str);
    119   scanf("%f", &shortVar);
    120   scanf("%f", &uShortVar);
    121   scanf("%p", &intVar);
    122   scanf("%Lf", &uIntVar);
    123   scanf("%ld", &floatVar);
    124   scanf("%f", &doubleVar);
    125   scanf("%d", &longDoubleVar);
    126   scanf("%f", &longVar);
    127   scanf("%f", &uLongVar);
    128   scanf("%f", &longLongVar);
    129   scanf("%f", &uLongLongVar);
    130 
    131   // Some named ints.
    132   scanf("%f", &sizeVar);
    133   scanf("%f", &intmaxVar);
    134   scanf("%f", &uIntmaxVar);
    135   scanf("%f", &ptrdiffVar);
    136 
    137   // Look beyond the first typedef for named integer types.
    138   typedef size_t my_size_type;
    139   typedef intmax_t my_intmax_type;
    140   typedef uintmax_t my_uintmax_type;
    141   typedef ptrdiff_t my_ptrdiff_type;
    142   typedef int my_int_type;
    143   scanf("%f", (my_size_type*)&sizeVar);
    144   scanf("%f", (my_intmax_type*)&intmaxVar);
    145   scanf("%f", (my_uintmax_type*)&uIntmaxVar);
    146   scanf("%f", (my_ptrdiff_type*)&ptrdiffVar);
    147   scanf("%f", (my_int_type*)&intVar);
    148 
    149   // Preserve the original formatting.
    150   scanf("%o", &longVar);
    151   scanf("%u", &longVar);
    152   scanf("%x", &longVar);
    153   scanf("%X", &longVar);
    154   scanf("%i", &uLongVar);
    155   scanf("%d", &uLongVar);
    156   scanf("%F", &longDoubleVar);
    157   scanf("%e", &longDoubleVar);
    158   scanf("%E", &longDoubleVar);
    159   scanf("%g", &longDoubleVar);
    160   scanf("%G", &longDoubleVar);
    161   scanf("%a", &longDoubleVar);
    162   scanf("%A", &longDoubleVar);
    163 }
    164 
    165 // Validate the fixes.
    166 // CHECK: printf("%d", (int) 123);
    167 // CHECK: printf("abc%s", "testing testing 123");
    168 // CHECK: printf("%ld", (long) -12);
    169 // CHECK: printf("%d", 123);
    170 // CHECK: printf("%s\n", "x");
    171 // CHECK: printf("%f\n", 1.23);
    172 // CHECK: printf("%+.2lld", (unsigned long long) 123456);
    173 // CHECK: printf("%1Lf", (long double) 1.23);
    174 // CHECK: printf("%0u", (unsigned) 31337);
    175 // CHECK: printf("%p", (void *) 0);
    176 // CHECK: printf("%+f", 1.23);
    177 // CHECK: printf("%-f", 1.23);
    178 // CHECK: printf("%1$d:%2$.*3$d:%4$.*3$d\n", 1, 2, 3, 4);
    179 // CHECK: printf("%10.5ld", 1l);
    180 // CHECK: printf("%c", 'a');
    181 // CHECK: printf("%-f", 1.23);
    182 // CHECK: printf("%s", "foo");
    183 // CHECK: printf("%1$p", (void *)0);
    184 // CHECK: printf("%lX", val);
    185 // CHECK: printf("%zu", (size_t) 42);
    186 // CHECK: printf("%jd", (intmax_t) 42);
    187 // CHECK: printf("%ju", (uintmax_t) 42);
    188 // CHECK: printf("%td", (ptrdiff_t) 42);
    189 // CHECK: printf("%zu", (my_size_type) 42);
    190 // CHECK: printf("%jd", (my_intmax_type) 42);
    191 // CHECK: printf("%ju", (my_uintmax_type) 42);
    192 // CHECK: printf("%td", (my_ptrdiff_type) 42);
    193 // CHECK: printf("%d", (my_int_type) 42);
    194 // CHECK: printf("%s", "foo");
    195 // CHECK: printf("%lo", (long) 42);
    196 // CHECK: printf("%ld", (long) 42);
    197 // CHECK: printf("%lx", (long) 42);
    198 // CHECK: printf("%lX", (long) 42);
    199 // CHECK: printf("%lu", (unsigned long) 42);
    200 // CHECK: printf("%lu", (unsigned long) 42);
    201 // CHECK: printf("%LF", (long double) 42);
    202 // CHECK: printf("%Le", (long double) 42);
    203 // CHECK: printf("%LE", (long double) 42);
    204 // CHECK: printf("%Lg", (long double) 42);
    205 // CHECK: printf("%LG", (long double) 42);
    206 // CHECK: printf("%La", (long double) 42);
    207 // CHECK: printf("%LA", (long double) 42);
    208 
    209 // CHECK: scanf("%s", str);
    210 // CHECK: scanf("%hd", &shortVar);
    211 // CHECK: scanf("%hu", &uShortVar);
    212 // CHECK: scanf("%d", &intVar);
    213 // CHECK: scanf("%u", &uIntVar);
    214 // CHECK: scanf("%f", &floatVar);
    215 // CHECK: scanf("%lf", &doubleVar);
    216 // CHECK: scanf("%Lf", &longDoubleVar);
    217 // CHECK: scanf("%ld", &longVar);
    218 // CHECK: scanf("%lu", &uLongVar);
    219 // CHECK: scanf("%lld", &longLongVar);
    220 // CHECK: scanf("%llu", &uLongLongVar);
    221 // CHECK: scanf("%zu", &sizeVar);
    222 // CHECK: scanf("%jd", &intmaxVar);
    223 // CHECK: scanf("%ju", &uIntmaxVar);
    224 // CHECK: scanf("%td", &ptrdiffVar);
    225 // CHECK: scanf("%zu", (my_size_type*)&sizeVar);
    226 // CHECK: scanf("%jd", (my_intmax_type*)&intmaxVar);
    227 // CHECK: scanf("%ju", (my_uintmax_type*)&uIntmaxVar);
    228 // CHECK: scanf("%td", (my_ptrdiff_type*)&ptrdiffVar);
    229 // CHECK: scanf("%d", (my_int_type*)&intVar);
    230 // CHECK: scanf("%lo", &longVar);
    231 // CHECK: scanf("%lu", &longVar);
    232 // CHECK: scanf("%lx", &longVar);
    233 // CHECK: scanf("%lX", &longVar);
    234 // CHECK: scanf("%li", &uLongVar);
    235 // CHECK: scanf("%ld", &uLongVar);
    236 // CHECK: scanf("%LF", &longDoubleVar);
    237 // CHECK: scanf("%Le", &longDoubleVar);
    238 // CHECK: scanf("%LE", &longDoubleVar);
    239 // CHECK: scanf("%Lg", &longDoubleVar);
    240 // CHECK: scanf("%LG", &longDoubleVar);
    241 // CHECK: scanf("%La", &longDoubleVar);
    242 // CHECK: scanf("%LA", &longDoubleVar);
    243