diff mbox

[Ada] Warnings on unused formals of expression functions

Message ID 20110915102302.GA15455@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Sept. 15, 2011, 10:23 a.m. UTC
This patch simplifies the expansion of expression functions, and ensures that
proper warnings are generated for unused formals, if present.

The following compilation:

     gcc -c -gnat12 -gnatwa -gnatc ada12.ads

must yield:

     ada12.ads:6:24: warning: formal parameter "Self" is not referenced
     ada12.ads:9:35: warning: formal parameter "Self" is not referenced

--
package Ada12 is
    type T is tagged record
       Value : Integer := 17;
    end record;

    function Template (Self : T) return String is ("foo.thtml");

    type T2 is new T with null record;
    overriding function Template (Self : T2) return String is ("bar.thtml");
end Ada12;

Tested on x86_64-pc-linux-gnu, committed on trunk

2011-09-15  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch6.adb (Analyze_Expression_Function): Code cleanup:
	if the expression function is not a completion, create a
	new specification for the generated declaration, and keep the
	original specification in the generated body. Shorter code also
	ensures that proper warnings are generated for unused formals
	in all cases.
diff mbox

Patch

Index: sem_ch6.adb
===================================================================
--- sem_ch6.adb	(revision 178876)
+++ sem_ch6.adb	(working copy)
@@ -268,6 +268,7 @@ 
       Loc      : constant Source_Ptr := Sloc (N);
       LocX     : constant Source_Ptr := Sloc (Expression (N));
       Def_Id   : constant Entity_Id  := Defining_Entity (Specification (N));
+      Expr     : constant Node_Id    := Expression (N);
       New_Body : Node_Id;
       New_Decl : Node_Id;
 
@@ -315,31 +316,28 @@ 
          Set_Is_Inlined (Prev);
          Analyze (N);
 
-      --  If this is not a completion, create both a declaration and a body,
-      --  so that the expression can be inlined whenever possible.
+      --  If this is not a completion, create both a declaration and a body, so
+      --  that the expression can be inlined whenever possible. The spec of the
+      --  new subprogram declaration is a copy of the original specification,
+      --  which is now part of the subprogram body.
 
       else
          New_Decl :=
            Make_Subprogram_Declaration (Loc,
-             Specification => Specification (N));
+             Specification => Copy_Separate_Tree (Specification (N)));
          Rewrite (N, New_Decl);
          Analyze (N);
          Set_Is_Inlined (Defining_Entity (New_Decl));
 
-         --  Create new set of formals for specification in body.
-
-         Set_Specification (New_Body,
-           Make_Function_Specification (Loc,
-             Defining_Unit_Name =>
-               Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))),
-             Parameter_Specifications =>
-               Copy_Parameter_List (Defining_Entity (New_Decl)),
-             Result_Definition =>
-               New_Copy_Tree (Result_Definition (Specification (New_Decl)))));
-
          Insert_After (N, New_Body);
          Analyze (New_Body);
       end if;
+
+      --  If the return expression is a static constant, we suppress warning
+      --  messages on unused formals, which in most cases will be noise.
+
+      Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
+        Is_OK_Static_Expression (Expr));
    end Analyze_Expression_Function;
 
    ----------------------------------------