blob: e87d8acef5696b86f9a91d0aa0d7e5620ab3db03 [file] [log] [blame]
Tom Care3bfc5f42010-06-09 04:11:11 +00001// RUN: cp %s %t
Richard Smith23153182011-09-06 03:01:15 +00002// RUN: %clang_cc1 -pedantic -Wall -fixit %t
Ted Kremenek1ad35be2011-07-14 17:05:32 +00003// RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Werror %t
4// RUN: %clang_cc1 -E -o - %t | FileCheck %s
Tom Care3bfc5f42010-06-09 04:11:11 +00005
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
11int printf(char const *, ...);
12
13void test() {
Tom Care876e9942010-06-11 04:22:02 +000014 // Basic types
15 printf("%s", (int) 123);
16 printf("abc%0f", "testing testing 123");
Tom Care3bfc5f42010-06-09 04:11:11 +000017 printf("%u", (long) -12);
Ted Kremenek13927a42010-06-16 21:23:04 +000018 printf("%p", 123);
Ted Kremenek01cb1aa2010-06-17 01:12:20 +000019 printf("%c\n", "x");
20 printf("%c\n", 1.23);
Tom Care876e9942010-06-11 04:22:02 +000021
22 // Larger types
Tom Care3bfc5f42010-06-09 04:11:11 +000023 printf("%+.2d", (unsigned long long) 123456);
24 printf("%1d", (long double) 1.23);
Tom Care876e9942010-06-11 04:22:02 +000025
26 // Flag handling
Tom Care45f9b7e2010-06-21 21:21:01 +000027 printf("%0+s", (unsigned) 31337); // 0 flag should stay
Tom Care4c602192010-06-18 03:02:16 +000028 printf("%#p", (void *) 0);
Tom Care45f9b7e2010-06-21 21:21:01 +000029 printf("% +f", 1.23); // + flag should stay
30 printf("%0-f", 1.23); // - flag should stay
Tom Care876e9942010-06-11 04:22:02 +000031
32 // Positional arguments
Tom Care3bfc5f42010-06-09 04:11:11 +000033 printf("%1$f:%2$.*3$f:%4$.*3$f\n", 1, 2, 3, 4);
Tom Care4c602192010-06-18 03:02:16 +000034
35 // Precision
36 printf("%10.5d", 1l); // (bug 7394)
37 printf("%.2c", 'a');
38
39 // Ignored flags
40 printf("%0-f", 1.23);
41
42 // Bad length modifiers
43 printf("%hhs", "foo");
44 printf("%1$zp", (void *)0);
Ted Kremenek1e713f52011-04-25 22:32:59 +000045
46 // Perserve the original formatting for unsigned integers.
47 unsigned long val = 42;
48 printf("%X", val);
Hans Wennborga7da2152011-10-18 08:10:06 +000049
50 typedef __SIZE_TYPE__ size_t;
Hans Wennborga7da2152011-10-18 08:10:06 +000051 typedef __INTMAX_TYPE__ intmax_t;
52 typedef __UINTMAX_TYPE__ uintmax_t;
53 typedef __PTRDIFF_TYPE__ ptrdiff_t;
54
55 // size_t, etc.
Hans Wennborg36e76082011-10-18 09:30:37 +000056 printf("%f", (size_t) 42);
Hans Wennborg36e76082011-10-18 09:30:37 +000057 printf("%f", (intmax_t) 42);
58 printf("%f", (uintmax_t) 42);
59 printf("%f", (ptrdiff_t) 42);
Tom Care3bfc5f42010-06-09 04:11:11 +000060}
Ted Kremenek1e713f52011-04-25 22:32:59 +000061
62// Validate the fixes...
63// CHECK: printf("%d", (int) 123);
64// CHECK: printf("abc%s", "testing testing 123");
65// CHECK: printf("%ld", (long) -12);
66// CHECK: printf("%d", 123);
67// CHECK: printf("%s\n", "x");
68// CHECK: printf("%f\n", 1.23);
69// CHECK: printf("%.2llu", (unsigned long long) 123456);
70// CHECK: printf("%1Lf", (long double) 1.23);
71// CHECK: printf("%0u", (unsigned) 31337);
72// CHECK: printf("%p", (void *) 0);
73// CHECK: printf("%+f", 1.23);
74// CHECK: printf("%-f", 1.23);
75// CHECK: printf("%1$d:%2$.*3$d:%4$.*3$d\n", 1, 2, 3, 4);
76// CHECK: printf("%10.5ld", 1l);
77// CHECK: printf("%c", 'a');
78// CHECK: printf("%-f", 1.23);
79// CHECK: printf("%s", "foo");
80// CHECK: printf("%1$p", (void *)0);
81// CHECK: printf("%lX", val);
Hans Wennborga7da2152011-10-18 08:10:06 +000082// CHECK: printf("%zu", (size_t) 42);
Hans Wennborga7da2152011-10-18 08:10:06 +000083// CHECK: printf("%jd", (intmax_t) 42);
84// CHECK: printf("%ju", (uintmax_t) 42);
85// CHECK: printf("%td", (ptrdiff_t) 42);