From: Alexey Bataev Date: Fri, 16 Feb 2018 21:23:23 +0000 (+0000) Subject: [OPENMP] Do not emit messages for templates in declare target X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=16003bcdb4287aab3b87300d9e95f9b49ce52c1c;p=clang [OPENMP] Do not emit messages for templates in declare target constructs. The compiler may emit some extra warnings for functions, that are implicit specialization of the templates, declared in the target region. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325391 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaOpenMP.cpp b/lib/Sema/SemaOpenMP.cpp index a712a81ee7..88c993d0f7 100644 --- a/lib/Sema/SemaOpenMP.cpp +++ b/lib/Sema/SemaOpenMP.cpp @@ -12798,7 +12798,7 @@ static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, Sema &SemaRef, Decl *D) { if (!D) return; - Decl *LD = nullptr; + const Decl *LD = nullptr; if (isa(D)) { LD = cast(D)->getDefinition(); } else if (isa(D)) { @@ -12814,22 +12814,29 @@ static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, ML->DeclarationMarkedOpenMPDeclareTarget(D, A); return; } - - } else if (isa(D)) { + } else if (auto *F = dyn_cast(D)) { const FunctionDecl *FD = nullptr; - if (cast(D)->hasBody(FD)) - LD = const_cast(FD); - - // If the definition is associated with the current declaration in the - // target region (it can be e.g. a lambda) that is legal and we do not need - // to do anything else. - if (LD == D) { - Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( - SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); - D->addAttr(A); - if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) - ML->DeclarationMarkedOpenMPDeclareTarget(D, A); - return; + if (cast(D)->hasBody(FD)) { + LD = FD; + // If the definition is associated with the current declaration in the + // target region (it can be e.g. a lambda) that is legal and we do not + // need to do anything else. + if (LD == D) { + Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( + SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To); + D->addAttr(A); + if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener()) + ML->DeclarationMarkedOpenMPDeclareTarget(D, A); + return; + } + } else if (F->isFunctionTemplateSpecialization() && + F->getTemplateSpecializationKind() == + TSK_ImplicitInstantiation) { + // Check if the function is implicitly instantiated from the template + // defined in the declare target region. + const FunctionTemplateDecl *FTD = F->getPrimaryTemplate(); + if (FTD && FTD->hasAttr()) + return; } } if (!LD) @@ -12841,7 +12848,7 @@ static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR, SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context); SemaRef.Diag(SL, diag::note_used_here) << SR; } else { - DeclContext *DC = LD->getDeclContext(); + const DeclContext *DC = LD->getDeclContext(); while (DC) { if (isa(DC) && cast(DC)->hasAttr()) @@ -12894,7 +12901,8 @@ void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, if ((E || !VD->getType()->isIncompleteType()) && !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) { // Mark decl as declared target to prevent further diagnostic. - if (isa(VD) || isa(VD)) { + if (isa(VD) || isa(VD) || + isa(VD)) { Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( Context, OMPDeclareTargetDeclAttr::MT_To); VD->addAttr(A); @@ -12914,10 +12922,21 @@ void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D, return; } } + if (auto *FTD = dyn_cast(D)) { + if (FTD->hasAttr() && + (FTD->getAttr()->getMapType() == + OMPDeclareTargetDeclAttr::MT_Link)) { + assert(IdLoc.isValid() && "Source location is expected"); + Diag(IdLoc, diag::err_omp_function_in_link_clause); + Diag(FTD->getLocation(), diag::note_defined_here) << FTD; + return; + } + } if (!E) { // Checking declaration inside declare target region. if (!D->hasAttr() && - (isa(D) || isa(D))) { + (isa(D) || isa(D) || + isa(D))) { Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit( Context, OMPDeclareTargetDeclAttr::MT_To); D->addAttr(A); diff --git a/test/OpenMP/declare_target_messages.cpp b/test/OpenMP/declare_target_messages.cpp index 4615dbdae4..3286a29dc4 100644 --- a/test/OpenMP/declare_target_messages.cpp +++ b/test/OpenMP/declare_target_messages.cpp @@ -33,6 +33,33 @@ struct NonT { typedef int sint; +template +T bla1() { return 0; } + +#pragma omp declare target +template +T bla2() { return 0; } +#pragma omp end declare target + +template<> +float bla2() { return 1.0; } + +#pragma omp declare target +void blub2() { + bla2(); + bla2(); +} +#pragma omp end declare target + +void t2() { +#pragma omp target + { + bla2(); + bla2(); + } +} + + #pragma omp declare target // expected-note {{to match this '#pragma omp declare target'}} #pragma omp threadprivate(a) // expected-note {{defined as threadprivate or thread local}} extern int b;