[Ada] Warning needed on anonymous access type allocators
This patch enhances the compiler to add an optional warning for
allocators of anonymous access types due to the somewhat confusing
runtime accessibility checks that they generate. For more details see RM
3.10.2 sections 14/3, 14.1/3, and 14.2/3.
These warnings can now be enabled with -gnatw_a, disabled with -gnatw_A
and are part of the "all warnings" flag -gnatwa.
------------
-- Source --
------------
-- main.adb
procedure Main is
type Int_Ptr is access all Integer;
Ptr_Obj : Int_Ptr;
Ptr_Not_Null_Obj : not null Int_Ptr := new Integer;
Ptr_Anon_Acc_Obj : not null access Integer :=
new Integer; -- WARNING
procedure Update_Ptr (Item : access Integer) is
begin
Ptr_Obj := Int_Ptr (Item); -- RUNTIME ERROR
end;
procedure Update_Ptr_With_Anonymous_Allocator is
Item : access Integer := new Integer;
begin
Update_Ptr (Item);
end;
type Rec_With_Coextension_A (Disc : access Integer)
is null record;
type Rec_With_Coextension_B (Disc : access Integer) is record
Comp : Integer;
end record;
Obj : Rec_With_Coextension_A :=
(Disc => new Integer'(32)); -- WARNING
procedure Test_Param (Param : access Integer) is
begin
null;
end;
function Test_Simple_Return return access Integer is
begin
return new Integer; -- WARNING
end;
function Test_Coextension_Return_A return Rec_With_Coextension_A is
begin
return (Disc => new Integer); -- WARNING
end;
function Test_Coextension_Return_B return Rec_With_Coextension_B is
begin
return (new Integer, 32); -- WARNING
end;
begin
Test_Param (new Integer); -- WARNING
Test_Param (Param => new Integer); -- WARNING
Update_Ptr_With_Anonymous_Allocator;
end;
-----------------
-- Compilation --
-----------------
$ gnatmake -q -gnatw_a main.adb
$ rm *.ali
$ gnatmake -q -gnatwa -gnatw_A main.adb
$ rm *.ali
$ gnatmake -q -gnatwa main.adb
$ main
main.adb:8:06: warning: use of an anonymous access type allocator
main.adb:16:32: warning: use of an anonymous access type allocator
main.adb:29:15: warning: use of an anonymous access type allocator
main.adb:38:14: warning: use of an anonymous access type allocator
main.adb:43:23: warning: coextension will not be deallocated when
its associated owner is deallocated
main.adb:43:23: warning: use of an anonymous access type allocator
main.adb:48:15: warning: coextension will not be deallocated when
its associated owner is deallocated
main.adb:48:15: warning: use of an anonymous access type allocator
main.adb:52:16: warning: use of an anonymous access type allocator
main.adb:53:25: warning: use of an anonymous access type allocator
main.adb:5:04: warning: variable "Ptr_Obj" is assigned but never read
main.adb:6:04: warning: variable "Ptr_Not_Null_Obj" is not referenced
main.adb:7:04: warning: variable "Ptr_Anon_Acc_Obj" is not referenced
main.adb:16:07: warning: "Item" is not modified, could be declared constant
main.adb:28:04: warning: variable "Obj" is not referenced
main.adb:36:13: warning: function "Test_Simple_Return" is not referenced
main.adb:41:13: warning: function "Test_Coextension_Return_A" is not referenced
main.adb:43:23: warning: coextension will not be deallocated when its
associated owner is deallocated
main.adb:46:13: warning: function "Test_Coextension_Return_B" is not referenced
main.adb:48:15: warning: coextension will not be deallocated when its
associated owner is deallocated
main.adb:5:04: warning: variable "Ptr_Obj" is assigned but never read
main.adb:6:04: warning: variable "Ptr_Not_Null_Obj" is not referenced
main.adb:7:04: warning: variable "Ptr_Anon_Acc_Obj" is not referenced
main.adb:8:06: warning: use of an anonymous access type allocator
main.adb:16:07: warning: "Item" is not modified, could be declared constant
main.adb:16:32: warning: use of an anonymous access type allocator
main.adb:28:04: warning: variable "Obj" is not referenced
main.adb:29:15: warning: use of an anonymous access type allocator
main.adb:36:13: warning: function "Test_Simple_Return" is not referenced
main.adb:38:14: warning: use of an anonymous access type allocator
main.adb:41:13: warning: function "Test_Coextension_Return_A" is not referenced
main.adb:43:23: warning: coextension will not be deallocated when its
associated owner is deallocated
main.adb:43:23: warning: use of an anonymous access type allocator
main.adb:46:13: warning: function "Test_Coextension_Return_B" is not referenced
main.adb:48:15: warning: coextension will not be deallocated when its
associated owner is deallocated
main.adb:48:15: warning: use of an anonymous access type allocator
main.adb:52:16: warning: use of an anonymous access type allocator
main.adb:53:25: warning: use of an anonymous access type allocator
raised PROGRAM_ERROR : main.adb:12 accessibility check failed
2019-07-09 Justin Squirek <squirek@adacore.com>
gcc/ada/
* exp_ch4.adb (Expand_N_Allocator): Add conditional to detect
the presence of anoymous access type allocators and issue a
warning if the appropriate warning flag is enabled.
* warnsw.ads: Add new warning flag for anonymous allocators
* warnsw.adb (All_Warnings, Restore_Warnings, Save_Warnings,
Set_Underscore_Warning_Switch): Register new flags.
(WA_Warnings): Register new flag as an "all warnings" switch
* usage.adb,
doc/gnat_ugn/building_executable_programs_with_gnat.rst:
Document new warning switches -gnatw_a and -gnatw_A.
* gnat_ugn.texi: Regenerate.
From-SVN: r273290