Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -8709,6 +8709,8 @@ def warn_omp_not_in_target_context : Warning< "declaration is not declared in any declare target region">, InGroup; +def err_omp_function_in_link_clause : Error< + "function name is not allowed in 'link' clause">; def err_omp_aligned_expected_array_or_ptr : Error< "argument of aligned clause should be array" "%select{ or pointer|, pointer, reference to array or reference to pointer}1" Index: include/clang/Sema/Sema.h =================================================================== --- include/clang/Sema/Sema.h +++ include/clang/Sema/Sema.h @@ -8652,7 +8652,8 @@ OMPDeclareTargetDeclAttr::MapTypeTy MT, NamedDeclSetType &SameDirectiveDecls); /// Check declaration inside target region. - void checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D); + void checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, + const DeclarationNameInfo *Id = nullptr); /// Return true inside OpenMP declare target region. bool isInOpenMPDeclareTargetContext() const { return IsInOpenMPDeclareTargetContext; Index: lib/Sema/SemaOpenMP.cpp =================================================================== --- lib/Sema/SemaOpenMP.cpp +++ lib/Sema/SemaOpenMP.cpp @@ -12578,7 +12578,7 @@ ND->addAttr(A); if (ASTMutationListener *ML = Context.getASTMutationListener()) ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); - checkDeclIsAllowedInOpenMPTarget(nullptr, ND); + checkDeclIsAllowedInOpenMPTarget(nullptr, ND, &Id); } else if (ND->getAttr()->getMapType() != MT) { Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) << Id.getName(); @@ -12667,7 +12667,8 @@ return true; } -void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) { +void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, + const DeclarationNameInfo *Id) { if (!D || D->isInvalidDecl()) return; SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); @@ -12696,6 +12697,16 @@ return; } } + if (FunctionDecl *FD = dyn_cast(D)) { + if (FD->hasAttr() && + (FD->getAttr()->getMapType() == + OMPDeclareTargetDeclAttr::MT_Link)) { + assert(Id && "Null decl name info on link clause."); + Diag(Id->getLoc(), diag::err_omp_function_in_link_clause); + Diag(FD->getLocation(), diag::note_defined_here) << FD; + return; + } + } if (!E) { // Checking declaration inside declare target region. if (!D->hasAttr() && Index: test/OpenMP/declare_target_ast_print.cpp =================================================================== --- test/OpenMP/declare_target_ast_print.cpp +++ test/OpenMP/declare_target_ast_print.cpp @@ -108,9 +108,7 @@ // CHECK: #pragma omp end declare target int c1, c2, c3; -void f3() { -} -#pragma omp declare target link(c1) link(c2), link(c3, f3) +#pragma omp declare target link(c1) link(c2), link(c3) // CHECK: #pragma omp declare target link // CHECK: int c1; // CHECK: #pragma omp end declare target @@ -120,9 +118,6 @@ // CHECK: #pragma omp declare target link // CHECK: int c3; // CHECK: #pragma omp end declare target -// CHECK: #pragma omp declare target link -// CHECK: void f3() -// CHECK: #pragma omp end declare target struct SSSt { #pragma omp declare target Index: test/OpenMP/declare_target_messages.cpp =================================================================== --- test/OpenMP/declare_target_messages.cpp +++ test/OpenMP/declare_target_messages.cpp @@ -19,6 +19,10 @@ void c(); // expected-warning {{declaration is not declared in any declare target region}} +void func() {} // expected-note {{'func' defined here}} + +#pragma omp declare target link(func) // expected-error {{function name is not allowed in 'link' clause}} + extern int b; struct NonT {