1------------------------------------------------------------------------------ 2-- -- 3-- GNAT COMPILER COMPONENTS -- 4-- -- 5-- S E M _ W A R N -- 6-- -- 7-- B o d y -- 8-- -- 9-- Copyright (C) 1999-2020, Free Software Foundation, Inc. -- 10-- -- 11-- GNAT is free software; you can redistribute it and/or modify it under -- 12-- terms of the GNU General Public License as published by the Free Soft- -- 13-- ware Foundation; either version 3, or (at your option) any later ver- -- 14-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- 15-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- 16-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- 17-- for more details. You should have received a copy of the GNU General -- 18-- Public License distributed with GNAT; see file COPYING3. If not, go to -- 19-- http://www.gnu.org/licenses for a complete copy of the license. -- 20-- -- 21-- GNAT was originally developed by the GNAT team at New York University. -- 22-- Extensive contributions were provided by Ada Core Technologies Inc. -- 23-- -- 24------------------------------------------------------------------------------ 25 26with Atree; use Atree; 27with Debug; use Debug; 28with Einfo; use Einfo; 29with Errout; use Errout; 30with Exp_Code; use Exp_Code; 31with Lib; use Lib; 32with Lib.Xref; use Lib.Xref; 33with Namet; use Namet; 34with Nlists; use Nlists; 35with Opt; use Opt; 36with Par_SCO; use Par_SCO; 37with Rtsfind; use Rtsfind; 38with Sem; use Sem; 39with Sem_Ch8; use Sem_Ch8; 40with Sem_Aux; use Sem_Aux; 41with Sem_Eval; use Sem_Eval; 42with Sem_Prag; use Sem_Prag; 43with Sem_Util; use Sem_Util; 44with Sinfo; use Sinfo; 45with Sinput; use Sinput; 46with Snames; use Snames; 47with Stand; use Stand; 48with Stringt; use Stringt; 49with Tbuild; use Tbuild; 50with Uintp; use Uintp; 51 52package body Sem_Warn is 53 54 -- The following table collects Id's of entities that are potentially 55 -- unreferenced. See Check_Unset_Reference for further details. 56 -- ??? Check_Unset_Reference has zero information about this table. 57 58 package Unreferenced_Entities is new Table.Table ( 59 Table_Component_Type => Entity_Id, 60 Table_Index_Type => Nat, 61 Table_Low_Bound => 1, 62 Table_Initial => Alloc.Unreferenced_Entities_Initial, 63 Table_Increment => Alloc.Unreferenced_Entities_Increment, 64 Table_Name => "Unreferenced_Entities"); 65 66 -- The following table collects potential warnings for IN OUT parameters 67 -- that are referenced but not modified. These warnings are processed when 68 -- the front end calls the procedure Output_Non_Modified_In_Out_Warnings. 69 -- The reason that we defer output of these messages is that we want to 70 -- detect the case where the relevant procedure is used as a generic actual 71 -- in an instantiation, since we suppress the warnings in this case. The 72 -- flag Used_As_Generic_Actual will be set in this case, but only at the 73 -- point of usage. Similarly, we suppress the message if the address of the 74 -- procedure is taken, where the flag Address_Taken may be set later. 75 76 package In_Out_Warnings is new Table.Table ( 77 Table_Component_Type => Entity_Id, 78 Table_Index_Type => Nat, 79 Table_Low_Bound => 1, 80 Table_Initial => Alloc.In_Out_Warnings_Initial, 81 Table_Increment => Alloc.In_Out_Warnings_Increment, 82 Table_Name => "In_Out_Warnings"); 83 84 -------------------------------------------------------- 85 -- Handling of Warnings Off, Unmodified, Unreferenced -- 86 -------------------------------------------------------- 87 88 -- The functions Has_Warnings_Off, Has_Unmodified, Has_Unreferenced must 89 -- generally be used instead of Warnings_Off, Has_Pragma_Unmodified and 90 -- Has_Pragma_Unreferenced, as noted in the specs in Einfo. 91 92 -- In order to avoid losing warnings in -gnatw.w (warn on unnecessary 93 -- warnings off pragma) mode, i.e. to avoid false negatives, the code 94 -- must follow some important rules. 95 96 -- Call these functions as late as possible, after completing all other 97 -- tests, just before the warnings is given. For example, don't write: 98 99 -- if not Has_Warnings_Off (E) 100 -- and then some-other-predicate-on-E then .. 101 102 -- Instead the following is preferred 103 104 -- if some-other-predicate-on-E 105 -- and then Has_Warnings_Off (E) 106 107 -- This way if some-other-predicate is false, we avoid a false indication 108 -- that a Warnings (Off, E) pragma was useful in preventing a warning. 109 110 -- The second rule is that if both Has_Unmodified and Has_Warnings_Off, or 111 -- Has_Unreferenced and Has_Warnings_Off are called, make sure that the 112 -- call to Has_Unmodified/Has_Unreferenced comes first, this way we record 113 -- that the Warnings (Off) could have been Unreferenced or Unmodified. In 114 -- fact Has_Unmodified/Has_Unreferenced includes a test for Warnings Off, 115 -- and so a subsequent test is not needed anyway (though it is harmless). 116 117 ----------------------- 118 -- Local Subprograms -- 119 ----------------------- 120 121 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean; 122 -- This returns true if the entity E is declared within a generic package. 123 -- The point of this is to detect variables which are not assigned within 124 -- the generic, but might be assigned outside the package for any given 125 -- instance. These are cases where we leave the warnings to be posted for 126 -- the instance, when we will know more. 127 128 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id; 129 -- If E is a parameter entity for a subprogram body, then this function 130 -- returns the corresponding spec entity, if not, E is returned unchanged. 131 132 function Has_Pragma_Unmodified_Check_Spec (E : Entity_Id) return Boolean; 133 -- Tests Has_Pragma_Unmodified flag for entity E. If E is not a formal, 134 -- this is simply the setting of the flag Has_Pragma_Unmodified. If E is 135 -- a body formal, the setting of the flag in the corresponding spec is 136 -- also checked (and True returned if either flag is True). 137 138 function Has_Pragma_Unreferenced_Check_Spec (E : Entity_Id) return Boolean; 139 -- Tests Has_Pragma_Unreferenced flag for entity E. If E is not a formal, 140 -- this is simply the setting of the flag Has_Pragma_Unreferenced. If E is 141 -- a body formal, the setting of the flag in the corresponding spec is 142 -- also checked (and True returned if either flag is True). 143 144 function Is_Attribute_And_Known_Value_Comparison 145 (Op : Node_Id) return Boolean; 146 -- Determine whether operator Op denotes a comparison where the left 147 -- operand is an attribute reference and the value of the right operand is 148 -- known at compile time. 149 150 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean; 151 -- Tests Never_Set_In_Source status for entity E. If E is not a formal, 152 -- this is simply the setting of the flag Never_Set_In_Source. If E is 153 -- a body formal, the setting of the flag in the corresponding spec is 154 -- also checked (and False returned if either flag is False). 155 156 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean; 157 -- This function traverses the expression tree represented by the node N 158 -- and determines if any sub-operand is a reference to an entity for which 159 -- the Warnings_Off flag is set. True is returned if such an entity is 160 -- encountered, and False otherwise. 161 162 function Referenced_Check_Spec (E : Entity_Id) return Boolean; 163 -- Tests Referenced status for entity E. If E is not a formal, this is 164 -- simply the setting of the flag Referenced. If E is a body formal, the 165 -- setting of the flag in the corresponding spec is also checked (and True 166 -- returned if either flag is True). 167 168 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean; 169 -- Tests Referenced_As_LHS status for entity E. If E is not a formal, this 170 -- is simply the setting of the flag Referenced_As_LHS. If E is a body 171 -- formal, the setting of the flag in the corresponding spec is also 172 -- checked (and True returned if either flag is True). 173 174 function Referenced_As_Out_Parameter_Check_Spec 175 (E : Entity_Id) return Boolean; 176 -- Tests Referenced_As_Out_Parameter status for entity E. If E is not a 177 -- formal, this is simply the setting of Referenced_As_Out_Parameter. If E 178 -- is a body formal, the setting of the flag in the corresponding spec is 179 -- also checked (and True returned if either flag is True). 180 181 procedure Warn_On_Unreferenced_Entity 182 (Spec_E : Entity_Id; 183 Body_E : Entity_Id := Empty); 184 -- Output warnings for unreferenced entity E. For the case of an entry 185 -- formal, Body_E is the corresponding body entity for a particular 186 -- accept statement, and the message is posted on Body_E. In all other 187 -- cases, Body_E is ignored and must be Empty. 188 189 function Warnings_Off_Check_Spec (E : Entity_Id) return Boolean; 190 -- Returns True if Warnings_Off is set for the entity E or (in the case 191 -- where there is a Spec_Entity), Warnings_Off is set for the Spec_Entity. 192 193 -------------------------- 194 -- Check_Code_Statement -- 195 -------------------------- 196 197 procedure Check_Code_Statement (N : Node_Id) is 198 begin 199 -- If volatile, nothing to worry about 200 201 if Is_Asm_Volatile (N) then 202 return; 203 end if; 204 205 -- Warn if no input or no output 206 207 Setup_Asm_Inputs (N); 208 209 if No (Asm_Input_Value) then 210 Error_Msg_F 211 ("??code statement with no inputs should usually be Volatile!", N); 212 return; 213 end if; 214 215 Setup_Asm_Outputs (N); 216 217 if No (Asm_Output_Variable) then 218 Error_Msg_F 219 ("??code statement with no outputs should usually be Volatile!", N); 220 return; 221 end if; 222 end Check_Code_Statement; 223 224 --------------------------------- 225 -- Check_Infinite_Loop_Warning -- 226 --------------------------------- 227 228 -- The case we look for is a while loop which tests a local variable, where 229 -- there is no obvious direct or possible indirect update of the variable 230 -- within the body of the loop. 231 232 procedure Check_Infinite_Loop_Warning (Loop_Statement : Node_Id) is 233 Expression : Node_Id := Empty; 234 -- Set to WHILE or EXIT WHEN condition to be tested 235 236 Ref : Node_Id := Empty; 237 -- Reference in Expression to variable that might not be modified 238 -- in loop, indicating a possible infinite loop. 239 240 Var : Entity_Id := Empty; 241 -- Corresponding entity (entity of Ref) 242 243 Function_Call_Found : Boolean := False; 244 -- True if Find_Var found a function call in the condition 245 246 procedure Find_Var (N : Node_Id); 247 -- Inspect condition to see if it depends on a single entity reference. 248 -- If so, Ref is set to point to the reference node, and Var is set to 249 -- the referenced Entity. 250 251 function Has_Condition_Actions (Iter : Node_Id) return Boolean; 252 -- Determine whether iteration scheme Iter has meaningful condition 253 -- actions. 254 255 function Has_Indirection (T : Entity_Id) return Boolean; 256 -- If the controlling variable is an access type, or is a record type 257 -- with access components, assume that it is changed indirectly and 258 -- suppress the warning. As a concession to low-level programming, in 259 -- particular within Declib, we also suppress warnings on a record 260 -- type that contains components of type Address or Short_Address. 261 262 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean; 263 -- Given an entity name, see if the name appears to have something to 264 -- do with I/O or network stuff, and if so, return True. Used to kill 265 -- some false positives on a heuristic basis that such functions will 266 -- likely have some strange side effect dependencies. A rather strange 267 -- test, but warning messages are in the heuristics business. 268 269 function Test_Ref (N : Node_Id) return Traverse_Result; 270 -- Test for reference to variable in question. Returns Abandon if 271 -- matching reference found. Used in instantiation of No_Ref_Found. 272 273 function No_Ref_Found is new Traverse_Func (Test_Ref); 274 -- Function to traverse body of procedure. Returns Abandon if matching 275 -- reference found. 276 277 -------------- 278 -- Find_Var -- 279 -------------- 280 281 procedure Find_Var (N : Node_Id) is 282 begin 283 -- Condition is a direct variable reference 284 285 if Is_Entity_Name (N) then 286 Ref := N; 287 Var := Entity (Ref); 288 289 -- Case of condition is a comparison with compile time known value 290 291 elsif Nkind (N) in N_Op_Compare then 292 if Compile_Time_Known_Value (Right_Opnd (N)) then 293 Find_Var (Left_Opnd (N)); 294 295 elsif Compile_Time_Known_Value (Left_Opnd (N)) then 296 Find_Var (Right_Opnd (N)); 297 298 -- Ignore any other comparison 299 300 else 301 return; 302 end if; 303 304 -- If condition is a negation, check its operand 305 306 elsif Nkind (N) = N_Op_Not then 307 Find_Var (Right_Opnd (N)); 308 309 -- Case of condition is function call 310 311 elsif Nkind (N) = N_Function_Call then 312 313 Function_Call_Found := True; 314 315 -- Forget it if function name is not entity, who knows what 316 -- we might be calling? 317 318 if not Is_Entity_Name (Name (N)) then 319 return; 320 321 -- Forget it if function name is suspicious. A strange test 322 -- but warning generation is in the heuristics business. 323 324 elsif Is_Suspicious_Function_Name (Entity (Name (N))) then 325 return; 326 327 -- Forget it if function is marked Volatile_Function 328 329 elsif Is_Volatile_Function (Entity (Name (N))) then 330 return; 331 332 -- Forget it if warnings are suppressed on function entity 333 334 elsif Has_Warnings_Off (Entity (Name (N))) then 335 return; 336 337 -- Forget it if the parameter is not In 338 339 elsif Has_Out_Or_In_Out_Parameter (Entity (Name (N))) then 340 return; 341 end if; 342 343 -- OK, see if we have one argument 344 345 declare 346 PA : constant List_Id := Parameter_Associations (N); 347 348 begin 349 -- One argument, so check the argument 350 351 if Present (PA) and then List_Length (PA) = 1 then 352 if Nkind (First (PA)) = N_Parameter_Association then 353 Find_Var (Explicit_Actual_Parameter (First (PA))); 354 else 355 Find_Var (First (PA)); 356 end if; 357 358 -- Not one argument 359 360 else 361 return; 362 end if; 363 end; 364 365 -- Any other kind of node is not something we warn for 366 367 else 368 return; 369 end if; 370 end Find_Var; 371 372 --------------------------- 373 -- Has_Condition_Actions -- 374 --------------------------- 375 376 function Has_Condition_Actions (Iter : Node_Id) return Boolean is 377 Action : Node_Id; 378 379 begin 380 -- A call marker is not considered a meaningful action because it 381 -- acts as an annotation and has no runtime semantics. 382 383 Action := First (Condition_Actions (Iter)); 384 while Present (Action) loop 385 if Nkind (Action) /= N_Call_Marker then 386 return True; 387 end if; 388 389 Next (Action); 390 end loop; 391 392 return False; 393 end Has_Condition_Actions; 394 395 --------------------- 396 -- Has_Indirection -- 397 --------------------- 398 399 function Has_Indirection (T : Entity_Id) return Boolean is 400 Comp : Entity_Id; 401 Rec : Entity_Id; 402 403 begin 404 if Is_Access_Type (T) then 405 return True; 406 407 elsif Is_Private_Type (T) 408 and then Present (Full_View (T)) 409 and then Is_Access_Type (Full_View (T)) 410 then 411 return True; 412 413 elsif Is_Record_Type (T) then 414 Rec := T; 415 416 elsif Is_Private_Type (T) 417 and then Present (Full_View (T)) 418 and then Is_Record_Type (Full_View (T)) 419 then 420 Rec := Full_View (T); 421 else 422 return False; 423 end if; 424 425 Comp := First_Component (Rec); 426 while Present (Comp) loop 427 if Is_Access_Type (Etype (Comp)) 428 or else Is_Descendant_Of_Address (Etype (Comp)) 429 then 430 return True; 431 end if; 432 433 Next_Component (Comp); 434 end loop; 435 436 return False; 437 end Has_Indirection; 438 439 --------------------------------- 440 -- Is_Suspicious_Function_Name -- 441 --------------------------------- 442 443 function Is_Suspicious_Function_Name (E : Entity_Id) return Boolean is 444 S : Entity_Id; 445 446 function Substring_Present (S : String) return Boolean; 447 -- Returns True if name buffer has given string delimited by non- 448 -- alphabetic characters or by end of string. S is lower case. 449 450 ----------------------- 451 -- Substring_Present -- 452 ----------------------- 453 454 function Substring_Present (S : String) return Boolean is 455 Len : constant Natural := S'Length; 456 457 begin 458 for J in 1 .. Name_Len - (Len - 1) loop 459 if Name_Buffer (J .. J + (Len - 1)) = S 460 and then (J = 1 or else Name_Buffer (J - 1) not in 'a' .. 'z') 461 and then 462 (J + Len > Name_Len 463 or else Name_Buffer (J + Len) not in 'a' .. 'z') 464 then 465 return True; 466 end if; 467 end loop; 468 469 return False; 470 end Substring_Present; 471 472 -- Start of processing for Is_Suspicious_Function_Name 473 474 begin 475 S := E; 476 while Present (S) and then S /= Standard_Standard loop 477 Get_Name_String (Chars (S)); 478 479 if Substring_Present ("io") 480 or else Substring_Present ("file") 481 or else Substring_Present ("network") 482 then 483 return True; 484 else 485 S := Scope (S); 486 end if; 487 end loop; 488 489 return False; 490 end Is_Suspicious_Function_Name; 491 492 -------------- 493 -- Test_Ref -- 494 -------------- 495 496 function Test_Ref (N : Node_Id) return Traverse_Result is 497 begin 498 -- Waste of time to look at the expression we are testing 499 500 if N = Expression then 501 return Skip; 502 503 -- Direct reference to variable in question 504 505 elsif Is_Entity_Name (N) 506 and then Present (Entity (N)) 507 and then Entity (N) = Var 508 then 509 -- If this is an lvalue, then definitely abandon, since 510 -- this could be a direct modification of the variable. 511 512 if May_Be_Lvalue (N) then 513 return Abandon; 514 end if; 515 516 -- If the condition contains a function call, we consider it may 517 -- be modified by side effects from a procedure call. Otherwise, 518 -- we consider the condition may not be modified, although that 519 -- might happen if Variable is itself a by-reference parameter, 520 -- and the procedure called modifies the global object referred to 521 -- by Variable, but we actually prefer to issue a warning in this 522 -- odd case. Note that the case where the procedure called has 523 -- visibility over Variable is treated in another case below. 524 525 if Function_Call_Found then 526 declare 527 P : Node_Id; 528 529 begin 530 P := N; 531 loop 532 P := Parent (P); 533 exit when P = Loop_Statement; 534 535 -- Abandon if at procedure call, or something strange is 536 -- going on (perhaps a node with no parent that should 537 -- have one but does not?) As always, for a warning we 538 -- prefer to just abandon the warning than get into the 539 -- business of complaining about the tree structure here. 540 541 if No (P) 542 or else Nkind (P) = N_Procedure_Call_Statement 543 then 544 return Abandon; 545 end if; 546 end loop; 547 end; 548 end if; 549 550 -- Reference to variable renaming variable in question 551 552 elsif Is_Entity_Name (N) 553 and then Present (Entity (N)) 554 and then Ekind (Entity (N)) = E_Variable 555 and then Present (Renamed_Object (Entity (N))) 556 and then Is_Entity_Name (Renamed_Object (Entity (N))) 557 and then Entity (Renamed_Object (Entity (N))) = Var 558 and then May_Be_Lvalue (N) 559 then 560 return Abandon; 561 562 -- Call to subprogram 563 564 elsif Nkind (N) in N_Subprogram_Call then 565 566 -- If subprogram is within the scope of the entity we are dealing 567 -- with as the loop variable, then it could modify this parameter, 568 -- so we abandon in this case. In the case of a subprogram that is 569 -- not an entity we also abandon. The check for no entity being 570 -- present is a defense against previous errors. 571 572 if not Is_Entity_Name (Name (N)) 573 or else No (Entity (Name (N))) 574 or else Scope_Within (Entity (Name (N)), Scope (Var)) 575 then 576 return Abandon; 577 end if; 578 579 -- If any of the arguments are of type access to subprogram, then 580 -- we may have funny side effects, so no warning in this case. 581 582 declare 583 Actual : Node_Id; 584 begin 585 Actual := First_Actual (N); 586 while Present (Actual) loop 587 if Is_Access_Subprogram_Type (Etype (Actual)) then 588 return Abandon; 589 else 590 Next_Actual (Actual); 591 end if; 592 end loop; 593 end; 594 595 -- Declaration of the variable in question 596 597 elsif Nkind (N) = N_Object_Declaration 598 and then Defining_Identifier (N) = Var 599 then 600 return Abandon; 601 end if; 602 603 -- All OK, continue scan 604 605 return OK; 606 end Test_Ref; 607 608 -- Start of processing for Check_Infinite_Loop_Warning 609 610 begin 611 -- Skip processing if debug flag gnatd.w is set 612 613 if Debug_Flag_Dot_W then 614 return; 615 end if; 616 617 -- Deal with Iteration scheme present 618 619 declare 620 Iter : constant Node_Id := Iteration_Scheme (Loop_Statement); 621 622 begin 623 if Present (Iter) then 624 625 -- While iteration 626 627 if Present (Condition (Iter)) then 628 629 -- Skip processing for while iteration with conditions actions, 630 -- since they make it too complicated to get the warning right. 631 632 if Has_Condition_Actions (Iter) then 633 return; 634 end if; 635 636 -- Capture WHILE condition 637 638 Expression := Condition (Iter); 639 640 -- For Loop_Parameter_Specification, do not process, since loop 641 -- will always terminate. For Iterator_Specification, also do not 642 -- process. Either it will always terminate (e.g. "for X of 643 -- Some_Array ..."), or we can't tell if it's going to terminate 644 -- without looking at the iterator, so any warning here would be 645 -- noise. 646 647 elsif Present (Loop_Parameter_Specification (Iter)) 648 or else Present (Iterator_Specification (Iter)) 649 then 650 return; 651 end if; 652 end if; 653 end; 654 655 -- Check chain of EXIT statements, we only process loops that have a 656 -- single exit condition (either a single EXIT WHEN statement, or a 657 -- WHILE loop not containing any EXIT WHEN statements). 658 659 declare 660 Ident : constant Node_Id := Identifier (Loop_Statement); 661 Exit_Stmt : Node_Id; 662 663 begin 664 -- If we don't have a proper chain set, ignore call entirely. This 665 -- happens because of previous errors. 666 667 if No (Entity (Ident)) 668 or else Ekind (Entity (Ident)) /= E_Loop 669 then 670 Check_Error_Detected; 671 return; 672 end if; 673 674 -- Otherwise prepare to scan list of EXIT statements 675 676 Exit_Stmt := First_Exit_Statement (Entity (Ident)); 677 while Present (Exit_Stmt) loop 678 679 -- Check for EXIT WHEN 680 681 if Present (Condition (Exit_Stmt)) then 682 683 -- Quit processing if EXIT WHEN in WHILE loop, or more than 684 -- one EXIT WHEN statement present in the loop. 685 686 if Present (Expression) then 687 return; 688 689 -- Otherwise capture condition from EXIT WHEN statement 690 691 else 692 Expression := Condition (Exit_Stmt); 693 end if; 694 695 -- If an unconditional exit statement is the last statement in the 696 -- loop, assume that no warning is needed, without any attempt at 697 -- checking whether the exit is reachable. 698 699 elsif Exit_Stmt = Last (Statements (Loop_Statement)) then 700 return; 701 end if; 702 703 Exit_Stmt := Next_Exit_Statement (Exit_Stmt); 704 end loop; 705 end; 706 707 -- Return if no condition to test 708 709 if No (Expression) then 710 return; 711 end if; 712 713 -- Initial conditions met, see if condition is of right form 714 715 Find_Var (Expression); 716 717 -- Nothing to do if local variable from source not found. If it's a 718 -- renaming, it is probably renaming something too complicated to deal 719 -- with here. 720 721 if No (Var) 722 or else Ekind (Var) /= E_Variable 723 or else Is_Library_Level_Entity (Var) 724 or else not Comes_From_Source (Var) 725 or else Nkind (Parent (Var)) = N_Object_Renaming_Declaration 726 then 727 return; 728 729 -- Nothing to do if there is some indirection involved (assume that the 730 -- designated variable might be modified in some way we don't see). 731 -- However, if no function call was found, then we don't care about 732 -- indirections, because the condition must be something like "while X 733 -- /= null loop", so we don't care if X.all is modified in the loop. 734 735 elsif Function_Call_Found and then Has_Indirection (Etype (Var)) then 736 return; 737 738 -- Same sort of thing for volatile variable, might be modified by 739 -- some other task or by the operating system in some way. 740 741 elsif Is_Volatile (Var) then 742 return; 743 end if; 744 745 -- Filter out case of original statement sequence starting with delay. 746 -- We assume this is a multi-tasking program and that the condition 747 -- is affected by other threads (some kind of busy wait). 748 749 declare 750 Fstm : constant Node_Id := 751 Original_Node (First (Statements (Loop_Statement))); 752 begin 753 if Nkind (Fstm) in N_Delay_Statement then 754 return; 755 end if; 756 end; 757 758 -- We have a variable reference of the right form, now we scan the loop 759 -- body to see if it looks like it might not be modified 760 761 if No_Ref_Found (Loop_Statement) = OK then 762 Error_Msg_NE 763 ("??variable& is not modified in loop body!", Ref, Var); 764 Error_Msg_N 765 ("\??possible infinite loop!", Ref); 766 end if; 767 end Check_Infinite_Loop_Warning; 768 769 ---------------------------- 770 -- Check_Low_Bound_Tested -- 771 ---------------------------- 772 773 procedure Check_Low_Bound_Tested (Expr : Node_Id) is 774 procedure Check_Low_Bound_Tested_For (Opnd : Node_Id); 775 -- Determine whether operand Opnd denotes attribute 'First whose prefix 776 -- is a formal parameter. If this is the case, mark the entity of the 777 -- prefix as having its low bound tested. 778 779 -------------------------------- 780 -- Check_Low_Bound_Tested_For -- 781 -------------------------------- 782 783 procedure Check_Low_Bound_Tested_For (Opnd : Node_Id) is 784 begin 785 if Nkind (Opnd) = N_Attribute_Reference 786 and then Attribute_Name (Opnd) = Name_First 787 and then Is_Entity_Name (Prefix (Opnd)) 788 and then Present (Entity (Prefix (Opnd))) 789 and then Is_Formal (Entity (Prefix (Opnd))) 790 then 791 Set_Low_Bound_Tested (Entity (Prefix (Opnd))); 792 end if; 793 end Check_Low_Bound_Tested_For; 794 795 -- Start of processing for Check_Low_Bound_Tested 796 797 begin 798 if Comes_From_Source (Expr) then 799 Check_Low_Bound_Tested_For (Left_Opnd (Expr)); 800 Check_Low_Bound_Tested_For (Right_Opnd (Expr)); 801 end if; 802 end Check_Low_Bound_Tested; 803 804 ---------------------- 805 -- Check_References -- 806 ---------------------- 807 808 procedure Check_References (E : Entity_Id; Anod : Node_Id := Empty) is 809 E1 : Entity_Id; 810 E1T : Entity_Id; 811 UR : Node_Id; 812 813 function Body_Formal 814 (E : Entity_Id; 815 Accept_Statement : Node_Id) return Entity_Id; 816 -- For an entry formal entity from an entry declaration, find the 817 -- corresponding body formal from the given accept statement. 818 819 function Generic_Body_Formal (E : Entity_Id) return Entity_Id; 820 -- Warnings on unused formals of subprograms are placed on the entity 821 -- in the subprogram body, which seems preferable because it suggests 822 -- a better codefix for GNAT Studio. The analysis of generic subprogram 823 -- bodies uses a different circuitry, so the choice for the proper 824 -- placement of the warning in the generic case takes place here, by 825 -- finding the body entity that corresponds to a formal in a spec. 826 827 procedure May_Need_Initialized_Actual (Ent : Entity_Id); 828 -- If an entity of a generic type has default initialization, then the 829 -- corresponding actual type should be fully initialized, or else there 830 -- will be uninitialized components in the instantiation, that might go 831 -- unreported. This routine marks the type of the uninitialized variable 832 -- appropriately to allow the compiler to emit an appropriate warning 833 -- in the instance. In a sense, the use of a type that requires full 834 -- initialization is a weak part of the generic contract. 835 836 function Missing_Subunits return Boolean; 837 -- We suppress warnings when there are missing subunits, because this 838 -- may generate too many false positives: entities in a parent may only 839 -- be referenced in one of the subunits. We make an exception for 840 -- subunits that contain no other stubs. 841 842 procedure Output_Reference_Error (M : String); 843 -- Used to output an error message. Deals with posting the error on the 844 -- body formal in the accept case. 845 846 function Publicly_Referenceable (Ent : Entity_Id) return Boolean; 847 -- This is true if the entity in question is potentially referenceable 848 -- from another unit. This is true for entities in packages that are at 849 -- the library level. 850 851 function Warnings_Off_E1 return Boolean; 852 -- Return True if Warnings_Off is set for E1, or for its Etype (E1T), 853 -- or for the base type of E1T. 854 855 ----------------- 856 -- Body_Formal -- 857 ----------------- 858 859 function Body_Formal 860 (E : Entity_Id; 861 Accept_Statement : Node_Id) return Entity_Id 862 is 863 Body_Param : Node_Id; 864 Body_E : Entity_Id; 865 866 begin 867 -- Loop to find matching parameter in accept statement 868 869 Body_Param := First (Parameter_Specifications (Accept_Statement)); 870 while Present (Body_Param) loop 871 Body_E := Defining_Identifier (Body_Param); 872 873 if Chars (Body_E) = Chars (E) then 874 return Body_E; 875 end if; 876 877 Next (Body_Param); 878 end loop; 879 880 -- Should never fall through, should always find a match 881 882 raise Program_Error; 883 end Body_Formal; 884 885 ------------------------- 886 -- Generic_Body_Formal -- 887 ------------------------- 888 889 function Generic_Body_Formal (E : Entity_Id) return Entity_Id is 890 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Scope (E)); 891 Gen_Body : constant Entity_Id := Corresponding_Body (Gen_Decl); 892 Form : Entity_Id; 893 894 begin 895 if No (Gen_Body) then 896 return E; 897 898 else 899 Form := First_Entity (Gen_Body); 900 while Present (Form) loop 901 if Chars (Form) = Chars (E) then 902 return Form; 903 end if; 904 905 Next_Entity (Form); 906 end loop; 907 end if; 908 909 -- Should never fall through, should always find a match 910 911 raise Program_Error; 912 end Generic_Body_Formal; 913 914 --------------------------------- 915 -- May_Need_Initialized_Actual -- 916 --------------------------------- 917 918 procedure May_Need_Initialized_Actual (Ent : Entity_Id) is 919 T : constant Entity_Id := Etype (Ent); 920 Par : constant Node_Id := Parent (T); 921 922 begin 923 if not Is_Generic_Type (T) then 924 null; 925 926 elsif (Nkind (Par)) = N_Private_Extension_Declaration then 927 928 -- We only indicate the first such variable in the generic. 929 930 if No (Uninitialized_Variable (Par)) then 931 Set_Uninitialized_Variable (Par, Ent); 932 end if; 933 934 elsif (Nkind (Par)) = N_Formal_Type_Declaration 935 and then Nkind (Formal_Type_Definition (Par)) = 936 N_Formal_Private_Type_Definition 937 then 938 if No (Uninitialized_Variable (Formal_Type_Definition (Par))) then 939 Set_Uninitialized_Variable (Formal_Type_Definition (Par), Ent); 940 end if; 941 end if; 942 end May_Need_Initialized_Actual; 943 944 ---------------------- 945 -- Missing_Subunits -- 946 ---------------------- 947 948 function Missing_Subunits return Boolean is 949 D : Node_Id; 950 951 begin 952 if not Unloaded_Subunits then 953 954 -- Normal compilation, all subunits are present 955 956 return False; 957 958 elsif E /= Main_Unit_Entity then 959 960 -- No warnings on a stub that is not the main unit 961 962 return True; 963 964 elsif Nkind (Unit_Declaration_Node (E)) in N_Proper_Body then 965 D := First (Declarations (Unit_Declaration_Node (E))); 966 while Present (D) loop 967 968 -- No warnings if the proper body contains nested stubs 969 970 if Nkind (D) in N_Body_Stub then 971 return True; 972 end if; 973 974 Next (D); 975 end loop; 976 977 return False; 978 979 else 980 -- Missing stubs elsewhere 981 982 return True; 983 end if; 984 end Missing_Subunits; 985 986 ---------------------------- 987 -- Output_Reference_Error -- 988 ---------------------------- 989 990 procedure Output_Reference_Error (M : String) is 991 begin 992 -- Never issue messages for internal names or renamings 993 994 if Is_Internal_Name (Chars (E1)) 995 or else Nkind (Parent (E1)) = N_Object_Renaming_Declaration 996 then 997 return; 998 end if; 999 1000 -- Don't output message for IN OUT formal unless we have the warning 1001 -- flag specifically set. It is a bit odd to distinguish IN OUT 1002 -- formals from other cases. This distinction is historical in 1003 -- nature. Warnings for IN OUT formals were added fairly late. 1004 1005 if Ekind (E1) = E_In_Out_Parameter 1006 and then not Check_Unreferenced_Formals 1007 then 1008 return; 1009 end if; 1010 1011 -- Other than accept case, post error on defining identifier 1012 1013 if No (Anod) then 1014 Error_Msg_N (M, E1); 1015 1016 -- Accept case, find body formal to post the message 1017 1018 else 1019 Error_Msg_NE (M, Body_Formal (E1, Accept_Statement => Anod), E1); 1020 1021 end if; 1022 end Output_Reference_Error; 1023 1024 ---------------------------- 1025 -- Publicly_Referenceable -- 1026 ---------------------------- 1027 1028 function Publicly_Referenceable (Ent : Entity_Id) return Boolean is 1029 P : Node_Id; 1030 Prev : Node_Id; 1031 1032 begin 1033 -- A formal parameter is never referenceable outside the body of its 1034 -- subprogram or entry. 1035 1036 if Is_Formal (Ent) then 1037 return False; 1038 end if; 1039 1040 -- Examine parents to look for a library level package spec. But if 1041 -- we find a body or block or other similar construct along the way, 1042 -- we cannot be referenced. 1043 1044 Prev := Ent; 1045 P := Parent (Ent); 1046 loop 1047 case Nkind (P) is 1048 1049 -- If we get to top of tree, then publicly referenceable 1050 1051 when N_Empty => 1052 return True; 1053 1054 -- If we reach a generic package declaration, then always 1055 -- consider this referenceable, since any instantiation will 1056 -- have access to the entities in the generic package. Note 1057 -- that the package itself may not be instantiated, but then 1058 -- we will get a warning for the package entity. 1059 1060 -- Note that generic formal parameters are themselves not 1061 -- publicly referenceable in an instance, and warnings on them 1062 -- are useful. 1063 1064 when N_Generic_Package_Declaration => 1065 return 1066 not Is_List_Member (Prev) 1067 or else List_Containing (Prev) /= 1068 Generic_Formal_Declarations (P); 1069 1070 -- Similarly, the generic formals of a generic subprogram are 1071 -- not accessible. 1072 1073 when N_Generic_Subprogram_Declaration => 1074 if Is_List_Member (Prev) 1075 and then List_Containing (Prev) = 1076 Generic_Formal_Declarations (P) 1077 then 1078 return False; 1079 else 1080 P := Parent (P); 1081 end if; 1082 1083 -- If we reach a subprogram body, entity is not referenceable 1084 -- unless it is the defining entity of the body. This will 1085 -- happen, e.g. when a function is an attribute renaming that 1086 -- is rewritten as a body. 1087 1088 when N_Subprogram_Body => 1089 if Ent /= Defining_Entity (P) then 1090 return False; 1091 else 1092 P := Parent (P); 1093 end if; 1094 1095 -- If we reach any other body, definitely not referenceable 1096 1097 when N_Block_Statement 1098 | N_Entry_Body 1099 | N_Package_Body 1100 | N_Protected_Body 1101 | N_Subunit 1102 | N_Task_Body 1103 => 1104 return False; 1105 1106 -- For all other cases, keep looking up tree 1107 1108 when others => 1109 Prev := P; 1110 P := Parent (P); 1111 end case; 1112 end loop; 1113 end Publicly_Referenceable; 1114 1115 --------------------- 1116 -- Warnings_Off_E1 -- 1117 --------------------- 1118 1119 function Warnings_Off_E1 return Boolean is 1120 begin 1121 return Has_Warnings_Off (E1T) 1122 or else Has_Warnings_Off (Base_Type (E1T)) 1123 or else Warnings_Off_Check_Spec (E1); 1124 end Warnings_Off_E1; 1125 1126 -- Start of processing for Check_References 1127 1128 begin 1129 Process_Deferred_References; 1130 1131 -- No messages if warnings are suppressed, or if we have detected any 1132 -- real errors so far (this last check avoids junk messages resulting 1133 -- from errors, e.g. a subunit that is not loaded). 1134 1135 if Warning_Mode = Suppress or else Serious_Errors_Detected /= 0 then 1136 return; 1137 end if; 1138 1139 -- We also skip the messages if any subunits were not loaded (see 1140 -- comment in Sem_Ch10 to understand how this is set, and why it is 1141 -- necessary to suppress the warnings in this case). 1142 1143 if Missing_Subunits then 1144 return; 1145 end if; 1146 1147 -- Otherwise loop through entities, looking for suspicious stuff 1148 1149 E1 := First_Entity (E); 1150 while Present (E1) loop 1151 E1T := Etype (E1); 1152 1153 -- We are only interested in source entities. We also don't issue 1154 -- warnings within instances, since the proper place for such 1155 -- warnings is on the template when it is compiled, and we don't 1156 -- issue warnings for variables with names like Junk, Discard etc. 1157 1158 if Comes_From_Source (E1) 1159 and then Instantiation_Location (Sloc (E1)) = No_Location 1160 then 1161 -- We are interested in variables and out/in-out parameters, but 1162 -- we exclude protected types, too complicated to worry about. 1163 1164 if Ekind (E1) = E_Variable 1165 or else 1166 (Ekind (E1) in E_Out_Parameter | E_In_Out_Parameter 1167 and then not Is_Protected_Type (Current_Scope)) 1168 then 1169 -- If the formal has a class-wide type, retrieve its type 1170 -- because checks below depend on its private nature. 1171 1172 if Is_Class_Wide_Type (E1T) then 1173 E1T := Etype (E1T); 1174 end if; 1175 1176 -- Case of an unassigned variable 1177 1178 -- First gather any Unset_Reference indication for E1. In the 1179 -- case of a parameter, it is the Spec_Entity that is relevant. 1180 1181 if Ekind (E1) = E_Out_Parameter 1182 and then Present (Spec_Entity (E1)) 1183 then 1184 UR := Unset_Reference (Spec_Entity (E1)); 1185 else 1186 UR := Unset_Reference (E1); 1187 end if; 1188 1189 -- Special processing for access types 1190 1191 if Present (UR) and then Is_Access_Type (E1T) then 1192 1193 -- For access types, the only time we made a UR entry was 1194 -- for a dereference, and so we post the appropriate warning 1195 -- here (note that the dereference may not be explicit in 1196 -- the source, for example in the case of a dispatching call 1197 -- with an anonymous access controlling formal, or of an 1198 -- assignment of a pointer involving discriminant check on 1199 -- the designated object). 1200 1201 if not Warnings_Off_E1 then 1202 Error_Msg_NE ("??& may be null!", UR, E1); 1203 end if; 1204 1205 goto Continue; 1206 1207 -- Case of variable that could be a constant. Note that we 1208 -- never signal such messages for generic package entities, 1209 -- since a given instance could have modifications outside 1210 -- the package. 1211 1212 -- Note that we used to check Address_Taken here, but we don't 1213 -- want to do that since it can be set for non-source cases, 1214 -- e.g. the Unrestricted_Access from a valid attribute, and 1215 -- the wanted effect is included in Never_Set_In_Source. 1216 1217 elsif Warn_On_Constant 1218 and then (Ekind (E1) = E_Variable 1219 and then Has_Initial_Value (E1)) 1220 and then Never_Set_In_Source_Check_Spec (E1) 1221 and then not Generic_Package_Spec_Entity (E1) 1222 then 1223 -- A special case, if this variable is volatile and not 1224 -- imported, it is not helpful to tell the programmer 1225 -- to mark the variable as constant, since this would be 1226 -- illegal by virtue of RM C.6(13). Instead we suggest 1227 -- using pragma Export (can't be Import because of the 1228 -- initial value). 1229 1230 if (Is_Volatile (E1) or else Has_Volatile_Components (E1)) 1231 and then not Is_Imported (E1) 1232 then 1233 Error_Msg_N 1234 ("?k?& is not modified, consider pragma Export for " 1235 & "volatile variable!", E1); 1236 1237 -- Another special case, Exception_Occurrence, this catches 1238 -- the case of exception choice (and a bit more too, but not 1239 -- worth doing more investigation here). 1240 1241 elsif Is_RTE (E1T, RE_Exception_Occurrence) then 1242 null; 1243 1244 -- Here we give the warning if referenced and no pragma 1245 -- Unreferenced or Unmodified is present. 1246 1247 else 1248 -- Variable case 1249 1250 if Ekind (E1) = E_Variable then 1251 if Referenced_Check_Spec (E1) 1252 and then not Has_Pragma_Unreferenced_Check_Spec (E1) 1253 and then not Has_Pragma_Unmodified_Check_Spec (E1) 1254 then 1255 if not Warnings_Off_E1 1256 and then not Has_Junk_Name (E1) 1257 then 1258 Error_Msg_N -- CODEFIX 1259 ("?k?& is not modified, " 1260 & "could be declared constant!", 1261 E1); 1262 end if; 1263 end if; 1264 end if; 1265 end if; 1266 1267 -- Other cases of a variable or parameter never set in source 1268 1269 elsif Never_Set_In_Source_Check_Spec (E1) 1270 1271 -- No warning if warning for this case turned off 1272 1273 and then Warn_On_No_Value_Assigned 1274 1275 -- No warning if address taken somewhere 1276 1277 and then not Address_Taken (E1) 1278 1279 -- No warning if explicit initial value 1280 1281 and then not Has_Initial_Value (E1) 1282 1283 -- No warning for generic package spec entities, since we 1284 -- might set them in a child unit or something like that 1285 1286 and then not Generic_Package_Spec_Entity (E1) 1287 1288 -- No warning if fully initialized type, except that for 1289 -- this purpose we do not consider access types to qualify 1290 -- as fully initialized types (relying on an access type 1291 -- variable being null when it is never set is a bit odd). 1292 1293 -- Also we generate warning for an out parameter that is 1294 -- never referenced, since again it seems odd to rely on 1295 -- default initialization to set an out parameter value. 1296 1297 and then (Is_Access_Type (E1T) 1298 or else Ekind (E1) = E_Out_Parameter 1299 or else not Is_Fully_Initialized_Type (E1T)) 1300 then 1301 -- Do not output complaint about never being assigned a 1302 -- value if a pragma Unmodified applies to the variable 1303 -- we are examining, or if it is a parameter, if there is 1304 -- a pragma Unreferenced for the corresponding spec, or 1305 -- if the type is marked as having unreferenced objects. 1306 -- The last is a little peculiar, but better too few than 1307 -- too many warnings in this situation. 1308 1309 if Has_Pragma_Unreferenced_Objects (E1T) 1310 or else Has_Pragma_Unmodified_Check_Spec (E1) 1311 then 1312 null; 1313 1314 -- IN OUT parameter case where parameter is referenced. We 1315 -- separate this out, since this is the case where we delay 1316 -- output of the warning until more information is available 1317 -- (about use in an instantiation or address being taken). 1318 1319 elsif Ekind (E1) = E_In_Out_Parameter 1320 and then Referenced_Check_Spec (E1) 1321 then 1322 -- Suppress warning if private type, and the procedure 1323 -- has a separate declaration in a different unit. This 1324 -- is the case where the client of a package sees only 1325 -- the private type, and it may be quite reasonable 1326 -- for the logical view to be IN OUT, even if the 1327 -- implementation ends up using access types or some 1328 -- other method to achieve the local effect of a 1329 -- modification. On the other hand if the spec and body 1330 -- are in the same unit, we are in the package body and 1331 -- there we have less excuse for a junk IN OUT parameter. 1332 1333 if Has_Private_Declaration (E1T) 1334 and then Present (Spec_Entity (E1)) 1335 and then not In_Same_Source_Unit (E1, Spec_Entity (E1)) 1336 then 1337 null; 1338 1339 -- Suppress warning for any parameter of a dispatching 1340 -- operation, since it is quite reasonable to have an 1341 -- operation that is overridden, and for some subclasses 1342 -- needs the formal to be IN OUT and for others happens 1343 -- not to assign it. 1344 1345 elsif Is_Dispatching_Operation 1346 (Scope (Goto_Spec_Entity (E1))) 1347 then 1348 null; 1349 1350 -- Suppress warning if composite type contains any access 1351 -- component, since the logical effect of modifying a 1352 -- parameter may be achieved by modifying a referenced 1353 -- object. 1354 1355 elsif Is_Composite_Type (E1T) 1356 and then Has_Access_Values (E1T) 1357 then 1358 null; 1359 1360 -- Suppress warning on formals of an entry body. All 1361 -- references are attached to the formal in the entry 1362 -- declaration, which are marked Is_Entry_Formal. 1363 1364 elsif Ekind (Scope (E1)) = E_Entry 1365 and then not Is_Entry_Formal (E1) 1366 then 1367 null; 1368 1369 -- OK, looks like warning for an IN OUT parameter that 1370 -- could be IN makes sense, but we delay the output of 1371 -- the warning, pending possibly finding out later on 1372 -- that the associated subprogram is used as a generic 1373 -- actual, or its address/access is taken. In these two 1374 -- cases, we suppress the warning because the context may 1375 -- force use of IN OUT, even if in this particular case 1376 -- the formal is not modified. 1377 1378 else 1379 -- Suppress the warnings for a junk name 1380 1381 if not Has_Junk_Name (E1) then 1382 In_Out_Warnings.Append (E1); 1383 end if; 1384 end if; 1385 1386 -- Other cases of formals 1387 1388 elsif Is_Formal (E1) then 1389 if not Is_Trivial_Subprogram (Scope (E1)) then 1390 if Referenced_Check_Spec (E1) then 1391 if not Has_Pragma_Unmodified_Check_Spec (E1) 1392 and then not Warnings_Off_E1 1393 and then not Has_Junk_Name (E1) 1394 then 1395 Output_Reference_Error 1396 ("?f?formal parameter& is read but " 1397 & "never assigned!"); 1398 end if; 1399 1400 elsif not Has_Pragma_Unreferenced_Check_Spec (E1) 1401 and then not Warnings_Off_E1 1402 and then not Has_Junk_Name (E1) 1403 then 1404 Output_Reference_Error 1405 ("?f?formal parameter& is not referenced!"); 1406 end if; 1407 end if; 1408 1409 -- Case of variable 1410 1411 else 1412 if Referenced (E1) then 1413 if not Has_Unmodified (E1) 1414 and then not Warnings_Off_E1 1415 and then not Has_Junk_Name (E1) 1416 then 1417 if Is_Access_Type (E1T) 1418 or else 1419 not Is_Partially_Initialized_Type (E1T, False) 1420 then 1421 Output_Reference_Error 1422 ("?v?variable& is read but never assigned!"); 1423 end if; 1424 1425 May_Need_Initialized_Actual (E1); 1426 end if; 1427 1428 elsif not Has_Unreferenced (E1) 1429 and then not Warnings_Off_E1 1430 and then not Has_Junk_Name (E1) 1431 then 1432 Output_Reference_Error -- CODEFIX 1433 ("?v?variable& is never read and never assigned!"); 1434 end if; 1435 1436 -- Deal with special case where this variable is hidden 1437 -- by a loop variable. 1438 1439 if Ekind (E1) = E_Variable 1440 and then Present (Hiding_Loop_Variable (E1)) 1441 and then not Warnings_Off_E1 1442 then 1443 Error_Msg_N 1444 ("?v?for loop implicitly declares loop variable!", 1445 Hiding_Loop_Variable (E1)); 1446 1447 Error_Msg_Sloc := Sloc (E1); 1448 Error_Msg_N 1449 ("\?v?declaration hides & declared#!", 1450 Hiding_Loop_Variable (E1)); 1451 end if; 1452 end if; 1453 1454 goto Continue; 1455 end if; 1456 1457 -- Check for unset reference. If type of object has 1458 -- preelaborable initialization, warning is misleading. 1459 1460 if Warn_On_No_Value_Assigned 1461 and then Present (UR) 1462 and then not Known_To_Have_Preelab_Init (Etype (E1)) 1463 then 1464 1465 -- For other than access type, go back to original node to 1466 -- deal with case where original unset reference has been 1467 -- rewritten during expansion. 1468 1469 -- In some cases, the original node may be a type 1470 -- conversion, a qualification or an attribute reference and 1471 -- in this case we want the object entity inside. Same for 1472 -- an expression with actions. 1473 1474 UR := Original_Node (UR); 1475 loop 1476 if Nkind (UR) in N_Expression_With_Actions 1477 | N_Qualified_Expression 1478 | N_Type_Conversion 1479 then 1480 UR := Expression (UR); 1481 1482 elsif Nkind (UR) = N_Attribute_Reference then 1483 UR := Prefix (UR); 1484 1485 else 1486 exit; 1487 end if; 1488 end loop; 1489 1490 -- Don't issue warning if appearing inside Initial_Condition 1491 -- pragma or aspect, since that expression is not evaluated 1492 -- at the point where it occurs in the source. 1493 1494 if In_Pragma_Expression (UR, Name_Initial_Condition) then 1495 goto Continue; 1496 end if; 1497 1498 -- Here we issue the warning, all checks completed 1499 1500 -- If we have a return statement, this was a case of an OUT 1501 -- parameter not being set at the time of the return. (Note: 1502 -- it can't be N_Extended_Return_Statement, because those 1503 -- are only for functions, and functions do not allow OUT 1504 -- parameters.) 1505 1506 if not Is_Trivial_Subprogram (Scope (E1)) then 1507 if Nkind (UR) = N_Simple_Return_Statement 1508 and then not Has_Pragma_Unmodified_Check_Spec (E1) 1509 then 1510 if not Warnings_Off_E1 1511 and then not Has_Junk_Name (E1) 1512 then 1513 Error_Msg_NE 1514 ("?v?OUT parameter& not set before return", 1515 UR, E1); 1516 end if; 1517 1518 -- If the unset reference is a selected component 1519 -- prefix from source, mention the component as well. 1520 -- If the selected component comes from expansion, all 1521 -- we know is that the entity is not fully initialized 1522 -- at the point of the reference. Locate a random 1523 -- uninitialized component to get a better message. 1524 1525 elsif Nkind (Parent (UR)) = N_Selected_Component then 1526 Error_Msg_Node_2 := Selector_Name (Parent (UR)); 1527 1528 if not Comes_From_Source (Parent (UR)) then 1529 declare 1530 Comp : Entity_Id; 1531 1532 begin 1533 Comp := First_Entity (E1T); 1534 while Present (Comp) loop 1535 if Ekind (Comp) = E_Component 1536 and then Nkind (Parent (Comp)) = 1537 N_Component_Declaration 1538 and then No (Expression (Parent (Comp))) 1539 then 1540 Error_Msg_Node_2 := Comp; 1541 exit; 1542 end if; 1543 1544 Next_Entity (Comp); 1545 end loop; 1546 end; 1547 end if; 1548 1549 -- Issue proper warning. This is a case of referencing 1550 -- a variable before it has been explicitly assigned. 1551 -- For access types, UR was only set for dereferences, 1552 -- so the issue is that the value may be null. 1553 1554 if not Is_Trivial_Subprogram (Scope (E1)) then 1555 if not Warnings_Off_E1 then 1556 if Is_Access_Type (Etype (Parent (UR))) then 1557 Error_Msg_N ("??`&.&` may be null!", UR); 1558 else 1559 Error_Msg_N 1560 ("??`&.&` may be referenced before " 1561 & "it has a value!", UR); 1562 end if; 1563 end if; 1564 end if; 1565 1566 -- All other cases of unset reference active 1567 1568 elsif not Warnings_Off_E1 then 1569 Error_Msg_N 1570 ("??& may be referenced before it has a value!", UR); 1571 end if; 1572 end if; 1573 1574 goto Continue; 1575 1576 end if; 1577 end if; 1578 1579 -- Then check for unreferenced entities. Note that we are only 1580 -- interested in entities whose Referenced flag is not set. 1581 1582 if not Referenced_Check_Spec (E1) 1583 1584 -- If Referenced_As_LHS is set, then that's still interesting 1585 -- (potential "assigned but never read" case), but not if we 1586 -- have pragma Unreferenced, which cancels this warning. 1587 1588 and then (not Referenced_As_LHS_Check_Spec (E1) 1589 or else not Has_Unreferenced (E1)) 1590 1591 -- Check that warnings on unreferenced entities are enabled 1592 1593 and then 1594 ((Check_Unreferenced and then not Is_Formal (E1)) 1595 1596 -- Case of warning on unreferenced formal 1597 1598 or else (Check_Unreferenced_Formals and then Is_Formal (E1)) 1599 1600 -- Case of warning on unread variables modified by an 1601 -- assignment, or an OUT parameter if it is the only one. 1602 1603 or else (Warn_On_Modified_Unread 1604 and then Referenced_As_LHS_Check_Spec (E1)) 1605 1606 -- Case of warning on any unread OUT parameter (note such 1607 -- indications are only set if the appropriate warning 1608 -- options were set, so no need to recheck here.) 1609 1610 or else Referenced_As_Out_Parameter_Check_Spec (E1)) 1611 1612 -- All other entities, including local packages that cannot be 1613 -- referenced from elsewhere, including those declared within a 1614 -- package body. 1615 1616 and then (Is_Object (E1) 1617 or else Is_Type (E1) 1618 or else Ekind (E1) = E_Label 1619 or else Ekind (E1) in E_Exception 1620 | E_Named_Integer 1621 | E_Named_Real 1622 or else Is_Overloadable (E1) 1623 1624 -- Package case, if the main unit is a package spec 1625 -- or generic package spec, then there may be a 1626 -- corresponding body that references this package 1627 -- in some other file. Otherwise we can be sure 1628 -- that there is no other reference. 1629 1630 or else 1631 (Ekind (E1) = E_Package 1632 and then 1633 not Is_Package_Or_Generic_Package 1634 (Cunit_Entity (Current_Sem_Unit)))) 1635 1636 -- Exclude instantiations, since there is no reason why every 1637 -- entity in an instantiation should be referenced. 1638 1639 and then Instantiation_Location (Sloc (E1)) = No_Location 1640 1641 -- Exclude formal parameters from bodies if the corresponding 1642 -- spec entity has been referenced in the case where there is 1643 -- a separate spec. 1644 1645 and then not (Is_Formal (E1) 1646 and then Ekind (Scope (E1)) = E_Subprogram_Body 1647 and then Present (Spec_Entity (E1)) 1648 and then Referenced (Spec_Entity (E1))) 1649 1650 -- Consider private type referenced if full view is referenced. 1651 -- If there is not full view, this is a generic type on which 1652 -- warnings are also useful. 1653 1654 and then 1655 not (Is_Private_Type (E1) 1656 and then Present (Full_View (E1)) 1657 and then Referenced (Full_View (E1))) 1658 1659 -- Don't worry about full view, only about private type 1660 1661 and then not Has_Private_Declaration (E1) 1662 1663 -- Eliminate dispatching operations from consideration, we 1664 -- cannot tell if these are referenced or not in any easy 1665 -- manner (note this also catches Adjust/Finalize/Initialize). 1666 1667 and then not Is_Dispatching_Operation (E1) 1668 1669 -- Check entity that can be publicly referenced (we do not give 1670 -- messages for such entities, since there could be other 1671 -- units, not involved in this compilation, that contain 1672 -- relevant references. 1673 1674 and then not Publicly_Referenceable (E1) 1675 1676 -- Class wide types are marked as source entities, but they are 1677 -- not really source entities, and are always created, so we do 1678 -- not care if they are not referenced. 1679 1680 and then Ekind (E1) /= E_Class_Wide_Type 1681 1682 -- Objects other than parameters of task types are allowed to 1683 -- be non-referenced, since they start up tasks. 1684 1685 and then ((Ekind (E1) /= E_Variable 1686 and then Ekind (E1) /= E_Constant 1687 and then Ekind (E1) /= E_Component) 1688 or else not Is_Task_Type (E1T)) 1689 1690 -- For subunits, only place warnings on the main unit itself, 1691 -- since parent units are not completely compiled. 1692 1693 and then (Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit 1694 or else Get_Source_Unit (E1) = Main_Unit) 1695 1696 -- No warning on a return object, because these are often 1697 -- created with a single expression and an implicit return. 1698 -- If the object is a variable there will be a warning 1699 -- indicating that it could be declared constant. 1700 1701 and then not 1702 (Ekind (E1) = E_Constant and then Is_Return_Object (E1)) 1703 then 1704 -- Suppress warnings in internal units if not in -gnatg mode 1705 -- (these would be junk warnings for an applications program, 1706 -- since they refer to problems in internal units). 1707 1708 if GNAT_Mode or else not In_Internal_Unit (E1) then 1709 -- We do not immediately flag the error. This is because we 1710 -- have not expanded generic bodies yet, and they may have 1711 -- the missing reference. So instead we park the entity on a 1712 -- list, for later processing. However for the case of an 1713 -- accept statement we want to output messages now, since 1714 -- we know we already have all information at hand, and we 1715 -- also want to have separate warnings for each accept 1716 -- statement for the same entry. 1717 1718 if Present (Anod) then 1719 pragma Assert (Is_Formal (E1)); 1720 1721 -- The unreferenced entity is E1, but post the warning 1722 -- on the body entity for this accept statement. 1723 1724 if not Warnings_Off_E1 then 1725 Warn_On_Unreferenced_Entity 1726 (E1, Body_Formal (E1, Accept_Statement => Anod)); 1727 end if; 1728 1729 elsif not Warnings_Off_E1 1730 and then not Has_Junk_Name (E1) 1731 then 1732 if Is_Formal (E1) 1733 and then Nkind (Unit_Declaration_Node (Scope (E1))) 1734 = N_Generic_Subprogram_Declaration 1735 then 1736 Unreferenced_Entities.Append 1737 (Generic_Body_Formal (E1)); 1738 else 1739 Unreferenced_Entities.Append (E1); 1740 end if; 1741 end if; 1742 end if; 1743 1744 -- Generic units are referenced in the generic body, but if they 1745 -- are not public and never instantiated we want to force a 1746 -- warning on them. We treat them as redundant constructs to 1747 -- minimize noise. 1748 1749 elsif Is_Generic_Subprogram (E1) 1750 and then not Is_Instantiated (E1) 1751 and then not Publicly_Referenceable (E1) 1752 and then Instantiation_Depth (Sloc (E1)) = 0 1753 and then Warn_On_Redundant_Constructs 1754 then 1755 if not Warnings_Off_E1 and then not Has_Junk_Name (E1) then 1756 Unreferenced_Entities.Append (E1); 1757 1758 -- Force warning on entity 1759 1760 Set_Referenced (E1, False); 1761 end if; 1762 end if; 1763 end if; 1764 1765 -- Recurse into nested package or block. Do not recurse into a formal 1766 -- package, because the corresponding body is not analyzed. 1767 1768 <<Continue>> 1769 if (Is_Package_Or_Generic_Package (E1) 1770 and then Nkind (Parent (E1)) = N_Package_Specification 1771 and then 1772 Nkind (Original_Node (Unit_Declaration_Node (E1))) /= 1773 N_Formal_Package_Declaration) 1774 1775 or else Ekind (E1) = E_Block 1776 then 1777 Check_References (E1); 1778 end if; 1779 1780 Next_Entity (E1); 1781 end loop; 1782 end Check_References; 1783 1784 --------------------------- 1785 -- Check_Unset_Reference -- 1786 --------------------------- 1787 1788 procedure Check_Unset_Reference (N : Node_Id) is 1789 Typ : constant Entity_Id := Etype (N); 1790 1791 function Is_OK_Fully_Initialized return Boolean; 1792 -- This function returns true if the given node N is fully initialized 1793 -- so that the reference is safe as far as this routine is concerned. 1794 -- Safe generally means that the type of N is a fully initialized type. 1795 -- The one special case is that for access types, which are always fully 1796 -- initialized, we don't consider a dereference OK since it will surely 1797 -- be dereferencing a null value, which won't do. 1798 1799 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean; 1800 -- Used to test indexed or selected component or slice to see if the 1801 -- evaluation of the prefix depends on a dereference, and if so, returns 1802 -- True, in which case we always check the prefix, even if we know that 1803 -- the referenced component is initialized. Pref is the prefix to test. 1804 1805 ----------------------------- 1806 -- Is_OK_Fully_Initialized -- 1807 ----------------------------- 1808 1809 function Is_OK_Fully_Initialized return Boolean is 1810 begin 1811 if Is_Access_Type (Typ) and then Is_Dereferenced (N) then 1812 return False; 1813 1814 -- A type subject to pragma Default_Initial_Condition may be fully 1815 -- default initialized depending on inheritance and the argument of 1816 -- the pragma (SPARK RM 3.1 and SPARK RM 7.3.3). 1817 1818 elsif Has_Fully_Default_Initializing_DIC_Pragma (Typ) then 1819 return True; 1820 1821 else 1822 return Is_Fully_Initialized_Type (Typ); 1823 end if; 1824 end Is_OK_Fully_Initialized; 1825 1826 ---------------------------- 1827 -- Prefix_Has_Dereference -- 1828 ---------------------------- 1829 1830 function Prefix_Has_Dereference (Pref : Node_Id) return Boolean is 1831 begin 1832 -- If prefix is of an access type, it certainly needs a dereference 1833 1834 if Is_Access_Type (Etype (Pref)) then 1835 return True; 1836 1837 -- If prefix is explicit dereference, that's a dereference for sure 1838 1839 elsif Nkind (Pref) = N_Explicit_Dereference then 1840 return True; 1841 1842 -- If prefix is itself a component reference or slice check prefix 1843 1844 elsif Nkind (Pref) = N_Slice 1845 or else Nkind (Pref) = N_Indexed_Component 1846 or else Nkind (Pref) = N_Selected_Component 1847 then 1848 return Prefix_Has_Dereference (Prefix (Pref)); 1849 1850 -- All other cases do not involve a dereference 1851 1852 else 1853 return False; 1854 end if; 1855 end Prefix_Has_Dereference; 1856 1857 -- Start of processing for Check_Unset_Reference 1858 1859 begin 1860 -- Nothing to do if warnings suppressed 1861 1862 if Warning_Mode = Suppress then 1863 return; 1864 end if; 1865 1866 -- Nothing to do for numeric or string literal. Do this test early to 1867 -- save time in a common case (it does not matter that we do not include 1868 -- character literal here, since that will be caught later on in the 1869 -- when others branch of the case statement). 1870 1871 if Nkind (N) in N_Numeric_Or_String_Literal then 1872 return; 1873 end if; 1874 1875 -- Ignore reference unless it comes from source. Almost always if we 1876 -- have a reference from generated code, it is bogus (e.g. calls to init 1877 -- procs to set default discriminant values). 1878 1879 if not Comes_From_Source (Original_Node (N)) then 1880 return; 1881 end if; 1882 1883 -- Otherwise see what kind of node we have. If the entity already has an 1884 -- unset reference, it is not necessarily the earliest in the text, 1885 -- because resolution of the prefix of selected components is completed 1886 -- before the resolution of the selected component itself. As a result, 1887 -- given (R /= null and then R.X > 0), the occurrences of R are examined 1888 -- in right-to-left order. If there is already an unset reference, we 1889 -- check whether N is earlier before proceeding. 1890 1891 case Nkind (N) is 1892 1893 -- For identifier or expanded name, examine the entity involved 1894 1895 when N_Expanded_Name 1896 | N_Identifier 1897 => 1898 declare 1899 E : constant Entity_Id := Entity (N); 1900 1901 begin 1902 if Ekind (E) in E_Variable | E_Out_Parameter 1903 and then Never_Set_In_Source_Check_Spec (E) 1904 and then not Has_Initial_Value (E) 1905 and then (No (Unset_Reference (E)) 1906 or else 1907 Earlier_In_Extended_Unit 1908 (Sloc (N), Sloc (Unset_Reference (E)))) 1909 and then not Has_Pragma_Unmodified_Check_Spec (E) 1910 and then not Warnings_Off_Check_Spec (E) 1911 and then not Has_Junk_Name (E) 1912 then 1913 -- We may have an unset reference. The first test is whether 1914 -- this is an access to a discriminant of a record or a 1915 -- component with default initialization. Both of these 1916 -- cases can be ignored, since the actual object that is 1917 -- referenced is definitely initialized. Note that this 1918 -- covers the case of reading discriminants of an OUT 1919 -- parameter, which is OK even in Ada 83. 1920 1921 -- Note that we are only interested in a direct reference to 1922 -- a record component here. If the reference is through an 1923 -- access type, then the access object is being referenced, 1924 -- not the record, and still deserves an unset reference. 1925 1926 if Nkind (Parent (N)) = N_Selected_Component 1927 and not Is_Access_Type (Typ) 1928 then 1929 declare 1930 ES : constant Entity_Id := 1931 Entity (Selector_Name (Parent (N))); 1932 begin 1933 if Ekind (ES) = E_Discriminant 1934 or else 1935 (Present (Declaration_Node (ES)) 1936 and then 1937 Present (Expression (Declaration_Node (ES)))) 1938 then 1939 return; 1940 end if; 1941 end; 1942 end if; 1943 1944 -- Exclude fully initialized types 1945 1946 if Is_OK_Fully_Initialized then 1947 return; 1948 end if; 1949 1950 -- Here we have a potential unset reference. But before we 1951 -- get worried about it, we have to make sure that the 1952 -- entity declaration is in the same procedure as the 1953 -- reference, since if they are in separate procedures, then 1954 -- we have no idea about sequential execution. 1955 1956 -- The tests in the loop below catch all such cases, but do 1957 -- allow the reference to appear in a loop, block, or 1958 -- package spec that is nested within the declaring scope. 1959 -- As always, it is possible to construct cases where the 1960 -- warning is wrong, that is why it is a warning. 1961 1962 Potential_Unset_Reference : declare 1963 SR : Entity_Id; 1964 SE : constant Entity_Id := Scope (E); 1965 1966 function Within_Postcondition return Boolean; 1967 -- Returns True if N is within a Postcondition, a 1968 -- Refined_Post, an Ensures component in a Test_Case, 1969 -- or a Contract_Cases. 1970 1971 -------------------------- 1972 -- Within_Postcondition -- 1973 -------------------------- 1974 1975 function Within_Postcondition return Boolean is 1976 Nod, P : Node_Id; 1977 1978 begin 1979 Nod := Parent (N); 1980 while Present (Nod) loop 1981 if Nkind (Nod) = N_Pragma 1982 and then 1983 Pragma_Name_Unmapped (Nod) 1984 in Name_Postcondition 1985 | Name_Refined_Post 1986 | Name_Contract_Cases 1987 then 1988 return True; 1989 1990 elsif Present (Parent (Nod)) then 1991 P := Parent (Nod); 1992 1993 if Nkind (P) = N_Pragma 1994 and then Pragma_Name (P) = 1995 Name_Test_Case 1996 and then Nod = Test_Case_Arg (P, Name_Ensures) 1997 then 1998 return True; 1999 end if; 2000 end if; 2001 2002 Nod := Parent (Nod); 2003 end loop; 2004 2005 return False; 2006 end Within_Postcondition; 2007 2008 -- Start of processing for Potential_Unset_Reference 2009 2010 begin 2011 SR := Current_Scope; 2012 while SR /= SE loop 2013 if SR = Standard_Standard 2014 or else Is_Subprogram (SR) 2015 or else Is_Concurrent_Body (SR) 2016 or else Is_Concurrent_Type (SR) 2017 then 2018 return; 2019 end if; 2020 2021 SR := Scope (SR); 2022 end loop; 2023 2024 -- Case of reference has an access type. This is a 2025 -- special case since access types are always set to null 2026 -- so cannot be truly uninitialized, but we still want to 2027 -- warn about cases of obvious null dereference. 2028 2029 if Is_Access_Type (Typ) then 2030 Access_Type_Case : declare 2031 P : Node_Id; 2032 2033 function Process 2034 (N : Node_Id) return Traverse_Result; 2035 -- Process function for instantiation of Traverse 2036 -- below. Checks if N contains reference to E other 2037 -- than a dereference. 2038 2039 function Ref_In (Nod : Node_Id) return Boolean; 2040 -- Determines whether Nod contains a reference to 2041 -- the entity E that is not a dereference. 2042 2043 ------------- 2044 -- Process -- 2045 ------------- 2046 2047 function Process 2048 (N : Node_Id) return Traverse_Result 2049 is 2050 begin 2051 if Is_Entity_Name (N) 2052 and then Entity (N) = E 2053 and then not Is_Dereferenced (N) 2054 then 2055 return Abandon; 2056 else 2057 return OK; 2058 end if; 2059 end Process; 2060 2061 ------------ 2062 -- Ref_In -- 2063 ------------ 2064 2065 function Ref_In (Nod : Node_Id) return Boolean is 2066 function Traverse is new Traverse_Func (Process); 2067 begin 2068 return Traverse (Nod) = Abandon; 2069 end Ref_In; 2070 2071 -- Start of processing for Access_Type_Case 2072 2073 begin 2074 -- Don't bother if we are inside an instance, since 2075 -- the compilation of the generic template is where 2076 -- the warning should be issued. 2077 2078 if In_Instance then 2079 return; 2080 end if; 2081 2082 -- Don't bother if this is not the main unit. If we 2083 -- try to give this warning for with'ed units, we 2084 -- get some false positives, since we do not record 2085 -- references in other units. 2086 2087 if not In_Extended_Main_Source_Unit (E) 2088 or else 2089 not In_Extended_Main_Source_Unit (N) 2090 then 2091 return; 2092 end if; 2093 2094 -- We are only interested in dereferences 2095 2096 if not Is_Dereferenced (N) then 2097 return; 2098 end if; 2099 2100 -- One more check, don't bother with references 2101 -- that are inside conditional statements or WHILE 2102 -- loops if the condition references the entity in 2103 -- question. This avoids most false positives. 2104 2105 P := Parent (N); 2106 loop 2107 P := Parent (P); 2108 exit when No (P); 2109 2110 if Nkind (P) in N_If_Statement | N_Elsif_Part 2111 and then Ref_In (Condition (P)) 2112 then 2113 return; 2114 2115 elsif Nkind (P) = N_Loop_Statement 2116 and then Present (Iteration_Scheme (P)) 2117 and then 2118 Ref_In (Condition (Iteration_Scheme (P))) 2119 then 2120 return; 2121 end if; 2122 end loop; 2123 end Access_Type_Case; 2124 end if; 2125 2126 -- One more check, don't bother if we are within a 2127 -- postcondition, since the expression occurs in a 2128 -- place unrelated to the actual test. 2129 2130 if not Within_Postcondition then 2131 2132 -- Here we definitely have a case for giving a warning 2133 -- for a reference to an unset value. But we don't 2134 -- give the warning now. Instead set Unset_Reference 2135 -- in the identifier involved. The reason for this is 2136 -- that if we find the variable is never ever assigned 2137 -- a value then that warning is more important and 2138 -- there is no point in giving the reference warning. 2139 2140 -- If this is an identifier, set the field directly 2141 2142 if Nkind (N) = N_Identifier then 2143 Set_Unset_Reference (E, N); 2144 2145 -- Otherwise it is an expanded name, so set the field 2146 -- of the actual identifier for the reference. 2147 2148 else 2149 Set_Unset_Reference (E, Selector_Name (N)); 2150 end if; 2151 end if; 2152 end Potential_Unset_Reference; 2153 end if; 2154 end; 2155 2156 -- Indexed component or slice 2157 2158 when N_Indexed_Component 2159 | N_Slice 2160 => 2161 -- If prefix does not involve dereferencing an access type, then 2162 -- we know we are OK if the component type is fully initialized, 2163 -- since the component will have been set as part of the default 2164 -- initialization. 2165 2166 if not Prefix_Has_Dereference (Prefix (N)) 2167 and then Is_OK_Fully_Initialized 2168 then 2169 return; 2170 2171 -- Look at prefix in access type case, or if the component is not 2172 -- fully initialized. 2173 2174 else 2175 Check_Unset_Reference (Prefix (N)); 2176 end if; 2177 2178 -- Record component 2179 2180 when N_Selected_Component => 2181 declare 2182 Pref : constant Node_Id := Prefix (N); 2183 Ent : constant Entity_Id := Entity (Selector_Name (N)); 2184 2185 begin 2186 -- If prefix involves dereferencing an access type, always 2187 -- check the prefix, since the issue then is whether this 2188 -- access value is null. 2189 2190 if Prefix_Has_Dereference (Pref) then 2191 null; 2192 2193 -- Always go to prefix if no selector entity is set. Can this 2194 -- happen in the normal case? Not clear, but it definitely can 2195 -- happen in error cases. 2196 2197 elsif No (Ent) then 2198 null; 2199 2200 -- For a record component, check some cases where we have 2201 -- reasonable cause to consider that the component is known to 2202 -- be or probably is initialized. In this case, we don't care 2203 -- if the prefix itself was explicitly initialized. 2204 2205 -- Discriminants are always considered initialized 2206 2207 elsif Ekind (Ent) = E_Discriminant then 2208 return; 2209 2210 -- An explicitly initialized component is certainly initialized 2211 2212 elsif Nkind (Parent (Ent)) = N_Component_Declaration 2213 and then Present (Expression (Parent (Ent))) 2214 then 2215 return; 2216 2217 -- A fully initialized component is initialized 2218 2219 elsif Is_OK_Fully_Initialized then 2220 return; 2221 end if; 2222 2223 -- If none of those cases apply, check the record type prefix 2224 2225 Check_Unset_Reference (Pref); 2226 end; 2227 2228 -- For type conversions, qualifications, or expressions with actions, 2229 -- examine the expression. 2230 2231 when N_Expression_With_Actions 2232 | N_Qualified_Expression 2233 | N_Type_Conversion 2234 => 2235 Check_Unset_Reference (Expression (N)); 2236 2237 -- For explicit dereference, always check prefix, which will generate 2238 -- an unset reference (since this is a case of dereferencing null). 2239 2240 when N_Explicit_Dereference => 2241 Check_Unset_Reference (Prefix (N)); 2242 2243 -- All other cases are not cases of an unset reference 2244 2245 when others => 2246 null; 2247 end case; 2248 end Check_Unset_Reference; 2249 2250 ------------------------ 2251 -- Check_Unused_Withs -- 2252 ------------------------ 2253 2254 procedure Check_Unused_Withs (Spec_Unit : Unit_Number_Type := No_Unit) is 2255 2256 Munite : constant Entity_Id := Cunit_Entity (Main_Unit); 2257 -- This is needed for checking the special renaming case 2258 2259 procedure Check_One_Unit (Unit : Unit_Number_Type); 2260 -- Subsidiary procedure, performs checks for specified unit 2261 2262 -------------------- 2263 -- Check_One_Unit -- 2264 -------------------- 2265 2266 procedure Check_One_Unit (Unit : Unit_Number_Type) is 2267 Cnode : constant Node_Id := Cunit (Unit); 2268 2269 Is_Visible_Renaming : Boolean := False; 2270 2271 procedure Check_Inner_Package (Pack : Entity_Id); 2272 -- Pack is a package local to a unit in a with_clause. Both the unit 2273 -- and Pack are referenced. If none of the entities in Pack are 2274 -- referenced, then the only occurrence of Pack is in a USE clause 2275 -- or a pragma, and a warning is worthwhile as well. 2276 2277 function Check_System_Aux (Lunit : Entity_Id) return Boolean; 2278 -- Before giving a warning on a with_clause for System, check whether 2279 -- a system extension is present. 2280 2281 function Find_Package_Renaming 2282 (P : Entity_Id; 2283 L : Entity_Id) return Entity_Id; 2284 -- The only reference to a context unit may be in a renaming 2285 -- declaration. If this renaming declares a visible entity, do not 2286 -- warn that the context clause could be moved to the body, because 2287 -- the renaming may be intended to re-export the unit. 2288 2289 function Has_Visible_Entities (P : Entity_Id) return Boolean; 2290 -- This function determines if a package has any visible entities. 2291 -- True is returned if there is at least one declared visible entity, 2292 -- otherwise False is returned (e.g. case of only pragmas present). 2293 2294 ------------------------- 2295 -- Check_Inner_Package -- 2296 ------------------------- 2297 2298 procedure Check_Inner_Package (Pack : Entity_Id) is 2299 E : Entity_Id; 2300 Un : constant Node_Id := Sinfo.Unit (Cnode); 2301 2302 function Check_Use_Clause (N : Node_Id) return Traverse_Result; 2303 -- If N is a use_clause for Pack, emit warning 2304 2305 procedure Check_Use_Clauses is new 2306 Traverse_Proc (Check_Use_Clause); 2307 2308 ---------------------- 2309 -- Check_Use_Clause -- 2310 ---------------------- 2311 2312 function Check_Use_Clause (N : Node_Id) return Traverse_Result is 2313 begin 2314 if Nkind (N) = N_Use_Package_Clause 2315 and then Entity (Name (N)) = Pack 2316 then 2317 -- Suppress message if any serious errors detected that turn 2318 -- off expansion, and thus result in false positives for 2319 -- this warning. 2320 2321 if Serious_Errors_Detected = 0 then 2322 Error_Msg_Qual_Level := 1; 2323 Error_Msg_NE -- CODEFIX 2324 ("?u?no entities of package& are referenced!", 2325 Name (N), Pack); 2326 Error_Msg_Qual_Level := 0; 2327 end if; 2328 end if; 2329 2330 return OK; 2331 end Check_Use_Clause; 2332 2333 -- Start of processing for Check_Inner_Package 2334 2335 begin 2336 E := First_Entity (Pack); 2337 while Present (E) loop 2338 if Referenced_Check_Spec (E) then 2339 return; 2340 end if; 2341 2342 Next_Entity (E); 2343 end loop; 2344 2345 -- No entities of the package are referenced. Check whether the 2346 -- reference to the package itself is a use clause, and if so 2347 -- place a warning on it. 2348 2349 Check_Use_Clauses (Un); 2350 end Check_Inner_Package; 2351 2352 ---------------------- 2353 -- Check_System_Aux -- 2354 ---------------------- 2355 2356 function Check_System_Aux (Lunit : Entity_Id) return Boolean is 2357 Ent : Entity_Id; 2358 2359 begin 2360 if Chars (Lunit) = Name_System 2361 and then Scope (Lunit) = Standard_Standard 2362 and then Present_System_Aux 2363 then 2364 Ent := First_Entity (System_Aux_Id); 2365 while Present (Ent) loop 2366 if Referenced_Check_Spec (Ent) then 2367 return True; 2368 end if; 2369 2370 Next_Entity (Ent); 2371 end loop; 2372 end if; 2373 2374 return False; 2375 end Check_System_Aux; 2376 2377 --------------------------- 2378 -- Find_Package_Renaming -- 2379 --------------------------- 2380 2381 function Find_Package_Renaming 2382 (P : Entity_Id; 2383 L : Entity_Id) return Entity_Id 2384 is 2385 E1 : Entity_Id; 2386 R : Entity_Id; 2387 2388 begin 2389 Is_Visible_Renaming := False; 2390 2391 E1 := First_Entity (P); 2392 while Present (E1) loop 2393 if Ekind (E1) = E_Package and then Renamed_Object (E1) = L then 2394 Is_Visible_Renaming := not Is_Hidden (E1); 2395 return E1; 2396 2397 elsif Ekind (E1) = E_Package 2398 and then No (Renamed_Object (E1)) 2399 and then not Is_Generic_Instance (E1) 2400 then 2401 R := Find_Package_Renaming (E1, L); 2402 2403 if Present (R) then 2404 Is_Visible_Renaming := not Is_Hidden (R); 2405 return R; 2406 end if; 2407 end if; 2408 2409 Next_Entity (E1); 2410 end loop; 2411 2412 return Empty; 2413 end Find_Package_Renaming; 2414 2415 -------------------------- 2416 -- Has_Visible_Entities -- 2417 -------------------------- 2418 2419 function Has_Visible_Entities (P : Entity_Id) return Boolean is 2420 E : Entity_Id; 2421 2422 begin 2423 -- If unit in context is not a package, it is a subprogram that 2424 -- is not called or a generic unit that is not instantiated 2425 -- in the current unit, and warning is appropriate. 2426 2427 if Ekind (P) /= E_Package then 2428 return True; 2429 end if; 2430 2431 -- If unit comes from a limited_with clause, look for declaration 2432 -- of shadow entities. 2433 2434 if Present (Limited_View (P)) then 2435 E := First_Entity (Limited_View (P)); 2436 else 2437 E := First_Entity (P); 2438 end if; 2439 2440 while Present (E) and then E /= First_Private_Entity (P) loop 2441 if Comes_From_Source (E) or else Present (Limited_View (P)) then 2442 return True; 2443 end if; 2444 2445 Next_Entity (E); 2446 end loop; 2447 2448 return False; 2449 end Has_Visible_Entities; 2450 2451 -- Local variables 2452 2453 Ent : Entity_Id; 2454 Item : Node_Id; 2455 Lunit : Entity_Id; 2456 Pack : Entity_Id; 2457 2458 -- Start of processing for Check_One_Unit 2459 2460 begin 2461 -- Only do check in units that are part of the extended main unit. 2462 -- This is actually a necessary restriction, because in the case of 2463 -- subprogram acting as its own specification, there can be with's in 2464 -- subunits that we will not see. 2465 2466 if not In_Extended_Main_Source_Unit (Cnode) then 2467 return; 2468 end if; 2469 2470 -- Loop through context items in this unit 2471 2472 Item := First (Context_Items (Cnode)); 2473 while Present (Item) loop 2474 if Nkind (Item) = N_With_Clause 2475 and then not Implicit_With (Item) 2476 and then In_Extended_Main_Source_Unit (Item) 2477 2478 -- Guard for no entity present. Not clear under what conditions 2479 -- this happens, but it does occur, and since this is only a 2480 -- warning, we just suppress the warning in this case. 2481 2482 and then Nkind (Name (Item)) in N_Has_Entity 2483 and then Present (Entity (Name (Item))) 2484 then 2485 Lunit := Entity (Name (Item)); 2486 2487 -- Check if this unit is referenced (skip the check if this 2488 -- is explicitly marked by a pragma Unreferenced). 2489 2490 if not Referenced (Lunit) and then not Has_Unreferenced (Lunit) 2491 then 2492 -- Suppress warnings in internal units if not in -gnatg mode 2493 -- (these would be junk warnings for an application program, 2494 -- since they refer to problems in internal units). 2495 2496 if GNAT_Mode or else not Is_Internal_Unit (Unit) then 2497 -- Here we definitely have a non-referenced unit. If it 2498 -- is the special call for a spec unit, then just set the 2499 -- flag to be read later. 2500 2501 if Unit = Spec_Unit then 2502 Set_Unreferenced_In_Spec (Item); 2503 2504 -- Otherwise simple unreferenced message, but skip this 2505 -- if no visible entities, because that is most likely a 2506 -- case where warning would be false positive (e.g. a 2507 -- package with only a linker options pragma and nothing 2508 -- else or a pragma elaborate with a body library task). 2509 2510 elsif Has_Visible_Entities (Lunit) then 2511 Error_Msg_N -- CODEFIX 2512 ("?u?unit& is not referenced!", Name (Item)); 2513 end if; 2514 end if; 2515 2516 -- If main unit is a renaming of this unit, then we consider 2517 -- the with to be OK (obviously it is needed in this case). 2518 -- This may be transitive: the unit in the with_clause may 2519 -- itself be a renaming, in which case both it and the main 2520 -- unit rename the same ultimate package. 2521 2522 elsif Present (Renamed_Entity (Munite)) 2523 and then 2524 (Renamed_Entity (Munite) = Lunit 2525 or else Renamed_Entity (Munite) = Renamed_Entity (Lunit)) 2526 then 2527 null; 2528 2529 -- If this unit is referenced, and it is a package, we do 2530 -- another test, to see if any of the entities in the package 2531 -- are referenced. If none of the entities are referenced, we 2532 -- still post a warning. This occurs if the only use of the 2533 -- package is in a use clause, or in a package renaming 2534 -- declaration. This check is skipped for packages that are 2535 -- renamed in a spec, since the entities in such a package are 2536 -- visible to clients via the renaming. 2537 2538 elsif Ekind (Lunit) = E_Package 2539 and then not Renamed_In_Spec (Lunit) 2540 then 2541 -- If Is_Instantiated is set, it means that the package is 2542 -- implicitly instantiated (this is the case of parent 2543 -- instance or an actual for a generic package formal), and 2544 -- this counts as a reference. 2545 2546 if Is_Instantiated (Lunit) then 2547 null; 2548 2549 -- If no entities in package, and there is a pragma 2550 -- Elaborate_Body present, then assume that this with is 2551 -- done for purposes of this elaboration. 2552 2553 elsif No (First_Entity (Lunit)) 2554 and then Has_Pragma_Elaborate_Body (Lunit) 2555 then 2556 null; 2557 2558 -- Otherwise see if any entities have been referenced 2559 2560 else 2561 if Limited_Present (Item) then 2562 Ent := First_Entity (Limited_View (Lunit)); 2563 else 2564 Ent := First_Entity (Lunit); 2565 end if; 2566 2567 loop 2568 -- No more entities, and we did not find one that was 2569 -- referenced. Means we have a definite case of a with 2570 -- none of whose entities was referenced. 2571 2572 if No (Ent) then 2573 2574 -- If in spec, just set the flag 2575 2576 if Unit = Spec_Unit then 2577 Set_No_Entities_Ref_In_Spec (Item); 2578 2579 elsif Check_System_Aux (Lunit) then 2580 null; 2581 2582 -- Else the warning may be needed 2583 2584 else 2585 -- Warn if we unreferenced flag set and we have 2586 -- not had serious errors. The reason we inhibit 2587 -- the message if there are errors is to prevent 2588 -- false positives from disabling expansion. 2589 2590 if not Has_Unreferenced (Lunit) 2591 and then Serious_Errors_Detected = 0 2592 then 2593 -- Get possible package renaming 2594 2595 Pack := Find_Package_Renaming (Munite, Lunit); 2596 2597 -- No warning if either the package or its 2598 -- renaming is used as a generic actual. 2599 2600 if Used_As_Generic_Actual (Lunit) 2601 or else 2602 (Present (Pack) 2603 and then 2604 Used_As_Generic_Actual (Pack)) 2605 then 2606 exit; 2607 end if; 2608 2609 -- Here we give the warning 2610 2611 Error_Msg_N -- CODEFIX 2612 ("?u?no entities of & are referenced!", 2613 Name (Item)); 2614 2615 -- Flag renaming of package as well. If 2616 -- the original package has warnings off, 2617 -- we suppress the warning on the renaming 2618 -- as well. 2619 2620 if Present (Pack) 2621 and then not Has_Warnings_Off (Lunit) 2622 and then not Has_Unreferenced (Pack) 2623 then 2624 Error_Msg_NE -- CODEFIX 2625 ("?u?no entities of& are referenced!", 2626 Unit_Declaration_Node (Pack), Pack); 2627 end if; 2628 end if; 2629 end if; 2630 2631 exit; 2632 2633 -- Case of entity being referenced. The reference may 2634 -- come from a limited_with_clause, in which case the 2635 -- limited view of the entity carries the flag. 2636 2637 elsif Referenced_Check_Spec (Ent) 2638 or else Referenced_As_LHS_Check_Spec (Ent) 2639 or else Referenced_As_Out_Parameter_Check_Spec (Ent) 2640 or else 2641 (From_Limited_With (Ent) 2642 and then Is_Incomplete_Type (Ent) 2643 and then Present (Non_Limited_View (Ent)) 2644 and then Referenced (Non_Limited_View (Ent))) 2645 then 2646 -- This means that the with is indeed fine, in that 2647 -- it is definitely needed somewhere, and we can 2648 -- quit worrying about this one... 2649 2650 -- Except for one little detail: if either of the 2651 -- flags was set during spec processing, this is 2652 -- where we complain that the with could be moved 2653 -- from the spec. If the spec contains a visible 2654 -- renaming of the package, inhibit warning to move 2655 -- with_clause to body. 2656 2657 if Ekind (Munite) = E_Package_Body then 2658 Pack := 2659 Find_Package_Renaming 2660 (Spec_Entity (Munite), Lunit); 2661 else 2662 Pack := Empty; 2663 end if; 2664 2665 -- If a renaming is present in the spec do not warn 2666 -- because the body or child unit may depend on it. 2667 2668 if Present (Pack) 2669 and then Renamed_Entity (Pack) = Lunit 2670 then 2671 exit; 2672 2673 elsif Unreferenced_In_Spec (Item) then 2674 Error_Msg_N -- CODEFIX 2675 ("?u?unit& is not referenced in spec!", 2676 Name (Item)); 2677 2678 elsif No_Entities_Ref_In_Spec (Item) then 2679 Error_Msg_N -- CODEFIX 2680 ("?u?no entities of & are referenced in spec!", 2681 Name (Item)); 2682 2683 else 2684 if Ekind (Ent) = E_Package then 2685 Check_Inner_Package (Ent); 2686 end if; 2687 2688 exit; 2689 end if; 2690 2691 if not Is_Visible_Renaming then 2692 Error_Msg_N -- CODEFIX 2693 ("\?u?with clause might be moved to body!", 2694 Name (Item)); 2695 end if; 2696 2697 exit; 2698 2699 -- Move to next entity to continue search 2700 2701 else 2702 Next_Entity (Ent); 2703 end if; 2704 end loop; 2705 end if; 2706 2707 -- For a generic package, the only interesting kind of 2708 -- reference is an instantiation, since entities cannot be 2709 -- referenced directly. 2710 2711 elsif Is_Generic_Unit (Lunit) then 2712 2713 -- Unit was never instantiated, set flag for case of spec 2714 -- call, or give warning for normal call. 2715 2716 if not Is_Instantiated (Lunit) then 2717 if Unit = Spec_Unit then 2718 Set_Unreferenced_In_Spec (Item); 2719 else 2720 Error_Msg_N -- CODEFIX 2721 ("?u?unit& is never instantiated!", Name (Item)); 2722 end if; 2723 2724 -- If unit was indeed instantiated, make sure that flag is 2725 -- not set showing it was uninstantiated in the spec, and if 2726 -- so, give warning. 2727 2728 elsif Unreferenced_In_Spec (Item) then 2729 Error_Msg_N 2730 ("?u?unit& is not instantiated in spec!", Name (Item)); 2731 Error_Msg_N -- CODEFIX 2732 ("\?u?with clause can be moved to body!", Name (Item)); 2733 end if; 2734 end if; 2735 end if; 2736 2737 Next (Item); 2738 end loop; 2739 end Check_One_Unit; 2740 2741 -- Start of processing for Check_Unused_Withs 2742 2743 begin 2744 -- Immediate return if no semantics or warning flag not set 2745 2746 if not Opt.Check_Withs or else Operating_Mode = Check_Syntax then 2747 return; 2748 end if; 2749 2750 Process_Deferred_References; 2751 2752 -- Flag any unused with clauses. For a subunit, check only the units 2753 -- in its context, not those of the parent, which may be needed by other 2754 -- subunits. We will get the full warnings when we compile the parent, 2755 -- but the following is helpful when compiling a subunit by itself. 2756 2757 if Nkind (Unit (Cunit (Main_Unit))) = N_Subunit then 2758 if Current_Sem_Unit = Main_Unit then 2759 Check_One_Unit (Main_Unit); 2760 end if; 2761 2762 return; 2763 end if; 2764 2765 -- Process specified units 2766 2767 if Spec_Unit = No_Unit then 2768 2769 -- For main call, check all units 2770 2771 for Unit in Main_Unit .. Last_Unit loop 2772 Check_One_Unit (Unit); 2773 end loop; 2774 2775 else 2776 -- For call for spec, check only the spec 2777 2778 Check_One_Unit (Spec_Unit); 2779 end if; 2780 end Check_Unused_Withs; 2781 2782 --------------------------------- 2783 -- Generic_Package_Spec_Entity -- 2784 --------------------------------- 2785 2786 function Generic_Package_Spec_Entity (E : Entity_Id) return Boolean is 2787 S : Entity_Id; 2788 2789 begin 2790 if Is_Package_Body_Entity (E) then 2791 return False; 2792 2793 else 2794 S := Scope (E); 2795 loop 2796 if S = Standard_Standard then 2797 return False; 2798 2799 elsif Ekind (S) = E_Generic_Package then 2800 return True; 2801 2802 elsif Ekind (S) = E_Package then 2803 S := Scope (S); 2804 2805 else 2806 return False; 2807 end if; 2808 end loop; 2809 end if; 2810 end Generic_Package_Spec_Entity; 2811 2812 ---------------------- 2813 -- Goto_Spec_Entity -- 2814 ---------------------- 2815 2816 function Goto_Spec_Entity (E : Entity_Id) return Entity_Id is 2817 begin 2818 if Is_Formal (E) and then Present (Spec_Entity (E)) then 2819 return Spec_Entity (E); 2820 else 2821 return E; 2822 end if; 2823 end Goto_Spec_Entity; 2824 2825 ------------------- 2826 -- Has_Junk_Name -- 2827 ------------------- 2828 2829 function Has_Junk_Name (E : Entity_Id) return Boolean is 2830 function Match (S : String) return Boolean; 2831 -- Return true if substring S is found in Name_Buffer (1 .. Name_Len) 2832 2833 ----------- 2834 -- Match -- 2835 ----------- 2836 2837 function Match (S : String) return Boolean is 2838 Slen1 : constant Integer := S'Length - 1; 2839 2840 begin 2841 for J in 1 .. Name_Len - S'Length + 1 loop 2842 if Name_Buffer (J .. J + Slen1) = S then 2843 return True; 2844 end if; 2845 end loop; 2846 2847 return False; 2848 end Match; 2849 2850 -- Start of processing for Has_Junk_Name 2851 2852 begin 2853 Get_Unqualified_Decoded_Name_String (Chars (E)); 2854 2855 return 2856 Match ("discard") or else 2857 Match ("dummy") or else 2858 Match ("ignore") or else 2859 Match ("junk") or else 2860 Match ("unused"); 2861 end Has_Junk_Name; 2862 2863 -------------------------------------- 2864 -- Has_Pragma_Unmodified_Check_Spec -- 2865 -------------------------------------- 2866 2867 function Has_Pragma_Unmodified_Check_Spec 2868 (E : Entity_Id) return Boolean 2869 is 2870 begin 2871 if Is_Formal (E) and then Present (Spec_Entity (E)) then 2872 2873 -- Note: use of OR instead of OR ELSE here is deliberate, we want 2874 -- to mess with Unmodified flags on both body and spec entities. 2875 -- Has_Unmodified has side effects! 2876 2877 return Has_Unmodified (E) 2878 or 2879 Has_Unmodified (Spec_Entity (E)); 2880 2881 else 2882 return Has_Unmodified (E); 2883 end if; 2884 end Has_Pragma_Unmodified_Check_Spec; 2885 2886 ---------------------------------------- 2887 -- Has_Pragma_Unreferenced_Check_Spec -- 2888 ---------------------------------------- 2889 2890 function Has_Pragma_Unreferenced_Check_Spec 2891 (E : Entity_Id) return Boolean 2892 is 2893 begin 2894 if Is_Formal (E) and then Present (Spec_Entity (E)) then 2895 2896 -- Note: use of OR here instead of OR ELSE is deliberate, we want 2897 -- to mess with flags on both entities. 2898 2899 return Has_Unreferenced (E) 2900 or 2901 Has_Unreferenced (Spec_Entity (E)); 2902 2903 else 2904 return Has_Unreferenced (E); 2905 end if; 2906 end Has_Pragma_Unreferenced_Check_Spec; 2907 2908 ---------------- 2909 -- Initialize -- 2910 ---------------- 2911 2912 procedure Initialize is 2913 begin 2914 Warnings_Off_Pragmas.Init; 2915 Unreferenced_Entities.Init; 2916 In_Out_Warnings.Init; 2917 end Initialize; 2918 2919 --------------------------------------------- 2920 -- Is_Attribute_And_Known_Value_Comparison -- 2921 --------------------------------------------- 2922 2923 function Is_Attribute_And_Known_Value_Comparison 2924 (Op : Node_Id) return Boolean 2925 is 2926 Orig_Op : constant Node_Id := Original_Node (Op); 2927 2928 begin 2929 return 2930 Nkind (Orig_Op) in N_Op_Compare 2931 and then Nkind (Original_Node (Left_Opnd (Orig_Op))) = 2932 N_Attribute_Reference 2933 and then Compile_Time_Known_Value (Right_Opnd (Orig_Op)); 2934 end Is_Attribute_And_Known_Value_Comparison; 2935 2936 ------------------------------------ 2937 -- Never_Set_In_Source_Check_Spec -- 2938 ------------------------------------ 2939 2940 function Never_Set_In_Source_Check_Spec (E : Entity_Id) return Boolean is 2941 begin 2942 if Is_Formal (E) and then Present (Spec_Entity (E)) then 2943 return Never_Set_In_Source (E) 2944 and then 2945 Never_Set_In_Source (Spec_Entity (E)); 2946 else 2947 return Never_Set_In_Source (E); 2948 end if; 2949 end Never_Set_In_Source_Check_Spec; 2950 2951 ------------------------------------- 2952 -- Operand_Has_Warnings_Suppressed -- 2953 ------------------------------------- 2954 2955 function Operand_Has_Warnings_Suppressed (N : Node_Id) return Boolean is 2956 2957 function Check_For_Warnings (N : Node_Id) return Traverse_Result; 2958 -- Function used to check one node to see if it is or was originally 2959 -- a reference to an entity for which Warnings are off. If so, Abandon 2960 -- is returned, otherwise OK_Orig is returned to continue the traversal 2961 -- of the original expression. 2962 2963 function Traverse is new Traverse_Func (Check_For_Warnings); 2964 -- Function used to traverse tree looking for warnings 2965 2966 ------------------------ 2967 -- Check_For_Warnings -- 2968 ------------------------ 2969 2970 function Check_For_Warnings (N : Node_Id) return Traverse_Result is 2971 R : constant Node_Id := Original_Node (N); 2972 2973 begin 2974 if Nkind (R) in N_Has_Entity 2975 and then Present (Entity (R)) 2976 and then Has_Warnings_Off (Entity (R)) 2977 then 2978 return Abandon; 2979 else 2980 return OK_Orig; 2981 end if; 2982 end Check_For_Warnings; 2983 2984 -- Start of processing for Operand_Has_Warnings_Suppressed 2985 2986 begin 2987 return Traverse (N) = Abandon; 2988 2989 -- If any exception occurs, then something has gone wrong, and this is 2990 -- only a minor aesthetic issue anyway, so just say we did not find what 2991 -- we are looking for, rather than blow up. 2992 2993 exception 2994 when others => 2995 -- With debug flag K we will get an exception unless an error has 2996 -- already occurred (useful for debugging). 2997 2998 if Debug_Flag_K then 2999 Check_Error_Detected; 3000 end if; 3001 3002 return False; 3003 end Operand_Has_Warnings_Suppressed; 3004 3005 ----------------------------------------- 3006 -- Output_Non_Modified_In_Out_Warnings -- 3007 ----------------------------------------- 3008 3009 procedure Output_Non_Modified_In_Out_Warnings is 3010 3011 function No_Warn_On_In_Out (E : Entity_Id) return Boolean; 3012 -- Given a formal parameter entity E, determines if there is a reason to 3013 -- suppress IN OUT warnings (not modified, could be IN) for formals of 3014 -- the subprogram. We suppress these warnings if Warnings Off is set, or 3015 -- if we have seen the address of the subprogram being taken, or if the 3016 -- subprogram is used as a generic actual (in the latter cases the 3017 -- context may force use of IN OUT, even if the parameter is not 3018 -- modifies for this particular case. 3019 3020 ----------------------- 3021 -- No_Warn_On_In_Out -- 3022 ----------------------- 3023 3024 function No_Warn_On_In_Out (E : Entity_Id) return Boolean is 3025 S : constant Entity_Id := Scope (E); 3026 SE : constant Entity_Id := Spec_Entity (E); 3027 3028 begin 3029 -- Do not warn if address is taken, since funny business may be going 3030 -- on in treating the parameter indirectly as IN OUT. 3031 3032 if Address_Taken (S) 3033 or else (Present (SE) and then Address_Taken (Scope (SE))) 3034 then 3035 return True; 3036 3037 -- Do not warn if used as a generic actual, since the generic may be 3038 -- what is forcing the use of an "unnecessary" IN OUT. 3039 3040 elsif Used_As_Generic_Actual (S) 3041 or else (Present (SE) and then Used_As_Generic_Actual (Scope (SE))) 3042 then 3043 return True; 3044 3045 -- Else test warnings off 3046 3047 elsif Warnings_Off_Check_Spec (S) then 3048 return True; 3049 3050 -- All tests for suppressing warning failed 3051 3052 else 3053 return False; 3054 end if; 3055 end No_Warn_On_In_Out; 3056 3057 -- Start of processing for Output_Non_Modified_In_Out_Warnings 3058 3059 begin 3060 -- Loop through entities for which a warning may be needed 3061 3062 for J in In_Out_Warnings.First .. In_Out_Warnings.Last loop 3063 declare 3064 E1 : constant Entity_Id := In_Out_Warnings.Table (J); 3065 3066 begin 3067 -- Suppress warning in specific cases (see details in comments for 3068 -- No_Warn_On_In_Out), or if there is a pragma Unmodified. 3069 3070 if Has_Pragma_Unmodified_Check_Spec (E1) 3071 or else No_Warn_On_In_Out (E1) 3072 then 3073 null; 3074 3075 -- Here we generate the warning 3076 3077 else 3078 -- If -gnatwk is set then output message that we could be IN 3079 3080 if not Is_Trivial_Subprogram (Scope (E1)) then 3081 if Warn_On_Constant then 3082 Error_Msg_N 3083 ("?k?formal parameter & is not modified!", E1); 3084 Error_Msg_N 3085 ("\?k?mode could be IN instead of `IN OUT`!", E1); 3086 3087 -- We do not generate warnings for IN OUT parameters 3088 -- unless we have at least -gnatwu. This is deliberately 3089 -- inconsistent with the treatment of variables, but 3090 -- otherwise we get too many unexpected warnings in 3091 -- default mode. 3092 3093 elsif Check_Unreferenced then 3094 Error_Msg_N 3095 ("?u?formal parameter& is read but " 3096 & "never assigned!", E1); 3097 end if; 3098 end if; 3099 3100 -- Kill any other warnings on this entity, since this is the 3101 -- one that should dominate any other unreferenced warning. 3102 3103 Set_Warnings_Off (E1); 3104 end if; 3105 end; 3106 end loop; 3107 end Output_Non_Modified_In_Out_Warnings; 3108 3109 ---------------------------------------- 3110 -- Output_Obsolescent_Entity_Warnings -- 3111 ---------------------------------------- 3112 3113 procedure Output_Obsolescent_Entity_Warnings (N : Node_Id; E : Entity_Id) is 3114 P : constant Node_Id := Parent (N); 3115 S : Entity_Id; 3116 3117 begin 3118 S := Current_Scope; 3119 3120 -- Do not output message if we are the scope of standard. This means 3121 -- we have a reference from a context clause from when it is originally 3122 -- processed, and that's too early to tell whether it is an obsolescent 3123 -- unit doing the with'ing. In Sem_Ch10.Analyze_Compilation_Unit we make 3124 -- sure that we have a later call when the scope is available. This test 3125 -- also eliminates all messages for use clauses, which is fine (we do 3126 -- not want messages for use clauses, since they are always redundant 3127 -- with respect to the associated with clause). 3128 3129 if S = Standard_Standard then 3130 return; 3131 end if; 3132 3133 -- Do not output message if we are in scope of an obsolescent package 3134 -- or subprogram. 3135 3136 loop 3137 if Is_Obsolescent (S) then 3138 return; 3139 end if; 3140 3141 S := Scope (S); 3142 exit when S = Standard_Standard; 3143 end loop; 3144 3145 -- Here we will output the message 3146 3147 Error_Msg_Sloc := Sloc (E); 3148 3149 -- Case of with clause 3150 3151 if Nkind (P) = N_With_Clause then 3152 if Ekind (E) = E_Package then 3153 Error_Msg_NE 3154 ("?j?with of obsolescent package& declared#", N, E); 3155 elsif Ekind (E) = E_Procedure then 3156 Error_Msg_NE 3157 ("?j?with of obsolescent procedure& declared#", N, E); 3158 else 3159 Error_Msg_NE 3160 ("??with of obsolescent function& declared#", N, E); 3161 end if; 3162 3163 -- If we do not have a with clause, then ignore any reference to an 3164 -- obsolescent package name. We only want to give the one warning of 3165 -- withing the package, not one each time it is used to qualify. 3166 3167 elsif Ekind (E) = E_Package then 3168 return; 3169 3170 -- Procedure call statement 3171 3172 elsif Nkind (P) = N_Procedure_Call_Statement then 3173 Error_Msg_NE 3174 ("??call to obsolescent procedure& declared#", N, E); 3175 3176 -- Function call 3177 3178 elsif Nkind (P) = N_Function_Call then 3179 Error_Msg_NE 3180 ("??call to obsolescent function& declared#", N, E); 3181 3182 -- Reference to obsolescent type 3183 3184 elsif Is_Type (E) then 3185 Error_Msg_NE 3186 ("??reference to obsolescent type& declared#", N, E); 3187 3188 -- Reference to obsolescent component 3189 3190 elsif Ekind (E) in E_Component | E_Discriminant then 3191 Error_Msg_NE 3192 ("??reference to obsolescent component& declared#", N, E); 3193 3194 -- Reference to obsolescent variable 3195 3196 elsif Ekind (E) = E_Variable then 3197 Error_Msg_NE 3198 ("??reference to obsolescent variable& declared#", N, E); 3199 3200 -- Reference to obsolescent constant 3201 3202 elsif Ekind (E) = E_Constant or else Ekind (E) in Named_Kind then 3203 Error_Msg_NE 3204 ("??reference to obsolescent constant& declared#", N, E); 3205 3206 -- Reference to obsolescent enumeration literal 3207 3208 elsif Ekind (E) = E_Enumeration_Literal then 3209 Error_Msg_NE 3210 ("??reference to obsolescent enumeration literal& declared#", N, E); 3211 3212 -- Generic message for any other case we missed 3213 3214 else 3215 Error_Msg_NE 3216 ("??reference to obsolescent entity& declared#", N, E); 3217 end if; 3218 3219 -- Output additional warning if present 3220 3221 for J in Obsolescent_Warnings.First .. Obsolescent_Warnings.Last loop 3222 if Obsolescent_Warnings.Table (J).Ent = E then 3223 String_To_Name_Buffer (Obsolescent_Warnings.Table (J).Msg); 3224 Error_Msg_Strlen := Name_Len; 3225 Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len); 3226 Error_Msg_N ("\\??~", N); 3227 exit; 3228 end if; 3229 end loop; 3230 end Output_Obsolescent_Entity_Warnings; 3231 3232 ---------------------------------- 3233 -- Output_Unreferenced_Messages -- 3234 ---------------------------------- 3235 3236 procedure Output_Unreferenced_Messages is 3237 begin 3238 for J in Unreferenced_Entities.First .. Unreferenced_Entities.Last loop 3239 Warn_On_Unreferenced_Entity (Unreferenced_Entities.Table (J)); 3240 end loop; 3241 end Output_Unreferenced_Messages; 3242 3243 ----------------------------------------- 3244 -- Output_Unused_Warnings_Off_Warnings -- 3245 ----------------------------------------- 3246 3247 procedure Output_Unused_Warnings_Off_Warnings is 3248 begin 3249 for J in Warnings_Off_Pragmas.First .. Warnings_Off_Pragmas.Last loop 3250 declare 3251 Wentry : Warnings_Off_Entry renames Warnings_Off_Pragmas.Table (J); 3252 N : Node_Id renames Wentry.N; 3253 E : Node_Id renames Wentry.E; 3254 3255 begin 3256 -- Turn off Warnings_Off, or we won't get the warning 3257 3258 Set_Warnings_Off (E, False); 3259 3260 -- Nothing to do if pragma was used to suppress a general warning 3261 3262 if Warnings_Off_Used (E) then 3263 null; 3264 3265 -- If pragma was used both in unmodified and unreferenced contexts 3266 -- then that's as good as the general case, no warning. 3267 3268 elsif Warnings_Off_Used_Unmodified (E) 3269 and 3270 Warnings_Off_Used_Unreferenced (E) 3271 then 3272 null; 3273 3274 -- Used only in context where Unmodified would have worked 3275 3276 elsif Warnings_Off_Used_Unmodified (E) then 3277 Error_Msg_NE 3278 ("?W?could use Unmodified instead of " 3279 & "Warnings Off for &", Pragma_Identifier (N), E); 3280 3281 -- Used only in context where Unreferenced would have worked 3282 3283 elsif Warnings_Off_Used_Unreferenced (E) then 3284 Error_Msg_NE 3285 ("?W?could use Unreferenced instead of " 3286 & "Warnings Off for &", Pragma_Identifier (N), E); 3287 3288 -- Not used at all 3289 3290 else 3291 Error_Msg_NE 3292 ("?W?pragma Warnings Off for & unused, " 3293 & "could be omitted", N, E); 3294 end if; 3295 end; 3296 end loop; 3297 end Output_Unused_Warnings_Off_Warnings; 3298 3299 --------------------------- 3300 -- Referenced_Check_Spec -- 3301 --------------------------- 3302 3303 function Referenced_Check_Spec (E : Entity_Id) return Boolean is 3304 begin 3305 if Is_Formal (E) and then Present (Spec_Entity (E)) then 3306 return Referenced (E) or else Referenced (Spec_Entity (E)); 3307 else 3308 return Referenced (E); 3309 end if; 3310 end Referenced_Check_Spec; 3311 3312 ---------------------------------- 3313 -- Referenced_As_LHS_Check_Spec -- 3314 ---------------------------------- 3315 3316 function Referenced_As_LHS_Check_Spec (E : Entity_Id) return Boolean is 3317 begin 3318 if Is_Formal (E) and then Present (Spec_Entity (E)) then 3319 return Referenced_As_LHS (E) 3320 or else Referenced_As_LHS (Spec_Entity (E)); 3321 else 3322 return Referenced_As_LHS (E); 3323 end if; 3324 end Referenced_As_LHS_Check_Spec; 3325 3326 -------------------------------------------- 3327 -- Referenced_As_Out_Parameter_Check_Spec -- 3328 -------------------------------------------- 3329 3330 function Referenced_As_Out_Parameter_Check_Spec 3331 (E : Entity_Id) return Boolean 3332 is 3333 begin 3334 if Is_Formal (E) and then Present (Spec_Entity (E)) then 3335 return Referenced_As_Out_Parameter (E) 3336 or else Referenced_As_Out_Parameter (Spec_Entity (E)); 3337 else 3338 return Referenced_As_Out_Parameter (E); 3339 end if; 3340 end Referenced_As_Out_Parameter_Check_Spec; 3341 3342 -------------------------------------- 3343 -- Warn_On_Constant_Valid_Condition -- 3344 -------------------------------------- 3345 3346 procedure Warn_On_Constant_Valid_Condition (Op : Node_Id) is 3347 Left : constant Node_Id := Left_Opnd (Op); 3348 Right : constant Node_Id := Right_Opnd (Op); 3349 3350 True_Result : Boolean; 3351 False_Result : Boolean; 3352 3353 begin 3354 -- Determine the potential outcome of the comparison assuming that the 3355 -- scalar operands are valid. 3356 3357 if Constant_Condition_Warnings 3358 and then Comes_From_Source (Original_Node (Op)) 3359 and then Is_Scalar_Type (Etype (Left)) 3360 and then Is_Scalar_Type (Etype (Right)) 3361 3362 -- Do not consider instances because the check was already performed 3363 -- in the generic. 3364 3365 and then not In_Instance 3366 3367 -- Do not consider comparisons between two static expressions such as 3368 -- constants or literals because those values cannot be invalidated. 3369 3370 and then not (Is_Static_Expression (Left) 3371 and then Is_Static_Expression (Right)) 3372 3373 -- Do not consider comparison between an attribute reference and a 3374 -- compile-time known value since this is most likely a conditional 3375 -- compilation. 3376 3377 and then not Is_Attribute_And_Known_Value_Comparison (Op) 3378 3379 -- Do not consider internal files to allow for various assertions and 3380 -- safeguards within our runtime. 3381 3382 and then not In_Internal_Unit (Op) 3383 then 3384 Test_Comparison 3385 (Op => Op, 3386 Assume_Valid => True, 3387 True_Result => True_Result, 3388 False_Result => False_Result); 3389 3390 -- Warn on a possible evaluation to False / True in the presence of 3391 -- invalid values. 3392 3393 if True_Result then 3394 Error_Msg_N 3395 ("condition can only be False if invalid values present?c?", Op); 3396 3397 elsif False_Result then 3398 Error_Msg_N 3399 ("condition can only be True if invalid values present?c?", Op); 3400 end if; 3401 end if; 3402 end Warn_On_Constant_Valid_Condition; 3403 3404 ----------------------------- 3405 -- Warn_On_Known_Condition -- 3406 ----------------------------- 3407 3408 procedure Warn_On_Known_Condition (C : Node_Id) is 3409 Test_Result : Boolean := False; 3410 -- Force initialization to facilitate static analysis 3411 3412 function Is_Known_Branch return Boolean; 3413 -- If the type of the condition is Boolean, the constant value of the 3414 -- condition is a boolean literal. If the type is a derived boolean 3415 -- type, the constant is wrapped in a type conversion of the derived 3416 -- literal. If the value of the condition is not a literal, no warnings 3417 -- can be produced. This function returns True if the result can be 3418 -- determined, and Test_Result is set True/False accordingly. Otherwise 3419 -- False is returned, and Test_Result is unchanged. 3420 3421 procedure Track (N : Node_Id; Loc : Node_Id); 3422 -- Adds continuation warning(s) pointing to reason (assignment or test) 3423 -- for the operand of the conditional having a known value (or at least 3424 -- enough is known about the value to issue the warning). N is the node 3425 -- which is judged to have a known value. Loc is the warning location. 3426 3427 --------------------- 3428 -- Is_Known_Branch -- 3429 --------------------- 3430 3431 function Is_Known_Branch return Boolean is 3432 begin 3433 if Etype (C) = Standard_Boolean 3434 and then Is_Entity_Name (C) 3435 and then 3436 (Entity (C) = Standard_False or else Entity (C) = Standard_True) 3437 then 3438 Test_Result := Entity (C) = Standard_True; 3439 return True; 3440 3441 elsif Is_Boolean_Type (Etype (C)) 3442 and then Nkind (C) = N_Unchecked_Type_Conversion 3443 and then Is_Entity_Name (Expression (C)) 3444 and then Ekind (Entity (Expression (C))) = E_Enumeration_Literal 3445 then 3446 Test_Result := 3447 Chars (Entity (Expression (C))) = Chars (Standard_True); 3448 return True; 3449 3450 else 3451 return False; 3452 end if; 3453 end Is_Known_Branch; 3454 3455 ----------- 3456 -- Track -- 3457 ----------- 3458 3459 procedure Track (N : Node_Id; Loc : Node_Id) is 3460 Nod : constant Node_Id := Original_Node (N); 3461 3462 begin 3463 if Nkind (Nod) in N_Op_Compare then 3464 Track (Left_Opnd (Nod), Loc); 3465 Track (Right_Opnd (Nod), Loc); 3466 3467 elsif Is_Entity_Name (Nod) and then Is_Object (Entity (Nod)) then 3468 declare 3469 CV : constant Node_Id := Current_Value (Entity (Nod)); 3470 3471 begin 3472 if Present (CV) then 3473 Error_Msg_Sloc := Sloc (CV); 3474 3475 if Nkind (CV) not in N_Subexpr then 3476 Error_Msg_N ("\\??(see test #)", Loc); 3477 3478 elsif Nkind (Parent (CV)) = 3479 N_Case_Statement_Alternative 3480 then 3481 Error_Msg_N ("\\??(see case alternative #)", Loc); 3482 3483 else 3484 Error_Msg_N ("\\??(see assignment #)", Loc); 3485 end if; 3486 end if; 3487 end; 3488 end if; 3489 end Track; 3490 3491 -- Local variables 3492 3493 Orig : constant Node_Id := Original_Node (C); 3494 P : Node_Id; 3495 3496 -- Start of processing for Warn_On_Known_Condition 3497 3498 begin 3499 -- Adjust SCO condition if from source 3500 3501 if Generate_SCO 3502 and then Comes_From_Source (Orig) 3503 and then Is_Known_Branch 3504 then 3505 declare 3506 Atrue : Boolean; 3507 3508 begin 3509 Atrue := Test_Result; 3510 3511 if Present (Parent (C)) and then Nkind (Parent (C)) = N_Op_Not then 3512 Atrue := not Atrue; 3513 end if; 3514 3515 Set_SCO_Condition (Orig, Atrue); 3516 end; 3517 end if; 3518 3519 -- Argument replacement in an inlined body can make conditions static. 3520 -- Do not emit warnings in this case. 3521 3522 if In_Inlined_Body then 3523 return; 3524 end if; 3525 3526 if Constant_Condition_Warnings 3527 and then Is_Known_Branch 3528 and then Comes_From_Source (Orig) 3529 and then Nkind (Orig) in N_Has_Entity 3530 and then not In_Instance 3531 then 3532 -- Don't warn if comparison of result of attribute against a constant 3533 -- value, since this is likely legitimate conditional compilation. 3534 3535 if Is_Attribute_And_Known_Value_Comparison (C) then 3536 return; 3537 end if; 3538 3539 -- See if this is in a statement or a declaration 3540 3541 P := Parent (C); 3542 loop 3543 -- If tree is not attached, do not issue warning (this is very 3544 -- peculiar, and probably arises from some other error condition). 3545 3546 if No (P) then 3547 return; 3548 3549 -- If we are in a declaration, then no warning, since in practice 3550 -- conditionals in declarations are used for intended tests which 3551 -- may be known at compile time, e.g. things like 3552 3553 -- x : constant Integer := 2 + (Word'Size = 32); 3554 3555 -- And a warning is annoying in such cases 3556 3557 elsif Nkind (P) in N_Declaration 3558 or else 3559 Nkind (P) in N_Later_Decl_Item 3560 then 3561 return; 3562 3563 -- Don't warn in assert or check pragma, since presumably tests in 3564 -- such a context are very definitely intended, and might well be 3565 -- known at compile time. Note that we have to test the original 3566 -- node, since assert pragmas get rewritten at analysis time. 3567 3568 elsif Nkind (Original_Node (P)) = N_Pragma 3569 and then 3570 Pragma_Name_Unmapped (Original_Node (P)) 3571 in Name_Assert | Name_Check 3572 then 3573 return; 3574 end if; 3575 3576 exit when Is_Statement (P); 3577 P := Parent (P); 3578 end loop; 3579 3580 -- Here we issue the warning unless some sub-operand has warnings 3581 -- set off, in which case we suppress the warning for the node. If 3582 -- the original expression is an inequality, it has been expanded 3583 -- into a negation, and the value of the original expression is the 3584 -- negation of the equality. If the expression is an entity that 3585 -- appears within a negation, it is clearer to flag the negation 3586 -- itself, and report on its constant value. 3587 3588 if not Operand_Has_Warnings_Suppressed (C) then 3589 declare 3590 True_Branch : Boolean := Test_Result; 3591 Cond : Node_Id := C; 3592 3593 begin 3594 if Present (Parent (C)) 3595 and then Nkind (Parent (C)) = N_Op_Not 3596 then 3597 True_Branch := not True_Branch; 3598 Cond := Parent (C); 3599 end if; 3600 3601 -- Condition always True 3602 3603 if True_Branch then 3604 if Is_Entity_Name (Original_Node (C)) 3605 and then Nkind (Cond) /= N_Op_Not 3606 then 3607 Error_Msg_NE 3608 ("object & is always True at this point?c?", 3609 Cond, Original_Node (C)); 3610 Track (Original_Node (C), Cond); 3611 3612 else 3613 Error_Msg_N ("condition is always True?c?", Cond); 3614 Track (Cond, Cond); 3615 end if; 3616 3617 -- Condition always False 3618 3619 else 3620 if Is_Entity_Name (Original_Node (C)) 3621 and then Nkind (Cond) /= N_Op_Not 3622 then 3623 Error_Msg_NE 3624 ("object & is always False at this point?c?", 3625 Cond, Original_Node (C)); 3626 Track (Original_Node (C), Cond); 3627 3628 else 3629 Error_Msg_N ("condition is always False?c?", Cond); 3630 Track (Cond, Cond); 3631 end if; 3632 end if; 3633 end; 3634 end if; 3635 end if; 3636 end Warn_On_Known_Condition; 3637 3638 --------------------------------------- 3639 -- Warn_On_Modified_As_Out_Parameter -- 3640 --------------------------------------- 3641 3642 function Warn_On_Modified_As_Out_Parameter (E : Entity_Id) return Boolean is 3643 begin 3644 return 3645 (Warn_On_Modified_Unread and then Is_Only_Out_Parameter (E)) 3646 or else Warn_On_All_Unread_Out_Parameters; 3647 end Warn_On_Modified_As_Out_Parameter; 3648 3649 --------------------------------- 3650 -- Warn_On_Overlapping_Actuals -- 3651 --------------------------------- 3652 3653 procedure Warn_On_Overlapping_Actuals (Subp : Entity_Id; N : Node_Id) is 3654 function Refer_Same_Object 3655 (Act1 : Node_Id; 3656 Act2 : Node_Id) return Boolean; 3657 -- Two names are known to refer to the same object if the two names 3658 -- are known to denote the same object; or one of the names is a 3659 -- selected_component, indexed_component, or slice and its prefix is 3660 -- known to refer to the same object as the other name; or one of the 3661 -- two names statically denotes a renaming declaration whose renamed 3662 -- object_name is known to refer to the same object as the other name 3663 -- (RM 6.4.1(6.11/3)) 3664 3665 ----------------------- 3666 -- Refer_Same_Object -- 3667 ----------------------- 3668 3669 function Refer_Same_Object 3670 (Act1 : Node_Id; 3671 Act2 : Node_Id) return Boolean 3672 is 3673 begin 3674 return 3675 Denotes_Same_Object (Act1, Act2) 3676 or else Denotes_Same_Prefix (Act1, Act2); 3677 end Refer_Same_Object; 3678 3679 -- Local variables 3680 3681 Act1 : Node_Id; 3682 Act2 : Node_Id; 3683 Form1 : Entity_Id; 3684 Form2 : Entity_Id; 3685 3686 -- Start of processing for Warn_On_Overlapping_Actuals 3687 3688 begin 3689 3690 if Ada_Version < Ada_2012 and then not Warn_On_Overlap then 3691 return; 3692 end if; 3693 3694 -- Exclude calls rewritten as enumeration literals 3695 3696 if Nkind (N) not in N_Subprogram_Call 3697 and then Nkind (N) /= N_Entry_Call_Statement 3698 then 3699 return; 3700 end if; 3701 3702 -- If a call C has two or more parameters of mode in out or out that are 3703 -- of an elementary type, then the call is legal only if for each name 3704 -- N that is passed as a parameter of mode in out or out to the call C, 3705 -- there is no other name among the other parameters of mode in out or 3706 -- out to C that is known to denote the same object (RM 6.4.1(6.15/3)) 3707 -- This has been clarified in AI12-0216 to indicate that the illegality 3708 -- only occurs if both formals are of an elementary type, because of the 3709 -- nondeterminism on the write-back of the corresponding actuals. 3710 -- Earlier versions of the language made it illegal if only one of the 3711 -- actuals was an elementary parameter that overlapped a composite 3712 -- actual, and both were writable. 3713 3714 -- If appropriate warning switch is set, we also report warnings on 3715 -- overlapping parameters that are composite types. Users find these 3716 -- warnings useful, and they are used in style guides. 3717 3718 -- It is also worthwhile to warn on overlaps of composite objects when 3719 -- only one of the formals is (in)-out. Note that the RM rule above is 3720 -- a legality rule. We choose to implement this check as a warning to 3721 -- avoid major incompatibilities with legacy code. 3722 3723 -- Note also that the rule in 6.4.1 (6.17/3), introduced by AI12-0324, 3724 -- is potentially more expensive to verify, and is not yet implemented. 3725 3726 Form1 := First_Formal (Subp); 3727 Act1 := First_Actual (N); 3728 while Present (Form1) and then Present (Act1) loop 3729 if Is_Generic_Type (Etype (Act1)) then 3730 return; 3731 end if; 3732 3733 -- One of the formals must be either (in)-out or composite. 3734 -- The other must be (in)-out. 3735 3736 if Is_Elementary_Type (Etype (Act1)) 3737 and then Ekind (Form1) = E_In_Parameter 3738 then 3739 null; 3740 3741 else 3742 Form2 := Next_Formal (Form1); 3743 Act2 := Next_Actual (Act1); 3744 while Present (Form2) and then Present (Act2) loop 3745 if Refer_Same_Object (Act1, Act2) then 3746 if Is_Generic_Type (Etype (Act2)) then 3747 return; 3748 end if; 3749 3750 -- First case : two writable elementary parameters 3751 -- that overlap. 3752 3753 if (Is_Elementary_Type (Etype (Form1)) 3754 and then Is_Elementary_Type (Etype (Form2)) 3755 and then Ekind (Form1) /= E_In_Parameter 3756 and then Ekind (Form2) /= E_In_Parameter) 3757 3758 -- Second case : two composite parameters that overlap, 3759 -- one of which is writable. 3760 3761 or else (Is_Composite_Type (Etype (Form1)) 3762 and then Is_Composite_Type (Etype (Form2)) 3763 and then (Ekind (Form1) /= E_In_Parameter 3764 or else Ekind (Form2) /= E_In_Parameter)) 3765 3766 -- Third case : an elementary writable parameter that 3767 -- overlaps a composite one. 3768 3769 or else (Is_Elementary_Type (Etype (Form1)) 3770 and then Ekind (Form1) /= E_In_Parameter 3771 and then Is_Composite_Type (Etype (Form2))) 3772 3773 or else (Is_Elementary_Type (Etype (Form2)) 3774 and then Ekind (Form2) /= E_In_Parameter 3775 and then Is_Composite_Type (Etype (Form1))) 3776 then 3777 3778 -- Guard against previous errors 3779 3780 if Error_Posted (N) 3781 or else No (Etype (Act1)) 3782 or else No (Etype (Act2)) 3783 then 3784 null; 3785 3786 -- If the actual is a function call in prefix notation, 3787 -- there is no real overlap. 3788 3789 elsif Nkind (Act2) = N_Function_Call then 3790 null; 3791 3792 -- If type is explicitly not by-copy, assume that 3793 -- aliasing is intended. 3794 3795 elsif 3796 Present (Underlying_Type (Etype (Form1))) 3797 and then 3798 (Is_By_Reference_Type 3799 (Underlying_Type (Etype (Form1))) 3800 or else 3801 Convention (Underlying_Type (Etype (Form1))) = 3802 Convention_Ada_Pass_By_Reference) 3803 then 3804 null; 3805 3806 -- Under Ada 2012 we only report warnings on overlapping 3807 -- arrays and record types if switch is set. 3808 3809 elsif Ada_Version >= Ada_2012 3810 and then not Is_Elementary_Type (Etype (Form1)) 3811 and then not Warn_On_Overlap 3812 then 3813 null; 3814 3815 -- Here we may need to issue overlap message 3816 3817 else 3818 Error_Msg_Warn := 3819 3820 -- Overlap checking is an error only in Ada 2012. 3821 -- For earlier versions of Ada, this is a warning. 3822 3823 Ada_Version < Ada_2012 3824 3825 -- Overlap is only illegal in Ada 2012 in the case 3826 -- of elementary types (passed by copy). For other 3827 -- types we always have a warning in all versions. 3828 -- This is clarified by AI12-0216. 3829 3830 or else not 3831 (Is_Elementary_Type (Etype (Form1)) 3832 and then Is_Elementary_Type (Etype (Form2))) 3833 3834 -- debug flag -gnatd.E changes the error to a 3835 -- warning even in Ada 2012 mode. 3836 3837 or else Error_To_Warning; 3838 3839 if Is_Elementary_Type (Etype (Act1)) 3840 and then Ekind (Form2) = E_In_Parameter 3841 then 3842 null; -- No real aliasing 3843 3844 elsif Is_Elementary_Type (Etype (Act2)) 3845 and then Ekind (Form2) = E_In_Parameter 3846 then 3847 null; -- Ditto 3848 3849 -- If the call was written in prefix notation, and 3850 -- thus its prefix before rewriting was a selected 3851 -- component, count only visible actuals in call. 3852 3853 elsif Is_Entity_Name (First_Actual (N)) 3854 and then Nkind (Original_Node (N)) = Nkind (N) 3855 and then Nkind (Name (Original_Node (N))) = 3856 N_Selected_Component 3857 and then 3858 Is_Entity_Name (Prefix (Name (Original_Node (N)))) 3859 and then 3860 Entity (Prefix (Name (Original_Node (N)))) = 3861 Entity (First_Actual (N)) 3862 then 3863 if Act1 = First_Actual (N) then 3864 Error_Msg_FE 3865 ("<I<`IN OUT` prefix overlaps with " 3866 & "actual for&", Act1, Form2); 3867 3868 else 3869 -- For greater clarity, give name of formal 3870 3871 Error_Msg_Node_2 := Form2; 3872 Error_Msg_FE 3873 ("<I<writable actual for & overlaps with " 3874 & "actual for&", Act1, Form2); 3875 end if; 3876 3877 else 3878 -- For greater clarity, give name of formal 3879 3880 Error_Msg_Node_2 := Form2; 3881 3882 -- This is one of the messages 3883 3884 Error_Msg_FE 3885 ("<I<writable actual for & overlaps with " 3886 & "actual for&", Act1, Form1); 3887 end if; 3888 end if; 3889 end if; 3890 3891 return; 3892 end if; 3893 3894 Next_Formal (Form2); 3895 Next_Actual (Act2); 3896 end loop; 3897 end if; 3898 3899 Next_Formal (Form1); 3900 Next_Actual (Act1); 3901 end loop; 3902 end Warn_On_Overlapping_Actuals; 3903 3904 ------------------------------ 3905 -- Warn_On_Suspicious_Index -- 3906 ------------------------------ 3907 3908 procedure Warn_On_Suspicious_Index (Name : Entity_Id; X : Node_Id) is 3909 3910 Low_Bound : Uint; 3911 -- Set to lower bound for a suspicious type 3912 3913 Ent : Entity_Id; 3914 -- Entity for array reference 3915 3916 Typ : Entity_Id; 3917 -- Array type 3918 3919 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean; 3920 -- Tests to see if Typ is a type for which we may have a suspicious 3921 -- index, namely an unconstrained array type, whose lower bound is 3922 -- either zero or one. If so, True is returned, and Low_Bound is set 3923 -- to this lower bound. If not, False is returned, and Low_Bound is 3924 -- undefined on return. 3925 -- 3926 -- For now, we limit this to standard string types, so any other 3927 -- unconstrained types return False. We may change our minds on this 3928 -- later on, but strings seem the most important case. 3929 3930 procedure Test_Suspicious_Index; 3931 -- Test if index is of suspicious type and if so, generate warning 3932 3933 ------------------------ 3934 -- Is_Suspicious_Type -- 3935 ------------------------ 3936 3937 function Is_Suspicious_Type (Typ : Entity_Id) return Boolean is 3938 LB : Node_Id; 3939 3940 begin 3941 if Is_Array_Type (Typ) 3942 and then not Is_Constrained (Typ) 3943 and then Number_Dimensions (Typ) = 1 3944 and then Is_Standard_String_Type (Typ) 3945 and then not Has_Warnings_Off (Typ) 3946 then 3947 LB := Type_Low_Bound (Etype (First_Index (Typ))); 3948 3949 if Compile_Time_Known_Value (LB) then 3950 Low_Bound := Expr_Value (LB); 3951 return Low_Bound = Uint_0 or else Low_Bound = Uint_1; 3952 end if; 3953 end if; 3954 3955 return False; 3956 end Is_Suspicious_Type; 3957 3958 --------------------------- 3959 -- Test_Suspicious_Index -- 3960 --------------------------- 3961 3962 procedure Test_Suspicious_Index is 3963 3964 function Length_Reference (N : Node_Id) return Boolean; 3965 -- Check if node N is of the form Name'Length 3966 3967 procedure Warn1; 3968 -- Generate first warning line 3969 3970 procedure Warn_On_Index_Below_Lower_Bound; 3971 -- Generate a warning on indexing the array with a literal value 3972 -- below the lower bound of the index type. 3973 3974 procedure Warn_On_Literal_Index; 3975 -- Generate a warning on indexing the array with a literal value 3976 3977 ---------------------- 3978 -- Length_Reference -- 3979 ---------------------- 3980 3981 function Length_Reference (N : Node_Id) return Boolean is 3982 R : constant Node_Id := Original_Node (N); 3983 begin 3984 return 3985 Nkind (R) = N_Attribute_Reference 3986 and then Attribute_Name (R) = Name_Length 3987 and then Is_Entity_Name (Prefix (R)) 3988 and then Entity (Prefix (R)) = Ent; 3989 end Length_Reference; 3990 3991 ----------- 3992 -- Warn1 -- 3993 ----------- 3994 3995 procedure Warn1 is 3996 begin 3997 Error_Msg_Uint_1 := Low_Bound; 3998 Error_Msg_FE -- CODEFIX 3999 ("?w?index for& may assume lower bound of^", X, Ent); 4000 end Warn1; 4001 4002 ------------------------------------- 4003 -- Warn_On_Index_Below_Lower_Bound -- 4004 ------------------------------------- 4005 4006 procedure Warn_On_Index_Below_Lower_Bound is 4007 begin 4008 if Is_Standard_String_Type (Typ) then 4009 Discard_Node 4010 (Compile_Time_Constraint_Error 4011 (N => X, 4012 Msg => "?w?string index should be positive")); 4013 else 4014 Discard_Node 4015 (Compile_Time_Constraint_Error 4016 (N => X, 4017 Msg => "?w?index out of the allowed range")); 4018 end if; 4019 end Warn_On_Index_Below_Lower_Bound; 4020 4021 --------------------------- 4022 -- Warn_On_Literal_Index -- 4023 --------------------------- 4024 4025 procedure Warn_On_Literal_Index is 4026 begin 4027 Warn1; 4028 4029 -- Case where original form of subscript is an integer literal 4030 4031 if Nkind (Original_Node (X)) = N_Integer_Literal then 4032 if Intval (X) = Low_Bound then 4033 Error_Msg_FE -- CODEFIX 4034 ("\?w?suggested replacement: `&''First`", X, Ent); 4035 else 4036 Error_Msg_Uint_1 := Intval (X) - Low_Bound; 4037 Error_Msg_FE -- CODEFIX 4038 ("\?w?suggested replacement: `&''First + ^`", X, Ent); 4039 4040 end if; 4041 4042 -- Case where original form of subscript is more complex 4043 4044 else 4045 -- Build string X'First - 1 + expression where the expression 4046 -- is the original subscript. If the expression starts with "1 4047 -- + ", then the "- 1 + 1" is elided. 4048 4049 Error_Msg_String (1 .. 13) := "'First - 1 + "; 4050 Error_Msg_Strlen := 13; 4051 4052 declare 4053 Sref : Source_Ptr := Sloc (First_Node (Original_Node (X))); 4054 Tref : constant Source_Buffer_Ptr := 4055 Source_Text (Get_Source_File_Index (Sref)); 4056 -- Tref (Sref) is used to scan the subscript 4057 4058 Pctr : Natural; 4059 -- Parentheses counter when scanning subscript 4060 4061 begin 4062 -- Tref (Sref) points to start of subscript 4063 4064 -- Elide - 1 if subscript starts with 1 + 4065 4066 if Tref (Sref .. Sref + 2) = "1 +" then 4067 Error_Msg_Strlen := Error_Msg_Strlen - 6; 4068 Sref := Sref + 2; 4069 4070 elsif Tref (Sref .. Sref + 1) = "1+" then 4071 Error_Msg_Strlen := Error_Msg_Strlen - 6; 4072 Sref := Sref + 1; 4073 end if; 4074 4075 -- Now we will copy the subscript to the string buffer 4076 4077 Pctr := 0; 4078 loop 4079 -- Count parens, exit if terminating right paren. Note 4080 -- check to ignore paren appearing as character literal. 4081 4082 if Tref (Sref + 1) = ''' 4083 and then 4084 Tref (Sref - 1) = ''' 4085 then 4086 null; 4087 else 4088 if Tref (Sref) = '(' then 4089 Pctr := Pctr + 1; 4090 elsif Tref (Sref) = ')' then 4091 exit when Pctr = 0; 4092 Pctr := Pctr - 1; 4093 end if; 4094 end if; 4095 4096 -- Done if terminating double dot (slice case) 4097 4098 exit when Pctr = 0 4099 and then (Tref (Sref .. Sref + 1) = ".." 4100 or else 4101 Tref (Sref .. Sref + 2) = " .."); 4102 4103 -- Quit if we have hit EOF character, something wrong 4104 4105 if Tref (Sref) = EOF then 4106 return; 4107 end if; 4108 4109 -- String literals are too much of a pain to handle 4110 4111 if Tref (Sref) = '"' or else Tref (Sref) = '%' then 4112 return; 4113 end if; 4114 4115 -- If we have a 'Range reference, then this is a case 4116 -- where we cannot easily give a replacement. Don't try. 4117 4118 if Tref (Sref .. Sref + 4) = "range" 4119 and then Tref (Sref - 1) < 'A' 4120 and then Tref (Sref + 5) < 'A' 4121 then 4122 return; 4123 end if; 4124 4125 -- Else store next character 4126 4127 Error_Msg_Strlen := Error_Msg_Strlen + 1; 4128 Error_Msg_String (Error_Msg_Strlen) := Tref (Sref); 4129 Sref := Sref + 1; 4130 4131 -- If we get more than 40 characters then the expression 4132 -- is too long to copy, or something has gone wrong. In 4133 -- either case, just skip the attempt at a suggested fix. 4134 4135 if Error_Msg_Strlen > 40 then 4136 return; 4137 end if; 4138 end loop; 4139 end; 4140 4141 -- Replacement subscript is now in string buffer 4142 4143 Error_Msg_FE -- CODEFIX 4144 ("\?w?suggested replacement: `&~`", Original_Node (X), Ent); 4145 end if; 4146 end Warn_On_Literal_Index; 4147 4148 -- Start of processing for Test_Suspicious_Index 4149 4150 begin 4151 -- Nothing to do if subscript does not come from source (we don't 4152 -- want to give garbage warnings on compiler expanded code, e.g. the 4153 -- loops generated for slice assignments. Such junk warnings would 4154 -- be placed on source constructs with no subscript in sight). 4155 4156 if not Comes_From_Source (Original_Node (X)) then 4157 return; 4158 end if; 4159 4160 -- Case where subscript is a constant integer 4161 4162 if Nkind (X) = N_Integer_Literal then 4163 4164 -- Case where subscript is lower than the lowest possible bound. 4165 -- This might be the case for example when programmers try to 4166 -- access a string at index 0, as they are used to in other 4167 -- programming languages like C. 4168 4169 if Intval (X) < Low_Bound then 4170 Warn_On_Index_Below_Lower_Bound; 4171 else 4172 Warn_On_Literal_Index; 4173 end if; 4174 4175 -- Case where subscript is of the form X'Length 4176 4177 elsif Length_Reference (X) then 4178 Warn1; 4179 Error_Msg_Node_2 := Ent; 4180 Error_Msg_FE 4181 ("\?w?suggest replacement of `&''Length` by `&''Last`", 4182 X, Ent); 4183 4184 -- Case where subscript is of the form X'Length - expression 4185 4186 elsif Nkind (X) = N_Op_Subtract 4187 and then Length_Reference (Left_Opnd (X)) 4188 then 4189 Warn1; 4190 Error_Msg_Node_2 := Ent; 4191 Error_Msg_FE 4192 ("\?w?suggest replacement of `&''Length` by `&''Last`", 4193 Left_Opnd (X), Ent); 4194 end if; 4195 end Test_Suspicious_Index; 4196 4197 -- Start of processing for Warn_On_Suspicious_Index 4198 4199 begin 4200 -- Only process if warnings activated 4201 4202 if Warn_On_Assumed_Low_Bound then 4203 4204 -- Test if array is simple entity name 4205 4206 if Is_Entity_Name (Name) then 4207 4208 -- Test if array is parameter of unconstrained string type 4209 4210 Ent := Entity (Name); 4211 Typ := Etype (Ent); 4212 4213 if Is_Formal (Ent) 4214 and then Is_Suspicious_Type (Typ) 4215 and then not Low_Bound_Tested (Ent) 4216 then 4217 Test_Suspicious_Index; 4218 end if; 4219 end if; 4220 end if; 4221 end Warn_On_Suspicious_Index; 4222 4223 ------------------------------- 4224 -- Warn_On_Suspicious_Update -- 4225 ------------------------------- 4226 4227 procedure Warn_On_Suspicious_Update (N : Node_Id) is 4228 Par : constant Node_Id := Parent (N); 4229 Arg : Node_Id; 4230 4231 begin 4232 -- Only process if warnings activated 4233 4234 if Warn_On_Suspicious_Contract then 4235 if Nkind (Par) in N_Op_Eq | N_Op_Ne then 4236 if N = Left_Opnd (Par) then 4237 Arg := Right_Opnd (Par); 4238 else 4239 Arg := Left_Opnd (Par); 4240 end if; 4241 4242 if Same_Object (Prefix (N), Arg) then 4243 if Nkind (Par) = N_Op_Eq then 4244 Error_Msg_N 4245 ("suspicious equality test with modified version of " 4246 & "same object?T?", Par); 4247 else 4248 Error_Msg_N 4249 ("suspicious inequality test with modified version of " 4250 & "same object?T?", Par); 4251 end if; 4252 end if; 4253 end if; 4254 end if; 4255 end Warn_On_Suspicious_Update; 4256 4257 -------------------------------------- 4258 -- Warn_On_Unassigned_Out_Parameter -- 4259 -------------------------------------- 4260 4261 procedure Warn_On_Unassigned_Out_Parameter 4262 (Return_Node : Node_Id; 4263 Scope_Id : Entity_Id) 4264 is 4265 Form : Entity_Id; 4266 Form2 : Entity_Id; 4267 4268 begin 4269 -- Ignore if procedure or return statement does not come from source 4270 4271 if not Comes_From_Source (Scope_Id) 4272 or else not Comes_From_Source (Return_Node) 4273 then 4274 return; 4275 end if; 4276 4277 -- Loop through formals 4278 4279 Form := First_Formal (Scope_Id); 4280 while Present (Form) loop 4281 4282 -- We are only interested in OUT parameters that come from source 4283 -- and are never set in the source, and furthermore only in scalars 4284 -- since non-scalars generate too many false positives. 4285 4286 if Ekind (Form) = E_Out_Parameter 4287 and then Never_Set_In_Source_Check_Spec (Form) 4288 and then Is_Scalar_Type (Etype (Form)) 4289 and then not Present (Unset_Reference (Form)) 4290 then 4291 -- Before we issue the warning, an add ad hoc defence against the 4292 -- most common case of false positives with this warning which is 4293 -- the case where there is a Boolean OUT parameter that has been 4294 -- set, and whose meaning is "ignore the values of the other 4295 -- parameters". We can't of course reliably tell this case at 4296 -- compile time, but the following test kills a lot of false 4297 -- positives, without generating a significant number of false 4298 -- negatives (missed real warnings). 4299 4300 Form2 := First_Formal (Scope_Id); 4301 while Present (Form2) loop 4302 if Ekind (Form2) = E_Out_Parameter 4303 and then Root_Type (Etype (Form2)) = Standard_Boolean 4304 and then not Never_Set_In_Source_Check_Spec (Form2) 4305 then 4306 return; 4307 end if; 4308 4309 Next_Formal (Form2); 4310 end loop; 4311 4312 -- Here all conditions are met, record possible unset reference 4313 4314 Set_Unset_Reference (Form, Return_Node); 4315 end if; 4316 4317 Next_Formal (Form); 4318 end loop; 4319 end Warn_On_Unassigned_Out_Parameter; 4320 4321 --------------------------------- 4322 -- Warn_On_Unreferenced_Entity -- 4323 --------------------------------- 4324 4325 procedure Warn_On_Unreferenced_Entity 4326 (Spec_E : Entity_Id; 4327 Body_E : Entity_Id := Empty) 4328 is 4329 E : Entity_Id := Spec_E; 4330 4331 begin 4332 if not Referenced_Check_Spec (E) 4333 and then not Has_Pragma_Unreferenced_Check_Spec (E) 4334 and then not Warnings_Off_Check_Spec (E) 4335 and then not Has_Junk_Name (Spec_E) 4336 and then not Is_Exported (Spec_E) 4337 then 4338 case Ekind (E) is 4339 when E_Variable => 4340 4341 -- Case of variable that is assigned but not read. We suppress 4342 -- the message if the variable is volatile, has an address 4343 -- clause, is aliased, or is a renaming, or is imported. 4344 4345 if Referenced_As_LHS_Check_Spec (E) then 4346 if Warn_On_Modified_Unread 4347 and then No (Address_Clause (E)) 4348 and then not Is_Volatile (E) 4349 and then not Is_Imported (E) 4350 and then not Is_Aliased (E) 4351 and then No (Renamed_Object (E)) 4352 then 4353 if not Has_Pragma_Unmodified_Check_Spec (E) then 4354 Error_Msg_N -- CODEFIX 4355 ("?m?variable & is assigned but never read!", E); 4356 end if; 4357 4358 Set_Last_Assignment (E, Empty); 4359 end if; 4360 4361 -- Normal case of neither assigned nor read (exclude variables 4362 -- referenced as out parameters, since we already generated 4363 -- appropriate warnings at the call point in this case). 4364 4365 elsif not Referenced_As_Out_Parameter (E) then 4366 4367 -- We suppress the message for types for which a valid 4368 -- pragma Unreferenced_Objects has been given, otherwise 4369 -- we go ahead and give the message. 4370 4371 if not Has_Pragma_Unreferenced_Objects (Etype (E)) then 4372 4373 -- Distinguish renamed case in message 4374 4375 if Present (Renamed_Object (E)) 4376 and then Comes_From_Source (Renamed_Object (E)) 4377 then 4378 Error_Msg_N -- CODEFIX 4379 ("?u?renamed variable & is not referenced!", E); 4380 else 4381 Error_Msg_N -- CODEFIX 4382 ("?u?variable & is not referenced!", E); 4383 end if; 4384 end if; 4385 end if; 4386 4387 when E_Constant => 4388 if not Has_Pragma_Unreferenced_Objects (Etype (E)) then 4389 if Present (Renamed_Object (E)) 4390 and then Comes_From_Source (Renamed_Object (E)) 4391 then 4392 Error_Msg_N -- CODEFIX 4393 ("?u?renamed constant & is not referenced!", E); 4394 else 4395 Error_Msg_N -- CODEFIX 4396 ("?u?constant & is not referenced!", E); 4397 end if; 4398 end if; 4399 4400 when E_In_Out_Parameter 4401 | E_In_Parameter 4402 => 4403 -- Do not emit message for formals of a renaming, because they 4404 -- are never referenced explicitly. 4405 4406 if Nkind (Original_Node (Unit_Declaration_Node (Scope (E)))) /= 4407 N_Subprogram_Renaming_Declaration 4408 then 4409 -- Suppress this message for an IN OUT parameter of a 4410 -- non-scalar type, since it is normal to have only an 4411 -- assignment in such a case. 4412 4413 if Ekind (E) = E_In_Parameter 4414 or else not Referenced_As_LHS_Check_Spec (E) 4415 or else Is_Scalar_Type (Etype (E)) 4416 then 4417 if Present (Body_E) then 4418 E := Body_E; 4419 end if; 4420 4421 declare 4422 S : Node_Id := Scope (E); 4423 begin 4424 if Ekind (S) = E_Subprogram_Body then 4425 S := Parent (S); 4426 4427 while Nkind (S) not in 4428 N_Expression_Function | 4429 N_Subprogram_Body | 4430 N_Subprogram_Renaming_Declaration | 4431 N_Empty 4432 loop 4433 S := Parent (S); 4434 end loop; 4435 4436 if Present (S) then 4437 S := Corresponding_Spec (S); 4438 end if; 4439 end if; 4440 4441 -- Do not warn for dispatching operations, because 4442 -- that causes too much noise. Also do not warn for 4443 -- trivial subprograms (e.g. stubs). 4444 4445 if (No (S) or else not Is_Dispatching_Operation (S)) 4446 and then not Is_Trivial_Subprogram (Scope (E)) 4447 then 4448 Error_Msg_NE -- CODEFIX 4449 ("?u?formal parameter & is not referenced!", 4450 E, Spec_E); 4451 end if; 4452 end; 4453 end if; 4454 end if; 4455 4456 when E_Out_Parameter => 4457 null; 4458 4459 when E_Discriminant => 4460 Error_Msg_N ("?u?discriminant & is not referenced!", E); 4461 4462 when E_Named_Integer 4463 | E_Named_Real 4464 => 4465 Error_Msg_N -- CODEFIX 4466 ("?u?named number & is not referenced!", E); 4467 4468 when Formal_Object_Kind => 4469 Error_Msg_N -- CODEFIX 4470 ("?u?formal object & is not referenced!", E); 4471 4472 when E_Enumeration_Literal => 4473 Error_Msg_N -- CODEFIX 4474 ("?u?literal & is not referenced!", E); 4475 4476 when E_Function => 4477 Error_Msg_N -- CODEFIX 4478 ("?u?function & is not referenced!", E); 4479 4480 when E_Procedure => 4481 Error_Msg_N -- CODEFIX 4482 ("?u?procedure & is not referenced!", E); 4483 4484 when E_Package => 4485 Error_Msg_N -- CODEFIX 4486 ("?u?package & is not referenced!", E); 4487 4488 when E_Exception => 4489 Error_Msg_N -- CODEFIX 4490 ("?u?exception & is not referenced!", E); 4491 4492 when E_Label => 4493 Error_Msg_N -- CODEFIX 4494 ("?u?label & is not referenced!", E); 4495 4496 when E_Generic_Procedure => 4497 Error_Msg_N -- CODEFIX 4498 ("?u?generic procedure & is never instantiated!", E); 4499 4500 when E_Generic_Function => 4501 Error_Msg_N -- CODEFIX 4502 ("?u?generic function & is never instantiated!", E); 4503 4504 when Type_Kind => 4505 Error_Msg_N -- CODEFIX 4506 ("?u?type & is not referenced!", E); 4507 4508 when others => 4509 Error_Msg_N -- CODEFIX 4510 ("?u?& is not referenced!", E); 4511 end case; 4512 4513 -- Kill warnings on the entity on which the message has been posted 4514 -- (nothing is posted on out parameters because back end might be 4515 -- able to uncover an uninitialized path, and warn accordingly). 4516 4517 if Ekind (E) /= E_Out_Parameter then 4518 Set_Warnings_Off (E); 4519 end if; 4520 end if; 4521 end Warn_On_Unreferenced_Entity; 4522 4523 -------------------------------- 4524 -- Warn_On_Useless_Assignment -- 4525 -------------------------------- 4526 4527 procedure Warn_On_Useless_Assignment 4528 (Ent : Entity_Id; 4529 N : Node_Id := Empty) 4530 is 4531 P : Node_Id; 4532 X : Node_Id; 4533 4534 function Check_Ref (N : Node_Id) return Traverse_Result; 4535 -- Used to instantiate Traverse_Func. Returns Abandon if a reference to 4536 -- the entity in question is found. 4537 4538 function Test_No_Refs is new Traverse_Func (Check_Ref); 4539 4540 --------------- 4541 -- Check_Ref -- 4542 --------------- 4543 4544 function Check_Ref (N : Node_Id) return Traverse_Result is 4545 begin 4546 -- Check reference to our identifier. We use name equality here 4547 -- because the exception handlers have not yet been analyzed. This 4548 -- is not quite right, but it really does not matter that we fail 4549 -- to output the warning in some obscure cases of name clashes. 4550 4551 if Nkind (N) = N_Identifier and then Chars (N) = Chars (Ent) then 4552 return Abandon; 4553 else 4554 return OK; 4555 end if; 4556 end Check_Ref; 4557 4558 -- Start of processing for Warn_On_Useless_Assignment 4559 4560 begin 4561 -- Check if this is a case we want to warn on, a scalar or access 4562 -- variable with the last assignment field set, with warnings enabled, 4563 -- and which is not imported or exported. We also check that it is OK 4564 -- to capture the value. We are not going to capture any value, but 4565 -- the warning message depends on the same kind of conditions. 4566 4567 -- If the assignment appears as an out-parameter in a call within an 4568 -- expression function it may be detected twice: once when expression 4569 -- itself is analyzed, and once when the constructed body is analyzed. 4570 -- We don't want to emit a spurious warning in this case. 4571 4572 if Is_Assignable (Ent) 4573 and then not Is_Return_Object (Ent) 4574 and then Present (Last_Assignment (Ent)) 4575 and then Last_Assignment (Ent) /= N 4576 and then not Is_Imported (Ent) 4577 and then not Is_Exported (Ent) 4578 and then Safe_To_Capture_Value (N, Ent) 4579 and then not Has_Pragma_Unreferenced_Check_Spec (Ent) 4580 and then not Has_Junk_Name (Ent) 4581 then 4582 -- Before we issue the message, check covering exception handlers. 4583 -- Search up tree for enclosing statement sequences and handlers. 4584 4585 P := Parent (Last_Assignment (Ent)); 4586 while Present (P) loop 4587 4588 -- Something is really wrong if we don't find a handled statement 4589 -- sequence, so just suppress the warning. 4590 4591 if No (P) then 4592 Set_Last_Assignment (Ent, Empty); 4593 return; 4594 4595 -- When we hit a package/subprogram body, issue warning and exit 4596 4597 elsif Nkind (P) in N_Entry_Body 4598 | N_Package_Body 4599 | N_Subprogram_Body 4600 | N_Task_Body 4601 then 4602 -- Case of assigned value never referenced 4603 4604 if No (N) then 4605 declare 4606 LA : constant Node_Id := Last_Assignment (Ent); 4607 4608 begin 4609 -- Don't give this for OUT and IN OUT formals, since 4610 -- clearly caller may reference the assigned value. Also 4611 -- never give such warnings for internal variables. In 4612 -- either case, word the warning in a conditional way, 4613 -- because in the case of a component of a controlled 4614 -- type, the assigned value might be referenced in the 4615 -- Finalize operation, so we can't make a definitive 4616 -- statement that it's never referenced. 4617 4618 if Ekind (Ent) = E_Variable 4619 and then not Is_Internal_Name (Chars (Ent)) 4620 then 4621 -- Give appropriate message, distinguishing between 4622 -- assignment statements and out parameters. 4623 4624 if Nkind (Parent (LA)) in N_Parameter_Association 4625 | N_Procedure_Call_Statement 4626 then 4627 Error_Msg_NE 4628 ("?m?& modified by call, but value might not be " 4629 & "referenced", LA, Ent); 4630 4631 else 4632 Error_Msg_NE -- CODEFIX 4633 ("?m?possibly useless assignment to&, value " 4634 & "might not be referenced!", LA, Ent); 4635 end if; 4636 end if; 4637 end; 4638 4639 -- Case of assigned value overwritten 4640 4641 else 4642 declare 4643 LA : constant Node_Id := Last_Assignment (Ent); 4644 4645 begin 4646 Error_Msg_Sloc := Sloc (N); 4647 4648 -- Give appropriate message, distinguishing between 4649 -- assignment statements and out parameters. 4650 4651 if Nkind (Parent (LA)) in N_Procedure_Call_Statement 4652 | N_Parameter_Association 4653 then 4654 Error_Msg_NE 4655 ("?m?& modified by call, but value overwritten #!", 4656 LA, Ent); 4657 else 4658 Error_Msg_NE -- CODEFIX 4659 ("?m?useless assignment to&, value overwritten #!", 4660 LA, Ent); 4661 end if; 4662 end; 4663 end if; 4664 4665 -- Clear last assignment indication and we are done 4666 4667 Set_Last_Assignment (Ent, Empty); 4668 return; 4669 4670 -- Enclosing handled sequence of statements 4671 4672 elsif Nkind (P) = N_Handled_Sequence_Of_Statements then 4673 4674 -- Check exception handlers present 4675 4676 if Present (Exception_Handlers (P)) then 4677 4678 -- If we are not at the top level, we regard an inner 4679 -- exception handler as a decisive indicator that we should 4680 -- not generate the warning, since the variable in question 4681 -- may be accessed after an exception in the outer block. 4682 4683 if Nkind (Parent (P)) not in N_Entry_Body 4684 | N_Package_Body 4685 | N_Subprogram_Body 4686 | N_Task_Body 4687 then 4688 Set_Last_Assignment (Ent, Empty); 4689 return; 4690 4691 -- Otherwise we are at the outer level. An exception 4692 -- handler is significant only if it references the 4693 -- variable in question, or if the entity in question 4694 -- is an OUT or IN OUT parameter, in which case 4695 -- the caller can reference it after the exception 4696 -- handler completes. 4697 4698 else 4699 if Is_Formal (Ent) then 4700 Set_Last_Assignment (Ent, Empty); 4701 return; 4702 4703 else 4704 X := First (Exception_Handlers (P)); 4705 while Present (X) loop 4706 if Test_No_Refs (X) = Abandon then 4707 Set_Last_Assignment (Ent, Empty); 4708 return; 4709 end if; 4710 4711 Next (X); 4712 end loop; 4713 end if; 4714 end if; 4715 end if; 4716 end if; 4717 4718 P := Parent (P); 4719 end loop; 4720 end if; 4721 end Warn_On_Useless_Assignment; 4722 4723 --------------------------------- 4724 -- Warn_On_Useless_Assignments -- 4725 --------------------------------- 4726 4727 procedure Warn_On_Useless_Assignments (E : Entity_Id) is 4728 Ent : Entity_Id; 4729 4730 begin 4731 Process_Deferred_References; 4732 4733 if Warn_On_Modified_Unread 4734 and then In_Extended_Main_Source_Unit (E) 4735 then 4736 Ent := First_Entity (E); 4737 while Present (Ent) loop 4738 Warn_On_Useless_Assignment (Ent); 4739 Next_Entity (Ent); 4740 end loop; 4741 end if; 4742 end Warn_On_Useless_Assignments; 4743 4744 ----------------------------- 4745 -- Warnings_Off_Check_Spec -- 4746 ----------------------------- 4747 4748 function Warnings_Off_Check_Spec (E : Entity_Id) return Boolean is 4749 begin 4750 if Is_Formal (E) and then Present (Spec_Entity (E)) then 4751 4752 -- Note: use of OR here instead of OR ELSE is deliberate, we want 4753 -- to mess with flags on both entities. 4754 4755 return Has_Warnings_Off (E) 4756 or 4757 Has_Warnings_Off (Spec_Entity (E)); 4758 4759 else 4760 return Has_Warnings_Off (E); 4761 end if; 4762 end Warnings_Off_Check_Spec; 4763 4764end Sem_Warn; 4765