Hi!

This patch fixes PR preprocessor/112373. In addition to the original
issue, it also addresses two other occurrences of the unnecessary
copy.

Regression tested on x86_64-linux. OK for trunk?

Thanks,
Yuao
From c91d78f46f3d609e477efe20695fe145920c7a16 Mon Sep 17 00:00:00 2001
From: Yuao Ma <[email protected]>
Date: Wed, 17 Jun 2026 15:00:36 +0800
Subject: [PATCH] libcpp: avoid cset_converter copy in charset

        PR preprocessor/112373

libcpp/ChangeLog:

        * charset.cc (convert_ucn, emit_numeric_escape, convert_hex,
        convert_oct, convert_escape): Pass argument as reference.
---
 libcpp/charset.cc | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/libcpp/charset.cc b/libcpp/charset.cc
index 563b7c55f79..e359bf471d7 100644
--- a/libcpp/charset.cc
+++ b/libcpp/charset.cc
@@ -1892,9 +1892,8 @@ _cpp_valid_ucn (cpp_reader *pfile, const uchar **pstr,
    is read from *LOC_READER, and *RANGES is updated accordingly.  */
 static const uchar *
 convert_ucn (cpp_reader *pfile, const uchar *from, const uchar *limit,
-            struct _cpp_strbuf *tbuf, struct cset_converter cvt,
-            source_range char_range,
-            cpp_string_location_reader *loc_reader,
+            struct _cpp_strbuf *tbuf, const struct cset_converter &cvt,
+            source_range char_range, cpp_string_location_reader *loc_reader,
             cpp_substring_ranges *ranges)
 {
   cppchar_t ucn;
@@ -2058,8 +2057,8 @@ cpp_valid_utf8_p (const char *buffer, size_t num_bytes)
    is true if it's a wide string that's being assembled in TBUF.  This
    function issues no diagnostics and never fails.  */
 static void
-emit_numeric_escape (cpp_reader *pfile, cppchar_t n,
-                    struct _cpp_strbuf *tbuf, struct cset_converter cvt)
+emit_numeric_escape (cpp_reader *pfile, cppchar_t n, struct _cpp_strbuf *tbuf,
+                    const struct cset_converter &cvt)
 {
   size_t width = cvt.width;
 
@@ -2113,9 +2112,8 @@ emit_numeric_escape (cpp_reader *pfile, cppchar_t n,
    is read from *LOC_READER, and *RANGES is updated accordingly.  */
 static const uchar *
 convert_hex (cpp_reader *pfile, const uchar *from, const uchar *limit,
-            struct _cpp_strbuf *tbuf, struct cset_converter cvt,
-            source_range char_range,
-            cpp_string_location_reader *loc_reader,
+            struct _cpp_strbuf *tbuf, const struct cset_converter &cvt,
+            source_range char_range, cpp_string_location_reader *loc_reader,
             cpp_substring_ranges *ranges)
 {
   cppchar_t c, n = 0, overflow = 0;
@@ -2228,9 +2226,8 @@ convert_hex (cpp_reader *pfile, const uchar *from, const 
uchar *limit,
    is read from *LOC_READER, and *RANGES is updated accordingly.  */
 static const uchar *
 convert_oct (cpp_reader *pfile, const uchar *from, const uchar *limit,
-            struct _cpp_strbuf *tbuf, struct cset_converter cvt,
-            source_range char_range,
-            cpp_string_location_reader *loc_reader,
+            struct _cpp_strbuf *tbuf, const struct cset_converter &cvt,
+            source_range char_range, cpp_string_location_reader *loc_reader,
             cpp_substring_ranges *ranges)
 {
   size_t count = 0;
@@ -2336,7 +2333,7 @@ convert_oct (cpp_reader *pfile, const uchar *from, const 
uchar *limit,
    accordingly.  */
 static const uchar *
 convert_escape (cpp_reader *pfile, const uchar *from, const uchar *limit,
-               struct _cpp_strbuf *tbuf, struct cset_converter cvt,
+               struct _cpp_strbuf *tbuf, const struct cset_converter &cvt,
                cpp_string_location_reader *loc_reader,
                cpp_substring_ranges *ranges, bool uneval)
 {
-- 
2.54.0

Reply via email to