[patch] Refactor duplicated code into a helper function

Cary Coutant ccoutant@google.com
Wed Feb 4 19:13:00 GMT 2015


> 2014-09-18  Rafael Ávila de Espíndola <rafael.espindola@gmail.com>
>   * merge.h (add_string): declare.
>   * merge.cc (add_string): define.
>    (do_add_input_section): use add_string.

+Output_merge_string<Char_type>::add_string(const Char_type *&p, size_t len,
+                                           uintptr_t init_align_modulo,
+                                           bool &has_misaligned_strings,
+                                           Merged_strings &merged_strings,
+                                           section_size_type &i) {

By convention, we avoid non-const reference parameters in gold. This
new method has four output parameters, and to me it's a somewhat
awkward way to refactor the code. To avoid having so many output
parameters, I'd consider introducing a small class to manage all that
state. Instead of doing that, though, I'd prefer folding the loop
epilogue back into the loop -- the only difference is the computation
of len, and we can handle that inside the loop:

   while (p < pend)
     {
       const size_t len = p < pend0 ? string_length(p) : pend - p;
       ...
     }

-cary



More information about the Binutils mailing list