From b0e419e34dc06187c3b26dedb2d7eee3e9301a2f Mon Sep 17 00:00:00 2001 From: John McCall Date: Thu, 12 Nov 2009 00:06:05 +0000 Subject: [PATCH] Add = [ nextObject] to the -Widiomatic-parentheses category, and give that category an explicit test. Generalize the internal diagnostic name. llvm-svn: 86905 --- .../clang/Basic/DiagnosticSemaKinds.td | 5 +-- clang/lib/Sema/SemaExpr.cpp | 25 ++++++++----- clang/test/SemaObjC/idiomatic-parentheses.m | 35 +++++++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 clang/test/SemaObjC/idiomatic-parentheses.m diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 3abacd9b3acf..26693aa78cf2 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -1815,8 +1815,9 @@ def err_incomplete_object_call : Error< def warn_condition_is_assignment : Warning<"using the result of an " "assignment as a condition without parentheses">, InGroup; -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>, DefaultIgnore; def warn_value_always_zero : Warning< diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 5050dbcce8c8..9d6af7a1b892 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -6517,14 +6517,23 @@ void Sema::DiagnoseAssignmentAsCondition(Expr *E) { 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(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(Op->getRHS()->IgnoreParenCasts())) { + Selector Sel = ME->getSelector(); + + llvm::errs() << "selector is '" << Sel.getIdentifierInfoForSlot(0)->getName() << "'\n"; + + // self = [ init...] + if (isSelfExpr(Op->getLHS()) + && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) + diagnostic = diag::warn_condition_is_idiomatic_assignment; + + // = [ nextObject] + else if (Sel.isUnarySelector() && + Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") + diagnostic = diag::warn_condition_is_idiomatic_assignment; + } Loc = Op->getOperatorLoc(); } else if (isa(E)) { diff --git a/clang/test/SemaObjC/idiomatic-parentheses.m b/clang/test/SemaObjC/idiomatic-parentheses.m new file mode 100644 index 000000000000..b4c52fa0d146 --- /dev/null +++ b/clang/test/SemaObjC/idiomatic-parentheses.m @@ -0,0 +1,35 @@ +// RUN: clang-cc -fsyntax-only -verify %s + +// Don't warn about some common ObjC idioms unless we have -Wparentheses on. +// + +@interface Object +- (id) init; +- (id) initWithInt: (int) i; +- (void) iterate: (id) coll; +- (id) nextObject; +@end + +@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; +} +@end