r216271 - Objective-C. Warn if user has made explicit call

Fariborz Jahanian fjahanian at apple.com
Fri Aug 22 09:57:26 PDT 2014


Author: fjahanian
Date: Fri Aug 22 11:57:26 2014
New Revision: 216271

URL: http://llvm.org/viewvc/llvm-project?rev=216271&view=rev
Log:
Objective-C. Warn if user has made explicit call
to +initilize as this results in an extra call
to this method. rdar://16628028

Added:
    cfe/trunk/test/SemaObjC/warn-explicit-call-initialize.m
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/include/clang/Basic/IdentifierTable.h
    cfe/trunk/lib/AST/DeclObjC.cpp
    cfe/trunk/lib/Basic/IdentifierTable.cpp
    cfe/trunk/lib/Sema/SemaDeclObjC.cpp
    cfe/trunk/lib/Sema/SemaExprObjC.cpp
    cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=216271&r1=216270&r2=216271&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Aug 22 11:57:26 2014
@@ -6874,6 +6874,9 @@ def err_invalid_protocol_qualifiers : Er
 def warn_ivar_use_hidden : Warning<
   "local declaration of %0 hides instance variable">,
    InGroup<DiagGroup<"shadow-ivar">>;
+def warn_direct_initialize_call : Warning<
+  "explicit call to +initialize results in duplicate call to +initialize">,
+   InGroup<DiagGroup<"explicit-initialize-call">>;
 def error_ivar_use_in_class_method : Error<
   "instance variable %0 accessed in class method">;
 def error_implicit_ivar_access : Error<

Modified: cfe/trunk/include/clang/Basic/IdentifierTable.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/IdentifierTable.h?rev=216271&r1=216270&r2=216271&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/IdentifierTable.h (original)
+++ cfe/trunk/include/clang/Basic/IdentifierTable.h Fri Aug 22 11:57:26 2014
@@ -563,6 +563,7 @@ enum ObjCMethodFamily {
   OMF_retain,
   OMF_retainCount,
   OMF_self,
+  OMF_initialize,
 
   // performSelector families
   OMF_performSelector

Modified: cfe/trunk/lib/AST/DeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclObjC.cpp?rev=216271&r1=216270&r2=216271&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclObjC.cpp (original)
+++ cfe/trunk/lib/AST/DeclObjC.cpp Fri Aug 22 11:57:26 2014
@@ -849,6 +849,11 @@ ObjCMethodFamily ObjCMethodDecl::getMeth
       family = OMF_None;
     break;
       
+  case OMF_initialize:
+    if (isInstanceMethod() || !getReturnType()->isVoidType())
+      family = OMF_None;
+    break;
+      
   case OMF_performSelector:
     if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
       family = OMF_None;

Modified: cfe/trunk/lib/Basic/IdentifierTable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/IdentifierTable.cpp?rev=216271&r1=216270&r2=216271&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/IdentifierTable.cpp (original)
+++ cfe/trunk/lib/Basic/IdentifierTable.cpp Fri Aug 22 11:57:26 2014
@@ -428,6 +428,7 @@ ObjCMethodFamily Selector::getMethodFami
     if (name == "retain") return OMF_retain;
     if (name == "retainCount") return OMF_retainCount;
     if (name == "self") return OMF_self;
+    if (name == "initialize") return OMF_initialize;
   }
  
   if (name == "performSelector") return OMF_performSelector;

Modified: cfe/trunk/lib/Sema/SemaDeclObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclObjC.cpp?rev=216271&r1=216270&r2=216271&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclObjC.cpp Fri Aug 22 11:57:26 2014
@@ -201,6 +201,7 @@ bool Sema::CheckARCMethodDecl(ObjCMethod
   case OMF_autorelease:
   case OMF_retainCount:
   case OMF_self:
+  case OMF_initialize:
   case OMF_performSelector:
     return false;
 
@@ -353,6 +354,7 @@ void Sema::ActOnStartOfObjCMethodDef(Sco
     case OMF_copy:
     case OMF_new:
     case OMF_self:
+    case OMF_initialize:
     case OMF_performSelector:
       break;
     }
@@ -1515,6 +1517,7 @@ static bool checkMethodFamilyMismatch(Se
   case OMF_finalize:
   case OMF_retainCount:
   case OMF_self:
+  case OMF_initialize:
   case OMF_performSelector:
     // Mismatches for these methods don't change ownership
     // conventions, so we don't care.
@@ -3259,6 +3262,7 @@ Decl *Sema::ActOnMethodDeclaration(
     case OMF_mutableCopy:
     case OMF_release:
     case OMF_retainCount:
+    case OMF_initialize:
     case OMF_performSelector:
       break;
       

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=216271&r1=216270&r2=216271&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Fri Aug 22 11:57:26 2014
@@ -1137,6 +1137,7 @@ ExprResult Sema::ParseObjCSelectorExpres
     case OMF_mutableCopy:
     case OMF_new:
     case OMF_self:
+    case OMF_initialize:
     case OMF_performSelector:
       break;
     }
@@ -2230,6 +2231,17 @@ ExprResult Sema::BuildClassMessage(TypeS
       RequireCompleteType(LBracLoc, Method->getReturnType(),
                           diag::err_illegal_message_expr_incomplete_type))
     return ExprError();
+  
+  // Warn about explicit call of +initialize on its own class.
+  if (Method && Method->getMethodFamily() == OMF_initialize) {
+    const ObjCInterfaceDecl *ID =
+      dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext());
+    if (ID == Class) {
+      Diag(Loc, diag::warn_direct_initialize_call);
+      Diag(Method->getLocation(), diag::note_method_declared_at)
+      << Method->getDeclName();
+    }
+  }
 
   // Construct the appropriate ObjCMessageExpr.
   ObjCMessageExpr *Result;
@@ -2652,6 +2664,7 @@ ExprResult Sema::BuildInstanceMessage(Ex
     case OMF_mutableCopy:
     case OMF_new:
     case OMF_self:
+    case OMF_initialize:
       break;
 
     case OMF_dealloc:

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp?rev=216271&r1=216270&r2=216271&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp Fri Aug 22 11:57:26 2014
@@ -1359,6 +1359,7 @@ RetainSummaryManager::getStandardMethodS
   // Check the method family, and apply any default annotations.
   switch (MD ? MD->getMethodFamily() : S.getMethodFamily()) {
     case OMF_None:
+    case OMF_initialize:
     case OMF_performSelector:
       // Assume all Objective-C methods follow Cocoa Memory Management rules.
       // FIXME: Does the non-threaded performSelector family really belong here?

Added: cfe/trunk/test/SemaObjC/warn-explicit-call-initialize.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/warn-explicit-call-initialize.m?rev=216271&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/warn-explicit-call-initialize.m (added)
+++ cfe/trunk/test/SemaObjC/warn-explicit-call-initialize.m Fri Aug 22 11:57:26 2014
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1  -fsyntax-only  -triple x86_64-apple-darwin10 -verify %s
+// rdar://16628028
+
+ at interface NSObject
++ (void)initialize; // expected-note {{method 'initialize' declared here}}
+ at end
+
+ at interface I : NSObject 
++ (void)initialize; // expected-note {{method 'initialize' declared here}}
+ at end
+
+ at implementation I
+- (void) Meth { 
+  [I initialize];     // expected-warning {{explicit call to +initialize results in duplicate call to +initialize}} 
+  [NSObject initialize]; // expected-warning {{explicit call to +initialize results in duplicate call to +initialize}}
+}
++ (void)initialize {}
+ at end
+





More information about the cfe-commits mailing list