From 89bc314c6ddf3b851ccf68bc34d3f1b5927a10f6 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Fri, 8 May 2009 23:02:36 +0000 Subject: [PATCH] Warn if forward class is used as a receiver. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71278 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaExprObjC.cpp | 10 +++++++++- test/SemaObjC/forward-class-receiver.m | 13 +++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/SemaObjC/forward-class-receiver.m diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index a8d079d720..f491a43e3b 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1690,6 +1690,8 @@ def err_collection_expr_type : Error< // Type def ext_invalid_sign_spec : Extension<"'%0' cannot be signed or unsigned">; +def warn_receiver_forward_class : Warning< + "receiver %0 is a forward class and corresponding @interface may not exist">; def warn_missing_declspec : Warning< "declaration specifier missing, defaulting to 'int'">; def warn_missing_type_specifier : Warning< diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index e8ff18ba0c..3dbc2cf015 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -422,7 +422,15 @@ Sema::ExprResult Sema::ActOnClassMessage( assert(ClassDecl && "missing interface declaration"); ObjCMethodDecl *Method = 0; QualType returnType; - Method = ClassDecl->lookupClassMethod(Context, Sel); + if (ClassDecl->isForwardDecl()) { + // A forward class used in messaging is tread as a 'Class' + Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac)); + if (Method) + Diag(lbrac, diag::warn_receiver_forward_class) + << ClassDecl->getDeclName(); + } + if (!Method) + Method = ClassDecl->lookupClassMethod(Context, Sel); // If we have an implementation in scope, check "private" methods. if (!Method) diff --git a/test/SemaObjC/forward-class-receiver.m b/test/SemaObjC/forward-class-receiver.m new file mode 100644 index 0000000000..8353f39d38 --- /dev/null +++ b/test/SemaObjC/forward-class-receiver.m @@ -0,0 +1,13 @@ +// RUN: clang-cc -fsyntax-only -verify %s + +@interface I ++ new; +@end +Class isa; + +@class NotKnown; + +void foo(NotKnown *n) { + [isa new]; + [NotKnown new]; /* expected-warning {{receiver 'NotKnown' is a forward class and corresponding}} */ +} -- 2.40.0