[cfe-commits] r86905 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/SemaObjC/idiomatic-parentheses.m
John McCall
rjmccall at apple.com
Wed Nov 11 16:06:05 PST 2009
Author: rjmccall
Date: Wed Nov 11 18:06:05 2009
New Revision: 86905
URL: http://llvm.org/viewvc/llvm-project?rev=86905&view=rev
Log:
Add <foo> = [<bar> nextObject] to the -Widiomatic-parentheses category,
and give that category an explicit test. Generalize the internal diagnostic
name.
Added:
cfe/trunk/test/SemaObjC/idiomatic-parentheses.m
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaExpr.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=86905&r1=86904&r2=86905&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Nov 11 18:06:05 2009
@@ -1815,8 +1815,9 @@
def warn_condition_is_assignment : Warning<"using the result of an "
"assignment as a condition without parentheses">,
InGroup<Parentheses>;
-def warn_condition_is_self_assignment : Warning<"using the result of an "
- "assignment to 'self' as a condition without parentheses">,
+// Completely identical except off by default.
+def warn_condition_is_idiomatic_assignment : Warning<"using the result "
+ "of an assignment as a condition without parentheses">,
InGroup<DiagGroup<"idiomatic-parentheses">>, DefaultIgnore;
def warn_value_always_zero : Warning<
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=86905&r1=86904&r2=86905&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Nov 11 18:06:05 2009
@@ -6517,14 +6517,23 @@
if (Op->getOpcode() != BinaryOperator::Assign)
return;
- // Greylist the following Cocoa ObjC idiom by putting it into a
- // warning subcategory which defaults off:
- // if (self = [super init])
- // The selector can vary, and it's possible that the base might,
- // too, so we just recognize any message call.
- if (isSelfExpr(Op->getLHS()) &&
- isa<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts()))
- diagnostic = diag::warn_condition_is_self_assignment;
+ // Greylist some idioms by putting them into a warning subcategory.
+ if (ObjCMessageExpr *ME
+ = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) {
+ Selector Sel = ME->getSelector();
+
+ llvm::errs() << "selector is '" << Sel.getIdentifierInfoForSlot(0)->getName() << "'\n";
+
+ // self = [<foo> init...]
+ if (isSelfExpr(Op->getLHS())
+ && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init"))
+ diagnostic = diag::warn_condition_is_idiomatic_assignment;
+
+ // <foo> = [<bar> nextObject]
+ else if (Sel.isUnarySelector() &&
+ Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject")
+ diagnostic = diag::warn_condition_is_idiomatic_assignment;
+ }
Loc = Op->getOperatorLoc();
} else if (isa<CXXOperatorCallExpr>(E)) {
Added: cfe/trunk/test/SemaObjC/idiomatic-parentheses.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/idiomatic-parentheses.m?rev=86905&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/idiomatic-parentheses.m (added)
+++ cfe/trunk/test/SemaObjC/idiomatic-parentheses.m Wed Nov 11 18:06:05 2009
@@ -0,0 +1,35 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+// Don't warn about some common ObjC idioms unless we have -Wparentheses on.
+// <rdar://problem/7382435>
+
+ at interface Object
+- (id) init;
+- (id) initWithInt: (int) i;
+- (void) iterate: (id) coll;
+- (id) nextObject;
+ at end
+
+ at implementation Object
+- (id) init {
+ if (self = [self init]) {
+ }
+ return self;
+}
+
+- (id) initWithInt: (int) i {
+ if (self = [self initWithInt: i]) {
+ }
+ return self;
+}
+
+- (void) iterate: (id) coll {
+ id cur;
+ while (cur = [coll nextObject]) {
+ }
+}
+
+- (id) nextObject {
+ return self;
+}
+ at end
More information about the cfe-commits
mailing list