| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- W A R N S W -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1999-2025, 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 |
|
| 26 |
with Opt; use Opt; |
| 27 |
with Output; use Output; |
| 28 |
|
| 29 |
with System.Case_Util; use System.Case_Util; |
| 30 |
|
| 31 |
package body Warnsw is |
| 32 |
|
| 33 |
subtype Lowercase is Character range 'a' .. 'z'; |
| 34 |
-- Warning-enable switches are lowercase letters |
| 35 |
|
| 36 |
Switch_To_Flag_Mapping : constant array (Warning_Family, Lowercase) of |
| 37 |
-- Mapping from the letter after "-gnatw", "-gnatw." or "-gnatw_" to |
| 38 |
-- the corresponding flag for the warning it enables. Special_Case means |
| 39 |
-- Set_Warning_Switch must do something special, as opposed to simply |
| 40 |
-- setting the corresponding flag. No_Such_Warning means the letter |
| 41 |
-- is not a defined warning switch, which is an error. |
| 42 |
X.Opt_Warnings_Enum := |
| 43 |
(Plain => |
| 44 |
('a' | 'e' | 'n' | 's' | 'u' | 'y' => Special_Case, |
| 45 |
|
| 46 |
'b' => X.Warn_On_Bad_Fixed_Value, |
| 47 |
'c' => X.Constant_Condition_Warnings, |
| 48 |
'd' => X.Warn_On_Dereference, |
| 49 |
'f' => X.Check_Unreferenced_Formals, |
| 50 |
'g' => X.Warn_On_Unrecognized_Pragma, |
| 51 |
'h' => X.Warn_On_Hiding, |
| 52 |
'i' => X.Implementation_Unit_Warnings, |
| 53 |
'j' => X.Warn_On_Obsolescent_Feature, |
| 54 |
'k' => X.Warn_On_Constant, |
| 55 |
'l' => X.Elab_Warnings, |
| 56 |
'm' => X.Warn_On_Modified_Unread, |
| 57 |
'o' => X.Address_Clause_Overlay_Warnings, |
| 58 |
'p' => X.Ineffective_Inline_Warnings, |
| 59 |
'q' => X.Warn_On_Questionable_Missing_Parens, |
| 60 |
'r' => X.Warn_On_Redundant_Constructs, |
| 61 |
't' => X.Warn_On_Deleted_Code, |
| 62 |
'v' => X.Warn_On_No_Value_Assigned, |
| 63 |
'w' => X.Warn_On_Assumed_Low_Bound, |
| 64 |
'x' => X.Warn_On_Export_Import, |
| 65 |
'z' => X.Warn_On_Unchecked_Conversion), |
| 66 |
|
| 67 |
'.' => |
| 68 |
('e' | 'g' | 'x' => Special_Case, |
| 69 |
|
| 70 |
'a' => X.Warn_On_Assertion_Failure, |
| 71 |
'b' => X.Warn_On_Biased_Representation, |
| 72 |
'c' => X.Warn_On_Unrepped_Components, |
| 73 |
'd' => X.Warning_Doc_Switch, |
| 74 |
'f' => X.Warn_On_Elab_Access, |
| 75 |
'h' => X.Warn_On_Record_Holes, |
| 76 |
'i' => X.Warn_On_Overlap, |
| 77 |
'j' => X.Warn_On_Late_Primitives, |
| 78 |
'k' => X.Warn_On_Standard_Redefinition, |
| 79 |
'l' => X.List_Inherited_Aspects, |
| 80 |
'm' => X.Warn_On_Suspicious_Modulus_Value, |
| 81 |
'n' => X.Warn_On_Atomic_Synchronization, |
| 82 |
'o' => X.Warn_On_All_Unread_Out_Parameters, |
| 83 |
'p' => X.Warn_On_Parameter_Order, |
| 84 |
'q' => X.Warn_On_Questionable_Layout, |
| 85 |
'r' => X.Warn_On_Object_Renames_Function, |
| 86 |
's' => X.Warn_On_Overridden_Size, |
| 87 |
't' => X.Warn_On_Suspicious_Contract, |
| 88 |
'u' => X.Warn_On_Unordered_Enumeration_Type, |
| 89 |
'v' => X.Warn_On_Reverse_Bit_Order, |
| 90 |
'w' => X.Warn_On_Warnings_Off, |
| 91 |
'y' => X.List_Body_Required_Info, |
| 92 |
'z' => X.Warn_On_Size_Alignment), |
| 93 |
|
| 94 |
'_' => |
| 95 |
('b' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'k' | 'm' | |
| 96 |
'n' | 'o' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' => |
| 97 |
No_Such_Warning, |
| 98 |
|
| 99 |
'a' => X.Warn_On_Anonymous_Allocators, |
| 100 |
'c' => X.Warn_On_Unknown_Compile_Time_Warning, |
| 101 |
'j' => X.Warn_On_Non_Dispatching_Primitives, |
| 102 |
'l' => X.Warn_On_Inherently_Limited_Type, |
| 103 |
'p' => X.Warn_On_Pedantic_Checks, |
| 104 |
'q' => X.Warn_On_Ignored_Equality, |
| 105 |
'r' => X.Warn_On_Component_Order, |
| 106 |
's' => X.Warn_On_Ineffective_Predicate_Test)); |
| 107 |
|
| 108 |
All_Warnings : constant Warnings_State := -- Warnings set by -gnatw.e |
| 109 |
(X.Elab_Info_Messages | |
| 110 |
X.Warning_Doc_Switch | |
| 111 |
X.Warn_On_Ada_2022_Compatibility | |
| 112 |
X.Warn_On_Elab_Access | |
| 113 |
X.Warn_On_GNAT_Extension_Compatibility | |
| 114 |
X.No_Warn_On_Non_Local_Exception => False, |
| 115 |
others => True); |
| 116 |
-- Warning_Doc_Switch is not really a warning to be enabled, but controls |
| 117 |
-- the form of warnings printed. No_Warn_On_Non_Local_Exception is handled |
| 118 |
-- specially (see Warn_On_Non_Local_Exception). The others are not part of |
| 119 |
-- -gnatw.e for historical reasons. |
| 120 |
|
| 121 |
WA_Warnings : constant Warnings_State := -- Warnings set by -gnatwa |
| 122 |
(X.Check_Unreferenced | -- -gnatwf/-gnatwu |
| 123 |
X.Check_Unreferenced_Formals | -- -gnatwf/-gnatwu |
| 124 |
X.Check_Withs | -- -gnatwu |
| 125 |
X.Constant_Condition_Warnings | -- -gnatwc |
| 126 |
X.Implementation_Unit_Warnings | -- -gnatwi |
| 127 |
X.Ineffective_Inline_Warnings | -- -gnatwp |
| 128 |
X.Warn_On_Ada_2005_Compatibility | -- -gnatwy |
| 129 |
X.Warn_On_Ada_2012_Compatibility | -- -gnatwy |
| 130 |
X.Warn_On_Anonymous_Allocators | -- -gnatw_a |
| 131 |
X.Warn_On_Assertion_Failure | -- -gnatw.a |
| 132 |
X.Warn_On_Assumed_Low_Bound | -- -gnatww |
| 133 |
X.Warn_On_Bad_Fixed_Value | -- -gnatwb |
| 134 |
X.Warn_On_Biased_Representation | -- -gnatw.b |
| 135 |
X.Warn_On_Constant | -- -gnatwk |
| 136 |
X.Warn_On_Export_Import | -- -gnatwx |
| 137 |
X.Warn_On_Ineffective_Predicate_Test | -- -gnatw_s |
| 138 |
X.Warn_On_Late_Primitives | -- -gnatw.j |
| 139 |
X.Warn_On_Modified_Unread | -- -gnatwm |
| 140 |
X.Warn_On_No_Value_Assigned | -- -gnatwv |
| 141 |
X.Warn_On_Non_Local_Exception | -- -gnatw.x |
| 142 |
X.Warn_On_Object_Renames_Function | -- -gnatw.r |
| 143 |
X.Warn_On_Obsolescent_Feature | -- -gnatwj |
| 144 |
X.Warn_On_Overlap | -- -gnatw.i |
| 145 |
X.Warn_On_Parameter_Order | -- -gnatw.p |
| 146 |
X.Warn_On_Questionable_Missing_Parens | -- -gnatwq |
| 147 |
X.Warn_On_Redundant_Constructs | -- -gnatwr |
| 148 |
X.Warn_On_Reverse_Bit_Order | -- -gnatw.v |
| 149 |
X.Warn_On_Size_Alignment | -- -gnatw.z |
| 150 |
X.Warn_On_Suspicious_Contract | -- -gnatw.t |
| 151 |
X.Warn_On_Suspicious_Modulus_Value | -- -gnatw.m |
| 152 |
X.Warn_On_Unchecked_Conversion | -- -gnatwz |
| 153 |
X.Warn_On_Unrecognized_Pragma | -- -gnatwg |
| 154 |
X.Warn_On_Unrepped_Components => -- -gnatw.c |
| 155 |
True, |
| 156 |
|
| 157 |
others => False); |
| 158 |
|
| 159 |
---------------------- |
| 160 |
-- Restore_Warnings -- |
| 161 |
---------------------- |
| 162 |
|
| 163 |
procedure Restore_Warnings (W : Warnings_State) is |
| 164 |
begin |
| 165 |
Warning_Flags := W; |
| 166 |
end Restore_Warnings; |
| 167 |
|
| 168 |
------------------- |
| 169 |
-- Save_Warnings -- |
| 170 |
------------------- |
| 171 |
|
| 172 |
function Save_Warnings return Warnings_State is |
| 173 |
begin |
| 174 |
return Warning_Flags; |
| 175 |
end Save_Warnings; |
| 176 |
|
| 177 |
---------------------------- |
| 178 |
-- Set_GNAT_Mode_Warnings -- |
| 179 |
---------------------------- |
| 180 |
|
| 181 |
procedure Set_GNAT_Mode_Warnings is |
| 182 |
begin |
| 183 |
-- Set -gnatwa warnings and no others |
| 184 |
|
| 185 |
Warning_Flags := (Warning_Flags and not All_Warnings) or WA_Warnings; |
| 186 |
|
| 187 |
-- These warnings are added to the -gnatwa set |
| 188 |
|
| 189 |
Address_Clause_Overlay_Warnings := True; |
| 190 |
Warn_On_Questionable_Layout := True; |
| 191 |
Warn_On_Overridden_Size := True; |
| 192 |
|
| 193 |
-- These warnings are removed from the -gnatwa set |
| 194 |
|
| 195 |
Implementation_Unit_Warnings := False; |
| 196 |
Warn_On_Non_Dispatching_Primitives := False; |
| 197 |
Warn_On_Non_Local_Exception := False; |
| 198 |
No_Warn_On_Non_Local_Exception := True; |
| 199 |
Warn_On_Reverse_Bit_Order := False; |
| 200 |
Warn_On_Size_Alignment := False; |
| 201 |
Warn_On_Unrepped_Components := False; |
| 202 |
end Set_GNAT_Mode_Warnings; |
| 203 |
|
| 204 |
------------------------ |
| 205 |
-- Set_Warning_Switch -- |
| 206 |
------------------------ |
| 207 |
|
| 208 |
function Set_Warning_Switch |
| 209 |
(Family : Warning_Family; C : Character) return Boolean |
| 210 |
is |
| 211 |
L : constant Character := To_Lower (C); |
| 212 |
begin |
| 213 |
-- Error case |
| 214 |
|
| 215 |
if L not in Lowercase |
| 216 |
or else Switch_To_Flag_Mapping (Family, L) = No_Such_Warning |
| 217 |
then |
| 218 |
if Ignore_Unrecognized_VWY_Switches then |
| 219 |
declare |
| 220 |
Family_Switch : constant String := |
| 221 |
(case Family is |
| 222 |
when Plain => "", when '.' => ".", when '_' => "_"); |
| 223 |
begin |
| 224 |
Write_Line |
| 225 |
("unrecognized switch -gnatw" & Family_Switch & C & |
| 226 |
" ignored"); |
| 227 |
end; |
| 228 |
return True; |
| 229 |
else |
| 230 |
return False; |
| 231 |
end if; |
| 232 |
end if; |
| 233 |
|
| 234 |
-- Special cases that don't fall into the normal pattern below |
| 235 |
|
| 236 |
if Switch_To_Flag_Mapping (Family, L) = Special_Case then |
| 237 |
case Family is |
| 238 |
when Plain => |
| 239 |
case C is |
| 240 |
when 'a' => |
| 241 |
-- "or" in the -gnatwa flags, possibly leaving others set |
| 242 |
Warning_Flags := Warning_Flags or WA_Warnings; |
| 243 |
|
| 244 |
when 'A' => |
| 245 |
-- Turn off the All_Warnings flags, except that |
| 246 |
-- No_Warn_On_Non_Local_Exception is a special case. |
| 247 |
Warning_Flags := Warning_Flags and not All_Warnings; |
| 248 |
No_Warn_On_Non_Local_Exception := True; |
| 249 |
|
| 250 |
when 'e' => |
| 251 |
Warning_Mode := Treat_As_Error; |
| 252 |
|
| 253 |
when 'E' => |
| 254 |
Warning_Mode := Treat_Run_Time_Warnings_As_Errors; |
| 255 |
|
| 256 |
when 'n' => |
| 257 |
Warning_Mode := Normal; |
| 258 |
|
| 259 |
when 's' => |
| 260 |
Warning_Mode := Suppress; |
| 261 |
|
| 262 |
when 'u' => |
| 263 |
Check_Unreferenced := True; |
| 264 |
Check_Withs := True; |
| 265 |
Check_Unreferenced_Formals := True; |
| 266 |
|
| 267 |
when 'U' => |
| 268 |
Check_Unreferenced := False; |
| 269 |
Check_Withs := False; |
| 270 |
Check_Unreferenced_Formals := False; |
| 271 |
|
| 272 |
when 'y' => |
| 273 |
Warn_On_Ada_2005_Compatibility := True; |
| 274 |
Warn_On_Ada_2012_Compatibility := True; |
| 275 |
|
| 276 |
when 'Y' => |
| 277 |
Warn_On_Ada_2005_Compatibility := False; |
| 278 |
Warn_On_Ada_2012_Compatibility := False; |
| 279 |
|
| 280 |
when others => raise Program_Error; |
| 281 |
end case; |
| 282 |
|
| 283 |
when '.' => |
| 284 |
case C is |
| 285 |
when 'e' => |
| 286 |
-- "or" in the All_Warnings flags |
| 287 |
Warning_Flags := Warning_Flags or All_Warnings; |
| 288 |
when 'g' => |
| 289 |
Set_GNAT_Mode_Warnings; |
| 290 |
|
| 291 |
when 'x' => |
| 292 |
Warn_On_Non_Local_Exception := True; |
| 293 |
|
| 294 |
when 'X' => |
| 295 |
Warn_On_Non_Local_Exception := False; |
| 296 |
No_Warn_On_Non_Local_Exception := True; |
| 297 |
|
| 298 |
when others => raise Program_Error; |
| 299 |
end case; |
| 300 |
|
| 301 |
when '_' => |
| 302 |
raise Program_Error; |
| 303 |
end case; |
| 304 |
|
| 305 |
return True; |
| 306 |
end if; |
| 307 |
|
| 308 |
-- Normal pattern (lower case enables the warning, upper case disables |
| 309 |
-- the warning). |
| 310 |
|
| 311 |
if C in Lowercase then |
| 312 |
Warning_Flags (Switch_To_Flag_Mapping (Family, C)) := True; |
| 313 |
elsif L in Lowercase then |
| 314 |
Warning_Flags (Switch_To_Flag_Mapping (Family, L)) := False; |
| 315 |
else |
| 316 |
raise Program_Error; |
| 317 |
end if; |
| 318 |
|
| 319 |
return True; |
| 320 |
end Set_Warning_Switch; |
| 321 |
|
| 322 |
end Warnsw; |