@@ -1237,6 +1237,133 @@ generated saying the pragma didn't resolve to a declaration. For example:
12371237 #pragma export(func)
12381238 int func(double) { return 0; } // warning: failed to resolve '#pragma export' to a declaration
12391239
1240+ Overflow behavior types
1241+ =======================
1242+
1243+ Clang provides an extension that allows developers to annotate integer types
1244+ with explicit overflow behavior. This enables fine-grained control over whether
1245+ arithmetic operations should wrap on overflow (with two's complement semantics)
1246+ or be checked for overflow (trapping or reporting via sanitizers).
1247+
1248+ This feature is experimental and must be enabled with the ``-cc1 `` option
1249+ ``-fexperimental-overflow-behavior-types ``.
1250+
1251+ Query for this feature with ``__has_extension(overflow_behavior_types) ``.
1252+
1253+ Syntax
1254+ ------
1255+
1256+ Overflow behavior can be specified using either attribute syntax or keyword
1257+ syntax:
1258+
1259+ **Attribute syntax: **
1260+
1261+ .. code-block :: c
1262+
1263+ typedef int __attribute__((overflow_behavior(wrap))) wrapping_int;
1264+ typedef int __attribute__((overflow_behavior(trap))) checked_int;
1265+
1266+ **Keyword syntax: **
1267+
1268+ .. code-block :: c
1269+
1270+ typedef int __ob_wrap wrapping_int;
1271+ typedef int __ob_trap checked_int;
1272+
1273+ The annotation can also be applied directly to variable declarations:
1274+
1275+ .. code-block :: c
1276+
1277+ int __ob_wrap counter = 0;
1278+ unsigned __ob_trap safe_index = 0;
1279+
1280+ Semantics
1281+ ---------
1282+
1283+ **wrap behavior: **
1284+
1285+ When an integer type is annotated with ``wrap `` (or ``__ob_wrap ``), arithmetic
1286+ operations on values of that type use two's complement wrapping semantics on
1287+ overflow. This behavior is well-defined regardless of signedness. Compilers
1288+ must not optimize based on the assumption that overflow does not occur.
1289+
1290+ **trap behavior: **
1291+
1292+ When an integer type is annotated with ``trap `` (or ``__ob_trap ``), arithmetic
1293+ operations on values of that type are checked for overflow. If overflow occurs,
1294+ the program traps or reports via sanitizers (depending on compiler settings).
1295+
1296+ Integer Promotions and Conversions
1297+ ----------------------------------
1298+
1299+ Overflow behavior types follow standard C integer promotion and conversion
1300+ rules. The overflow behavior annotation is preserved through implicit
1301+ promotions and conversions.
1302+
1303+ **Standard promotion rules apply: **
1304+
1305+ Integer literals without a suffix have type ``int `` (or a larger type if the
1306+ value requires it), following normal C rules. When such a literal is used in
1307+ an operation with an overflow behavior type, standard promotion rules determine
1308+ the result type, and the overflow behavior is propagated:
1309+
1310+ .. code-block :: c
1311+
1312+ typedef int __ob_wrap wrap_int;
1313+ wrap_int x = 100;
1314+ wrap_int y = x + 1; // 1 is promoted; result is __ob_wrap int
1315+
1316+ **Combining different overflow behaviors: **
1317+
1318+ When operands have different overflow behaviors, ``trap `` takes precedence
1319+ over ``wrap ``:
1320+
1321+ .. code-block :: c
1322+
1323+ typedef int __ob_wrap wrap_int;
1324+ typedef int __ob_trap trap_int;
1325+ wrap_int a = 1;
1326+ trap_int b = 2;
1327+ auto c = a + b; // Result is __ob_trap int (trap dominates)
1328+
1329+ **Conversion to standard types: **
1330+
1331+ When an overflow behavior type is converted to a standard integer type (without
1332+ an overflow behavior annotation), the overflow behavior is discarded. Compilers
1333+ may warn about this:
1334+
1335+ .. code-block :: c
1336+
1337+ typedef int __ob_wrap wrap_int;
1338+ wrap_int w = 42;
1339+ int i = w; // Warning: discards overflow behavior
1340+
1341+ Interaction with Compiler Flags
1342+ -------------------------------
1343+
1344+ Overflow behavior annotations take precedence over global compiler flags:
1345+
1346+ - A ``wrap `` type wraps on overflow even when ``-ftrapv `` is enabled.
1347+ - A ``trap `` type is checked for overflow even when ``-fwrapv `` is enabled.
1348+
1349+ This allows mixing different overflow behaviors within the same program,
1350+ enabling developers to selectively apply wrapping or checking to specific
1351+ types while using different defaults elsewhere.
1352+
1353+ Incompatible Assignments
1354+ ------------------------
1355+
1356+ Direct assignment between types with different overflow behaviors (``wrap ``
1357+ vs ``trap ``) is an error:
1358+
1359+ .. code-block :: c
1360+
1361+ int __ob_wrap w;
1362+ int __ob_trap t;
1363+ w = t; // Error: incompatible overflow behaviors
1364+
1365+ For more detailed documentation, see :doc: `OverflowBehaviorTypes `.
1366+
12401367Messages on ``deprecated `` and ``unavailable `` Attributes
12411368=========================================================
12421369
0 commit comments