File tree Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Expand file tree Collapse file tree 2 files changed +27
-4
lines changed Original file line number Diff line number Diff line change @@ -1291,7 +1291,9 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
1291
1291
ArrayRef<const Attr *> ForAttrs) {
1292
1292
JumpDest LoopExit = getJumpDestInCurrentScope (" for.end" );
1293
1293
1294
- LexicalScope ForScope (*this , S.getSourceRange ());
1294
+ std::optional<LexicalScope> ForScope;
1295
+ if (getLangOpts ().C99 || getLangOpts ().CPlusPlus )
1296
+ ForScope.emplace (*this , S.getSourceRange ());
1295
1297
1296
1298
// Evaluate the first part before the loop.
1297
1299
if (S.getInit ())
@@ -1350,7 +1352,7 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
1350
1352
llvm::BasicBlock *ExitBlock = LoopExit.getBlock ();
1351
1353
// If there are any cleanups between here and the loop-exit scope,
1352
1354
// create a block to stage a loop exit along.
1353
- if (ForScope. requiresCleanups ())
1355
+ if (ForScope && ForScope-> requiresCleanups ())
1354
1356
ExitBlock = createBasicBlock (" for.cond.cleanup" );
1355
1357
1356
1358
// As long as the condition is true, iterate the loop.
@@ -1419,7 +1421,8 @@ void CodeGenFunction::EmitForStmt(const ForStmt &S,
1419
1421
EmitStopPoint (&S);
1420
1422
EmitBranch (CondBlock);
1421
1423
1422
- ForScope.ForceCleanup ();
1424
+ if (ForScope)
1425
+ ForScope->ForceCleanup ();
1423
1426
1424
1427
LoopStack.pop ();
1425
1428
Original file line number Diff line number Diff line change 1
- // RUN: %clang_cc1 -emit-llvm %s -o %t
1
+ // RUN: %clang_cc1 -std=c89 -emit-llvm %s -o - | FileCheck %s --check-prefix=C89
2
+ // RUN: %clang_cc1 -std=c99 -emit-llvm %s -o - | FileCheck %s --check-prefix=C99
2
3
3
4
void f (void * arg );
4
5
void g (void ) {
5
6
__attribute__((cleanup (f ))) void * g ;
6
7
}
7
8
9
+ void cleaner (int * p );
10
+
11
+ // C89-LABEL: define{{.*}} void @test_nested_for_loop_cleanup()
12
+ // C99-LABEL: define{{.*}} void @test_nested_for_loop_cleanup()
13
+ void test_nested_for_loop_cleanup (void ) {
14
+ for (int i = 10 ; 0 ;) {
15
+ for (__attribute__((cleanup (cleaner ))) int j = 20 ; 0 ;)
16
+ ;
17
+ i = 5 ; // Some operation after inner loop
18
+ }
19
+ }
20
+
21
+ // C89: for.end:
22
+ // C89-NEXT: store i32 5, ptr %i, align 4
23
+ // C89-NEXT: call void @cleaner(ptr noundef %j)
24
+
25
+ // C99: for.cond.cleanup:
26
+ // C99-NEXT: call void @cleaner(ptr noundef %j)
27
+ // C99-NEXT: br label %for.end
You can’t perform that action at this time.
0 commit comments