This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] improve warnings


Tested on i686-linux, committed on trunk

This patch was designed to address the specific issue of not giving
the warning that "X is not modified, could be declared as constant"
for the case of unbounded strings, as shown by the example below
which did give the second warning when compiled with -gnatwa.

     1. with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
     2. procedure Cst is
     3.    C : Integer := 12;
           |
        >>> warning: "C" is not modified, could be declared constant
        >>> warning: variable "C" is not referenced

     4.    V : Unbounded_String := To_Unbounded_String ("toto");
           |
        >>> warning: "V" is not modified, could be declared constant

     5.    W : Unbounded_String;
           |
        >>> warning: variable "W" is assigned but never read

     6. begin
     7.    W := V;
     8. end Cst;

However the patch grew to be an overhaul of the mechanism for
warnings about unused, unreferenced etc variables, and the
changes, which involve setting the flags more accurately,
and testing them more accurately in Sem_Warn eliminate both
false negatives and false positives, generally improving this
warning mechanism

This patch also improves generation of warnings for in out parameters. The
flag Never_Set_In_Source is now properly set for in out parameters and
under control of -gnatwu, more cases of unreferenced in out parameters
now generate warnings. In addition, if an in out parameter is read but
never assigned, then under control of -gnatwk, a warning is given that
the mode could be in, as shown by the following, compiled with -gnatwk
-gnatj60 -gnatl:

     1. procedure k
     2.  (b : in out integer;
          |
        >>> warning: formal parameter "b" is not modified,
            mode could be "in" instead of "in out"

     3.   c : in out integer;
     4.   l : in out integer) is
     5. begin
     6.     c := b;
     7. end;

if we also set -gnatwu, we get:

     1. procedure k
     2.  (b : in out integer;
          |
        >>> warning: formal parameter "b" is not modified,
            mode could be "in" instead of "in out"

     3.   c : in out integer;
     4.   l : in out integer) is
          |
        >>> warning: formal parameter "l" is not referenced

     5. begin
     6.     c := b;
     7. end;


Finally, this patch enables a new warning for a return in a procedure where one
or more out parameters have not been set. The warning is limited to
scalar types, and a special exception is made if there is a boolean
out parameter that has been set (since this is a common idiom for
the case where other out parameters are not to be accessed if the
out boolean is set). Also, we don't give the warning if the out
parameter is never referenced, since the never read, never assigned
warning is quite sufficient in this case.

The following example shows the various cases:
	
	     1. procedure wtest is
	     2.    procedure b1
	     3.      (Flag       : Boolean;
	     4.       I1, I2, I3 : out Integer;
	                      |
	        >>> warning: variable "I3" is never read and never assigned
	
	     5.       S          : out String)
	     6.    is
	     7.    begin
	     8.       if Flag then
	     9.          I1 := 0;
	    10.          return;
	                 |
	        >>> warning: "out" parameter "I2" not set before return
	
	    11.       end if;
	    12.       I2 := 0;
	    13.       S := "Hello";
	    14.    end;
	    15.
	    16.    procedure b2
	    17.      (Flag       : Boolean;
	    18.       I1, I2, I3 : out Integer;
	                      |
	        >>> warning: variable "I3" is never read and never assigned
	
	    19.       x          : out boolean)
	    20.    is
	    21.    begin
	    22.       if Flag then
	    23.          x := False;
	    24.          I1 := 0;
	    25.          return;
	    26.       end if;
	    27.       I2 := 0;
	    28.    end;
	    29.
	    30. begin
	    31.    null;
	    32. end;

2007-08-14  Ed Schonberg  <schonberg@adacore.com>
	    Robert Dewar  <dewar@adacore.com>
	    Javier Miranda  <miranda@adacore.com>
	    Gary Dismukes  <dismukes@adacore.com>

	* einfo.ads, einfo.adb: Create a limited view of an incomplete type,
	to make treatment of limited views uniform for all visible declarations
	in a limited_withed package.
	Improve warnings for in out parameters
	(Set_Related_Interaface/Related_Interface): Allow the use of this
	attribute with constants.
	(Write_Field26_Name): Handle attribute Related_Interface in constants.
	Warn on duplicate pragma Preelaborable_Initialialization

	* sem_ch6.ads, sem_ch6.adb (Analyze_Subprogram_Body): Force the
	generation of a freezing node to ensure proper management of null
	excluding access types in the backend.
	(Create_Extra_Formals): Test base type of the formal when checking for
	the need to add an extra accessibility-level formal. Pass the entity E
	on all calls to Add_Extra_Formal (rather than Scope (Formal) as was
	originally being done in a couple of cases), to ensure that the
	Extra_Formals list gets set on the entity E when the first entity is
	added.
	(Conforming_Types): Add missing calls to Base_Type to the code that
	handles anonymous access types. This is required to handle the
	general case because Process_Formals builds internal subtype entities
	to handle null-excluding access types.
	(Make_Controlling_Function_Wrappers): Create wrappers for constructor
	functions that need it, even when not marked Requires_Overriding.
	Improve warnings for in out parameters
	(Analyze_Function_Return): Warn for disallowed null return
	Warn on return from procedure with unset out parameter
	Ensure consistent use of # in error messages
	(Check_Overriding_Indicator): Add in parameter Is_Primitive.
	(Analyze_Function_Return): Move call to Apply_Constraint_Check before
	the implicit conversion of the expression done for anonymous access
	types. This is required to generate the code of the null excluding
	check (if required).

	* sem_warn.ads, sem_warn.adb (Check_References.Publicly_Referenceable):
	A formal parameter is never publicly referenceable outside of its body.
	(Check_References): For an unreferenced formal parameter in an accept
	statement, use the same warning circuitry as for subprogram formal
	parameters.
	(Warn_On_Unreferenced_Entity): New subprogram, taken from
	Output_Unreferenced_Messages, containing the part of that routine that
	is now reused for entry formals as described above.
	(Goto_Spec_Entity): New function
	(Check_References): Do not give IN OUT warning for dispatching operation
	Improve warnings for in out parameters
	(Test_Ref): Check that the entity is not undefinite before calling
	Scope_Within, in order to avoid infinite loops.
	Warn on return from procedure with unset out parameter
	Improved warnings for unused variables

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]