diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp index 794a78408863c..117d4cbb8ad0c 100644 --- a/flang/lib/Semantics/resolve-directives.cpp +++ b/flang/lib/Semantics/resolve-directives.cpp @@ -1944,18 +1944,19 @@ void OmpAttributeVisitor::ResolveOmpNameList( Symbol *OmpAttributeVisitor::ResolveOmpCommonBlockName( const parser::Name *name) { - if (auto *prev{name - ? GetContext().scope.parent().FindCommonBlock(name->source) - : nullptr}) { + if (!name) { + return nullptr; + } + // First check if the Common Block is declared in the current scope + if (auto *cur{GetContext().scope.FindCommonBlock(name->source)}) { + name->symbol = cur; + return cur; + } + // Then check parent scope + if (auto *prev{GetContext().scope.parent().FindCommonBlock(name->source)}) { name->symbol = prev; return prev; } - // Check if the Common Block is declared in the current scope - if (auto *commonBlockSymbol{ - name ? GetContext().scope.FindCommonBlock(name->source) : nullptr}) { - name->symbol = commonBlockSymbol; - return commonBlockSymbol; - } return nullptr; } diff --git a/flang/test/Semantics/OpenMP/threadprivate06.f90 b/flang/test/Semantics/OpenMP/threadprivate06.f90 new file mode 100644 index 0000000000000..f31c38f6f2b24 --- /dev/null +++ b/flang/test/Semantics/OpenMP/threadprivate06.f90 @@ -0,0 +1,30 @@ +! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp +! OpenMP Version 5.1 +! Check OpenMP construct validity for the following directives: +! 2.21.2 Threadprivate Directive + +program main + call sub1() + print *, 'pass' +end program main + +subroutine sub1() + common /c/ a + !$omp threadprivate(/c/) + integer :: a + + a = 100 + call sub2() + if (a .ne. 101) print *, 'err' + +contains + subroutine sub2() + common /c/ a + !$omp threadprivate(/c/) + integer :: a + + !$omp parallel copyin(/c/) + a = a + 1 + !$omp end parallel + end subroutine +end subroutine