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
|
------------------------------------------------------------------------------
-- --
-- GNATCHECK COMPONENTS --
-- --
-- ASIS_UL.SOURCE_TABLE.GNATCHECK_PROCESSING --
-- --
-- B o d y --
-- --
-- Copyright (C) 2004-2007, 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 2, 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 COPYING. If --
-- not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02110-1301, USA. --
-- --
-- GNATCHECK is maintained by AdaCore (http://www.adacore.com). --
-- --
------------------------------------------------------------------------------
with Ada.Characters.Handling; use Ada.Characters.Handling;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with Asis; use Asis;
with Asis.Ada_Environments;
with Asis.Compilation_Units; use Asis.Compilation_Units;
with Asis.Elements; use Asis.Elements;
with Asis.Errors;
with Asis.Exceptions;
with Asis.Extensions; use Asis.Extensions;
with Asis.Implementation;
with Asis.Text; use Asis.Text;
with ASIS_UL.Common; use ASIS_UL.Common;
with ASIS_UL.Global_State;
with ASIS_UL.Global_State.CG;
with ASIS_UL.Options; use ASIS_UL.Options;
with ASIS_UL.Output; use ASIS_UL.Output;
with ASIS_UL.Strings; use ASIS_UL.Strings;
with Gnatcheck.Compiler; use Gnatcheck.Compiler;
with Gnatcheck.Diagnoses;
with Gnatcheck.Options; use Gnatcheck.Options;
with Gnatcheck.Output; use Gnatcheck.Output;
with Gnatcheck.Rules; use Gnatcheck.Rules;
with Gnatcheck.Rules.Rule_Table; use Gnatcheck.Rules.Rule_Table;
with Gnatcheck.Rules.Traversing; use Gnatcheck.Rules.Traversing;
with Gnatcheck.Traversal_Stack;
package body ASIS_UL.Source_Table.Gnatcheck_Processing is
------------------------
-- Local subprograms --
------------------------
procedure Process_Sources_From_Table (Only_Bodies : Boolean := False);
-- Processes sources stores in the sources table trying to minimize
-- compilations needed to create the tree files. If Only_Bodies is set ON,
-- only files with .adb suffixes are compiled for the trees.
procedure Process_Source (SF : SF_Id);
-- Processes the source file stored under SF index into source file table.
-- The caller is responsible to keep the actual parameter inside the
-- range of the existing table entries. The processing consists of
-- creating the tree file for this source, and if the tree is successfully
-- created, then the ASIS Compilation Unit corresponding to this source
-- is processed. Then this routine tries to locate in the set of ASIS
-- Compilation Units representing by this tree units corresponding to some
-- other sources stored in the source table, and to process all these
-- units. When the processing is complete, the tree file and the
-- corresponding ALI file are deleted from the temporary directory.
procedure Collect_Global_Info (CU : Asis.Compilation_Unit; SF : SF_Id);
-- Traverses a unit and collects all the information needed to create a
-- global structure. Sets SF to Processed
procedure Traverse_Source
(CU : Asis.Compilation_Unit;
SF : SF_Id;
Do_Rule_Check : Boolean);
-- Implements the general ASIS Compilation Unit traversal algorythm:
-- traverses the ASIS Compilation Unit CU contained in the source file SF,
-- if Do_Rule_Check is ON checks all the active rules, collects the
-- information needed to create global structure. Sets SF status to
-- Processed.
procedure Collect_Global_Information;
pragma Unreferenced (Collect_Global_Information);
-- Processes all the sources that have not been processed yet (basically it
-- should be sources added as needed sources during the main rule checking
-- process and collects the information related to the global state of the
-- program.
procedure Debug_Output_Source (SF : SF_Id; Arg_Source : Boolean);
-- In debug mode, outputs into Stderrthe short name of SF and either
-- "(argument)" or "(needed)" string depending on the value of Arg_Source.
-- The output is three positions indented.
function Can_Be_Processed_From_Guest_Tree
(CU : Asis.Compilation_Unit)
return Boolean;
-- Checks if the argument unit can be processed on the base of a tree where
-- this unit is not the main unit.
-- ??? Now we unconditionally returns False. Impoving the productivity by
-- means of reducung compilations requires more systematic design. The main
-- problem here are expanded generic bodies - they can be processed from
-- the tree created for the given unit in most of the cases.
--------------------------------------
-- Can_Be_Processed_From_Guest_Tree --
--------------------------------------
function Can_Be_Processed_From_Guest_Tree
(CU : Asis.Compilation_Unit)
return Boolean
is
Result : Boolean := False;
begin
case Unit_Kind (CU) is
when A_Generic_Unit_Instance =>
-- We can get an expanded body only from the tree created for a
-- library-level instantiation itself
Result := False;
when A_Generic_Package |
A_Package =>
if Is_Body_Required (CU) then
-- If we have a generic instantiated in the spec, the expanded
-- body for it will be in the unit body.
Result := False;
end if;
when A_Package_Body =>
-- If the body contains generic instantiations, they do not have
-- expanded bodies in the "guest" tree
Result := False;
when others =>
null;
end case;
return Result;
end Can_Be_Processed_From_Guest_Tree;
-------------------------
-- Collect_Global_Info --
-------------------------
procedure Collect_Global_Info (CU : Asis.Compilation_Unit; SF : SF_Id) is
begin
Traverse_Source (CU, SF, Do_Rule_Check => False);
end Collect_Global_Info;
--------------------------------
-- Collect_Global_Information --
--------------------------------
procedure Collect_Global_Information is
Success : Boolean;
SF : SF_Id;
begin
-- The implementation is a simplified clone of
-- Process_Sources
if ASIS_UL.Options.Debug_Mode
or else
ASIS_UL.Options.Verbose_Mode
then
Info ("Processing needed sources...");
end if;
Reset_Source_Iterator;
SF := Next_Non_Processed_Source (Include_Needed_Sources => True);
while Present (SF) loop
Total_Sources := Natural (Last_Source);
Sources_Left :=
Total_Sources - Already_Processed (Include_Needed_Sources => True);
Create_Tree
(SF,
Success,
Compiler_Out => Compiler_Out_File_Name_String,
All_Warnings_Off => True);
if not Success then
return;
end if;
Asis.Ada_Environments.Associate
(The_Context => The_Context,
Name => "",
Parameters => "-C1 "
& To_Wide_String (Suffixless_Name (SF) & ".adt"));
declare
use type Asis.Errors.Error_Kinds;
begin
Asis.Ada_Environments.Open (The_Context);
Success := True;
exception
when Asis.Exceptions.ASIS_Failed =>
-- The only known situation when we can not open a C1 context
-- for newly created tree is recompilation of System
-- (see D617-017)
if Asis.Implementation.Status = Asis.Errors.Use_Error
and then
Asis.Implementation.Diagnosis =
"Internal implementation error:" &
" Asis.Ada_Environments.Open - System is recompiled"
then
Error ("can not process redefinition of System in " &
Source_Name (SF));
Set_Source_Status (SF, Not_A_Legal_Source);
Success := False;
else
raise;
end if;
end;
if Success then
declare
All_CUs : constant Asis.Compilation_Unit_List :=
Asis.Compilation_Units.Compilation_Units (The_Context);
Next_SF : SF_Id;
begin
Output_Source (SF);
for J in All_CUs'Range loop
Next_SF :=
File_Find (Normalize_Pathname
(To_String (Text_Name (All_CUs (J)))));
if Present (Next_SF)
and then
Source_Status (Next_SF) = Waiting
and then
(Unit_Origin (All_CUs (J)) = An_Application_Unit
or else
Process_RTL_Units)
and then
(Can_Be_Processed_From_Guest_Tree (All_CUs (J))
or else
Is_Main_Unit_In_Tree (All_CUs (J)))
then
Debug_Output_Source (Next_SF, Arg_Source => False);
The_CU := All_CUs (J);
-- Output_Source (Next_SF);
Collect_Global_Info (All_CUs (J), Next_SF);
end if;
end loop;
exception
when Ex : others =>
Error
("unknown bug detected when processing " &
Source_Name (Next_SF));
Error_No_Tool_Name
("Please submit bug report to report@adacore.com");
Report_Unhandled_Exception (Ex);
Source_Clean_Up (Next_SF);
raise Fatal_Error;
end;
end if;
Source_Clean_Up (SF);
SF := Next_Non_Processed_Source (Include_Needed_Sources => True);
end loop;
exception
when Fatal_Error =>
raise;
when Ex : others =>
Error ("unknown bug detected when processing " & Source_Name (SF));
Error_No_Tool_Name ("Please submit bug report to report@adacore.com");
Report_Unhandled_Exception (Ex);
Source_Clean_Up (SF);
raise Fatal_Error;
end Collect_Global_Information;
-------------------------
-- Debug_Output_Source --
-------------------------
procedure Debug_Output_Source (SF : SF_Id; Arg_Source : Boolean) is
begin
if ASIS_UL.Options.Debug_Mode then
Info_No_EOL (" " & Short_Source_Name (SF));
if Arg_Source then
Info (" (argument)");
else
Info (" (needed)");
end if;
end if;
end Debug_Output_Source;
----------------------
-- Define_Exit_Code --
----------------------
procedure Define_Exit_Code is
begin
if Gnatcheck.Diagnoses.Detected_Rule_Violations > 0 then
Exit_Code := E_Violation;
elsif Tool_Failures = 0 then
Exit_Code := E_Success;
else
Exit_Code := E_Non_Trusted;
end if;
end Define_Exit_Code;
--------------------
-- Exit_Gnatcheck --
--------------------
procedure Exit_Gnatcheck (Exit_Code : Exit_Code_Type) is
begin
case Exit_Code is
when E_Success => OS_Exit (0);
when E_Violation => OS_Exit (1);
when E_Fatal => OS_Exit (2);
when E_Non_Trusted => OS_Exit (2);
end case;
end Exit_Gnatcheck;
--------------
-- Finalize --
--------------
procedure Finalize is
begin
if not ASIS_UL.Options.Nothing_To_Do then
if ASIS_UL.Options.Buld_Call_Graph then
-- When creating a global data structure, we may have added new
-- files (as needed files) in the sources table, and nodes in
-- the global structure may refer to these added source as
-- their enclosing sources. So we have to add the corresponding
-- nodes to the diagnosis mapping table.
for J in Last_Argument_Source + 1 .. Last_Source loop
Gnatcheck.Diagnoses.Add_Line_To_Mapping_Table;
end loop;
if ASIS_UL.Options.Debug_Mode then
Info ("Call graph closure ... ");
end if;
ASIS_UL.Global_State.CG.Transitive_Closure;
if ASIS_UL.Options.Debug_Mode then
Info ("...Done");
end if;
end if;
Gnatcheck.Rules.Traversing.Check_Global_Rules;
if ASIS_UL.Options.Debug_Mode then
ASIS_UL.Global_State.Print_Global_Structure;
-- Gnatcheck.Diagnoses.Diag_Structure_Debug_Image;
end if;
if ASIS_UL.Options.Debug_Mode then
Info ("Generate report ... ");
end if;
Gnatcheck.Diagnoses.Generate_Report;
ASIS_UL.Output.Close_Report_File;
if ASIS_UL.Options.Debug_Mode then
Info ("...Done");
end if;
if Tool_Failures > 0 or else Debug_Mode then
Info ("Total gnatcheck failures :" & Tool_Failures'Img);
end if;
end if;
end Finalize;
----------------
-- Initialize --
----------------
procedure Initialize is
begin
Gnatcheck.Diagnoses.Create_Mapping_Table;
Analyze_Compiler_Output :=
Use_gnaty_Option or else Use_gnatw_Option or else Check_Restrictions;
if ASIS_UL.Options.Buld_Call_Graph then
ASIS_UL.Global_State.Initialize;
end if;
if Check_Restrictions then
Create_Restriction_Pragmas_File;
end if;
Gnatcheck.Traversal_Stack.Initialize;
end Initialize;
--------------------
-- Process_Source --
--------------------
procedure Process_Source (SF : SF_Id) is
Success : Boolean;
use type Asis.Errors.Error_Kinds;
begin
Output_Source (SF);
if Analyze_Compiler_Output then
Create_Temp_File (Compiler_Out_FD, Compiler_Out_File_Name);
Close (Compiler_Out_FD);
Delete_File (Compiler_Out_File_Name, Success);
end if;
Create_Tree
(SF,
Success,
Compiler_Out => Compiler_Out_File_Name_String,
All_Warnings_Off => Suppess_Compiler_Check);
if not Success then
if Analyze_Compiler_Output then
Close (Compiler_Out_FD);
Delete_File (Compiler_Out_File_Name, Success);
end if;
return;
end if;
Asis.Ada_Environments.Associate
(The_Context => The_Context,
Name => "",
Parameters => "-C1 "
& To_Wide_String (Suffixless_Name (SF) & ".adt"));
begin
Asis.Ada_Environments.Open (The_Context);
Success := True;
exception
when Asis.Exceptions.ASIS_Failed =>
-- The only known situation when we can not open a C1 context for
-- newly created tree is recompilation of System (see D617-017)
if Asis.Implementation.Status = Asis.Errors.Use_Error
and then
Asis.Implementation.Diagnosis = "Internal implementation error:"
& " Asis.Ada_Environments.Open - System is recompiled"
then
Error ("can not process redefinition of System in " &
Source_Name (SF));
Set_Source_Status (SF, Not_A_Legal_Source);
Success := False;
else
raise;
end if;
end;
if Success then
The_CU := Main_Unit_In_Current_Tree (The_Context);
if Unit_Origin (The_CU) /= An_Application_Unit
and then
not Process_RTL_Units
then
Error ("cannot process RTL unit " & Source_Name (SF) &
" Use '-a' option for processing RTL components");
Set_Source_Status (SF, Processed);
else
Traverse_Source (The_CU, SF, Do_Rule_Check => True);
declare
All_CUs : constant Asis.Compilation_Unit_List :=
Asis.Compilation_Units.Compilation_Units (The_Context);
CUs_Processed : array (All_CUs'Range) of Boolean :=
(others => False);
-- Indicates if the given CU has been processed
Next_SF : SF_Id;
begin
for J in All_CUs'Range loop
if Unit_Origin (All_CUs (J)) = An_Application_Unit
or else
not Process_RTL_Units
then
Next_SF :=
File_Find (Normalize_Pathname
(To_String (Text_Name (All_CUs (J)))));
if Is_Argument_Source (Next_SF)
and then
Source_Status (Next_SF) = Waiting
then
if Unit_Origin (All_CUs (J)) /= An_Application_Unit
and then
not Process_RTL_Units
then
Error ("cannot process RTL unit " &
Source_Name (Next_SF) &
" Use '-a' option for processing " &
"RTL components");
Set_Source_Status (Next_SF, Processed);
elsif Can_Be_Processed_From_Guest_Tree
(All_CUs (J))
then
The_CU := All_CUs (J);
Debug_Output_Source (Next_SF, Arg_Source => True);
Traverse_Source
(All_CUs (J), Next_SF, Do_Rule_Check => True);
CUs_Processed (J) := True;
end if;
end if;
end if;
end loop;
-- And now - complete the global structure by processing the
-- units that have been added as needed units when processing
-- argument units.
if ASIS_UL.Options.Buld_Call_Graph then
for J in All_CUs'Range loop
if not CUs_Processed (J) then
Next_SF :=
File_Find (Normalize_Pathname
(To_String (Text_Name (All_CUs (J)))));
if not (Unit_Origin (All_CUs (J)) /=
An_Application_Unit
and then
not Process_RTL_Units)
and then
Is_Needed_Source (Next_SF)
and then
Can_Be_Processed_From_Guest_Tree (All_CUs (J))
then
Debug_Output_Source (Next_SF, Arg_Source => False);
Collect_Global_Info (All_CUs (J), Next_SF);
end if;
end if;
end loop;
end if;
exception
when Ex : others =>
Error
("unknown bug detected when processing " &
Source_Name (Next_SF));
Error_No_Tool_Name
("Please submit bug report to report@adacore.com");
Report_Unhandled_Exception (Ex);
Source_Clean_Up (Next_SF);
raise Fatal_Error;
end;
end if;
end if;
if Analyze_Compiler_Output then
Analyze_Compiler_Warnings (Compiler_Out_File_Name);
Close (Compiler_Out_FD);
Delete_File (Compiler_Out_File_Name, Success);
end if;
Source_Clean_Up (SF);
exception
when Fatal_Error =>
raise;
when Ex : others =>
Error ("unknown bug detected when processing " & Source_Name (SF));
Error_No_Tool_Name
("Please submit bug report to report@adacore.com");
Report_Unhandled_Exception (Ex);
Source_Clean_Up (SF);
raise Fatal_Error;
end Process_Source;
---------------------
-- Process_Sources --
---------------------
procedure Process_Sources is
begin
if Generate_Rules_Help then
Rules_Help;
end if;
if Last_Source = No_SF_Id then
-- Nothing to do!
if not Generate_Rules_Help then
Brief_Help;
end if;
return;
end if;
Asis.Implementation.Initialize ("-k -asis05 -ws");
Process_Sources_From_Table (Only_Bodies => True);
Process_Sources_From_Table;
-- if Gnatcheck.Global_State.Buld_Call_Graph then
-- Collect_Global_Information;
-- end if;
Asis.Implementation.Finalize;
end Process_Sources;
--------------------------------
-- Process_Sources_From_Table --
--------------------------------
procedure Process_Sources_From_Table (Only_Bodies : Boolean := False) is
Next_SF : SF_Id;
begin
Reset_Source_Iterator;
Next_SF := Next_Non_Processed_Source (Only_Bodies);
while Present (Next_SF) loop
Process_Source (Next_SF);
Next_SF := Next_Non_Processed_Source (Only_Bodies);
end loop;
end Process_Sources_From_Table;
---------------------
-- Traverse_Source --
---------------------
procedure Traverse_Source
(CU : Asis.Compilation_Unit;
SF : SF_Id;
Do_Rule_Check : Boolean)
is
Program_Unit : constant Asis.Element := Unit_Declaration (CU);
Contex_Clause : constant Asis.Element_List :=
Context_Clause_Elements (CU, True);
Comp_Pragmas : constant Asis.Element_List :=
Compilation_Pragmas (CU);
First_Pragma_After : List_Index := Comp_Pragmas'Last + 1;
Unit_Span : constant Asis.Text.Span :=
Element_Span (Program_Unit);
-- We also may have to check pragmas after the unit, that's why we need
-- these objects.
Check_Control : Traverse_Control := Continue;
Check_State : Rule_Traversal_State :=
(Initial_State, False, SF, 0, Nil_String_Loc, 0, 0, 0, Do_Rule_Check);
begin
for J in Comp_Pragmas'Range loop
if Unit_Span.Last_Line <=
Element_Span (Comp_Pragmas (J)).First_Line
then
First_Pragma_After := J;
exit;
end if;
end loop;
for J in Contex_Clause'Range loop
Check_Rules (Contex_Clause (J), Check_Control, Check_State);
end loop;
Check_Rules (Program_Unit, Check_Control, Check_State);
for J in First_Pragma_After .. Comp_Pragmas'Last loop
if Is_Equal (Enclosing_Compilation_Unit (Comp_Pragmas (J)), CU) then
-- We may have configuration pragmas in the list
Check_Rules (Comp_Pragmas (J), Check_Control, Check_State);
end if;
end loop;
Set_Source_Status (SF, Processed);
end Traverse_Source;
end ASIS_UL.Source_Table.Gnatcheck_Processing;
|