Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src tests/lint: add tests for GCC __attribute__



details:   https://anonhg.NetBSD.org/src/rev/1b9f4f8c218c
branches:  trunk
changeset: 1022162:1b9f4f8c218c
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Jul 06 17:33:07 2021 +0000

description:
tests/lint: add tests for GCC __attribute__

Before fixing the wrong handling of __attribute__ that is demonstrated
at the end of gcc_attribute.c, ensure that the attribute handling works
in the most basic cases.

Lint currently accepts __attribute__ in more places than it should.
This leads to some ambiguities in the grammar.

diffstat:

 distrib/sets/lists/tests/mi                       |  14 ++++++++-
 tests/usr.bin/xlint/lint1/Makefile                |  14 ++++++++-
 tests/usr.bin/xlint/lint1/gcc_attribute.c         |   8 +----
 tests/usr.bin/xlint/lint1/gcc_attribute.exp       |   2 +-
 tests/usr.bin/xlint/lint1/gcc_attribute_enum.c    |  22 ++++++++++++++
 tests/usr.bin/xlint/lint1/gcc_attribute_enum.exp  |   1 +
 tests/usr.bin/xlint/lint1/gcc_attribute_func.c    |  26 ++++++++++++++++
 tests/usr.bin/xlint/lint1/gcc_attribute_func.exp  |   1 +
 tests/usr.bin/xlint/lint1/gcc_attribute_label.c   |  31 +++++++++++++++++++
 tests/usr.bin/xlint/lint1/gcc_attribute_label.exp |   4 ++
 tests/usr.bin/xlint/lint1/gcc_attribute_stmt.c    |  36 +++++++++++++++++++++++
 tests/usr.bin/xlint/lint1/gcc_attribute_stmt.exp  |   1 +
 tests/usr.bin/xlint/lint1/gcc_attribute_type.c    |  29 ++++++++++++++++++
 tests/usr.bin/xlint/lint1/gcc_attribute_type.exp  |   1 +
 tests/usr.bin/xlint/lint1/gcc_attribute_var.c     |  28 +++++++++++++++++
 tests/usr.bin/xlint/lint1/gcc_attribute_var.exp   |   1 +
 16 files changed, 209 insertions(+), 10 deletions(-)

diffs (truncated from 313 to 300 lines):

diff -r b3bf1f3a59e3 -r 1b9f4f8c218c distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Tue Jul 06 16:02:44 2021 +0000
+++ b/distrib/sets/lists/tests/mi       Tue Jul 06 17:33:07 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1076 2021/07/04 20:22:31 rillig Exp $
+# $NetBSD: mi,v 1.1077 2021/07/06 17:33:07 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -6219,6 +6219,18 @@
 ./usr/tests/usr.bin/xlint/lint1/gcc_attribute.exp              tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c                tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.exp      tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_enum.c           tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_enum.exp         tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_func.c           tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_func.exp         tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_label.c          tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_label.exp                tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_stmt.c           tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_stmt.exp         tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_type.c           tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_type.exp         tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_var.c            tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/gcc_attribute_var.exp          tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/gcc_bit_field_types.c          tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/gcc_bit_field_types.exp                tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/gcc_init_compound_literal.c    tests-usr.bin-tests     compattestfile,atf
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/Makefile
--- a/tests/usr.bin/xlint/lint1/Makefile        Tue Jul 06 16:02:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/Makefile        Tue Jul 06 17:33:07 2021 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.79 2021/07/04 20:22:31 rillig Exp $
+# $NetBSD: Makefile,v 1.80 2021/07/06 17:33:07 rillig Exp $
 
 NOMAN=         # defined
 MAX_MESSAGE=   345             # see lint1/err.c
@@ -121,6 +121,18 @@
 FILES+=                gcc_attribute.exp
 FILES+=                gcc_attribute_aligned.c
 FILES+=                gcc_attribute_aligned.exp
+FILES+=                gcc_attribute_enum.c
+FILES+=                gcc_attribute_enum.exp
+FILES+=                gcc_attribute_func.c
+FILES+=                gcc_attribute_func.exp
+FILES+=                gcc_attribute_label.c
+FILES+=                gcc_attribute_label.exp
+FILES+=                gcc_attribute_stmt.c
+FILES+=                gcc_attribute_stmt.exp
+FILES+=                gcc_attribute_type.c
+FILES+=                gcc_attribute_type.exp
+FILES+=                gcc_attribute_var.c
+FILES+=                gcc_attribute_var.exp
 FILES+=                gcc_bit_field_types.c
 FILES+=                gcc_bit_field_types.exp
 FILES+=                gcc_init_compound_literal.c
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute.c
--- a/tests/usr.bin/xlint/lint1/gcc_attribute.c Tue Jul 06 16:02:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute.c Tue Jul 06 17:33:07 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gcc_attribute.c,v 1.6 2021/07/06 06:38:29 rillig Exp $ */
+/*     $NetBSD: gcc_attribute.c,v 1.7 2021/07/06 17:33:07 rillig Exp $ */
 # 3 "gcc_attribute.c"
 
 /*
@@ -6,12 +6,6 @@
  * provided by GCC.
  *
  * https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html
- * https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
- * https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html
- * https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
- * https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html
- * https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html
- * https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html
  */
 
 void __attribute__((noinline))
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute.exp
--- a/tests/usr.bin/xlint/lint1/gcc_attribute.exp       Tue Jul 06 16:02:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute.exp       Tue Jul 06 17:33:07 2021 +0000
@@ -1,1 +1,1 @@
-gcc_attribute.c(39): error: syntax error 'unknown_attribute' [249]
+gcc_attribute.c(33): error: syntax error 'unknown_attribute' [249]
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_enum.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_enum.c    Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,22 @@
+/*     $NetBSD: gcc_attribute_enum.c,v 1.1 2021/07/06 17:33:07 rillig Exp $    */
+# 3 "gcc_attribute_enum.c"
+
+/*
+ * Tests for the GCC __attribute__ for enumerators.
+ *
+ * https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html
+ */
+
+enum Planet {
+       Mercury,
+       Venus,
+       Earth,
+       Mars,
+       Jupiter,
+       Saturn,
+       Uranus,
+       Neptune,
+       /* https://en.wikipedia.org/wiki/Pluto_(planet) */
+       /*FIXME*//* expect+1: error: syntax error '__attribute__' [249] */
+       Pluto __attribute__((__deprecated__ /* since August 2006 */))
+};
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_enum.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_enum.exp  Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,1 @@
+gcc_attribute_enum.c(21): error: syntax error '__attribute__' [249]
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_func.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_func.c    Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,26 @@
+/*     $NetBSD: gcc_attribute_func.c,v 1.1 2021/07/06 17:33:07 rillig Exp $    */
+# 3 "gcc_attribute_func.c"
+
+/*
+ * Tests for the GCC __attribute__ for functions.
+ *
+ * https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
+ */
+
+void deprecated_function(void)
+    __attribute__((__noreturn__))
+    __attribute__((__aligned__(8), __cold__))
+    __attribute__((__deprecated__("do not use while driving")));
+
+__attribute__((__cold__))
+void attribute_as_prefix(void);
+
+void __attribute__((__cold__)) attribute_after_type_spec(void);
+void *__attribute__((__cold__)) attribute_before_name(void);
+/*TODO: do not allow __attribute__ after function name */
+void *attribute_after_name __attribute__((__cold__))(void);
+void *attribute_after_parameters(void) __attribute__((__cold__));
+
+/* just to trigger _some_ error, to keep the .exp file */
+/* expect+1: error: syntax error 'syntax_error' [249] */
+__attribute__((syntax_error));
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_func.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_func.exp  Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,1 @@
+gcc_attribute_func.c(26): error: syntax error 'syntax_error' [249]
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_label.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_label.c   Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,31 @@
+/*     $NetBSD: gcc_attribute_label.c,v 1.1 2021/07/06 17:33:07 rillig Exp $   */
+# 3 "gcc_attribute_label.c"
+
+/*
+ * Tests for the GCC __attribute__ for labels.
+ *
+ * https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html
+ */
+
+void dead(void);
+
+void
+test(int i)
+{
+       if (i < 1000)
+               goto hot;
+       /* TODO: add quotes to message 232 */
+       /*FIXME*//* expect+1: warning: label error unused in function test [232] */
+error:
+       __attribute__((__cold__));
+       dead();
+
+hot:
+       /* expect+1: error: syntax error '__hot__' [249] */
+       __attribute__((__hot__));
+       /*FIXME*//* expect+1: error: 'i' undefined [99] */
+       if (i < 0)
+               /* TODO: add quotes to message 23 */
+               /* expect+1: warning: undefined label error [23] */
+               goto error;
+}
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_label.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_label.exp Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,4 @@
+gcc_attribute_label.c(25): error: syntax error '__hot__' [249]
+gcc_attribute_label.c(27): error: 'i' undefined [99]
+gcc_attribute_label.c(19): warning: label error unused in function test [232]
+gcc_attribute_label.c(30): warning: undefined label error [23]
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_stmt.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_stmt.c    Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,36 @@
+/*     $NetBSD: gcc_attribute_stmt.c,v 1.1 2021/07/06 17:33:07 rillig Exp $    */
+# 3 "gcc_attribute_stmt.c"
+
+/*
+ * Tests for the GCC __attribute__ for statements.
+ *
+ * https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html
+ */
+
+void println(const char *);
+
+void
+attribute_fallthrough(int i)
+{
+       switch (i) {
+       case 5:
+               /*
+                * The attribute 'fallthrough' is only valid after a
+                * preceding statement. This is already caught by GCC, so
+                * lint does not need to care.
+                */
+               __attribute__((__fallthrough__));
+       case 3:
+               println("odd");
+               __attribute__((__fallthrough__));
+       case 2:
+               /*
+                * Only the null statement can have the attribute
+                * 'fallthrough'. This is already caught by GCC, so
+                * lint does not need to care.
+                */
+               /* expect+2: error: syntax error '__attribute__' [249] */
+               println("prime")
+                   __attribute__((__fallthrough__));
+       }
+}
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_stmt.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_stmt.exp  Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,1 @@
+gcc_attribute_stmt.c(34): error: syntax error '__attribute__' [249]
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_type.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_type.c    Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,29 @@
+/*     $NetBSD: gcc_attribute_type.c,v 1.1 2021/07/06 17:33:07 rillig Exp $    */
+# 3 "gcc_attribute_type.c"
+
+/*
+ * Tests for the GCC __attribute__ for types.
+ *
+ * https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
+ */
+
+struct __attribute__((__packed__)) packed_record {
+       unsigned char len[2];
+       unsigned long magic;
+};
+
+struct record_packed {
+       unsigned char len[2];
+       unsigned long magic;
+} __attribute__((__packed__));
+
+/* TODO: do not allow __attribute__ before 'struct' */
+__attribute__((__packed__))
+struct attribute_before_keyword {
+       unsigned char len[2];
+       unsigned long magic;
+};
+
+/* just to trigger _some_ error, to keep the .exp file */
+/* expect+1: error: syntax error 'syntax_error' [249] */
+__attribute__((syntax_error));
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_type.exp
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_type.exp  Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,1 @@
+gcc_attribute_type.c(29): error: syntax error 'syntax_error' [249]
diff -r b3bf1f3a59e3 -r 1b9f4f8c218c tests/usr.bin/xlint/lint1/gcc_attribute_var.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/gcc_attribute_var.c     Tue Jul 06 17:33:07 2021 +0000
@@ -0,0 +1,28 @@
+/*     $NetBSD: gcc_attribute_var.c,v 1.1 2021/07/06 17:33:07 rillig Exp $     */
+# 3 "gcc_attribute_var.c"
+
+/*
+ * Tests for the GCC __attribute__ for variables.
+ *
+ * https://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html
+ */
+
+void
+write_to_page(unsigned index, char ch)
+{
+       static char page[4096]
+           __attribute__((__aligned__(4096)));
+
+       page[index] = ch;
+}
+
+void
+placement(



Home | Main Index | Thread Index | Old Index