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 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
|
//===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- C++ -*--=//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// These tablegen backends emit Clang attribute processing code
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/StringMatcher.h"
#include "llvm/TableGen/TableGenBackend.h"
#include <algorithm>
#include <cctype>
#include <memory>
#include <set>
#include <sstream>
using namespace llvm;
class FlattenedSpelling {
std::string V, N, NS;
bool K;
public:
FlattenedSpelling(const std::string &Variety, const std::string &Name,
const std::string &Namespace, bool KnownToGCC) :
V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {}
explicit FlattenedSpelling(const Record &Spelling) :
V(Spelling.getValueAsString("Variety")),
N(Spelling.getValueAsString("Name")) {
assert(V != "GCC" && "Given a GCC spelling, which means this hasn't been"
"flattened!");
if (V == "CXX11" || V == "Pragma")
NS = Spelling.getValueAsString("Namespace");
bool Unset;
K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset);
}
const std::string &variety() const { return V; }
const std::string &name() const { return N; }
const std::string &nameSpace() const { return NS; }
bool knownToGCC() const { return K; }
};
std::vector<FlattenedSpelling> GetFlattenedSpellings(const Record &Attr) {
std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
std::vector<FlattenedSpelling> Ret;
for (const auto &Spelling : Spellings) {
if (Spelling->getValueAsString("Variety") == "GCC") {
// Gin up two new spelling objects to add into the list.
Ret.push_back(FlattenedSpelling("GNU", Spelling->getValueAsString("Name"),
"", true));
Ret.push_back(FlattenedSpelling(
"CXX11", Spelling->getValueAsString("Name"), "gnu", true));
} else
Ret.push_back(FlattenedSpelling(*Spelling));
}
return Ret;
}
static std::string ReadPCHRecord(StringRef type) {
return StringSwitch<std::string>(type)
.EndsWith("Decl *", "GetLocalDeclAs<"
+ std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
.Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)")
.Case("Expr *", "ReadExpr(F)")
.Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
.Default("Record[Idx++]");
}
// Assumes that the way to get the value is SA->getname()
static std::string WritePCHRecord(StringRef type, StringRef name) {
return StringSwitch<std::string>(type)
.EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
", Record);\n")
.Case("TypeSourceInfo *",
"AddTypeSourceInfo(" + std::string(name) + ", Record);\n")
.Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
.Case("IdentifierInfo *",
"AddIdentifierRef(" + std::string(name) + ", Record);\n")
.Default("Record.push_back(" + std::string(name) + ");\n");
}
// Normalize attribute name by removing leading and trailing
// underscores. For example, __foo, foo__, __foo__ would
// become foo.
static StringRef NormalizeAttrName(StringRef AttrName) {
if (AttrName.startswith("__"))
AttrName = AttrName.substr(2, AttrName.size());
if (AttrName.endswith("__"))
AttrName = AttrName.substr(0, AttrName.size() - 2);
return AttrName;
}
// Normalize the name by removing any and all leading and trailing underscores.
// This is different from NormalizeAttrName in that it also handles names like
// _pascal and __pascal.
static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
while (Name.startswith("_"))
Name = Name.substr(1, Name.size());
while (Name.endswith("_"))
Name = Name.substr(0, Name.size() - 1);
return Name;
}
// Normalize attribute spelling only if the spelling has both leading
// and trailing underscores. For example, __ms_struct__ will be
// normalized to "ms_struct"; __cdecl will remain intact.
static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
}
return AttrSpelling;
}
typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
ParsedAttrMap *Dupes = nullptr) {
std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
std::set<std::string> Seen;
ParsedAttrMap R;
for (const auto *Attr : Attrs) {
if (Attr->getValueAsBit("SemaHandler")) {
std::string AN;
if (Attr->isSubClassOf("TargetSpecificAttr") &&
!Attr->isValueUnset("ParseKind")) {
AN = Attr->getValueAsString("ParseKind");
// If this attribute has already been handled, it does not need to be
// handled again.
if (Seen.find(AN) != Seen.end()) {
if (Dupes)
Dupes->push_back(std::make_pair(AN, Attr));
continue;
}
Seen.insert(AN);
} else
AN = NormalizeAttrName(Attr->getName()).str();
R.push_back(std::make_pair(AN, Attr));
}
}
return R;
}
namespace {
class Argument {
std::string lowerName, upperName;
StringRef attrName;
bool isOpt;
public:
Argument(const Record &Arg, StringRef Attr)
: lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
attrName(Attr), isOpt(false) {
if (!lowerName.empty()) {
lowerName[0] = std::tolower(lowerName[0]);
upperName[0] = std::toupper(upperName[0]);
}
}
virtual ~Argument() {}
StringRef getLowerName() const { return lowerName; }
StringRef getUpperName() const { return upperName; }
StringRef getAttrName() const { return attrName; }
bool isOptional() const { return isOpt; }
void setOptional(bool set) { isOpt = set; }
// These functions print the argument contents formatted in different ways.
virtual void writeAccessors(raw_ostream &OS) const = 0;
virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
virtual void writeCloneArgs(raw_ostream &OS) const = 0;
virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
virtual void writeCtorBody(raw_ostream &OS) const {}
virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
virtual void writeCtorParameters(raw_ostream &OS) const = 0;
virtual void writeDeclarations(raw_ostream &OS) const = 0;
virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
virtual void writePCHWrite(raw_ostream &OS) const = 0;
virtual void writeValue(raw_ostream &OS) const = 0;
virtual void writeDump(raw_ostream &OS) const = 0;
virtual void writeDumpChildren(raw_ostream &OS) const {}
virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
virtual bool isEnumArg() const { return false; }
virtual bool isVariadicEnumArg() const { return false; }
virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
OS << getUpperName();
}
};
class SimpleArgument : public Argument {
std::string type;
public:
SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
: Argument(Arg, Attr), type(T)
{}
std::string getType() const { return type; }
void writeAccessors(raw_ostream &OS) const override {
OS << " " << type << " get" << getUpperName() << "() const {\n";
OS << " return " << getLowerName() << ";\n";
OS << " }";
}
void writeCloneArgs(raw_ostream &OS) const override {
OS << getLowerName();
}
void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
OS << "A->get" << getUpperName() << "()";
}
void writeCtorInitializers(raw_ostream &OS) const override {
OS << getLowerName() << "(" << getUpperName() << ")";
}
void writeCtorDefaultInitializers(raw_ostream &OS) const override {
OS << getLowerName() << "()";
}
void writeCtorParameters(raw_ostream &OS) const override {
OS << type << " " << getUpperName();
}
void writeDeclarations(raw_ostream &OS) const override {
OS << type << " " << getLowerName() << ";";
}
void writePCHReadDecls(raw_ostream &OS) const override {
std::string read = ReadPCHRecord(type);
OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
}
void writePCHReadArgs(raw_ostream &OS) const override {
OS << getLowerName();
}
void writePCHWrite(raw_ostream &OS) const override {
OS << " " << WritePCHRecord(type, "SA->get" +
std::string(getUpperName()) + "()");
}
void writeValue(raw_ostream &OS) const override {
if (type == "FunctionDecl *") {
OS << "\" << get" << getUpperName()
<< "()->getNameInfo().getAsString() << \"";
} else if (type == "IdentifierInfo *") {
OS << "\" << get" << getUpperName() << "()->getName() << \"";
} else if (type == "TypeSourceInfo *") {
OS << "\" << get" << getUpperName() << "().getAsString() << \"";
} else {
OS << "\" << get" << getUpperName() << "() << \"";
}
}
void writeDump(raw_ostream &OS) const override {
if (type == "FunctionDecl *") {
OS << " OS << \" \";\n";
OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
} else if (type == "IdentifierInfo *") {
OS << " OS << \" \" << SA->get" << getUpperName()
<< "()->getName();\n";
} else if (type == "TypeSourceInfo *") {
OS << " OS << \" \" << SA->get" << getUpperName()
<< "().getAsString();\n";
} else if (type == "bool") {
OS << " if (SA->get" << getUpperName() << "()) OS << \" "
<< getUpperName() << "\";\n";
} else if (type == "int" || type == "unsigned") {
OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
} else {
llvm_unreachable("Unknown SimpleArgument type!");
}
}
};
class DefaultSimpleArgument : public SimpleArgument {
int64_t Default;
public:
DefaultSimpleArgument(const Record &Arg, StringRef Attr,
std::string T, int64_t Default)
: SimpleArgument(Arg, Attr, T), Default(Default) {}
void writeAccessors(raw_ostream &OS) const override {
SimpleArgument::writeAccessors(OS);
OS << "\n\n static const " << getType() << " Default" << getUpperName()
<< " = " << Default << ";";
}
};
class StringArgument : public Argument {
public:
StringArgument(const Record &Arg, StringRef Attr)
: Argument(Arg, Attr)
{}
void writeAccessors(raw_ostream &OS) const override {
OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
OS << " return llvm::StringRef(" << getLowerName() << ", "
<< getLowerName() << "Length);\n";
OS << " }\n";
OS << " unsigned get" << getUpperName() << "Length() const {\n";
OS << " return " << getLowerName() << "Length;\n";
OS << " }\n";
OS << " void set" << getUpperName()
<< "(ASTContext &C, llvm::StringRef S) {\n";
OS << " " << getLowerName() << "Length = S.size();\n";
OS << " this->" << getLowerName() << " = new (C, 1) char ["
<< getLowerName() << "Length];\n";
OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
<< getLowerName() << "Length);\n";
OS << " }";
}
void writeCloneArgs(raw_ostream &OS) const override {
OS << "get" << getUpperName() << "()";
}
void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
OS << "A->get" << getUpperName() << "()";
}
void writeCtorBody(raw_ostream &OS) const override {
OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
<< ".data(), " << getLowerName() << "Length);";
}
void writeCtorInitializers(raw_ostream &OS) const override {
OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
<< getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
<< "Length])";
}
void writeCtorDefaultInitializers(raw_ostream &OS) const override {
OS << getLowerName() << "Length(0)," << getLowerName() << "(0)";
}
void writeCtorParameters(raw_ostream &OS) const override {
OS << "llvm::StringRef " << getUpperName();
}
void writeDeclarations(raw_ostream &OS) const override {
OS << "unsigned " << getLowerName() << "Length;\n";
OS << "char *" << getLowerName() << ";";
}
void writePCHReadDecls(raw_ostream &OS) const override {
OS << " std::string " << getLowerName()
<< "= ReadString(Record, Idx);\n";
}
void writePCHReadArgs(raw_ostream &OS) const override {
OS << getLowerName();
}
void writePCHWrite(raw_ostream &OS) const override {
OS << " AddString(SA->get" << getUpperName() << "(), Record);\n";
}
void writeValue(raw_ostream &OS) const override {
OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
}
void writeDump(raw_ostream &OS) const override {
OS << " OS << \" \\\"\" << SA->get" << getUpperName()
<< "() << \"\\\"\";\n";
}
};
class AlignedArgument : public Argument {
public:
AlignedArgument(const Record &Arg, StringRef Attr)
: Argument(Arg, Attr)
{}
void writeAccessors(raw_ostream &OS) const override {
OS << " bool is" << getUpperName() << "Dependent() const;\n";
OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
OS << " bool is" << getUpperName() << "Expr() const {\n";
OS << " return is" << getLowerName() << "Expr;\n";
OS << " }\n";
OS << " Expr *get" << getUpperName() << "Expr() const {\n";
OS << " assert(is" << getLowerName() << "Expr);\n";
OS << " return " << getLowerName() << "Expr;\n";
OS << " }\n";
OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
OS << " assert(!is" << getLowerName() << "Expr);\n";
OS << " return " << getLowerName() << "Type;\n";
OS << " }";
}
void writeAccessorDefinitions(raw_ostream &OS) const override {
OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
<< "Dependent() const {\n";
OS << " if (is" << getLowerName() << "Expr)\n";
OS << " return " << getLowerName() << "Expr && (" << getLowerName()
<< "Expr->isValueDependent() || " << getLowerName()
<< "Expr->isTypeDependent());\n";
OS << " else\n";
OS << " return " << getLowerName()
<< "Type->getType()->isDependentType();\n";
OS << "}\n";
// FIXME: Do not do the calculation here
// FIXME: Handle types correctly
// A null pointer means maximum alignment
// FIXME: Load the platform-specific maximum alignment, rather than
// 16, the x86 max.
OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
<< "(ASTContext &Ctx) const {\n";
OS << " assert(!is" << getUpperName() << "Dependent());\n";
OS << " if (is" << getLowerName() << "Expr)\n";
OS << " return (" << getLowerName() << "Expr ? " << getLowerName()
<< "Expr->EvaluateKnownConstInt(Ctx).getZExtValue() : 16)"
<< "* Ctx.getCharWidth();\n";
OS << " else\n";
OS << " return 0; // FIXME\n";
OS << "}\n";
}
void writeCloneArgs(raw_ostream &OS) const override {
OS << "is" << getLowerName() << "Expr, is" << getLowerName()
<< "Expr ? static_cast<void*>(" << getLowerName()
<< "Expr) : " << getLowerName()
<< "Type";
}
void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
// FIXME: move the definition in Sema::InstantiateAttrs to here.
// In the meantime, aligned attributes are cloned.
}
void writeCtorBody(raw_ostream &OS) const override {
OS << " if (is" << getLowerName() << "Expr)\n";
OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
<< getUpperName() << ");\n";
OS << " else\n";
OS << " " << getLowerName()
<< "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
<< ");";
}
void writeCtorInitializers(raw_ostream &OS) const override {
OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
}
void writeCtorDefaultInitializers(raw_ostream &OS) const override {
OS << "is" << getLowerName() << "Expr(false)";
}
void writeCtorParameters(raw_ostream &OS) const override {
OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
}
void writeImplicitCtorArgs(raw_ostream &OS) const override {
OS << "Is" << getUpperName() << "Expr, " << getUpperName();
}
void writeDeclarations(raw_ostream &OS) const override {
OS << "bool is" << getLowerName() << "Expr;\n";
OS << "union {\n";
OS << "Expr *" << getLowerName() << "Expr;\n";
OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
OS << "};";
}
void writePCHReadArgs(raw_ostream &OS) const override {
OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
}
void writePCHReadDecls(raw_ostream &OS) const override {
OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
OS << " void *" << getLowerName() << "Ptr;\n";
OS << " if (is" << getLowerName() << "Expr)\n";
OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
OS << " else\n";
OS << " " << getLowerName()
<< "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
}
void writePCHWrite(raw_ostream &OS) const override {
OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
OS << " if (SA->is" << getUpperName() << "Expr())\n";
OS << " AddStmt(SA->get" << getUpperName() << "Expr());\n";
OS << " else\n";
OS << " AddTypeSourceInfo(SA->get" << getUpperName()
<< "Type(), Record);\n";
}
void writeValue(raw_ostream &OS) const override {
OS << "\";\n";
OS << " assert(is" << getLowerName() << "Expr && " << getLowerName()
<< "Expr != nullptr);\n";
OS << " " << getLowerName() << "Expr->printPretty(OS, 0, Policy);\n";
OS << " OS << \"";
}
void writeDump(raw_ostream &OS) const override {
}
void writeDumpChildren(raw_ostream &OS) const override {
OS << " if (SA->is" << getUpperName() << "Expr()) {\n";
OS << " lastChild();\n";
OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
OS << " } else\n";
OS << " dumpType(SA->get" << getUpperName()
<< "Type()->getType());\n";
}
void writeHasChildren(raw_ostream &OS) const override {
OS << "SA->is" << getUpperName() << "Expr()";
}
};
class VariadicArgument : public Argument {
std::string Type, ArgName, ArgSizeName, RangeName;
public:
VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
: Argument(Arg, Attr), Type(T), ArgName(getLowerName().str() + "_"),
ArgSizeName(ArgName + "Size"), RangeName(getLowerName()) {}
std::string getType() const { return Type; }
void writeAccessors(raw_ostream &OS) const override {
std::string IteratorType = getLowerName().str() + "_iterator";
std::string BeginFn = getLowerName().str() + "_begin()";
std::string EndFn = getLowerName().str() + "_end()";
OS << " typedef " << Type << "* " << IteratorType << ";\n";
OS << " " << IteratorType << " " << BeginFn << " const {"
<< " return " << ArgName << "; }\n";
OS << " " << IteratorType << " " << EndFn << " const {"
<< " return " << ArgName << " + " << ArgSizeName << "; }\n";
OS << " unsigned " << getLowerName() << "_size() const {"
<< " return " << ArgSizeName << "; }\n";
OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName
<< "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
<< "); }\n";
}
void writeCloneArgs(raw_ostream &OS) const override {
OS << ArgName << ", " << ArgSizeName;
}
void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
// This isn't elegant, but we have to go through public methods...
OS << "A->" << getLowerName() << "_begin(), "
<< "A->" << getLowerName() << "_size()";
}
void writeCtorBody(raw_ostream &OS) const override {
OS << " std::copy(" << getUpperName() << ", " << getUpperName()
<< " + " << ArgSizeName << ", " << ArgName << ");";
}
void writeCtorInitializers(raw_ostream &OS) const override {
OS << ArgSizeName << "(" << getUpperName() << "Size), "
<< ArgName << "(new (Ctx, 16) " << getType() << "["
<< ArgSizeName << "])";
}
void writeCtorDefaultInitializers(raw_ostream &OS) const override {
OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
}
void writeCtorParameters(raw_ostream &OS) const override {
OS << getType() << " *" << getUpperName() << ", unsigned "
<< getUpperName() << "Size";
}
void writeImplicitCtorArgs(raw_ostream &OS) const override {
OS << getUpperName() << ", " << getUpperName() << "Size";
}
void writeDeclarations(raw_ostream &OS) const override {
OS << " unsigned " << ArgSizeName << ";\n";
OS << " " << getType() << " *" << ArgName << ";";
}
void writePCHReadDecls(raw_ostream &OS) const override {
OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
OS << " SmallVector<" << Type << ", 4> " << getLowerName()
<< ";\n";
OS << " " << getLowerName() << ".reserve(" << getLowerName()
<< "Size);\n";
OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
std::string read = ReadPCHRecord(Type);
OS << " " << getLowerName() << ".push_back(" << read << ");\n";
}
void writePCHReadArgs(raw_ostream &OS) const override {
OS << getLowerName() << ".data(), " << getLowerName() << "Size";
}
void writePCHWrite(raw_ostream &OS) const override {
OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
OS << " for (auto &Val : SA->" << RangeName << "())\n";
OS << " " << WritePCHRecord(Type, "Val");
}
void writeValue(raw_ostream &OS) const override {
OS << "\";\n";
OS << " bool isFirst = true;\n"
<< " for (const auto &Val : " << RangeName << "()) {\n"
<< " if (isFirst) isFirst = false;\n"
<< " else OS << \", \";\n"
<< " OS << Val;\n"
<< " }\n";
OS << " OS << \"";
}
void writeDump(raw_ostream &OS) const override {
OS << " for (const auto &Val : SA->" << RangeName << "())\n";
OS << " OS << \" \" << Val;\n";
}
};
// Unique the enums, but maintain the original declaration ordering.
std::vector<std::string>
uniqueEnumsInOrder(const std::vector<std::string> &enums) {
std::vector<std::string> uniques;
std::set<std::string> unique_set(enums.begin(), enums.end());
for (const auto &i : enums) {
std::set<std::string>::iterator set_i = unique_set.find(i);
if (set_i != unique_set.end()) {
uniques.push_back(i);
unique_set.erase(set_i);
}
}
return uniques;
}
class EnumArgument : public Argument {
std::string type;
std::vector<std::string> values, enums, uniques;
public:
EnumArgument(const Record &Arg, StringRef Attr)
: Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
values(Arg.getValueAsListOfStrings("Values")),
enums(Arg.getValueAsListOfStrings("Enums")),
uniques(uniqueEnumsInOrder(enums))
{
// FIXME: Emit a proper error
assert(!uniques.empty());
}
bool isEnumArg() const override { return true; }
void writeAccessors(raw_ostream &OS) const override {
OS << " " << type << " get" << getUpperName() << "() const {\n";
OS << " return " << getLowerName() << ";\n";
OS << " }";
}
void writeCloneArgs(raw_ostream &OS) const override {
OS << getLowerName();
}
void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
OS << "A->get" << getUpperName() << "()";
}
void writeCtorInitializers(raw_ostream &OS) const override {
OS << getLowerName() << "(" << getUpperName() << ")";
}
void writeCtorDefaultInitializers(raw_ostream &OS) const override {
OS << getLowerName() << "(" << type << "(0))";
}
void writeCtorParameters(raw_ostream &OS) const override {
OS << type << " " << getUpperName();
}
void writeDeclarations(raw_ostream &OS) const override {
std::vector<std::string>::const_iterator i = uniques.begin(),
e = uniques.end();
// The last one needs to not have a comma.
--e;
OS << "public:\n";
OS << " enum " << type << " {\n";
for (; i != e; ++i)
OS << " " << *i << ",\n";
OS << " " << *e << "\n";
OS << " };\n";
OS << "private:\n";
OS << " " << type << " " << getLowerName() << ";";
}
void writePCHReadDecls(raw_ostream &OS) const override {
OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
<< "(static_cast<" << getAttrName() << "Attr::" << type
<< ">(Record[Idx++]));\n";
}
void writePCHReadArgs(raw_ostream &OS) const override {
OS << getLowerName();
}
void writePCHWrite(raw_ostream &OS) const override {
OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
}
void writeValue(raw_ostream &OS) const override {
OS << "\" << get" << getUpperName() << "() << \"";
}
void writeDump(raw_ostream &OS) const override {
OS << " switch(SA->get" << getUpperName() << "()) {\n";
for (const auto &I : uniques) {
OS << " case " << getAttrName() << "Attr::" << I << ":\n";
OS << " OS << \" " << I << "\";\n";
OS << " break;\n";
}
OS << " }\n";
}
void writeConversion(raw_ostream &OS) const {
OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
OS << type << " &Out) {\n";
OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
OS << type << ">>(Val)\n";
for (size_t I = 0; I < enums.size(); ++I) {
OS << " .Case(\"" << values[I] << "\", ";
OS << getAttrName() << "Attr::" << enums[I] << ")\n";
}
OS << " .Default(Optional<" << type << ">());\n";
OS << " if (R) {\n";
OS << " Out = *R;\n return true;\n }\n";
OS << " return false;\n";
OS << " }\n";
}
};
class VariadicEnumArgument: public VariadicArgument {
std::string type, QualifiedTypeName;
std::vector<std::string> values, enums, uniques;
public:
VariadicEnumArgument(const Record &Arg, StringRef Attr)
: VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
type(Arg.getValueAsString("Type")),
values(Arg.getValueAsListOfStrings("Values")),
enums(Arg.getValueAsListOfStrings("Enums")),
uniques(uniqueEnumsInOrder(enums))
{
QualifiedTypeName = getAttrName().str() + "Attr::" + type;
// FIXME: Emit a proper error
assert(!uniques.empty());
}
bool isVariadicEnumArg() const override { return true; }
void writeDeclarations(raw_ostream &OS) const override {
std::vector<std::string>::const_iterator i = uniques.begin(),
e = uniques.end();
// The last one needs to not have a comma.
--e;
OS << "public:\n";
OS << " enum " << type << " {\n";
for (; i != e; ++i)
OS << " " << *i << ",\n";
OS << " " << *e << "\n";
OS << " };\n";
OS << "private:\n";
VariadicArgument::writeDeclarations(OS);
}
void writeDump(raw_ostream &OS) const override {
OS << " for (" << getAttrName() << "Attr::" << getLowerName()
<< "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
<< getLowerName() << "_end(); I != E; ++I) {\n";
OS << " switch(*I) {\n";
for (const auto &UI : uniques) {
OS << " case " << getAttrName() << "Attr::" << UI << ":\n";
OS << " OS << \" " << UI << "\";\n";
OS << " break;\n";
}
OS << " }\n";
OS << " }\n";
}
void writePCHReadDecls(raw_ostream &OS) const override {
OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
<< ";\n";
OS << " " << getLowerName() << ".reserve(" << getLowerName()
<< "Size);\n";
OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
OS << " " << getLowerName() << ".push_back(" << "static_cast<"
<< QualifiedTypeName << ">(Record[Idx++]));\n";
}
void writePCHWrite(raw_ostream &OS) const override {
OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
OS << " for (" << getAttrName() << "Attr::" << getLowerName()
<< "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
<< getLowerName() << "_end(); i != e; ++i)\n";
OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
}
void writeConversion(raw_ostream &OS) const {
OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
OS << type << " &Out) {\n";
OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
OS << type << ">>(Val)\n";
for (size_t I = 0; I < enums.size(); ++I) {
OS << " .Case(\"" << values[I] << "\", ";
OS << getAttrName() << "Attr::" << enums[I] << ")\n";
}
OS << " .Default(Optional<" << type << ">());\n";
OS << " if (R) {\n";
OS << " Out = *R;\n return true;\n }\n";
OS << " return false;\n";
OS << " }\n";
}
};
class VersionArgument : public Argument {
public:
VersionArgument(const Record &Arg, StringRef Attr)
: Argument(Arg, Attr)
{}
void writeAccessors(raw_ostream &OS) const override {
OS << " VersionTuple get" << getUpperName() << "() const {\n";
OS << " return " << getLowerName() << ";\n";
OS << " }\n";
OS << " void set" << getUpperName()
<< "(ASTContext &C, VersionTuple V) {\n";
OS << " " << getLowerName() << " = V;\n";
OS << " }";
}
void writeCloneArgs(raw_ostream &OS) const override {
OS << "get" << getUpperName() << "()";
}
void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
OS << "A->get" << getUpperName() << "()";
}
void writeCtorInitializers(raw_ostream &OS) const override {
OS << getLowerName() << "(" << getUpperName() << ")";
}
void writeCtorDefaultInitializers(raw_ostream &OS) const override {
OS << getLowerName() << "()";
}
void writeCtorParameters(raw_ostream &OS) const override {
OS << "VersionTuple " << getUpperName();
}
void writeDeclarations(raw_ostream &OS) const override {
OS << "VersionTuple " << getLowerName() << ";\n";
}
void writePCHReadDecls(raw_ostream &OS) const override {
OS << " VersionTuple " << getLowerName()
<< "= ReadVersionTuple(Record, Idx);\n";
}
void writePCHReadArgs(raw_ostream &OS) const override {
OS << getLowerName();
}
void writePCHWrite(raw_ostream &OS) const override {
OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
}
void writeValue(raw_ostream &OS) const override {
OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
}
void writeDump(raw_ostream &OS) const override {
OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
}
};
class ExprArgument : public SimpleArgument {
public:
ExprArgument(const Record &Arg, StringRef Attr)
: SimpleArgument(Arg, Attr, "Expr *")
{}
void writeASTVisitorTraversal(raw_ostream &OS) const override {
OS << " if (!"
<< "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
OS << " return false;\n";
}
void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
OS << "tempInst" << getUpperName();
}
void writeTemplateInstantiation(raw_ostream &OS) const override {
OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
OS << " {\n";
OS << " EnterExpressionEvaluationContext "
<< "Unevaluated(S, Sema::Unevaluated);\n";
OS << " ExprResult " << "Result = S.SubstExpr("
<< "A->get" << getUpperName() << "(), TemplateArgs);\n";
OS << " tempInst" << getUpperName() << " = "
<< "Result.getAs<Expr>();\n";
OS << " }\n";
}
void writeDump(raw_ostream &OS) const override {}
void writeDumpChildren(raw_ostream &OS) const override {
OS << " lastChild();\n";
OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
}
void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
};
class VariadicExprArgument : public VariadicArgument {
public:
VariadicExprArgument(const Record &Arg, StringRef Attr)
: VariadicArgument(Arg, Attr, "Expr *")
{}
void writeASTVisitorTraversal(raw_ostream &OS) const override {
OS << " {\n";
OS << " " << getType() << " *I = A->" << getLowerName()
<< "_begin();\n";
OS << " " << getType() << " *E = A->" << getLowerName()
<< "_end();\n";
OS << " for (; I != E; ++I) {\n";
OS << " if (!getDerived().TraverseStmt(*I))\n";
OS << " return false;\n";
OS << " }\n";
OS << " }\n";
}
void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
OS << "tempInst" << getUpperName() << ", "
<< "A->" << getLowerName() << "_size()";
}
void writeTemplateInstantiation(raw_ostream &OS) const override {
OS << " " << getType() << " *tempInst" << getUpperName()
<< " = new (C, 16) " << getType()
<< "[A->" << getLowerName() << "_size()];\n";
OS << " {\n";
OS << " EnterExpressionEvaluationContext "
<< "Unevaluated(S, Sema::Unevaluated);\n";
OS << " " << getType() << " *TI = tempInst" << getUpperName()
<< ";\n";
OS << " " << getType() << " *I = A->" << getLowerName()
<< "_begin();\n";
OS << " " << getType() << " *E = A->" << getLowerName()
<< "_end();\n";
OS << " for (; I != E; ++I, ++TI) {\n";
OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
OS << " *TI = Result.getAs<Expr>();\n";
OS << " }\n";
OS << " }\n";
}
void writeDump(raw_ostream &OS) const override {}
void writeDumpChildren(raw_ostream &OS) const override {
OS << " for (" << getAttrName() << "Attr::" << getLowerName()
<< "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
<< getLowerName() << "_end(); I != E; ++I) {\n";
OS << " if (I + 1 == E)\n";
OS << " lastChild();\n";
OS << " dumpStmt(*I);\n";
OS << " }\n";
}
void writeHasChildren(raw_ostream &OS) const override {
OS << "SA->" << getLowerName() << "_begin() != "
<< "SA->" << getLowerName() << "_end()";
}
};
class TypeArgument : public SimpleArgument {
public:
TypeArgument(const Record &Arg, StringRef Attr)
: SimpleArgument(Arg, Attr, "TypeSourceInfo *")
{}
void writeAccessors(raw_ostream &OS) const override {
OS << " QualType get" << getUpperName() << "() const {\n";
OS << " return " << getLowerName() << "->getType();\n";
OS << " }";
OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
OS << " return " << getLowerName() << ";\n";
OS << " }";
}
void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
OS << "A->get" << getUpperName() << "Loc()";
}
void writePCHWrite(raw_ostream &OS) const override {
OS << " " << WritePCHRecord(
getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
}
};
}
static std::unique_ptr<Argument>
createArgument(const Record &Arg, StringRef Attr,
const Record *Search = nullptr) {
if (!Search)
Search = &Arg;
Argument *Ptr = nullptr;
llvm::StringRef ArgName = Search->getName();
if (ArgName == "AlignedArgument") Ptr = new AlignedArgument(Arg, Attr);
else if (ArgName == "EnumArgument") Ptr = new EnumArgument(Arg, Attr);
else if (ArgName == "ExprArgument") Ptr = new ExprArgument(Arg, Attr);
else if (ArgName == "FunctionArgument")
Ptr = new SimpleArgument(Arg, Attr, "FunctionDecl *");
else if (ArgName == "IdentifierArgument")
Ptr = new SimpleArgument(Arg, Attr, "IdentifierInfo *");
else if (ArgName == "DefaultBoolArgument")
Ptr = new DefaultSimpleArgument(Arg, Attr, "bool",
Arg.getValueAsBit("Default"));
else if (ArgName == "BoolArgument") Ptr = new SimpleArgument(Arg, Attr,
"bool");
else if (ArgName == "DefaultIntArgument")
Ptr = new DefaultSimpleArgument(Arg, Attr, "int",
Arg.getValueAsInt("Default"));
else if (ArgName == "IntArgument") Ptr = new SimpleArgument(Arg, Attr, "int");
else if (ArgName == "StringArgument") Ptr = new StringArgument(Arg, Attr);
else if (ArgName == "TypeArgument") Ptr = new TypeArgument(Arg, Attr);
else if (ArgName == "UnsignedArgument")
Ptr = new SimpleArgument(Arg, Attr, "unsigned");
else if (ArgName == "VariadicUnsignedArgument")
Ptr = new VariadicArgument(Arg, Attr, "unsigned");
else if (ArgName == "VariadicEnumArgument")
Ptr = new VariadicEnumArgument(Arg, Attr);
else if (ArgName == "VariadicExprArgument")
Ptr = new VariadicExprArgument(Arg, Attr);
else if (ArgName == "VersionArgument")
Ptr = new VersionArgument(Arg, Attr);
if (!Ptr) {
// Search in reverse order so that the most-derived type is handled first.
std::vector<Record*> Bases = Search->getSuperClasses();
for (const auto *Base : llvm::make_range(Bases.rbegin(), Bases.rend())) {
Ptr = createArgument(Arg, Attr, Base).release();
if (Ptr)
break;
}
}
if (Ptr && Arg.getValueAsBit("Optional"))
Ptr->setOptional(true);
return std::unique_ptr<Argument>(Ptr);
}
static void writeAvailabilityValue(raw_ostream &OS) {
OS << "\" << getPlatform()->getName();\n"
<< " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
<< " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
<< " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
<< " if (getUnavailable()) OS << \", unavailable\";\n"
<< " OS << \"";
}
static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
if (Spellings.empty()) {
OS << " return \"(No spelling)\";\n}\n\n";
return;
}
OS << " switch (SpellingListIndex) {\n"
" default:\n"
" llvm_unreachable(\"Unknown attribute spelling!\");\n"
" return \"(No spelling)\";\n";
for (unsigned I = 0; I < Spellings.size(); ++I)
OS << " case " << I << ":\n"
" return \"" << Spellings[I].name() << "\";\n";
// End of the switch statement.
OS << " }\n";
// End of the getSpelling function.
OS << "}\n\n";
}
static void
writePrettyPrintFunction(Record &R,
const std::vector<std::unique_ptr<Argument>> &Args,
raw_ostream &OS) {
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
OS << "void " << R.getName() << "Attr::printPretty("
<< "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
if (Spellings.empty()) {
OS << "}\n\n";
return;
}
OS <<
" switch (SpellingListIndex) {\n"
" default:\n"
" llvm_unreachable(\"Unknown attribute spelling!\");\n"
" break;\n";
for (unsigned I = 0; I < Spellings.size(); ++ I) {
llvm::SmallString<16> Prefix;
llvm::SmallString<8> Suffix;
// The actual spelling of the name and namespace (if applicable)
// of an attribute without considering prefix and suffix.
llvm::SmallString<64> Spelling;
std::string Name = Spellings[I].name();
std::string Variety = Spellings[I].variety();
if (Variety == "GNU") {
Prefix = " __attribute__((";
Suffix = "))";
} else if (Variety == "CXX11") {
Prefix = " [[";
Suffix = "]]";
std::string Namespace = Spellings[I].nameSpace();
if (!Namespace.empty()) {
Spelling += Namespace;
Spelling += "::";
}
} else if (Variety == "Declspec") {
Prefix = " __declspec(";
Suffix = ")";
} else if (Variety == "Keyword") {
Prefix = " ";
Suffix = "";
} else if (Variety == "Pragma") {
Prefix = "#pragma ";
Suffix = "\n";
std::string Namespace = Spellings[I].nameSpace();
if (!Namespace.empty()) {
Spelling += Namespace;
Spelling += " ";
}
} else {
llvm_unreachable("Unknown attribute syntax variety!");
}
Spelling += Name;
OS <<
" case " << I << " : {\n"
" OS << \"" + Prefix.str() + Spelling.str();
if (Variety == "Pragma") {
OS << " \";\n";
OS << " printPrettyPragma(OS, Policy);\n";
OS << " break;\n";
OS << " }\n";
continue;
}
if (!Args.empty())
OS << "(";
if (Spelling == "availability") {
writeAvailabilityValue(OS);
} else {
for (auto I = Args.begin(), E = Args.end(); I != E; ++ I) {
if (I != Args.begin()) OS << ", ";
(*I)->writeValue(OS);
}
}
if (!Args.empty())
OS << ")";
OS << Suffix.str() + "\";\n";
OS <<
" break;\n"
" }\n";
}
// End of the switch statement.
OS << "}\n";
// End of the print function.
OS << "}\n\n";
}
/// \brief Return the index of a spelling in a spelling list.
static unsigned
getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
const FlattenedSpelling &Spelling) {
assert(SpellingList.size() && "Spelling list is empty!");
for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
const FlattenedSpelling &S = SpellingList[Index];
if (S.variety() != Spelling.variety())
continue;
if (S.nameSpace() != Spelling.nameSpace())
continue;
if (S.name() != Spelling.name())
continue;
return Index;
}
llvm_unreachable("Unknown spelling!");
}
static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
for (const auto *Accessor : Accessors) {
std::string Name = Accessor->getValueAsString("Name");
std::vector<FlattenedSpelling> Spellings =
GetFlattenedSpellings(*Accessor);
std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
assert(SpellingList.size() &&
"Attribute with empty spelling list can't have accessors!");
OS << " bool " << Name << "() const { return SpellingListIndex == ";
for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
OS << getSpellingListIndex(SpellingList, Spellings[Index]);
if (Index != Spellings.size() -1)
OS << " ||\n SpellingListIndex == ";
else
OS << "; }\n";
}
}
}
static bool
SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
assert(!Spellings.empty() && "An empty list of spellings was provided");
std::string FirstName = NormalizeNameForSpellingComparison(
Spellings.front().name());
for (const auto &Spelling :
llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
if (Name != FirstName)
return false;
}
return true;
}
typedef std::map<unsigned, std::string> SemanticSpellingMap;
static std::string
CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
SemanticSpellingMap &Map) {
// The enumerants are automatically generated based on the variety,
// namespace (if present) and name for each attribute spelling. However,
// care is taken to avoid trampling on the reserved namespace due to
// underscores.
std::string Ret(" enum Spelling {\n");
std::set<std::string> Uniques;
unsigned Idx = 0;
for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
const FlattenedSpelling &S = *I;
std::string Variety = S.variety();
std::string Spelling = S.name();
std::string Namespace = S.nameSpace();
std::string EnumName = "";
EnumName += (Variety + "_");
if (!Namespace.empty())
EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
"_");
EnumName += NormalizeNameForSpellingComparison(Spelling);
// Even if the name is not unique, this spelling index corresponds to a
// particular enumerant name that we've calculated.
Map[Idx] = EnumName;
// Since we have been stripping underscores to avoid trampling on the
// reserved namespace, we may have inadvertently created duplicate
// enumerant names. These duplicates are not considered part of the
// semantic spelling, and can be elided.
if (Uniques.find(EnumName) != Uniques.end())
continue;
Uniques.insert(EnumName);
if (I != Spellings.begin())
Ret += ",\n";
Ret += " " + EnumName;
}
Ret += "\n };\n\n";
return Ret;
}
void WriteSemanticSpellingSwitch(const std::string &VarName,
const SemanticSpellingMap &Map,
raw_ostream &OS) {
OS << " switch (" << VarName << ") {\n default: "
<< "llvm_unreachable(\"Unknown spelling list index\");\n";
for (const auto &I : Map)
OS << " case " << I.first << ": return " << I.second << ";\n";
OS << " }\n";
}
// Emits the LateParsed property for attributes.
static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
for (const auto *Attr : Attrs) {
bool LateParsed = Attr->getValueAsBit("LateParsed");
if (LateParsed) {
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
// FIXME: Handle non-GNU attributes
for (const auto &I : Spellings) {
if (I.variety() != "GNU")
continue;
OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
}
}
}
OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
}
/// \brief Emits the first-argument-is-type property for attributes.
static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
for (const auto *Attr : Attrs) {
// Determine whether the first argument is a type.
std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
if (Args.empty())
continue;
if (Args[0]->getSuperClasses().back()->getName() != "TypeArgument")
continue;
// All these spellings take a single type argument.
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
std::set<std::string> Emitted;
for (const auto &S : Spellings) {
if (Emitted.insert(S.name()).second)
OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
}
}
OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
}
/// \brief Emits the parse-arguments-in-unevaluated-context property for
/// attributes.
static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
ParsedAttrMap Attrs = getParsedAttrList(Records);
for (const auto &I : Attrs) {
const Record &Attr = *I.second;
if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
continue;
// All these spellings take are parsed unevaluated.
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
std::set<std::string> Emitted;
for (const auto &S : Spellings) {
if (Emitted.insert(S.name()).second)
OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
}
}
OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
}
static bool isIdentifierArgument(Record *Arg) {
return !Arg->getSuperClasses().empty() &&
llvm::StringSwitch<bool>(Arg->getSuperClasses().back()->getName())
.Case("IdentifierArgument", true)
.Case("EnumArgument", true)
.Default(false);
}
// Emits the first-argument-is-identifier property for attributes.
static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
for (const auto *Attr : Attrs) {
// Determine whether the first argument is an identifier.
std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
if (Args.empty() || !isIdentifierArgument(Args[0]))
continue;
// All these spellings take an identifier argument.
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
std::set<std::string> Emitted;
for (const auto &S : Spellings) {
if (Emitted.insert(S.name()).second)
OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
}
}
OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
}
namespace clang {
// Emits the class definitions for attributes.
void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Attribute classes' definitions", OS);
OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
for (const auto *Attr : Attrs) {
const Record &R = *Attr;
// FIXME: Currently, documentation is generated as-needed due to the fact
// that there is no way to allow a generated project "reach into" the docs
// directory (for instance, it may be an out-of-tree build). However, we want
// to ensure that every attribute has a Documentation field, and produce an
// error if it has been neglected. Otherwise, the on-demand generation which
// happens server-side will fail. This code is ensuring that functionality,
// even though this Emitter doesn't technically need the documentation.
// When attribute documentation can be generated as part of the build
// itself, this code can be removed.
(void)R.getValueAsListOfDefs("Documentation");
if (!R.getValueAsBit("ASTNode"))
continue;
const std::vector<Record *> Supers = R.getSuperClasses();
assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
std::string SuperName;
for (const auto *Super : llvm::make_range(Supers.rbegin(), Supers.rend())) {
const Record &R = *Super;
if (R.getName() != "TargetSpecificAttr" && SuperName.empty())
SuperName = R.getName();
}
OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
std::vector<std::unique_ptr<Argument>> Args;
Args.reserve(ArgRecords.size());
for (const auto *ArgRecord : ArgRecords) {
Args.emplace_back(createArgument(*ArgRecord, R.getName()));
Args.back()->writeDeclarations(OS);
OS << "\n\n";
}
OS << "\npublic:\n";
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
// If there are zero or one spellings, all spelling-related functionality
// can be elided. If all of the spellings share the same name, the spelling
// functionality can also be elided.
bool ElideSpelling = (Spellings.size() <= 1) ||
SpellingNamesAreCommon(Spellings);
// This maps spelling index values to semantic Spelling enumerants.
SemanticSpellingMap SemanticToSyntacticMap;
if (!ElideSpelling)
OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
OS << " static " << R.getName() << "Attr *CreateImplicit(";
OS << "ASTContext &Ctx";
if (!ElideSpelling)
OS << ", Spelling S";
for (auto const &ai : Args) {
OS << ", ";
ai->writeCtorParameters(OS);
}
OS << ", SourceRange Loc = SourceRange()";
OS << ") {\n";
OS << " " << R.getName() << "Attr *A = new (Ctx) " << R.getName();
OS << "Attr(Loc, Ctx, ";
for (auto const &ai : Args) {
ai->writeImplicitCtorArgs(OS);
OS << ", ";
}
OS << (ElideSpelling ? "0" : "S") << ");\n";
OS << " A->setImplicit(true);\n";
OS << " return A;\n }\n\n";
OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
bool HasOpt = false;
for (auto const &ai : Args) {
OS << " , ";
ai->writeCtorParameters(OS);
OS << "\n";
if (ai->isOptional())
HasOpt = true;
}
OS << " , ";
OS << "unsigned SI\n";
OS << " )\n";
OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
for (auto const &ai : Args) {
OS << " , ";
ai->writeCtorInitializers(OS);
OS << "\n";
}
OS << " {\n";
for (auto const &ai : Args) {
ai->writeCtorBody(OS);
OS << "\n";
}
OS << " }\n\n";
// If there are optional arguments, write out a constructor that elides the
// optional arguments as well.
if (HasOpt) {
OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
for (auto const &ai : Args) {
if (!ai->isOptional()) {
OS << " , ";
ai->writeCtorParameters(OS);
OS << "\n";
}
}
OS << " , ";
OS << "unsigned SI\n";
OS << " )\n";
OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
for (auto const &ai : Args) {
OS << " , ";
ai->writeCtorDefaultInitializers(OS);
OS << "\n";
}
OS << " {\n";
for (auto const &ai : Args) {
if (!ai->isOptional()) {
ai->writeCtorBody(OS);
OS << "\n";
}
}
OS << " }\n\n";
}
OS << " " << R.getName() << "Attr *clone(ASTContext &C) const override;\n";
OS << " void printPretty(raw_ostream &OS,\n"
<< " const PrintingPolicy &Policy) const override;\n";
OS << " const char *getSpelling() const override;\n";
if (!ElideSpelling) {
assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
OS << " Spelling getSemanticSpelling() const {\n";
WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap,
OS);
OS << " }\n";
}
writeAttrAccessorDefinition(R, OS);
for (auto const &ai : Args) {
ai->writeAccessors(OS);
OS << "\n\n";
if (ai->isEnumArg())
static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
else if (ai->isVariadicEnumArg())
static_cast<const VariadicEnumArgument *>(ai.get())
->writeConversion(OS);
}
OS << R.getValueAsString("AdditionalMembers");
OS << "\n\n";
OS << " static bool classof(const Attr *A) { return A->getKind() == "
<< "attr::" << R.getName() << "; }\n";
bool LateParsed = R.getValueAsBit("LateParsed");
OS << " bool isLateParsed() const override { return "
<< LateParsed << "; }\n";
if (R.getValueAsBit("DuplicatesAllowedWhileMerging"))
OS << " bool duplicatesAllowed() const override { return true; }\n\n";
OS << "};\n\n";
}
OS << "#endif\n";
}
// Emits the class method definitions for attributes.
void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Attribute classes' member function definitions", OS);
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
for (auto *Attr : Attrs) {
Record &R = *Attr;
if (!R.getValueAsBit("ASTNode"))
continue;
std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
std::vector<std::unique_ptr<Argument>> Args;
for (const auto *Arg : ArgRecords)
Args.emplace_back(createArgument(*Arg, R.getName()));
for (auto const &ai : Args)
ai->writeAccessorDefinitions(OS);
OS << R.getName() << "Attr *" << R.getName()
<< "Attr::clone(ASTContext &C) const {\n";
OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C";
for (auto const &ai : Args) {
OS << ", ";
ai->writeCloneArgs(OS);
}
OS << ", getSpellingListIndex());\n";
OS << " A->Inherited = Inherited;\n";
OS << " A->IsPackExpansion = IsPackExpansion;\n";
OS << " A->Implicit = Implicit;\n";
OS << " return A;\n}\n\n";
writePrettyPrintFunction(R, Args, OS);
writeGetSpellingFunction(R, OS);
}
}
} // end namespace clang
static void EmitAttrList(raw_ostream &OS, StringRef Class,
const std::vector<Record*> &AttrList) {
std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
if (i != e) {
// Move the end iterator back to emit the last attribute.
for(--e; i != e; ++i) {
if (!(*i)->getValueAsBit("ASTNode"))
continue;
OS << Class << "(" << (*i)->getName() << ")\n";
}
OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
}
}
namespace clang {
// Emits the enumeration list for attributes.
void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
OS << "#ifndef LAST_ATTR\n";
OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
OS << "#endif\n\n";
OS << "#ifndef INHERITABLE_ATTR\n";
OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
OS << "#endif\n\n";
OS << "#ifndef LAST_INHERITABLE_ATTR\n";
OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
OS << "#endif\n\n";
OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
OS << "#endif\n\n";
OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
" INHERITABLE_PARAM_ATTR(NAME)\n";
OS << "#endif\n\n";
Record *InhClass = Records.getClass("InheritableAttr");
Record *InhParamClass = Records.getClass("InheritableParamAttr");
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
NonInhAttrs, InhAttrs, InhParamAttrs;
for (auto *Attr : Attrs) {
if (!Attr->getValueAsBit("ASTNode"))
continue;
if (Attr->isSubClassOf(InhParamClass))
InhParamAttrs.push_back(Attr);
else if (Attr->isSubClassOf(InhClass))
InhAttrs.push_back(Attr);
else
NonInhAttrs.push_back(Attr);
}
EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
EmitAttrList(OS, "ATTR", NonInhAttrs);
OS << "#undef LAST_ATTR\n";
OS << "#undef INHERITABLE_ATTR\n";
OS << "#undef LAST_INHERITABLE_ATTR\n";
OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
OS << "#undef ATTR\n";
}
// Emits the code to read an attribute from a precompiled header.
void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Attribute deserialization code", OS);
Record *InhClass = Records.getClass("InheritableAttr");
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
ArgRecords;
std::vector<std::unique_ptr<Argument>> Args;
OS << " switch (Kind) {\n";
OS << " default:\n";
OS << " llvm_unreachable(\"Unknown attribute!\");\n";
for (const auto *Attr : Attrs) {
const Record &R = *Attr;
if (!R.getValueAsBit("ASTNode"))
continue;
OS << " case attr::" << R.getName() << ": {\n";
if (R.isSubClassOf(InhClass))
OS << " bool isInherited = Record[Idx++];\n";
OS << " bool isImplicit = Record[Idx++];\n";
OS << " unsigned Spelling = Record[Idx++];\n";
ArgRecords = R.getValueAsListOfDefs("Args");
Args.clear();
for (const auto *Arg : ArgRecords) {
Args.emplace_back(createArgument(*Arg, R.getName()));
Args.back()->writePCHReadDecls(OS);
}
OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
for (auto const &ri : Args) {
OS << ", ";
ri->writePCHReadArgs(OS);
}
OS << ", Spelling);\n";
if (R.isSubClassOf(InhClass))
OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
OS << " New->setImplicit(isImplicit);\n";
OS << " break;\n";
OS << " }\n";
}
OS << " }\n";
}
// Emits the code to write an attribute to a precompiled header.
void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Attribute serialization code", OS);
Record *InhClass = Records.getClass("InheritableAttr");
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
OS << " switch (A->getKind()) {\n";
OS << " default:\n";
OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
OS << " break;\n";
for (const auto *Attr : Attrs) {
const Record &R = *Attr;
if (!R.getValueAsBit("ASTNode"))
continue;
OS << " case attr::" << R.getName() << ": {\n";
Args = R.getValueAsListOfDefs("Args");
if (R.isSubClassOf(InhClass) || !Args.empty())
OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
<< "Attr>(A);\n";
if (R.isSubClassOf(InhClass))
OS << " Record.push_back(SA->isInherited());\n";
OS << " Record.push_back(A->isImplicit());\n";
OS << " Record.push_back(A->getSpellingListIndex());\n";
for (const auto *Arg : Args)
createArgument(*Arg, R.getName())->writePCHWrite(OS);
OS << " break;\n";
OS << " }\n";
}
OS << " }\n";
}
static void GenerateHasAttrSpellingStringSwitch(
const std::vector<Record *> &Attrs, raw_ostream &OS,
const std::string &Variety = "", const std::string &Scope = "") {
for (const auto *Attr : Attrs) {
// It is assumed that there will be an llvm::Triple object named T within
// scope that can be used to determine whether the attribute exists in
// a given target.
std::string Test;
if (Attr->isSubClassOf("TargetSpecificAttr")) {
const Record *R = Attr->getValueAsDef("Target");
std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
Test += "(";
for (auto AI = Arches.begin(), AE = Arches.end(); AI != AE; ++AI) {
std::string Part = *AI;
Test += "T.getArch() == llvm::Triple::" + Part;
if (AI + 1 != AE)
Test += " || ";
}
Test += ")";
std::vector<std::string> OSes;
if (!R->isValueUnset("OSes")) {
Test += " && (";
std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
for (auto AI = OSes.begin(), AE = OSes.end(); AI != AE; ++AI) {
std::string Part = *AI;
Test += "T.getOS() == llvm::Triple::" + Part;
if (AI + 1 != AE)
Test += " || ";
}
Test += ")";
}
// If this is the C++11 variety, also add in the LangOpts test.
if (Variety == "CXX11")
Test += " && LangOpts.CPlusPlus11";
} else if (Variety == "CXX11")
// C++11 mode should be checked against LangOpts, which is presumed to be
// present in the caller.
Test = "LangOpts.CPlusPlus11";
else
Test = "true";
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
for (const auto &S : Spellings)
if (Variety.empty() || (Variety == S.variety() &&
(Scope.empty() || Scope == S.nameSpace())))
OS << " .Case(\"" << S.name() << "\", " << Test << ")\n";
}
OS << " .Default(false);\n";
}
// Emits the list of spellings for attributes.
void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
// Separate all of the attributes out into four group: generic, C++11, GNU,
// and declspecs. Then generate a big switch statement for each of them.
std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
std::vector<Record *> Declspec, GNU, Pragma;
std::map<std::string, std::vector<Record *>> CXX;
// Walk over the list of all attributes, and split them out based on the
// spelling variety.
for (auto *R : Attrs) {
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
for (const auto &SI : Spellings) {
std::string Variety = SI.variety();
if (Variety == "GNU")
GNU.push_back(R);
else if (Variety == "Declspec")
Declspec.push_back(R);
else if (Variety == "CXX11")
CXX[SI.nameSpace()].push_back(R);
else if (Variety == "Pragma")
Pragma.push_back(R);
}
}
OS << "switch (Syntax) {\n";
OS << "case AttrSyntax::Generic:\n";
OS << " return llvm::StringSwitch<bool>(Name)\n";
GenerateHasAttrSpellingStringSwitch(Attrs, OS);
OS << "case AttrSyntax::GNU:\n";
OS << " return llvm::StringSwitch<bool>(Name)\n";
GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
OS << "case AttrSyntax::Declspec:\n";
OS << " return llvm::StringSwitch<bool>(Name)\n";
GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
OS << "case AttrSyntax::Pragma:\n";
OS << " return llvm::StringSwitch<bool>(Name)\n";
GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
OS << "case AttrSyntax::CXX: {\n";
// C++11-style attributes are further split out based on the Scope.
for (std::map<std::string, std::vector<Record *>>::iterator I = CXX.begin(),
E = CXX.end();
I != E; ++I) {
if (I != CXX.begin())
OS << " else ";
if (I->first.empty())
OS << "if (!Scope || Scope->getName() == \"\") {\n";
else
OS << "if (Scope->getName() == \"" << I->first << "\") {\n";
OS << " return llvm::StringSwitch<bool>(Name)\n";
GenerateHasAttrSpellingStringSwitch(I->second, OS, "CXX11", I->first);
OS << "}";
}
OS << "\n}\n";
OS << "}\n";
}
void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Code to translate different attribute spellings "
"into internal identifiers", OS);
OS <<
" switch (AttrKind) {\n"
" default:\n"
" llvm_unreachable(\"Unknown attribute kind!\");\n"
" break;\n";
ParsedAttrMap Attrs = getParsedAttrList(Records);
for (const auto &I : Attrs) {
const Record &R = *I.second;
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
OS << " case AT_" << I.first << ": {\n";
for (unsigned I = 0; I < Spellings.size(); ++ I) {
OS << " if (Name == \"" << Spellings[I].name() << "\" && "
<< "SyntaxUsed == "
<< StringSwitch<unsigned>(Spellings[I].variety())
.Case("GNU", 0)
.Case("CXX11", 1)
.Case("Declspec", 2)
.Case("Keyword", 3)
.Case("Pragma", 4)
.Default(0)
<< " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
<< " return " << I << ";\n";
}
OS << " break;\n";
OS << " }\n";
}
OS << " }\n";
OS << " return 0;\n";
}
// Emits code used by RecursiveASTVisitor to visit attributes
void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
// Write method declarations for Traverse* methods.
// We emit this here because we only generate methods for attributes that
// are declared as ASTNodes.
OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
for (const auto *Attr : Attrs) {
const Record &R = *Attr;
if (!R.getValueAsBit("ASTNode"))
continue;
OS << " bool Traverse"
<< R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
OS << " bool Visit"
<< R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
<< " return true; \n"
<< " };\n";
}
OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
// Write individual Traverse* methods for each attribute class.
for (const auto *Attr : Attrs) {
const Record &R = *Attr;
if (!R.getValueAsBit("ASTNode"))
continue;
OS << "template <typename Derived>\n"
<< "bool VISITORCLASS<Derived>::Traverse"
<< R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
<< " if (!getDerived().VisitAttr(A))\n"
<< " return false;\n"
<< " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
<< " return false;\n";
std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
for (const auto *Arg : ArgRecords)
createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
OS << " return true;\n";
OS << "}\n\n";
}
// Write generic Traverse routine
OS << "template <typename Derived>\n"
<< "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
<< " if (!A)\n"
<< " return true;\n"
<< "\n"
<< " switch (A->getKind()) {\n"
<< " default:\n"
<< " return true;\n";
for (const auto *Attr : Attrs) {
const Record &R = *Attr;
if (!R.getValueAsBit("ASTNode"))
continue;
OS << " case attr::" << R.getName() << ":\n"
<< " return getDerived().Traverse" << R.getName() << "Attr("
<< "cast<" << R.getName() << "Attr>(A));\n";
}
OS << " }\n"; // end case
OS << "}\n"; // end function
OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
}
// Emits code to instantiate dependent attributes on templates.
void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Template instantiation code for attributes", OS);
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
OS << "namespace clang {\n"
<< "namespace sema {\n\n"
<< "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
<< "Sema &S,\n"
<< " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
<< " switch (At->getKind()) {\n"
<< " default:\n"
<< " break;\n";
for (const auto *Attr : Attrs) {
const Record &R = *Attr;
if (!R.getValueAsBit("ASTNode"))
continue;
OS << " case attr::" << R.getName() << ": {\n";
bool ShouldClone = R.getValueAsBit("Clone");
if (!ShouldClone) {
OS << " return NULL;\n";
OS << " }\n";
continue;
}
OS << " const " << R.getName() << "Attr *A = cast<"
<< R.getName() << "Attr>(At);\n";
bool TDependent = R.getValueAsBit("TemplateDependent");
if (!TDependent) {
OS << " return A->clone(C);\n";
OS << " }\n";
continue;
}
std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
std::vector<std::unique_ptr<Argument>> Args;
Args.reserve(ArgRecords.size());
for (const auto *ArgRecord : ArgRecords)
Args.emplace_back(createArgument(*ArgRecord, R.getName()));
for (auto const &ai : Args)
ai->writeTemplateInstantiation(OS);
OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
for (auto const &ai : Args) {
OS << ", ";
ai->writeTemplateInstantiationArgs(OS);
}
OS << ", A->getSpellingListIndex());\n }\n";
}
OS << " } // end switch\n"
<< " llvm_unreachable(\"Unknown attribute!\");\n"
<< " return 0;\n"
<< "}\n\n"
<< "} // end namespace sema\n"
<< "} // end namespace clang\n";
}
// Emits the list of parsed attributes.
void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
OS << "#ifndef PARSED_ATTR\n";
OS << "#define PARSED_ATTR(NAME) NAME\n";
OS << "#endif\n\n";
ParsedAttrMap Names = getParsedAttrList(Records);
for (const auto &I : Names) {
OS << "PARSED_ATTR(" << I.first << ")\n";
}
}
static void emitArgInfo(const Record &R, std::stringstream &OS) {
// This function will count the number of arguments specified for the
// attribute and emit the number of required arguments followed by the
// number of optional arguments.
std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
unsigned ArgCount = 0, OptCount = 0;
for (const auto *Arg : Args) {
Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
}
OS << ArgCount << ", " << OptCount;
}
static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
OS << "const Decl *) {\n";
OS << " return true;\n";
OS << "}\n\n";
}
static std::string CalculateDiagnostic(const Record &S) {
// If the SubjectList object has a custom diagnostic associated with it,
// return that directly.
std::string CustomDiag = S.getValueAsString("CustomDiag");
if (!CustomDiag.empty())
return CustomDiag;
// Given the list of subjects, determine what diagnostic best fits.
enum {
Func = 1U << 0,
Var = 1U << 1,
ObjCMethod = 1U << 2,
Param = 1U << 3,
Class = 1U << 4,
GenericRecord = 1U << 5,
Type = 1U << 6,
ObjCIVar = 1U << 7,
ObjCProp = 1U << 8,
ObjCInterface = 1U << 9,
Block = 1U << 10,
Namespace = 1U << 11,
Field = 1U << 12,
CXXMethod = 1U << 13,
ObjCProtocol = 1U << 14
};
uint32_t SubMask = 0;
std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
for (const auto *Subject : Subjects) {
const Record &R = *Subject;
std::string Name;
if (R.isSubClassOf("SubsetSubject")) {
PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
// As a fallback, look through the SubsetSubject to see what its base
// type is, and use that. This needs to be updated if SubsetSubjects
// are allowed within other SubsetSubjects.
Name = R.getValueAsDef("Base")->getName();
} else
Name = R.getName();
uint32_t V = StringSwitch<uint32_t>(Name)
.Case("Function", Func)
.Case("Var", Var)
.Case("ObjCMethod", ObjCMethod)
.Case("ParmVar", Param)
.Case("TypedefName", Type)
.Case("ObjCIvar", ObjCIVar)
.Case("ObjCProperty", ObjCProp)
.Case("Record", GenericRecord)
.Case("ObjCInterface", ObjCInterface)
.Case("ObjCProtocol", ObjCProtocol)
.Case("Block", Block)
.Case("CXXRecord", Class)
.Case("Namespace", Namespace)
.Case("Field", Field)
.Case("CXXMethod", CXXMethod)
.Default(0);
if (!V) {
// Something wasn't in our mapping, so be helpful and let the developer
// know about it.
PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName());
return "";
}
SubMask |= V;
}
switch (SubMask) {
// For the simple cases where there's only a single entry in the mask, we
// don't have to resort to bit fiddling.
case Func: return "ExpectedFunction";
case Var: return "ExpectedVariable";
case Param: return "ExpectedParameter";
case Class: return "ExpectedClass";
case CXXMethod:
// FIXME: Currently, this maps to ExpectedMethod based on existing code,
// but should map to something a bit more accurate at some point.
case ObjCMethod: return "ExpectedMethod";
case Type: return "ExpectedType";
case ObjCInterface: return "ExpectedObjectiveCInterface";
case ObjCProtocol: return "ExpectedObjectiveCProtocol";
// "GenericRecord" means struct, union or class; check the language options
// and if not compiling for C++, strip off the class part. Note that this
// relies on the fact that the context for this declares "Sema &S".
case GenericRecord:
return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
"ExpectedStructOrUnion)";
case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
case Func | Param:
case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
case Func | Var: return "ExpectedVariableOrFunction";
// If not compiling for C++, the class portion does not apply.
case Func | Var | Class:
return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
"ExpectedVariableOrFunction)";
case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
case ObjCProtocol | ObjCInterface:
return "ExpectedObjectiveCInterfaceOrProtocol";
case Field | Var: return "ExpectedFieldOrGlobalVar";
}
PrintFatalError(S.getLoc(),
"Could not deduce diagnostic argument for Attr subjects");
return "";
}
static std::string GetSubjectWithSuffix(const Record *R) {
std::string B = R->getName();
if (B == "DeclBase")
return "Decl";
return B + "Decl";
}
static std::string GenerateCustomAppertainsTo(const Record &Subject,
raw_ostream &OS) {
std::string FnName = "is" + Subject.getName();
// If this code has already been generated, simply return the previous
// instance of it.
static std::set<std::string> CustomSubjectSet;
std::set<std::string>::iterator I = CustomSubjectSet.find(FnName);
if (I != CustomSubjectSet.end())
return *I;
Record *Base = Subject.getValueAsDef("Base");
// Not currently support custom subjects within custom subjects.
if (Base->isSubClassOf("SubsetSubject")) {
PrintFatalError(Subject.getLoc(),
"SubsetSubjects within SubsetSubjects is not supported");
return "";
}
OS << "static bool " << FnName << "(const Decl *D) {\n";
OS << " if (const " << GetSubjectWithSuffix(Base) << " *S = dyn_cast<";
OS << GetSubjectWithSuffix(Base);
OS << ">(D))\n";
OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
OS << " return false;\n";
OS << "}\n\n";
CustomSubjectSet.insert(FnName);
return FnName;
}
static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
// If the attribute does not contain a Subjects definition, then use the
// default appertainsTo logic.
if (Attr.isValueUnset("Subjects"))
return "defaultAppertainsTo";
const Record *SubjectObj = Attr.getValueAsDef("Subjects");
std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
// If the list of subjects is empty, it is assumed that the attribute
// appertains to everything.
if (Subjects.empty())
return "defaultAppertainsTo";
bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
// Otherwise, generate an appertainsTo check specific to this attribute which
// checks all of the given subjects against the Decl passed in. Return the
// name of that check to the caller.
std::string FnName = "check" + Attr.getName() + "AppertainsTo";
std::stringstream SS;
SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
SS << "const Decl *D) {\n";
SS << " if (";
for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
// If the subject has custom code associated with it, generate a function
// for it. The function cannot be inlined into this check (yet) because it
// requires the subject to be of a specific type, and were that information
// inlined here, it would not support an attribute with multiple custom
// subjects.
if ((*I)->isSubClassOf("SubsetSubject")) {
SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
} else {
SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
}
if (I + 1 != E)
SS << " && ";
}
SS << ") {\n";
SS << " S.Diag(Attr.getLoc(), diag::";
SS << (Warn ? "warn_attribute_wrong_decl_type" :
"err_attribute_wrong_decl_type");
SS << ")\n";
SS << " << Attr.getName() << ";
SS << CalculateDiagnostic(*SubjectObj) << ";\n";
SS << " return false;\n";
SS << " }\n";
SS << " return true;\n";
SS << "}\n\n";
OS << SS.str();
return FnName;
}
static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
OS << "const AttributeList &) {\n";
OS << " return true;\n";
OS << "}\n\n";
}
static std::string GenerateLangOptRequirements(const Record &R,
raw_ostream &OS) {
// If the attribute has an empty or unset list of language requirements,
// return the default handler.
std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
if (LangOpts.empty())
return "defaultDiagnoseLangOpts";
// Generate the test condition, as well as a unique function name for the
// diagnostic test. The list of options should usually be short (one or two
// options), and the uniqueness isn't strictly necessary (it is just for
// codegen efficiency).
std::string FnName = "check", Test;
for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
std::string Part = (*I)->getValueAsString("Name");
Test += "S.LangOpts." + Part;
if (I + 1 != E)
Test += " || ";
FnName += Part;
}
FnName += "LangOpts";
// If this code has already been generated, simply return the previous
// instance of it.
static std::set<std::string> CustomLangOptsSet;
std::set<std::string>::iterator I = CustomLangOptsSet.find(FnName);
if (I != CustomLangOptsSet.end())
return *I;
OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
OS << " if (" << Test << ")\n";
OS << " return true;\n\n";
OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
OS << "<< Attr.getName();\n";
OS << " return false;\n";
OS << "}\n\n";
CustomLangOptsSet.insert(FnName);
return FnName;
}
static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
OS << "static bool defaultTargetRequirements(const llvm::Triple &) {\n";
OS << " return true;\n";
OS << "}\n\n";
}
static std::string GenerateTargetRequirements(const Record &Attr,
const ParsedAttrMap &Dupes,
raw_ostream &OS) {
// If the attribute is not a target specific attribute, return the default
// target handler.
if (!Attr.isSubClassOf("TargetSpecificAttr"))
return "defaultTargetRequirements";
// Get the list of architectures to be tested for.
const Record *R = Attr.getValueAsDef("Target");
std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
if (Arches.empty()) {
PrintError(Attr.getLoc(), "Empty list of target architectures for a "
"target-specific attr");
return "defaultTargetRequirements";
}
// If there are other attributes which share the same parsed attribute kind,
// such as target-specific attributes with a shared spelling, collapse the
// duplicate architectures. This is required because a shared target-specific
// attribute has only one AttributeList::Kind enumeration value, but it
// applies to multiple target architectures. In order for the attribute to be
// considered valid, all of its architectures need to be included.
if (!Attr.isValueUnset("ParseKind")) {
std::string APK = Attr.getValueAsString("ParseKind");
for (const auto &I : Dupes) {
if (I.first == APK) {
std::vector<std::string> DA = I.second->getValueAsDef("Target")
->getValueAsListOfStrings("Arches");
std::copy(DA.begin(), DA.end(), std::back_inserter(Arches));
}
}
}
std::string FnName = "isTarget", Test = "(";
for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
std::string Part = *I;
Test += "Arch == llvm::Triple::" + Part;
if (I + 1 != E)
Test += " || ";
FnName += Part;
}
Test += ")";
// If the target also requires OS testing, generate those tests as well.
bool UsesOS = false;
if (!R->isValueUnset("OSes")) {
UsesOS = true;
// We know that there was at least one arch test, so we need to and in the
// OS tests.
Test += " && (";
std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) {
std::string Part = *I;
Test += "OS == llvm::Triple::" + Part;
if (I + 1 != E)
Test += " || ";
FnName += Part;
}
Test += ")";
}
// If this code has already been generated, simply return the previous
// instance of it.
static std::set<std::string> CustomTargetSet;
std::set<std::string>::iterator I = CustomTargetSet.find(FnName);
if (I != CustomTargetSet.end())
return *I;
OS << "static bool " << FnName << "(const llvm::Triple &T) {\n";
OS << " llvm::Triple::ArchType Arch = T.getArch();\n";
if (UsesOS)
OS << " llvm::Triple::OSType OS = T.getOS();\n";
OS << " return " << Test << ";\n";
OS << "}\n\n";
CustomTargetSet.insert(FnName);
return FnName;
}
static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
<< "const AttributeList &Attr) {\n";
OS << " return UINT_MAX;\n";
OS << "}\n\n";
}
static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
raw_ostream &OS) {
// If the attribute does not have a semantic form, we can bail out early.
if (!Attr.getValueAsBit("ASTNode"))
return "defaultSpellingIndexToSemanticSpelling";
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
// If there are zero or one spellings, or all of the spellings share the same
// name, we can also bail out early.
if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
return "defaultSpellingIndexToSemanticSpelling";
// Generate the enumeration we will use for the mapping.
SemanticSpellingMap SemanticToSyntacticMap;
std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
std::string Name = Attr.getName() + "AttrSpellingMap";
OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n";
OS << Enum;
OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
OS << "}\n\n";
return Name;
}
static bool IsKnownToGCC(const Record &Attr) {
// Look at the spellings for this subject; if there are any spellings which
// claim to be known to GCC, the attribute is known to GCC.
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
for (const auto &I : Spellings) {
if (I.knownToGCC())
return true;
}
return false;
}
/// Emits the parsed attribute helpers
void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Parsed attribute helpers", OS);
// Get the list of parsed attributes, and accept the optional list of
// duplicates due to the ParseKind.
ParsedAttrMap Dupes;
ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
// Generate the default appertainsTo, target and language option diagnostic,
// and spelling list index mapping methods.
GenerateDefaultAppertainsTo(OS);
GenerateDefaultLangOptRequirements(OS);
GenerateDefaultTargetRequirements(OS);
GenerateDefaultSpellingIndexToSemanticSpelling(OS);
// Generate the appertainsTo diagnostic methods and write their names into
// another mapping. At the same time, generate the AttrInfoMap object
// contents. Due to the reliance on generated code, use separate streams so
// that code will not be interleaved.
std::stringstream SS;
for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
// TODO: If the attribute's kind appears in the list of duplicates, that is
// because it is a target-specific attribute that appears multiple times.
// It would be beneficial to test whether the duplicates are "similar
// enough" to each other to not cause problems. For instance, check that
// the spellings are identical, and custom parsing rules match, etc.
// We need to generate struct instances based off ParsedAttrInfo from
// AttributeList.cpp.
SS << " { ";
emitArgInfo(*I->second, SS);
SS << ", " << I->second->getValueAsBit("HasCustomParsing");
SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
SS << ", " << I->second->isSubClassOf("TypeAttr");
SS << ", " << IsKnownToGCC(*I->second);
SS << ", " << GenerateAppertainsTo(*I->second, OS);
SS << ", " << GenerateLangOptRequirements(*I->second, OS);
SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
SS << " }";
if (I + 1 != E)
SS << ",";
SS << " // AT_" << I->first << "\n";
}
OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
OS << SS.str();
OS << "};\n\n";
}
// Emits the kind list of parsed attributes
void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Attribute name matcher", OS);
std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
std::vector<StringMatcher::StringPair> GNU, Declspec, CXX11, Keywords, Pragma;
std::set<std::string> Seen;
for (const auto *A : Attrs) {
const Record &Attr = *A;
bool SemaHandler = Attr.getValueAsBit("SemaHandler");
bool Ignored = Attr.getValueAsBit("Ignored");
if (SemaHandler || Ignored) {
// Attribute spellings can be shared between target-specific attributes,
// and can be shared between syntaxes for the same attribute. For
// instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
// specific attribute, or MSP430-specific attribute. Additionally, an
// attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
// for the same semantic attribute. Ultimately, we need to map each of
// these to a single AttributeList::Kind value, but the StringMatcher
// class cannot handle duplicate match strings. So we generate a list of
// string to match based on the syntax, and emit multiple string matchers
// depending on the syntax used.
std::string AttrName;
if (Attr.isSubClassOf("TargetSpecificAttr") &&
!Attr.isValueUnset("ParseKind")) {
AttrName = Attr.getValueAsString("ParseKind");
if (Seen.find(AttrName) != Seen.end())
continue;
Seen.insert(AttrName);
} else
AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
for (const auto &S : Spellings) {
std::string RawSpelling = S.name();
std::vector<StringMatcher::StringPair> *Matches = nullptr;
std::string Spelling, Variety = S.variety();
if (Variety == "CXX11") {
Matches = &CXX11;
Spelling += S.nameSpace();
Spelling += "::";
} else if (Variety == "GNU")
Matches = &GNU;
else if (Variety == "Declspec")
Matches = &Declspec;
else if (Variety == "Keyword")
Matches = &Keywords;
else if (Variety == "Pragma")
Matches = &Pragma;
assert(Matches && "Unsupported spelling variety found");
Spelling += NormalizeAttrSpelling(RawSpelling);
if (SemaHandler)
Matches->push_back(StringMatcher::StringPair(Spelling,
"return AttributeList::AT_" + AttrName + ";"));
else
Matches->push_back(StringMatcher::StringPair(Spelling,
"return AttributeList::IgnoredAttribute;"));
}
}
}
OS << "static AttributeList::Kind getAttrKind(StringRef Name, ";
OS << "AttributeList::Syntax Syntax) {\n";
OS << " if (AttributeList::AS_GNU == Syntax) {\n";
StringMatcher("Name", GNU, OS).Emit();
OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n";
StringMatcher("Name", Declspec, OS).Emit();
OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n";
StringMatcher("Name", CXX11, OS).Emit();
OS << " } else if (AttributeList::AS_Keyword == Syntax) {\n";
StringMatcher("Name", Keywords, OS).Emit();
OS << " } else if (AttributeList::AS_Pragma == Syntax) {\n";
StringMatcher("Name", Pragma, OS).Emit();
OS << " }\n";
OS << " return AttributeList::UnknownAttribute;\n"
<< "}\n";
}
// Emits the code to dump an attribute.
void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
emitSourceFileHeader("Attribute dumper", OS);
OS <<
" switch (A->getKind()) {\n"
" default:\n"
" llvm_unreachable(\"Unknown attribute kind!\");\n"
" break;\n";
std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
for (const auto *Attr : Attrs) {
const Record &R = *Attr;
if (!R.getValueAsBit("ASTNode"))
continue;
OS << " case attr::" << R.getName() << ": {\n";
// If the attribute has a semantically-meaningful name (which is determined
// by whether there is a Spelling enumeration for it), then write out the
// spelling used for the attribute.
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
OS << " OS << \" \" << A->getSpelling();\n";
Args = R.getValueAsListOfDefs("Args");
if (!Args.empty()) {
OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
<< "Attr>(A);\n";
for (const auto *Arg : Args)
createArgument(*Arg, R.getName())->writeDump(OS);
// Code for detecting the last child.
OS << " bool OldMoreChildren = hasMoreChildren();\n";
OS << " bool MoreChildren;\n";
for (auto AI = Args.begin(), AE = Args.end(); AI != AE; ++AI) {
// More code for detecting the last child.
OS << " MoreChildren = OldMoreChildren";
for (auto Next = AI + 1; Next != AE; ++Next) {
OS << " || ";
createArgument(**Next, R.getName())->writeHasChildren(OS);
}
OS << ";\n";
OS << " setMoreChildren(MoreChildren);\n";
createArgument(**AI, R.getName())->writeDumpChildren(OS);
}
// Reset the last child.
OS << " setMoreChildren(OldMoreChildren);\n";
}
OS <<
" break;\n"
" }\n";
}
OS << " }\n";
}
void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
raw_ostream &OS) {
emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
emitClangAttrArgContextList(Records, OS);
emitClangAttrIdentifierArgList(Records, OS);
emitClangAttrTypeArgList(Records, OS);
emitClangAttrLateParsedList(Records, OS);
}
class DocumentationData {
public:
const Record *Documentation;
const Record *Attribute;
DocumentationData(const Record &Documentation, const Record &Attribute)
: Documentation(&Documentation), Attribute(&Attribute) {}
};
static void WriteCategoryHeader(const Record *DocCategory,
raw_ostream &OS) {
const std::string &Name = DocCategory->getValueAsString("Name");
OS << Name << "\n" << std::string(Name.length(), '=') << "\n";
// If there is content, print that as well.
std::string ContentStr = DocCategory->getValueAsString("Content");
if (!ContentStr.empty()) {
// Trim leading and trailing newlines and spaces.
StringRef Content(ContentStr);
while (Content.startswith("\r") || Content.startswith("\n") ||
Content.startswith(" ") || Content.startswith("\t"))
Content = Content.substr(1);
while (Content.endswith("\r") || Content.endswith("\n") ||
Content.endswith(" ") || Content.endswith("\t"))
Content = Content.substr(0, Content.size() - 1);
OS << Content;
}
OS << "\n\n";
}
enum SpellingKind {
GNU = 1 << 0,
CXX11 = 1 << 1,
Declspec = 1 << 2,
Keyword = 1 << 3,
Pragma = 1 << 4
};
static void WriteDocumentation(const DocumentationData &Doc,
raw_ostream &OS) {
// FIXME: there is no way to have a per-spelling category for the attribute
// documentation. This may not be a limiting factor since the spellings
// should generally be consistently applied across the category.
std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Doc.Attribute);
// Determine the heading to be used for this attribute.
std::string Heading = Doc.Documentation->getValueAsString("Heading");
bool CustomHeading = !Heading.empty();
if (Heading.empty()) {
// If there's only one spelling, we can simply use that.
if (Spellings.size() == 1)
Heading = Spellings.begin()->name();
else {
std::set<std::string> Uniques;
for (auto I = Spellings.begin(), E = Spellings.end();
I != E && Uniques.size() <= 1; ++I) {
std::string Spelling = NormalizeNameForSpellingComparison(I->name());
Uniques.insert(Spelling);
}
// If the semantic map has only one spelling, that is sufficient for our
// needs.
if (Uniques.size() == 1)
Heading = *Uniques.begin();
}
}
// If the heading is still empty, it is an error.
if (Heading.empty())
PrintFatalError(Doc.Attribute->getLoc(),
"This attribute requires a heading to be specified");
// Gather a list of unique spellings; this is not the same as the semantic
// spelling for the attribute. Variations in underscores and other non-
// semantic characters are still acceptable.
std::vector<std::string> Names;
unsigned SupportedSpellings = 0;
for (const auto &I : Spellings) {
SpellingKind Kind = StringSwitch<SpellingKind>(I.variety())
.Case("GNU", GNU)
.Case("CXX11", CXX11)
.Case("Declspec", Declspec)
.Case("Keyword", Keyword)
.Case("Pragma", Pragma);
// Mask in the supported spelling.
SupportedSpellings |= Kind;
std::string Name;
if (Kind == CXX11 && !I.nameSpace().empty())
Name = I.nameSpace() + "::";
Name += I.name();
// If this name is the same as the heading, do not add it.
if (Name != Heading)
Names.push_back(Name);
}
// Print out the heading for the attribute. If there are alternate spellings,
// then display those after the heading.
if (!CustomHeading && !Names.empty()) {
Heading += " (";
for (auto I = Names.begin(), E = Names.end(); I != E; ++I) {
if (I != Names.begin())
Heading += ", ";
Heading += *I;
}
Heading += ")";
}
OS << Heading << "\n" << std::string(Heading.length(), '-') << "\n";
if (!SupportedSpellings)
PrintFatalError(Doc.Attribute->getLoc(),
"Attribute has no supported spellings; cannot be "
"documented");
// List what spelling syntaxes the attribute supports.
OS << ".. csv-table:: Supported Syntaxes\n";
OS << " :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\",";
OS << " \"Pragma\"\n\n";
OS << " \"";
if (SupportedSpellings & GNU) OS << "X";
OS << "\",\"";
if (SupportedSpellings & CXX11) OS << "X";
OS << "\",\"";
if (SupportedSpellings & Declspec) OS << "X";
OS << "\",\"";
if (SupportedSpellings & Keyword) OS << "X";
OS << "\", \"";
if (SupportedSpellings & Pragma) OS << "X";
OS << "\"\n\n";
// If the attribute is deprecated, print a message about it, and possibly
// provide a replacement attribute.
if (!Doc.Documentation->isValueUnset("Deprecated")) {
OS << "This attribute has been deprecated, and may be removed in a future "
<< "version of Clang.";
const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
std::string Replacement = Deprecated.getValueAsString("Replacement");
if (!Replacement.empty())
OS << " This attribute has been superseded by ``"
<< Replacement << "``.";
OS << "\n\n";
}
std::string ContentStr = Doc.Documentation->getValueAsString("Content");
// Trim leading and trailing newlines and spaces.
StringRef Content(ContentStr);
while (Content.startswith("\r") || Content.startswith("\n") ||
Content.startswith(" ") || Content.startswith("\t"))
Content = Content.substr(1);
while (Content.endswith("\r") || Content.endswith("\n") ||
Content.endswith(" ") || Content.endswith("\t"))
Content = Content.substr(0, Content.size() - 1);
OS << Content;
OS << "\n\n\n";
}
void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
// Get the documentation introduction paragraph.
const Record *Documentation = Records.getDef("GlobalDocumentation");
if (!Documentation) {
PrintFatalError("The Documentation top-level definition is missing, "
"no documentation will be generated.");
return;
}
OS << Documentation->getValueAsString("Intro") << "\n";
// Gather the Documentation lists from each of the attributes, based on the
// category provided.
std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
for (const auto *A : Attrs) {
const Record &Attr = *A;
std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
for (const auto *D : Docs) {
const Record &Doc = *D;
const Record *Category = Doc.getValueAsDef("Category");
// If the category is "undocumented", then there cannot be any other
// documentation categories (otherwise, the attribute would become
// documented).
std::string Cat = Category->getValueAsString("Name");
bool Undocumented = Cat == "Undocumented";
if (Undocumented && Docs.size() > 1)
PrintFatalError(Doc.getLoc(),
"Attribute is \"Undocumented\", but has multiple "
"documentation categories");
if (!Undocumented)
SplitDocs[Category].push_back(DocumentationData(Doc, Attr));
}
}
// Having split the attributes out based on what documentation goes where,
// we can begin to generate sections of documentation.
for (const auto &I : SplitDocs) {
WriteCategoryHeader(I.first, OS);
// Walk over each of the attributes in the category and write out their
// documentation.
for (const auto &Doc : I.second)
WriteDocumentation(Doc, OS);
}
}
} // end namespace clang
|