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 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
|
// ASBeautifier.cpp
// Copyright (c) 2017 by Jim Pattee <jimp03@email.com>.
// This code is licensed under the MIT License.
// License.md describes the conditions under which this software may be distributed.
//-----------------------------------------------------------------------------
// headers
//-----------------------------------------------------------------------------
#include "astyle.h"
#include <algorithm>
//-----------------------------------------------------------------------------
// astyle namespace
//-----------------------------------------------------------------------------
namespace astyle {
//
// this must be global
static int g_preprocessorCppExternCBrace;
//-----------------------------------------------------------------------------
// ASBeautifier class
//-----------------------------------------------------------------------------
/**
* ASBeautifier's constructor
* This constructor is called only once for each source file.
* The cloned ASBeautifier objects are created with the copy constructor.
*/
ASBeautifier::ASBeautifier()
{
waitingBeautifierStack = nullptr;
activeBeautifierStack = nullptr;
waitingBeautifierStackLengthStack = nullptr;
activeBeautifierStackLengthStack = nullptr;
headerStack = nullptr;
tempStacks = nullptr;
squareBracketDepthStack = nullptr;
blockStatementStack = nullptr;
parenStatementStack = nullptr;
braceBlockStateStack = nullptr;
continuationIndentStack = nullptr;
continuationIndentStackSizeStack = nullptr;
parenIndentStack = nullptr;
preprocIndentStack = nullptr;
sourceIterator = nullptr;
isModeManuallySet = false;
shouldForceTabIndentation = false;
setSpaceIndentation(4);
setContinuationIndentation(1);
setMinConditionalIndentOption(MINCOND_TWO);
setMaxContinuationIndentLength(40);
classInitializerIndents = 1;
tabLength = 0;
setClassIndent(false);
setModifierIndent(false);
setSwitchIndent(false);
setCaseIndent(false);
setBlockIndent(false);
setBraceIndent(false);
setBraceIndentVtk(false);
setNamespaceIndent(false);
setAfterParenIndent(false);
setLabelIndent(false);
setEmptyLineFill(false);
setCStyle();
setPreprocDefineIndent(false);
setPreprocConditionalIndent(false);
setAlignMethodColon(false);
// initialize ASBeautifier member vectors
beautifierFileType = 9; // reset to an invalid type
headers = new vector<const string*>;
nonParenHeaders = new vector<const string*>;
assignmentOperators = new vector<const string*>;
nonAssignmentOperators = new vector<const string*>;
preBlockStatements = new vector<const string*>;
preCommandHeaders = new vector<const string*>;
indentableHeaders = new vector<const string*>;
}
/**
* ASBeautifier's copy constructor
* Copy the vector objects to vectors in the new ASBeautifier
* object so the new object can be destroyed without deleting
* the vector objects in the copied vector.
* This is the reason a copy constructor is needed.
*
* Must explicitly call the base class copy constructor.
*/
ASBeautifier::ASBeautifier(const ASBeautifier& other) : ASBase(other)
{
// these don't need to copy the stack
waitingBeautifierStack = nullptr;
activeBeautifierStack = nullptr;
waitingBeautifierStackLengthStack = nullptr;
activeBeautifierStackLengthStack = nullptr;
// vector '=' operator performs a DEEP copy of all elements in the vector
headerStack = new vector<const string*>;
*headerStack = *other.headerStack;
tempStacks = copyTempStacks(other);
squareBracketDepthStack = new vector<int>;
*squareBracketDepthStack = *other.squareBracketDepthStack;
blockStatementStack = new vector<bool>;
*blockStatementStack = *other.blockStatementStack;
parenStatementStack = new vector<bool>;
*parenStatementStack = *other.parenStatementStack;
braceBlockStateStack = new vector<bool>;
*braceBlockStateStack = *other.braceBlockStateStack;
continuationIndentStack = new vector<int>;
*continuationIndentStack = *other.continuationIndentStack;
continuationIndentStackSizeStack = new vector<int>;
*continuationIndentStackSizeStack = *other.continuationIndentStackSizeStack;
parenIndentStack = new vector<int>;
*parenIndentStack = *other.parenIndentStack;
preprocIndentStack = new vector<pair<int, int> >;
*preprocIndentStack = *other.preprocIndentStack;
// Copy the pointers to vectors.
// This is ok because the original ASBeautifier object
// is not deleted until end of job.
beautifierFileType = other.beautifierFileType;
headers = other.headers;
nonParenHeaders = other.nonParenHeaders;
assignmentOperators = other.assignmentOperators;
nonAssignmentOperators = other.nonAssignmentOperators;
preBlockStatements = other.preBlockStatements;
preCommandHeaders = other.preCommandHeaders;
indentableHeaders = other.indentableHeaders;
// protected variables
// variables set by ASFormatter
// must also be updated in activeBeautifierStack
inLineNumber = other.inLineNumber;
runInIndentContinuation = other.runInIndentContinuation;
nonInStatementBrace = other.nonInStatementBrace;
objCColonAlignSubsequent = other.objCColonAlignSubsequent;
lineCommentNoBeautify = other.lineCommentNoBeautify;
isElseHeaderIndent = other.isElseHeaderIndent;
isCaseHeaderCommentIndent = other.isCaseHeaderCommentIndent;
isNonInStatementArray = other.isNonInStatementArray;
isSharpAccessor = other.isSharpAccessor;
isSharpDelegate = other.isSharpDelegate;
isInExternC = other.isInExternC;
isInBeautifySQL = other.isInBeautifySQL;
isInIndentableStruct = other.isInIndentableStruct;
isInIndentablePreproc = other.isInIndentablePreproc;
// private variables
sourceIterator = other.sourceIterator;
currentHeader = other.currentHeader;
previousLastLineHeader = other.previousLastLineHeader;
probationHeader = other.probationHeader;
lastLineHeader = other.lastLineHeader;
indentString = other.indentString;
verbatimDelimiter = other.verbatimDelimiter;
isInQuote = other.isInQuote;
isInVerbatimQuote = other.isInVerbatimQuote;
haveLineContinuationChar = other.haveLineContinuationChar;
isInAsm = other.isInAsm;
isInAsmOneLine = other.isInAsmOneLine;
isInAsmBlock = other.isInAsmBlock;
isInComment = other.isInComment;
isInPreprocessorComment = other.isInPreprocessorComment;
isInRunInComment = other.isInRunInComment;
isInCase = other.isInCase;
isInQuestion = other.isInQuestion;
isContinuation = other.isContinuation;
isInHeader = other.isInHeader;
isInTemplate = other.isInTemplate;
isInDefine = other.isInDefine;
isInDefineDefinition = other.isInDefineDefinition;
classIndent = other.classIndent;
isIndentModeOff = other.isIndentModeOff;
isInClassHeader = other.isInClassHeader;
isInClassHeaderTab = other.isInClassHeaderTab;
isInClassInitializer = other.isInClassInitializer;
isInClass = other.isInClass;
isInObjCMethodDefinition = other.isInObjCMethodDefinition;
isInObjCMethodCall = other.isInObjCMethodCall;
isInObjCMethodCallFirst = other.isInObjCMethodCallFirst;
isImmediatelyPostObjCMethodDefinition = other.isImmediatelyPostObjCMethodDefinition;
isImmediatelyPostObjCMethodCall = other.isImmediatelyPostObjCMethodCall;
isInIndentablePreprocBlock = other.isInIndentablePreprocBlock;
isInObjCInterface = other.isInObjCInterface;
isInEnum = other.isInEnum;
isInEnumTypeID = other.isInEnumTypeID;
isInLet = other.isInLet;
modifierIndent = other.modifierIndent;
switchIndent = other.switchIndent;
caseIndent = other.caseIndent;
namespaceIndent = other.namespaceIndent;
braceIndent = other.braceIndent;
braceIndentVtk = other.braceIndentVtk;
blockIndent = other.blockIndent;
shouldIndentAfterParen = other.shouldIndentAfterParen;
labelIndent = other.labelIndent;
isInConditional = other.isInConditional;
isModeManuallySet = other.isModeManuallySet;
shouldForceTabIndentation = other.shouldForceTabIndentation;
emptyLineFill = other.emptyLineFill;
lineOpensWithLineComment = other.lineOpensWithLineComment;
lineOpensWithComment = other.lineOpensWithComment;
lineStartsInComment = other.lineStartsInComment;
backslashEndsPrevLine = other.backslashEndsPrevLine;
blockCommentNoIndent = other.blockCommentNoIndent;
blockCommentNoBeautify = other.blockCommentNoBeautify;
previousLineProbationTab = other.previousLineProbationTab;
lineBeginsWithOpenBrace = other.lineBeginsWithOpenBrace;
lineBeginsWithCloseBrace = other.lineBeginsWithCloseBrace;
lineBeginsWithComma = other.lineBeginsWithComma;
lineIsCommentOnly = other.lineIsCommentOnly;
lineIsLineCommentOnly = other.lineIsLineCommentOnly;
shouldIndentBracedLine = other.shouldIndentBracedLine;
isInSwitch = other.isInSwitch;
foundPreCommandHeader = other.foundPreCommandHeader;
foundPreCommandMacro = other.foundPreCommandMacro;
shouldAlignMethodColon = other.shouldAlignMethodColon;
shouldIndentPreprocDefine = other.shouldIndentPreprocDefine;
shouldIndentPreprocConditional = other.shouldIndentPreprocConditional;
indentCount = other.indentCount;
spaceIndentCount = other.spaceIndentCount;
spaceIndentObjCMethodAlignment = other.spaceIndentObjCMethodAlignment;
bracePosObjCMethodAlignment = other.bracePosObjCMethodAlignment;
colonIndentObjCMethodAlignment = other.colonIndentObjCMethodAlignment;
lineOpeningBlocksNum = other.lineOpeningBlocksNum;
lineClosingBlocksNum = other.lineClosingBlocksNum;
fileType = other.fileType;
minConditionalOption = other.minConditionalOption;
minConditionalIndent = other.minConditionalIndent;
parenDepth = other.parenDepth;
indentLength = other.indentLength;
tabLength = other.tabLength;
continuationIndent = other.continuationIndent;
blockTabCount = other.blockTabCount;
maxContinuationIndent = other.maxContinuationIndent;
classInitializerIndents = other.classInitializerIndents;
templateDepth = other.templateDepth;
squareBracketCount = other.squareBracketCount;
prevFinalLineSpaceIndentCount = other.prevFinalLineSpaceIndentCount;
prevFinalLineIndentCount = other.prevFinalLineIndentCount;
defineIndentCount = other.defineIndentCount;
preprocBlockIndent = other.preprocBlockIndent;
quoteChar = other.quoteChar;
prevNonSpaceCh = other.prevNonSpaceCh;
currentNonSpaceCh = other.currentNonSpaceCh;
currentNonLegalCh = other.currentNonLegalCh;
prevNonLegalCh = other.prevNonLegalCh;
}
/**
* ASBeautifier's destructor
*/
ASBeautifier::~ASBeautifier()
{
deleteBeautifierContainer(waitingBeautifierStack);
deleteBeautifierContainer(activeBeautifierStack);
deleteContainer(waitingBeautifierStackLengthStack);
deleteContainer(activeBeautifierStackLengthStack);
deleteContainer(headerStack);
deleteTempStacksContainer(tempStacks);
deleteContainer(squareBracketDepthStack);
deleteContainer(blockStatementStack);
deleteContainer(parenStatementStack);
deleteContainer(braceBlockStateStack);
deleteContainer(continuationIndentStack);
deleteContainer(continuationIndentStackSizeStack);
deleteContainer(parenIndentStack);
deleteContainer(preprocIndentStack);
}
/**
* initialize the ASBeautifier.
*
* This init() should be called every time a ABeautifier object is to start
* beautifying a NEW source file.
* It is called only when a new ASFormatter object is created.
* init() receives a pointer to a ASSourceIterator object that will be
* used to iterate through the source code.
*
* @param iter a pointer to the ASSourceIterator or ASStreamIterator object.
*/
void ASBeautifier::init(ASSourceIterator* iter)
{
sourceIterator = iter;
initVectors();
ASBase::init(getFileType());
g_preprocessorCppExternCBrace = 0;
initContainer(waitingBeautifierStack, new vector<ASBeautifier*>);
initContainer(activeBeautifierStack, new vector<ASBeautifier*>);
initContainer(waitingBeautifierStackLengthStack, new vector<int>);
initContainer(activeBeautifierStackLengthStack, new vector<int>);
initContainer(headerStack, new vector<const string*>);
initTempStacksContainer(tempStacks, new vector<vector<const string*>*>);
tempStacks->emplace_back(new vector<const string*>);
initContainer(squareBracketDepthStack, new vector<int>);
initContainer(blockStatementStack, new vector<bool>);
initContainer(parenStatementStack, new vector<bool>);
initContainer(braceBlockStateStack, new vector<bool>);
braceBlockStateStack->push_back(true);
initContainer(continuationIndentStack, new vector<int>);
initContainer(continuationIndentStackSizeStack, new vector<int>);
continuationIndentStackSizeStack->emplace_back(0);
initContainer(parenIndentStack, new vector<int>);
initContainer(preprocIndentStack, new vector<pair<int, int> >);
previousLastLineHeader = nullptr;
currentHeader = nullptr;
isInQuote = false;
isInVerbatimQuote = false;
haveLineContinuationChar = false;
isInAsm = false;
isInAsmOneLine = false;
isInAsmBlock = false;
isInComment = false;
isInPreprocessorComment = false;
isInRunInComment = false;
isContinuation = false;
isInCase = false;
isInQuestion = false;
isIndentModeOff = false;
isInClassHeader = false;
isInClassHeaderTab = false;
isInClassInitializer = false;
isInClass = false;
isInObjCMethodDefinition = false;
isInObjCMethodCall = false;
isInObjCMethodCallFirst = false;
isImmediatelyPostObjCMethodDefinition = false;
isImmediatelyPostObjCMethodCall = false;
isInIndentablePreprocBlock = false;
isInObjCInterface = false;
isInEnum = false;
isInEnumTypeID = false;
isInLet = false;
isInHeader = false;
isInTemplate = false;
isInConditional = false;
indentCount = 0;
spaceIndentCount = 0;
spaceIndentObjCMethodAlignment = 0;
bracePosObjCMethodAlignment = 0;
colonIndentObjCMethodAlignment = 0;
lineOpeningBlocksNum = 0;
lineClosingBlocksNum = 0;
templateDepth = 0;
squareBracketCount = 0;
parenDepth = 0;
blockTabCount = 0;
prevFinalLineSpaceIndentCount = 0;
prevFinalLineIndentCount = 0;
defineIndentCount = 0;
preprocBlockIndent = 0;
prevNonSpaceCh = '{';
currentNonSpaceCh = '{';
prevNonLegalCh = '{';
currentNonLegalCh = '{';
quoteChar = ' ';
probationHeader = nullptr;
lastLineHeader = nullptr;
backslashEndsPrevLine = false;
lineOpensWithLineComment = false;
lineOpensWithComment = false;
lineStartsInComment = false;
isInDefine = false;
isInDefineDefinition = false;
lineCommentNoBeautify = false;
isElseHeaderIndent = false;
isCaseHeaderCommentIndent = false;
blockCommentNoIndent = false;
blockCommentNoBeautify = false;
previousLineProbationTab = false;
lineBeginsWithOpenBrace = false;
lineBeginsWithCloseBrace = false;
lineBeginsWithComma = false;
lineIsCommentOnly = false;
lineIsLineCommentOnly = false;
shouldIndentBracedLine = true;
isInSwitch = false;
foundPreCommandHeader = false;
foundPreCommandMacro = false;
isNonInStatementArray = false;
isSharpAccessor = false;
isSharpDelegate = false;
isInExternC = false;
isInBeautifySQL = false;
isInIndentableStruct = false;
isInIndentablePreproc = false;
inLineNumber = 0;
runInIndentContinuation = 0;
nonInStatementBrace = 0;
objCColonAlignSubsequent = 0;
}
/*
* initialize the vectors
*/
void ASBeautifier::initVectors()
{
if (fileType == beautifierFileType) // don't build unless necessary
return;
beautifierFileType = fileType;
headers->clear();
nonParenHeaders->clear();
assignmentOperators->clear();
nonAssignmentOperators->clear();
preBlockStatements->clear();
preCommandHeaders->clear();
indentableHeaders->clear();
ASResource::buildHeaders(headers, fileType, true);
ASResource::buildNonParenHeaders(nonParenHeaders, fileType, true);
ASResource::buildAssignmentOperators(assignmentOperators);
ASResource::buildNonAssignmentOperators(nonAssignmentOperators);
ASResource::buildPreBlockStatements(preBlockStatements, fileType);
ASResource::buildPreCommandHeaders(preCommandHeaders, fileType);
ASResource::buildIndentableHeaders(indentableHeaders);
}
/**
* set indentation style to C/C++.
*/
void ASBeautifier::setCStyle()
{
fileType = C_TYPE;
}
/**
* set indentation style to Java.
*/
void ASBeautifier::setJavaStyle()
{
fileType = JAVA_TYPE;
}
/**
* set indentation style to C#.
*/
void ASBeautifier::setSharpStyle()
{
fileType = SHARP_TYPE;
}
/**
* set mode manually set flag
*/
void ASBeautifier::setModeManuallySet(bool state)
{
isModeManuallySet = state;
}
/**
* set tabLength equal to indentLength.
* This is done when tabLength is not explicitly set by
* "indent=force-tab-x"
*
*/
void ASBeautifier::setDefaultTabLength()
{
tabLength = indentLength;
}
/**
* indent using a different tab setting for indent=force-tab
*
* @param length number of spaces per tab.
*/
void ASBeautifier::setForceTabXIndentation(int length)
{
// set tabLength instead of indentLength
indentString = "\t";
tabLength = length;
shouldForceTabIndentation = true;
}
/**
* indent using one tab per indentation
*/
void ASBeautifier::setTabIndentation(int length, bool forceTabs)
{
indentString = "\t";
indentLength = length;
shouldForceTabIndentation = forceTabs;
}
/**
* indent using a number of spaces per indentation.
*
* @param length number of spaces per indent.
*/
void ASBeautifier::setSpaceIndentation(int length)
{
indentString = string(length, ' ');
indentLength = length;
}
/**
* indent continuation lines using a number of indents.
*
* @param indent number of indents per line.
*/
void ASBeautifier::setContinuationIndentation(int indent)
{
continuationIndent = indent;
}
/**
* set the maximum indentation between two lines in a multi-line statement.
*
* @param max maximum indentation length.
*/
void ASBeautifier::setMaxContinuationIndentLength(int max)
{
maxContinuationIndent = max;
}
// retained for compatibility with release 2.06
// "MaxInStatementIndent" has been changed to "MaxContinuationIndent" in 3.0
// it is referenced only by the old "MaxInStatementIndent" options
void ASBeautifier::setMaxInStatementIndentLength(int max)
{
setMaxContinuationIndentLength(max);
}
/**
* set the minimum conditional indentation option.
*
* @param min minimal indentation option.
*/
void ASBeautifier::setMinConditionalIndentOption(int min)
{
minConditionalOption = min;
}
/**
* set minConditionalIndent from the minConditionalOption.
*/
void ASBeautifier::setMinConditionalIndentLength()
{
if (minConditionalOption == MINCOND_ZERO)
minConditionalIndent = 0;
else if (minConditionalOption == MINCOND_ONE)
minConditionalIndent = indentLength;
else if (minConditionalOption == MINCOND_ONEHALF)
minConditionalIndent = indentLength / 2;
// minConditionalOption = INDENT_TWO
else
minConditionalIndent = indentLength * 2;
}
/**
* set the state of the brace indent option. If true, braces will
* be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setBraceIndent(bool state)
{
braceIndent = state;
}
/**
* set the state of the brace indent VTK option. If true, braces will
* be indented one additional indent, except for the opening brace.
*
* @param state state of option.
*/
void ASBeautifier::setBraceIndentVtk(bool state)
{
// need to set both of these
setBraceIndent(state);
braceIndentVtk = state;
}
/**
* set the state of the block indentation option. If true, entire blocks
* will be indented one additional indent, similar to the GNU indent style.
*
* @param state state of option.
*/
void ASBeautifier::setBlockIndent(bool state)
{
blockIndent = state;
}
/**
* set the state of the class indentation option. If true, C++ class
* definitions will be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setClassIndent(bool state)
{
classIndent = state;
}
/**
* set the state of the modifier indentation option. If true, C++ class
* access modifiers will be indented one-half an indent.
*
* @param state state of option.
*/
void ASBeautifier::setModifierIndent(bool state)
{
modifierIndent = state;
}
/**
* set the state of the switch indentation option. If true, blocks of 'switch'
* statements will be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setSwitchIndent(bool state)
{
switchIndent = state;
}
/**
* set the state of the case indentation option. If true, lines of 'case'
* statements will be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setCaseIndent(bool state)
{
caseIndent = state;
}
/**
* set the state of the namespace indentation option.
* If true, blocks of 'namespace' statements will be indented one
* additional indent. Otherwise, NO indentation will be added.
*
* @param state state of option.
*/
void ASBeautifier::setNamespaceIndent(bool state)
{
namespaceIndent = state;
}
/**
* set the state of the indent after parens option.
*
* @param state state of option.
*/
void ASBeautifier::setAfterParenIndent(bool state)
{
shouldIndentAfterParen = state;
}
/**
* set the state of the label indentation option.
* If true, labels will be indented one indent LESS than the
* current indentation level.
* If false, labels will be flushed to the left with NO
* indent at all.
*
* @param state state of option.
*/
void ASBeautifier::setLabelIndent(bool state)
{
labelIndent = state;
}
/**
* set the state of the preprocessor indentation option.
* If true, multi-line #define statements will be indented.
*
* @param state state of option.
*/
void ASBeautifier::setPreprocDefineIndent(bool state)
{
shouldIndentPreprocDefine = state;
}
void ASBeautifier::setPreprocConditionalIndent(bool state)
{
shouldIndentPreprocConditional = state;
}
/**
* set the state of the empty line fill option.
* If true, empty lines will be filled with the whitespace.
* of their previous lines.
* If false, these lines will remain empty.
*
* @param state state of option.
*/
void ASBeautifier::setEmptyLineFill(bool state)
{
emptyLineFill = state;
}
void ASBeautifier::setAlignMethodColon(bool state)
{
shouldAlignMethodColon = state;
}
/**
* get the file type.
*/
int ASBeautifier::getFileType() const
{
return fileType;
}
/**
* get the number of spaces per indent
*
* @return value of indentLength option.
*/
int ASBeautifier::getIndentLength() const
{
return indentLength;
}
/**
* get the char used for indentation, space or tab
*
* @return the char used for indentation.
*/
string ASBeautifier::getIndentString() const
{
return indentString;
}
/**
* get mode manually set flag
*/
bool ASBeautifier::getModeManuallySet() const
{
return isModeManuallySet;
}
/**
* get the state of the force tab indentation option.
*
* @return state of force tab indentation.
*/
bool ASBeautifier::getForceTabIndentation() const
{
return shouldForceTabIndentation;
}
/**
* Get the state of the Objective-C align method colon option.
*
* @return state of shouldAlignMethodColon option.
*/
bool ASBeautifier::getAlignMethodColon() const
{
return shouldAlignMethodColon;
}
/**
* get the state of the block indentation option.
*
* @return state of blockIndent option.
*/
bool ASBeautifier::getBlockIndent() const
{
return blockIndent;
}
/**
* get the state of the brace indentation option.
*
* @return state of braceIndent option.
*/
bool ASBeautifier::getBraceIndent() const
{
return braceIndent;
}
/**
* Get the state of the namespace indentation option. If true, blocks
* of the 'namespace' statement will be indented one additional indent.
*
* @return state of namespaceIndent option.
*/
bool ASBeautifier::getNamespaceIndent() const
{
return namespaceIndent;
}
/**
* Get the state of the class indentation option. If true, blocks of
* the 'class' statement will be indented one additional indent.
*
* @return state of classIndent option.
*/
bool ASBeautifier::getClassIndent() const
{
return classIndent;
}
/**
* Get the state of the class access modifier indentation option.
* If true, the class access modifiers will be indented one-half indent.
*
* @return state of modifierIndent option.
*/
bool ASBeautifier::getModifierIndent() const
{
return modifierIndent;
}
/**
* get the state of the switch indentation option. If true, blocks of
* the 'switch' statement will be indented one additional indent.
*
* @return state of switchIndent option.
*/
bool ASBeautifier::getSwitchIndent() const
{
return switchIndent;
}
/**
* get the state of the case indentation option. If true, lines of 'case'
* statements will be indented one additional indent.
*
* @return state of caseIndent option.
*/
bool ASBeautifier::getCaseIndent() const
{
return caseIndent;
}
/**
* get the state of the empty line fill option.
* If true, empty lines will be filled with the whitespace.
* of their previous lines.
* If false, these lines will remain empty.
*
* @return state of emptyLineFill option.
*/
bool ASBeautifier::getEmptyLineFill() const
{
return emptyLineFill;
}
/**
* get the state of the preprocessor indentation option.
* If true, preprocessor "define" lines will be indented.
* If false, preprocessor "define" lines will be unchanged.
*
* @return state of shouldIndentPreprocDefine option.
*/
bool ASBeautifier::getPreprocDefineIndent() const
{
return shouldIndentPreprocDefine;
}
/**
* get the length of the tab indentation option.
*
* @return length of tab indent option.
*/
int ASBeautifier::getTabLength() const
{
return tabLength;
}
/**
* beautify a line of source code.
* every line of source code in a source code file should be sent
* one after the other to the beautify method.
*
* @return the indented line.
* @param originalLine the original unindented line.
*/
string ASBeautifier::beautify(const string& originalLine)
{
string line;
bool isInQuoteContinuation = isInVerbatimQuote || haveLineContinuationChar;
currentHeader = nullptr;
lastLineHeader = nullptr;
blockCommentNoBeautify = blockCommentNoIndent;
isInClass = false;
isInSwitch = false;
lineBeginsWithOpenBrace = false;
lineBeginsWithCloseBrace = false;
lineBeginsWithComma = false;
lineIsCommentOnly = false;
lineIsLineCommentOnly = false;
shouldIndentBracedLine = true;
isInAsmOneLine = false;
lineOpensWithLineComment = false;
lineOpensWithComment = false;
lineStartsInComment = isInComment;
previousLineProbationTab = false;
lineOpeningBlocksNum = 0;
lineClosingBlocksNum = 0;
if (isImmediatelyPostObjCMethodDefinition)
clearObjCMethodDefinitionAlignment();
if (isImmediatelyPostObjCMethodCall)
{
isImmediatelyPostObjCMethodCall = false;
isInObjCMethodCall = false;
objCColonAlignSubsequent = 0;
}
// handle and remove white spaces around the line:
// If not in comment, first find out size of white space before line,
// so that possible comments starting in the line continue in
// relation to the preliminary white-space.
if (isInQuoteContinuation)
{
// trim a single space added by ASFormatter, otherwise leave it alone
if (!(originalLine.length() == 1 && originalLine[0] == ' '))
line = originalLine;
}
else if (isInComment || isInBeautifySQL)
{
// trim the end of comment and SQL lines
line = originalLine;
size_t trimEnd = line.find_last_not_of(" \t");
if (trimEnd == string::npos)
trimEnd = 0;
else
trimEnd++;
if (trimEnd < line.length())
line.erase(trimEnd);
// does a brace open the line
size_t firstChar = line.find_first_not_of(" \t");
if (firstChar != string::npos)
{
if (line[firstChar] == '{')
lineBeginsWithOpenBrace = true;
else if (line[firstChar] == '}')
lineBeginsWithCloseBrace = true;
else if (line[firstChar] == ',')
lineBeginsWithComma = true;
}
}
else
{
line = trim(originalLine);
if (line.length() > 0)
{
if (line[0] == '{')
lineBeginsWithOpenBrace = true;
else if (line[0] == '}')
lineBeginsWithCloseBrace = true;
else if (line[0] == ',')
lineBeginsWithComma = true;
else if (line.compare(0, 2, "//") == 0)
lineIsLineCommentOnly = true;
else if (line.compare(0, 2, "/*") == 0)
{
if (line.find("*/", 2) != string::npos)
lineIsCommentOnly = true;
}
}
isInRunInComment = false;
size_t j = line.find_first_not_of(" \t{");
if (j != string::npos && line.compare(j, 2, "//") == 0)
lineOpensWithLineComment = true;
if (j != string::npos && line.compare(j, 2, "/*") == 0)
{
lineOpensWithComment = true;
size_t k = line.find_first_not_of(" \t");
if (k != string::npos && line.compare(k, 1, "{") == 0)
isInRunInComment = true;
}
}
// When indent is OFF the lines must still be processed by ASBeautifier.
// Otherwise the lines immediately following may not be indented correctly.
if ((lineIsLineCommentOnly || lineIsCommentOnly)
&& line.find("*INDENT-OFF*", 0) != string::npos)
isIndentModeOff = true;
if (line.length() == 0)
{
if (backslashEndsPrevLine)
{
backslashEndsPrevLine = false;
// check if this line ends a multi-line #define
// if so, remove the #define's cloned beautifier from the active
// beautifier stack and delete it.
if (isInDefineDefinition && !isInDefine)
{
isInDefineDefinition = false;
ASBeautifier* defineBeautifier = activeBeautifierStack->back();
activeBeautifierStack->pop_back();
delete defineBeautifier;
}
}
if (emptyLineFill && !isInQuoteContinuation)
{
if (isInIndentablePreprocBlock)
return preLineWS(preprocBlockIndent, 0);
if (!headerStack->empty() || isInEnum)
return preLineWS(prevFinalLineIndentCount, prevFinalLineSpaceIndentCount);
// must fall thru here
}
else
return line;
}
// handle preprocessor commands
if (isInIndentablePreprocBlock
&& line.length() > 0
&& line[0] != '#')
{
string indentedLine;
if (isInClassHeaderTab || isInClassInitializer)
{
// parsing is turned off in ASFormatter by indent-off
// the originalLine will probably never be returned here
indentedLine = preLineWS(prevFinalLineIndentCount, prevFinalLineSpaceIndentCount) + line;
return getIndentedLineReturn(indentedLine, originalLine);
}
indentedLine = preLineWS(preprocBlockIndent, 0) + line;
return getIndentedLineReturn(indentedLine, originalLine);
}
if (!isInComment
&& !isInQuoteContinuation
&& line.length() > 0
&& ((line[0] == '#' && !isIndentedPreprocessor(line, 0))
|| backslashEndsPrevLine))
{
if (line[0] == '#' && !isInDefine)
{
string preproc = extractPreprocessorStatement(line);
processPreprocessor(preproc, line);
if (isInIndentablePreprocBlock || isInIndentablePreproc)
{
string indentedLine;
if ((preproc.length() >= 2 && preproc.substr(0, 2) == "if")) // #if, #ifdef, #ifndef
{
indentedLine = preLineWS(preprocBlockIndent, 0) + line;
preprocBlockIndent += 1;
isInIndentablePreprocBlock = true;
}
else if (preproc == "else" || preproc == "elif")
{
indentedLine = preLineWS(preprocBlockIndent - 1, 0) + line;
}
else if (preproc == "endif")
{
preprocBlockIndent -= 1;
indentedLine = preLineWS(preprocBlockIndent, 0) + line;
if (preprocBlockIndent == 0)
isInIndentablePreprocBlock = false;
}
else
indentedLine = preLineWS(preprocBlockIndent, 0) + line;
return getIndentedLineReturn(indentedLine, originalLine);
}
if (shouldIndentPreprocConditional && preproc.length() > 0)
{
string indentedLine;
if (preproc.length() >= 2 && preproc.substr(0, 2) == "if") // #if, #ifdef, #ifndef
{
pair<int, int> entry; // indentCount, spaceIndentCount
if (!isInDefine && activeBeautifierStack != nullptr && !activeBeautifierStack->empty())
entry = activeBeautifierStack->back()->computePreprocessorIndent();
else
entry = computePreprocessorIndent();
preprocIndentStack->emplace_back(entry);
indentedLine = preLineWS(preprocIndentStack->back().first,
preprocIndentStack->back().second) + line;
return getIndentedLineReturn(indentedLine, originalLine);
}
if (preproc == "else" || preproc == "elif")
{
if (!preprocIndentStack->empty()) // if no entry don't indent
{
indentedLine = preLineWS(preprocIndentStack->back().first,
preprocIndentStack->back().second) + line;
return getIndentedLineReturn(indentedLine, originalLine);
}
}
else if (preproc == "endif")
{
if (!preprocIndentStack->empty()) // if no entry don't indent
{
indentedLine = preLineWS(preprocIndentStack->back().first,
preprocIndentStack->back().second) + line;
preprocIndentStack->pop_back();
return getIndentedLineReturn(indentedLine, originalLine);
}
}
}
}
// check if the last char is a backslash
if (line.length() > 0)
backslashEndsPrevLine = (line[line.length() - 1] == '\\');
// comments within the definition line can be continued without the backslash
if (isInPreprocessorUnterminatedComment(line))
backslashEndsPrevLine = true;
// check if this line ends a multi-line #define
// if so, use the #define's cloned beautifier for the line's indentation
// and then remove it from the active beautifier stack and delete it.
if (!backslashEndsPrevLine && isInDefineDefinition && !isInDefine)
{
isInDefineDefinition = false;
ASBeautifier* defineBeautifier = activeBeautifierStack->back();
activeBeautifierStack->pop_back();
string indentedLine = defineBeautifier->beautify(line);
delete defineBeautifier;
return getIndentedLineReturn(indentedLine, originalLine);
}
// unless this is a multi-line #define, return this precompiler line as is.
if (!isInDefine && !isInDefineDefinition)
return originalLine;
}
// if there exists any worker beautifier in the activeBeautifierStack,
// then use it instead of me to indent the current line.
// variables set by ASFormatter must be updated.
if (!isInDefine && activeBeautifierStack != nullptr && !activeBeautifierStack->empty())
{
activeBeautifierStack->back()->inLineNumber = inLineNumber;
activeBeautifierStack->back()->runInIndentContinuation = runInIndentContinuation;
activeBeautifierStack->back()->nonInStatementBrace = nonInStatementBrace;
activeBeautifierStack->back()->objCColonAlignSubsequent = objCColonAlignSubsequent;
activeBeautifierStack->back()->lineCommentNoBeautify = lineCommentNoBeautify;
activeBeautifierStack->back()->isElseHeaderIndent = isElseHeaderIndent;
activeBeautifierStack->back()->isCaseHeaderCommentIndent = isCaseHeaderCommentIndent;
activeBeautifierStack->back()->isNonInStatementArray = isNonInStatementArray;
activeBeautifierStack->back()->isSharpAccessor = isSharpAccessor;
activeBeautifierStack->back()->isSharpDelegate = isSharpDelegate;
activeBeautifierStack->back()->isInExternC = isInExternC;
activeBeautifierStack->back()->isInBeautifySQL = isInBeautifySQL;
activeBeautifierStack->back()->isInIndentableStruct = isInIndentableStruct;
activeBeautifierStack->back()->isInIndentablePreproc = isInIndentablePreproc;
// must return originalLine not the trimmed line
return activeBeautifierStack->back()->beautify(originalLine);
}
// Flag an indented header in case this line is a one-line block.
// The header in the header stack will be deleted by a one-line block.
bool isInExtraHeaderIndent = false;
if (!headerStack->empty()
&& lineBeginsWithOpenBrace
&& (headerStack->back() != &AS_OPEN_BRACE
|| probationHeader != nullptr))
isInExtraHeaderIndent = true;
size_t iPrelim = headerStack->size();
// calculate preliminary indentation based on headerStack and data from past lines
computePreliminaryIndentation();
// parse characters in the current line.
parseCurrentLine(line);
// handle special cases of indentation
adjustParsedLineIndentation(iPrelim, isInExtraHeaderIndent);
if (isInObjCMethodDefinition)
adjustObjCMethodDefinitionIndentation(line);
if (isInObjCMethodCall)
adjustObjCMethodCallIndentation(line);
if (isInDefine)
{
if (line.length() > 0 && line[0] == '#')
{
// the 'define' does not have to be attached to the '#'
string preproc = trim(line.substr(1));
if (preproc.compare(0, 6, "define") == 0)
{
if (!continuationIndentStack->empty()
&& continuationIndentStack->back() > 0)
{
defineIndentCount = indentCount;
}
else
{
defineIndentCount = indentCount - 1;
--indentCount;
}
}
}
indentCount -= defineIndentCount;
}
if (indentCount < 0)
indentCount = 0;
if (lineCommentNoBeautify || blockCommentNoBeautify || isInQuoteContinuation)
indentCount = spaceIndentCount = 0;
// finally, insert indentations into beginning of line
string indentedLine = preLineWS(indentCount, spaceIndentCount) + line;
indentedLine = getIndentedLineReturn(indentedLine, originalLine);
prevFinalLineSpaceIndentCount = spaceIndentCount;
prevFinalLineIndentCount = indentCount;
if (lastLineHeader != nullptr)
previousLastLineHeader = lastLineHeader;
if ((lineIsLineCommentOnly || lineIsCommentOnly)
&& line.find("*INDENT-ON*", 0) != string::npos)
isIndentModeOff = false;
return indentedLine;
}
const string& ASBeautifier::getIndentedLineReturn(const string& newLine, const string& originalLine) const
{
if (isIndentModeOff)
return originalLine;
return newLine;
}
string ASBeautifier::preLineWS(int lineIndentCount, int lineSpaceIndentCount) const
{
if (shouldForceTabIndentation)
{
if (tabLength != indentLength)
{
// adjust for different tab length
int indentCountOrig = lineIndentCount;
int spaceIndentCountOrig = lineSpaceIndentCount;
lineIndentCount = ((indentCountOrig * indentLength) + spaceIndentCountOrig) / tabLength;
lineSpaceIndentCount = ((indentCountOrig * indentLength) + spaceIndentCountOrig) % tabLength;
}
else
{
lineIndentCount += lineSpaceIndentCount / indentLength;
lineSpaceIndentCount = lineSpaceIndentCount % indentLength;
}
}
string ws;
for (int i = 0; i < lineIndentCount; i++)
ws += indentString;
while ((lineSpaceIndentCount--) > 0)
ws += string(" ");
return ws;
}
/**
* register a continuation indent.
*/
void ASBeautifier::registerContinuationIndent(const string& line, int i, int spaceIndentCount_,
int tabIncrementIn, int minIndent, bool updateParenStack)
{
int remainingCharNum = line.length() - i;
int nextNonWSChar = getNextProgramCharDistance(line, i);
// if indent is around the last char in the line OR indent-after-paren is requested,
// indent with the continuation indent
if (nextNonWSChar == remainingCharNum || shouldIndentAfterParen)
{
int previousIndent = spaceIndentCount_;
if (!continuationIndentStack->empty())
previousIndent = continuationIndentStack->back();
int currIndent = continuationIndent * indentLength + previousIndent;
if (currIndent > maxContinuationIndent && line[i] != '{')
currIndent = indentLength * 2 + spaceIndentCount_;
continuationIndentStack->emplace_back(currIndent);
if (updateParenStack)
parenIndentStack->emplace_back(previousIndent);
return;
}
if (updateParenStack)
parenIndentStack->emplace_back(i + spaceIndentCount_ - runInIndentContinuation);
int tabIncrement = tabIncrementIn;
// check for following tabs
for (int j = i + 1; j < (i + nextNonWSChar); j++)
{
if (line[j] == '\t')
tabIncrement += convertTabToSpaces(j, tabIncrement);
}
int continuationIndentCount = i + nextNonWSChar + spaceIndentCount_ + tabIncrement;
// check for run-in statement
if (i > 0 && line[0] == '{')
continuationIndentCount -= indentLength;
if (continuationIndentCount < minIndent)
continuationIndentCount = minIndent + spaceIndentCount_;
// this is not done for an in-statement array
if (continuationIndentCount > maxContinuationIndent
&& !(prevNonLegalCh == '=' && currentNonLegalCh == '{'))
continuationIndentCount = indentLength * 2 + spaceIndentCount_;
if (!continuationIndentStack->empty()
&& continuationIndentCount < continuationIndentStack->back())
continuationIndentCount = continuationIndentStack->back();
// the block opener is not indented for a NonInStatementArray
if ((isNonInStatementArray && line[i] == '{')
&& !isInEnum && !braceBlockStateStack->empty() && braceBlockStateStack->back())
continuationIndentCount = 0;
continuationIndentStack->emplace_back(continuationIndentCount);
}
/**
* Register a continuation indent for a class header or a class initializer colon.
*/
void ASBeautifier::registerContinuationIndentColon(const string& line, int i, int tabIncrementIn)
{
assert(line[i] == ':');
assert(isInClassInitializer || isInClassHeaderTab);
// register indent at first word after the colon
size_t firstChar = line.find_first_not_of(" \t");
if (firstChar == (size_t) i) // firstChar is ':'
{
size_t firstWord = line.find_first_not_of(" \t", firstChar + 1);
if (firstWord != string::npos)
{
int continuationIndentCount = firstWord + spaceIndentCount + tabIncrementIn;
continuationIndentStack->emplace_back(continuationIndentCount);
isContinuation = true;
}
}
}
/**
* Compute indentation for a preprocessor #if statement.
* This may be called for the activeBeautiferStack
* instead of the active ASBeautifier object.
*/
pair<int, int> ASBeautifier::computePreprocessorIndent()
{
computePreliminaryIndentation();
pair<int, int> entry(indentCount, spaceIndentCount);
if (!headerStack->empty()
&& entry.first > 0
&& (headerStack->back() == &AS_IF
|| headerStack->back() == &AS_ELSE
|| headerStack->back() == &AS_FOR
|| headerStack->back() == &AS_WHILE))
--entry.first;
return entry;
}
/**
* get distance to the next non-white space, non-comment character in the line.
* if no such character exists, return the length remaining to the end of the line.
*/
int ASBeautifier::getNextProgramCharDistance(const string& line, int i) const
{
bool inComment = false;
int remainingCharNum = line.length() - i;
int charDistance;
char ch;
for (charDistance = 1; charDistance < remainingCharNum; charDistance++)
{
ch = line[i + charDistance];
if (inComment)
{
if (line.compare(i + charDistance, 2, "*/") == 0)
{
charDistance++;
inComment = false;
}
continue;
}
else if (isWhiteSpace(ch))
continue;
else if (ch == '/')
{
if (line.compare(i + charDistance, 2, "//") == 0)
return remainingCharNum;
if (line.compare(i + charDistance, 2, "/*") == 0)
{
charDistance++;
inComment = true;
}
}
else
return charDistance;
}
return charDistance;
}
/**
* find the index number of a string element in a container of strings
*
* @return the index number of element in the container. -1 if element not found.
* @param container a vector of strings.
* @param element the element to find .
*/
int ASBeautifier::indexOf(const vector<const string*>& container, const string* element) const
{
vector<const string*>::const_iterator where;
where = find(container.begin(), container.end(), element);
if (where == container.end())
return -1;
return (int) (where - container.begin());
}
/**
* convert tabs to spaces.
* i is the position of the character to convert to spaces.
* tabIncrementIn is the increment that must be added for tab indent characters
* to get the correct column for the current tab.
*/
int ASBeautifier::convertTabToSpaces(int i, int tabIncrementIn) const
{
int tabToSpacesAdjustment = indentLength - 1 - ((tabIncrementIn + i) % indentLength);
return tabToSpacesAdjustment;
}
/**
* trim removes the white space surrounding a line.
*
* @return the trimmed line.
* @param str the line to trim.
*/
string ASBeautifier::trim(const string& str) const
{
int start = 0;
int end = str.length() - 1;
while (start < end && isWhiteSpace(str[start]))
start++;
while (start <= end && isWhiteSpace(str[end]))
end--;
// don't trim if it ends in a continuation
if (end > -1 && str[end] == '\\')
end = str.length() - 1;
string returnStr(str, start, end + 1 - start);
return returnStr;
}
/**
* rtrim removes the white space from the end of a line.
*
* @return the trimmed line.
* @param str the line to trim.
*/
string ASBeautifier::rtrim(const string& str) const
{
size_t len = str.length();
size_t end = str.find_last_not_of(" \t");
if (end == string::npos
|| end == len - 1)
return str;
string returnStr(str, 0, end + 1);
return returnStr;
}
/**
* Copy tempStacks for the copy constructor.
* The value of the vectors must also be copied.
*/
vector<vector<const string*>*>* ASBeautifier::copyTempStacks(const ASBeautifier& other) const
{
vector<vector<const string*>*>* tempStacksNew = new vector<vector<const string*>*>;
vector<vector<const string*>*>::iterator iter;
for (iter = other.tempStacks->begin();
iter != other.tempStacks->end();
++iter)
{
vector<const string*>* newVec = new vector<const string*>;
*newVec = **iter;
tempStacksNew->emplace_back(newVec);
}
return tempStacksNew;
}
/**
* delete a member vectors to eliminate memory leak reporting
*/
void ASBeautifier::deleteBeautifierVectors()
{
beautifierFileType = 9; // reset to an invalid type
delete headers;
delete nonParenHeaders;
delete preBlockStatements;
delete preCommandHeaders;
delete assignmentOperators;
delete nonAssignmentOperators;
delete indentableHeaders;
}
/**
* delete a vector object
* T is the type of vector
* used for all vectors except tempStacks
*/
template<typename T>
void ASBeautifier::deleteContainer(T& container)
{
if (container != nullptr)
{
container->clear();
delete (container);
container = nullptr;
}
}
/**
* Delete the ASBeautifier vector object.
* This is a vector of pointers to ASBeautifier objects allocated with the 'new' operator.
* Therefore the ASBeautifier objects have to be deleted in addition to the
* ASBeautifier pointer entries.
*/
void ASBeautifier::deleteBeautifierContainer(vector<ASBeautifier*>*& container)
{
if (container != nullptr)
{
vector<ASBeautifier*>::iterator iter = container->begin();
while (iter < container->end())
{
delete *iter;
++iter;
}
container->clear();
delete (container);
container = nullptr;
}
}
/**
* Delete the tempStacks vector object.
* The tempStacks is a vector of pointers to strings allocated with the 'new' operator.
* Therefore the strings have to be deleted in addition to the tempStacks entries.
*/
void ASBeautifier::deleteTempStacksContainer(vector<vector<const string*>*>*& container)
{
if (container != nullptr)
{
vector<vector<const string*>*>::iterator iter = container->begin();
while (iter < container->end())
{
delete *iter;
++iter;
}
container->clear();
delete (container);
container = nullptr;
}
}
/**
* initialize a vector object
* T is the type of vector used for all vectors
*/
template<typename T>
void ASBeautifier::initContainer(T& container, T value)
{
// since the ASFormatter object is never deleted,
// the existing vectors must be deleted before creating new ones
if (container != nullptr)
deleteContainer(container);
container = value;
}
/**
* Initialize the tempStacks vector object.
* The tempStacks is a vector of pointers to strings allocated with the 'new' operator.
* Any residual entries are deleted before the vector is initialized.
*/
void ASBeautifier::initTempStacksContainer(vector<vector<const string*>*>*& container,
vector<vector<const string*>*>* value)
{
if (container != nullptr)
deleteTempStacksContainer(container);
container = value;
}
/**
* Determine if an assignment statement ends with a comma
* that is not in a function argument. It ends with a
* comma if a comma is the last char on the line.
*
* @return true if line ends with a comma, otherwise false.
*/
bool ASBeautifier::statementEndsWithComma(const string& line, int index) const
{
assert(line[index] == '=');
bool isInComment_ = false;
bool isInQuote_ = false;
int parenCount = 0;
size_t lineLength = line.length();
size_t i = 0;
char quoteChar_ = ' ';
for (i = index + 1; i < lineLength; ++i)
{
char ch = line[i];
if (isInComment_)
{
if (line.compare(i, 2, "*/") == 0)
{
isInComment_ = false;
++i;
}
continue;
}
if (ch == '\\')
{
++i;
continue;
}
if (isInQuote_)
{
if (ch == quoteChar_)
isInQuote_ = false;
continue;
}
if (ch == '"'
|| (ch == '\'' && !isDigitSeparator(line, i)))
{
isInQuote_ = true;
quoteChar_ = ch;
continue;
}
if (line.compare(i, 2, "//") == 0)
break;
if (line.compare(i, 2, "/*") == 0)
{
if (isLineEndComment(line, i))
break;
else
{
isInComment_ = true;
++i;
continue;
}
}
if (ch == '(')
parenCount++;
if (ch == ')')
parenCount--;
}
if (isInComment_
|| isInQuote_
|| parenCount > 0)
return false;
size_t lastChar = line.find_last_not_of(" \t", i - 1);
if (lastChar == string::npos || line[lastChar] != ',')
return false;
return true;
}
/**
* check if current comment is a line-end comment
*
* @return is before a line-end comment.
*/
bool ASBeautifier::isLineEndComment(const string& line, int startPos) const
{
assert(line.compare(startPos, 2, "/*") == 0);
// comment must be closed on this line with nothing after it
size_t endNum = line.find("*/", startPos + 2);
if (endNum != string::npos)
{
size_t nextChar = line.find_first_not_of(" \t", endNum + 2);
if (nextChar == string::npos)
return true;
}
return false;
}
/**
* get the previous word index for an assignment operator
*
* @return is the index to the previous word (the in statement indent).
*/
int ASBeautifier::getContinuationIndentAssign(const string& line, size_t currPos) const
{
assert(line[currPos] == '=');
if (currPos == 0)
return 0;
// get the last legal word (may be a number)
size_t end = line.find_last_not_of(" \t", currPos - 1);
if (end == string::npos || !isLegalNameChar(line[end]))
return 0;
int start; // start of the previous word
for (start = end; start > -1; start--)
{
if (!isLegalNameChar(line[start]) || line[start] == '.')
break;
}
start++;
return start;
}
/**
* get the instatement indent for a comma
*
* @return is the indent to the second word on the line (the in statement indent).
*/
int ASBeautifier::getContinuationIndentComma(const string& line, size_t currPos) const
{
assert(line[currPos] == ',');
// get first word on a line
size_t indent = line.find_first_not_of(" \t");
if (indent == string::npos || !isLegalNameChar(line[indent]))
return 0;
// bypass first word
for (; indent < currPos; indent++)
{
if (!isLegalNameChar(line[indent]))
break;
}
indent++;
if (indent >= currPos || indent < 4)
return 0;
// point to second word or assignment operator
indent = line.find_first_not_of(" \t", indent);
if (indent == string::npos || indent >= currPos)
return 0;
return indent;
}
/**
* get the next word on a line
* the argument 'currPos' must point to the current position.
*
* @return is the next word or an empty string if none found.
*/
string ASBeautifier::getNextWord(const string& line, size_t currPos) const
{
size_t lineLength = line.length();
// get the last legal word (may be a number)
if (currPos == lineLength - 1)
return string();
size_t start = line.find_first_not_of(" \t", currPos + 1);
if (start == string::npos || !isLegalNameChar(line[start]))
return string();
size_t end; // end of the current word
for (end = start + 1; end <= lineLength; end++)
{
if (!isLegalNameChar(line[end]) || line[end] == '.')
break;
}
return line.substr(start, end - start);
}
/**
* Check if a preprocessor directive is always indented.
* C# "region" and "endregion" are always indented.
* C/C++ "pragma omp" is always indented.
*
* @return is true or false.
*/
bool ASBeautifier::isIndentedPreprocessor(const string& line, size_t currPos) const
{
assert(line[0] == '#');
string nextWord = getNextWord(line, currPos);
if (nextWord == "region" || nextWord == "endregion")
return true;
// is it #pragma omp
if (nextWord == "pragma")
{
// find pragma
size_t start = line.find("pragma");
if (start == string::npos || !isLegalNameChar(line[start]))
return false;
// bypass pragma
for (; start < line.length(); start++)
{
if (!isLegalNameChar(line[start]))
break;
}
start++;
if (start >= line.length())
return false;
// point to start of second word
start = line.find_first_not_of(" \t", start);
if (start == string::npos)
return false;
// point to end of second word
size_t end;
for (end = start; end < line.length(); end++)
{
if (!isLegalNameChar(line[end]))
break;
}
// check for "pragma omp"
string word = line.substr(start, end - start);
if (word == "omp" || word == "region" || word == "endregion")
return true;
}
return false;
}
/**
* Check if a preprocessor directive is checking for __cplusplus defined.
*
* @return is true or false.
*/
bool ASBeautifier::isPreprocessorConditionalCplusplus(const string& line) const
{
string preproc = trim(line.substr(1));
if (preproc.compare(0, 5, "ifdef") == 0 && getNextWord(preproc, 4) == "__cplusplus")
return true;
if (preproc.compare(0, 2, "if") == 0)
{
// check for " #if defined(__cplusplus)"
size_t charNum = 2;
charNum = preproc.find_first_not_of(" \t", charNum);
if (charNum != string::npos && preproc.compare(charNum, 7, "defined") == 0)
{
charNum += 7;
charNum = preproc.find_first_not_of(" \t", charNum);
if (preproc.compare(charNum, 1, "(") == 0)
{
++charNum;
charNum = preproc.find_first_not_of(" \t", charNum);
if (preproc.compare(charNum, 11, "__cplusplus") == 0)
return true;
}
}
}
return false;
}
/**
* Check if a preprocessor definition contains an unterminated comment.
* Comments within a preprocessor definition can be continued without the backslash.
*
* @return is true or false.
*/
bool ASBeautifier::isInPreprocessorUnterminatedComment(const string& line)
{
if (!isInPreprocessorComment)
{
size_t startPos = line.find("/*");
if (startPos == string::npos)
return false;
}
size_t endNum = line.find("*/");
if (endNum != string::npos)
{
isInPreprocessorComment = false;
return false;
}
isInPreprocessorComment = true;
return true;
}
void ASBeautifier::popLastContinuationIndent()
{
assert(!continuationIndentStackSizeStack->empty());
int previousIndentStackSize = continuationIndentStackSizeStack->back();
if (continuationIndentStackSizeStack->size() > 1)
continuationIndentStackSizeStack->pop_back();
while (previousIndentStackSize < (int) continuationIndentStack->size())
continuationIndentStack->pop_back();
}
// for unit testing
int ASBeautifier::getBeautifierFileType() const
{ return beautifierFileType; }
/**
* Process preprocessor statements and update the beautifier stacks.
*/
void ASBeautifier::processPreprocessor(const string& preproc, const string& line)
{
// When finding a multi-lined #define statement, the original beautifier
// 1. sets its isInDefineDefinition flag
// 2. clones a new beautifier that will be used for the actual indentation
// of the #define. This clone is put into the activeBeautifierStack in order
// to be called for the actual indentation.
// The original beautifier will have isInDefineDefinition = true, isInDefine = false
// The cloned beautifier will have isInDefineDefinition = true, isInDefine = true
if (shouldIndentPreprocDefine && preproc == "define" && line[line.length() - 1] == '\\')
{
if (!isInDefineDefinition)
{
// this is the original beautifier
isInDefineDefinition = true;
// push a new beautifier into the active stack
// this beautifier will be used for the indentation of this define
ASBeautifier* defineBeautifier = new ASBeautifier(*this);
activeBeautifierStack->emplace_back(defineBeautifier);
}
else
{
// the is the cloned beautifier that is in charge of indenting the #define.
isInDefine = true;
}
}
else if (preproc.length() >= 2 && preproc.substr(0, 2) == "if")
{
if (isPreprocessorConditionalCplusplus(line) && !g_preprocessorCppExternCBrace)
g_preprocessorCppExternCBrace = 1;
// push a new beautifier into the stack
waitingBeautifierStackLengthStack->push_back(waitingBeautifierStack->size());
activeBeautifierStackLengthStack->push_back(activeBeautifierStack->size());
if (activeBeautifierStackLengthStack->back() == 0)
waitingBeautifierStack->emplace_back(new ASBeautifier(*this));
else
waitingBeautifierStack->emplace_back(new ASBeautifier(*activeBeautifierStack->back()));
}
else if (preproc == "else")
{
if ((waitingBeautifierStack != nullptr) && !waitingBeautifierStack->empty())
{
// MOVE current waiting beautifier to active stack.
activeBeautifierStack->emplace_back(waitingBeautifierStack->back());
waitingBeautifierStack->pop_back();
}
}
else if (preproc == "elif")
{
if ((waitingBeautifierStack != nullptr) && !waitingBeautifierStack->empty())
{
// append a COPY current waiting beautifier to active stack, WITHOUT deleting the original.
activeBeautifierStack->emplace_back(new ASBeautifier(*(waitingBeautifierStack->back())));
}
}
else if (preproc == "endif")
{
int stackLength = 0;
ASBeautifier* beautifier = nullptr;
if (waitingBeautifierStackLengthStack != nullptr && !waitingBeautifierStackLengthStack->empty())
{
stackLength = waitingBeautifierStackLengthStack->back();
waitingBeautifierStackLengthStack->pop_back();
while ((int) waitingBeautifierStack->size() > stackLength)
{
beautifier = waitingBeautifierStack->back();
waitingBeautifierStack->pop_back();
delete beautifier;
}
}
if (!activeBeautifierStackLengthStack->empty())
{
stackLength = activeBeautifierStackLengthStack->back();
activeBeautifierStackLengthStack->pop_back();
while ((int) activeBeautifierStack->size() > stackLength)
{
beautifier = activeBeautifierStack->back();
activeBeautifierStack->pop_back();
delete beautifier;
}
}
}
}
// Compute the preliminary indentation based on data in the headerStack
// and data from previous lines.
// Update the class variable indentCount.
void ASBeautifier::computePreliminaryIndentation()
{
indentCount = 0;
spaceIndentCount = 0;
isInClassHeaderTab = false;
if (isInObjCMethodDefinition && !continuationIndentStack->empty())
spaceIndentObjCMethodAlignment = continuationIndentStack->back();
if (!continuationIndentStack->empty())
spaceIndentCount = continuationIndentStack->back();
for (size_t i = 0; i < headerStack->size(); i++)
{
isInClass = false;
if (blockIndent)
{
// do NOT indent opening block for these headers
if (!((*headerStack)[i] == &AS_NAMESPACE
|| (*headerStack)[i] == &AS_MODULE
|| (*headerStack)[i] == &AS_CLASS
|| (*headerStack)[i] == &AS_STRUCT
|| (*headerStack)[i] == &AS_UNION
|| (*headerStack)[i] == &AS_INTERFACE
|| (*headerStack)[i] == &AS_THROWS
|| (*headerStack)[i] == &AS_STATIC))
++indentCount;
}
else if (!(i > 0 && (*headerStack)[i - 1] != &AS_OPEN_BRACE
&& (*headerStack)[i] == &AS_OPEN_BRACE))
++indentCount;
if (!isJavaStyle() && !namespaceIndent && i > 0
&& ((*headerStack)[i - 1] == &AS_NAMESPACE
|| (*headerStack)[i - 1] == &AS_MODULE)
&& (*headerStack)[i] == &AS_OPEN_BRACE)
--indentCount;
if (isCStyle() && i >= 1
&& (*headerStack)[i - 1] == &AS_CLASS
&& (*headerStack)[i] == &AS_OPEN_BRACE)
{
if (classIndent)
++indentCount;
isInClass = true;
}
// is the switchIndent option is on, indent switch statements an additional indent.
else if (switchIndent && i > 1
&& (*headerStack)[i - 1] == &AS_SWITCH
&& (*headerStack)[i] == &AS_OPEN_BRACE)
{
++indentCount;
isInSwitch = true;
}
} // end of for loop
if (isInClassHeader)
{
if (!isJavaStyle())
isInClassHeaderTab = true;
if (lineOpensWithLineComment || lineStartsInComment || lineOpensWithComment)
{
if (!lineBeginsWithOpenBrace)
--indentCount;
if (!continuationIndentStack->empty())
spaceIndentCount -= continuationIndentStack->back();
}
else if (blockIndent)
{
if (!lineBeginsWithOpenBrace)
++indentCount;
}
}
if (isInClassInitializer || isInEnumTypeID)
{
indentCount += classInitializerIndents;
}
if (isInEnum && lineBeginsWithComma && !continuationIndentStack->empty())
{
// unregister '=' indent from the previous line
continuationIndentStack->pop_back();
isContinuation = false;
spaceIndentCount = 0;
}
// Objective-C interface continuation line
if (isInObjCInterface)
++indentCount;
// unindent a class closing brace...
if (!lineStartsInComment
&& isCStyle()
&& isInClass
&& classIndent
&& headerStack->size() >= 2
&& (*headerStack)[headerStack->size() - 2] == &AS_CLASS
&& (*headerStack)[headerStack->size() - 1] == &AS_OPEN_BRACE
&& lineBeginsWithCloseBrace
&& braceBlockStateStack->back())
--indentCount;
// unindent an indented switch closing brace...
else if (!lineStartsInComment
&& isInSwitch
&& switchIndent
&& headerStack->size() >= 2
&& (*headerStack)[headerStack->size() - 2] == &AS_SWITCH
&& (*headerStack)[headerStack->size() - 1] == &AS_OPEN_BRACE
&& lineBeginsWithCloseBrace)
--indentCount;
// handle special case of run-in comment in an indented class statement
if (isInClass
&& classIndent
&& isInRunInComment
&& !lineOpensWithComment
&& headerStack->size() > 1
&& (*headerStack)[headerStack->size() - 2] == &AS_CLASS)
--indentCount;
if (isInConditional)
--indentCount;
if (g_preprocessorCppExternCBrace >= 4)
--indentCount;
}
void ASBeautifier::adjustParsedLineIndentation(size_t iPrelim, bool isInExtraHeaderIndent)
{
if (lineStartsInComment)
return;
// unindent a one-line statement in a header indent
if (!blockIndent
&& lineBeginsWithOpenBrace
&& headerStack->size() < iPrelim
&& isInExtraHeaderIndent
&& (lineOpeningBlocksNum > 0 && lineOpeningBlocksNum <= lineClosingBlocksNum)
&& shouldIndentBracedLine)
--indentCount;
/*
* if '{' doesn't follow an immediately previous '{' in the headerStack
* (but rather another header such as "for" or "if", then unindent it
* by one indentation relative to its block.
*/
else if (!blockIndent
&& lineBeginsWithOpenBrace
&& !(lineOpeningBlocksNum > 0 && lineOpeningBlocksNum <= lineClosingBlocksNum)
&& (headerStack->size() > 1 && (*headerStack)[headerStack->size() - 2] != &AS_OPEN_BRACE)
&& shouldIndentBracedLine)
--indentCount;
// must check one less in headerStack if more than one header on a line (allow-addins)...
else if (headerStack->size() > iPrelim + 1
&& !blockIndent
&& lineBeginsWithOpenBrace
&& !(lineOpeningBlocksNum > 0 && lineOpeningBlocksNum <= lineClosingBlocksNum)
&& (headerStack->size() > 2 && (*headerStack)[headerStack->size() - 3] != &AS_OPEN_BRACE)
&& shouldIndentBracedLine)
--indentCount;
// unindent a closing brace...
else if (lineBeginsWithCloseBrace
&& shouldIndentBracedLine)
--indentCount;
// correctly indent one-line-blocks...
else if (lineOpeningBlocksNum > 0
&& lineOpeningBlocksNum == lineClosingBlocksNum
&& previousLineProbationTab)
--indentCount;
if (indentCount < 0)
indentCount = 0;
// take care of extra brace indentation option...
if (!lineStartsInComment
&& braceIndent
&& shouldIndentBracedLine
&& (lineBeginsWithOpenBrace || lineBeginsWithCloseBrace))
{
if (!braceIndentVtk)
++indentCount;
else
{
// determine if a style VTK brace is indented
bool haveUnindentedBrace = false;
for (size_t i = 0; i < headerStack->size(); i++)
{
if (((*headerStack)[i] == &AS_NAMESPACE
|| (*headerStack)[i] == &AS_MODULE
|| (*headerStack)[i] == &AS_CLASS
|| (*headerStack)[i] == &AS_STRUCT)
&& i + 1 < headerStack->size()
&& (*headerStack)[i + 1] == &AS_OPEN_BRACE)
i++;
else if (lineBeginsWithOpenBrace)
{
// don't double count the current brace
if (i + 1 < headerStack->size()
&& (*headerStack)[i] == &AS_OPEN_BRACE)
haveUnindentedBrace = true;
}
else if ((*headerStack)[i] == &AS_OPEN_BRACE)
haveUnindentedBrace = true;
} // end of for loop
if (haveUnindentedBrace)
++indentCount;
}
}
}
/**
* Compute indentCount adjustment when in a series of else-if statements
* and shouldBreakElseIfs is requested.
* It increments by one for each 'else' in the tempStack.
*/
int ASBeautifier::adjustIndentCountForBreakElseIfComments() const
{
assert(isElseHeaderIndent && !tempStacks->empty());
int indentCountIncrement = 0;
vector<const string*>* lastTempStack = tempStacks->back();
if (lastTempStack != nullptr)
{
for (size_t i = 0; i < lastTempStack->size(); i++)
{
if (*lastTempStack->at(i) == AS_ELSE)
indentCountIncrement++;
}
}
return indentCountIncrement;
}
/**
* Extract a preprocessor statement without the #.
* If a error occurs an empty string is returned.
*/
string ASBeautifier::extractPreprocessorStatement(const string& line) const
{
string preproc;
size_t start = line.find_first_not_of("#/ \t");
if (start == string::npos)
return preproc;
size_t end = line.find_first_of("/ \t", start);
if (end == string::npos)
end = line.length();
preproc = line.substr(start, end - start);
return preproc;
}
void ASBeautifier::adjustObjCMethodDefinitionIndentation(const string& line_)
{
// register indent for Objective-C continuation line
if (line_.length() > 0
&& (line_[0] == '-' || line_[0] == '+'))
{
if (shouldAlignMethodColon && objCColonAlignSubsequent != -1)
{
string convertedLine = getIndentedSpaceEquivalent(line_);
colonIndentObjCMethodAlignment = convertedLine.find(':');
int objCColonAlignSubsequentIndent = objCColonAlignSubsequent + indentLength;
if (objCColonAlignSubsequentIndent > colonIndentObjCMethodAlignment)
colonIndentObjCMethodAlignment = objCColonAlignSubsequentIndent;
}
else if (continuationIndentStack->empty()
|| continuationIndentStack->back() == 0)
{
continuationIndentStack->emplace_back(indentLength);
isContinuation = true;
}
}
// set indent for last definition line
else if (!lineBeginsWithOpenBrace)
{
if (shouldAlignMethodColon)
spaceIndentCount = computeObjCColonAlignment(line_, colonIndentObjCMethodAlignment);
else if (continuationIndentStack->empty())
spaceIndentCount = spaceIndentObjCMethodAlignment;
}
}
void ASBeautifier::adjustObjCMethodCallIndentation(const string& line_)
{
static int keywordIndentObjCMethodAlignment = 0;
if (shouldAlignMethodColon && objCColonAlignSubsequent != -1)
{
if (isInObjCMethodCallFirst)
{
isInObjCMethodCallFirst = false;
string convertedLine = getIndentedSpaceEquivalent(line_);
bracePosObjCMethodAlignment = convertedLine.find('[');
keywordIndentObjCMethodAlignment =
getObjCFollowingKeyword(convertedLine, bracePosObjCMethodAlignment);
colonIndentObjCMethodAlignment = convertedLine.find(':');
if (colonIndentObjCMethodAlignment >= 0)
{
int objCColonAlignSubsequentIndent = objCColonAlignSubsequent + indentLength;
if (objCColonAlignSubsequentIndent > colonIndentObjCMethodAlignment)
colonIndentObjCMethodAlignment = objCColonAlignSubsequentIndent;
if (lineBeginsWithOpenBrace)
colonIndentObjCMethodAlignment -= indentLength;
}
}
else
{
if (line_.find(':') != string::npos)
{
if (colonIndentObjCMethodAlignment < 0)
spaceIndentCount += computeObjCColonAlignment(line_, objCColonAlignSubsequent);
else if (objCColonAlignSubsequent > colonIndentObjCMethodAlignment)
spaceIndentCount = computeObjCColonAlignment(line_, objCColonAlignSubsequent);
else
spaceIndentCount = computeObjCColonAlignment(line_, colonIndentObjCMethodAlignment);
}
else
{
if (spaceIndentCount < colonIndentObjCMethodAlignment)
spaceIndentCount += keywordIndentObjCMethodAlignment;
}
}
}
else // align keywords instead of colons
{
if (isInObjCMethodCallFirst)
{
isInObjCMethodCallFirst = false;
string convertedLine = getIndentedSpaceEquivalent(line_);
bracePosObjCMethodAlignment = convertedLine.find('[');
keywordIndentObjCMethodAlignment =
getObjCFollowingKeyword(convertedLine, bracePosObjCMethodAlignment);
}
else
{
if (spaceIndentCount < keywordIndentObjCMethodAlignment + bracePosObjCMethodAlignment)
spaceIndentCount += keywordIndentObjCMethodAlignment;
}
}
}
/**
* Clear the variables used to align the Objective-C method definitions.
*/
void ASBeautifier::clearObjCMethodDefinitionAlignment()
{
assert(isImmediatelyPostObjCMethodDefinition);
spaceIndentCount = 0;
spaceIndentObjCMethodAlignment = 0;
colonIndentObjCMethodAlignment = 0;
isInObjCMethodDefinition = false;
isImmediatelyPostObjCMethodDefinition = false;
if (!continuationIndentStack->empty())
continuationIndentStack->pop_back();
}
/**
* Compute the spaceIndentCount necessary to align the current line colon
* with the colon position in the argument.
* If it cannot be aligned indentLength is returned and a new colon
* position is calculated.
*/
int ASBeautifier::computeObjCColonAlignment(const string& line, int colonAlignPosition) const
{
int colonPosition = line.find(':');
if (colonPosition < 0 || colonPosition > colonAlignPosition)
return indentLength;
return (colonAlignPosition - colonPosition);
}
/*
* Compute postition of the keyword following the method call object.
*/
int ASBeautifier::getObjCFollowingKeyword(const string& line, int bracePos) const
{
assert(line[bracePos] == '[');
size_t firstText = line.find_first_not_of(" \t", bracePos + 1);
if (firstText == string::npos)
return -(indentCount * indentLength - 1);
size_t searchBeg = firstText;
size_t objectEnd = 0; // end of object text
if (line[searchBeg] == '[')
{
objectEnd = line.find(']', searchBeg + 1);
if (objectEnd == string::npos)
return 0;
}
else
{
if (line[searchBeg] == '(')
{
searchBeg = line.find(')', searchBeg + 1);
if (searchBeg == string::npos)
return 0;
}
// bypass the object name
objectEnd = line.find_first_of(" \t", searchBeg + 1);
if (objectEnd == string::npos)
return 0;
--objectEnd;
}
size_t keyPos = line.find_first_not_of(" \t", objectEnd + 1);
if (keyPos == string::npos)
return 0;
return keyPos - firstText;
}
/**
* Get a line using the current space indent with all tabs replaced by spaces.
* The indentCount is NOT included
* Needed to compute an accurate alignment.
*/
string ASBeautifier::getIndentedSpaceEquivalent(const string& line_) const
{
string spaceIndent;
spaceIndent.append(spaceIndentCount, ' ');
string convertedLine = spaceIndent + line_;
for (size_t i = spaceIndent.length(); i < convertedLine.length(); i++)
{
if (convertedLine[i] == '\t')
{
size_t numSpaces = indentLength - (i % indentLength);
convertedLine.replace(i, 1, numSpaces, ' ');
i += indentLength - 1;
}
}
return convertedLine;
}
/**
* Parse the current line to update indentCount and spaceIndentCount.
*/
void ASBeautifier::parseCurrentLine(const string& line)
{
bool isInLineComment = false;
bool isInOperator = false;
bool isSpecialChar = false;
bool haveCaseIndent = false;
bool haveAssignmentThisLine = false;
bool closingBraceReached = false;
bool previousLineProbation = (probationHeader != nullptr);
char ch = ' ';
int tabIncrementIn = 0;
if (isInQuote
&& !haveLineContinuationChar
&& !isInVerbatimQuote
&& !isInAsm)
isInQuote = false; // missing closing quote
haveLineContinuationChar = false;
for (size_t i = 0; i < line.length(); i++)
{
ch = line[i];
if (isInBeautifySQL)
continue;
// handle special characters (i.e. backslash+character such as \n, \t, ...)
if (isInQuote && !isInVerbatimQuote)
{
if (isSpecialChar)
{
isSpecialChar = false;
continue;
}
if (line.compare(i, 2, "\\\\") == 0)
{
i++;
continue;
}
if (ch == '\\')
{
if (peekNextChar(line, i) == ' ') // is this '\' at end of line
haveLineContinuationChar = true;
else
isSpecialChar = true;
continue;
}
}
else if (isInDefine && ch == '\\')
continue;
// bypass whitespace here
if (isWhiteSpace(ch))
{
if (ch == '\t')
tabIncrementIn += convertTabToSpaces(i, tabIncrementIn);
continue;
}
// handle quotes (such as 'x' and "Hello Dolly")
if (!(isInComment || isInLineComment)
&& (ch == '"'
|| (ch == '\'' && !isDigitSeparator(line, i))))
{
if (!isInQuote)
{
quoteChar = ch;
isInQuote = true;
char prevCh = i > 0 ? line[i - 1] : ' ';
if (isCStyle() && prevCh == 'R')
{
int parenPos = line.find('(', i);
if (parenPos != -1)
{
isInVerbatimQuote = true;
verbatimDelimiter = line.substr(i + 1, parenPos - i - 1);
}
}
else if (isSharpStyle() && prevCh == '@')
isInVerbatimQuote = true;
// check for "C" following "extern"
else if (g_preprocessorCppExternCBrace == 2 && line.compare(i, 3, "\"C\"") == 0)
++g_preprocessorCppExternCBrace;
}
else if (isInVerbatimQuote && ch == '"')
{
if (isCStyle())
{
string delim = ')' + verbatimDelimiter;
int delimStart = i - delim.length();
if (delimStart > 0 && line.substr(delimStart, delim.length()) == delim)
{
isInQuote = false;
isInVerbatimQuote = false;
}
}
else if (isSharpStyle())
{
if (line.compare(i, 2, "\"\"") == 0)
i++;
else
{
isInQuote = false;
isInVerbatimQuote = false;
continue;
}
}
}
else if (quoteChar == ch)
{
isInQuote = false;
isContinuation = true;
continue;
}
}
if (isInQuote)
continue;
// handle comments
if (!(isInComment || isInLineComment) && line.compare(i, 2, "//") == 0)
{
// if there is a 'case' statement after these comments unindent by 1
if (isCaseHeaderCommentIndent)
--indentCount;
// isElseHeaderIndent is set by ASFormatter if shouldBreakElseIfs is requested
// if there is an 'else' after these comments a tempStacks indent is required
if (isElseHeaderIndent && lineOpensWithLineComment && !tempStacks->empty())
indentCount += adjustIndentCountForBreakElseIfComments();
isInLineComment = true;
i++;
continue;
}
else if (!(isInComment || isInLineComment) && line.compare(i, 2, "/*") == 0)
{
// if there is a 'case' statement after these comments unindent by 1
if (isCaseHeaderCommentIndent && lineOpensWithComment)
--indentCount;
// isElseHeaderIndent is set by ASFormatter if shouldBreakElseIfs is requested
// if there is an 'else' after these comments a tempStacks indent is required
if (isElseHeaderIndent && lineOpensWithComment && !tempStacks->empty())
indentCount += adjustIndentCountForBreakElseIfComments();
isInComment = true;
i++;
if (!lineOpensWithComment) // does line start with comment?
blockCommentNoIndent = true; // if no, cannot indent continuation lines
continue;
}
else if ((isInComment || isInLineComment) && line.compare(i, 2, "*/") == 0)
{
size_t firstText = line.find_first_not_of(" \t");
// if there is a 'case' statement after these comments unindent by 1
// only if the ending comment is the first entry on the line
if (isCaseHeaderCommentIndent && firstText == i)
--indentCount;
// if this comment close starts the line, must check for else-if indent
// isElseHeaderIndent is set by ASFormatter if shouldBreakElseIfs is requested
// if there is an 'else' after these comments a tempStacks indent is required
if (firstText == i)
{
if (isElseHeaderIndent && !lineOpensWithComment && !tempStacks->empty())
indentCount += adjustIndentCountForBreakElseIfComments();
}
isInComment = false;
i++;
blockCommentNoIndent = false; // ok to indent next comment
continue;
}
// treat indented preprocessor lines as a line comment
else if (line[0] == '#' && isIndentedPreprocessor(line, i))
{
isInLineComment = true;
}
if (isInLineComment)
{
// bypass rest of the comment up to the comment end
while (i + 1 < line.length())
i++;
continue;
}
if (isInComment)
{
// if there is a 'case' statement after these comments unindent by 1
if (!lineOpensWithComment && isCaseHeaderCommentIndent)
--indentCount;
// isElseHeaderIndent is set by ASFormatter if shouldBreakElseIfs is requested
// if there is an 'else' after these comments a tempStacks indent is required
if (!lineOpensWithComment && isElseHeaderIndent && !tempStacks->empty())
indentCount += adjustIndentCountForBreakElseIfComments();
// bypass rest of the comment up to the comment end
while (i + 1 < line.length()
&& line.compare(i + 1, 2, "*/") != 0)
i++;
continue;
}
// if we have reached this far then we are NOT in a comment or string of special character...
if (probationHeader != nullptr)
{
if ((probationHeader == &AS_STATIC && ch == '{')
|| (probationHeader == &AS_SYNCHRONIZED && ch == '('))
{
// insert the probation header as a new header
isInHeader = true;
headerStack->emplace_back(probationHeader);
// handle the specific probation header
isInConditional = (probationHeader == &AS_SYNCHRONIZED);
isContinuation = false;
// if the probation comes from the previous line, then indent by 1 tab count.
if (previousLineProbation
&& ch == '{'
&& !(blockIndent && probationHeader == &AS_STATIC))
{
++indentCount;
previousLineProbationTab = true;
}
previousLineProbation = false;
}
// dismiss the probation header
probationHeader = nullptr;
}
prevNonSpaceCh = currentNonSpaceCh;
currentNonSpaceCh = ch;
if (!isLegalNameChar(ch) && ch != ',' && ch != ';')
{
prevNonLegalCh = currentNonLegalCh;
currentNonLegalCh = ch;
}
if (isInHeader)
{
isInHeader = false;
currentHeader = headerStack->back();
}
else
currentHeader = nullptr;
if (isCStyle() && isInTemplate
&& (ch == '<' || ch == '>')
&& !(line.length() > i + 1 && line.compare(i, 2, ">=") == 0))
{
if (ch == '<')
{
++templateDepth;
continuationIndentStackSizeStack->push_back(continuationIndentStack->size());
registerContinuationIndent(line, i, spaceIndentCount, tabIncrementIn, 0, true);
}
else if (ch == '>')
{
popLastContinuationIndent();
if (--templateDepth <= 0)
{
ch = ';';
isInTemplate = false;
templateDepth = 0;
}
}
}
// handle parentheses
if (ch == '(' || ch == '[' || ch == ')' || ch == ']')
{
if (ch == '(' || ch == '[')
{
isInOperator = false;
// if have a struct header, this is a declaration not a definition
if (ch == '('
&& !headerStack->empty()
&& headerStack->back() == &AS_STRUCT)
{
headerStack->pop_back();
isInClassHeader = false;
if (line.find(AS_STRUCT, 0) > i) // if not on this line
indentCount -= classInitializerIndents;
if (indentCount < 0)
indentCount = 0;
}
if (parenDepth == 0)
{
parenStatementStack->push_back(isContinuation);
isContinuation = true;
}
parenDepth++;
if (ch == '[')
{
++squareBracketCount;
if (squareBracketCount == 1 && isCStyle())
{
isInObjCMethodCall = true;
isInObjCMethodCallFirst = true;
}
}
continuationIndentStackSizeStack->push_back(continuationIndentStack->size());
if (currentHeader != nullptr)
registerContinuationIndent(line, i, spaceIndentCount, tabIncrementIn, minConditionalIndent, true);
else
registerContinuationIndent(line, i, spaceIndentCount, tabIncrementIn, 0, true);
}
else if (ch == ')' || ch == ']')
{
if (ch == ']')
--squareBracketCount;
if (squareBracketCount <= 0)
{
squareBracketCount = 0;
if (isInObjCMethodCall)
isImmediatelyPostObjCMethodCall = true;
}
foundPreCommandHeader = false;
parenDepth--;
if (parenDepth == 0)
{
if (!parenStatementStack->empty()) // in case of unmatched closing parens
{
isContinuation = parenStatementStack->back();
parenStatementStack->pop_back();
}
isInAsm = false;
isInConditional = false;
}
if (!continuationIndentStackSizeStack->empty())
{
popLastContinuationIndent();
if (!parenIndentStack->empty())
{
int poppedIndent = parenIndentStack->back();
parenIndentStack->pop_back();
if (i == 0)
spaceIndentCount = poppedIndent;
}
}
}
continue;
}
if (ch == '{')
{
// first, check if '{' is a block-opener or a static-array opener
bool isBlockOpener = ((prevNonSpaceCh == '{' && braceBlockStateStack->back())
|| prevNonSpaceCh == '}'
|| prevNonSpaceCh == ')'
|| prevNonSpaceCh == ';'
|| peekNextChar(line, i) == '{'
|| foundPreCommandHeader
|| foundPreCommandMacro
|| isInClassHeader
|| (isInClassInitializer && !isLegalNameChar(prevNonSpaceCh))
|| isNonInStatementArray
|| isInObjCMethodDefinition
|| isInObjCInterface
|| isSharpAccessor
|| isSharpDelegate
|| isInExternC
|| isInAsmBlock
|| getNextWord(line, i) == AS_NEW
|| (isInDefine
&& (prevNonSpaceCh == '('
|| isLegalNameChar(prevNonSpaceCh))));
if (isInObjCMethodDefinition)
{
objCColonAlignSubsequent = 0;
isImmediatelyPostObjCMethodDefinition = true;
if (lineBeginsWithOpenBrace) // for run-in braces
clearObjCMethodDefinitionAlignment();
}
if (!isBlockOpener && !isContinuation && !isInClassInitializer && !isInEnum)
{
if (headerStack->empty())
isBlockOpener = true;
else if (headerStack->back() == &AS_OPEN_BRACE
&& headerStack->size() >= 2)
{
if ((*headerStack)[headerStack->size() - 2] == &AS_NAMESPACE
|| (*headerStack)[headerStack->size() - 2] == &AS_MODULE
|| (*headerStack)[headerStack->size() - 2] == &AS_CLASS
|| (*headerStack)[headerStack->size() - 2] == &AS_INTERFACE
|| (*headerStack)[headerStack->size() - 2] == &AS_STRUCT
|| (*headerStack)[headerStack->size() - 2] == &AS_UNION)
isBlockOpener = true;
}
else if (headerStack->back() == &AS_NAMESPACE
|| headerStack->back() == &AS_MODULE
|| headerStack->back() == &AS_CLASS
|| headerStack->back() == &AS_INTERFACE
|| headerStack->back() == &AS_STRUCT
|| headerStack->back() == &AS_UNION)
isBlockOpener = true;
}
if (!isBlockOpener && currentHeader != nullptr)
{
for (size_t n = 0; n < nonParenHeaders->size(); n++)
if (currentHeader == (*nonParenHeaders)[n])
{
isBlockOpener = true;
break;
}
}
braceBlockStateStack->push_back(isBlockOpener);
if (!isBlockOpener)
{
continuationIndentStackSizeStack->push_back(continuationIndentStack->size());
registerContinuationIndent(line, i, spaceIndentCount, tabIncrementIn, 0, true);
parenDepth++;
if (i == 0)
shouldIndentBracedLine = false;
isInEnumTypeID = false;
continue;
}
// this brace is a block opener...
++lineOpeningBlocksNum;
if (isInClassInitializer || isInEnumTypeID)
{
// decrease tab count if brace is broken
if (lineBeginsWithOpenBrace)
{
indentCount -= classInitializerIndents;
// decrease one more if an empty class
if (!headerStack->empty()
&& (*headerStack).back() == &AS_CLASS)
{
int nextChar = getNextProgramCharDistance(line, i);
if ((int) line.length() > nextChar && line[nextChar] == '}')
--indentCount;
}
}
}
if (isInObjCInterface)
{
isInObjCInterface = false;
if (lineBeginsWithOpenBrace)
--indentCount;
}
if (braceIndent && !namespaceIndent && !headerStack->empty()
&& ((*headerStack).back() == &AS_NAMESPACE
|| (*headerStack).back() == &AS_MODULE))
{
shouldIndentBracedLine = false;
--indentCount;
}
// an indentable struct is treated like a class in the header stack
if (!headerStack->empty()
&& (*headerStack).back() == &AS_STRUCT
&& isInIndentableStruct)
(*headerStack).back() = &AS_CLASS;
squareBracketDepthStack->emplace_back(parenDepth);
blockStatementStack->push_back(isContinuation);
if (!continuationIndentStack->empty())
{
// completely purge the inStatementIndentStack
while (!continuationIndentStack->empty())
popLastContinuationIndent();
if (isInClassInitializer || isInClassHeaderTab)
{
if (lineBeginsWithOpenBrace || lineBeginsWithComma)
spaceIndentCount = 0;
}
else
spaceIndentCount = 0;
}
blockTabCount += (isContinuation ? 1 : 0);
if (g_preprocessorCppExternCBrace == 3)
++g_preprocessorCppExternCBrace;
parenDepth = 0;
isInClassHeader = false;
isInClassHeaderTab = false;
isInClassInitializer = false;
isInEnumTypeID = false;
isContinuation = false;
isInQuestion = false;
isInLet = false;
foundPreCommandHeader = false;
foundPreCommandMacro = false;
isInExternC = false;
tempStacks->emplace_back(new vector<const string*>);
headerStack->emplace_back(&AS_OPEN_BRACE);
lastLineHeader = &AS_OPEN_BRACE;
continue;
} // end '{'
//check if a header has been reached
bool isPotentialHeader = isCharPotentialHeader(line, i);
if (isPotentialHeader && squareBracketCount == 0)
{
const string* newHeader = findHeader(line, i, headers);
// Qt headers may be variables in C++
if (isCStyle()
&& (newHeader == &AS_FOREVER || newHeader == &AS_FOREACH))
{
if (line.find_first_of("=;", i) != string::npos)
newHeader = nullptr;
}
else if (newHeader == &AS_USING
&& ASBeautifier::peekNextChar(line, i + (*newHeader).length() - 1) != '(')
newHeader = nullptr;
if (newHeader != nullptr)
{
// if we reached here, then this is a header...
bool isIndentableHeader = true;
isInHeader = true;
vector<const string*>* lastTempStack = nullptr;;
if (!tempStacks->empty())
lastTempStack = tempStacks->back();
// if a new block is opened, push a new stack into tempStacks to hold the
// future list of headers in the new block.
// take care of the special case: 'else if (...)'
if (newHeader == &AS_IF && lastLineHeader == &AS_ELSE)
{
headerStack->pop_back();
}
// take care of 'else'
else if (newHeader == &AS_ELSE)
{
if (lastTempStack != nullptr)
{
int indexOfIf = indexOf(*lastTempStack, &AS_IF);
if (indexOfIf != -1)
{
// recreate the header list in headerStack up to the previous 'if'
// from the temporary snapshot stored in lastTempStack.
int restackSize = lastTempStack->size() - indexOfIf - 1;
for (int r = 0; r < restackSize; r++)
{
headerStack->emplace_back(lastTempStack->back());
lastTempStack->pop_back();
}
if (!closingBraceReached)
indentCount += restackSize;
}
/*
* If the above if is not true, i.e. no 'if' before the 'else',
* then nothing beautiful will come out of this...
* I should think about inserting an Exception here to notify the caller of this...
*/
}
}
// check if 'while' closes a previous 'do'
else if (newHeader == &AS_WHILE)
{
if (lastTempStack != nullptr)
{
int indexOfDo = indexOf(*lastTempStack, &AS_DO);
if (indexOfDo != -1)
{
// recreate the header list in headerStack up to the previous 'do'
// from the temporary snapshot stored in lastTempStack.
int restackSize = lastTempStack->size() - indexOfDo - 1;
for (int r = 0; r < restackSize; r++)
{
headerStack->emplace_back(lastTempStack->back());
lastTempStack->pop_back();
}
if (!closingBraceReached)
indentCount += restackSize;
}
}
}
// check if 'catch' closes a previous 'try' or 'catch'
else if (newHeader == &AS_CATCH || newHeader == &AS_FINALLY)
{
if (lastTempStack != nullptr)
{
int indexOfTry = indexOf(*lastTempStack, &AS_TRY);
if (indexOfTry == -1)
indexOfTry = indexOf(*lastTempStack, &AS_CATCH);
if (indexOfTry != -1)
{
// recreate the header list in headerStack up to the previous 'try'
// from the temporary snapshot stored in lastTempStack.
int restackSize = lastTempStack->size() - indexOfTry - 1;
for (int r = 0; r < restackSize; r++)
{
headerStack->emplace_back(lastTempStack->back());
lastTempStack->pop_back();
}
if (!closingBraceReached)
indentCount += restackSize;
}
}
}
else if (newHeader == &AS_CASE)
{
isInCase = true;
if (!haveCaseIndent)
{
haveCaseIndent = true;
if (!lineBeginsWithOpenBrace)
--indentCount;
}
}
else if (newHeader == &AS_DEFAULT)
{
isInCase = true;
--indentCount;
}
else if (newHeader == &AS_STATIC
|| newHeader == &AS_SYNCHRONIZED)
{
if (!headerStack->empty()
&& (headerStack->back() == &AS_STATIC
|| headerStack->back() == &AS_SYNCHRONIZED))
{
isIndentableHeader = false;
}
else
{
isIndentableHeader = false;
probationHeader = newHeader;
}
}
else if (newHeader == &AS_TEMPLATE)
{
isInTemplate = true;
isIndentableHeader = false;
}
if (isIndentableHeader)
{
headerStack->emplace_back(newHeader);
isContinuation = false;
if (indexOf(*nonParenHeaders, newHeader) == -1)
{
isInConditional = true;
}
lastLineHeader = newHeader;
}
else
isInHeader = false;
i += newHeader->length() - 1;
continue;
} // newHeader != nullptr
if (findHeader(line, i, preCommandHeaders) != nullptr)
foundPreCommandHeader = true;
// Objective-C NSException macros are preCommandHeaders
if (isCStyle() && findKeyword(line, i, AS_NS_DURING))
foundPreCommandMacro = true;
if (isCStyle() && findKeyword(line, i, AS_NS_HANDLER))
foundPreCommandMacro = true;
if (parenDepth == 0 && findKeyword(line, i, AS_ENUM))
isInEnum = true;
if (isSharpStyle() && findKeyword(line, i, AS_LET))
isInLet = true;
} // isPotentialHeader
if (ch == '?')
isInQuestion = true;
// special handling of colons
if (ch == ':')
{
if (line.length() > i + 1 && line[i + 1] == ':') // look for ::
{
++i;
continue;
}
else if (isInQuestion)
{
// do nothing special
}
else if (parenDepth > 0)
{
// found a 'for' loop or an objective-C statement
// so do nothing special
}
else if (isInEnum)
{
// found an enum with a base-type
isInEnumTypeID = true;
if (i == 0)
indentCount += classInitializerIndents;
}
else if (isCStyle()
&& !isInCase
&& (prevNonSpaceCh == ')' || foundPreCommandHeader))
{
// found a 'class' c'tor initializer
isInClassInitializer = true;
registerContinuationIndentColon(line, i, tabIncrementIn);
if (i == 0)
indentCount += classInitializerIndents;
}
else if (isInClassHeader || isInObjCInterface)
{
// is in a 'class A : public B' definition
isInClassHeaderTab = true;
registerContinuationIndentColon(line, i, tabIncrementIn);
}
else if (isInAsm || isInAsmOneLine || isInAsmBlock)
{
// do nothing special
}
else if (isDigit(peekNextChar(line, i)))
{
// found a bit field - do nothing special
}
else if (isCStyle() && isInClass && prevNonSpaceCh != ')')
{
// found a 'private:' or 'public:' inside a class definition
--indentCount;
if (modifierIndent)
spaceIndentCount += (indentLength / 2);
}
else if (isCStyle() && !isInClass
&& headerStack->size() >= 2
&& (*headerStack)[headerStack->size() - 2] == &AS_CLASS
&& (*headerStack)[headerStack->size() - 1] == &AS_OPEN_BRACE)
{
// found a 'private:' or 'public:' inside a class definition
// and on the same line as the class opening brace
// do nothing
}
else if (isJavaStyle() && lastLineHeader == &AS_FOR)
{
// found a java for-each statement
// so do nothing special
}
else
{
currentNonSpaceCh = ';'; // so that braces after the ':' will appear as block-openers
char peekedChar = peekNextChar(line, i);
if (isInCase)
{
isInCase = false;
ch = ';'; // from here on, treat char as ';'
}
else if (isCStyle() || (isSharpStyle() && peekedChar == ';'))
{
// is in a label (e.g. 'label1:')
if (labelIndent)
--indentCount; // unindent label by one indent
else if (!lineBeginsWithOpenBrace)
indentCount = 0; // completely flush indent to left
}
}
}
if ((ch == ';' || (parenDepth > 0 && ch == ',')) && !continuationIndentStackSizeStack->empty())
while ((int) continuationIndentStackSizeStack->back() + (parenDepth > 0 ? 1 : 0)
< (int) continuationIndentStack->size())
continuationIndentStack->pop_back();
else if (ch == ',' && isInEnum && isNonInStatementArray && !continuationIndentStack->empty())
continuationIndentStack->pop_back();
// handle commas
// previous "isInStatement" will be from an assignment operator or class initializer
if (ch == ',' && parenDepth == 0 && !isContinuation && !isNonInStatementArray)
{
// is comma at end of line
size_t nextChar = line.find_first_not_of(" \t", i + 1);
if (nextChar != string::npos)
{
if (line.compare(nextChar, 2, "//") == 0
|| line.compare(nextChar, 2, "/*") == 0)
nextChar = string::npos;
}
// register indent
if (nextChar == string::npos)
{
// register indent at previous word
if (isJavaStyle() && isInClassHeader)
{
// do nothing for now
}
// register indent at second word on the line
else if (!isInTemplate && !isInClassHeaderTab && !isInClassInitializer)
{
int prevWord = getContinuationIndentComma(line, i);
int continuationIndentCount = prevWord + spaceIndentCount + tabIncrementIn;
continuationIndentStack->emplace_back(continuationIndentCount);
isContinuation = true;
}
}
}
// handle comma first initializers
if (ch == ',' && parenDepth == 0 && lineBeginsWithComma
&& (isInClassInitializer || isInClassHeaderTab))
spaceIndentCount = 0;
// handle ends of statements
if ((ch == ';' && parenDepth == 0) || ch == '}')
{
if (ch == '}')
{
// first check if this '}' closes a previous block, or a static array...
if (braceBlockStateStack->size() > 1)
{
bool braceBlockState = braceBlockStateStack->back();
braceBlockStateStack->pop_back();
if (!braceBlockState)
{
if (!continuationIndentStackSizeStack->empty())
{
// this brace is a static array
popLastContinuationIndent();
parenDepth--;
if (i == 0)
shouldIndentBracedLine = false;
if (!parenIndentStack->empty())
{
int poppedIndent = parenIndentStack->back();
parenIndentStack->pop_back();
if (i == 0)
spaceIndentCount = poppedIndent;
}
}
continue;
}
}
// this brace is block closer...
++lineClosingBlocksNum;
if (!continuationIndentStackSizeStack->empty())
popLastContinuationIndent();
if (!squareBracketDepthStack->empty())
{
parenDepth = squareBracketDepthStack->back();
squareBracketDepthStack->pop_back();
isContinuation = blockStatementStack->back();
blockStatementStack->pop_back();
if (isContinuation)
blockTabCount--;
}
closingBraceReached = true;
if (i == 0)
spaceIndentCount = 0;
isInAsmBlock = false;
isInAsm = isInAsmOneLine = isInQuote = false; // close these just in case
int headerPlace = indexOf(*headerStack, &AS_OPEN_BRACE);
if (headerPlace != -1)
{
const string* popped = headerStack->back();
while (popped != &AS_OPEN_BRACE)
{
headerStack->pop_back();
popped = headerStack->back();
}
headerStack->pop_back();
if (headerStack->empty())
g_preprocessorCppExternCBrace = 0;
// do not indent namespace brace unless namespaces are indented
if (!namespaceIndent && !headerStack->empty()
&& ((*headerStack).back() == &AS_NAMESPACE
|| (*headerStack).back() == &AS_MODULE)
&& i == 0) // must be the first brace on the line
shouldIndentBracedLine = false;
if (!tempStacks->empty())
{
vector<const string*>* temp = tempStacks->back();
tempStacks->pop_back();
delete temp;
}
}
ch = ' '; // needed due to cases such as '}else{', so that headers ('else' in this case) will be identified...
} // ch == '}'
/*
* Create a temporary snapshot of the current block's header-list in the
* uppermost inner stack in tempStacks, and clear the headerStack up to
* the beginning of the block.
* Thus, the next future statement will think it comes one indent past
* the block's '{' unless it specifically checks for a companion-header
* (such as a previous 'if' for an 'else' header) within the tempStacks,
* and recreates the temporary snapshot by manipulating the tempStacks.
*/
if (!tempStacks->back()->empty())
while (!tempStacks->back()->empty())
tempStacks->back()->pop_back();
while (!headerStack->empty() && headerStack->back() != &AS_OPEN_BRACE)
{
tempStacks->back()->emplace_back(headerStack->back());
headerStack->pop_back();
}
if (parenDepth == 0 && ch == ';')
isContinuation = false;
if (isInObjCMethodDefinition)
{
objCColonAlignSubsequent = 0;
isImmediatelyPostObjCMethodDefinition = true;
}
previousLastLineHeader = nullptr;
isInClassHeader = false; // for 'friend' class
isInEnum = false;
isInEnumTypeID = false;
isInQuestion = false;
isInTemplate = false;
isInObjCInterface = false;
foundPreCommandHeader = false;
foundPreCommandMacro = false;
squareBracketCount = 0;
continue;
}
if (isPotentialHeader)
{
// check for preBlockStatements in C/C++ ONLY if not within parentheses
// (otherwise 'struct XXX' statements would be wrongly interpreted...)
if (!isInTemplate && !(isCStyle() && parenDepth > 0))
{
const string* newHeader = findHeader(line, i, preBlockStatements);
// handle CORBA IDL module
if (newHeader == &AS_MODULE)
{
char nextChar = peekNextChar(line, i + newHeader->length() - 1);
if (prevNonSpaceCh == ')' || !isalpha(nextChar))
newHeader = nullptr;
}
if (newHeader != nullptr
&& !(isCStyle() && newHeader == &AS_CLASS && isInEnum)) // is not 'enum class'
{
if (!isSharpStyle())
headerStack->emplace_back(newHeader);
// do not need 'where' in the headerStack
// do not need second 'class' statement in a row
else if (!(newHeader == &AS_WHERE
|| ((newHeader == &AS_CLASS || newHeader == &AS_STRUCT)
&& !headerStack->empty()
&& (headerStack->back() == &AS_CLASS
|| headerStack->back() == &AS_STRUCT))))
headerStack->emplace_back(newHeader);
if (!headerStack->empty())
{
if ((*headerStack).back() == &AS_CLASS
|| (*headerStack).back() == &AS_STRUCT
|| (*headerStack).back() == &AS_INTERFACE)
{
isInClassHeader = true;
}
else if ((*headerStack).back() == &AS_NAMESPACE
|| (*headerStack).back() == &AS_MODULE)
{
// remove continuationIndent from namespace
if (!continuationIndentStack->empty())
continuationIndentStack->pop_back();
isContinuation = false;
}
}
i += newHeader->length() - 1;
continue;
}
}
const string* foundIndentableHeader = findHeader(line, i, indentableHeaders);
if (foundIndentableHeader != nullptr)
{
// must bypass the header before registering the in statement
i += foundIndentableHeader->length() - 1;
if (!isInOperator && !isInTemplate && !isNonInStatementArray)
{
registerContinuationIndent(line, i, spaceIndentCount, tabIncrementIn, 0, false);
isContinuation = true;
}
continue;
}
if (isCStyle() && findKeyword(line, i, AS_OPERATOR))
isInOperator = true;
if (g_preprocessorCppExternCBrace == 1 && findKeyword(line, i, AS_EXTERN))
++g_preprocessorCppExternCBrace;
if (g_preprocessorCppExternCBrace == 3) // extern "C" is not followed by a '{'
g_preprocessorCppExternCBrace = 0;
// "new" operator is a pointer, not a calculation
if (findKeyword(line, i, AS_NEW))
{
if (isContinuation && !continuationIndentStack->empty() && prevNonSpaceCh == '=')
continuationIndentStack->back() = 0;
}
if (isCStyle())
{
if (findKeyword(line, i, AS_ASM)
|| findKeyword(line, i, AS__ASM__))
{
isInAsm = true;
}
else if (findKeyword(line, i, AS_MS_ASM) // microsoft specific
|| findKeyword(line, i, AS_MS__ASM))
{
int index = 4;
if (peekNextChar(line, i) == '_') // check for __asm
index = 5;
char peekedChar = peekNextChar(line, i + index);
if (peekedChar == '{' || peekedChar == ' ')
isInAsmBlock = true;
else
isInAsmOneLine = true;
}
}
// bypass the entire name for all others
string name = getCurrentWord(line, i);
i += name.length() - 1;
continue;
}
// Handle Objective-C statements
if (ch == '@' && !isWhiteSpace(line[i + 1])
&& isCharPotentialHeader(line, i + 1))
{
string curWord = getCurrentWord(line, i + 1);
if (curWord == AS_INTERFACE && headerStack->empty())
{
isInObjCInterface = true;
string name = '@' + curWord;
i += name.length() - 1;
continue;
}
else if (isInObjCInterface)
{
--indentCount;
isInObjCInterface = false;
}
if (curWord == AS_PUBLIC
|| curWord == AS_PRIVATE
|| curWord == AS_PROTECTED)
{
--indentCount;
if (modifierIndent)
spaceIndentCount += (indentLength / 2);
string name = '@' + curWord;
i += name.length() - 1;
continue;
}
else if (curWord == AS_END)
{
popLastContinuationIndent();
spaceIndentCount = 0;
isInObjCMethodDefinition = false;
string name = '@' + curWord;
i += name.length() - 1;
continue;
}
}
else if ((ch == '-' || ch == '+')
&& peekNextChar(line, i) == '('
&& headerStack->empty()
&& line.find_first_not_of(" \t") == i)
{
if (isInObjCInterface)
--indentCount;
isInObjCInterface = false;
isInObjCMethodDefinition = true;
continue;
}
// Handle operators
bool isPotentialOperator = isCharPotentialOperator(ch);
if (isPotentialOperator)
{
// Check if an operator has been reached.
const string* foundAssignmentOp = findOperator(line, i, assignmentOperators);
const string* foundNonAssignmentOp = findOperator(line, i, nonAssignmentOperators);
if (foundNonAssignmentOp != nullptr)
{
if (foundNonAssignmentOp == &AS_LAMBDA)
foundPreCommandHeader = true;
if (isInTemplate && foundNonAssignmentOp == &AS_GR_GR)
foundNonAssignmentOp = nullptr;
}
// Since findHeader's boundary checking was not used above, it is possible
// that both an assignment op and a non-assignment op where found,
// e.g. '>>' and '>>='. If this is the case, treat the LONGER one as the
// found operator.
if (foundAssignmentOp != nullptr && foundNonAssignmentOp != nullptr)
{
if (foundAssignmentOp->length() < foundNonAssignmentOp->length())
foundAssignmentOp = nullptr;
else
foundNonAssignmentOp = nullptr;
}
if (foundNonAssignmentOp != nullptr)
{
if (foundNonAssignmentOp->length() > 1)
i += foundNonAssignmentOp->length() - 1;
// For C++ input/output, operator<< and >> should be
// aligned, if we are not in a statement already and
// also not in the "operator<<(...)" header line
if (!isInOperator
&& continuationIndentStack->empty()
&& isCStyle()
&& (foundNonAssignmentOp == &AS_GR_GR
|| foundNonAssignmentOp == &AS_LS_LS))
{
// this will be true if the line begins with the operator
if (i < 2 && spaceIndentCount == 0)
spaceIndentCount += 2 * indentLength;
// align to the beginning column of the operator
registerContinuationIndent(line, i - foundNonAssignmentOp->length(), spaceIndentCount, tabIncrementIn, 0, false);
}
}
else if (foundAssignmentOp != nullptr)
{
foundPreCommandHeader = false; // clears this for array assignments
foundPreCommandMacro = false;
if (foundAssignmentOp->length() > 1)
i += foundAssignmentOp->length() - 1;
if (!isInOperator && !isInTemplate && (!isNonInStatementArray || isInEnum))
{
// if multiple assignments, align on the previous word
if (foundAssignmentOp == &AS_ASSIGN
&& prevNonSpaceCh != ']' // an array
&& statementEndsWithComma(line, i))
{
if (!haveAssignmentThisLine) // only one assignment indent per line
{
// register indent at previous word
haveAssignmentThisLine = true;
int prevWordIndex = getContinuationIndentAssign(line, i);
int continuationIndentCount = prevWordIndex + spaceIndentCount + tabIncrementIn;
continuationIndentStack->emplace_back(continuationIndentCount);
isContinuation = true;
}
}
// don't indent an assignment if 'let'
else if (isInLet)
{
isInLet = false;
}
else if (!lineBeginsWithComma)
{
if (i == 0 && spaceIndentCount == 0)
spaceIndentCount += indentLength;
registerContinuationIndent(line, i, spaceIndentCount, tabIncrementIn, 0, false);
isContinuation = true;
}
}
}
}
} // end of for loop * end of for loop * end of for loop * end of for loop * end of for loop *
}
} // end namespace astyle
|