[cfe-commits] r86790 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/SemaObjC/call-super-2.m

John McCall rjmccall at apple.com
Tue Nov 10 18:41:58 PST 2009


Author: rjmccall
Date: Tue Nov 10 20:41:58 2009
New Revision: 86790

URL: http://llvm.org/viewvc/llvm-project?rev=86790&view=rev
Log:
Apparently the following idiom is specifically encouraged:
  if (self = [super init])
Recognize it and only warn if -Wparentheses is explicitly enabled.



Modified:
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaObjC/call-super-2.m

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=86790&r1=86789&r2=86790&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Nov 10 20:41:58 2009
@@ -40,6 +40,7 @@
 def FormatZeroLength : DiagGroup<"format-zero-length">;
 
 def FourByteMultiChar : DiagGroup<"four-char-constants">;
+def : DiagGroup<"idiomatic-parentheses">;
 def : DiagGroup<"import">;
 def : DiagGroup<"init-self">;
 def : DiagGroup<"inline">;
@@ -63,7 +64,6 @@
 def : DiagGroup<"overflow">;
 def : DiagGroup<"overloaded-virtual">;
 def : DiagGroup<"packed">;
-def Parentheses : DiagGroup<"parentheses">;
 def PointerArith : DiagGroup<"pointer-arith">;
 def : DiagGroup<"pointer-to-int-cast">;
 def : DiagGroup<"redundant-decls">;
@@ -117,6 +117,10 @@
 
 // Aggregation warning settings.
 
+// -Widiomatic-parentheses contains warnings about 'idiomatic'
+// missing parentheses;  it is off by default.
+def Parentheses : DiagGroup<"parentheses", [DiagGroup<"idiomatic-parentheses">]>;
+
 // -Wconversion has its own warnings, but we split this one out for
 // legacy reasons.
 def Conversion : DiagGroup<"conversion",

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=86790&r1=86789&r2=86790&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Nov 10 20:41:58 2009
@@ -1806,6 +1806,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">,
+  InGroup<DiagGroup<"idiomatic-parentheses">>, DefaultIgnore;
 
 def warn_value_always_zero : Warning<
   "%0 is always %select{zero|false|NULL}1 in this context">;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=86790&r1=86789&r2=86790&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Nov 10 20:41:58 2009
@@ -6510,11 +6510,22 @@
 void Sema::DiagnoseAssignmentAsCondition(Expr *E) {
   SourceLocation Loc;
 
+  unsigned diagnostic = diag::warn_condition_is_assignment;
+
   if (isa<BinaryOperator>(E)) {
     BinaryOperator *Op = cast<BinaryOperator>(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<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts()))
+      diagnostic = diag::warn_condition_is_self_assignment;
+
     Loc = Op->getOperatorLoc();
   } else if (isa<CXXOperatorCallExpr>(E)) {
     CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E);
@@ -6530,7 +6541,7 @@
   SourceLocation Open = E->getSourceRange().getBegin();
   SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd());
   
-  Diag(Loc, diag::warn_condition_is_assignment)
+  Diag(Loc, diagnostic)
     << E->getSourceRange()
     << CodeModificationHint::CreateInsertion(Open, "(")
     << CodeModificationHint::CreateInsertion(Close, ")");

Modified: cfe/trunk/test/SemaObjC/call-super-2.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/call-super-2.m?rev=86790&r1=86789&r2=86790&view=diff

==============================================================================
--- cfe/trunk/test/SemaObjC/call-super-2.m (original)
+++ cfe/trunk/test/SemaObjC/call-super-2.m Tue Nov 10 20:41:58 2009
@@ -6,6 +6,7 @@
 id objc_getClass(const char *s);
 
 @interface Object 
+- (id) initWithInt: (int) i;
 @end
 
 @protocol Func
@@ -28,6 +29,7 @@
 - (int) instance_func5;
 - (int) instance_func6;
 - (int) instance_func7;
+- (id) initWithInt: (int) i;
 @end
 
 @implementation Derived
@@ -94,5 +96,13 @@
 {
    return [objc_getClass("Derived") class_func1];
 }
+- (id) initWithInt: (int) i
+{
+   // Don't warn about parentheses here.
+   if (self = [super initWithInt: i]) {
+     [self instance_func1];
+   }
+   return self;
+}
 @end
 





More information about the cfe-commits mailing list