diff --git a/clang/lib/Sema/JumpDiagnostics.cpp b/clang/lib/Sema/JumpDiagnostics.cpp index a852a950b47f..36704c3826df 100644 --- a/clang/lib/Sema/JumpDiagnostics.cpp +++ b/clang/lib/Sema/JumpDiagnostics.cpp @@ -602,7 +602,10 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S, Scopes.push_back(GotoScope( ParentScope, diag::note_acc_branch_into_compute_construct, diag::note_acc_branch_out_of_compute_construct, CC->getBeginLoc())); - BuildScopeInformation(CC->getStructuredBlock(), NewParentScope); + // This can be 'null' if the 'body' is a break that we diagnosed, so no + // reason to put the scope into place. + if (CC->getStructuredBlock()) + BuildScopeInformation(CC->getStructuredBlock(), NewParentScope); return; } @@ -612,7 +615,10 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S, Scopes.push_back(GotoScope( ParentScope, diag::note_acc_branch_into_compute_construct, diag::note_acc_branch_out_of_compute_construct, CC->getBeginLoc())); - BuildScopeInformation(CC->getLoop(), NewParentScope); + // This can be 'null' if the 'body' is a break that we diagnosed, so no + // reason to put the scope into place. + if (CC->getLoop()) + BuildScopeInformation(CC->getLoop(), NewParentScope); return; } diff --git a/clang/test/SemaOpenACC/gh140712.cpp b/clang/test/SemaOpenACC/gh140712.cpp new file mode 100644 index 000000000000..ca1b6234be72 --- /dev/null +++ b/clang/test/SemaOpenACC/gh140712.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 %s -fopenacc -verify + +void foo() { + switch (int x = 0) { + case 0: +#pragma acc parallel + break; // expected-error{{invalid branch out of OpenACC Compute/Combined Construct}} + } +}