From: Steve Naroff Date: Fri, 26 Sep 2008 14:41:28 +0000 (+0000) Subject: Tweak Expr::isModifiableLvalue() and Expr::isLvalue() to better deal with BlockDeclRe... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4f6a7d7ead09b439216c32f2de806a998aeb222a;p=clang Tweak Expr::isModifiableLvalue() and Expr::isLvalue() to better deal with BlockDeclRef exprs. This fixes clang: Error when using address of stack variable inside block. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@56652 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 18860534f1..9430b51dea 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -87,7 +87,8 @@ public: MLV_InvalidExpression, MLV_IncompleteType, MLV_ConstQualified, - MLV_ArrayType + MLV_ArrayType, + MLV_NotBlockQualified }; isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx) const; diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def index 5d4d893174..cf4bf2a235 100644 --- a/include/clang/Basic/DiagnosticKinds.def +++ b/include/clang/Basic/DiagnosticKinds.def @@ -1036,6 +1036,8 @@ DIAG(err_typecheck_incomplete_type_not_modifiable_lvalue, ERROR, DIAG(err_typecheck_duplicate_vector_components_not_mlvalue, ERROR, "vector is not assignable (contains duplicate components)") +DIAG(err_block_decl_ref_not_modifiable_lvalue, ERROR, + "variable is not assignable (missing __block type specifier)") DIAG(err_typecheck_call_not_function, ERROR, "called object is not a function or function pointer") DIAG(err_typecheck_call_too_few_args, ERROR, diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 9c3d623c16..456b87b64e 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -425,7 +425,7 @@ Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { } case BlockDeclRefExprClass: { const BlockDeclRefExpr *BDR = cast(this); - if (BDR->isByRef() && isa(BDR->getDecl())) + if (isa(BDR->getDecl())) return LV_Valid; break; } @@ -497,6 +497,15 @@ Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const { if (r->hasConstFields()) return MLV_ConstQualified; } + // The following is illegal: + // void takeclosure(void (^C)(void)); + // void func() { int x = 1; takeclosure(^{ x = 7 }); } + // + if (getStmtClass() == BlockDeclRefExprClass) { + const BlockDeclRefExpr *BDR = cast(this); + if (!BDR->isByRef() && isa(BDR->getDecl())) + return MLV_NotBlockQualified; + } return MLV_Valid; } diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 1a80283f82..e8b4d8cb4b 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -2181,6 +2181,10 @@ inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue, lex->getSourceRange()); return QualType(); + case Expr::MLV_NotBlockQualified: + Diag(loc, diag::err_block_decl_ref_not_modifiable_lvalue, + lex->getSourceRange()); + return QualType(); } AssignConvertType ConvTy; diff --git a/test/Sema/block-literal.c b/test/Sema/block-literal.c index 235bb7e23d..3a091395d0 100644 --- a/test/Sema/block-literal.c +++ b/test/Sema/block-literal.c @@ -39,7 +39,9 @@ void test2() { } foo: - takeclosure(^{ x = 4; }); // expected-error {{expression is not assignable}} + takeclosure(^{ x = 4; }); // expected-error {{variable is not assignable (missing __block type specifier)}} + __block y = 7; + takeclosure(^{ y = 8; }); } @@ -52,6 +54,19 @@ void test4() { void (*noop2)() = 0; } +void myfunc(int (^block)(int)) {} + +void myfunc3(int *x); + +void test5() { + int a; + + myfunc(^(int abcd) { + myfunc3(&a); + return 1; + }); +} + void *X; void test_arguments() {