LCOV - code coverage report
Current view: top level - gcc/rust - rust-attribs.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 62.4 % 93 58
Test Date: 2026-10-24 16:24:59 Functions: 85.7 % 14 12
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* rust-attribs.c -- Rust attributes handling.
       2              :    Copyright (C) 2015-2026 Free Software Foundation, Inc.
       3              : 
       4              : GCC is free software; you can redistribute it and/or modify
       5              : it under the terms of the GNU General Public License as published by
       6              : the Free Software Foundation; either version 3, or (at your option)
       7              : any later version.
       8              : 
       9              : GCC is distributed in the hope that it will be useful,
      10              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      11              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12              : GNU General Public License for more details.
      13              : 
      14              : You should have received a copy of the GNU General Public License
      15              : along with GCC; see the file COPYING3.  If not see
      16              : <http://www.gnu.org/licenses/>.  */
      17              : 
      18              : #include "config.h"
      19              : #include "system.h"
      20              : #include "coretypes.h"
      21              : 
      22              : #include "tree.h"
      23              : #include "diagnostic.h"
      24              : #include "tm.h"
      25              : #include "cgraph.h"
      26              : #include "toplev.h"
      27              : #include "target.h"
      28              : #include "common/common-target.h"
      29              : #include "stringpool.h"
      30              : #include "attribs.h"
      31              : #include "varasm.h"
      32              : #include "fold-const.h"
      33              : #include "opts.h"
      34              : #include "attr-handlers.h"
      35              : 
      36              : /* Heavily based on the D frontend Only a subset of the attributes found in the
      37              :  * D frontend have been pulled, the goal being to have the builtin function
      38              :  * correctly setup. It's possible we may need to add extra attributes in the
      39              :  * future.
      40              :  */
      41              : 
      42              : extern const struct scoped_attribute_specs grs_langhook_gnu_attribute_table;
      43              : extern const struct scoped_attribute_specs grs_langhook_common_attribute_table;
      44              : 
      45              : /* Internal attribute handlers for built-in functions.  */
      46              : static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
      47              : static tree rust_handle_leaf_attribute (tree *, tree, tree, int, bool *);
      48              : static tree rust_handle_const_attribute (tree *, tree, tree, int, bool *);
      49              : static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
      50              : static tree rust_handle_pure_attribute (tree *, tree, tree, int, bool *);
      51              : static tree rust_handle_novops_attribute (tree *, tree, tree, int, bool *);
      52              : static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
      53              : static tree rust_handle_nothrow_attribute (tree *, tree, tree, int, bool *);
      54              : static tree handle_transaction_pure_attribute (tree *, tree, tree, int, bool *);
      55              : static tree rust_handle_returns_twice_attribute (tree *, tree, tree, int,
      56              :                                                  bool *);
      57              : static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *);
      58              : 
      59              : /* Rust attribute handlers for user defined attributes.  */
      60              : static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
      61              : static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
      62              : 
      63              : /* Helper to define attribute exclusions.  */
      64              : #define ATTR_EXCL(name, function, type, variable)                              \
      65              :   {                                                                            \
      66              :     name, function, type, variable                                             \
      67              :   }
      68              : 
      69              : // clang-format off
      70              : // Disabling clang-format because it insists in having several ATTR_EXCL() on a
      71              : // single line.
      72              : 
      73              : static const struct attribute_spec::exclusions rust_attr_noreturn_exclusions[]
      74              :   = {
      75              :   //  ATTR_EXCL ("alloc_size", true, true, true),
      76              :   ATTR_EXCL ("const", true, true, true),
      77              :   //  ATTR_EXCL ("malloc", true, true, true),
      78              :   ATTR_EXCL ("pure", true, true, true),
      79              :   ATTR_EXCL ("returns_twice", true, true, true),
      80              :   ATTR_EXCL (NULL, false, false, false),
      81              : };
      82              : 
      83              : static const struct attribute_spec::exclusions rust_attr_const_pure_exclusions[]
      84              :   = {
      85              :   // ATTR_EXCL ("alloc_size", true, true, true),
      86              :   ATTR_EXCL ("const", true, true, true),
      87              :   ATTR_EXCL ("noreturn", true, true, true),
      88              :   ATTR_EXCL ("pure", true, true, true),
      89              :   ATTR_EXCL (NULL, false, false, false)
      90              : };
      91              : 
      92              : // clang-format on
      93              : 
      94              : /* Helper to define an attribute.  */
      95              : #define ATTR_SPEC(name, min_len, max_len, decl_req, type_req, fn_type_req,     \
      96              :                   affects_type_identity, handler, exclude)                     \
      97              :   {                                                                            \
      98              :     name, min_len, max_len, decl_req, type_req, fn_type_req,                   \
      99              :       affects_type_identity, handler, exclude                                  \
     100              :   }
     101              : 
     102              : /* Table of machine-independent attributes.
     103              :    For internal use (marking of built-ins) only.  */
     104              : static const attribute_spec grs_langhook_common_attributes[] = {
     105              :   ATTR_SPEC ("noreturn", 0, 0, true, false, false, false,
     106              :              handle_noreturn_attribute, rust_attr_noreturn_exclusions),
     107              :   ATTR_SPEC ("leaf", 0, 0, true, false, false, false,
     108              :              rust_handle_leaf_attribute, NULL),
     109              :   ATTR_SPEC ("const", 0, 0, true, false, false, false,
     110              :              rust_handle_const_attribute, rust_attr_const_pure_exclusions),
     111              :   ATTR_SPEC ("malloc", 0, 0, true, false, false, false, handle_malloc_attribute,
     112              :              NULL),
     113              :   ATTR_SPEC ("returns_twice", 0, 0, true, false, false, false,
     114              :              rust_handle_returns_twice_attribute,
     115              :              attr_returns_twice_exclusions),
     116              :   ATTR_SPEC ("pure", 0, 0, true, false, false, false,
     117              :              rust_handle_pure_attribute, rust_attr_const_pure_exclusions),
     118              :   ATTR_SPEC ("nonnull", 0, -1, false, true, true, false,
     119              :              handle_nonnull_attribute, NULL),
     120              :   ATTR_SPEC ("nothrow", 0, 0, true, false, false, false,
     121              :              rust_handle_nothrow_attribute, NULL),
     122              :   ATTR_SPEC ("transaction_pure", 0, 0, false, true, true, false,
     123              :              handle_transaction_pure_attribute, NULL),
     124              :   ATTR_SPEC ("no vops", 0, 0, true, false, false, false,
     125              :              rust_handle_novops_attribute, NULL),
     126              :   ATTR_SPEC ("type generic", 0, 0, false, true, true, false,
     127              :              handle_type_generic_attribute, NULL),
     128              :   ATTR_SPEC ("fn spec", 1, 1, false, true, true, false, handle_fnspec_attribute,
     129              :              NULL),
     130              :   ATTR_SPEC ("omp declare simd", 0, -1, true, false, false, false,
     131              :              handle_omp_declare_simd_attribute, NULL),
     132              : };
     133              : 
     134              : const scoped_attribute_specs grs_langhook_common_attribute_table
     135              :   = {"gnu", {grs_langhook_common_attributes}};
     136              : 
     137              : static const attribute_spec grs_langhook_gnu_attributes[] = {
     138              :   ATTR_SPEC ("cold", 0, 0, true, false, false, false, handle_cold_attribute,
     139              :              attr_cold_hot_exclusions),
     140              :   ATTR_SPEC ("hot", 0, 0, true, false, false, false, handle_hot_attribute,
     141              :              attr_cold_hot_exclusions),
     142              : };
     143              : 
     144              : const scoped_attribute_specs grs_langhook_gnu_attribute_table
     145              :   = {"gnu", {grs_langhook_gnu_attributes}};
     146              : 
     147              : /* Built-in attribute handlers.
     148              :    These functions take the arguments:
     149              :    (tree *node, tree name, tree args, int flags, bool *no_add_attrs)  */
     150              : 
     151              : /* Handle a "noreturn" attribute; arguments as in
     152              :    struct attribute_spec.handler.  */
     153              : 
     154              : static tree
     155        51910 : handle_noreturn_attribute (tree *node, tree, tree, int, bool *)
     156              : {
     157        51910 :   tree type = TREE_TYPE (*node);
     158              : 
     159        51910 :   if (TREE_CODE (*node) == FUNCTION_DECL)
     160        51910 :     TREE_THIS_VOLATILE (*node) = 1;
     161            0 :   else if (TREE_CODE (type) == POINTER_TYPE
     162            0 :            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
     163            0 :     TREE_TYPE (*node) = build_pointer_type (
     164            0 :       build_type_variant (TREE_TYPE (type), TYPE_READONLY (TREE_TYPE (type)),
     165              :                           1));
     166              :   else
     167            0 :     gcc_unreachable ();
     168              : 
     169        51910 :   return NULL_TREE;
     170              : }
     171              : 
     172              : /* Handle a "leaf" attribute; arguments as in
     173              :    struct attribute_spec.handler.  */
     174              : 
     175              : static tree
     176      7028614 : rust_handle_leaf_attribute (tree *node, tree name, tree, int,
     177              :                             bool *no_add_attrs)
     178              : {
     179      7028614 :   if (TREE_CODE (*node) != FUNCTION_DECL)
     180              :     {
     181            0 :       warning (OPT_Wattributes, "%qE attribute ignored", name);
     182            0 :       *no_add_attrs = true;
     183              :     }
     184      7028614 :   if (!TREE_PUBLIC (*node))
     185              :     {
     186            0 :       warning (OPT_Wattributes, "%qE attribute has no effect", name);
     187            0 :       *no_add_attrs = true;
     188              :     }
     189              : 
     190      7028614 :   return NULL_TREE;
     191              : }
     192              : 
     193              : /* Handle a "const" attribute; arguments as in
     194              :    struct attribute_spec.handler.  */
     195              : 
     196              : static tree
     197      2964061 : rust_handle_const_attribute (tree *node, tree, tree, int, bool *)
     198              : {
     199      2964061 :   tree type = TREE_TYPE (*node);
     200              : 
     201      2964061 :   if (TREE_CODE (*node) == FUNCTION_DECL)
     202      2964061 :     TREE_READONLY (*node) = 1;
     203            0 :   else if (TREE_CODE (type) == POINTER_TYPE
     204            0 :            && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
     205            0 :     TREE_TYPE (*node) = build_pointer_type (
     206            0 :       build_type_variant (TREE_TYPE (type), 1,
     207              :                           TREE_THIS_VOLATILE (TREE_TYPE (type))));
     208              :   else
     209            0 :     gcc_unreachable ();
     210              : 
     211      2964061 :   return NULL_TREE;
     212              : }
     213              : 
     214              : /* Handle a "malloc" attribute; arguments as in
     215              :    struct attribute_spec.handler.  */
     216              : 
     217              : static tree
     218        31146 : handle_malloc_attribute (tree *node, tree, tree, int, bool *)
     219              : {
     220        31146 :   gcc_assert (TREE_CODE (*node) == FUNCTION_DECL
     221              :               && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))));
     222        31146 :   DECL_IS_MALLOC (*node) = 1;
     223        31146 :   return NULL_TREE;
     224              : }
     225              : 
     226              : /* Handle a "pure" attribute; arguments as in
     227              :    struct attribute_spec.handler.  */
     228              : 
     229              : static tree
     230       243977 : rust_handle_pure_attribute (tree *node, tree name, tree, int,
     231              :                             bool *no_add_attrs)
     232              : {
     233       243977 :   if (TREE_CODE (*node) != FUNCTION_DECL)
     234              :     {
     235            0 :       warning (OPT_Wattributes, "%qE attribute ignored", name);
     236            0 :       *no_add_attrs = true;
     237              :     }
     238              : 
     239       243977 :   DECL_PURE_P (*node) = 1;
     240       243977 :   return NULL_TREE;
     241              : }
     242              : 
     243              : /* Handle a "no vops" attribute; arguments as in
     244              :    struct attribute_spec.handler.  */
     245              : 
     246              : static tree
     247        41528 : rust_handle_novops_attribute (tree *node, tree name, tree, int,
     248              :                               bool *no_add_attrs)
     249              : {
     250        41528 :   if (TREE_CODE (*node) != FUNCTION_DECL)
     251              :     {
     252            0 :       warning (OPT_Wattributes, "%qE attribute ignored", name);
     253            0 :       *no_add_attrs = true;
     254              :     }
     255              : 
     256        41528 :   DECL_IS_NOVOPS (*node) = 1;
     257        41528 :   return NULL_TREE;
     258              : }
     259              : 
     260              : /* Helper for nonnull attribute handling; fetch the operand number
     261              :    from the attribute argument list.  */
     262              : 
     263              : static bool
     264       218022 : get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
     265              : {
     266              :   /* Verify the arg number is a constant.  */
     267       218022 :   if (!tree_fits_uhwi_p (arg_num_expr))
     268              :     return false;
     269              : 
     270       218022 :   *valp = TREE_INT_CST_LOW (arg_num_expr);
     271       218022 :   return true;
     272              : }
     273              : 
     274              : /* Handle the "nonnull" attribute.  */
     275              : 
     276              : static tree
     277       596965 : handle_nonnull_attribute (tree *node, tree, tree args, int, bool *)
     278              : {
     279       596965 :   tree type = *node;
     280              : 
     281              :   /* If no arguments are specified, all pointer arguments should be
     282              :      non-null.  Verify a full prototype is given so that the arguments
     283              :      will have the correct types when we actually check them later.
     284              :      Avoid diagnosing type-generic built-ins since those have no
     285              :      prototype.  */
     286       596965 :   if (!args)
     287              :     {
     288       425662 :       gcc_assert (prototype_p (type) || !TYPE_ATTRIBUTES (type)
     289              :                   || lookup_attribute ("type generic", TYPE_ATTRIBUTES (type)));
     290              : 
     291              :       return NULL_TREE;
     292              :     }
     293              : 
     294              :   /* Argument list specified.  Verify that each argument number references
     295              :      a pointer argument.  */
     296       389325 :   for (; args; args = TREE_CHAIN (args))
     297              :     {
     298       218022 :       tree argument;
     299       218022 :       unsigned HOST_WIDE_INT arg_num = 0, ck_num;
     300              : 
     301       218022 :       if (!get_nonnull_operand (TREE_VALUE (args), &arg_num))
     302            0 :         gcc_unreachable ();
     303              : 
     304       218022 :       argument = TYPE_ARG_TYPES (type);
     305       218022 :       if (argument)
     306              :         {
     307       233595 :           for (ck_num = 1;; ck_num++)
     308              :             {
     309       451617 :               if (!argument || ck_num == arg_num)
     310              :                 break;
     311       233595 :               argument = TREE_CHAIN (argument);
     312              :             }
     313              : 
     314       218022 :           gcc_assert (argument
     315              :                       && TREE_CODE (TREE_VALUE (argument)) == POINTER_TYPE);
     316              :         }
     317              :     }
     318              : 
     319              :   return NULL_TREE;
     320              : }
     321              : 
     322              : /* Handle a "nothrow" attribute; arguments as in
     323              :    struct attribute_spec.handler.  */
     324              : 
     325              : static tree
     326      7153198 : rust_handle_nothrow_attribute (tree *node, tree name, tree, int,
     327              :                                bool *no_add_attrs)
     328              : {
     329      7153198 :   if (TREE_CODE (*node) != FUNCTION_DECL)
     330              :     {
     331            0 :       warning (OPT_Wattributes, "%qE attribute ignored", name);
     332            0 :       *no_add_attrs = true;
     333              :     }
     334              : 
     335      7153198 :   TREE_NOTHROW (*node) = 1;
     336      7153198 :   return NULL_TREE;
     337              : }
     338              : 
     339              : /* Handle a "transaction_pure" attribute; arguments as in
     340              :    struct attribute_spec.handler.  */
     341              : 
     342              : static tree
     343         5191 : handle_transaction_pure_attribute (tree *node, tree, tree, int, bool *)
     344              : {
     345              :   /* Ensure we have a function type.  */
     346         5191 :   gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
     347              : 
     348         5191 :   return NULL_TREE;
     349              : }
     350              : 
     351              : /* Handle a "returns_twice" attribute; arguments as in
     352              :    struct attribute_spec.handler.  */
     353              : 
     354              : static tree
     355         5191 : rust_handle_returns_twice_attribute (tree *node, tree name, tree, int,
     356              :                                      bool *no_add_attrs)
     357              : {
     358         5191 :   if (TREE_CODE (*node) != FUNCTION_DECL)
     359              :     {
     360            0 :       warning (OPT_Wattributes, "%qE attribute ignored", name);
     361            0 :       *no_add_attrs = true;
     362              :     }
     363              : 
     364         5191 :   DECL_IS_RETURNS_TWICE (*node) = 1;
     365              : 
     366         5191 :   return NULL_TREE;
     367              : }
     368              : 
     369              : /* Handle an "omp declare simd" attribute; arguments as in
     370              :    struct attribute_spec.handler.  */
     371              : 
     372              : static tree
     373            0 : handle_omp_declare_simd_attribute (tree *node, tree name, tree, int,
     374              :                                    bool *no_add_attrs)
     375              : {
     376            0 :   if (TREE_CODE (*node) != FUNCTION_DECL)
     377              :     {
     378            0 :       warning (OPT_Wattributes, "%qE attribute ignored", name);
     379            0 :       *no_add_attrs = true;
     380              :     }
     381              : 
     382            0 :   return NULL_TREE;
     383              : }
     384              : 
     385              : /* Language specific attribute handlers.
     386              :    These functions take the arguments:
     387              :    (tree *node, tree name, tree args, int flags, bool *no_add_attrs)  */
     388              : 
     389              : /* Handle a "cold" and attribute; arguments as in
     390              :    struct attribute_spec.handler.  */
     391              : 
     392              : static tree
     393        20764 : handle_cold_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
     394              : {
     395        20764 :   if (TREE_CODE (*node) != FUNCTION_DECL)
     396              :     {
     397            0 :       warning (OPT_Wattributes, "%qE attribute ignored", name);
     398            0 :       *no_add_attrs = true;
     399              :     }
     400              : 
     401        20764 :   return NULL_TREE;
     402              : }
     403              : 
     404              : static tree
     405            0 : handle_hot_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
     406              : {
     407            0 :   if (TREE_CODE (*node) != FUNCTION_DECL)
     408              :     {
     409            0 :       warning (OPT_Wattributes, "%qE attribute ignored", name);
     410            0 :       *no_add_attrs = true;
     411              :     }
     412              : 
     413            0 :   return NULL_TREE;
     414              : }
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.