| 1 |
// RUN: %clang_cc1 -x c -std=c2x -fsyntax-only -verify -Wno-string-plus-int -Wno-unused-value %s |
| 2 |
// RUN: %clang_cc1 -x c -std=c2x -fsyntax-only -verify -Wno-string-plus-int -Wno-unused-value %s -fexperimental-new-constant-interpreter |
| 3 |
// RUN: %clang_cc1 -x c++ -std=c++23 -fsyntax-only -verify -Wno-string-plus-int -Wno-unused-value %s |
| 4 |
// RUN: %clang_cc1 -x c++ -std=c++23 -fsyntax-only -verify -Wno-string-plus-int -Wno-unused-value %s -fexperimental-new-constant-interpreter |
| 5 |
|
| 6 |
void test(void) { |
| 7 |
char buffer[10]; |
| 8 |
__builtin_sprintf(buffer, "%%d%d%d"+0, 1); |
| 9 |
// expected-warning@-1 {{more '%' conversions than data arguments}} |
| 10 |
__builtin_sprintf(buffer, "%%d%d%d"+1, 1); |
| 11 |
// expected-warning@-1 {{more '%' conversions than data arguments}} |
| 12 |
__builtin_sprintf(buffer, "%%d%d%d"+2, 1); |
| 13 |
// expected-warning@-1 {{more '%' conversions than data arguments}} |
| 14 |
__builtin_sprintf(buffer, "%%d%d%d"+3, 1); |
| 15 |
// expected-warning@-1 {{more '%' conversions than data arguments}} |
| 16 |
__builtin_sprintf(buffer, "%%d%d%d"+4, 1); |
| 17 |
__builtin_sprintf(buffer, "%%d%d%d"+5, 1); |
| 18 |
__builtin_sprintf(buffer, "%%d%d%d"+6, 1); |
| 19 |
// expected-warning@-1 {{data argument not used by format string}} |
| 20 |
__builtin_sprintf(buffer, "%%d%d%d"+7, 1); |
| 21 |
// expected-warning@-1 {{format string is empty}} |
| 22 |
__builtin_sprintf(buffer, "%%d%d%d"+8, 1); |
| 23 |
// TODO: we should probably warning about the format string being out of bounds |
| 24 |
|
| 25 |
__builtin_sprintf(buffer, "%%d%d%d"+0, 1, 2); |
| 26 |
__builtin_sprintf(buffer, "%%d%d%d"+1, 1, 2); |
| 27 |
// expected-warning@-1 {{more '%' conversions than data arguments}} |
| 28 |
__builtin_sprintf(buffer, "%%d%d%d"+2, 1, 2); |
| 29 |
__builtin_sprintf(buffer, "%%d%d%d"+3, 1, 2); |
| 30 |
__builtin_sprintf(buffer, "%%d%d%d"+4, 1, 2); |
| 31 |
// expected-warning@-1 {{data argument not used by format string}} |
| 32 |
__builtin_sprintf(buffer, "%%d%d%d"+5, 1, 2); |
| 33 |
// expected-warning@-1 {{data argument not used by format string}} |
| 34 |
__builtin_sprintf(buffer, "%%d%d%d"+6, 1, 2); |
| 35 |
// expected-warning@-1 {{data argument not used by format string}} |
| 36 |
__builtin_sprintf(buffer, "%%d%d%d"+7, 1, 2); |
| 37 |
// expected-warning@-1 {{format string is empty}} |
| 38 |
__builtin_sprintf(buffer, "%%d%d%d"+8, 1, 2); |
| 39 |
__builtin_sprintf(buffer, "%%d%d%d"+9, 1, 2); |
| 40 |
|
| 41 |
__builtin_sprintf(buffer, "%%d%d%d"+0, 1, 2, 3); |
| 42 |
// expected-warning@-1 {{data argument not used by format string}} |
| 43 |
__builtin_sprintf(buffer, "%%d%d%d"+1, 1, 2, 3); |
| 44 |
__builtin_sprintf(buffer, "%%d%d%d"+2, 1, 2, 3); |
| 45 |
// expected-warning@-1 {{data argument not used by format string}} |
| 46 |
__builtin_sprintf(buffer, "%%d%d%d"+3, 1, 2, 3); |
| 47 |
// expected-warning@-1 {{data argument not used by format string}} |
| 48 |
__builtin_sprintf(buffer, "%%d%d%d"+4, 1, 2, 3); |
| 49 |
// expected-warning@-1 {{data argument not used by format string}} |
| 50 |
__builtin_sprintf(buffer, "%%d%d%d"+5, 1, 2, 3); |
| 51 |
// expected-warning@-1 {{data argument not used by format string}} |
| 52 |
__builtin_sprintf(buffer, "%%d%d%d"+6, 1, 2, 3); |
| 53 |
// expected-warning@-1 {{data argument not used by format string}} |
| 54 |
__builtin_sprintf(buffer, "%%d%d%d"+7, 1, 2, 3); |
| 55 |
// expected-warning@-1 {{format string is empty}} |
| 56 |
__builtin_sprintf(buffer, "%%d%d%d"+8, 1, 2, 3); |
| 57 |
__builtin_sprintf(buffer, "%%d%d%d"+9, 1, 2, 3); |
| 58 |
static const char format_string[] = {'%', '%', 'd', '%', 'd', '%', 'd'}; |
| 59 |
__builtin_sprintf(buffer, format_string+0, 1); |
| 60 |
__builtin_sprintf(buffer, format_string+1, 1); |
| 61 |
__builtin_sprintf(buffer, format_string+2, 1); |
| 62 |
__builtin_sprintf(buffer, format_string+3, 1); |
| 63 |
__builtin_sprintf(buffer, format_string+4, 1); |
| 64 |
__builtin_sprintf(buffer, format_string+5, 1); |
| 65 |
__builtin_sprintf(buffer, format_string+6, 1); |
| 66 |
__builtin_sprintf(buffer, format_string+7, 1); |
| 67 |
#ifdef __cplusplus |
| 68 |
static constexpr char ce_format_string[] = {'%', '%', 'd', '%', 'd', '%', 'd'}; |
| 69 |
__builtin_sprintf(buffer, ce_format_string+0, 1); |
| 70 |
__builtin_sprintf(buffer, ce_format_string+1, 1); |
| 71 |
__builtin_sprintf(buffer, ce_format_string+2, 1); |
| 72 |
__builtin_sprintf(buffer, ce_format_string+3, 1); |
| 73 |
__builtin_sprintf(buffer, ce_format_string+4, 1); |
| 74 |
__builtin_sprintf(buffer, ce_format_string+5, 1); |
| 75 |
__builtin_sprintf(buffer, ce_format_string+6, 1); |
| 76 |
__builtin_sprintf(buffer, ce_format_string+7, 1); |
| 77 |
#endif |
| 78 |
} |
| 79 |
|
| 80 |
#ifdef __cplusplus |
| 81 |
template <class FormatStringSource> bool test_template() { |
| 82 |
char buffer[10]; |
| 83 |
__builtin_sprintf(buffer, FormatStringSource::format(0), 1); // #template_test1 |
| 84 |
__builtin_sprintf(buffer, FormatStringSource::format(1), 1); // #template_test2 |
| 85 |
__builtin_sprintf(buffer, FormatStringSource::format(2), 1); // #template_test3 |
| 86 |
__builtin_sprintf(buffer, FormatStringSource::format(3), 1); // #template_test4 |
| 87 |
__builtin_sprintf(buffer, FormatStringSource::format(4), 1); // #template_test5 |
| 88 |
__builtin_sprintf(buffer, FormatStringSource::format(5), 1); // #template_test6 |
| 89 |
__builtin_sprintf(buffer, FormatStringSource::format(6), 1); // #template_test7 |
| 90 |
__builtin_sprintf(buffer, FormatStringSource::format(7), 1); // #template_test8 |
| 91 |
__builtin_sprintf(buffer, FormatStringSource::format(8), 1); // #template_test9 |
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
struct LiteralFormatStr { |
| 96 |
static consteval const char *format(int N) { |
| 97 |
return "%%d%d%d" + N; // #LiteralFormatStrLiteral |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
struct ConstLiteralFormatStr { |
| 102 |
static constexpr const char *formatStr = "%%d%d%d"; // #ConstLiteralFormatStrLiteral |
| 103 |
static consteval const char *format(int N) { |
| 104 |
return formatStr + N; |
| 105 |
} |
| 106 |
}; |
| 107 |
|
| 108 |
struct NullTerminatedArrayFormatStr { |
| 109 |
static constexpr char formatStr[] = {'%', '%', 'd', '%', 'd', '%', 'd', 0}; |
| 110 |
static consteval const char *format(int N) { |
| 111 |
return formatStr + N; |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
struct NoNullTerminatedArrayFormatStr { |
| 116 |
static constexpr char formatStr[] = {'%', '%', 'd', '%', 'd', '%', 'd'}; |
| 117 |
static consteval const char *format(int N) { |
| 118 |
return formatStr + N; // #NoNullTerminatedArrayFormatStr_format |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
void test_templates() { |
| 123 |
test_template<LiteralFormatStr>(); |
| 124 |
// expected-note@-1 {{in instantiation of function template specialization 'test_template<LiteralFormatStr>' requested here}} |
| 125 |
// expected-warning@#template_test1 {{more '%' conversions than data arguments}} |
| 126 |
// expected-warning@#template_test2 {{more '%' conversions than data arguments}} |
| 127 |
// expected-warning@#template_test3 {{more '%' conversions than data arguments}} |
| 128 |
// expected-warning@#template_test4 {{more '%' conversions than data arguments}} |
| 129 |
// expected-warning@#template_test5 {{more '%' conversions than data arguments}} |
| 130 |
// expected-warning@#template_test6 {{more '%' conversions than data arguments}} |
| 131 |
// expected-warning@#template_test7 {{more '%' conversions than data arguments}} |
| 132 |
// expected-warning@#template_test8 {{more '%' conversions than data arguments}} |
| 133 |
// expected-warning@#template_test9 {{more '%' conversions than data arguments}} |
| 134 |
// expected-note@#LiteralFormatStrLiteral 9 {{format string is defined here}} |
| 135 |
test_template<ConstLiteralFormatStr>(); |
| 136 |
// expected-note@-1 {{in instantiation of function template specialization 'test_template<ConstLiteralFormatStr>' requested here}} |
| 137 |
// expected-warning@#template_test1 {{more '%' conversions than data arguments}} |
| 138 |
// expected-warning@#template_test2 {{more '%' conversions than data arguments}} |
| 139 |
// expected-warning@#template_test3 {{more '%' conversions than data arguments}} |
| 140 |
// expected-warning@#template_test4 {{more '%' conversions than data arguments}} |
| 141 |
// expected-warning@#template_test5 {{more '%' conversions than data arguments}} |
| 142 |
// expected-warning@#template_test6 {{more '%' conversions than data arguments}} |
| 143 |
// expected-warning@#template_test7 {{more '%' conversions than data arguments}} |
| 144 |
// expected-warning@#template_test8 {{more '%' conversions than data arguments}} |
| 145 |
// expected-warning@#template_test9 {{more '%' conversions than data arguments}} |
| 146 |
// expected-note@#ConstLiteralFormatStrLiteral 9 {{format string is defined here}} |
| 147 |
test_template<NullTerminatedArrayFormatStr>(); |
| 148 |
test_template<NoNullTerminatedArrayFormatStr>(); |
| 149 |
// expected-note@-1 {{in instantiation of function template specialization 'test_template<NoNullTerminatedArrayFormatStr>' requested here}} |
| 150 |
// expected-note@#NoNullTerminatedArrayFormatStr_format {{cannot refer to element 8 of array of 7 elements in a constant expression}} |
| 151 |
// expected-error@#template_test9 {{call to consteval function 'NoNullTerminatedArrayFormatStr::format' is not a constant expression}} |
| 152 |
// expected-note@#template_test9 {{in call to 'format(8)'}} |
| 153 |
} |
| 154 |
|
| 155 |
#endif |