c-family: Fix ICE with -Wsuggest-attribute [PR98487]
authorMarek Polacek <polacek@redhat.com>
Fri, 16 Dec 2022 17:28:43 +0000 (12:28 -0500)
committerMarek Polacek <polacek@redhat.com>
Mon, 19 Dec 2022 16:16:42 +0000 (11:16 -0500)
Here we crash because check_function_format was using TREE_PURPOSE
directly rather than using get_attribute_name.

PR c/98487

gcc/c-family/ChangeLog:

* c-format.cc (check_function_format): Use get_attribute_name.

gcc/testsuite/ChangeLog:

* c-c++-common/Wsuggest-attribute-1.c: New test.

gcc/c-family/c-format.cc
gcc/testsuite/c-c++-common/Wsuggest-attribute-1.c [new file with mode: 0644]

index 01adea4..08643c5 100644 (file)
@@ -1215,7 +1215,7 @@ check_function_format (const_tree fn, tree attrs, int nargs,
              for (c = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl));
                   c;
                   c = TREE_CHAIN (c))
-               if (is_attribute_p ("format", TREE_PURPOSE (c))
+               if (is_attribute_p ("format", get_attribute_name (c))
                    && (decode_format_type (IDENTIFIER_POINTER
                                            (TREE_VALUE (TREE_VALUE (c))))
                        == info.format_type))
diff --git a/gcc/testsuite/c-c++-common/Wsuggest-attribute-1.c b/gcc/testsuite/c-c++-common/Wsuggest-attribute-1.c
new file mode 100644 (file)
index 0000000..8b5b398
--- /dev/null
@@ -0,0 +1,36 @@
+/* PR c/98487 */
+/* { dg-do compile { target { c || c++11 } } } */
+/* { dg-options "-Wsuggest-attribute=format" } */
+
+#include <stdarg.h>
+
+[[gnu::__format__(__printf__, 1, 2)]]
+void
+do_printf(const char * const a0, ...)
+{
+  va_list ap;
+  va_start(ap, a0);
+  __builtin_vprintf(a0, ap);
+  va_end(ap);
+}
+
+[[gnu::__format__(__scanf__, 1, 2)]]
+void
+do_scanf(const char * const a0, ...)
+{
+  va_list ap;
+  va_start(ap, a0);
+  __builtin_vscanf(a0, ap);
+  va_end(ap);
+}
+
+struct tm;
+
+[[gnu::__format__(__strftime__, 1, 0)]]
+void
+do_strftime(const char * const a0, struct tm * a1)
+{
+  char buff[256];
+  __builtin_strftime(buff, sizeof(buff), a0, a1);
+  __builtin_puts(buff);
+}