diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp --- a/flang/lib/Semantics/resolve-directives.cpp +++ b/flang/lib/Semantics/resolve-directives.cpp @@ -1064,15 +1064,19 @@ Symbol *AccAttributeVisitor::ResolveAccCommonBlockName( const parser::Name *name) { - if (!name) { - return nullptr; - } else if (auto *prev{ - GetContext().scope.parent().FindCommonBlock(name->source)}) { + if (auto *prev{name + ? GetContext().scope.parent().FindCommonBlock(name->source) + : nullptr}) { name->symbol = prev; return prev; - } else { - return nullptr; } + // 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; } void AccAttributeVisitor::ResolveAccObjectList( diff --git a/flang/test/Semantics/OpenACC/acc-resolve04.f90 b/flang/test/Semantics/OpenACC/acc-resolve04.f90 --- a/flang/test/Semantics/OpenACC/acc-resolve04.f90 +++ b/flang/test/Semantics/OpenACC/acc-resolve04.f90 @@ -1,7 +1,22 @@ ! RUN: %flang_fc1 -fopenacc %s +! Check common block resolution. ! Check that symbol are correctly resolved in device, host and self clause. +subroutine sub(a) + implicit none + real :: a(10) + real :: b(10), c(10), d + common/foo/ b, d, c + integer :: i, n + + !$acc declare present(/foo/) + !$acc parallel loop gang vector + do i = 1, n + b(i) = a(i) + c(i) * d + end do +end subroutine + program test_resolve04 real :: a(10), b(10) common /foo/ b, c