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
|
------------------------------------------------------------------------------
-- --
-- GNATCHECK COMPONENTS --
-- --
-- G N A T C H E C K . R U L E S . C U S T O M _ 2 --
-- --
-- B o d y --
-- --
-- Copyright (C) 2008-2018, AdaCore --
-- --
-- GNATCHECK is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or ( at your option) any later --
-- version. GNATCHECK is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received a copy of the --
-- GNU General Public License distributed with GNAT; see file COPYING3. If --
-- not, go to http://www.gnu.org/licenses for a complete copy of the --
-- license. --
-- --
-- GNATCHECK is maintained by AdaCore (http://www.adacore.com). --
-- --
------------------------------------------------------------------------------
pragma Ada_2012;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Asis.Clauses; use Asis.Clauses;
with Asis.Compilation_Units; use Asis.Compilation_Units;
with Asis.Declarations; use Asis.Declarations;
with Asis.Definitions; use Asis.Definitions;
with Asis.Elements; use Asis.Elements;
with Asis.Expressions; use Asis.Expressions;
with Asis.Extensions; use Asis.Extensions;
with Asis.Extensions.Flat_Kinds; use Asis.Extensions.Flat_Kinds;
with Asis.Iterator; use Asis.Iterator;
with Asis.Statements; use Asis.Statements;
with Asis.Text; use Asis.Text;
with ASIS_UL.Global_State.Utilities; use ASIS_UL.Global_State.Utilities;
with ASIS_UL.Misc; use ASIS_UL.Misc;
with ASIS_UL.Utilities; use ASIS_UL.Utilities;
with Gnatcheck.ASIS_Utilities; use Gnatcheck.ASIS_Utilities;
with Gnatcheck.Rules.Traversing; use Gnatcheck.Rules.Traversing;
with Gnatcheck.Traversal_Stack; use Gnatcheck.Traversal_Stack;
package body Gnatcheck.Rules.Custom_2 is
---------------------------------
-- Complex_Inlined_Subprograms --
---------------------------------
---------------------------------------------
-- Init_Rule (Complex_Inlined_Subprograms) --
---------------------------------------------
procedure Init_Rule (Rule : in out Complex_Inlined_Subprograms_Rule_Type)
is
begin
Init_Rule (One_Integer_Parameter_Rule_Template (Rule));
Rule.Name := new String'("Complex_Inlined_Subprograms");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("complex inlined subprograms");
Rule.Diagnosis := new String'
("#1#too many statements in inlined subprogram" &
"#2#branching in inlined subprogram (%1%)" &
"#3#complex declaration in inlined subprogram (%1%)");
end Init_Rule;
-----------------------------------------------------
-- Rule_Check_Pre_Op (Complex_Inlined_Subprograms) --
-----------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Complex_Inlined_Subprograms_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
Corr_Generic : Asis.Element;
Exp_Body : Asis.Element;
Local_State : Natural := 0;
Local_Control : Traverse_Control := Continue;
procedure Check_Statement
(Element : Asis.Element;
Local_Control : in out Traverse_Control;
Local_State : in out Natural);
-- Check if the argument is a branching construct, that is, an IF, LOOP
-- or CASE statement or a short-circuit control form. Also counts the
-- number of statements. If a branching construct is encountered or
-- if the number of statements exceeds the rule limit, terminates the
-- traversing and sets the diagnosis accordingly.
procedure No_Operation
(Element : Asis.Element;
Local_Control : in out Traverse_Control;
Local_State : in out Natural);
-- Does nothing
procedure Check_Statements is new Traverse_Element
(State_Information => Natural,
Pre_Operation => Check_Statement,
Post_Operation => No_Operation);
---------------------
-- Check_Statement --
---------------------
procedure Check_Statement
(Element : Asis.Element;
Local_Control : in out Traverse_Control;
Local_State : in out Natural)
is
begin
if Element_Kind (Element) = A_Statement then
Local_State := Local_State + 1;
if Local_State > Rule.Rule_Limit then
State.Detected := True;
State.Diagnosis := 1;
Local_Control := Terminate_Immediately;
return;
end if;
end if;
if Statement_Kind (Element) in
An_If_Statement .. A_For_Loop_Statement
then
State.Detected := True;
State.Diagnosis := 2;
if Is_Part_Of_Instance (Element) then
State.Diag_Params := Enter_String
("%1%" & Build_GNAT_Location (Element));
else
State.Diag_Params := Enter_String
("%1%" & "line " &
ASIS_UL.Misc.Image (Element_Span (Element).First_Line));
end if;
Local_Control := Terminate_Immediately;
end if;
end Check_Statement;
------------------
-- No_Operation --
------------------
procedure No_Operation
(Element : Asis.Element;
Local_Control : in out Traverse_Control;
Local_State : in out Natural)
is
begin
null;
end No_Operation;
begin -- Rule_Check_Pre_Op (Complex_Inlined_Subprograms)
if Declaration_Kind (Element) in
A_Procedure_Body_Declaration .. A_Function_Body_Declaration
and then
Has_Pragma_Inline (Element)
then
declare
Dcls : constant Asis.Element_List :=
Body_Declarative_Items (Element);
begin
-- First, check if we have "bad" declarations:
for J in Dcls'Range loop
case Declaration_Kind (Dcls (J)) is
when A_Task_Type_Declaration |
A_Protected_Type_Declaration |
A_Single_Task_Declaration |
A_Single_Protected_Declaration |
A_Procedure_Body_Declaration |
A_Function_Body_Declaration |
A_Package_Declaration |
A_Package_Body_Declaration |
A_Task_Body_Declaration |
A_Protected_Body_Declaration |
A_Procedure_Body_Stub |
A_Function_Body_Stub |
A_Package_Body_Stub |
A_Task_Body_Stub |
A_Protected_Body_Stub |
A_Generic_Procedure_Declaration |
A_Generic_Function_Declaration |
A_Generic_Package_Declaration |
A_Package_Instantiation |
A_Procedure_Instantiation =>
State.Detected := True;
State.Diagnosis := 3;
if Is_Part_Of_Instance (Dcls (J)) then
State.Diag_Params := Enter_String
("%1%" & Build_GNAT_Location (Dcls (J)));
else
State.Diag_Params := Enter_String
("%1%" & "line " &
ASIS_UL.Misc.Image
(Element_Span (Dcls (J)).First_Line));
end if;
return;
when A_Function_Instantiation =>
-- check if this is not an instantiation of
-- Ada.Unchecked_Convertion
if not Is_Unchecked_Convertion_Instance (Dcls (J)) then
State.Detected := True;
State.Diagnosis := 3;
if Is_Part_Of_Instance (Dcls (J)) then
State.Diag_Params := Enter_String
("%1%" & Build_GNAT_Location (Dcls (J)));
else
State.Diag_Params := Enter_String
("%1%" & "line " &
ASIS_UL.Misc.Image
(Element_Span (Dcls (J)).First_Line));
end if;
return;
end if;
when others =>
null;
end case;
end loop;
-- Now we know that there are no bad local declarations, so -
-- checking the statements.
end;
Check_Statements (Element, Local_Control, Local_State);
elsif Declaration_Kind (Element) in
A_Procedure_Instantiation .. A_Function_Instantiation
and then
Has_Pragma_Inline (Element)
then
Corr_Generic := Generic_Unit_Name (Element);
Corr_Generic := Normalize_Reference (Corr_Generic);
Corr_Generic := Corresponding_Name_Declaration (Corr_Generic);
if Declaration_Kind (Corr_Generic) in
A_Generic_Procedure_Renaming_Declaration ..
A_Generic_Function_Renaming_Declaration
then
Corr_Generic := Corresponding_Base_Entity (Corr_Generic);
Corr_Generic := Normalize_Reference (Corr_Generic);
Corr_Generic := Corresponding_Name_Declaration (Corr_Generic);
end if;
if not Has_Pragma_Inline (Corr_Generic) then
-- Otherwise we will report the same problem for the body of the
-- generic and for each of its instantiations!
Exp_Body := Corresponding_Body (Element);
if not Is_Nil (Exp_Body) then
Rule_Check_Pre_Op
(Rule => Rule,
Element => Exp_Body,
Control => Control,
State => State);
end if;
end if;
end if;
end Rule_Check_Pre_Op;
-----------------------------
-- Conditional_Expressions --
-----------------------------
-----------------------------------------------------
-- Activate_In_Test_Mode (Conditional_Expressions) --
-----------------------------------------------------
overriding procedure Activate_In_Test_Mode
(Rule : in out Conditional_Expressions_Rule_Type)
is
begin
Process_Rule_Parameter
(Rule => Rule,
Param => "Except_Assertions",
Enable => True,
Defined_At => "");
end Activate_In_Test_Mode;
----------------------------------------------
-- Exception_Name (Conditional_Expressions) --
----------------------------------------------
overriding function Exception_Name
(Rule : Conditional_Expressions_Rule_Type;
Exc_Index : Exception_Index)
return String
is
pragma Unreferenced (Rule);
begin
case Exc_Index is
when 1 =>
return "Except_Assertions";
when others =>
return "";
end case;
end Exception_Name;
------------------------------------------------
-- Exception_Number (Conditional_Expressions) --
------------------------------------------------
overriding function Exception_Number
(Rule : Conditional_Expressions_Rule_Type;
Exc_Name : String)
return Exception_Numbers
is
pragma Unreferenced (Rule);
Result : Exception_Numbers := Not_An_Exception;
Normalized_Exc_Name : constant String := To_Lower (Exc_Name);
begin
if Normalized_Exc_Name = "except_assertions" then
Result := 1;
end if;
return Result;
end Exception_Number;
------------------------------------------
-- Init_Rule (Conditional_Expressions) --
------------------------------------------
overriding procedure Init_Rule
(Rule : in out Conditional_Expressions_Rule_Type)
is
begin
Init_Rule (Rule_With_Exceptions_Template (Rule));
Rule.Name := new String'("Conditional_Expressions");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("Ada 2012 conditional expressions");
Rule.Diagnosis := new String'("conditional expression");
end Init_Rule;
-------------------------------------------------
-- Rule_Check_Pre_Op (Conditional_Expressions) --
-------------------------------------------------
overriding procedure Rule_Check_Pre_Op
(Rule : in out Conditional_Expressions_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
Enclosing_El : Asis.Element;
Steps_Up : Elmt_Idx;
pragma Unreferenced (Control);
begin
if Expression_Kind (Element) in
A_Case_Expression |
An_If_Expression
then
if Rule.Exceptions (1) then
Enclosing_El := Get_Enclosing_Element;
Steps_Up := 1;
while Element_Kind (Enclosing_El) not in
A_Pragma |
A_Declaration |
A_Definition |
A_Statement
loop
Enclosing_El := Get_Enclosing_Element (Steps_Up);
Steps_Up := Steps_Up + 1;
end loop;
if not Is_Assertion (Enclosing_El) then
State.Detected := True;
end if;
else
State.Detected := True;
end if;
end if;
end Rule_Check_Pre_Op;
----------------------------------
-- Deep_Inheritance_Hierarchies --
----------------------------------
----------------------------------------------
-- Init_Rule (Deep_Inheritance_Hierarchies) --
----------------------------------------------
procedure Init_Rule (Rule : in out Deep_Inheritance_Hierarchies_Rule_Type)
is
begin
Init_Rule (One_Integer_Parameter_Rule_Template (Rule));
Rule.Rule_Bound := -1;
Rule.Name := new String'("Deep_Inheritance_Hierarchies");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("derivation tree is too deep");
Rule.Diagnosis := new String'("derivation tree is too deep (%1%)");
Rule.Check_In_Expanded_Generics := True;
end Init_Rule;
------------------------------------------------------
-- Rule_Check_Pre_Op (Deep_Inheritance_Hierarchies) --
------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Deep_Inheritance_Hierarchies_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control);
Type_Def : Asis.Element := Nil_Element;
Depth : Natural;
begin
if Declaration_Kind (Element) = An_Ordinary_Type_Declaration then
Type_Def := Type_Declaration_View (Element);
end if;
if Type_Kind (Type_Def) = A_Derived_Record_Extension_Definition
or else
Type_Kind (Type_Def) = A_Tagged_Record_Type_Definition
or else
Type_Kind (Type_Def) = An_Interface_Type_Definition
then
Depth := Inheritance_Depth (Type_Def);
if Depth > Rule.Rule_Limit then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" & ASIS_UL.Misc.Image (Depth));
end if;
end if;
end Rule_Check_Pre_Op;
----------------------------
-- Deeply_Nested_Generics --
----------------------------
----------------------------------------
-- Init_Rule (Deeply_Nested_Generics) --
----------------------------------------
procedure Init_Rule (Rule : in out Deeply_Nested_Generics_Rule_Type)
is
begin
Init_Rule (One_Integer_Parameter_Rule_Template (Rule));
Rule.Rule_Bound := 0;
Rule.Name := new String'("Deeply_Nested_Generics");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("deeply nested generic declarations");
Rule.Diagnosis := new String'("deeply nested generic (%1%)");
end Init_Rule;
------------------------------------------------
-- Rule_Check_Pre_Op (Deeply_Nested_Generics) --
------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Deeply_Nested_Generics_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control);
Generic_Nesting_Level : Natural := 0;
Encl_El : Asis.Element;
begin
if Declaration_Kind (Element) in A_Generic_Declaration then
Encl_El := Enclosing_Element (Element);
while not Is_Nil (Encl_El) loop
case Declaration_Kind (Encl_El) is
when A_Generic_Declaration =>
Generic_Nesting_Level := Generic_Nesting_Level + 1;
when Not_A_Declaration |
A_Procedure_Body_Declaration |
A_Function_Body_Declaration |
A_Package_Body_Declaration |
A_Task_Body_Declaration |
A_Protected_Body_Declaration |
An_Entry_Body_Declaration =>
exit;
when others =>
null;
end case;
Encl_El := Enclosing_Element (Encl_El);
end loop;
if Generic_Nesting_Level > Rule.Rule_Limit then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" &
ASIS_UL.Misc.Image (Generic_Nesting_Level));
end if;
end if;
end Rule_Check_Pre_Op;
----------------------------------
-- Deeply_Nested_Local_Inlining --
----------------------------------
----------------------------------------------
-- Init_Rule (Deeply_Nested_Local_Inlining) --
----------------------------------------------
procedure Init_Rule (Rule : in out Deeply_Nested_Local_Inlining_Rule_Type)
is
begin
Init_Rule (One_Integer_Parameter_Rule_Template (Rule));
Rule.Name := new String'("Deeply_Nested_Local_Inlining");
Rule.Rule_Status := Non_Documented;
Rule.Help_Info := new String'("deeply nested inlining (local check)");
Rule.Diagnosis := new String'("deeply nested inlining (local check)");
end Init_Rule;
------------------------------------------------------
-- Rule_Check_Pre_Op (Deeply_Nested_Local_Inlining) --
------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Deeply_Nested_Local_Inlining_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control);
Local_State : Natural := 0;
Local_Control : Traverse_Control := Continue;
procedure Check_Inlined_Body_Pre_Op
(Element : Asis.Element;
Local_Control : in out Traverse_Control;
Local_State : in out Natural);
-- Checks nesting inlining. That is, if Element is a call to a
-- subprogram that is inlined (has a pragma Inline applied to it and
-- located in the same body as Rule_Check_Pre_Op.Element), adds 1 to
-- Local_State and processes the body of the called inlined subprogram.
-- If Local_State exceeds the rule limit, sets Control
procedure Check_Inlined_Body_Post_Op
(Element : Asis.Element;
Local_Control : in out Traverse_Control;
Local_State : in out Natural);
-- Does nothing
procedure Check_Local_Inlinings is new Traverse_Element
(State_Information => Natural,
Pre_Operation => Check_Inlined_Body_Pre_Op,
Post_Operation => Check_Inlined_Body_Post_Op);
procedure Check_Inlined_Body_Pre_Op
(Element : Asis.Element;
Local_Control : in out Traverse_Control;
Local_State : in out Natural)
is
Tmp : Asis.Element;
begin
if Statement_Kind (Element) = A_Procedure_Call_Statement
or else
Expression_Kind (Element) = A_Function_Call
then
Tmp := Get_Called_Element (Element);
if Has_Pragma_Inline (Tmp) then
-- This means that Tmp is not Nil_Element
Tmp :=
ASIS_UL.Global_State.Utilities.Corresponding_Element (Tmp);
if not Is_Nil (Tmp) then
Tmp := Corresponding_Body (Tmp);
if not Is_Nil (Tmp) then
Local_State := Local_State + 1;
if Local_State > Rule.Rule_Limit then
State.Detected := True;
Local_Control := Terminate_Immediately;
else
Check_Local_Inlinings
(Tmp, Local_Control, Local_State);
end if;
Local_State := Local_State - 1;
end if;
end if;
end if;
end if;
end Check_Inlined_Body_Pre_Op;
procedure Check_Inlined_Body_Post_Op
(Element : Asis.Element;
Local_Control : in out Traverse_Control;
Local_State : in out Natural)
is
begin
null;
end Check_Inlined_Body_Post_Op;
begin
if Declaration_Kind (Element) in
A_Procedure_Body_Declaration .. A_Function_Body_Declaration
and then
Has_Pragma_Inline (Element)
then
Check_Local_Inlinings (Element, Local_Control, Local_State);
end if;
end Rule_Check_Pre_Op;
--------------------------------
-- Direct_Calls_To_Primitives --
--------------------------------
--------------------------------------------------------
-- Activate_In_Test_Mode (Direct_Calls_To_Primitives) --
--------------------------------------------------------
overriding procedure Activate_In_Test_Mode
(Rule : in out Direct_Calls_To_Primitives_Rule_Type)
is
begin
Process_Rule_Parameter
(Rule => Rule,
Param => "Except_Constructors",
Enable => True,
Defined_At => "");
end Activate_In_Test_Mode;
-------------------------------------------------
-- Exception_Name (Direct_Calls_To_Primitives) --
-------------------------------------------------
overriding function Exception_Name
(Rule : Direct_Calls_To_Primitives_Rule_Type;
Exc_Index : Exception_Index)
return String
is
pragma Unreferenced (Rule);
begin
case Exc_Index is
when 1 =>
return "Except_Constructors";
when others =>
return "";
end case;
end Exception_Name;
---------------------------------------------------
-- Exception_Number (Direct_Calls_To_Primitives) --
---------------------------------------------------
overriding function Exception_Number
(Rule : Direct_Calls_To_Primitives_Rule_Type;
Exc_Name : String)
return Exception_Numbers
is
pragma Unreferenced (Rule);
Result : Exception_Numbers := Not_An_Exception;
Normalized_Exc_Name : constant String := To_Lower (Exc_Name);
begin
if Normalized_Exc_Name = "except_constructors" then
Result := 1;
end if;
return Result;
end Exception_Number;
--------------------------------------------
-- Init_Rule (Direct_Calls_To_Primitives) --
--------------------------------------------
overriding procedure Init_Rule
(Rule : in out Direct_Calls_To_Primitives_Rule_Type)
is
begin
Init_Rule (Rule_With_Exceptions_Template (Rule));
Rule.Name := new String'("Direct_Calls_To_Primitives");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("non-dispatching calls to primitives");
Rule.Diagnosis :=
new String'("non-dispatching call to primitive operation");
end Init_Rule;
------------------------------------------------
-- Rule_Check_Pre_Op (Direct_Calls_To_Primitives) --
------------------------------------------------
overriding procedure Rule_Check_Pre_Op
(Rule : in out Direct_Calls_To_Primitives_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control);
Called_Routine : Asis.Element;
Calling_Context : Asis.Element;
Owner : Asis.Element;
Steps_Up : Elmt_Idx;
begin
if (Statement_Kind (Element) = A_Procedure_Call_Statement
or else
Expression_Kind (Element) = A_Function_Call)
and then
not Is_Dispatching_Call (Element)
then
Called_Routine := Get_Called_Element (Element);
if Declaration_Kind (Called_Routine) in
A_Procedure_Renaming_Declaration .. A_Function_Renaming_Declaration
then
Called_Routine := Get_Renamed_Subprogram (Called_Routine);
end if;
if Is_Dispatching_Operation (Called_Routine) then
State.Detected := True;
if Is_Part_Of_Inherited (Called_Routine) then
Owner := Corresponding_Type (Called_Routine);
else
Owner := Primitive_Owner (Called_Routine);
end if;
if Definition_Kind (Owner) = A_Private_Type_Definition
and then
not Full_View_Visible
(Type_Decl => Enclosing_Element (Owner),
At_Place => Element)
then
-- At the place of the call we have an operation of non-tagged
-- private type, so this call should not be considered as
-- a dispatching call
Reset_State (State);
end if;
-- And now we have to check if we have one of the possible
-- exception cases.
-- 1. We have the exception Except_Constructors ON, and we have
-- a call to a constructor function.
if State.Detected
and then
Rule.Exceptions (1)
and then
Expression_Kind (Element) = A_Function_Call
and then
Is_Constructor_Function (Called_Routine)
then
Reset_State (State);
end if;
-- 2. We are in the situation when (grand)parent's primitive is
-- called in the body of overriding child's primitive.
if State.Detected then
Calling_Context := Get_Enclosing_Element;
Steps_Up := 1;
while not (Is_Nil (Calling_Context)
or else
Is_Body (Calling_Context))
loop
Calling_Context := Get_Enclosing_Element (Steps_Up);
Steps_Up := Steps_Up + 1;
end loop;
if Declaration_Kind (Calling_Context) in
A_Procedure_Body_Declaration .. A_Function_Body_Declaration
then
Calling_Context :=
Corresponding_Declaration (Calling_Context);
if Is_Overriding_Operation (Calling_Context) then
Calling_Context :=
Corresponding_Overridden_Operation (Calling_Context);
if Is_Equal (Calling_Context, Called_Routine) then
Reset_State (State);
end if;
end if;
end if;
end if;
end if;
end if;
end Rule_Check_Pre_Op;
----------------------------------
-- Exits_From_Conditional_Loops --
----------------------------------
----------------------------------------------
-- Init_Rule (Exits_From_Conditional_Loops) --
----------------------------------------------
procedure Init_Rule (Rule : in out Exits_From_Conditional_Loops_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Exits_From_Conditional_Loops");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("exit from conditional loops");
Rule.Diagnosis := new String'("exit from conditional loop");
end Init_Rule;
---------------------------------------------------------
-- Rule_Check_Pre_Op (Exits_From_Conditional_Loops) --
---------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Exits_From_Conditional_Loops_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
Loop_Exited : Asis.Element;
Tmp : Asis.Element;
pragma Unreferenced (Rule, Control);
begin
if Statement_Kind (Element) = An_Exit_Statement then
Loop_Exited := Corresponding_Loop_Exited (Element);
if Statement_Kind (Loop_Exited) in
A_While_Loop_Statement .. A_For_Loop_Statement
then
State.Detected := True;
else
Tmp := Enclosing_Element (Element);
while not Is_Equal (Tmp, Loop_Exited) loop
if Statement_Kind (Tmp) in
A_While_Loop_Statement .. A_For_Loop_Statement
then
State.Detected := True;
exit;
end if;
Tmp := Enclosing_Element (Tmp);
end loop;
end if;
end if;
end Rule_Check_Pre_Op;
----------------------
-- Global_Variables --
----------------------
----------------------------------------------
-- Activate_In_Test_Mode (Global_Variables) --
----------------------------------------------
overriding procedure Activate_In_Test_Mode
(Rule : in out Global_Variables_Rule_Type)
is
begin
Process_Rule_Parameter
(Rule => Rule,
Param => "Only_Public",
Enable => True,
Defined_At => "");
end Activate_In_Test_Mode;
---------------------------------------
-- Exception_Name (Global_Variables) --
---------------------------------------
overriding function Exception_Name
(Rule : Global_Variables_Rule_Type;
Exc_Index : Exception_Index)
return String
is
pragma Unreferenced (Rule);
begin
case Exc_Index is
when 1 =>
return "Only_Public";
when others =>
return "";
end case;
end Exception_Name;
-----------------------------------------
-- Exception_Number (Global_Variables) --
-----------------------------------------
overriding function Exception_Number
(Rule : Global_Variables_Rule_Type;
Exc_Name : String)
return Exception_Numbers
is
pragma Unreferenced (Rule);
Result : Exception_Numbers := Not_An_Exception;
Normalized_Exc_Name : constant String := To_Lower (Exc_Name);
begin
if Normalized_Exc_Name = "only_public" then
Result := 1;
end if;
return Result;
end Exception_Number;
----------------------------------
-- Init_Rule (Global_Variables) --
----------------------------------
overriding procedure Init_Rule
(Rule : in out Global_Variables_Rule_Type)
is
begin
Init_Rule (Rule_With_Exceptions_Template (Rule));
Rule.Name := new String'("Global_Variables");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("declarations of global variables");
Rule.Diagnosis := new String'("declaration of global variable");
end Init_Rule;
------------------------------------------
-- Rule_Check_Pre_Op (Global_Variables) --
------------------------------------------
overriding procedure Rule_Check_Pre_Op
(Rule : in out Global_Variables_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
Encl_Unit : Asis.Element;
pragma Unreferenced (Control);
begin
if Declaration_Kind (Element) = A_Variable_Declaration then
Encl_Unit := Get_Enclosing_Element;
if Declaration_Kind (Encl_Unit) in
A_Package_Declaration | A_Generic_Package_Declaration
and then
Is_Nil (Enclosing_Element (Encl_Unit))
then
declare
Encl_CUnit : constant Asis.Compilation_Unit :=
Enclosing_Compilation_Unit (Element);
begin
if Rule.Exceptions (1) then
if Unit_Class (Encl_CUnit) /= A_Private_Declaration then
declare
Public_Dcls : constant Asis.Element_List :=
Visible_Part_Declarative_Items (Encl_Unit);
Last_Public_Dcl : Asis.Element;
Last_Public_Span : Span;
Arg_Span : Span;
begin
if not Is_Nil (Public_Dcls) then
Last_Public_Dcl := Public_Dcls (Public_Dcls'Last);
Last_Public_Span := Element_Span (Last_Public_Dcl);
Arg_Span := Element_Span (Element);
State.Detected :=
Arg_Span.Last_Line < Last_Public_Span.First_Line
or else
-- pathological case
(Arg_Span.Last_Line = Last_Public_Span.First_Line
and then
Arg_Span.First_Column <=
Last_Public_Span.Last_Column);
end if;
end;
end if;
else
State.Detected := True;
end if;
end;
end if;
end if;
end Rule_Check_Pre_Op;
------------------------
-- Maximum_Parameters --
------------------------
------------------------------------
-- Init_Rule (Maximum_Parameters) --
------------------------------------
overriding procedure Init_Rule (Rule : in out Maximum_Parameters_Rule_Type)
is
begin
Init_Rule (One_Integer_Parameter_Rule_Template (Rule));
Rule.Name := new String'("Maximum_Parameters");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String
'("maxumum number of subprogram parameters");
Rule.Diagnosis := new String'("too many formal parameters (%1%)");
end Init_Rule;
--------------------------------------------
-- Rule_Check_Pre_Op (Maximum_Parameters) --
--------------------------------------------
overriding procedure Rule_Check_Pre_Op
(Rule : in out Maximum_Parameters_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control);
begin
if not Is_Subunit (Element)
and then
(Declaration_Kind (Element) in
A_Procedure_Declaration |
A_Function_Declaration |
A_Generic_Procedure_Declaration |
A_Generic_Function_Declaration
or else
(Declaration_Kind (Element) in
A_Procedure_Body_Declaration |
A_Function_Body_Declaration |
A_Null_Procedure_Declaration |
An_Expression_Function_Declaration |
A_Procedure_Body_Stub |
A_Function_Body_Stub
and then
Is_Nil (Corresponding_Declaration (Element))))
then
declare
Params : constant Asis.Element_List := Parameter_Profile (Element);
Par_N : Natural := 0;
begin
for J in Params'Range loop
Par_N := Par_N + Names (Params (J))'Length;
end loop;
if Par_N > Rule.Rule_Limit then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" & ASIS_UL.Misc.Image (Par_N));
end if;
end;
end if;
end Rule_Check_Pre_Op;
----------------------
-- Membership_Tests --
----------------------
----------------------------------------------
-- Activate_In_Test_Mode (Membership_Tests) --
----------------------------------------------
overriding procedure Activate_In_Test_Mode
(Rule : in out Membership_Tests_Rule_Type)
is
begin
Process_Rule_Parameter
(Rule => Rule,
Param => "Except_Assertions",
Enable => True,
Defined_At => "");
Process_Rule_Parameter
(Rule => Rule,
Param => "Multi_Alternative_Only",
Enable => True,
Defined_At => "");
end Activate_In_Test_Mode;
---------------------------------------
-- Exception_Name (Membership_Tests) --
---------------------------------------
overriding function Exception_Name
(Rule : Membership_Tests_Rule_Type;
Exc_Index : Exception_Index)
return String
is
pragma Unreferenced (Rule);
begin
case Exc_Index is
when 1 =>
return "Except_Assertions";
when 2 =>
return "Multi_Alternative_Only";
when others =>
return "";
end case;
end Exception_Name;
-----------------------------------------
-- Exception_Number (Membership_Tests) --
-----------------------------------------
overriding function Exception_Number
(Rule : Membership_Tests_Rule_Type;
Exc_Name : String)
return Exception_Numbers
is
pragma Unreferenced (Rule);
Result : Exception_Numbers := Not_An_Exception;
Normalized_Exc_Name : constant String := To_Lower (Exc_Name);
begin
if Normalized_Exc_Name = "except_assertions" then
Result := 1;
elsif Normalized_Exc_Name = "multi_alternative_only" then
Result := 2;
end if;
return Result;
end Exception_Number;
----------------------------------
-- Init_Rule (Membership_Tests) --
----------------------------------
overriding procedure Init_Rule
(Rule : in out Membership_Tests_Rule_Type)
is
begin
Init_Rule (Rule_With_Exceptions_Template (Rule));
Rule.Name := new String'("Membership_Tests");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("membership test");
Rule.Diagnosis := new String'("membership test");
end Init_Rule;
------------------------------------------
-- Rule_Check_Pre_Op (Membership_Tests) --
------------------------------------------
overriding procedure Rule_Check_Pre_Op
(Rule : in out Membership_Tests_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
Detected : Boolean := False;
Enclosing_El : Asis.Element;
Steps_Up : Elmt_Idx;
pragma Unreferenced (Control);
begin
Detected := Expression_Kind (Element) in
An_In_Membership_Test | A_Not_In_Membership_Test;
if Detected and then Rule.Exceptions (2) then
Detected := Membership_Test_Choices (Element)'Length > 1;
end if;
if Detected then
if Rule.Exceptions (1) then
Enclosing_El := Get_Enclosing_Element;
Steps_Up := 1;
while Element_Kind (Enclosing_El) not in
A_Pragma |
A_Declaration |
A_Definition |
A_Statement
loop
Enclosing_El := Get_Enclosing_Element (Steps_Up);
Steps_Up := Steps_Up + 1;
end loop;
if not Is_Assertion (Enclosing_El) then
State.Detected := True;
end if;
else
State.Detected := True;
end if;
end if;
end Rule_Check_Pre_Op;
------------------------------------
-- Misnamed_Controlling_Parameter --
------------------------------------
-------------------------------------------------
-- Init_Rule (Misnamed_Controlling_Parameters) --
-------------------------------------------------
procedure Init_Rule
(Rule : in out Misnamed_Controlling_Parameters_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Misnamed_Controlling_Parameters");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("Badly formatted profile of " &
"a primitive operation");
Rule.Diagnosis := new String'("#1#first parameter should be " &
"of type %1%" &
"#2#first parameter should have " &
"name 'This'");
end Init_Rule;
---------------------------------------------------------
-- Rule_Check_Pre_Op (Misnamed_Controlling_Parameters) --
---------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Misnamed_Controlling_Parameters_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
if Is_Dispatching_Operation (Element) then
-- Filter out the cases we are not interested in:
case Declaration_Kind (Element) is
when A_Procedure_Body_Declaration |
A_Function_Body_Declaration |
A_Procedure_Body_Stub |
A_Function_Body_Stub =>
if not Is_Nil (Corresponding_Declaration (Element)) then
return;
end if;
when others =>
null;
end case;
declare
This : Asis.Element;
Params : constant Asis.Element_List := Parameter_Profile (Element);
Owner : constant Asis.Element :=
First_Name (Enclosing_Element (Primitive_Owner (Element)));
No_Control_Par : Boolean := True;
-- Used to check if there is a case of a function with a
-- controlling result and no control parameter
begin
if Is_Nil (Params) then
-- The only possibility is a parameterless function with
-- controlling result, no rule violation in this case
return;
end if;
This := First_Name (Params (Params'First));
if To_Lower (To_String (Defining_Name_Image (This))) /= "this" then
State.Detected := True;
State.Diagnosis := 2;
else
This := Object_Declaration_View (Params (Params'First));
if Definition_Kind (This) = An_Access_Definition then
This := Anonymous_Access_To_Object_Subtype_Mark (This);
end if;
This := Normalize_Reference (This);
This := Corresponding_Name_Declaration (This);
if Declaration_Kind (This) = A_Subtype_Declaration then
This := Corresponding_First_Subtype (This);
end if;
This := First_Name (This);
if not (Defining_Name_Kind (This) = A_Defining_Identifier
and then
Defining_Name_Image (This) =
Defining_Name_Image (Owner))
then
State.Detected := True;
State.Diagnosis := 1;
State.Diag_Params :=
Enter_String ("%1%" &
To_String (Defining_Name_Image (Owner)));
end if;
end if;
-- Filter out the case of a function with controlling result and
-- no parameter of tagged type
if State.Detected
and then
Declaration_Kind (Element) in A_Function_Declaration |
A_Function_Body_Declaration |
A_Function_Renaming_Declaration |
A_Function_Body_Stub |
An_Expression_Function_Declaration
then
for J in Params'Range loop
This := Object_Declaration_View (Params (J));
if Definition_Kind (This) = An_Access_Definition then
This := Anonymous_Access_To_Object_Subtype_Mark (This);
end if;
if Expression_Kind (This) /= An_Attribute_Reference then
This := Normalize_Reference (This);
This := Corresponding_Name_Declaration (This);
This := Corresponding_First_Subtype (This);
This := First_Name (This);
if Is_Equal (This, Owner) then
No_Control_Par := False;
exit;
end if;
end if;
end loop;
if No_Control_Par then
-- Not a rule violation, function with no controlling
-- parameters
Reset_State (State);
end if;
end if;
end;
end if;
end Rule_Check_Pre_Op;
----------------------------------------
-- No_Scalar_Storage_Order_Specified --
----------------------------------------
---------------------------------------------------
-- Init_Rule (No_Scalar_Storage_Order_Specified) --
---------------------------------------------------
overriding procedure Init_Rule
(Rule : in out No_Scalar_Storage_Order_Specified_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("No_Scalar_Storage_Order_Specified");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("a record/record extension type has " &
"record representation clause but no " &
"Scalar_Storage_Order attribute clause");
Rule.Diagnosis := new String'
("#1#Scalar_Storage_Order is not specified" &
"#2#Scalar_Storage_Order is not specified " &
"(Bit_Order is Low_Order_First)" &
"#3#Scalar_Storage_Order is not specified " &
"(Bit_Order is High_Order_First)");
end Init_Rule;
-----------------------------------------------------------
-- Rule_Check_Pre_Op (No_Scalar_Storage_Order_Specified) --
-----------------------------------------------------------
overriding procedure Rule_Check_Pre_Op
(Rule : in out No_Scalar_Storage_Order_Specified_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control, Rule);
Type_Def : Asis.Element;
Is_Record_Type : Boolean := False;
Asp_Mark : Asis.Element;
begin
if Declaration_Kind (Element) = An_Ordinary_Type_Declaration then
Type_Def := Type_Declaration_View (Element);
case Type_Kind (Type_Def) is
when A_Derived_Record_Extension_Definition |
A_Record_Type_Definition |
A_Tagged_Record_Type_Definition =>
Is_Record_Type := True;
when A_Derived_Type_Definition =>
Is_Record_Type :=
Type_Kind (Type_Declaration_View
(Corresponding_Root_Type (Type_Def))) =
A_Record_Type_Definition;
when others =>
null;
end case;
if Is_Record_Type then
declare
CRC : constant Asis.Element_List :=
Corresponding_Representation_Items (First_Name (Element));
Rec_Rep_Clause_Found : Boolean := False;
Scalar_Storage_Order_Clause_Found : Boolean := False;
Bit_Order_Low : Boolean := False;
Bit_Order_High : Boolean := False;
Attr : Asis.Element;
begin
for J in CRC'Range loop
case Flat_Element_Kind (CRC (J)) is
when A_Record_Representation_Clause =>
if Component_Clauses
(CRC (J), Include_Pragmas => False)'Length > 0
then
Rec_Rep_Clause_Found := True;
exit when Scalar_Storage_Order_Clause_Found
and then
(Bit_Order_Low or else Bit_Order_High);
end if;
when An_Attribute_Definition_Clause =>
Attr := Representation_Clause_Name (CRC (J));
if Attribute_Kind (Attr) =
An_Implementation_Defined_Attribute
then
Attr := Attribute_Designator_Identifier (Attr);
if To_Lower_Case (Name_Image (Attr)) =
"scalar_storage_order"
then
Scalar_Storage_Order_Clause_Found := True;
exit when Rec_Rep_Clause_Found
and then
(Bit_Order_Low or else Bit_Order_High);
end if;
elsif Attribute_Kind (Attr) =
A_Bit_Order_Attribute
then
Attr := Representation_Clause_Expression (CRC (J));
Attr := Normalize_Reference (Attr);
if To_Lower_Case (Name_Image (Attr)) =
"high_order_first"
then
Bit_Order_High := True;
elsif To_Lower_Case (Name_Image (Attr)) =
"low_order_first"
then
Bit_Order_Low := True;
end if;
end if;
when An_Aspect_Specification =>
Asp_Mark := Aspect_Mark (CRC (J));
if To_Lower_Case (Name_Image (Asp_Mark)) =
"scalar_storage_order"
then
Scalar_Storage_Order_Clause_Found := True;
exit when Rec_Rep_Clause_Found
and then
(Bit_Order_Low or else Bit_Order_High);
elsif To_Lower_Case (Name_Image (Asp_Mark)) =
"bit_order"
then
Attr := Aspect_Definition (CRC (J));
Attr := Normalize_Reference (Attr);
if To_Lower_Case (Name_Image (Attr)) =
"high_order_first"
then
Bit_Order_High := True;
elsif To_Lower_Case (Name_Image (Attr)) =
"low_order_first"
then
Bit_Order_Low := True;
end if;
end if;
when others =>
null;
end case;
end loop;
if Rec_Rep_Clause_Found
and then
not Scalar_Storage_Order_Clause_Found
then
Scalar_Storage_Order_Clause_Found :=
Storage_Order_Defined_By_Pragma (Element);
end if;
if Rec_Rep_Clause_Found
and then
not Scalar_Storage_Order_Clause_Found
then
State.Detected := True;
if Bit_Order_Low then
State.Diagnosis := 2;
elsif Bit_Order_High then
State.Diagnosis := 3;
else
State.Diagnosis := 1;
end if;
end if;
end;
end if;
end if;
end Rule_Check_Pre_Op;
------------------------
-- Predicate_Testing --
------------------------
-----------------------------------------------
-- Activate_In_Test_Mode (Predicate_Testing) --
-----------------------------------------------
overriding procedure Activate_In_Test_Mode
(Rule : in out Predicate_Testing_Rule_Type)
is
begin
Process_Rule_Parameter
(Rule => Rule,
Param => "Except_Assertions",
Enable => True,
Defined_At => "");
end Activate_In_Test_Mode;
----------------------------------------
-- Exception_Name (Predicate_Testing) --
----------------------------------------
overriding function Exception_Name
(Rule : Predicate_Testing_Rule_Type;
Exc_Index : Exception_Index)
return String
is
pragma Unreferenced (Rule);
begin
case Exc_Index is
when 1 =>
return "Except_Assertions";
when others =>
return "";
end case;
end Exception_Name;
------------------------------------------
-- Exception_Number (Predicate_Testing) --
------------------------------------------
overriding function Exception_Number
(Rule : Predicate_Testing_Rule_Type;
Exc_Name : String)
return Exception_Numbers
is
pragma Unreferenced (Rule);
Result : Exception_Numbers := Not_An_Exception;
Normalized_Exc_Name : constant String := To_Lower (Exc_Name);
begin
if Normalized_Exc_Name = "except_assertions" then
Result := 1;
end if;
return Result;
end Exception_Number;
-----------------------------------
-- Init_Rule (Predicate_Testing) --
-----------------------------------
overriding procedure Init_Rule
(Rule : in out Predicate_Testing_Rule_Type)
is
begin
Init_Rule (Rule_With_Exceptions_Template (Rule));
Rule.Name := new String'("Predicate_Testing");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("expressions need subtype predicate" &
"evaluation");
Rule.Diagnosis := new String'("#1#membership choice needs subtype " &
"predicate evaluation" &
"#2#attribute 'Valid needs subtype " &
"predicate evaluation");
end Init_Rule;
-------------------------------------------
-- Rule_Check_Pre_Op (Predicate_Testing) --
-------------------------------------------
overriding procedure Rule_Check_Pre_Op
(Rule : in out Predicate_Testing_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
Detected : Boolean := False;
Diag_Var : Diagnosis_Variant range 1 .. 2;
Enclosing_El : Asis.Element;
Steps_Up : Elmt_Idx;
pragma Unreferenced (Control);
begin
if Expression_Kind (Element) in An_Identifier | A_Selected_Component
and then
Expression_Kind (Get_Enclosing_Element) in
An_In_Membership_Test | A_Not_In_Membership_Test
and then
Denotes_Subtype_With_Predicate (Element)
then
Detected := True;
Diag_Var := 1;
elsif Attribute_Kind (Element) = A_Valid_Attribute
and then
From_Subtype_With_Predicate (Prefix (Element))
then
Detected := True;
Diag_Var := 2;
end if;
if Detected then
if Rule.Exceptions (1) then
Enclosing_El := Get_Enclosing_Element;
Steps_Up := 1;
while Element_Kind (Enclosing_El) not in
A_Pragma |
A_Declaration |
A_Definition |
A_Statement
loop
Enclosing_El := Get_Enclosing_Element (Steps_Up);
Steps_Up := Steps_Up + 1;
end loop;
if not Is_Assertion (Enclosing_El) then
State.Detected := True;
end if;
else
State.Detected := True;
end if;
if State.Detected then
State.Diagnosis := Diag_Var;
end if;
end if;
end Rule_Check_Pre_Op;
----------------------------
-- Quantified_Expressions --
----------------------------
----------------------------------------------------
-- Activate_In_Test_Mode (Quantified_Expressions) --
----------------------------------------------------
overriding procedure Activate_In_Test_Mode
(Rule : in out Quantified_Expressions_Rule_Type)
is
begin
Process_Rule_Parameter
(Rule => Rule,
Param => "Except_Assertions",
Enable => True,
Defined_At => "");
end Activate_In_Test_Mode;
---------------------------------------------
-- Exception_Name (Quantified_Expressions) --
---------------------------------------------
overriding function Exception_Name
(Rule : Quantified_Expressions_Rule_Type;
Exc_Index : Exception_Index)
return String
is
pragma Unreferenced (Rule);
begin
case Exc_Index is
when 1 =>
return "Except_Assertions";
when others =>
return "";
end case;
end Exception_Name;
-----------------------------------------------
-- Exception_Number (Quantified_Expressions) --
-----------------------------------------------
overriding function Exception_Number
(Rule : Quantified_Expressions_Rule_Type;
Exc_Name : String)
return Exception_Numbers
is
pragma Unreferenced (Rule);
Result : Exception_Numbers := Not_An_Exception;
Normalized_Exc_Name : constant String := To_Lower (Exc_Name);
begin
if Normalized_Exc_Name = "except_assertions" then
Result := 1;
end if;
return Result;
end Exception_Number;
----------------------------------------
-- Init_Rule (Quantified_Expressions) --
----------------------------------------
overriding procedure Init_Rule
(Rule : in out Quantified_Expressions_Rule_Type)
is
begin
Init_Rule (Rule_With_Exceptions_Template (Rule));
Rule.Name := new String'("Quantified_Expressions");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("Ada 2012 quantified expressions");
Rule.Diagnosis := new String'("quantified expression");
end Init_Rule;
------------------------------------------------
-- Rule_Check_Pre_Op (Quantified_Expressions) --
------------------------------------------------
overriding procedure Rule_Check_Pre_Op
(Rule : in out Quantified_Expressions_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
Enclosing_El : Asis.Element;
Steps_Up : Elmt_Idx;
pragma Unreferenced (Control);
begin
if Expression_Kind (Element) in
A_For_All_Quantified_Expression |
A_For_Some_Quantified_Expression
then
if Rule.Exceptions (1) then
Enclosing_El := Get_Enclosing_Element;
Steps_Up := 1;
while Element_Kind (Enclosing_El) not in
A_Pragma |
A_Declaration |
A_Definition |
A_Statement
loop
Enclosing_El := Get_Enclosing_Element (Steps_Up);
Steps_Up := Steps_Up + 1;
end loop;
if not Is_Assertion (Enclosing_El) then
State.Detected := True;
end if;
else
State.Detected := True;
end if;
end if;
null;
end Rule_Check_Pre_Op;
-------------------------------------
-- Separate_Numeric_Error_Handlers --
-------------------------------------
-------------------------------------------------
-- Init_Rule (Separate_Numeric_Error_Handlers) --
-------------------------------------------------
procedure Init_Rule
(Rule : in out Separate_Numeric_Error_Handlers_Rule_Type)
is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Separate_Numeric_Error_Handlers");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("Numeric_Error and Constraint error " &
"are not handled together");
Rule.Diagnosis := new String'("#1#Numeric_Error is handled " &
"separately from Constraint_Error" &
"#2#Constraint_Error is handled " &
"separately from Numeric_Error");
end Init_Rule;
---------------------------------------------------------
-- Rule_Check_Pre_Op (Separate_Numeric_Error_Handlers) --
---------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Separate_Numeric_Error_Handlers_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
if Element_Kind (Element) = An_Exception_Handler then
declare
Choices : constant Asis.Element_List :=
Exception_Choices (Element);
Next_Choice : Asis.Element;
Constraint_Error_Present : Boolean := False;
Numeric_Error_Present : Boolean := False;
begin
for J in Choices'Range loop
Next_Choice := Normalize_Reference (Choices (J));
exit when Definition_Kind (Next_Choice) = An_Others_Choice;
if not Constraint_Error_Present then
Constraint_Error_Present :=
Is_Constraint_Error (Next_Choice);
end if;
if not Numeric_Error_Present then
Numeric_Error_Present := Is_Numeric_Error (Next_Choice);
end if;
exit when Constraint_Error_Present
and then
Numeric_Error_Present;
end loop;
if Constraint_Error_Present and then not Numeric_Error_Present then
State.Detected := True;
State.Diagnosis := 2;
elsif Numeric_Error_Present
and then
not Constraint_Error_Present
then
State.Detected := True;
State.Diagnosis := 1;
end if;
end;
end if;
end Rule_Check_Pre_Op;
----------------------
-- Too_Many_Parents --
----------------------
----------------------------------
-- Init_Rule (Too_Many_Parents) --
----------------------------------
procedure Init_Rule (Rule : in out Too_Many_Parents_Rule_Type) is
begin
Init_Rule (One_Integer_Parameter_Rule_Template (Rule));
Rule.Name := new String'("Too_Many_Parents");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("type/object has too many parents");
Rule.Diagnosis :=
new String'("#1#type has too many parents (%1%)" &
"#2#task object has too many parents (%1%)" &
"#3#protected object has too many parents (%1%)");
end Init_Rule;
------------------------------------------
-- Rule_Check_Pre_Op (Too_Many_Parents) --
------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Too_Many_Parents_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control);
begin
if May_Have_Interface_List (Element) then
declare
Int_List : constant Asis.Element_List := Interface_List (Element);
begin
case Declaration_Kind (Element) is
when An_Ordinary_Type_Declaration |
A_Formal_Type_Declaration |
A_Private_Extension_Declaration =>
-- In case of an interface type declaration we do not have
-- a parent subtype, so we have to handle this case
-- separately:
if Type_Kind (Type_Declaration_View (Element)) =
An_Interface_Type_Definition
then
State.Detected := Int_List'Length > Rule.Rule_Limit;
State.Diagnosis := 1;
State.Diag_Params := Enter_String ("%1%" &
ASIS_UL.Misc.Image (Int_List'Length));
else
State.Detected := Int_List'Length + 1 > Rule.Rule_Limit;
State.Diagnosis := 1;
State.Diag_Params := Enter_String ("%1%" &
ASIS_UL.Misc.Image (Int_List'Length + 1));
end if;
when A_Task_Type_Declaration |
A_Protected_Type_Declaration |
A_Single_Task_Declaration |
A_Single_Protected_Declaration =>
if Int_List'Length > Rule.Rule_Limit then
State.Detected := True;
State.Diag_Params := Enter_String ("%1%" &
ASIS_UL.Misc.Image (Int_List'Length));
if Declaration_Kind (Element) =
A_Single_Task_Declaration
then
State.Diagnosis := 2;
elsif Declaration_Kind (Element) =
A_Single_Protected_Declaration
then
State.Diagnosis := 3;
else
State.Diagnosis := 1;
end if;
end if;
when others =>
pragma Assert (False);
null;
end case;
end;
end if;
end Rule_Check_Pre_Op;
-------------------------
-- Unconditional_Exits --
-------------------------
-------------------------------------
-- Init_Rule (Unconditional_Exits) --
-------------------------------------
procedure Init_Rule (Rule : in out Unconditional_Exits_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Unconditional_Exits");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("Exit statement with no condition");
Rule.Diagnosis := new String'("exit statement does not contain " &
"condition");
end Init_Rule;
---------------------------------------------
-- Rule_Check_Pre_Op (Unconditional_Exits) --
---------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Unconditional_Exits_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
if Statement_Kind (Element) = An_Exit_Statement
and then
Is_Nil (Exit_Condition (Element))
then
State.Detected := True;
end if;
end Rule_Check_Pre_Op;
------------------------
-- Visible_Components --
------------------------
------------------------------------
-- Init_Rule (Visible_Components) --
------------------------------------
procedure Init_Rule (Rule : in out Visible_Components_Rule_Type) is
begin
Init_Rule (Rule_Template (Rule));
Rule.Name := new String'("Visible_Components");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("Types with publicly accessible " &
"components");
Rule.Diagnosis := new String'("type defines publicly accessible " &
"components");
end Init_Rule;
--------------------------------------------
-- Rule_Check_Pre_Op (Visible_Components) --
--------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Visible_Components_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Rule, Control);
begin
if Defines_Components (Element)
and then
Is_Publically_Accessible (Element)
then
State.Detected := True;
end if;
end Rule_Check_Pre_Op;
end Gnatcheck.Rules.Custom_2;
|