1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Casing
; use Casing
;
28 with Csets
; use Csets
;
29 with Einfo
; use Einfo
;
30 with Einfo
.Entities
; use Einfo
.Entities
;
31 with Einfo
.Utils
; use Einfo
.Utils
;
32 with Errout
; use Errout
;
33 with Namet
; use Namet
;
34 with Nlists
; use Nlists
;
36 with Sinfo
; use Sinfo
;
37 with Sinfo
.Nodes
; use Sinfo
.Nodes
;
38 with Sinput
; use Sinput
;
39 with Snames
; use Snames
;
40 with Stylesw
; use Stylesw
;
44 -----------------------
45 -- Body_With_No_Spec --
46 -----------------------
48 -- If the check specs mode (-gnatys) is set, then all subprograms must
49 -- have specs unless they are parameterless procedures at the library
50 -- level (i.e. they are possible main programs).
52 procedure Body_With_No_Spec
(N
: Node_Id
) is
54 if Style_Check_Specs
then
55 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
57 Spec
: constant Node_Id
:= Specification
(N
);
58 Defnm
: constant Node_Id
:= Defining_Unit_Name
(Spec
);
61 if Nkind
(Spec
) = N_Procedure_Specification
62 and then Nkind
(Defnm
) = N_Defining_Identifier
63 and then No
(First_Formal
(Defnm
))
70 Error_Msg_N
("(style) subprogram body has no previous spec?s?", N
);
72 end Body_With_No_Spec
;
74 ---------------------------------
75 -- Check_Array_Attribute_Index --
76 ---------------------------------
78 procedure Check_Array_Attribute_Index
84 if Style_Check_Array_Attribute_Index
then
85 if D
= 1 and then Present
(E1
) then
86 Error_Msg_N
-- CODEFIX
87 ("(style) index number not allowed for one dimensional array?A?",
89 elsif D
> 1 and then No
(E1
) then
90 Error_Msg_N
-- CODEFIX
91 ("(style) index number required for multi-dimensional array?A?",
95 end Check_Array_Attribute_Index
;
97 ----------------------
98 -- Check_Identifier --
99 ----------------------
101 -- In check references mode (-gnatyr), identifier uses must be cased
102 -- the same way as the corresponding identifier declaration. If standard
103 -- references are checked (-gnatyn), then identifiers from Standard must
104 -- be cased as in the Reference Manual.
106 procedure Check_Identifier
107 (Ref
: Node_Or_Entity_Id
;
108 Def
: Node_Or_Entity_Id
)
110 Sref
: Source_Ptr
:= Sloc
(Ref
);
111 Sdef
: Source_Ptr
:= Sloc
(Def
);
112 Tref
: Source_Buffer_Ptr
;
113 Tdef
: Source_Buffer_Ptr
;
118 -- If reference does not come from source, nothing to check
120 if not Comes_From_Source
(Ref
) then
123 -- If previous error on either node/entity, ignore
125 elsif Error_Posted
(Ref
) or else Error_Posted
(Def
) then
128 -- Case of definition comes from source, or a record component whose
129 -- Original_Record_Component comes from source.
131 elsif Comes_From_Source
(Def
) or else
132 (Ekind
(Def
) in Record_Field_Kind
133 and then Present
(Original_Record_Component
(Def
))
134 and then Comes_From_Source
(Original_Record_Component
(Def
)))
137 -- Check same casing if we are checking references
139 if Style_Check_References
then
140 Tref
:= Source_Text
(Get_Source_File_Index
(Sref
));
141 Tdef
:= Source_Text
(Get_Source_File_Index
(Sdef
));
143 -- Ignore case of operator names. This also catches the case
144 -- where one is an operator and the other is not. This is a
145 -- phenomenon from rewriting of operators as functions, and is
148 if Tref
(Sref
) = '"' or else Tdef
(Sdef
) = '"' then
153 -- If end of identifiers, all done. Note that they are the
157 (Identifier_Char
(Tref
(Sref
)) =
158 Identifier_Char
(Tdef
(Sdef
)));
160 if not Identifier_Char
(Tref
(Sref
)) then
166 if Tref
(Sref
) /= Tdef
(Sdef
) then
167 Error_Msg_Node_1
:= Def
;
168 Error_Msg_Sloc
:= Sloc
(Def
);
170 ("(style) bad casing of & declared#?r?", Sref
, Ref
);
178 pragma Assert
(False);
182 -- Case of definition in package Standard
184 elsif Sdef
= Standard_Location
186 Sdef
= Standard_ASCII_Location
188 -- Check case of identifiers in Standard
190 if Style_Check_Standard
then
191 Tref
:= Source_Text
(Get_Source_File_Index
(Sref
));
195 if Tref
(Sref
) = '"' then
198 -- Otherwise determine required casing of Standard entity
201 -- ASCII is all upper case
203 if Chars
(Ref
) = Name_ASCII
then
204 Cas
:= All_Upper_Case
;
206 -- Special handling for names in package ASCII
208 elsif Sdef
= Standard_ASCII_Location
then
210 Nam
: constant String := Get_Name_String
(Chars
(Def
));
218 -- All names longer than 4 characters are mixed case
220 elsif Nam
'Length > 4 then
223 -- All names shorter than 4 characters (other than Bar,
224 -- which we already tested for specially) are Upper case.
227 Cas
:= All_Upper_Case
;
231 -- All other entities are in mixed case
237 Nlen
:= Length_Of_Name
(Chars
(Ref
));
239 -- Now check if we have the right casing
242 (Tref
(Sref
.. Sref
+ Source_Ptr
(Nlen
) - 1)) = Cas
246 Name_Len
:= Integer (Nlen
);
247 Name_Buffer
(1 .. Name_Len
) :=
248 String (Tref
(Sref
.. Sref
+ Source_Ptr
(Nlen
) - 1));
250 Error_Msg_Name_1
:= Name_Enter
;
251 Error_Msg_N
-- CODEFIX
252 ("(style) bad casing of %% declared in Standard?n?", Ref
);
257 end Check_Identifier
;
259 ------------------------
260 -- Missing_Overriding --
261 ------------------------
263 procedure Missing_Overriding
(N
: Node_Id
; E
: Entity_Id
) is
267 -- Perform the check on source subprograms and on subprogram instances,
268 -- because these can be primitives of untagged types. Note that such
269 -- indicators were introduced in Ada 2005. We apply Comes_From_Source
270 -- to Original_Node to catch the case of a procedure body declared with
271 -- "is null" that has been rewritten as a normal empty body.
272 -- We do not emit a warning on an inherited operation that comes from
273 -- a type derivation.
275 if Style_Check_Missing_Overriding
276 and then (Comes_From_Source
(Original_Node
(N
))
277 or else Is_Generic_Instance
(E
))
278 and then Ada_Version_Explicit
>= Ada_2005
279 and then Present
(Parent
(E
))
280 and then Nkind
(Parent
(E
)) /= N_Full_Type_Declaration
282 -- If the subprogram is an instantiation, its declaration appears
283 -- within a wrapper package that precedes the instance node. Place
284 -- warning on the node to avoid references to the original generic.
286 if Nkind
(N
) = N_Subprogram_Declaration
287 and then Is_Generic_Instance
(E
)
289 Nod
:= Next
(Parent
(Parent
(List_Containing
(N
))));
294 if Nkind
(N
) = N_Subprogram_Body
then
295 Error_Msg_NE
-- CODEFIX
296 ("(style) missing OVERRIDING indicator in body of&?O?", N
, E
);
298 elsif Nkind
(N
) = N_Abstract_Subprogram_Declaration
then
299 Error_Msg_NE
-- CODEFIX
300 ("(style) missing OVERRIDING indicator in declaration of&?O?",
301 Specification
(N
), E
);
304 Error_Msg_NE
-- CODEFIX
305 ("(style) missing OVERRIDING indicator in declaration of&?O?",
309 end Missing_Overriding
;
311 -----------------------------------
312 -- Subprogram_Not_In_Alpha_Order --
313 -----------------------------------
315 procedure Subprogram_Not_In_Alpha_Order
(Name
: Node_Id
) is
317 if Style_Check_Order_Subprograms
then
318 Error_Msg_N
-- CODEFIX
319 ("(style) subprogram body& not in alphabetical order?o?", Name
);
321 end Subprogram_Not_In_Alpha_Order
;