1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736
|
..
-------------------------------------------------------------------
NOTE: This file is automatically generated by running clang-tblgen
-gen-attr-docs. Do not edit this file by hand!!
-------------------------------------------------------------------
===================
Attributes in Clang
===================
.. contents::
:local:
Introduction
============
This page lists the attributes currently supported by Clang.
Function Attributes
===================
interrupt
---------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Clang supports the GNU style ``__attribute__((interrupt("TYPE")))`` attribute on
ARM targets. This attribute may be attached to a function definition and
instructs the backend to generate appropriate function entry/exit code so that
it can be used directly as an interrupt service routine.
The parameter passed to the interrupt attribute is optional, but if
provided it must be a string literal with one of the following values: "IRQ",
"FIQ", "SWI", "ABORT", "UNDEF".
The semantics are as follows:
- If the function is AAPCS, Clang instructs the backend to realign the stack to
8 bytes on entry. This is a general requirement of the AAPCS at public
interfaces, but may not hold when an exception is taken. Doing this allows
other AAPCS functions to be called.
- If the CPU is M-class this is all that needs to be done since the architecture
itself is designed in such a way that functions obeying the normal AAPCS ABI
constraints are valid exception handlers.
- If the CPU is not M-class, the prologue and epilogue are modified to save all
non-banked registers that are used, so that upon return the user-mode state
will not be corrupted. Note that to avoid unnecessary overhead, only
general-purpose (integer) registers are saved in this way. If VFP operations
are needed, that state must be saved manually.
Specifically, interrupt kinds other than "FIQ" will save all core registers
except "lr" and "sp". "FIQ" interrupts will save r0-r7.
- If the CPU is not M-class, the return instruction is changed to one of the
canonical sequences permitted by the architecture for exception return. Where
possible the function itself will make the necessary "lr" adjustments so that
the "preferred return address" is selected.
Unfortunately the compiler is unable to make this guarantee for an "UNDEF"
handler, where the offset from "lr" to the preferred return address depends on
the execution state of the code which generated the exception. In this case
a sequence equivalent to "movs pc, lr" will be used.
abi_tag (gnu::abi_tag)
----------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``abi_tag`` attribute can be applied to a function, variable, class or
inline namespace declaration to modify the mangled name of the entity. It gives
the ability to distinguish between different versions of the same entity but
with different ABI versions supported. For example, a newer version of a class
could have a different set of data members and thus have a different size. Using
the ``abi_tag`` attribute, it is possible to have different mangled names for
a global variable of the class type. Therefor, the old code could keep using
the old manged name and the new code will use the new mangled name with tags.
acquire_capability (acquire_shared_capability, clang::acquire_capability, clang::acquire_shared_capability)
-----------------------------------------------------------------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
Marks a function as acquiring a capability.
interrupt
---------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Clang supports the GNU style ``__attribute__((interrupt))`` attribute on
x86/x86-64 targets.The compiler generates function entry and exit sequences
suitable for use in an interrupt handler when this attribute is present.
The 'IRET' instruction, instead of the 'RET' instruction, is used to return
from interrupt or exception handlers. All registers, except for the EFLAGS
register which is restored by the 'IRET' instruction, are preserved by the
compiler.
Any interruptible-without-stack-switch code must be compiled with
-mno-red-zone since interrupt handlers can and will, because of the
hardware design, touch the red zone.
1. interrupt handler must be declared with a mandatory pointer argument:
.. code-block:: c
struct interrupt_frame
{
uword_t ip;
uword_t cs;
uword_t flags;
uword_t sp;
uword_t ss;
};
__attribute__ ((interrupt))
void f (struct interrupt_frame *frame) {
...
}
2. exception handler:
The exception handler is very similar to the interrupt handler with
a different mandatory function signature:
.. code-block:: c
__attribute__ ((interrupt))
void f (struct interrupt_frame *frame, uword_t error_code) {
...
}
and compiler pops 'ERROR_CODE' off stack before the 'IRET' instruction.
The exception handler should only be used for exceptions which push an
error code and all other exceptions must use the interrupt handler.
The system will crash if the wrong handler is used.
assert_capability (assert_shared_capability, clang::assert_capability, clang::assert_shared_capability)
-------------------------------------------------------------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
Marks a function that dynamically tests whether a capability is held, and halts
the program if it is not held.
assume_aligned (gnu::assume_aligned)
------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
Use ``__attribute__((assume_aligned(<alignment>[,<offset>]))`` on a function
declaration to specify that the return value of the function (which must be a
pointer type) has the specified offset, in bytes, from an address with the
specified alignment. The offset is taken to be zero if omitted.
.. code-block:: c++
// The returned pointer value has 32-byte alignment.
void *a() __attribute__((assume_aligned (32)));
// The returned pointer value is 4 bytes greater than an address having
// 32-byte alignment.
void *b() __attribute__((assume_aligned (32, 4)));
Note that this attribute provides information to the compiler regarding a
condition that the code already ensures is true. It does not cause the compiler
to enforce the provided alignment assumption.
availability
------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
The ``availability`` attribute can be placed on declarations to describe the
lifecycle of that declaration relative to operating system versions. Consider
the function declaration for a hypothetical function ``f``:
.. code-block:: c++
void f(void) __attribute__((availability(macos,introduced=10.4,deprecated=10.6,obsoleted=10.7)));
The availability attribute states that ``f`` was introduced in Mac OS X 10.4,
deprecated in Mac OS X 10.6, and obsoleted in Mac OS X 10.7. This information
is used by Clang to determine when it is safe to use ``f``: for example, if
Clang is instructed to compile code for Mac OS X 10.5, a call to ``f()``
succeeds. If Clang is instructed to compile code for Mac OS X 10.6, the call
succeeds but Clang emits a warning specifying that the function is deprecated.
Finally, if Clang is instructed to compile code for Mac OS X 10.7, the call
fails because ``f()`` is no longer available.
The availability attribute is a comma-separated list starting with the
platform name and then including clauses specifying important milestones in the
declaration's lifetime (in any order) along with additional information. Those
clauses can be:
introduced=\ *version*
The first version in which this declaration was introduced.
deprecated=\ *version*
The first version in which this declaration was deprecated, meaning that
users should migrate away from this API.
obsoleted=\ *version*
The first version in which this declaration was obsoleted, meaning that it
was removed completely and can no longer be used.
unavailable
This declaration is never available on this platform.
message=\ *string-literal*
Additional message text that Clang will provide when emitting a warning or
error about use of a deprecated or obsoleted declaration. Useful to direct
users to replacement APIs.
replacement=\ *string-literal*
Additional message text that Clang will use to provide Fix-It when emitting
a warning about use of a deprecated declaration. The Fix-It will replace
the deprecated declaration with the new declaration specified.
Multiple availability attributes can be placed on a declaration, which may
correspond to different platforms. Only the availability attribute with the
platform corresponding to the target platform will be used; any others will be
ignored. If no availability attribute specifies availability for the current
target platform, the availability attributes are ignored. Supported platforms
are:
``ios``
Apple's iOS operating system. The minimum deployment target is specified by
the ``-mios-version-min=*version*`` or ``-miphoneos-version-min=*version*``
command-line arguments.
``macos``
Apple's Mac OS X operating system. The minimum deployment target is
specified by the ``-mmacosx-version-min=*version*`` command-line argument.
``macosx`` is supported for backward-compatibility reasons, but it is
deprecated.
``tvos``
Apple's tvOS operating system. The minimum deployment target is specified by
the ``-mtvos-version-min=*version*`` command-line argument.
``watchos``
Apple's watchOS operating system. The minimum deployment target is specified by
the ``-mwatchos-version-min=*version*`` command-line argument.
A declaration can typically be used even when deploying back to a platform
version prior to when the declaration was introduced. When this happens, the
declaration is `weakly linked
<https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html>`_,
as if the ``weak_import`` attribute were added to the declaration. A
weakly-linked declaration may or may not be present a run-time, and a program
can determine whether the declaration is present by checking whether the
address of that declaration is non-NULL.
The flag ``strict`` disallows using API when deploying back to a
platform version prior to when the declaration was introduced. An
attempt to use such API before its introduction causes a hard error.
Weakly-linking is almost always a better API choice, since it allows
users to query availability at runtime.
If there are multiple declarations of the same entity, the availability
attributes must either match on a per-platform basis or later
declarations must not have availability attributes for that
platform. For example:
.. code-block:: c
void g(void) __attribute__((availability(macos,introduced=10.4)));
void g(void) __attribute__((availability(macos,introduced=10.4))); // okay, matches
void g(void) __attribute__((availability(ios,introduced=4.0))); // okay, adds a new platform
void g(void); // okay, inherits both macos and ios availability from above.
void g(void) __attribute__((availability(macos,introduced=10.5))); // error: mismatch
When one method overrides another, the overriding method can be more widely available than the overridden method, e.g.,:
.. code-block:: objc
@interface A
- (id)method __attribute__((availability(macos,introduced=10.4)));
- (id)method2 __attribute__((availability(macos,introduced=10.4)));
@end
@interface B : A
- (id)method __attribute__((availability(macos,introduced=10.3))); // okay: method moved into base class later
- (id)method __attribute__((availability(macos,introduced=10.5))); // error: this method was available via the base class in 10.4
@end
_Noreturn
---------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
A function declared as ``_Noreturn`` shall not return to its caller. The
compiler will generate a diagnostic for a function declared as ``_Noreturn``
that appears to be capable of returning to its caller.
noreturn
--------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","X","","", ""
A function declared as ``[[noreturn]]`` shall not return to its caller. The
compiler will generate a diagnostic for a function declared as ``[[noreturn]]``
that appears to be capable of returning to its caller.
carries_dependency
------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``carries_dependency`` attribute specifies dependency propagation into and
out of functions.
When specified on a function or Objective-C method, the ``carries_dependency``
attribute means that the return value carries a dependency out of the function,
so that the implementation need not constrain ordering upon return from that
function. Implementations of the function and its caller may choose to preserve
dependencies instead of emitting memory ordering instructions such as fences.
Note, this attribute does not change the meaning of the program, but may result
in generation of more efficient code.
deprecated (gnu::deprecated)
----------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","X","", ""
The ``deprecated`` attribute can be applied to a function, a variable, or a
type. This is useful when identifying functions, variables, or types that are
expected to be removed in a future version of a program.
Consider the function declaration for a hypothetical function ``f``:
.. code-block:: c++
void f(void) __attribute__((deprecated("message", "replacement")));
When spelled as `__attribute__((deprecated))`, the deprecated attribute can have
two optional string arguments. The first one is the message to display when
emitting the warning; the second one enables the compiler to provide a Fix-It
to replace the deprecated name with a new name. Otherwise, when spelled as
`[[gnu::deprecated]] or [[deprecated]]`, the attribute can have one optional
string argument which is the message to display when emitting the warning.
disable_tail_calls (clang::disable_tail_calls)
----------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``disable_tail_calls`` attribute instructs the backend to not perform tail call optimization inside the marked function.
For example:
.. code-block:: c
int callee(int);
int foo(int a) __attribute__((disable_tail_calls)) {
return callee(a); // This call is not tail-call optimized.
}
Marking virtual functions as ``disable_tail_calls`` is legal.
.. code-block:: c++
int callee(int);
class Base {
public:
[[clang::disable_tail_calls]] virtual int foo1() {
return callee(); // This call is not tail-call optimized.
}
};
class Derived1 : public Base {
public:
int foo1() override {
return callee(); // This call is tail-call optimized.
}
};
enable_if
---------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
.. Note:: Some features of this attribute are experimental. The meaning of
multiple enable_if attributes on a single declaration is subject to change in
a future version of clang. Also, the ABI is not standardized and the name
mangling may change in future versions. To avoid that, use asm labels.
The ``enable_if`` attribute can be placed on function declarations to control
which overload is selected based on the values of the function's arguments.
When combined with the ``overloadable`` attribute, this feature is also
available in C.
.. code-block:: c++
int isdigit(int c);
int isdigit(int c) __attribute__((enable_if(c <= -1 || c > 255, "chosen when 'c' is out of range"))) __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
void foo(char c) {
isdigit(c);
isdigit(10);
isdigit(-10); // results in a compile-time error.
}
The enable_if attribute takes two arguments, the first is an expression written
in terms of the function parameters, the second is a string explaining why this
overload candidate could not be selected to be displayed in diagnostics. The
expression is part of the function signature for the purposes of determining
whether it is a redeclaration (following the rules used when determining
whether a C++ template specialization is ODR-equivalent), but is not part of
the type.
The enable_if expression is evaluated as if it were the body of a
bool-returning constexpr function declared with the arguments of the function
it is being applied to, then called with the parameters at the call site. If the
result is false or could not be determined through constant expression
evaluation, then this overload will not be chosen and the provided string may
be used in a diagnostic if the compile fails as a result.
Because the enable_if expression is an unevaluated context, there are no global
state changes, nor the ability to pass information from the enable_if
expression to the function body. For example, suppose we want calls to
strnlen(strbuf, maxlen) to resolve to strnlen_chk(strbuf, maxlen, size of
strbuf) only if the size of strbuf can be determined:
.. code-block:: c++
__attribute__((always_inline))
static inline size_t strnlen(const char *s, size_t maxlen)
__attribute__((overloadable))
__attribute__((enable_if(__builtin_object_size(s, 0) != -1))),
"chosen when the buffer size is known but 'maxlen' is not")))
{
return strnlen_chk(s, maxlen, __builtin_object_size(s, 0));
}
Multiple enable_if attributes may be applied to a single declaration. In this
case, the enable_if expressions are evaluated from left to right in the
following manner. First, the candidates whose enable_if expressions evaluate to
false or cannot be evaluated are discarded. If the remaining candidates do not
share ODR-equivalent enable_if expressions, the overload resolution is
ambiguous. Otherwise, enable_if overload resolution continues with the next
enable_if attribute on the candidates that have not been discarded and have
remaining enable_if attributes. In this way, we pick the most specific
overload out of a number of viable overloads using enable_if.
.. code-block:: c++
void f() __attribute__((enable_if(true, ""))); // #1
void f() __attribute__((enable_if(true, ""))) __attribute__((enable_if(true, ""))); // #2
void g(int i, int j) __attribute__((enable_if(i, ""))); // #1
void g(int i, int j) __attribute__((enable_if(j, ""))) __attribute__((enable_if(true))); // #2
In this example, a call to f() is always resolved to #2, as the first enable_if
expression is ODR-equivalent for both declarations, but #1 does not have another
enable_if expression to continue evaluating, so the next round of evaluation has
only a single candidate. In a call to g(1, 1), the call is ambiguous even though
#2 has more enable_if attributes, because the first enable_if expressions are
not ODR-equivalent.
Query for this feature with ``__has_attribute(enable_if)``.
Note that functions with one or more ``enable_if`` attributes may not have
their address taken, unless all of the conditions specified by said
``enable_if`` are constants that evaluate to ``true``. For example:
.. code-block:: c
const int TrueConstant = 1;
const int FalseConstant = 0;
int f(int a) __attribute__((enable_if(a > 0, "")));
int g(int a) __attribute__((enable_if(a == 0 || a != 0, "")));
int h(int a) __attribute__((enable_if(1, "")));
int i(int a) __attribute__((enable_if(TrueConstant, "")));
int j(int a) __attribute__((enable_if(FalseConstant, "")));
void fn() {
int (*ptr)(int);
ptr = &f; // error: 'a > 0' is not always true
ptr = &g; // error: 'a == 0 || a != 0' is not a truthy constant
ptr = &h; // OK: 1 is a truthy constant
ptr = &i; // OK: 'TrueConstant' is a truthy constant
ptr = &j; // error: 'FalseConstant' is a constant, but not truthy
}
Because ``enable_if`` evaluation happens during overload resolution,
``enable_if`` may give unintuitive results when used with templates, depending
on when overloads are resolved. In the example below, clang will emit a
diagnostic about no viable overloads for ``foo`` in ``bar``, but not in ``baz``:
.. code-block:: c++
double foo(int i) __attribute__((enable_if(i > 0, "")));
void *foo(int i) __attribute__((enable_if(i <= 0, "")));
template <int I>
auto bar() { return foo(I); }
template <typename T>
auto baz() { return foo(T::number); }
struct WithNumber { constexpr static int number = 1; };
void callThem() {
bar<sizeof(WithNumber)>();
baz<WithNumber>();
}
This is because, in ``bar``, ``foo`` is resolved prior to template
instantiation, so the value for ``I`` isn't known (thus, both ``enable_if``
conditions for ``foo`` fail). However, in ``baz``, ``foo`` is resolved during
template instantiation, so the value for ``T::number`` is known.
flatten (gnu::flatten)
----------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``flatten`` attribute causes calls within the attributed function to
be inlined unless it is impossible to do so, for example if the body of the
callee is unavailable or if the callee has the ``noinline`` attribute.
format (gnu::format)
--------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
Clang supports the ``format`` attribute, which indicates that the function
accepts a ``printf`` or ``scanf``-like format string and corresponding
arguments or a ``va_list`` that contains these arguments.
Please see `GCC documentation about format attribute
<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ to find details
about attribute syntax.
Clang implements two kinds of checks with this attribute.
#. Clang checks that the function with the ``format`` attribute is called with
a format string that uses format specifiers that are allowed, and that
arguments match the format string. This is the ``-Wformat`` warning, it is
on by default.
#. Clang checks that the format string argument is a literal string. This is
the ``-Wformat-nonliteral`` warning, it is off by default.
Clang implements this mostly the same way as GCC, but there is a difference
for functions that accept a ``va_list`` argument (for example, ``vprintf``).
GCC does not emit ``-Wformat-nonliteral`` warning for calls to such
functions. Clang does not warn if the format string comes from a function
parameter, where the function is annotated with a compatible attribute,
otherwise it warns. For example:
.. code-block:: c
__attribute__((__format__ (__scanf__, 1, 3)))
void foo(const char* s, char *buf, ...) {
va_list ap;
va_start(ap, buf);
vprintf(s, ap); // warning: format string is not a string literal
}
In this case we warn because ``s`` contains a format string for a
``scanf``-like function, but it is passed to a ``printf``-like function.
If the attribute is removed, clang still warns, because the format string is
not a string literal.
Another example:
.. code-block:: c
__attribute__((__format__ (__printf__, 1, 3)))
void foo(const char* s, char *buf, ...) {
va_list ap;
va_start(ap, buf);
vprintf(s, ap); // warning
}
In this case Clang does not warn because the format string ``s`` and
the corresponding arguments are annotated. If the arguments are
incorrect, the caller of ``foo`` will receive a warning.
ifunc (gnu::ifunc)
------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
``__attribute__((ifunc("resolver")))`` is used to mark that the address of a declaration should be resolved at runtime by calling a resolver function.
The symbol name of the resolver function is given in quotes. A function with this name (after mangling) must be defined in the current translation unit; it may be ``static``. The resolver function should take no arguments and return a pointer.
The ``ifunc`` attribute may only be used on a function declaration. A function declaration with an ``ifunc`` attribute is considered to be a definition of the declared entity. The entity must not have weak linkage; for example, in C++, it cannot be applied to a declaration if a definition at that location would be considered inline.
Not all targets support this attribute. ELF targets support this attribute when using binutils v2.20.1 or higher and glibc v2.11.1 or higher. Non-ELF targets currently do not support this attribute.
internal_linkage (clang::internal_linkage)
------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``internal_linkage`` attribute changes the linkage type of the declaration to internal.
This is similar to C-style ``static``, but can be used on classes and class methods. When applied to a class definition,
this attribute affects all methods and static data members of that class.
This can be used to contain the ABI of a C++ library by excluding unwanted class methods from the export tables.
interrupt
---------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Clang supports the GNU style ``__attribute__((interrupt("ARGUMENT")))`` attribute on
MIPS targets. This attribute may be attached to a function definition and instructs
the backend to generate appropriate function entry/exit code so that it can be used
directly as an interrupt service routine.
By default, the compiler will produce a function prologue and epilogue suitable for
an interrupt service routine that handles an External Interrupt Controller (eic)
generated interrupt. This behaviour can be explicitly requested with the "eic"
argument.
Otherwise, for use with vectored interrupt mode, the argument passed should be
of the form "vector=LEVEL" where LEVEL is one of the following values:
"sw0", "sw1", "hw0", "hw1", "hw2", "hw3", "hw4", "hw5". The compiler will
then set the interrupt mask to the corresponding level which will mask all
interrupts up to and including the argument.
The semantics are as follows:
- The prologue is modified so that the Exception Program Counter (EPC) and
Status coprocessor registers are saved to the stack. The interrupt mask is
set so that the function can only be interrupted by a higher priority
interrupt. The epilogue will restore the previous values of EPC and Status.
- The prologue and epilogue are modified to save and restore all non-kernel
registers as necessary.
- The FPU is disabled in the prologue, as the floating pointer registers are not
spilled to the stack.
- The function return sequence is changed to use an exception return instruction.
- The parameter sets the interrupt mask for the function corresponding to the
interrupt level specified. If no mask is specified the interrupt mask
defaults to "eic".
noalias
-------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","X","", ""
The ``noalias`` attribute indicates that the only memory accesses inside
function are loads and stores from objects pointed to by its pointer-typed
arguments, with arbitrary offsets.
noduplicate (clang::noduplicate)
--------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``noduplicate`` attribute can be placed on function declarations to control
whether function calls to this function can be duplicated or not as a result of
optimizations. This is required for the implementation of functions with
certain special requirements, like the OpenCL "barrier" function, that might
need to be run concurrently by all the threads that are executing in lockstep
on the hardware. For example this attribute applied on the function
"nodupfunc" in the code below avoids that:
.. code-block:: c
void nodupfunc() __attribute__((noduplicate));
// Setting it as a C++11 attribute is also valid
// void nodupfunc() [[clang::noduplicate]];
void foo();
void bar();
nodupfunc();
if (a > n) {
foo();
} else {
bar();
}
gets possibly modified by some optimizations into code similar to this:
.. code-block:: c
if (a > n) {
nodupfunc();
foo();
} else {
nodupfunc();
bar();
}
where the call to "nodupfunc" is duplicated and sunk into the two branches
of the condition.
no_sanitize (clang::no_sanitize)
--------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
Use the ``no_sanitize`` attribute on a function declaration to specify
that a particular instrumentation or set of instrumentations should not be
applied to that function. The attribute takes a list of string literals,
which have the same meaning as values accepted by the ``-fno-sanitize=``
flag. For example, ``__attribute__((no_sanitize("address", "thread")))``
specifies that AddressSanitizer and ThreadSanitizer should not be applied
to the function.
See :ref:`Controlling Code Generation <controlling-code-generation>` for a
full list of supported sanitizer flags.
no_sanitize_address (no_address_safety_analysis, gnu::no_address_safety_analysis, gnu::no_sanitize_address)
-----------------------------------------------------------------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
.. _langext-address_sanitizer:
Use ``__attribute__((no_sanitize_address))`` on a function declaration to
specify that address safety instrumentation (e.g. AddressSanitizer) should
not be applied to that function.
no_sanitize_thread
------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
.. _langext-thread_sanitizer:
Use ``__attribute__((no_sanitize_thread))`` on a function declaration to
specify that checks for data races on plain (non-atomic) memory accesses should
not be inserted by ThreadSanitizer. The function is still instrumented by the
tool to avoid false positives and provide meaningful stack traces.
no_sanitize_memory
------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
.. _langext-memory_sanitizer:
Use ``__attribute__((no_sanitize_memory))`` on a function declaration to
specify that checks for uninitialized memory should not be inserted
(e.g. by MemorySanitizer). The function may still be instrumented by the tool
to avoid false positives in other places.
no_split_stack (gnu::no_split_stack)
------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``no_split_stack`` attribute disables the emission of the split stack
preamble for a particular function. It has no effect if ``-fsplit-stack``
is not specified.
not_tail_called (clang::not_tail_called)
----------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``not_tail_called`` attribute prevents tail-call optimization on statically bound calls. It has no effect on indirect calls. Virtual functions, objective-c methods, and functions marked as ``always_inline`` cannot be marked as ``not_tail_called``.
For example, it prevents tail-call optimization in the following case:
.. code-block:: c
int __attribute__((not_tail_called)) foo1(int);
int foo2(int a) {
return foo1(a); // No tail-call optimization on direct calls.
}
However, it doesn't prevent tail-call optimization in this case:
.. code-block:: c
int __attribute__((not_tail_called)) foo1(int);
int foo2(int a) {
int (*fn)(int) = &foo1;
// not_tail_called has no effect on an indirect call even if the call can be
// resolved at compile time.
return (*fn)(a);
}
Marking virtual functions as ``not_tail_called`` is an error:
.. code-block:: c++
class Base {
public:
// not_tail_called on a virtual function is an error.
[[clang::not_tail_called]] virtual int foo1();
virtual int foo2();
// Non-virtual functions can be marked ``not_tail_called``.
[[clang::not_tail_called]] int foo3();
};
class Derived1 : public Base {
public:
int foo1() override;
// not_tail_called on a virtual function is an error.
[[clang::not_tail_called]] int foo2() override;
};
#pragma omp declare simd
------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","", "X"
The `declare simd` construct can be applied to a function to enable the creation
of one or more versions that can process multiple arguments using SIMD
instructions from a single invocation in a SIMD loop. The `declare simd`
directive is a declarative directive. There may be multiple `declare simd`
directives for a function. The use of a `declare simd` construct on a function
enables the creation of SIMD versions of the associated function that can be
used to process multiple arguments from a single invocation from a SIMD loop
concurrently.
The syntax of the `declare simd` construct is as follows:
.. code-block:: c
#pragma omp declare simd [clause[[,] clause] ...] new-line
[#pragma omp declare simd [clause[[,] clause] ...] new-line]
[...]
function definition or declaration
where clause is one of the following:
.. code-block:: c
simdlen(length)
linear(argument-list[:constant-linear-step])
aligned(argument-list[:alignment])
uniform(argument-list)
inbranch
notinbranch
#pragma omp declare target
--------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","", "X"
The `declare target` directive specifies that variables and functions are mapped
to a device for OpenMP offload mechanism.
The syntax of the declare target directive is as follows:
.. code-block:: c
#pragma omp declare target new-line
declarations-definition-seq
#pragma omp end declare target new-line
objc_boxable
------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Structs and unions marked with the ``objc_boxable`` attribute can be used
with the Objective-C boxed expression syntax, ``@(...)``.
**Usage**: ``__attribute__((objc_boxable))``. This attribute
can only be placed on a declaration of a trivially-copyable struct or union:
.. code-block:: objc
struct __attribute__((objc_boxable)) some_struct {
int i;
};
union __attribute__((objc_boxable)) some_union {
int i;
float f;
};
typedef struct __attribute__((objc_boxable)) _some_struct some_struct;
// ...
some_struct ss;
NSValue *boxed = @(ss);
objc_method_family
------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Many methods in Objective-C have conventional meanings determined by their
selectors. It is sometimes useful to be able to mark a method as having a
particular conventional meaning despite not having the right selector, or as
not having the conventional meaning that its selector would suggest. For these
use cases, we provide an attribute to specifically describe the "method family"
that a method belongs to.
**Usage**: ``__attribute__((objc_method_family(X)))``, where ``X`` is one of
``none``, ``alloc``, ``copy``, ``init``, ``mutableCopy``, or ``new``. This
attribute can only be placed at the end of a method declaration:
.. code-block:: objc
- (NSString *)initMyStringValue __attribute__((objc_method_family(none)));
Users who do not wish to change the conventional meaning of a method, and who
merely want to document its non-standard retain and release semantics, should
use the retaining behavior attributes (``ns_returns_retained``,
``ns_returns_not_retained``, etc).
Query for this feature with ``__has_attribute(objc_method_family)``.
objc_requires_super
-------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Some Objective-C classes allow a subclass to override a particular method in a
parent class but expect that the overriding method also calls the overridden
method in the parent class. For these cases, we provide an attribute to
designate that a method requires a "call to ``super``" in the overriding
method in the subclass.
**Usage**: ``__attribute__((objc_requires_super))``. This attribute can only
be placed at the end of a method declaration:
.. code-block:: objc
- (void)foo __attribute__((objc_requires_super));
This attribute can only be applied the method declarations within a class, and
not a protocol. Currently this attribute does not enforce any placement of
where the call occurs in the overriding method (such as in the case of
``-dealloc`` where the call must appear at the end). It checks only that it
exists.
Note that on both OS X and iOS that the Foundation framework provides a
convenience macro ``NS_REQUIRES_SUPER`` that provides syntactic sugar for this
attribute:
.. code-block:: objc
- (void)foo NS_REQUIRES_SUPER;
This macro is conditionally defined depending on the compiler's support for
this attribute. If the compiler does not support the attribute the macro
expands to nothing.
Operationally, when a method has this annotation the compiler will warn if the
implementation of an override in a subclass does not call super. For example:
.. code-block:: objc
warning: method possibly missing a [super AnnotMeth] call
- (void) AnnotMeth{};
^
objc_runtime_name
-----------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
By default, the Objective-C interface or protocol identifier is used
in the metadata name for that object. The `objc_runtime_name`
attribute allows annotated interfaces or protocols to use the
specified string argument in the object's metadata name instead of the
default name.
**Usage**: ``__attribute__((objc_runtime_name("MyLocalName")))``. This attribute
can only be placed before an @protocol or @interface declaration:
.. code-block:: objc
__attribute__((objc_runtime_name("MyLocalName")))
@interface Message
@end
objc_runtime_visible
--------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
This attribute specifies that the Objective-C class to which it applies is visible to the Objective-C runtime but not to the linker. Classes annotated with this attribute cannot be subclassed and cannot have categories defined for them.
optnone (clang::optnone)
------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``optnone`` attribute suppresses essentially all optimizations
on a function or method, regardless of the optimization level applied to
the compilation unit as a whole. This is particularly useful when you
need to debug a particular function, but it is infeasible to build the
entire application without optimization. Avoiding optimization on the
specified function can improve the quality of the debugging information
for that function.
This attribute is incompatible with the ``always_inline`` and ``minsize``
attributes.
overloadable
------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Clang provides support for C++ function overloading in C. Function overloading
in C is introduced using the ``overloadable`` attribute. For example, one
might provide several overloaded versions of a ``tgsin`` function that invokes
the appropriate standard function computing the sine of a value with ``float``,
``double``, or ``long double`` precision:
.. code-block:: c
#include <math.h>
float __attribute__((overloadable)) tgsin(float x) { return sinf(x); }
double __attribute__((overloadable)) tgsin(double x) { return sin(x); }
long double __attribute__((overloadable)) tgsin(long double x) { return sinl(x); }
Given these declarations, one can call ``tgsin`` with a ``float`` value to
receive a ``float`` result, with a ``double`` to receive a ``double`` result,
etc. Function overloading in C follows the rules of C++ function overloading
to pick the best overload given the call arguments, with a few C-specific
semantics:
* Conversion from ``float`` or ``double`` to ``long double`` is ranked as a
floating-point promotion (per C99) rather than as a floating-point conversion
(as in C++).
* A conversion from a pointer of type ``T*`` to a pointer of type ``U*`` is
considered a pointer conversion (with conversion rank) if ``T`` and ``U`` are
compatible types.
* A conversion from type ``T`` to a value of type ``U`` is permitted if ``T``
and ``U`` are compatible types. This conversion is given "conversion" rank.
The declaration of ``overloadable`` functions is restricted to function
declarations and definitions. Most importantly, if any function with a given
name is given the ``overloadable`` attribute, then all function declarations
and definitions with that name (and in that scope) must have the
``overloadable`` attribute. This rule even applies to redeclarations of
functions whose original declaration had the ``overloadable`` attribute, e.g.,
.. code-block:: c
int f(int) __attribute__((overloadable));
float f(float); // error: declaration of "f" must have the "overloadable" attribute
int g(int) __attribute__((overloadable));
int g(int) { } // error: redeclaration of "g" must also have the "overloadable" attribute
Functions marked ``overloadable`` must have prototypes. Therefore, the
following code is ill-formed:
.. code-block:: c
int h() __attribute__((overloadable)); // error: h does not have a prototype
However, ``overloadable`` functions are allowed to use a ellipsis even if there
are no named parameters (as is permitted in C++). This feature is particularly
useful when combined with the ``unavailable`` attribute:
.. code-block:: c++
void honeypot(...) __attribute__((overloadable, unavailable)); // calling me is an error
Functions declared with the ``overloadable`` attribute have their names mangled
according to the same rules as C++ function names. For example, the three
``tgsin`` functions in our motivating example get the mangled names
``_Z5tgsinf``, ``_Z5tgsind``, and ``_Z5tgsine``, respectively. There are two
caveats to this use of name mangling:
* Future versions of Clang may change the name mangling of functions overloaded
in C, so you should not depend on an specific mangling. To be completely
safe, we strongly urge the use of ``static inline`` with ``overloadable``
functions.
* The ``overloadable`` attribute has almost no meaning when used in C++,
because names will already be mangled and functions are already overloadable.
However, when an ``overloadable`` function occurs within an ``extern "C"``
linkage specification, it's name *will* be mangled in the same way as it
would in C.
Query for this feature with ``__has_extension(attribute_overloadable)``.
release_capability (release_shared_capability, clang::release_capability, clang::release_shared_capability)
-----------------------------------------------------------------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
Marks a function as releasing a capability.
kernel
------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
``__attribute__((kernel))`` is used to mark a ``kernel`` function in
RenderScript.
In RenderScript, ``kernel`` functions are used to express data-parallel
computations. The RenderScript runtime efficiently parallelizes ``kernel``
functions to run on computational resources such as multi-core CPUs and GPUs.
See the RenderScript_ documentation for more information.
.. _RenderScript: https://developer.android.com/guide/topics/renderscript/compute.html
target (gnu::target)
--------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
Clang supports the GNU style ``__attribute__((target("OPTIONS")))`` attribute.
This attribute may be attached to a function definition and instructs
the backend to use different code generation options than were passed on the
command line.
The current set of options correspond to the existing "subtarget features" for
the target with or without a "-mno-" in front corresponding to the absence
of the feature, as well as ``arch="CPU"`` which will change the default "CPU"
for the function.
Example "subtarget features" from the x86 backend include: "mmx", "sse", "sse4.2",
"avx", "xop" and largely correspond to the machine specific options handled by
the front end.
try_acquire_capability (try_acquire_shared_capability, clang::try_acquire_capability, clang::try_acquire_shared_capability)
---------------------------------------------------------------------------------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
Marks a function that attempts to acquire a capability. This function may fail to
actually acquire the capability; they accept a Boolean value determining
whether acquiring the capability means success (true), or failing to acquire
the capability means success (false).
nodiscard, warn_unused_result, clang::warn_unused_result, gnu::warn_unused_result
---------------------------------------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
Clang supports the ability to diagnose when the results of a function call
expression are discarded under suspicious circumstances. A diagnostic is
generated when a function or its return type is marked with ``[[nodiscard]]``
(or ``__attribute__((warn_unused_result))``) and the function call appears as a
potentially-evaluated discarded-value expression that is not explicitly cast to
`void`.
.. code-block: c++
struct [[nodiscard]] error_info { /*...*/ };
error_info enable_missile_safety_mode();
void launch_missiles();
void test_missiles() {
enable_missile_safety_mode(); // diagnoses
launch_missiles();
}
error_info &foo();
void f() { foo(); } // Does not diagnose, error_info is a reference.
xray_always_instrument (clang::xray_always_instrument), xray_never_instrument (clang::xray_never_instrument)
------------------------------------------------------------------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
``__attribute__((xray_always_instrument))`` or ``[[clang::xray_always_instrument]]`` is used to mark member functions (in C++), methods (in Objective C), and free functions (in C, C++, and Objective C) to be instrumented with XRay. This will cause the function to always have space at the beginning and exit points to allow for runtime patching.
Conversely, ``__attribute__((xray_never_instrument))`` or ``[[clang::xray_never_instrument]]`` will inhibit the insertion of these instrumentation points.
If a function has neither of these attributes, they become subject to the XRay heuristics used to determine whether a function should be instrumented or otherwise.
Variable Attributes
===================
dllexport (gnu::dllexport)
--------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","X","", ""
The ``__declspec(dllexport)`` attribute declares a variable, function, or
Objective-C interface to be exported from the module. It is available under the
``-fdeclspec`` flag for compatibility with various compilers. The primary use
is for COFF object files which explicitly specify what interfaces are available
for external use. See the dllexport_ documentation on MSDN for more
information.
.. _dllexport: https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx
dllimport (gnu::dllimport)
--------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","X","", ""
The ``__declspec(dllimport)`` attribute declares a variable, function, or
Objective-C interface to be imported from an external module. It is available
under the ``-fdeclspec`` flag for compatibility with various compilers. The
primary use is for COFF object files which explicitly specify what interfaces
are imported from external modules. See the dllimport_ documentation on MSDN
for more information.
.. _dllimport: https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx
init_seg
--------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","", "X"
The attribute applied by ``pragma init_seg()`` controls the section into
which global initialization function pointers are emitted. It is only
available with ``-fms-extensions``. Typically, this function pointer is
emitted into ``.CRT$XCU`` on Windows. The user can change the order of
initialization by using a different section name with the same
``.CRT$XC`` prefix and a suffix that sorts lexicographically before or
after the standard ``.CRT$XCU`` sections. See the init_seg_
documentation on MSDN for more information.
.. _init_seg: http://msdn.microsoft.com/en-us/library/7977wcck(v=vs.110).aspx
nodebug (gnu::nodebug)
----------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``nodebug`` attribute allows you to suppress debugging information for a
function or method, or for a variable that is not a parameter or a non-static
data member.
nosvm
-----
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
OpenCL 2.0 supports the optional ``__attribute__((nosvm))`` qualifier for
pointer variable. It informs the compiler that the pointer does not refer
to a shared virtual memory region. See OpenCL v2.0 s6.7.2 for details.
Since it is not widely used and has been removed from OpenCL 2.1, it is ignored
by Clang.
pass_object_size
----------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
.. Note:: The mangling of functions with parameters that are annotated with
``pass_object_size`` is subject to change. You can get around this by
using ``__asm__("foo")`` to explicitly name your functions, thus preserving
your ABI; also, non-overloadable C functions with ``pass_object_size`` are
not mangled.
The ``pass_object_size(Type)`` attribute can be placed on function parameters to
instruct clang to call ``__builtin_object_size(param, Type)`` at each callsite
of said function, and implicitly pass the result of this call in as an invisible
argument of type ``size_t`` directly after the parameter annotated with
``pass_object_size``. Clang will also replace any calls to
``__builtin_object_size(param, Type)`` in the function by said implicit
parameter.
Example usage:
.. code-block:: c
int bzero1(char *const p __attribute__((pass_object_size(0))))
__attribute__((noinline)) {
int i = 0;
for (/**/; i < (int)__builtin_object_size(p, 0); ++i) {
p[i] = 0;
}
return i;
}
int main() {
char chars[100];
int n = bzero1(&chars[0]);
assert(n == sizeof(chars));
return 0;
}
If successfully evaluating ``__builtin_object_size(param, Type)`` at the
callsite is not possible, then the "failed" value is passed in. So, using the
definition of ``bzero1`` from above, the following code would exit cleanly:
.. code-block:: c
int main2(int argc, char *argv[]) {
int n = bzero1(argv);
assert(n == -1);
return 0;
}
``pass_object_size`` plays a part in overload resolution. If two overload
candidates are otherwise equally good, then the overload with one or more
parameters with ``pass_object_size`` is preferred. This implies that the choice
between two identical overloads both with ``pass_object_size`` on one or more
parameters will always be ambiguous; for this reason, having two such overloads
is illegal. For example:
.. code-block:: c++
#define PS(N) __attribute__((pass_object_size(N)))
// OK
void Foo(char *a, char *b); // Overload A
// OK -- overload A has no parameters with pass_object_size.
void Foo(char *a PS(0), char *b PS(0)); // Overload B
// Error -- Same signature (sans pass_object_size) as overload B, and both
// overloads have one or more parameters with the pass_object_size attribute.
void Foo(void *a PS(0), void *b);
// OK
void Bar(void *a PS(0)); // Overload C
// OK
void Bar(char *c PS(1)); // Overload D
void main() {
char known[10], *unknown;
Foo(unknown, unknown); // Calls overload B
Foo(known, unknown); // Calls overload B
Foo(unknown, known); // Calls overload B
Foo(known, known); // Calls overload B
Bar(known); // Calls overload D
Bar(unknown); // Calls overload D
}
Currently, ``pass_object_size`` is a bit restricted in terms of its usage:
* Only one use of ``pass_object_size`` is allowed per parameter.
* It is an error to take the address of a function with ``pass_object_size`` on
any of its parameters. If you wish to do this, you can create an overload
without ``pass_object_size`` on any parameters.
* It is an error to apply the ``pass_object_size`` attribute to parameters that
are not pointers. Additionally, any parameter that ``pass_object_size`` is
applied to must be marked ``const`` at its function's definition.
section (gnu::section, __declspec(allocate))
--------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","X","", ""
The ``section`` attribute allows you to specify a specific section a
global variable or function should be in after translation.
swiftcall (gnu::swiftcall)
--------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``swiftcall`` attribute indicates that a function should be called
using the Swift calling convention for a function or function pointer.
The lowering for the Swift calling convention, as described by the Swift
ABI documentation, occurs in multiple phases. The first, "high-level"
phase breaks down the formal parameters and results into innately direct
and indirect components, adds implicit paraameters for the generic
signature, and assigns the context and error ABI treatments to parameters
where applicable. The second phase breaks down the direct parameters
and results from the first phase and assigns them to registers or the
stack. The ``swiftcall`` convention only handles this second phase of
lowering; the C function type must accurately reflect the results
of the first phase, as follows:
- Results classified as indirect by high-level lowering should be
represented as parameters with the ``swift_indirect_result`` attribute.
- Results classified as direct by high-level lowering should be represented
as follows:
- First, remove any empty direct results.
- If there are no direct results, the C result type should be ``void``.
- If there is one direct result, the C result type should be a type with
the exact layout of that result type.
- If there are a multiple direct results, the C result type should be
a struct type with the exact layout of a tuple of those results.
- Parameters classified as indirect by high-level lowering should be
represented as parameters of pointer type.
- Parameters classified as direct by high-level lowering should be
omitted if they are empty types; otherwise, they should be represented
as a parameter type with a layout exactly matching the layout of the
Swift parameter type.
- The context parameter, if present, should be represented as a trailing
parameter with the ``swift_context`` attribute.
- The error result parameter, if present, should be represented as a
trailing parameter (always following a context parameter) with the
``swift_error_result`` attribute.
``swiftcall`` does not support variadic arguments or unprototyped functions.
The parameter ABI treatment attributes are aspects of the function type.
A function type which which applies an ABI treatment attribute to a
parameter is a different type from an otherwise-identical function type
that does not. A single parameter may not have multiple ABI treatment
attributes.
Support for this feature is target-dependent, although it should be
supported on every target that Swift supports. Query for this support
with ``__has_attribute(swiftcall)``. This implies support for the
``swift_context``, ``swift_error_result``, and ``swift_indirect_result``
attributes.
swift_context (gnu::swift_context)
----------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``swift_context`` attribute marks a parameter of a ``swiftcall``
function as having the special context-parameter ABI treatment.
This treatment generally passes the context value in a special register
which is normally callee-preserved.
A ``swift_context`` parameter must either be the last parameter or must be
followed by a ``swift_error_result`` parameter (which itself must always be
the last parameter).
A context parameter must have pointer or reference type.
swift_error_result (gnu::swift_error_result)
--------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``swift_error_result`` attribute marks a parameter of a ``swiftcall``
function as having the special error-result ABI treatment.
This treatment generally passes the underlying error value in and out of
the function through a special register which is normally callee-preserved.
This is modeled in C by pretending that the register is addressable memory:
- The caller appears to pass the address of a variable of pointer type.
The current value of this variable is copied into the register before
the call; if the call returns normally, the value is copied back into the
variable.
- The callee appears to receive the address of a variable. This address
is actually a hidden location in its own stack, initialized with the
value of the register upon entry. When the function returns normally,
the value in that hidden location is written back to the register.
A ``swift_error_result`` parameter must be the last parameter, and it must be
preceded by a ``swift_context`` parameter.
A ``swift_error_result`` parameter must have type ``T**`` or ``T*&`` for some
type T. Note that no qualifiers are permitted on the intermediate level.
It is undefined behavior if the caller does not pass a pointer or
reference to a valid object.
The standard convention is that the error value itself (that is, the
value stored in the apparent argument) will be null upon function entry,
but this is not enforced by the ABI.
swift_indirect_result (gnu::swift_indirect_result)
--------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``swift_indirect_result`` attribute marks a parameter of a ``swiftcall``
function as having the special indirect-result ABI treatmenet.
This treatment gives the parameter the target's normal indirect-result
ABI treatment, which may involve passing it differently from an ordinary
parameter. However, only the first indirect result will receive this
treatment. Furthermore, low-level lowering may decide that a direct result
must be returned indirectly; if so, this will take priority over the
``swift_indirect_result`` parameters.
A ``swift_indirect_result`` parameter must either be the first parameter or
follow another ``swift_indirect_result`` parameter.
A ``swift_indirect_result`` parameter must have type ``T*`` or ``T&`` for
some object type ``T``. If ``T`` is a complete type at the point of
definition of a function, it is undefined behavior if the argument
value does not point to storage of adequate size and alignment for a
value of type ``T``.
Making indirect results explicit in the signature allows C functions to
directly construct objects into them without relying on language
optimizations like C++'s named return value optimization (NRVO).
tls_model (gnu::tls_model)
--------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``tls_model`` attribute allows you to specify which thread-local storage
model to use. It accepts the following strings:
* global-dynamic
* local-dynamic
* initial-exec
* local-exec
TLS models are mutually exclusive.
thread
------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","X","", ""
The ``__declspec(thread)`` attribute declares a variable with thread local
storage. It is available under the ``-fms-extensions`` flag for MSVC
compatibility. See the documentation for `__declspec(thread)`_ on MSDN.
.. _`__declspec(thread)`: http://msdn.microsoft.com/en-us/library/9w1sdazb.aspx
In Clang, ``__declspec(thread)`` is generally equivalent in functionality to the
GNU ``__thread`` keyword. The variable must not have a destructor and must have
a constant initializer, if any. The attribute only applies to variables
declared with static storage duration, such as globals, class static data
members, and static locals.
maybe_unused, unused, gnu::unused
---------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
When passing the ``-Wunused`` flag to Clang, entities that are unused by the
program may be diagnosed. The ``[[maybe_unused]]`` (or
``__attribute__((unused))``) attribute can be used to silence such diagnostics
when the entity cannot be removed. For instance, a local variable may exist
solely for use in an ``assert()`` statement, which makes the local variable
unused when ``NDEBUG`` is defined.
The attribute may be applied to the declaration of a class, a typedef, a
variable, a function or method, a function parameter, an enumeration, an
enumerator, a non-static data member, or a label.
.. code-block: c++
#include <cassert>
[[maybe_unused]] void f([[maybe_unused]] bool thing1,
[[maybe_unused]] bool thing2) {
[[maybe_unused]] bool b = thing1 && thing2;
assert(b);
}
Type Attributes
===============
align_value
-----------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
The align_value attribute can be added to the typedef of a pointer type or the
declaration of a variable of pointer or reference type. It specifies that the
pointer will point to, or the reference will bind to, only objects with at
least the provided alignment. This alignment value must be some positive power
of 2.
.. code-block:: c
typedef double * aligned_double_ptr __attribute__((align_value(64)));
void foo(double & x __attribute__((align_value(128)),
aligned_double_ptr y) { ... }
If the pointer value does not have the specified alignment at runtime, the
behavior of the program is undefined.
empty_bases
-----------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","X","", ""
The empty_bases attribute permits the compiler to utilize the
empty-base-optimization more frequently.
This attribute only applies to struct, class, and union types.
It is only supported when using the Microsoft C++ ABI.
flag_enum
---------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
This attribute can be added to an enumerator to signal to the compiler that it
is intended to be used as a flag type. This will cause the compiler to assume
that the range of the type includes all of the values that you can get by
manipulating bits of the enumerator when issuing warnings.
lto_visibility_public (clang::lto_visibility_public)
----------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","X","","", ""
See :doc:`LTOVisibility`.
layout_version
--------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","X","", ""
The layout_version attribute requests that the compiler utilize the class
layout rules of a particular compiler version.
This attribute only applies to struct, class, and union types.
It is only supported when using the Microsoft C++ ABI.
__single_inhertiance, __multiple_inheritance, __virtual_inheritance
-------------------------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
This collection of keywords is enabled under ``-fms-extensions`` and controls
the pointer-to-member representation used on ``*-*-win32`` targets.
The ``*-*-win32`` targets utilize a pointer-to-member representation which
varies in size and alignment depending on the definition of the underlying
class.
However, this is problematic when a forward declaration is only available and
no definition has been made yet. In such cases, Clang is forced to utilize the
most general representation that is available to it.
These keywords make it possible to use a pointer-to-member representation other
than the most general one regardless of whether or not the definition will ever
be present in the current translation unit.
This family of keywords belong between the ``class-key`` and ``class-name``:
.. code-block:: c++
struct __single_inheritance S;
int S::*i;
struct S {};
This keyword can be applied to class templates but only has an effect when used
on full specializations:
.. code-block:: c++
template <typename T, typename U> struct __single_inheritance A; // warning: inheritance model ignored on primary template
template <typename T> struct __multiple_inheritance A<T, T>; // warning: inheritance model ignored on partial specialization
template <> struct __single_inheritance A<int, float>;
Note that choosing an inheritance model less general than strictly necessary is
an error:
.. code-block:: c++
struct __multiple_inheritance S; // error: inheritance model does not match definition
int S::*i;
struct S {};
novtable
--------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","X","", ""
This attribute can be added to a class declaration or definition to signal to
the compiler that constructors and destructors will not reference the virtual
function table. It is only supported when using the Microsoft C++ ABI.
Statement Attributes
====================
fallthrough, clang::fallthrough
-------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","X","","", ""
The ``fallthrough`` (or ``clang::fallthrough``) attribute is used
to annotate intentional fall-through
between switch labels. It can only be applied to a null statement placed at a
point of execution between any statement and the next switch label. It is
common to mark these places with a specific comment, but this attribute is
meant to replace comments with a more strict annotation, which can be checked
by the compiler. This attribute doesn't change semantics of the code and can
be used wherever an intended fall-through occurs. It is designed to mimic
control-flow statements like ``break;``, so it can be placed in most places
where ``break;`` can, but only if there are no statements on the execution path
between it and the next switch label.
By default, Clang does not warn on unannotated fallthrough from one ``switch``
case to another. Diagnostics on fallthrough without a corresponding annotation
can be enabled with the ``-Wimplicit-fallthrough`` argument.
Here is an example:
.. code-block:: c++
// compile with -Wimplicit-fallthrough
switch (n) {
case 22:
case 33: // no warning: no statements between case labels
f();
case 44: // warning: unannotated fall-through
g();
[[clang::fallthrough]];
case 55: // no warning
if (x) {
h();
break;
}
else {
i();
[[clang::fallthrough]];
}
case 66: // no warning
p();
[[clang::fallthrough]]; // warning: fallthrough annotation does not
// directly precede case label
q();
case 77: // warning: unannotated fall-through
r();
}
#pragma clang loop
------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","", "X"
The ``#pragma clang loop`` directive allows loop optimization hints to be
specified for the subsequent loop. The directive allows vectorization,
interleaving, and unrolling to be enabled or disabled. Vector width as well
as interleave and unrolling count can be manually specified. See
`language extensions
<http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations>`_
for details.
#pragma unroll, #pragma nounroll
--------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","", "X"
Loop unrolling optimization hints can be specified with ``#pragma unroll`` and
``#pragma nounroll``. The pragma is placed immediately before a for, while,
do-while, or c++11 range-based for loop.
Specifying ``#pragma unroll`` without a parameter directs the loop unroller to
attempt to fully unroll the loop if the trip count is known at compile time and
attempt to partially unroll the loop if the trip count is not known at compile
time:
.. code-block:: c++
#pragma unroll
for (...) {
...
}
Specifying the optional parameter, ``#pragma unroll _value_``, directs the
unroller to unroll the loop ``_value_`` times. The parameter may optionally be
enclosed in parentheses:
.. code-block:: c++
#pragma unroll 16
for (...) {
...
}
#pragma unroll(16)
for (...) {
...
}
Specifying ``#pragma nounroll`` indicates that the loop should not be unrolled:
.. code-block:: c++
#pragma nounroll
for (...) {
...
}
``#pragma unroll`` and ``#pragma unroll _value_`` have identical semantics to
``#pragma clang loop unroll(full)`` and
``#pragma clang loop unroll_count(_value_)`` respectively. ``#pragma nounroll``
is equivalent to ``#pragma clang loop unroll(disable)``. See
`language extensions
<http://clang.llvm.org/docs/LanguageExtensions.html#extensions-for-loop-hint-optimizations>`_
for further details including limitations of the unroll hints.
__read_only, __write_only, __read_write (read_only, write_only, read_write)
---------------------------------------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
The access qualifiers must be used with image object arguments or pipe arguments
to declare if they are being read or written by a kernel or function.
The read_only/__read_only, write_only/__write_only and read_write/__read_write
names are reserved for use as access qualifiers and shall not be used otherwise.
.. code-block:: c
kernel void
foo (read_only image2d_t imageA,
write_only image2d_t imageB) {
...
}
In the above example imageA is a read-only 2D image object, and imageB is a
write-only 2D image object.
The read_write (or __read_write) qualifier can not be used with pipe.
More details can be found in the OpenCL C language Spec v2.0, Section 6.6.
__attribute__((opencl_unroll_hint))
-----------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
The opencl_unroll_hint attribute qualifier can be used to specify that a loop
(for, while and do loops) can be unrolled. This attribute qualifier can be
used to specify full unrolling or partial unrolling by a specified amount.
This is a compiler hint and the compiler may ignore this directive. See
`OpenCL v2.0 <https://www.khronos.org/registry/cl/specs/opencl-2.0.pdf>`_
s6.11.5 for details.
AMD GPU Register Attributes
===========================
Clang supports attributes for controlling register usage on AMD GPU
targets. These attributes may be attached to a kernel function
definition and is an optimization hint to the backend for the maximum
number of registers to use. This is useful in cases where register
limited occupancy is known to be an important factor for the
performance for the kernel.
The semantics are as follows:
- The backend will attempt to limit the number of used registers to
the specified value, but the exact number used is not
guaranteed. The number used may be rounded up to satisfy the
allocation requirements or ABI constraints of the subtarget. For
example, on Southern Islands VGPRs may only be allocated in
increments of 4, so requesting a limit of 39 VGPRs will really
attempt to use up to 40. Requesting more registers than the
subtarget supports will truncate to the maximum allowed. The backend
may also use fewer registers than requested whenever possible.
- 0 implies the default no limit on register usage.
- Ignored on older VLIW subtargets which did not have separate scalar
and vector registers, R600 through Northern Islands.
amdgpu_num_sgpr
---------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Clang supports the
``__attribute__((amdgpu_num_sgpr(<num_registers>)))`` attribute on AMD
Southern Islands GPUs and later for controlling the number of scalar
registers. A typical value would be between 8 and 104 in increments of
8.
Due to common instruction constraints, an additional 2-4 SGPRs are
typically required for internal use depending on features used. This
value is a hint for the total number of SGPRs to use, and not the
number of user SGPRs, so no special consideration needs to be given
for these.
amdgpu_num_vgpr
---------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Clang supports the
``__attribute__((amdgpu_num_vgpr(<num_registers>)))`` attribute on AMD
Southern Islands GPUs and later for controlling the number of vector
registers. A typical value would be between 4 and 256 in increments
of 4.
Calling Conventions
===================
Clang supports several different calling conventions, depending on the target
platform and architecture. The calling convention used for a function determines
how parameters are passed, how results are returned to the caller, and other
low-level details of calling a function.
fastcall (gnu::fastcall, __fastcall, _fastcall)
-----------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","X", ""
On 32-bit x86 targets, this attribute changes the calling convention of a
function to use ECX and EDX as register parameters and clear parameters off of
the stack on return. This convention does not support variadic calls or
unprototyped functions in C, and has no effect on x86_64 targets. This calling
convention is supported primarily for compatibility with existing code. Users
seeking register parameters should use the ``regparm`` attribute, which does
not require callee-cleanup. See the documentation for `__fastcall`_ on MSDN.
.. _`__fastcall`: http://msdn.microsoft.com/en-us/library/6xa169sk.aspx
ms_abi (gnu::ms_abi)
--------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
On non-Windows x86_64 targets, this attribute changes the calling convention of
a function to match the default convention used on Windows x86_64. This
attribute has no effect on Windows targets or non-x86_64 targets.
pcs (gnu::pcs)
--------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
On ARM targets, this attribute can be used to select calling conventions
similar to ``stdcall`` on x86. Valid parameter values are "aapcs" and
"aapcs-vfp".
preserve_all
------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
On X86-64 and AArch64 targets, this attribute changes the calling convention of
a function. The ``preserve_all`` calling convention attempts to make the code
in the caller even less intrusive than the ``preserve_most`` calling convention.
This calling convention also behaves identical to the ``C`` calling convention
on how arguments and return values are passed, but it uses a different set of
caller/callee-saved registers. This removes the burden of saving and
recovering a large register set before and after the call in the caller. If
the arguments are passed in callee-saved registers, then they will be
preserved by the callee across the call. This doesn't apply for values
returned in callee-saved registers.
- On X86-64 the callee preserves all general purpose registers, except for
R11. R11 can be used as a scratch register. Furthermore it also preserves
all floating-point registers (XMMs/YMMs).
The idea behind this convention is to support calls to runtime functions
that don't need to call out to any other functions.
This calling convention, like the ``preserve_most`` calling convention, will be
used by a future version of the Objective-C runtime and should be considered
experimental at this time.
preserve_most
-------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
On X86-64 and AArch64 targets, this attribute changes the calling convention of
a function. The ``preserve_most`` calling convention attempts to make the code
in the caller as unintrusive as possible. This convention behaves identically
to the ``C`` calling convention on how arguments and return values are passed,
but it uses a different set of caller/callee-saved registers. This alleviates
the burden of saving and recovering a large register set before and after the
call in the caller. If the arguments are passed in callee-saved registers,
then they will be preserved by the callee across the call. This doesn't
apply for values returned in callee-saved registers.
- On X86-64 the callee preserves all general purpose registers, except for
R11. R11 can be used as a scratch register. Floating-point registers
(XMMs/YMMs) are not preserved and need to be saved by the caller.
The idea behind this convention is to support calls to runtime functions
that have a hot path and a cold path. The hot path is usually a small piece
of code that doesn't use many registers. The cold path might need to call out to
another function and therefore only needs to preserve the caller-saved
registers, which haven't already been saved by the caller. The
`preserve_most` calling convention is very similar to the ``cold`` calling
convention in terms of caller/callee-saved registers, but they are used for
different types of function calls. ``coldcc`` is for function calls that are
rarely executed, whereas `preserve_most` function calls are intended to be
on the hot path and definitely executed a lot. Furthermore ``preserve_most``
doesn't prevent the inliner from inlining the function call.
This calling convention will be used by a future version of the Objective-C
runtime and should therefore still be considered experimental at this time.
Although this convention was created to optimize certain runtime calls to
the Objective-C runtime, it is not limited to this runtime and might be used
by other runtimes in the future too. The current implementation only
supports X86-64 and AArch64, but the intention is to support more architectures
in the future.
regparm (gnu::regparm)
----------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
On 32-bit x86 targets, the regparm attribute causes the compiler to pass
the first three integer parameters in EAX, EDX, and ECX instead of on the
stack. This attribute has no effect on variadic functions, and all parameters
are passed via the stack as normal.
stdcall (gnu::stdcall, __stdcall, _stdcall)
-------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","X", ""
On 32-bit x86 targets, this attribute changes the calling convention of a
function to clear parameters off of the stack on return. This convention does
not support variadic calls or unprototyped functions in C, and has no effect on
x86_64 targets. This calling convention is used widely by the Windows API and
COM applications. See the documentation for `__stdcall`_ on MSDN.
.. _`__stdcall`: http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx
thiscall (gnu::thiscall, __thiscall, _thiscall)
-----------------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","X", ""
On 32-bit x86 targets, this attribute changes the calling convention of a
function to use ECX for the first parameter (typically the implicit ``this``
parameter of C++ methods) and clear parameters off of the stack on return. This
convention does not support variadic calls or unprototyped functions in C, and
has no effect on x86_64 targets. See the documentation for `__thiscall`_ on
MSDN.
.. _`__thiscall`: http://msdn.microsoft.com/en-us/library/ek8tkfbw.aspx
vectorcall (__vectorcall, _vectorcall)
--------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","X", ""
On 32-bit x86 *and* x86_64 targets, this attribute changes the calling
convention of a function to pass vector parameters in SSE registers.
On 32-bit x86 targets, this calling convention is similar to ``__fastcall``.
The first two integer parameters are passed in ECX and EDX. Subsequent integer
parameters are passed in memory, and callee clears the stack. On x86_64
targets, the callee does *not* clear the stack, and integer parameters are
passed in RCX, RDX, R8, and R9 as is done for the default Windows x64 calling
convention.
On both 32-bit x86 and x86_64 targets, vector and floating point arguments are
passed in XMM0-XMM5. Homogenous vector aggregates of up to four elements are
passed in sequential SSE registers if enough are available. If AVX is enabled,
256 bit vectors are passed in YMM0-YMM5. Any vector or aggregate type that
cannot be passed in registers for any reason is passed by reference, which
allows the caller to align the parameter memory.
See the documentation for `__vectorcall`_ on MSDN for more details.
.. _`__vectorcall`: http://msdn.microsoft.com/en-us/library/dn375768.aspx
Consumed Annotation Checking
============================
Clang supports additional attributes for checking basic resource management
properties, specifically for unique objects that have a single owning reference.
The following attributes are currently supported, although **the implementation
for these annotations is currently in development and are subject to change.**
callable_when
-------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Use ``__attribute__((callable_when(...)))`` to indicate what states a method
may be called in. Valid states are unconsumed, consumed, or unknown. Each
argument to this attribute must be a quoted string. E.g.:
``__attribute__((callable_when("unconsumed", "unknown")))``
consumable
----------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Each ``class`` that uses any of the typestate annotations must first be marked
using the ``consumable`` attribute. Failure to do so will result in a warning.
This attribute accepts a single parameter that must be one of the following:
``unknown``, ``consumed``, or ``unconsumed``.
param_typestate
---------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
This attribute specifies expectations about function parameters. Calls to an
function with annotated parameters will issue a warning if the corresponding
argument isn't in the expected state. The attribute is also used to set the
initial state of the parameter when analyzing the function's body.
return_typestate
----------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
The ``return_typestate`` attribute can be applied to functions or parameters.
When applied to a function the attribute specifies the state of the returned
value. The function's body is checked to ensure that it always returns a value
in the specified state. On the caller side, values returned by the annotated
function are initialized to the given state.
When applied to a function parameter it modifies the state of an argument after
a call to the function returns. The function's body is checked to ensure that
the parameter is in the expected state before returning.
set_typestate
-------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Annotate methods that transition an object into a new state with
``__attribute__((set_typestate(new_state)))``. The new state must be
unconsumed, consumed, or unknown.
test_typestate
--------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Use ``__attribute__((test_typestate(tested_state)))`` to indicate that a method
returns true if the object is in the specified state..
Type Safety Checking
====================
Clang supports additional attributes to enable checking type safety properties
that can't be enforced by the C type system. Use cases include:
* MPI library implementations, where these attributes enable checking that
the buffer type matches the passed ``MPI_Datatype``;
* for HDF5 library there is a similar use case to MPI;
* checking types of variadic functions' arguments for functions like
``fcntl()`` and ``ioctl()``.
You can detect support for these attributes with ``__has_attribute()``. For
example:
.. code-block:: c++
#if defined(__has_attribute)
# if __has_attribute(argument_with_type_tag) && \
__has_attribute(pointer_with_type_tag) && \
__has_attribute(type_tag_for_datatype)
# define ATTR_MPI_PWT(buffer_idx, type_idx) __attribute__((pointer_with_type_tag(mpi,buffer_idx,type_idx)))
/* ... other macros ... */
# endif
#endif
#if !defined(ATTR_MPI_PWT)
# define ATTR_MPI_PWT(buffer_idx, type_idx)
#endif
int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */)
ATTR_MPI_PWT(1,3);
argument_with_type_tag
----------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Use ``__attribute__((argument_with_type_tag(arg_kind, arg_idx,
type_tag_idx)))`` on a function declaration to specify that the function
accepts a type tag that determines the type of some other argument.
``arg_kind`` is an identifier that should be used when annotating all
applicable type tags.
This attribute is primarily useful for checking arguments of variadic functions
(``pointer_with_type_tag`` can be used in most non-variadic cases).
For example:
.. code-block:: c++
int fcntl(int fd, int cmd, ...)
__attribute__(( argument_with_type_tag(fcntl,3,2) ));
pointer_with_type_tag
---------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Use ``__attribute__((pointer_with_type_tag(ptr_kind, ptr_idx, type_tag_idx)))``
on a function declaration to specify that the function accepts a type tag that
determines the pointee type of some other pointer argument.
For example:
.. code-block:: c++
int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other args omitted */)
__attribute__(( pointer_with_type_tag(mpi,1,3) ));
type_tag_for_datatype
---------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","","","", ""
Clang supports annotating type tags of two forms.
* **Type tag that is an expression containing a reference to some declared
identifier.** Use ``__attribute__((type_tag_for_datatype(kind, type)))`` on a
declaration with that identifier:
.. code-block:: c++
extern struct mpi_datatype mpi_datatype_int
__attribute__(( type_tag_for_datatype(mpi,int) ));
#define MPI_INT ((MPI_Datatype) &mpi_datatype_int)
* **Type tag that is an integral literal.** Introduce a ``static const``
variable with a corresponding initializer value and attach
``__attribute__((type_tag_for_datatype(kind, type)))`` on that declaration,
for example:
.. code-block:: c++
#define MPI_INT ((MPI_Datatype) 42)
static const MPI_Datatype mpi_datatype_int
__attribute__(( type_tag_for_datatype(mpi,int) )) = 42
The attribute also accepts an optional third argument that determines how the
expression is compared to the type tag. There are two supported flags:
* ``layout_compatible`` will cause types to be compared according to
layout-compatibility rules (C++11 [class.mem] p 17, 18). This is
implemented to support annotating types like ``MPI_DOUBLE_INT``.
For example:
.. code-block:: c++
/* In mpi.h */
struct internal_mpi_double_int { double d; int i; };
extern struct mpi_datatype mpi_datatype_double_int
__attribute__(( type_tag_for_datatype(mpi, struct internal_mpi_double_int, layout_compatible) ));
#define MPI_DOUBLE_INT ((MPI_Datatype) &mpi_datatype_double_int)
/* In user code */
struct my_pair { double a; int b; };
struct my_pair *buffer;
MPI_Send(buffer, 1, MPI_DOUBLE_INT /*, ... */); // no warning
struct my_int_pair { int a; int b; }
struct my_int_pair *buffer2;
MPI_Send(buffer2, 1, MPI_DOUBLE_INT /*, ... */); // warning: actual buffer element
// type 'struct my_int_pair'
// doesn't match specified MPI_Datatype
* ``must_be_null`` specifies that the expression should be a null pointer
constant, for example:
.. code-block:: c++
/* In mpi.h */
extern struct mpi_datatype mpi_datatype_null
__attribute__(( type_tag_for_datatype(mpi, void, must_be_null) ));
#define MPI_DATATYPE_NULL ((MPI_Datatype) &mpi_datatype_null)
/* In user code */
MPI_Send(buffer, 1, MPI_DATATYPE_NULL /*, ... */); // warning: MPI_DATATYPE_NULL
// was specified but buffer
// is not a null pointer
OpenCL Address Spaces
=====================
The address space qualifier may be used to specify the region of memory that is
used to allocate the object. OpenCL supports the following address spaces:
__generic(generic), __global(global), __local(local), __private(private),
__constant(constant).
.. code-block:: c
__constant int c = ...;
__generic int* foo(global int* g) {
__local int* l;
private int p;
...
return l;
}
More details can be found in the OpenCL C language Spec v2.0, Section 6.5.
constant (__constant)
---------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
The constant address space attribute signals that an object is located in
a constant (non-modifiable) memory region. It is available to all work items.
Any type can be annotated with the constant address space attribute. Objects
with the constant address space qualifier can be declared in any scope and must
have an initializer.
generic (__generic)
-------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
The generic address space attribute is only available with OpenCL v2.0 and later.
It can be used with pointer types. Variables in global and local scope and
function parameters in non-kernel functions can have the generic address space
type attribute. It is intended to be a placeholder for any other address space
except for '__constant' in OpenCL code which can be used with multiple address
spaces.
global (__global)
-----------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
The global address space attribute specifies that an object is allocated in
global memory, which is accessible by all work items. The content stored in this
memory area persists between kernel executions. Pointer types to the global
address space are allowed as function parameters or local variables. Starting
with OpenCL v2.0, the global address space can be used with global (program
scope) variables and static local variable as well.
local (__local)
---------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
The local address space specifies that an object is allocated in the local (work
group) memory area, which is accessible to all work items in the same work
group. The content stored in this memory region is not accessible after
the kernel execution ends. In a kernel function scope, any variable can be in
the local address space. In other scopes, only pointer types to the local address
space are allowed. Local address space variables cannot have an initializer.
private (__private)
-------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
The private address space specifies that an object is allocated in the private
(work item) memory. Other work items cannot access the same memory area and its
content is destroyed after work item execution ends. Local variables can be
declared in the private address space. Function arguments are always in the
private address space. Kernel function arguments of a pointer or an array type
cannot point to the private address space.
Nullability Attributes
======================
Whether a particular pointer may be "null" is an important concern when working with pointers in the C family of languages. The various nullability attributes indicate whether a particular pointer can be null or not, which makes APIs more expressive and can help static analysis tools identify bugs involving null pointers. Clang supports several kinds of nullability attributes: the ``nonnull`` and ``returns_nonnull`` attributes indicate which function or method parameters and result types can never be null, while nullability type qualifiers indicate which pointer types can be null (``_Nullable``) or cannot be null (``_Nonnull``).
The nullability (type) qualifiers express whether a value of a given pointer type can be null (the ``_Nullable`` qualifier), doesn't have a defined meaning for null (the ``_Nonnull`` qualifier), or for which the purpose of null is unclear (the ``_Null_unspecified`` qualifier). Because nullability qualifiers are expressed within the type system, they are more general than the ``nonnull`` and ``returns_nonnull`` attributes, allowing one to express (for example) a nullable pointer to an array of nonnull pointers. Nullability qualifiers are written to the right of the pointer to which they apply. For example:
.. code-block:: c
// No meaningful result when 'ptr' is null (here, it happens to be undefined behavior).
int fetch(int * _Nonnull ptr) { return *ptr; }
// 'ptr' may be null.
int fetch_or_zero(int * _Nullable ptr) {
return ptr ? *ptr : 0;
}
// A nullable pointer to non-null pointers to const characters.
const char *join_strings(const char * _Nonnull * _Nullable strings, unsigned n);
In Objective-C, there is an alternate spelling for the nullability qualifiers that can be used in Objective-C methods and properties using context-sensitive, non-underscored keywords. For example:
.. code-block:: objective-c
@interface NSView : NSResponder
- (nullable NSView *)ancestorSharedWithView:(nonnull NSView *)aView;
@property (assign, nullable) NSView *superview;
@property (readonly, nonnull) NSArray *subviews;
@end
nonnull (gnu::nonnull)
----------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``nonnull`` attribute indicates that some function parameters must not be null, and can be used in several different ways. It's original usage (`from GCC <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes>`_) is as a function (or Objective-C method) attribute that specifies which parameters of the function are nonnull in a comma-separated list. For example:
.. code-block:: c
extern void * my_memcpy (void *dest, const void *src, size_t len)
__attribute__((nonnull (1, 2)));
Here, the ``nonnull`` attribute indicates that parameters 1 and 2
cannot have a null value. Omitting the parenthesized list of parameter indices means that all parameters of pointer type cannot be null:
.. code-block:: c
extern void * my_memcpy (void *dest, const void *src, size_t len)
__attribute__((nonnull));
Clang also allows the ``nonnull`` attribute to be placed directly on a function (or Objective-C method) parameter, eliminating the need to specify the parameter index ahead of type. For example:
.. code-block:: c
extern void * my_memcpy (void *dest __attribute__((nonnull)),
const void *src __attribute__((nonnull)), size_t len);
Note that the ``nonnull`` attribute indicates that passing null to a non-null parameter is undefined behavior, which the optimizer may take advantage of to, e.g., remove null checks. The ``_Nonnull`` type qualifier indicates that a pointer cannot be null in a more general manner (because it is part of the type system) and does not imply undefined behavior, making it more widely applicable.
returns_nonnull (gnu::returns_nonnull)
--------------------------------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"X","X","","", ""
The ``returns_nonnull`` attribute indicates that a particular function (or Objective-C method) always returns a non-null pointer. For example, a particular system ``malloc`` might be defined to terminate a process when memory is not available rather than returning a null pointer:
.. code-block:: c
extern void * malloc (size_t size) __attribute__((returns_nonnull));
The ``returns_nonnull`` attribute implies that returning a null pointer is undefined behavior, which the optimizer may take advantage of. The ``_Nonnull`` type qualifier indicates that a pointer cannot be null in a more general manner (because it is part of the type system) and does not imply undefined behavior, making it more widely applicable
_Nonnull
--------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
The ``_Nonnull`` nullability qualifier indicates that null is not a meaningful value for a value of the ``_Nonnull`` pointer type. For example, given a declaration such as:
.. code-block:: c
int fetch(int * _Nonnull ptr);
a caller of ``fetch`` should not provide a null value, and the compiler will produce a warning if it sees a literal null value passed to ``fetch``. Note that, unlike the declaration attribute ``nonnull``, the presence of ``_Nonnull`` does not imply that passing null is undefined behavior: ``fetch`` is free to consider null undefined behavior or (perhaps for backward-compatibility reasons) defensively handle null.
_Null_unspecified
-----------------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
The ``_Null_unspecified`` nullability qualifier indicates that neither the ``_Nonnull`` nor ``_Nullable`` qualifiers make sense for a particular pointer type. It is used primarily to indicate that the role of null with specific pointers in a nullability-annotated header is unclear, e.g., due to overly-complex implementations or historical factors with a long-lived API.
_Nullable
---------
.. csv-table:: Supported Syntaxes
:header: "GNU", "C++11", "__declspec", "Keyword", "Pragma"
"","","","X", ""
The ``_Nullable`` nullability qualifier indicates that a value of the ``_Nullable`` pointer type can be null. For example, given:
.. code-block:: c
int fetch_or_zero(int * _Nullable ptr);
a caller of ``fetch_or_zero`` can provide null.
|