From 80b0b42a09c3de8392f9ba1e24ffcc35355a8dea Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Thu, 20 Nov 2008 18:10:58 +0000 Subject: [PATCH] Added a test case for __weak field decls. Change SetVarDeclObjCAttribute to static function. Added comments. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59738 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 17 +++++++++-------- lib/CodeGen/CodeGenFunction.h | 2 -- test/SemaObjC/warn-weak-field.m | 13 +++++++++++++ 3 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 test/SemaObjC/warn-weak-field.m diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 4228bc22b6..a9cf4535cf 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -523,18 +523,19 @@ void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, /// SetVarDeclObjCAttribute - Set __weak/__strong attributes into the LValue /// object. -void CodeGenFunction::SetVarDeclObjCAttribute(const VarDecl *VD, - const QualType &Ty, - LValue &LV) +static void SetVarDeclObjCAttribute(ASTContext &Ctx, const VarDecl *VD, + const QualType &Ty, LValue &LV) { if (const ObjCGCAttr *A = VD->getAttr()) { ObjCGCAttr::GCAttrTypes attrType = A->getType(); LValue::SetObjCType(attrType == ObjCGCAttr::Weak, attrType == ObjCGCAttr::Strong, LV); } - else if (CGM.getLangOptions().ObjC1 && - CGM.getLangOptions().getGCMode() != LangOptions::NonGC) { - if (getContext().isObjCObjectPointerType(Ty)) + else if (Ctx.getLangOptions().ObjC1 && + Ctx.getLangOptions().getGCMode() != LangOptions::NonGC) { + // Default behavious under objective-c's gc is for objective-c pointers + // be treated as though they were declared as __strong. + if (Ctx.isObjCObjectPointerType(Ty)) LValue::SetObjCType(false, true, LV); } } @@ -557,12 +558,12 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { if (VD->isBlockVarDecl() && (VD->getStorageClass() == VarDecl::Static || VD->getStorageClass() == VarDecl::Extern)) - SetVarDeclObjCAttribute(VD, E->getType(), LV); + SetVarDeclObjCAttribute(getContext(), VD, E->getType(), LV); return LV; } else if (VD && VD->isFileVarDecl()) { LValue LV = LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD), E->getType().getCVRQualifiers()); - SetVarDeclObjCAttribute(VD, E->getType(), LV); + SetVarDeclObjCAttribute(getContext(), VD, E->getType(), LV); return LV; } else if (const FunctionDecl *FD = dyn_cast(E->getDecl())) { return LValue::MakeAddr(CGM.GetAddrOfFunction(FD), diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index 1da4e195ba..85c500b95e 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -462,8 +462,6 @@ public: LValue EmitBinaryOperatorLValue(const BinaryOperator *E); // Note: only availabe for agg return types LValue EmitCallExprLValue(const CallExpr *E); - void SetVarDeclObjCAttribute(const VarDecl *VD, const QualType &Ty, - LValue &LV); LValue EmitDeclRefLValue(const DeclRefExpr *E); LValue EmitStringLiteralLValue(const StringLiteral *E); LValue EmitPredefinedFunctionName(unsigned Type); diff --git a/test/SemaObjC/warn-weak-field.m b/test/SemaObjC/warn-weak-field.m new file mode 100644 index 0000000000..93c23be228 --- /dev/null +++ b/test/SemaObjC/warn-weak-field.m @@ -0,0 +1,13 @@ +// RUN: clang -fsyntax-only -fobjc-gc -verify %s + +struct S { + __weak id w; // expected-warning {{__weak attribute cannot be specified on a field declaration}} + __strong id p1; +}; + +int main () +{ + struct I { + __weak id w1; // expected-warning {{__weak attribute cannot be specified on a field declaration}} + }; +} -- 2.50.1