[cfe-commits] r119381 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/parentheses.c

Argyrios Kyrtzidis akyrtzi at gmail.com
Tue Nov 16 13:00:12 PST 2010


Author: akirtzidis
Date: Tue Nov 16 15:00:12 2010
New Revision: 119381

URL: http://llvm.org/viewvc/llvm-project?rev=119381&view=rev
Log:
Warn about arg1 && arg2 || arg3, as GCC 4.3+ does. Fixes rdar://8659922

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/parentheses.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=119381&r1=119380&r2=119381&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Nov 16 15:00:12 2010
@@ -2156,7 +2156,12 @@
 def warn_logical_instead_of_bitwise : Warning<
   "use of logical %0 with constant operand; switch to bitwise %1 or "
   "remove constant">, InGroup<DiagGroup<"constant-logical-operand">>;
-  
+
+def warn_logical_and_in_logical_or : Warning<
+  "'&&' within '||'">, InGroup<Parentheses>;
+def note_logical_and_in_logical_or_silence : Note<
+  "place parentheses around the '&&' expression to silence this warning">;
+
 def err_sizeof_nonfragile_interface : Error<
   "invalid application of '%select{alignof|sizeof}1' to interface %0 in "
   "non-fragile ABI">;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=119381&r1=119380&r2=119381&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Nov 16 15:00:12 2010
@@ -7329,13 +7329,33 @@
                        rhs->getSourceRange());
 }
 
+static void DiagnoseLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc,
+                                          Expr *E) {
+  if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(E)) {
+    if (Bop->getOpcode() == BO_LAnd) {
+      SuggestParentheses(Self, OpLoc,
+        Self.PDiag(diag::warn_logical_and_in_logical_or)
+            << E->getSourceRange(),
+        Self.PDiag(diag::note_logical_and_in_logical_or_silence),
+        E->getSourceRange(),
+        Self.PDiag(0), SourceRange());
+    }
+  }
+}
+
 /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky
-/// precedence. This currently diagnoses only "arg1 'bitwise' arg2 'eq' arg3".
-/// But it could also warn about arg1 && arg2 || arg3, as GCC 4.3+ does.
+/// precedence.
 static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc,
                                     SourceLocation OpLoc, Expr *lhs, Expr *rhs){
+  // Diagnose "arg1 'bitwise' arg2 'eq' arg3".
   if (BinaryOperator::isBitwiseOp(Opc))
-    DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs);
+    return DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs);
+
+  /// Warn about arg1 && arg2 || arg3, as GCC 4.3+ does.
+  if (Opc == BO_LOr) {
+    DiagnoseLogicalAndInLogicalOr(Self, OpLoc, lhs);
+    DiagnoseLogicalAndInLogicalOr(Self, OpLoc, rhs);
+  }
 }
 
 // Binary Operators.  'Tok' is the token for the operator.

Modified: cfe/trunk/test/Sema/parentheses.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/parentheses.c?rev=119381&r1=119380&r2=119381&view=diff
==============================================================================
--- cfe/trunk/test/Sema/parentheses.c (original)
+++ cfe/trunk/test/Sema/parentheses.c Tue Nov 16 15:00:12 2010
@@ -25,4 +25,7 @@
   // Eager logical op
   (void)(i == 1 | i == 2 | i == 3);
   (void)(i != 1 & i != 2 & i != 3);
+
+  (void)(i || i && i); // expected-warning {{'&&' within '||'}} \
+                       // expected-note {{place parentheses around the '&&' expression to silence this warning}}
 }





More information about the cfe-commits mailing list