libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / testsuite / gcc.dg / tree-ssa / user-printf-warn-1.c
blob1896e1c64890a34a50df1b538361a8295e81321e
1 /* PR middle-end/87041 - -Wformat "reading through null pointer" on
2 unreachable code
3 Test to verify that the applicable subset of -Wformat-overflow warnings
4 are issued for user-defined function declared attribute format printf.
5 { dg-do compile }
6 { dg-options "-O -Wformat -Wformat-overflow=1 -ftrack-macro-expansion=0" }
7 { dg-require-effective-target int32plus } */
9 /* When debugging, define LINE to the line number of the test case to exercise
10 and avoid exercising any of the others. The buffer and objsize macros
11 below make use of LINE to avoid warnings for other lines. */
12 #ifndef LINE
13 # define LINE 0
14 #endif
16 #define INT_MAX __INT_MAX__
17 #define ATTR(...) __attribute__ ((__VA_ARGS__))
19 typedef __SIZE_TYPE__ size_t;
21 #if !__cplusplus
22 typedef __WCHAR_TYPE__ wchar_t;
23 #endif
25 typedef __WINT_TYPE__ wint_t;
27 ATTR (format (printf, 2, 3)) void
28 user_print (char*, const char*, ...);
30 ATTR (format (printf, 2, 3), nonnull) void
31 user_print_nonnull (char*, const char*, ...);
33 ATTR (format (printf, 2, 3), nonnull (2)) void
34 user_print_nonnull_fmt (char*, const char*, ...);
36 ATTR (format (printf, 2, 4), nonnull (3)) void
37 user_print_nonnull_other (char*, const char*, char*, ...);
39 void dummy_print (char*, const char*, ...);
41 const char chr_no_nul = 'a';
42 const char arr_no_nul[] = { 'a', 'b' };
45 /* Helper to expand function to either __builtin_f or dummy_f to
46 make debugging GCC easy. */
47 #define T(...) \
48 (((!LINE || LINE == __LINE__) \
49 ? user_print : dummy_print) (0, __VA_ARGS__))
51 /* Exercise the "%c" directive with constant arguments. */
53 void test_user_print_format_string (void)
55 char *null = 0;
56 /* Verify that no warning is issued for a null format string unless
57 the corresponding parameter is declared nonnull. */
58 user_print (0, null);
59 user_print_nonnull ("x", "y");
60 user_print_nonnull ("x", null); /* { dg-warning "\\\[-Wnonnull]" } */
61 user_print_nonnull_fmt (null, "x");
62 user_print_nonnull_fmt (0, null); /* { dg-warning "\\\[-Wnonnull]" } */
63 user_print_nonnull_other (null, "x", "y");
64 user_print_nonnull_other (null, "x", null); /* { dg-warning "\\\[-Wnonnull]" } */
68 /* Exercise the "%c" directive with constant arguments. */
70 void test_user_print_c_const (int width)
72 /* Verify that a warning is issued for exceeding INT_MAX bytes and
73 not otherwise. */
74 T ("%*c", INT_MAX - 1, '1');
75 T ("%*c", INT_MAX, '1');
76 T ("X%*c", INT_MAX - 1, '1');
77 T ("X%*c", INT_MAX, '1'); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
79 T ("%*c%*c", INT_MAX - 1, '1', INT_MAX - 1, '2'); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
81 T ("%*cX", INT_MAX - 2, '1');
82 T ("%*cX", INT_MAX - 1, '1');
83 T ("%*cX", INT_MAX, '1'); /* { dg-warning "output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
85 if (width < INT_MAX - 1)
86 width = INT_MAX - 1;
87 T ("%*cX", width, '1');
88 T ("%*cXY", width, '1'); /* { dg-warning ".XY. directive output of 2 bytes causes result to exceed .INT_MAX." } */
90 /* Also exercise a non-constant format string. The warning points
91 to the line where the format is declared (see bug 87773) so avoid
92 triggering that bug here. */
93 const char *fmt = "%*cXYZ"; T (fmt, width, '1'); /* { dg-warning ".XYZ. directive output of 3 bytes causes result to exceed .INT_MAX." } */
97 /* Exercise the "%s" directive with constant arguments. */
99 void test_user_print_s_const (int width)
101 const char *null = 0;
103 T ("%s", null); /* { dg-warning ".%s. directive argument is null" } */
104 T ("%.0s", null); /* { dg-warning ".%.0s. directive argument is null" } */
106 /* Verify no warning is issued for unreachable code. */
107 if (null)
108 T ("%s", null);
110 T ("%s", &chr_no_nul); /* { dg-warning ".%s. directive argument is not a nul-terminated string" } */
111 T ("%s", arr_no_nul); /* { dg-warning ".%s. directive argument is not a nul-terminated string" } */
113 /* Verify that output in excess of INT_MAX bytes is diagnosed even
114 when the size of the destination object is unknown. */
115 T ("%*s", INT_MAX - 1, "");
116 T ("%*s", INT_MAX, "");
117 T ("X%*s", INT_MAX, ""); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
119 if (width < INT_MAX - 1)
120 width = INT_MAX - 1;
121 T ("%*sX", width, "1");
122 T ("%*sXY", width, "1"); /* { dg-warning ".XY. directive output of 2 bytes causes result to exceed .INT_MAX." } */
126 const wchar_t wchr_no_nul = L'a';
127 const wchar_t warr_no_nul[] = { L'a', L'b' };
129 /* Exercise the "%s" directive with constant arguments. */
131 void test_user_print_ls_const (int width)
133 const wchar_t *null = 0;
135 T ("%ls", null); /* { dg-warning ".%ls. directive argument is null" } */
136 T ("%.0ls", null); /* { dg-warning ".%.0ls. directive argument is null" } */
138 /* Verify no warning is issued for unreachable code. */
139 if (null)
140 T ("%ls", null);
142 T ("%ls", &wchr_no_nul); /* { dg-warning ".%ls. directive argument is not a nul-terminated string" } */
143 T ("%ls", warr_no_nul); /* { dg-warning ".%ls. directive argument is not a nul-terminated string" "pr88211" { xfail *-*-* } } */
145 /* Verify that output in excess of INT_MAX bytes is diagnosed even
146 when the size of the destination object is unknown. */
147 T ("%*ls", INT_MAX - 1, L"");
148 T ("%*ls", INT_MAX, L"");
149 T ("X%*ls", INT_MAX, L""); /* { dg-warning "directive output of \[0-9\]+ bytes causes result to exceed .INT_MAX." } */
151 if (width < INT_MAX - 1)
152 width = INT_MAX - 1;
153 T ("%*lsX", width, L"1");
154 T ("%*lsXY", width, L"1"); /* { dg-warning ".XY. directive output of 2 bytes causes result to exceed .INT_MAX." } */