r311779 - [ObjC] Add a -Wobjc-messaging-id warning

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 25 09:12:17 PDT 2017


Author: arphaman
Date: Fri Aug 25 09:12:17 2017
New Revision: 311779

URL: http://llvm.org/viewvc/llvm-project?rev=311779&view=rev
Log:
[ObjC] Add a -Wobjc-messaging-id warning

-Wobjc-messaging-id is a new, non-default warning that warns about
message sends to unqualified id in Objective-C. This warning is useful
for projects that would like to avoid any potential future compiler
errors/warnings, as the system frameworks might add a method with the same
selector which could make the message send to id ambiguous.

rdar://33303354

Added:
    cfe/trunk/test/SemaObjC/warn-messaging-id.mm
Modified:
    cfe/trunk/docs/ReleaseNotes.rst
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExprObjC.cpp

Modified: cfe/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/ReleaseNotes.rst?rev=311779&r1=311778&r2=311779&view=diff
==============================================================================
--- cfe/trunk/docs/ReleaseNotes.rst (original)
+++ cfe/trunk/docs/ReleaseNotes.rst Fri Aug 25 09:12:17 2017
@@ -66,6 +66,12 @@ Improvements to Clang's diagnostics
     a non-default alignment that has been specified using a ``#pragma pack``
     directive prior to the ``#include``.
 
+- ``-Wobjc-messaging-id`` is a new, non-default warning that warns about
+  message sends to unqualified ``id`` in Objective-C. This warning is useful
+  for projects that would like to avoid any potential future compiler
+  errors/warnings, as the system frameworks might add a method with the same
+  selector which could make the message send to ``id`` ambiguous.
+
 Non-comprehensive list of changes in this release
 -------------------------------------------------
 

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=311779&r1=311778&r2=311779&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Aug 25 09:12:17 2017
@@ -1211,6 +1211,10 @@ def err_objc_method_unsupported_param_re
   "%0 %select{parameter|return}1 type is unsupported; "
   "support for vector types for this target is introduced in %2">;
 
+def warn_messaging_unqualified_id : Warning<
+  "messaging unqualified id">, DefaultIgnore,
+  InGroup<DiagGroup<"objc-messaging-id">>;
+
 // C++ declarations
 def err_static_assert_expression_is_not_constant : Error<
   "static_assert expression is not an integral constant expression">;

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=311779&r1=311778&r2=311779&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Fri Aug 25 09:12:17 2017
@@ -2705,6 +2705,9 @@ ExprResult Sema::BuildInstanceMessage(Ex
     }
   }
 
+  if (ReceiverType->isObjCIdType() && !isImplicit)
+    Diag(Receiver->getExprLoc(), diag::warn_messaging_unqualified_id);
+
   // There's a somewhat weird interaction here where we assume that we
   // won't actually have a method unless we also don't need to do some
   // of the more detailed type-checking on the receiver.

Added: cfe/trunk/test/SemaObjC/warn-messaging-id.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/warn-messaging-id.mm?rev=311779&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/warn-messaging-id.mm (added)
+++ cfe/trunk/test/SemaObjC/warn-messaging-id.mm Fri Aug 25 09:12:17 2017
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-objc-root-class -Wobjc-messaging-id %s
+
+ at interface CallMeMaybe
+
+- (void)doThing:(int)intThing;
+
+ at property int thing;
+
+ at end
+
+template<typename T>
+void instantiate(const T &x) {
+  [x setThing: 22]; // expected-warning {{messaging unqualified id}}
+}
+
+void fn() {
+  id myObject;
+  [myObject doThing: 10]; // expected-warning {{messaging unqualified id}}
+  [myObject setThing: 11]; // expected-warning {{messaging unqualified id}}
+  instantiate(myObject); // expected-note {{in instantiation}}
+}




More information about the cfe-commits mailing list