Hi! Mentioning an automatic variable in #pragma omp declare target to or link clauses doesn't make any sense, but I can't find anything in OpenMP that would say it is invalid.
Thus the following patch accepts it and ignores (except for the diagnostics when a var is mentioned in both to and link or multiple times on the same directive - thus I'm afraid we can't avoid adding the attribute). Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk. 2017-02-09 Jakub Jelinek <ja...@redhat.com> PR c/79431 * gimplify.c (gimplify_adjust_omp_clauses): Ignore "omp declare target link" attribute unless is_global_var. * omp-offload.c (find_link_var_op): Likewise. c/ * c-parser.c (c_parser_omp_declare_target): Don't invoke symtab_node::get on automatic variables. cp/ * parser.c (cp_parser_oacc_declare): Formatting fix. (cp_parser_omp_declare_target): Don't invoke symtab_node::get on automatic variables. testsuite/ * c-c++-common/gomp/pr79431.c: New test. --- gcc/gimplify.c.jj 2017-01-26 12:04:02.000000000 +0100 +++ gcc/gimplify.c 2017-02-09 10:09:53.507844548 +0100 @@ -8947,8 +8947,9 @@ gimplify_adjust_omp_clauses (gimple_seq if ((ctx->region_type & ORT_TARGET) != 0 && !(n->value & GOVD_SEEN) && GOMP_MAP_ALWAYS_P (OMP_CLAUSE_MAP_KIND (c)) == 0 - && !lookup_attribute ("omp declare target link", - DECL_ATTRIBUTES (decl))) + && (!is_global_var (decl) + || !lookup_attribute ("omp declare target link", + DECL_ATTRIBUTES (decl)))) { remove = true; /* For struct element mapping, if struct is never referenced --- gcc/omp-offload.c.jj 2017-01-21 02:26:24.000000000 +0100 +++ gcc/omp-offload.c 2017-02-09 10:13:35.305834982 +0100 @@ -1679,7 +1679,9 @@ find_link_var_op (tree *tp, int *walk_su { tree t = *tp; - if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t) + if (VAR_P (t) + && DECL_HAS_VALUE_EXPR_P (t) + && is_global_var (t) && lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t))) { *walk_subtrees = 0; --- gcc/c/c-parser.c.jj 2017-01-24 23:29:06.000000000 +0100 +++ gcc/c/c-parser.c 2017-02-09 10:06:03.237989683 +0100 @@ -16847,8 +16847,11 @@ c_parser_omp_declare_target (c_parser *p } if (!at1) { - symtab_node *node = symtab_node::get (t); DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t)); + if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t)) + continue; + + symtab_node *node = symtab_node::get (t); if (node != NULL) { node->offloadable = 1; --- gcc/cp/parser.c.jj 2017-02-07 16:40:42.000000000 +0100 +++ gcc/cp/parser.c 2017-02-09 10:07:38.019687417 +0100 @@ -36219,7 +36219,7 @@ cp_parser_oacc_declare (cp_parser *parse id = get_identifier ("omp declare target"); DECL_ATTRIBUTES (decl) - = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl)); + = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl)); if (global_bindings_p ()) { symtab_node *node = symtab_node::get (decl); @@ -36759,8 +36759,11 @@ cp_parser_omp_declare_target (cp_parser } if (!at1) { - symtab_node *node = symtab_node::get (t); DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t)); + if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t)) + continue; + + symtab_node *node = symtab_node::get (t); if (node != NULL) { node->offloadable = 1; --- gcc/testsuite/c-c++-common/gomp/pr79431.c.jj 2017-02-09 10:18:03.538195351 +0100 +++ gcc/testsuite/c-c++-common/gomp/pr79431.c 2017-02-09 10:15:20.000000000 +0100 @@ -0,0 +1,8 @@ +/* PR c/79431 */ + +void +foo (void) +{ + int a; + #pragma omp declare target (a) +} Jakub