aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPiotr Trojanek <trojanek@adacore.com>2025-01-08 14:00:50 +0100
committerMarc Poulhiès <dkm@gcc.gnu.org>2025-01-13 11:52:59 +0100
commit686bd4e0bc484f9612038d51d07708ff8a4ff75b (patch)
treeef1b631157fe4cbbd1dd86a31b3d576a8a5e43f8 /gcc
parent34943af17e3ed7963bb8b85757e24b300aa03cce (diff)
downloadgcc-686bd4e0bc484f9612038d51d07708ff8a4ff75b.zip
gcc-686bd4e0bc484f9612038d51d07708ff8a4ff75b.tar.gz
gcc-686bd4e0bc484f9612038d51d07708ff8a4ff75b.tar.bz2
ada: Warn about redundant parentheses inside unary operators
GNAT already emits a style warning when redundant parentheses appear inside logical and short-circuit operators. A similar warning is now emitted for unary operators as well. gcc/ada/ChangeLog: * par-ch4.adb (P_Factor): Warn when the operand of a unary operator doesn't require parentheses.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ada/par-ch4.adb38
1 files changed, 38 insertions, 0 deletions
diff --git a/gcc/ada/par-ch4.adb b/gcc/ada/par-ch4.adb
index 648a4cf..ca02f1b 100644
--- a/gcc/ada/par-ch4.adb
+++ b/gcc/ada/par-ch4.adb
@@ -2839,6 +2839,30 @@ package body Ch4 is
Node1 : Node_Id;
Node2 : Node_Id;
+ subtype N_Primary is Node_Kind with Static_Predicate =>
+ N_Primary in N_Aggregate
+ | N_Allocator
+ | N_Attribute_Reference
+ | N_Case_Expression -- requires single parens
+ | N_Delta_Aggregate
+ | N_Direct_Name
+ | N_Explicit_Dereference
+ | N_Expression_With_Actions -- requires single parens
+ | N_Extension_Aggregate
+ | N_If_Expression -- requires single parens
+ | N_Indexed_Component
+ | N_Null
+ | N_Numeric_Or_String_Literal
+ | N_Qualified_Expression
+ | N_Quantified_Expression -- requires single parens
+ | N_Selected_Component
+ | N_Slice
+ | N_Subprogram_Call
+ | N_Target_Name
+ | N_Type_Conversion;
+ -- Node kinds that represents a "primary" subexpression, which does not
+ -- require parentheses when used as an operand of a unary operator.
+
begin
if Token = Tok_Abs then
Node1 := New_Op_Node (N_Op_Abs, Token_Ptr);
@@ -2849,6 +2873,13 @@ package body Ch4 is
Scan; -- past ABS
Set_Right_Opnd (Node1, P_Primary);
+
+ if Style_Check then
+ if Nkind (Right_Opnd (Node1)) in N_Primary then
+ Style.Check_Xtra_Parens_Precedence (Right_Opnd (Node1));
+ end if;
+ end if;
+
return Node1;
elsif Token = Tok_Not then
@@ -2860,6 +2891,13 @@ package body Ch4 is
Scan; -- past NOT
Set_Right_Opnd (Node1, P_Primary);
+
+ if Style_Check then
+ if Nkind (Right_Opnd (Node1)) in N_Primary then
+ Style.Check_Xtra_Parens_Precedence (Right_Opnd (Node1));
+ end if;
+ end if;
+
return Node1;
else