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_Boolean_Operator --
99 ----------------------------
101 procedure Check_Boolean_Operator
(Node
: Node_Id
) is
103 function OK_Boolean_Operand
(N
: Node_Id
) return Boolean;
104 -- Returns True for simple variable, or "not X1" or "X1 and X2" or
105 -- "X1 or X2" where X1, X2 are recursively OK_Boolean_Operand's.
107 ------------------------
108 -- OK_Boolean_Operand --
109 ------------------------
111 function OK_Boolean_Operand
(N
: Node_Id
) return Boolean is
113 if Nkind
(N
) in N_Identifier | N_Expanded_Name
then
116 elsif Nkind
(N
) = N_Op_Not
then
117 return OK_Boolean_Operand
(Original_Node
(Right_Opnd
(N
)));
119 elsif Nkind
(N
) in N_Op_And | N_Op_Or
then
120 return OK_Boolean_Operand
(Original_Node
(Left_Opnd
(N
)))
122 OK_Boolean_Operand
(Original_Node
(Right_Opnd
(N
)));
127 end OK_Boolean_Operand
;
129 -- Start of processing for Check_Boolean_Operator
132 if Style_Check_Boolean_And_Or
133 and then Comes_From_Source
(Node
)
136 Orig
: constant Node_Id
:= Original_Node
(Node
);
139 if Nkind
(Orig
) in N_Op_And | N_Op_Or
then
141 L
: constant Node_Id
:= Original_Node
(Left_Opnd
(Orig
));
142 R
: constant Node_Id
:= Original_Node
(Right_Opnd
(Orig
));
145 -- First OK case, simple boolean constants/identifiers
147 if OK_Boolean_Operand
(L
)
149 OK_Boolean_Operand
(R
)
153 -- Second OK case, modular types
155 elsif Is_Modular_Integer_Type
(Etype
(Node
)) then
158 -- Third OK case, array types
160 elsif Is_Array_Type
(Etype
(Node
)) then
163 -- Otherwise we have an error
165 elsif Nkind
(Orig
) = N_Op_And
then
167 ("(style) `AND THEN` required?B?", Sloc
(Orig
), Orig
);
170 ("(style) `OR ELSE` required?B?", Sloc
(Orig
), Orig
);
176 end Check_Boolean_Operator
;
178 ----------------------
179 -- Check_Identifier --
180 ----------------------
182 -- In check references mode (-gnatyr), identifier uses must be cased
183 -- the same way as the corresponding identifier declaration. If standard
184 -- references are checked (-gnatyn), then identifiers from Standard must
185 -- be cased as in the Reference Manual.
187 procedure Check_Identifier
188 (Ref
: Node_Or_Entity_Id
;
189 Def
: Node_Or_Entity_Id
)
191 Sref
: Source_Ptr
:= Sloc
(Ref
);
192 Sdef
: Source_Ptr
:= Sloc
(Def
);
193 Tref
: Source_Buffer_Ptr
;
194 Tdef
: Source_Buffer_Ptr
;
199 -- If reference does not come from source, nothing to check
201 if not Comes_From_Source
(Ref
) then
204 -- If previous error on either node/entity, ignore
206 elsif Error_Posted
(Ref
) or else Error_Posted
(Def
) then
209 -- Case of definition comes from source, or a record component whose
210 -- Original_Record_Component comes from source.
212 elsif Comes_From_Source
(Def
) or else
213 (Ekind
(Def
) in Record_Field_Kind
214 and then Present
(Original_Record_Component
(Def
))
215 and then Comes_From_Source
(Original_Record_Component
(Def
)))
218 -- Check same casing if we are checking references
220 if Style_Check_References
then
221 Tref
:= Source_Text
(Get_Source_File_Index
(Sref
));
222 Tdef
:= Source_Text
(Get_Source_File_Index
(Sdef
));
224 -- Ignore case of operator names. This also catches the case
225 -- where one is an operator and the other is not. This is a
226 -- phenomenon from rewriting of operators as functions, and is
229 if Tref
(Sref
) = '"' or else Tdef
(Sdef
) = '"' then
234 -- If end of identifiers, all done. Note that they are the
238 (Identifier_Char
(Tref
(Sref
)) =
239 Identifier_Char
(Tdef
(Sdef
)));
241 if not Identifier_Char
(Tref
(Sref
)) then
247 if Tref
(Sref
) /= Tdef
(Sdef
) then
248 Error_Msg_Node_1
:= Def
;
249 Error_Msg_Sloc
:= Sloc
(Def
);
251 ("(style) bad casing of & declared#?r?", Sref
, Ref
);
259 pragma Assert
(False);
263 -- Case of definition in package Standard
265 elsif Sdef
= Standard_Location
267 Sdef
= Standard_ASCII_Location
269 -- Check case of identifiers in Standard
271 if Style_Check_Standard
then
272 Tref
:= Source_Text
(Get_Source_File_Index
(Sref
));
276 if Tref
(Sref
) = '"' then
279 -- Otherwise determine required casing of Standard entity
282 -- ASCII is all upper case
284 if Chars
(Ref
) = Name_ASCII
then
285 Cas
:= All_Upper_Case
;
287 -- Special handling for names in package ASCII
289 elsif Sdef
= Standard_ASCII_Location
then
291 Nam
: constant String := Get_Name_String
(Chars
(Def
));
299 -- All names longer than 4 characters are mixed case
301 elsif Nam
'Length > 4 then
304 -- All names shorter than 4 characters (other than Bar,
305 -- which we already tested for specially) are Upper case.
308 Cas
:= All_Upper_Case
;
312 -- All other entities are in mixed case
318 Nlen
:= Length_Of_Name
(Chars
(Ref
));
320 -- Now check if we have the right casing
323 (Tref
(Sref
.. Sref
+ Source_Ptr
(Nlen
) - 1)) = Cas
327 Name_Len
:= Integer (Nlen
);
328 Name_Buffer
(1 .. Name_Len
) :=
329 String (Tref
(Sref
.. Sref
+ Source_Ptr
(Nlen
) - 1));
331 Error_Msg_Name_1
:= Name_Enter
;
332 Error_Msg_N
-- CODEFIX
333 ("(style) bad casing of %% declared in Standard?n?", Ref
);
338 end Check_Identifier
;
340 ----------------------------------
341 -- Check_Xtra_Parens_Precedence --
342 ----------------------------------
344 procedure Check_Xtra_Parens_Precedence
(N
: Node_Id
) is
346 if Style_Check_Xtra_Parens_Precedence
349 (if Nkind
(N
) in N_Case_Expression
350 | N_Expression_With_Actions
352 | N_Quantified_Expression
358 ("(style) redundant parentheses?z?", First_Sloc
(N
), N
);
360 end Check_Xtra_Parens_Precedence
;
362 ------------------------
363 -- Missing_Overriding --
364 ------------------------
366 procedure Missing_Overriding
(N
: Node_Id
; E
: Entity_Id
) is
370 -- Perform the check on source subprograms and on subprogram instances,
371 -- because these can be primitives of untagged types. Note that such
372 -- indicators were introduced in Ada 2005. We apply Comes_From_Source
373 -- to Original_Node to catch the case of a procedure body declared with
374 -- "is null" that has been rewritten as a normal empty body.
375 -- We do not emit a warning on an inherited operation that comes from
376 -- a type derivation.
378 if Style_Check_Missing_Overriding
379 and then (Comes_From_Source
(Original_Node
(N
))
380 or else Is_Generic_Instance
(E
))
381 and then Ada_Version_Explicit
>= Ada_2005
382 and then Present
(Parent
(E
))
383 and then Nkind
(Parent
(E
)) /= N_Full_Type_Declaration
385 -- If the subprogram is an instantiation, its declaration appears
386 -- within a wrapper package that precedes the instance node. Place
387 -- warning on the node to avoid references to the original generic.
389 if Nkind
(N
) = N_Subprogram_Declaration
390 and then Is_Generic_Instance
(E
)
392 Nod
:= Next
(Parent
(Parent
(List_Containing
(N
))));
397 if Nkind
(N
) = N_Subprogram_Body
then
398 Error_Msg_NE
-- CODEFIX
399 ("(style) missing OVERRIDING indicator in body of&?O?", N
, E
);
401 elsif Nkind
(N
) = N_Abstract_Subprogram_Declaration
then
402 Error_Msg_NE
-- CODEFIX
403 ("(style) missing OVERRIDING indicator in declaration of&?O?",
404 Specification
(N
), E
);
407 Error_Msg_NE
-- CODEFIX
408 ("(style) missing OVERRIDING indicator in declaration of&?O?",
412 end Missing_Overriding
;
414 -----------------------------------
415 -- Subprogram_Not_In_Alpha_Order --
416 -----------------------------------
418 procedure Subprogram_Not_In_Alpha_Order
(Name
: Node_Id
) is
420 if Style_Check_Order_Subprograms
then
421 Error_Msg_N
-- CODEFIX
422 ("(style) subprogram body& not in alphabetical order?o?", Name
);
424 end Subprogram_Not_In_Alpha_Order
;