Skip to content

Commit 6f13534

Browse files
committed
[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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311779 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 494ef6f commit 6f13534

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

docs/ReleaseNotes.rst

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ Improvements to Clang's diagnostics
6666
a non-default alignment that has been specified using a ``#pragma pack``
6767
directive prior to the ``#include``.
6868

69+
- ``-Wobjc-messaging-id`` is a new, non-default warning that warns about
70+
message sends to unqualified ``id`` in Objective-C. This warning is useful
71+
for projects that would like to avoid any potential future compiler
72+
errors/warnings, as the system frameworks might add a method with the same
73+
selector which could make the message send to ``id`` ambiguous.
74+
6975
Non-comprehensive list of changes in this release
7076
-------------------------------------------------
7177

include/clang/Basic/DiagnosticSemaKinds.td

+4
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,10 @@ def err_objc_method_unsupported_param_ret_type : Error<
12111211
"%0 %select{parameter|return}1 type is unsupported; "
12121212
"support for vector types for this target is introduced in %2">;
12131213

1214+
def warn_messaging_unqualified_id : Warning<
1215+
"messaging unqualified id">, DefaultIgnore,
1216+
InGroup<DiagGroup<"objc-messaging-id">>;
1217+
12141218
// C++ declarations
12151219
def err_static_assert_expression_is_not_constant : Error<
12161220
"static_assert expression is not an integral constant expression">;

lib/Sema/SemaExprObjC.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2705,6 +2705,9 @@ ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
27052705
}
27062706
}
27072707

2708+
if (ReceiverType->isObjCIdType() && !isImplicit)
2709+
Diag(Receiver->getExprLoc(), diag::warn_messaging_unqualified_id);
2710+
27082711
// There's a somewhat weird interaction here where we assume that we
27092712
// won't actually have a method unless we also don't need to do some
27102713
// of the more detailed type-checking on the receiver.

test/SemaObjC/warn-messaging-id.mm

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -Wno-objc-root-class -Wobjc-messaging-id %s
2+
3+
@interface CallMeMaybe
4+
5+
- (void)doThing:(int)intThing;
6+
7+
@property int thing;
8+
9+
@end
10+
11+
template<typename T>
12+
void instantiate(const T &x) {
13+
[x setThing: 22]; // expected-warning {{messaging unqualified id}}
14+
}
15+
16+
void fn() {
17+
id myObject;
18+
[myObject doThing: 10]; // expected-warning {{messaging unqualified id}}
19+
[myObject setThing: 11]; // expected-warning {{messaging unqualified id}}
20+
instantiate(myObject); // expected-note {{in instantiation}}
21+
}

0 commit comments

Comments
 (0)