Hmmmmm, it really doesn't make too much sense to get that warning, but
I can reproduce that when I compile with gcc 13 (and newer)...and
seems like a known issue [1][2]...

However I don't really like that approach, could you change the
argument type of get_riscv_ext_info to `const char *` to suppress that
warning instead?

```diff
diff --git a/gcc/common/config/riscv/riscv-common.cc
b/gcc/common/config/riscv/riscv-common.cc
index 53ca03910b3..a3105c851d6 100644
--- a/gcc/common/config/riscv/riscv-common.cc
+++ b/gcc/common/config/riscv/riscv-common.cc
@@ -214,8 +214,8 @@ static const std::unordered_map<std::string,
riscv_ext_info_t> riscv_ext_infos
#undef DEFINE_RISCV_EXT
};

-static const riscv_ext_info_t &
-get_riscv_ext_info (const std::string &ext)
+static inline const riscv_ext_info_t &
+get_riscv_ext_info (const char *ext)
{
  auto itr = riscv_ext_infos.find (ext);
  if (itr == riscv_ext_infos.end ())
```

[1] 
https://stackoverflow.com/questions/78759847/gcc-14-possibly-dangling-reference-to-a-temporary-warning-or-not-depending-on
[2] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107532


On Thu, May 15, 2025 at 7:56 PM Dongyan Chen
<chendong...@isrc.iscas.ac.cn> wrote:
>
> During the GCC compilation, some warnings about temporary object dangling 
> references emerged. They appeared in these code lines in riscv-common.cc:
> const riscv_ext_info_t &implied_ext_info, const riscv_ext_info_t &ext_info = 
> get_riscv_ext_info (ext) and auto &ext_info = get_riscv_ext_info (search_ext).
> The issue arose because the local variable types were not used in a 
> standardized way, causing their references to dangle once the function ended.
> To fix this, the patch converts the const char* type to std::string via 
> forced type conversion, thereby eliminating the warnings.
>
> gcc/ChangeLog:
>
>         * common/config/riscv/riscv-common.cc 
> (riscv_ext_info_t::apply_implied_ext): Type conversion.
>         (riscv_subset_list::handle_implied_ext): Ditto.
>         (riscv_minimal_hwprobe_feature_bits): Ditto.
>
> ---
>  gcc/common/config/riscv/riscv-common.cc | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/gcc/common/config/riscv/riscv-common.cc 
> b/gcc/common/config/riscv/riscv-common.cc
> index 53ca03910b38..c2e35dfe54d2 100644
> --- a/gcc/common/config/riscv/riscv-common.cc
> +++ b/gcc/common/config/riscv/riscv-common.cc
> @@ -245,8 +245,9 @@ riscv_ext_info_t::apply_implied_ext (riscv_subset_list 
> *subset_list) const
>        subset_list->add (implied_info.implied_ext, true);
>
>        /* Recursively add implied extension by implied_info->implied_ext.  */
> +      std::string implied_ext_str = implied_info.implied_ext;
>        const riscv_ext_info_t &implied_ext_info
> -       = get_riscv_ext_info (implied_info.implied_ext);
> +       = get_riscv_ext_info (implied_ext_str);
>        implied_ext_info.apply_implied_ext (subset_list);
>      }
>    return any_change;
> @@ -1089,7 +1090,8 @@ riscv_subset_list::parse_single_std_ext (const char *p, 
> bool exact_single_p)
>  void
>  riscv_subset_list::handle_implied_ext (const char *ext)
>  {
> -  const riscv_ext_info_t &ext_info = get_riscv_ext_info (ext);
> +  std::string ext_str = ext;
> +  const riscv_ext_info_t &ext_info = get_riscv_ext_info (ext_str);
>    ext_info.apply_implied_ext (this);
>
>    /* For RISC-V ISA version 2.2 or earlier version, zicsr and zifence is
> @@ -1642,7 +1644,8 @@ riscv_minimal_hwprobe_feature_bits (const char *isa,
>           search_q.pop ();
>
>           /* Iterate through the implied extension table.  */
> -         auto &ext_info = get_riscv_ext_info (search_ext);
> +         std::string search_ext_str = search_ext;
> +         auto &ext_info = get_riscv_ext_info (search_ext_str);
>           for (const auto &implied_ext : ext_info.implied_exts ())
>             {
>               /* When the search extension matches the implied extension and
> --
> 2.43.0
>

Reply via email to