diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 14db7bf55695..4792ae33591b 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -8710,6 +8710,8 @@ def err_omp_declare_target_to_and_link : Error< 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" diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 51c2bb72305c..47cea3029bd9 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -8656,7 +8656,8 @@ public: OMPDeclareTargetDeclAttr::MapTypeTy MT, NamedDeclSetType &SameDirectiveDecls); /// Check declaration inside target region. - void checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D); + void checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, + SourceLocation IdLoc = SourceLocation()); /// Return true inside OpenMP declare target region. bool isInOpenMPDeclareTargetContext() const { return IsInOpenMPDeclareTargetContext; diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp index 55a4133901c6..270f1c88a105 100644 --- a/clang/lib/Sema/SemaOpenMP.cpp +++ b/clang/lib/Sema/SemaOpenMP.cpp @@ -12600,7 +12600,7 @@ void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, ND->addAttr(A); if (ASTMutationListener *ML = Context.getASTMutationListener()) ML->DeclarationMarkedOpenMPDeclareTarget(ND, A); - checkDeclIsAllowedInOpenMPTarget(nullptr, ND); + checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc()); } else if (ND->getAttr()->getMapType() != MT) { Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link) << Id.getName(); @@ -12689,7 +12689,8 @@ static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR, return true; } -void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) { +void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, + SourceLocation IdLoc) { if (!D || D->isInvalidDecl()) return; SourceRange SR = E ? E->getSourceRange() : D->getSourceRange(); @@ -12718,6 +12719,16 @@ void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) { return; } } + if (FunctionDecl *FD = dyn_cast(D)) { + if (FD->hasAttr() && + (FD->getAttr()->getMapType() == + OMPDeclareTargetDeclAttr::MT_Link)) { + assert(IdLoc.isValid() && "Source location is expected"); + Diag(IdLoc, 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() && diff --git a/clang/test/OpenMP/declare_target_ast_print.cpp b/clang/test/OpenMP/declare_target_ast_print.cpp index 55f73cbc4e97..6c59563d1d1e 100644 --- a/clang/test/OpenMP/declare_target_ast_print.cpp +++ b/clang/test/OpenMP/declare_target_ast_print.cpp @@ -108,9 +108,7 @@ void f2() { // 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 @@ void f3() { // 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 diff --git a/clang/test/OpenMP/declare_target_messages.cpp b/clang/test/OpenMP/declare_target_messages.cpp index 7b4ba02bd4f7..c1c03951a3c1 100644 --- a/clang/test/OpenMP/declare_target_messages.cpp +++ b/clang/test/OpenMP/declare_target_messages.cpp @@ -19,6 +19,10 @@ void f(); 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 {