Author: ahatanak Date: Wed Apr 19 12:54:08 2017 New Revision: 300722 URL: http://llvm.org/viewvc/llvm-project?rev=300722&view=rev Log: [Sema][ObjC] Disallow jumping into ObjC fast enumeration loops.
rdar://problem/31635406 Differential Revision: https://reviews.llvm.org/D32187 Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td cfe/trunk/lib/Sema/JumpDiagnostics.cpp cfe/trunk/lib/Sema/SemaStmt.cpp cfe/trunk/test/SemaObjC/foreach.m Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=300722&r1=300721&r2=300722&view=diff ============================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original) +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Apr 19 12:54:08 2017 @@ -5000,6 +5000,8 @@ def note_protected_by_if_available : Not "jump enters controlled statement of if available">; def note_protected_by_vla : Note< "jump bypasses initialization of variable length array">; +def note_protected_by_objc_fast_enumeration : Note< + "jump enters Objective-C fast enumeration loop">; def note_protected_by_objc_try : Note< "jump bypasses initialization of @try block">; def note_protected_by_objc_catch : Note< Modified: cfe/trunk/lib/Sema/JumpDiagnostics.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/JumpDiagnostics.cpp?rev=300722&r1=300721&r2=300722&view=diff ============================================================================== --- cfe/trunk/lib/Sema/JumpDiagnostics.cpp (original) +++ cfe/trunk/lib/Sema/JumpDiagnostics.cpp Wed Apr 19 12:54:08 2017 @@ -287,6 +287,15 @@ void JumpScopeChecker::BuildScopeInforma IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel()); break; + case Stmt::ObjCForCollectionStmtClass: { + auto *CS = cast<ObjCForCollectionStmt>(S); + unsigned Diag = diag::note_protected_by_objc_fast_enumeration; + unsigned NewParentScope = Scopes.size(); + Scopes.push_back(GotoScope(ParentScope, Diag, 0, S->getLocStart())); + BuildScopeInformation(CS->getBody(), NewParentScope); + return; + } + case Stmt::IndirectGotoStmtClass: // "goto *&&lbl;" is a special case which we treat as equivalent // to a normal goto. In addition, we don't calculate scope in the Modified: cfe/trunk/lib/Sema/SemaStmt.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=300722&r1=300721&r2=300722&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaStmt.cpp (original) +++ cfe/trunk/lib/Sema/SemaStmt.cpp Wed Apr 19 12:54:08 2017 @@ -1783,6 +1783,7 @@ StmtResult Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, Stmt *First, Expr *collection, SourceLocation RParenLoc) { + getCurFunction()->setHasBranchProtectedScope(); ExprResult CollectionExprResult = CheckObjCForCollectionOperand(ForLoc, collection); Modified: cfe/trunk/test/SemaObjC/foreach.m URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/foreach.m?rev=300722&r1=300721&r2=300722&view=diff ============================================================================== --- cfe/trunk/test/SemaObjC/foreach.m (original) +++ cfe/trunk/test/SemaObjC/foreach.m Wed Apr 19 12:54:08 2017 @@ -55,3 +55,27 @@ void test2(NSObject<NSFastEnumeration> * for (obj.prop in collection) { /* expected-error {{selector element is not a valid lvalue}} */ } } + +int cond(); + +void test3(NSObject<NSFastEnumeration> *a0, NSObject<NSFastEnumeration> *a1) { + for (id i in a0) { /* expected-note 2 {{jump enters Objective-C fast enumeration loop}} */ + for (id j in a1) { /* expected-note 2 {{jump enters Objective-C fast enumeration loop}} */ + (void)i, (void)j; +label0: + if (cond()) + goto label1; + } +label1: + if (cond()) + goto label0; /* expected-error {{cannot jump from this goto statement to its label}} */ + if (cond()) + goto label2; + } + +label2: + if (cond()) + goto label0; /* expected-error {{cannot jump from this goto statement to its label}} */ + if (cond()) + goto label1; /* expected-error{{cannot jump from this goto statement to its label}} */ +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits