https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98487

            Bug ID: 98487
           Summary: ICE: tree check: expected identifier_node, have
                    tree_list in is_attribute_p, at attribs.h:155 [C2X
                    attribute syntax, gnu::format and
                    -Wsuggest-attribute=format]
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Keywords: ice-on-valid-code
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: p...@gcc-bugzilla.mail.kapsi.fi
  Target Milestone: ---

Created attachment 49862
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49862&action=edit
Triggers the ICE.

Hi.

Combining C2X attribute syntax ``[[]]'', gnu::format and
-Wsuggest-attribute=format seems to ICE.

This problem does not occur if a) ``__attribute__'' syntax is used instead, b)
-Wsuggest-attribute=format is not used.  Does not happen when processing
function declarations or if the corresponding library call is not present.

Only ``format(printf, ...)'' and ``format(scanf, ...)'' seem to be affected.


Attached test case.  Using GCC 11.0.0 20201231, aarch64-*-* on
x86_64-w64-mingw32 but I presume this is not host or target related.


Given e.g. PR90953 and PR94733 I presume the problematic line is at:

gcc/c-family/c-format.c:1216:
1216                 if (is_attribute_p ("format", TREE_PURPOSE (c))

and it should use` get_attribute_name' instead.  Attached a patch that does
just that.  Seems to fix the problem, but I'm no expert on GCC internals.


$ cat attribute__format.c [excerpt]
#define ATTR(...) [[gnu::__VA_ARGS__]]
// ...
ATTR(__format__(__printf__, 1, 2))
void
do_printf(const char * const a0, ...) { /* ... */ }

$ gcc -c -std=c2x -Wall -Wextra -Wpedantic -Wsuggest-attribute=format
attribute__format.c

attribute__format.c: In function 'do_printf':
attribute__format.c:17:3: internal compiler error: tree check: expected
identifier_node, have tree_list in is_attribute_p, at attribs.h:155
   17 |   vprintf(a0, ap);
      |   ^~~~~~~

$ gcc -c -std=c2x -Wall -Wextra -Wpedantic -Wsuggest-attribute=format -wrapper
gdb,--args
(gdb) b internal_error
(gdb) run
Thread 1 hit Breakpoint 1, internal_error (
    gmsgid=0x1cc3f90 <(anonymous namespace)::pass_data_vrp+2000> "tree check:
%s, have %s in %s, at %s:%d")
    at <...>/gcc/gcc/diagnostic.c:1764
(gdb) bt
#0  internal_error (
    gmsgid=0x1cc3f90 <(anonymous namespace)::pass_data_vrp+2000> "tree check:
%s, have %s in %s, at %s:%d")
    at <...>/gcc/gcc/diagnostic.c:1764
#1  0x0000000001a9adf2 in tree_check_failed (node=<optimized out>,
    file=0x1bc8580 <type_suffix(tree_node*)::suffixes+1312>
"<...>/gcc/gcc/attribs.h",
    line=<optimized out>, function=0x1bc856d
<type_suffix(tree_node*)::suffixes+1293> "is_attribute_p")
    at <...>/gcc/gcc/tree.c:13438
#2  0x0000000001921e1a in tree_check (__c=<optimized out>, __g=<optimized out>,
__l=<optimized out>,
    __f=<optimized out>, __t=<optimized out>) at <...>/gcc/gcc/tree.h:3594
#3  is_attribute_p (ident=0x1cc3f90 <(anonymous
namespace)::pass_data_vrp+2000>,
    attr_name=0x1bc90fd <type_suffix(tree_node*)::suffixes+4253> "format")
    at <...>/gcc/gcc/attribs.h:155
#4  0x00000000019228d6 in is_attribute_p (attr_name=0x1bc90fd
<type_suffix(tree_node*)::suffixes+4253> "format",
    ident=<optimized out>) at <...>/gcc/gcc/tree.h:3452
#5  check_function_format (fntype=fntype@entry=0x1eea39d8, attrs=<optimized
out>, nargs=nargs@entry=2,
    argarray=argarray@entry=0x1edf9e40, arglocs=arglocs@entry=0x1ad0e878)
    at <...>/gcc/gcc/c-family/c-format.c:1216
#6  0x00000000004cbea6 in check_function_arguments (loc=loc@entry=34268454,
fndecl=fndecl@entry=0x1ec0b900,
    fntype=fntype@entry=0x1eea39d8, nargs=<optimized out>, nargs@entry=2,
argarray=argarray@entry=0x1edf9e40,
    arglocs=arglocs@entry=0x1ad0e878) at <...>/gcc/gcc/tree.h:3718
#7  0x000000000044c701 in build_function_call_vec (loc=<optimized out>,
loc@entry=34268454, arg_loc=arg_loc@entry=...,
    function=<optimized out>, function@entry=0x1ec0b900, params=<optimized
out>, origtypes=<optimized out>,
    origtypes@entry=0x1eeddb90, orig_fundecl=0x1ec0b900,
orig_fundecl@entry=0x0)
    at <...>/gcc/gcc/c/c-typeck.c:3144
#8  0x000000000044d4fa in c_build_function_call_vec (loc=loc@entry=34268454,
arg_loc=...,
    function=function@entry=0x1ec0b900, params=<optimized out>,
params@entry=0x1edf9e38, origtypes=0x1eeddb90)
    at <...>/gcc/gcc/c/c-typeck.c:3210
#9  0x000000000046ea76 in c_parser_postfix_expression_after_primary
(parser=parser@entry=0x1ead4240,
    expr_loc=expr_loc@entry=34268454, expr=...) at
<...>/gcc/gcc/c/c-parser.c:10469
#10 0x0000000000465724 in c_parser_postfix_expression (parser=<optimized out>,
parser@entry=0x1ead4240)
    at <...>/gcc/gcc/../libcpp/include/line-map.h:577
#11 0x0000000000469fdb in c_parser_unary_expression
(parser=parser@entry=0x1ead4240)
    at <...>/gcc/gcc/c/c-parser.c:8230
#12 0x000000000046ba20 in c_parser_cast_expression
(parser=parser@entry=0x1ead4240, after=after@entry=0x0)
    at <...>/gcc/gcc/c/c-parser.c:8072
#13 0x000000000046bcf6 in c_parser_binary_expression
(parser=parser@entry=0x1ead4240, after=0x0, omp_atomic_lhs=0x0)
    at <...>/gcc/gcc/c/c-parser.c:7875
#14 0x000000000046d09e in c_parser_conditional_expression
(parser=parser@entry=0x1ead4240, after=<optimized out>,
    omp_atomic_lhs=<optimized out>) at <...>/gcc/gcc/c/c-parser.c:7598
#15 0x000000000046d7c9 in c_parser_expr_no_commas
(parser=parser@entry=0x1ead4240, after=after@entry=0x0,
    omp_atomic_lhs=omp_atomic_lhs@entry=0x0) at <...>/gcc/gcc/c/c-parser.c:7513
#16 0x000000000046da8d in c_parser_expression (parser=parser@entry=0x1ead4240)
    at <...>/gcc/gcc/c/c-parser.c:10606
#17 0x000000000046e34a in c_parser_expression_conv (parser=0x1ead4240)
    at <...>/gcc/gcc/c/c-parser.c:10645
#18 0x0000000000462b14 in c_parser_statement_after_labels
(parser=parser@entry=0x1ead4240, if_p=if_p@entry=0x0,
    chain=chain@entry=0x0) at <...>/gcc/gcc/c/c-parser.c:6249
#19 0x0000000000465228 in c_parser_compound_statement_nostart
(parser=parser@entry=0x1ead4240)
    at <...>/gcc/gcc/c/c-parser.c:5788
#20 0x00000000004850cb in c_parser_compound_statement
(parser=parser@entry=0x1ead4240,
    endlocp=endlocp@entry=0x1ad0f990) at <...>/gcc/gcc/c/c-parser.c:5597
#21 0x0000000000486d9b in c_parser_declaration_or_fndef (parser=0x1ead4240,
fndef_ok=<optimized out>,
    static_assert_ok=<optimized out>, empty_ok=<optimized out>,
nested=<optimized out>, start_attr_ok=<optimized out>,
    objc_foreach_object_declaration=<optimized out>,
omp_declare_simd_clauses=..., have_attrs=<optimized out>,
    attrs=<optimized out>, oacc_routine_data=<optimized out>,
fallthru_attr_p=<optimized out>)
    at <...>/gcc/gcc/c/c-parser.c:2539
#22 0x000000000048f97e in c_parser_external_declaration (parser=0x1ead4240)
    at <...>/gcc/gcc/c/c-parser.c:1777
#23 0x000000000049053c in c_parser_translation_unit (parser=<optimized out>)
    at <...>/gcc/gcc/c/c-parser.c:1650
#24 c_parse_file () at <...>/gcc/gcc/c/c-parser.c:21935
#25 0x00000000004f74ff in c_common_parse_file () at
<...>/gcc/gcc/c-family/c-opts.c:1211
#26 0x0000000000a11bd2 in compile_file () at <...>/gcc/gcc/toplev.c:457
#27 0x0000000001a08d58 in do_compile () at <...>/gcc/gcc/toplev.c:2193
#28 toplev::main (this=this@entry=0x1ad0fe0e, argc=<optimized out>,
argc@entry=21, argv=<optimized out>,
    argv@entry=0x1e25f0) at <...>/gcc/gcc/toplev.c:2332
#29 0x0000000001b70737 in main (argc=21, argv=0x1e25f0) at
<...>/gcc/gcc/main.c:39

Reply via email to