126 changes: 104 additions & 22 deletions clang/test/OpenMP/nesting_of_regions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,39 @@ template <class T>
void foo() {
#pragma omp parallel
#pragma omp for
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
#pragma omp parallel
#pragma omp simd
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
#pragma omp parallel
#pragma omp sections
{
bar();
}
#pragma omp parallel
#pragma omp section // expected-error {{'omp section' directive must be closely nested to a sections region, not a parallel region}}
{
bar();
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
#pragma omp for // expected-error {{OpenMP constructs may not be nested inside a simd region}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
#pragma omp simd // expected-error {{OpenMP constructs may not be nested inside a simd region}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
#pragma omp parallel // expected-error {{OpenMP constructs may not be nested inside a simd region}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
Expand All @@ -37,20 +47,30 @@ void foo() {
bar();
}
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
#pragma omp section // expected-error {{OpenMP constructs may not be nested inside a simd region}}
{
bar();
}
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
#pragma omp for // expected-error {{region cannot be closely nested inside 'for' region; perhaps you forget to enclose 'omp for' directive into a parallel region?}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
#pragma omp simd
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
#pragma omp parallel
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
Expand All @@ -59,20 +79,30 @@ void foo() {
bar();
}
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
#pragma omp section // expected-error {{'omp section' directive must be closely nested to a sections region, not a for region}}
{
bar();
}
}
#pragma omp sections
{
#pragma omp for // expected-error {{region cannot be closely nested inside 'sections' region; perhaps you forget to enclose 'omp for' directive into a parallel region?}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp sections
{
#pragma omp simd
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp sections
{
#pragma omp parallel
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp sections
{
Expand All @@ -81,34 +111,55 @@ void foo() {
bar();
}
}
#pragma omp sections
{
#pragma omp section
{
bar();
}
}
#pragma omp section // expected-error {{orphaned 'omp section' directives are prohibited, it must be closely nested to a sections region}}
{
bar();
}
}

void foo() {
#pragma omp parallel
#pragma omp for
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
#pragma omp parallel
#pragma omp simd
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
#pragma omp parallel
#pragma omp sections
{
bar();
}
#pragma omp parallel
#pragma omp section // expected-error {{'omp section' directive must be closely nested to a sections region, not a parallel region}}
{
bar();
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
#pragma omp for // expected-error {{OpenMP constructs may not be nested inside a simd region}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
#pragma omp simd // expected-error {{OpenMP constructs may not be nested inside a simd region}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
#pragma omp parallel // expected-error {{OpenMP constructs may not be nested inside a simd region}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
Expand All @@ -117,20 +168,30 @@ void foo() {
bar();
}
}
#pragma omp simd
for (int i = 0; i < 10; ++i) {
#pragma omp section // expected-error {{OpenMP constructs may not be nested inside a simd region}}
{
bar();
}
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
#pragma omp for // expected-error {{region cannot be closely nested inside 'for' region; perhaps you forget to enclose 'omp for' directive into a parallel region?}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
#pragma omp simd
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
#pragma omp parallel
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
Expand All @@ -139,20 +200,30 @@ void foo() {
bar();
}
}
#pragma omp for
for (int i = 0; i < 10; ++i) {
#pragma omp section // expected-error {{'omp section' directive must be closely nested to a sections region, not a for region}}
{
bar();
}
}
#pragma omp sections
{
#pragma omp for // expected-error {{region cannot be closely nested inside 'sections' region; perhaps you forget to enclose 'omp for' directive into a parallel region?}}
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp sections
{
#pragma omp simd
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp sections
{
#pragma omp parallel
for (int i = 0; i < 10; ++i);
for (int i = 0; i < 10; ++i)
;
}
#pragma omp sections
{
Expand All @@ -161,6 +232,17 @@ void foo() {
bar();
}
}
#pragma omp sections
{
#pragma omp section
{
bar();
}
}
#pragma omp section // expected-error {{orphaned 'omp section' directives are prohibited, it must be closely nested to a sections region}}
{
bar();
}
return foo<int>();
}

14 changes: 10 additions & 4 deletions clang/test/OpenMP/sections_ast_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ T tmain(T argc) {
static T a;
// CHECK: static T a;
#pragma omp parallel
#pragma omp sections private(argc, b), firstprivate(c, d), lastprivate(d, f) reduction (-: g) nowait
#pragma omp sections private(argc, b), firstprivate(c, d), lastprivate(d, f) reduction(- : g) nowait
{
foo();
foo();
}
// CHECK-NEXT: #pragma omp parallel
// CHECK-NEXT: #pragma omp sections private(argc,b) firstprivate(c,d) lastprivate(d,f) reduction(-: g) nowait
Expand All @@ -31,13 +31,19 @@ int main(int argc, char **argv) {
static int a;
// CHECK: static int a;
#pragma omp parallel
#pragma omp sections private(argc, b), firstprivate(argv, c), lastprivate(d, f) reduction(+:g) nowait
#pragma omp sections private(argc, b), firstprivate(argv, c), lastprivate(d, f) reduction(+ : g) nowait
{
foo();
#pragma omp section
foo();
#pragma omp section
foo();
}
// CHECK-NEXT: #pragma omp parallel
// CHECK-NEXT: #pragma omp sections private(argc,b) firstprivate(argv,c) lastprivate(d,f) reduction(+: g) nowait
// CHECK-NEXT: {
// CHECK-NEXT: #pragma omp section
// CHECK-NEXT: foo();
// CHECK-NEXT: #pragma omp section
// CHECK-NEXT: foo();
// CHECK-NEXT: }
return (tmain<int, 5>(argc) + tmain<char, 1>(argv[0][0]));
Expand Down
22 changes: 22 additions & 0 deletions clang/test/OpenMP/sections_misc_messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ void test_no_clause() {
// expected-error@+2 {{the statement for '#pragma omp sections' must be a compound statement}}
#pragma omp sections
++i;

#pragma omp sections
{
foo();
foo(); // expected-error {{statement in 'omp sections' directive must be enclosed into a section region}}
}

}

void test_branch_protected_scope() {
Expand All @@ -40,12 +47,24 @@ void test_branch_protected_scope() {
L2:
x[i]++;
}
#pragma omp section
if (i == 5)
goto L1; // expected-error {{use of undeclared label 'L1'}}
else if (i == 6)
return; // expected-error {{cannot return from OpenMP region}}
else if (i == 7)
goto L3;
else if (i == 8) {
L3:
x[i]++;
}
}

if (x[0] == 0)
goto L2; // expected-error {{use of undeclared label 'L2'}}
else if (x[1] == 1)
goto L1;
goto L3; // expected-error {{use of undeclared label 'L3'}}
}

void test_invalid_clause() {
Expand All @@ -55,6 +74,9 @@ void test_invalid_clause() {
#pragma omp sections foo bar
{
foo();
// expected-error@+1 {{unexpected OpenMP clause 'nowait' in directive '#pragma omp section'}}
#pragma omp section nowait
;
}
}

Expand Down
7 changes: 7 additions & 0 deletions clang/tools/libclang/CIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,7 @@ class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> {
void VisitOMPSimdDirective(const OMPSimdDirective *D);
void VisitOMPForDirective(const OMPForDirective *D);
void VisitOMPSectionsDirective(const OMPSectionsDirective *D);
void VisitOMPSectionDirective(const OMPSectionDirective *D);

private:
void AddDeclarationNameInfo(const Stmt *S);
Expand Down Expand Up @@ -2292,6 +2293,10 @@ void EnqueueVisitor::VisitOMPSectionsDirective(const OMPSectionsDirective *D) {
VisitOMPExecutableDirective(D);
}

void EnqueueVisitor::VisitOMPSectionDirective(const OMPSectionDirective *D) {
VisitOMPExecutableDirective(D);
}

void CursorVisitor::EnqueueWorkList(VisitorWorkList &WL, const Stmt *S) {
EnqueueVisitor(WL, MakeCXCursor(S, StmtParent, TU,RegionOfInterest)).Visit(S);
}
Expand Down Expand Up @@ -3972,6 +3977,8 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
return cxstring::createRef("OMPForDirective");
case CXCursor_OMPSectionsDirective:
return cxstring::createRef("OMPSectionsDirective");
case CXCursor_OMPSectionDirective:
return cxstring::createRef("OMPSectionDirective");
}

llvm_unreachable("Unhandled CXCursorKind");
Expand Down
3 changes: 3 additions & 0 deletions clang/tools/libclang/CXCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,9 @@ CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
case Stmt::OMPSectionsDirectiveClass:
K = CXCursor_OMPSectionsDirective;
break;
case Stmt::OMPSectionDirectiveClass:
K = CXCursor_OMPSectionDirective;
break;
}

CXCursor C = { K, 0, { Parent, S, TU } };
Expand Down