Skip to content

Commit 4503ee5

Browse files
committed
update docs with more promotion/narrowing info
Signed-off-by: Justin Stitt <[email protected]>
1 parent cc2a8f4 commit 4503ee5

File tree

2 files changed

+74
-40
lines changed

2 files changed

+74
-40
lines changed

clang/docs/OverflowBehaviorTypes.rst

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Where ``behavior`` can be one of the following:
4343

4444
This attribute can be applied to ``typedef`` declarations and to integer types directly.
4545

46+
Arithmetic operations containing one or more overflow behavior types may have
47+
result types that do not match standard integer promotion rules. The
48+
characteristics of result types across all scenarios are described under `Promotion Rules`_.
49+
4650
Examples
4751
========
4852

@@ -75,46 +79,6 @@ integral types.
7579
Note that C++ overload set formation rules treat promotions to and from
7680
overflow behavior types the same as normal integral promotions and conversions.
7781

78-
Interaction with Command-Line Flags and Sanitizer Special Case Lists
79-
====================================================================
80-
81-
The ``overflow_behavior`` attribute interacts with sanitizers, ``-ftrapv``,
82-
``-fwrapv``, and Sanitizer Special Case Lists (SSCL) by wholly overriding these
83-
global flags. The following table summarizes the interactions:
84-
85-
.. list-table:: Overflow Behavior Precedence
86-
:widths: 15 15 15 15 20 15
87-
:header-rows: 1
88-
89-
* - Behavior
90-
- Default(No Flags)
91-
- -ftrapv
92-
- -fwrapv
93-
- Sanitizers
94-
- SSCL
95-
* - ``overflow_behavior(wrap)``
96-
- Wraps
97-
- No trap
98-
- Wraps
99-
- No report
100-
- Overrides SSCL
101-
* - ``overflow_behavior(no_wrap)``
102-
- Traps
103-
- Traps
104-
- Traps
105-
- Reports
106-
- Overrides SSCL
107-
108-
It is important to note the distinction between signed and unsigned types. For
109-
unsigned integers, which wrap on overflow by default, ``overflow_behavior(no_wrap)``
110-
is particularly useful for enabling overflow checks. For signed integers, whose
111-
overflow behavior is undefined by default, ``overflow_behavior(wrap)`` provides
112-
a guaranteed wrapping behavior.
113-
114-
The ``overflow_behavior`` attribute can be used to override the behavior of
115-
entries from a :doc:`SanitizerSpecialCaseList`. This is useful for allowlisting
116-
specific types into overflow instrumentation.
117-
11882
Promotion Rules
11983
===============
12084

@@ -123,6 +87,9 @@ specified overflow behavior throughout an arithmetic expression. They differ
12387
from standard C/C++ integer promotions but in a predictable way, similar to
12488
how ``_Complex`` and ``_BitInt`` have their own promotion rules.
12589

90+
The resulting type characteristics for overflow behavior types (OBTs) across a
91+
variety of scenarios is detailed below.
92+
12693
* **OBT and Standard Integer Type**: In an operation involving an overflow
12794
behavior type (OBT) and a standard integer type, the result will have the
12895
type of the OBT, including its overflow behavior, sign, and bit-width. The
@@ -156,6 +123,59 @@ how ``_Complex`` and ``_BitInt`` have their own promotion rules.
156123
Regardless, the resulting type matches the bit-width, sign and behavior of
157124
the ``no_wrap`` type.
158125

126+
.. list-table:: Promotion Rules Summary
127+
:widths: 30 70
128+
:header-rows: 1
129+
130+
* - Operation Type
131+
- Result Type
132+
* - OBT + Standard Integer
133+
- OBT type (preserves overflow behavior, sign, and bit-width)
134+
* - Same Kind OBTs (both ``wrap`` or both ``no_wrap``)
135+
- Larger bit-width; unsigned favored if same width
136+
* - Different Kind OBTs (``wrap`` + ``no_wrap``)
137+
- ``no_wrap`` type (matches ``no_wrap`` operand's characteristics)
138+
139+
Interaction with Command-Line Flags and Sanitizer Special Case Lists
140+
====================================================================
141+
142+
The ``overflow_behavior`` attribute interacts with sanitizers, ``-ftrapv``,
143+
``-fwrapv``, and Sanitizer Special Case Lists (SSCL) by wholly overriding these
144+
global flags. The following table summarizes the interactions:
145+
146+
.. list-table:: Overflow Behavior Precedence
147+
:widths: 15 15 15 15 20 15
148+
:header-rows: 1
149+
150+
* - Behavior
151+
- Default(No Flags)
152+
- -ftrapv
153+
- -fwrapv
154+
- Sanitizers
155+
- SSCL
156+
* - ``overflow_behavior(wrap)``
157+
- Wraps
158+
- Wraps
159+
- Wraps
160+
- No report
161+
- Overrides SSCL
162+
* - ``overflow_behavior(no_wrap)``
163+
- Traps
164+
- Traps
165+
- Traps
166+
- Reports
167+
- Overrides SSCL
168+
169+
It is important to note the distinction between signed and unsigned types. For
170+
unsigned integers, which wrap on overflow by default, ``overflow_behavior(no_wrap)``
171+
is particularly useful for enabling overflow checks. For signed integers, whose
172+
overflow behavior is undefined by default, ``overflow_behavior(wrap)`` provides
173+
a guaranteed wrapping behavior.
174+
175+
The ``overflow_behavior`` attribute can be used to override the behavior of
176+
entries from a :doc:`SanitizerSpecialCaseList`. This is useful for allowlisting
177+
specific types into overflow instrumentation.
178+
159179
Diagnostics
160180
===========
161181

clang/docs/UndefinedBehaviorSanitizer.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,20 @@ arithmetic operations on that type should wrap on overflow. This can be used to
390390
disable overflow sanitization for specific types, while leaving it enabled for
391391
all other types.
392392

393+
The ``overflow_behavior`` attribute not only affects UBSan instrumentation
394+
but also changes the fundamental overflow behavior of arithmetic operations
395+
on the annotated type. Operations on types marked with ``wrap`` will have
396+
well-defined wrapping semantics, while operations on types marked with
397+
``no_wrap`` will be checked for overflow (regardless of global flags like
398+
``-fwrapv``).
399+
400+
The attribute also affects implicit type promotion rules: when an overflow
401+
behavior type participates in arithmetic operations with standard integer
402+
types, the result maintains the overflow behavior type's characteristics,
403+
including its bit-width. This means annotated types can preserve narrower
404+
widths that would normally be promoted, allowing operations to stay within
405+
the constraints of the smallest annotated type in the expression.
406+
393407
For more information, see :doc:`OverflowBehaviorTypes`.
394408

395409
Enforcing Overflow Instrumentation with ``__attribute__((overflow_behavior(no_wrap)))``

0 commit comments

Comments
 (0)