[cfe-commits] r55131 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/SemaExpr.cpp test/Sema/exprs.c
Chris Lattner
sabre at nondot.org
Thu Aug 21 11:04:14 PDT 2008
Author: lattner
Date: Thu Aug 21 13:04:13 2008
New Revision: 55131
URL: http://llvm.org/viewvc/llvm-project?rev=55131&view=rev
Log:
add a simple check to warn people who type "=+" when they probably meant
"+=".
Modified:
cfe/trunk/include/clang/Basic/DiagnosticKinds.def
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/Sema/exprs.c
Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=55131&r1=55130&r2=55131&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Thu Aug 21 13:04:13 2008
@@ -1045,6 +1045,9 @@
DIAG(err_incomplete_base_class, ERROR,
"base class has incomplete type")
+DIAG(warn_not_compound_assign, WARNING,
+ "use of unary operator that may be intended as compound assignment (%0=)")
+
// CHECK: printf format string errors
DIAG(warn_printf_not_string_constant, WARNING,
"format string is not a string literal (potentially insecure)")
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=55131&r1=55130&r2=55131&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Aug 21 13:04:13 2008
@@ -1901,10 +1901,30 @@
}
AssignConvertType ConvTy;
- if (compoundType.isNull())
+ if (compoundType.isNull()) {
+ // Simple assignment "x = y".
ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
- else
+
+ // If the RHS is a unary plus or minus, check to see if they = and + are
+ // right next to each other. If so, the user may have typo'd "x =+ 4"
+ // instead of "x += 4".
+ Expr *RHSCheck = rex;
+ if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck))
+ RHSCheck = ICE->getSubExpr();
+ if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) {
+ if ((UO->getOpcode() == UnaryOperator::Plus ||
+ UO->getOpcode() == UnaryOperator::Minus) &&
+ loc.isFileID() && UO->getOperatorLoc().isFileID() &&
+ // Only if the two operators are exactly adjacent.
+ loc.getFileLocWithOffset(1) == UO->getOperatorLoc())
+ Diag(loc, diag::warn_not_compound_assign,
+ UO->getOpcode() == UnaryOperator::Plus ? "+" : "-",
+ SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()));
+ }
+ } else {
+ // Compound assignment "x += y"
ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
+ }
if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
rex, "assigning"))
Modified: cfe/trunk/test/Sema/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/exprs.c?rev=55131&r1=55130&r2=55131&view=diff
==============================================================================
--- cfe/trunk/test/Sema/exprs.c (original)
+++ cfe/trunk/test/Sema/exprs.c Thu Aug 21 13:04:13 2008
@@ -15,3 +15,12 @@
(__extension__ x) = 10;
}
+// rdar://6162726
+void test4() {
+ static int var;
+ var =+ 5; // expected-warning {{use of unary operator that may be intended as compound assignment (+=)}}
+ var =- 5; // expected-warning {{use of unary operator that may be intended as compound assignment (-=)}}
+ var = +5;
+ var = -5;
+}
+
More information about the cfe-commits
mailing list