2009-07-22 Robert Dewar <dewar@adacore.com>
+ * sem_res.adb (Check_No_Direct_Boolean_Operators): Add check for -gnatyB
+ * style.ads, styleg.adb, styleg.ads (Check_Boolean_Operator): New
+ procedure.
+ * usage.adb, stylesw.ads, stylesw.adb: Add handling of -gnatyB switch
+ * gnat_ugn.texi: Add documentation of -gnatyB
+ * vms_data.ads: Add entry for -gnatyB (STYLE=BOOLEAN_OPERATORS)
+
+2009-07-22 Robert Dewar <dewar@adacore.com>
+
* s-stchop.adb, a-direct.adb, a-ztexio.adb, gnatchop.adb, prj-proc.adb,
make.adb, s-regpat.adb, ali-util.adb, a-ngcefu.adb, prep.adb,
s-tassta.adb, a-tifiio.adb, a-textio.adb, prj.adb, uintp.adb,
rule, together with h (no horizontal tabs), is to enforce a canonical format
for the use of blanks to separate source tokens.
+@item ^B^BOOLEAN_OPERATORS^
+@emph{Check Boolean operators.}
+The use of AND/OR operators is not permitted except in the cases of modular
+operands, array operands, and simple stand-alone boolean variables or
+boolean constants. In all other cases AND THEN/OR ELSE are required.
+
@item ^c^COMMENTS^
@emph{Check comments.}
Comments must meet the following set of rules:
Avoind raising PROGRAM_ERROR in the generated bodies of program unit stubs.
This is not always possible for function stubs.
+@item ^--no-local-header^/NO_LOCAL_HEADER^
+@cindex @option{^--no-local-header^/NO_LOCAL_HEADER^} (@command{gnatstub})
+Do not place local comment header with unit name before body stub for a
+unit.
+
@item ^-o ^/BODY=^@var{body-name}
@cindex @option{^-o^/BODY^} (@command{gnatstub})
Body file name. This should be set if the argument file name does not
procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
-- N is the node for a logical operator. If the operator is predefined, and
-- the root type of the operands is Standard.Boolean, then a check is made
- -- for restriction No_Direct_Boolean_Operators.
+ -- for restriction No_Direct_Boolean_Operators. This procedure also handles
+ -- the style check for Style_Check_Boolean_And_Or.
function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
-- Determine whether E is an access type declared by an access
Check_Restriction (No_Direct_Boolean_Operators, N);
end if;
end if;
+
+ if Style_Check then
+ Check_Boolean_Operator (N);
+ end if;
end Check_No_Direct_Boolean_Operators;
------------------------------
-- --
-- S p e c --
-- --
--- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
+-- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- designator is a reserved word (access, digits, delta or range) to allow
-- differing rules for the two cases.
+ procedure Check_Boolean_Operator (Node : Node_Id)
+ renames Style_Inst.Check_Boolean_Operator;
+ -- Called after resolving AND or OR node to check short circuit rules
+
procedure Check_Box
renames Style_Inst.Check_Box;
-- Called after scanning out a box to check spacing
-- checking rules. For documentation of these rules, see comments on the
-- individual procedures.
+with Atree; use Atree;
with Casing; use Casing;
with Csets; use Csets;
+with Einfo; use Einfo;
with Err_Vars; use Err_Vars;
with Opt; use Opt;
with Scans; use Scans;
+with Sinfo; use Sinfo;
with Sinput; use Sinput;
with Stylesw; use Stylesw;
end if;
end Check_Binary_Operator;
+ ----------------------------
+ -- Check_Boolean_Operator --
+ ----------------------------
+
+ procedure Check_Boolean_Operator (Node : Node_Id) is
+
+ function OK_Boolean_Operand (N : Node_Id) return Boolean;
+ -- Returns True for simple variable, or "not X1" or "X1 and X2" or
+ -- "X1 or X2" where X1, X2 are recursively OK_Boolean_Operand's.
+
+ ------------------------
+ -- OK_Boolean_Operand --
+ ------------------------
+
+ function OK_Boolean_Operand (N : Node_Id) return Boolean is
+ begin
+ if Nkind_In (N, N_Identifier, N_Expanded_Name) then
+ return True;
+
+ elsif Nkind (N) = N_Op_Not then
+ return OK_Boolean_Operand (Original_Node (Right_Opnd (N)));
+
+ elsif Nkind_In (N, N_Op_And, N_Op_Or) then
+ return OK_Boolean_Operand (Original_Node (Left_Opnd (N)))
+ and then
+ OK_Boolean_Operand (Original_Node (Right_Opnd (N)));
+
+ else
+ return False;
+ end if;
+ end OK_Boolean_Operand;
+
+ -- Start of processig for Check_Boolean_Operator
+ begin
+ if Style_Check_Boolean_And_Or
+ and then Comes_From_Source (Node)
+ then
+ declare
+ Orig : constant Node_Id := Original_Node (Node);
+
+ begin
+ if Nkind_In (Orig, N_Op_And, N_Op_Or) then
+ declare
+ L : constant Node_Id := Original_Node (Left_Opnd (Orig));
+ R : constant Node_Id := Original_Node (Right_Opnd (Orig));
+
+ begin
+ -- First OK case, simple boolean constants/identifiers
+
+ if OK_Boolean_Operand (L)
+ and then
+ OK_Boolean_Operand (R)
+ then
+ return;
+
+ -- Second OK case, modular types
+
+ elsif Is_Modular_Integer_Type (Etype (Node)) then
+ return;
+
+ -- Third OK case, array types
+
+ elsif Is_Array_Type (Etype (Node)) then
+ return;
+
+ -- Otherwise we have an error
+
+ elsif Nkind (Orig) = N_Op_And then
+ Error_Msg ("(style) `AND THEN` required", Sloc (Orig));
+ else
+ Error_Msg ("(style) `OR ELSE` required", Sloc (Orig));
+ end if;
+ end;
+ end if;
+ end;
+ end if;
+ end Check_Boolean_Operator;
+
---------------
-- Check_Box --
---------------
-- --
-- S p e c --
-- --
--- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
+-- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- the attribute designator is a reserved word (access, digits,
-- delta or range) to allow differing rules for the two cases.
+ procedure Check_Boolean_Operator (Node : Node_Id);
+ -- Node is a node for an AND or OR operator. Check that the usage meets
+ -- the style rules.
+
procedure Check_Box;
-- Called after scanning out a box to check spacing
-- --
-- B o d y --
-- --
--- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
+-- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
Style_Check_Attribute_Casing := False;
Style_Check_Blanks_At_End := False;
Style_Check_Blank_Lines := False;
+ Style_Check_Boolean_And_Or := False;
Style_Check_Comments := False;
Style_Check_DOS_Line_Terminator := False;
Style_Check_End_Labels := False;
Add ('a', Style_Check_Attribute_Casing);
Add ('A', Style_Check_Array_Attribute_Index);
Add ('b', Style_Check_Blanks_At_End);
+ Add ('B', Style_Check_Boolean_And_Or);
Add ('c', Style_Check_Comments);
Add ('d', Style_Check_DOS_Line_Terminator);
Add ('e', Style_Check_End_Labels);
when 'b' =>
Style_Check_Blanks_At_End := True;
+ when 'B' =>
+ Style_Check_Boolean_And_Or := True;
+
when 'c' =>
Style_Check_Comments := True;
when 'b' =>
Style_Check_Blanks_At_End := False;
+ when 'B' =>
+ Style_Check_Boolean_And_Or := False;
+
when 'c' =>
Style_Check_Comments := False;
-- multiple blank lines are not permitted, and there may not be a blank
-- line at the end of the file.
+ Style_Check_Boolean_And_Or : Boolean := False;
+ -- This can be set True by using the -gnatyB switch. If it is True, then
+ -- the use of AND THEN/OR ELSE rather than AND/OR is required except for
+ -- the following cases:
+ --
+ -- a) Both operands are simple Boolean constants or variables
+ -- b) Both operands are of a modular type
+ -- c) Both operands are of an array type
+
Style_Check_Comments : Boolean := False;
-- This can be set True by using the -gnatyc switch. If it is True, then
-- comments are style checked as follows:
Write_Line (" a check attribute casing");
Write_Line (" A check array attribute indexes");
Write_Line (" b check no blanks at end of lines");
+ Write_Line (" B check no use of AND/OR for boolean expressions");
Write_Line (" c check comment format");
Write_Line (" d check no DOS line terminators");
Write_Line (" e check end/exit labels present");
"-gnaty-A " &
"BLANKS " &
"-gnatyb " &
+ "BOOLEAN_OPERATORS " &
+ "-gnatyB " &
"NOBLANKS " &
"-gnaty-b " &
"COMMENTS " &