]> granicus.if.org Git - clang/commitdiff
run the jump checker on blocks, even though they don't have gotos,
authorChris Lattner <sabre@nondot.org>
Sun, 19 Apr 2009 05:28:12 +0000 (05:28 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 19 Apr 2009 05:28:12 +0000 (05:28 +0000)
they do allow switches.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69510 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/Sema.h
lib/Sema/SemaExpr.cpp
test/Sema/scope-check.c

index e8742e8e513e6616d9ad27b9de412ea100b992ca..e4af8a5c46c9bad264db040dac5ee89b1b74fe66 100644 (file)
@@ -107,6 +107,10 @@ struct BlockSemaInfo {
   /// block.
   llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
   
+  /// SavedFunctionNeedsScopeChecking - This is the value of
+  /// CurFunctionNeedsScopeChecking at the point when the block started.
+  bool SavedFunctionNeedsScopeChecking;
+  
   /// PrevBlockInfo - If this is nested inside another block, this points
   /// to the outer block.
   BlockSemaInfo *PrevBlockInfo;
index d8e57f928ddb2cb7e05f6a8e3573d6e78dab2b7e..3eeadce453cafc49145fa9422476c6665cbbc6f4 100644 (file)
@@ -4661,6 +4661,8 @@ void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) {
   BSI->ReturnType = 0;
   BSI->TheScope = BlockScope;
   BSI->hasBlockDeclRefExprs = false;
+  BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking;
+  CurFunctionNeedsScopeChecking = false;
 
   BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc);
   PushDeclContext(BlockScope, BSI->TheDecl);
@@ -4746,11 +4748,12 @@ void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) {
   // Ensure that CurBlock is deleted.
   llvm::OwningPtr<BlockSemaInfo> CC(CurBlock);
 
+  CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking;
+
   // Pop off CurBlock, handle nested blocks.
   CurBlock = CurBlock->PrevBlockInfo;
 
   // FIXME: Delete the ParmVarDecl objects as well???
-  
 }
 
 /// ActOnBlockStmtExpr - This is called when the body of a block statement
@@ -4793,6 +4796,11 @@ Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc,
 
   BlockTy = Context.getBlockPointerType(BlockTy);
 
+  // If needed, diagnose invalid gotos and switches in the block.
+  if (CurFunctionNeedsScopeChecking)
+    DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get()));
+  CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking;
+  
   BSI->TheDecl->setBody(static_cast<CompoundStmt*>(body.release()));
   return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy,
                                        BSI->hasBlockDeclRefExprs));
index 59fd832dc6373563d55256e4c101323d461a48b9..76041c4916536ede0bbe6459f7902dad5d713e12 100644 (file)
@@ -164,9 +164,22 @@ L2:
   return;
 }
 
+void test11(int n) {
+  void *P = ^{
+    switch (n) {
+    case 1:;
+    case 2: 
+    case 3:;
+      int Arr[n]; // expected-note {{jump bypasses initialization of variable length array}}
+    case 4:       // expected-error {{illegal switch case into protected scope}}
+      return;
+    }
+  };
+}
+
 
 // TODO: When and if gotos are allowed in blocks, this should work.
-void test13(int n) {
+void test12(int n) {
   void *P = ^{
     goto L1;  // expected-error {{goto not allowed in block literal}}
   L1: