@@ -161,10 +161,10 @@ Diagnostics
161
161
162
162
Clang provides diagnostics to help developers manage overflow behavior types.
163
163
164
- -Wimplicitly-discarded-overflow- behavior
165
- ----------------------------------------
164
+ -Woverflow- behavior-conversion
165
+ ------------------------------
166
166
167
- This warning is issued when an overflow behavior type is implicitly converted
167
+ This warning group is issued when an overflow behavior type is implicitly converted
168
168
to a standard integer type, which may lead to the loss of the specified
169
169
overflow behavior.
170
170
@@ -192,39 +192,20 @@ integer type.
192
192
some_function(static_cast<int>(w)); // OK
193
193
}
194
194
195
- This warning acts as a group that includes
196
- ``-Wimplicitly-discarded-overflow-behavior-pedantic `` and
197
- ``-Wimplicitly-discarded-overflow-behavior-assignment ``.
198
-
199
- -Wimplicitly-discarded-overflow-behavior-pedantic
200
- -------------------------------------------------
201
-
202
- A less severe version of the warning, ``-Wimplicitly-discarded-overflow-behavior-pedantic ``,
203
- is issued for implicit conversions from an unsigned wrapping type to a standard
204
- unsigned integer type. This is considered less problematic because both types
205
- have well-defined wrapping behavior, but the conversion still discards the
206
- explicit ``overflow_behavior `` attribute.
207
-
208
- .. code-block :: c++
209
-
210
- typedef unsigned int __attribute__((overflow_behavior(wrap))) wrapping_uint;
211
-
212
- void some_function(unsigned int);
195
+ This warning group includes
196
+ ``-Wimplicit-overflow-behavior-conversion `` and
197
+ ``-Wimplicit-overflow-behavior-conversion-pedantic ``.
213
198
214
- void another_function(wrapping_uint w) {
215
- some_function(w); // warning: implicit conversion from 'wrapping_uint' to
216
- // 'unsigned int' discards overflow behavior
217
- // [-Wimplicitly-discarded-overflow-behavior-pedantic]
218
- }
199
+ .. note ::
200
+ ``-Woverflow-behavior-conversion `` is implied by ``-Wconversion ``.
219
201
220
- -Wimplicitly-discarded- overflow-behavior-assignment
221
- ---------------------------------------------------
202
+ -Wimplicit- overflow-behavior-conversion
203
+ ---------------------------------------
222
204
223
205
This warning is issued when an overflow behavior type is implicitly converted
224
- to a standard integer type as part of an assignment, which may lead to the
225
- loss of the specified overflow behavior. This is a more specific version of
226
- the ``-Wimplicitly-discarded-overflow-behavior `` warning, and it is off by
227
- default.
206
+ to a standard integer type as part of most conversions, which may lead to the
207
+ loss of the specified overflow behavior. This is the main warning in the
208
+ ``-Woverflow-behavior-conversion `` group.
228
209
229
210
.. code-block :: c++
230
211
@@ -233,8 +214,23 @@ default.
233
214
void some_function() {
234
215
wrapping_int w = 1;
235
216
int i = w; // warning: implicit conversion from 'wrapping_int' to 'int'
236
- // discards overflow behavior
237
- // [-Wimplicitly-discarded-overflow-behavior-assignment]
217
+ // during assignment discards overflow behavior
218
+ // [-Wimplicit-overflow-behavior-conversion]
219
+ }
220
+
221
+ Here's another example showing function parameter conversion with a ``no_wrap `` type:
222
+
223
+ .. code-block :: c++
224
+
225
+ typedef int __attribute__((overflow_behavior(no_wrap))) safe_int;
226
+
227
+ void bar(int x); // Function expects standard int
228
+
229
+ void foo() {
230
+ safe_int s = 42;
231
+ bar(s); // warning: implicit conversion from 'safe_int' to 'int'
232
+ // discards overflow behavior
233
+ // [-Wimplicit-overflow-behavior-conversion]
238
234
}
239
235
240
236
To fix this, you can explicitly cast the overflow behavior type to a standard
@@ -243,13 +239,42 @@ integer type.
243
239
.. code-block :: c++
244
240
245
241
typedef int __attribute__((overflow_behavior(wrap))) wrapping_int;
242
+ typedef int __attribute__((overflow_behavior(no_wrap))) safe_int;
246
243
247
244
void some_function() {
248
245
wrapping_int w = 1;
249
246
int i = static_cast<int>(w); // OK
250
247
int j = (int)w; // C-style OK
251
248
}
252
249
250
+ void bar(int x);
251
+
252
+ void foo() {
253
+ safe_int s = 42;
254
+ bar(static_cast<int>(s)); // OK
255
+ }
256
+
257
+
258
+ -Wimplicit-overflow-behavior-conversion-pedantic
259
+ ------------------------------------------------
260
+
261
+ A less severe version of the warning, ``-Wimplicit-overflow-behavior-conversion-pedantic ``,
262
+ is issued for implicit conversions from an unsigned wrapping type to a standard
263
+ unsigned integer type. This is considered less problematic because both types
264
+ have well-defined wrapping behavior, but the conversion still discards the
265
+ explicit ``overflow_behavior `` attribute.
266
+
267
+ .. code-block :: c++
268
+
269
+ typedef unsigned int __attribute__((overflow_behavior(wrap))) wrapping_uint;
270
+
271
+ void some_function(unsigned int);
272
+
273
+ void another_function(wrapping_uint w) {
274
+ some_function(w); // warning: implicit conversion from 'wrapping_uint' to
275
+ // 'unsigned int' discards overflow behavior
276
+ // [-Wimplicit-overflow-behavior-conversion-pedantic]
277
+ }
253
278
254
279
-Woverflow-behavior-attribute-ignored
255
280
-------------------------------------
0 commit comments