Home | History | Annotate | Line # | Download | only in lint1
gcc_attribute.c revision 1.12
      1  1.12  rillig /*	$NetBSD: gcc_attribute.c,v 1.12 2022/08/25 19:03:48 rillig Exp $	*/
      2   1.1  rillig # 3 "gcc_attribute.c"
      3   1.1  rillig 
      4   1.1  rillig /*
      5   1.1  rillig  * Tests for the various attributes for functions, types, statements that are
      6   1.1  rillig  * provided by GCC.
      7   1.1  rillig  *
      8   1.1  rillig  * https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html
      9   1.1  rillig  */
     10   1.1  rillig 
     11   1.1  rillig void __attribute__((noinline))
     12   1.1  rillig do_not_inline(void)
     13   1.1  rillig {
     14   1.1  rillig }
     15   1.1  rillig 
     16   1.2  rillig /* All pointer arguments must be nonnull. */
     17   1.2  rillig void __attribute__((nonnull))
     18   1.2  rillig function_nonnull(void *, const void *, int);
     19   1.2  rillig 
     20   1.3  rillig /*
     21   1.3  rillig  * The documentation suggests that the argument list of nonnull be nonempty,
     22   1.3  rillig  * but GCC 9.3.0 accepts an empty list as well, treating all parameters as
     23   1.3  rillig  * nonnull.
     24   1.3  rillig  */
     25   1.3  rillig void __attribute__((nonnull()))
     26   1.3  rillig function_nonnull_list(void *, const void *, int);
     27   1.3  rillig 
     28   1.2  rillig /* Arguments 1 and 2 must be nonnull. */
     29   1.1  rillig void __attribute__((nonnull(1, 2)))
     30   1.2  rillig function_nonnull_list(void *, const void *, int);
     31   1.1  rillig 
     32  1.12  rillig /*
     33  1.12  rillig  * Unknown attributes are skipped, as lint does not have a list of all known
     34  1.12  rillig  * GCC attributes.
     35  1.12  rillig  */
     36   1.1  rillig void __attribute__((unknown_attribute))
     37   1.1  rillig function_with_unknown_attribute(void);
     38   1.4  rillig 
     39   1.4  rillig /*
     40   1.4  rillig  * There is an attribute called 'pcs', but that attribute must not prevent an
     41   1.5  rillig  * ordinary variable from being named the same.  Starting with scan.l 1.77
     42   1.5  rillig  * from 2017-01-07, that variable name generated a syntax error.  Fixed in
     43   1.5  rillig  * lex.c 1.33 from 2021-05-03.
     44   1.4  rillig  *
     45   1.4  rillig  * Seen in yds.c, function yds_allocate_slots.
     46   1.4  rillig  */
     47   1.5  rillig int
     48   1.4  rillig local_variable_pcs(void)
     49   1.4  rillig {
     50   1.5  rillig 	int pcs = 3;
     51   1.4  rillig 	return pcs;
     52   1.4  rillig }
     53   1.6  rillig 
     54   1.6  rillig /*
     55   1.6  rillig  * FIXME: The attributes are handled by different grammar rules even though
     56   1.6  rillig  *  they occur in the same syntactical position.
     57   1.6  rillig  *
     58   1.6  rillig  * Grammar rule abstract_decl_param_list handles the first attribute.
     59   1.6  rillig  *
     60   1.6  rillig  * Grammar rule direct_abstract_declarator handles all remaining attributes.
     61   1.6  rillig  *
     62   1.6  rillig  * Since abstract_decl_param_list contains type_attribute_opt, this could be
     63   1.6  rillig  * the source of the many shift/reduce conflicts in the grammar.
     64   1.6  rillig  */
     65   1.6  rillig int
     66   1.6  rillig func(
     67   1.6  rillig     int(int)
     68   1.6  rillig     __attribute__((__noreturn__))
     69   1.6  rillig     __attribute__((__noreturn__))
     70   1.6  rillig );
     71   1.8  rillig 
     72   1.8  rillig /*
     73   1.8  rillig  * https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html says that the
     74   1.8  rillig  * attribute-list is a "possibly empty comma-separated sequence of
     75   1.8  rillig  * attributes".
     76   1.8  rillig  *
     77   1.8  rillig  * No matter whether this particular example is interpreted as an empty list
     78   1.8  rillig  * or a list containing a single empty attribute, the result is the same in
     79   1.8  rillig  * both cases.
     80   1.8  rillig  */
     81   1.8  rillig void one_empty_attribute(void)
     82   1.8  rillig     __attribute__((/* none */));
     83   1.8  rillig 
     84   1.8  rillig /*
     85   1.8  rillig  * https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html further says that
     86   1.8  rillig  * each individual attribute may be "Empty. Empty attributes are ignored".
     87   1.8  rillig  */
     88   1.8  rillig void two_empty_attributes(void)
     89   1.8  rillig     __attribute__((/* none */, /* still none */));
     90   1.9  rillig 
     91   1.9  rillig /*
     92   1.9  rillig  * Ensure that __attribute__ can be specified everywhere in a declaration.
     93   1.9  rillig  * This is the simplest possible requirement that covers all valid code.
     94   1.9  rillig  * It accepts invalid code as well, but these cases are covered by GCC and
     95   1.9  rillig  * Clang already.
     96   1.9  rillig  *
     97   1.9  rillig  * Since lint only parses the attributes but doesn't really relate them to
     98   1.9  rillig  * identifiers or other entities, ensuring that valid code can be parsed is
     99   1.9  rillig  * enough for now.
    100   1.9  rillig  *
    101   1.9  rillig  * To really associate __attribute__ with the corresponding entity, the
    102   1.9  rillig  * grammar needs to be rewritten, see the example with __noreturn__ above.
    103   1.9  rillig  */
    104   1.9  rillig __attribute__((deprecated("d1")))
    105   1.9  rillig const
    106   1.9  rillig __attribute__((deprecated("d2")))
    107   1.9  rillig int
    108   1.9  rillig __attribute__((deprecated("d3")))
    109   1.9  rillig *
    110   1.9  rillig // The below line would produce a syntax error.
    111   1.9  rillig // __attribute__((deprecated("d3")))
    112   1.9  rillig const
    113   1.9  rillig __attribute__((deprecated("d4")))
    114   1.9  rillig identifier
    115   1.9  rillig __attribute__((deprecated("d5")))
    116   1.9  rillig (
    117   1.9  rillig     __attribute__((deprecated("d6")))
    118   1.9  rillig     void
    119   1.9  rillig     __attribute__((deprecated("d7")))
    120   1.9  rillig     )
    121   1.9  rillig     __attribute__((deprecated("d8")))
    122   1.9  rillig ;
    123  1.10  rillig 
    124  1.10  rillig /*
    125  1.10  rillig  * The attribute 'const' provides stronger guarantees than 'pure', and
    126  1.10  rillig  * 'volatile' is not defined.  To keep the grammar simple, any T_QUAL is
    127  1.10  rillig  * allowed at this point, but only syntactically.
    128  1.10  rillig  */
    129  1.10  rillig int const_function(int) __attribute__((const));
    130  1.10  rillig /* cover 'gcc_attribute_spec: T_QUAL' */
    131  1.11  rillig /* expect+1: error: syntax error 'volatile' [249] */
    132  1.10  rillig int volatile_function(int) __attribute__((volatile));
    133