@@ -2774,12 +2774,12 @@ clear_failure_queue (void)
2774
2774
2775
2775
Returns the value returned by CALLBACK. */
2776
2776
2777
- static void *
2777
+ template <typename fun>
2778
+ auto *
2778
2779
for_each_path (const struct path_prefix *paths,
2779
2780
bool do_multi,
2780
2781
size_t extra_space,
2781
- void *(*callback) (char *, void *),
2782
- void *callback_info)
2782
+ fun && callback)
2783
2783
{
2784
2784
struct prefix_list *pl;
2785
2785
const char *multi_dir = NULL ;
@@ -2788,7 +2788,7 @@ for_each_path (const struct path_prefix *paths,
2788
2788
const char *multi_suffix;
2789
2789
const char *just_multi_suffix;
2790
2790
char *path = NULL ;
2791
- void *ret = NULL ;
2791
+ decltype ( callback ( nullptr )) ret ;
2792
2792
bool skip_multi_dir = false ;
2793
2793
bool skip_multi_os_dir = false ;
2794
2794
@@ -2839,7 +2839,7 @@ for_each_path (const struct path_prefix *paths,
2839
2839
if (!skip_multi_dir)
2840
2840
{
2841
2841
memcpy (path + len, multi_suffix, suffix_len + 1 );
2842
- ret = callback (path, callback_info );
2842
+ ret = callback (path);
2843
2843
if (ret)
2844
2844
break ;
2845
2845
}
@@ -2850,7 +2850,7 @@ for_each_path (const struct path_prefix *paths,
2850
2850
&& pl->require_machine_suffix == 2 )
2851
2851
{
2852
2852
memcpy (path + len, just_multi_suffix, just_suffix_len + 1 );
2853
- ret = callback (path, callback_info );
2853
+ ret = callback (path);
2854
2854
if (ret)
2855
2855
break ;
2856
2856
}
@@ -2860,7 +2860,7 @@ for_each_path (const struct path_prefix *paths,
2860
2860
&& !pl->require_machine_suffix && multiarch_dir)
2861
2861
{
2862
2862
memcpy (path + len, multiarch_suffix, multiarch_len + 1 );
2863
- ret = callback (path, callback_info );
2863
+ ret = callback (path);
2864
2864
if (ret)
2865
2865
break ;
2866
2866
}
@@ -2888,7 +2888,7 @@ for_each_path (const struct path_prefix *paths,
2888
2888
else
2889
2889
path[len] = ' \0 ' ;
2890
2890
2891
- ret = callback (path, callback_info );
2891
+ ret = callback (path);
2892
2892
if (ret)
2893
2893
break ;
2894
2894
}
@@ -2934,31 +2934,6 @@ for_each_path (const struct path_prefix *paths,
2934
2934
return ret;
2935
2935
}
2936
2936
2937
- /* Callback for build_search_list. Adds path to obstack being built. */
2938
-
2939
- struct add_to_obstack_info {
2940
- struct obstack *ob;
2941
- bool check_dir;
2942
- bool first_time;
2943
- };
2944
-
2945
- static void *
2946
- add_to_obstack (char *path, void *data)
2947
- {
2948
- struct add_to_obstack_info *info = (struct add_to_obstack_info *) data;
2949
-
2950
- if (info->check_dir && !is_directory (path))
2951
- return NULL ;
2952
-
2953
- if (!info->first_time )
2954
- obstack_1grow (info->ob , PATH_SEPARATOR);
2955
-
2956
- obstack_grow (info->ob , path, strlen (path));
2957
-
2958
- info->first_time = false ;
2959
- return NULL ;
2960
- }
2961
-
2962
2937
/* Add or change the value of an environment variable, outputting the
2963
2938
change to standard error if in verbose mode. */
2964
2939
static void
@@ -2979,16 +2954,26 @@ static char *
2979
2954
build_search_list (const struct path_prefix *paths, const char *prefix,
2980
2955
bool check_dir, bool do_multi)
2981
2956
{
2982
- struct add_to_obstack_info info;
2983
-
2984
- info.ob = &collect_obstack;
2985
- info.check_dir = check_dir;
2986
- info.first_time = true ;
2957
+ struct obstack *const ob = &collect_obstack;
2958
+ bool first_time = true ;
2987
2959
2988
2960
obstack_grow (&collect_obstack, prefix, strlen (prefix));
2989
2961
obstack_1grow (&collect_obstack, ' =' );
2990
2962
2991
- for_each_path (paths, do_multi, 0 , add_to_obstack, &info);
2963
+ /* Callback adds path to obstack being built. */
2964
+ for_each_path (paths, do_multi, 0 , [&](char *path) -> void *
2965
+ {
2966
+ if (check_dir && !is_directory (path))
2967
+ return NULL ;
2968
+
2969
+ if (!first_time)
2970
+ obstack_1grow (ob, PATH_SEPARATOR);
2971
+
2972
+ obstack_grow (ob, path, strlen (path));
2973
+
2974
+ first_time = false ;
2975
+ return NULL ;
2976
+ });
2992
2977
2993
2978
obstack_1grow (&collect_obstack, ' \0 ' );
2994
2979
return XOBFINISH (&collect_obstack, char *);
@@ -3022,42 +3007,6 @@ access_check (const char *name, int mode)
3022
3007
return access (name, mode);
3023
3008
}
3024
3009
3025
- /* Callback for find_a_file. Appends the file name to the directory
3026
- path. If the resulting file exists in the right mode, return the
3027
- full pathname to the file. */
3028
-
3029
- struct file_at_path_info {
3030
- const char *name;
3031
- const char *suffix;
3032
- int name_len;
3033
- int suffix_len;
3034
- int mode;
3035
- };
3036
-
3037
- static void *
3038
- file_at_path (char *path, void *data)
3039
- {
3040
- struct file_at_path_info *info = (struct file_at_path_info *) data;
3041
- size_t len = strlen (path);
3042
-
3043
- memcpy (path + len, info->name , info->name_len );
3044
- len += info->name_len ;
3045
-
3046
- /* Some systems have a suffix for executable files.
3047
- So try appending that first. */
3048
- if (info->suffix_len )
3049
- {
3050
- memcpy (path + len, info->suffix , info->suffix_len + 1 );
3051
- if (access_check (path, info->mode ) == 0 )
3052
- return path;
3053
- }
3054
-
3055
- path[len] = ' \0 ' ;
3056
- if (access_check (path, info->mode ) == 0 )
3057
- return path;
3058
-
3059
- return NULL ;
3060
- }
3061
3010
3062
3011
/* Search for NAME using the prefix list PREFIXES. MODE is passed to
3063
3012
access to check permissions. If DO_MULTI is true, search multilib
@@ -3068,8 +3017,6 @@ static char *
3068
3017
find_a_file (const struct path_prefix *pprefix, const char *name, int mode,
3069
3018
bool do_multi)
3070
3019
{
3071
- struct file_at_path_info info;
3072
-
3073
3020
/* Find the filename in question (special case for absolute paths). */
3074
3021
3075
3022
if (IS_ABSOLUTE_PATH (name))
@@ -3080,15 +3027,38 @@ find_a_file (const struct path_prefix *pprefix, const char *name, int mode,
3080
3027
return NULL ;
3081
3028
}
3082
3029
3083
- info.name = name;
3084
- info.suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : " " ;
3085
- info.name_len = strlen (info.name );
3086
- info.suffix_len = strlen (info.suffix );
3087
- info.mode = mode;
3030
+ const char *suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : " " ;
3031
+ const int name_len = strlen (name);
3032
+ const int suffix_len = strlen (suffix);
3088
3033
3089
- return (char *) for_each_path (pprefix, do_multi,
3090
- info.name_len + info.suffix_len ,
3091
- file_at_path, &info);
3034
+
3035
+ /* Callback appends the file name to the directory path. If the
3036
+ resulting file exists in the right mode, return the full pathname
3037
+ to the file. */
3038
+ return for_each_path (pprefix, do_multi,
3039
+ name_len + suffix_len,
3040
+ [=](char *path) -> char *
3041
+ {
3042
+ size_t len = strlen (path);
3043
+
3044
+ memcpy (path + len, name, name_len);
3045
+ len += name_len;
3046
+
3047
+ /* Some systems have a suffix for executable files.
3048
+ So try appending that first. */
3049
+ if (suffix_len)
3050
+ {
3051
+ memcpy (path + len, suffix, suffix_len + 1 );
3052
+ if (access_check (path, mode) == 0 )
3053
+ return path;
3054
+ }
3055
+
3056
+ path[len] = ' \0 ' ;
3057
+ if (access_check (path, mode) == 0 )
3058
+ return path;
3059
+
3060
+ return NULL ;
3061
+ });
3092
3062
}
3093
3063
3094
3064
/* Specialization of find_a_file for programs that also takes into account
@@ -6008,49 +5978,50 @@ do_self_spec (const char *spec)
6008
5978
6009
5979
/* Callback for processing %D and %I specs. */
6010
5980
6011
- struct spec_path_info {
5981
+ struct spec_path {
6012
5982
const char *option;
6013
5983
const char *append;
6014
5984
size_t append_len;
6015
5985
bool omit_relative;
6016
5986
bool separate_options;
6017
5987
bool realpaths;
5988
+
5989
+ void *operator () (char *path);
6018
5990
};
6019
5991
6020
- static void *
6021
- spec_path (char *path, void *data )
5992
+ void *
5993
+ spec_path::operator () (char *path)
6022
5994
{
6023
- struct spec_path_info *info = (struct spec_path_info *) data;
6024
5995
size_t len = 0 ;
6025
5996
char save = 0 ;
6026
5997
6027
5998
/* The path must exist; we want to resolve it to the realpath so that this
6028
5999
can be embedded as a runpath. */
6029
- if (info-> realpaths )
6000
+ if (realpaths)
6030
6001
path = lrealpath (path);
6031
6002
6032
6003
/* However, if we failed to resolve it - perhaps because there was a bogus
6033
6004
-B option on the command line, then punt on this entry. */
6034
6005
if (!path)
6035
6006
return NULL ;
6036
6007
6037
- if (info-> omit_relative && !IS_ABSOLUTE_PATH (path))
6008
+ if (omit_relative && !IS_ABSOLUTE_PATH (path))
6038
6009
return NULL ;
6039
6010
6040
- if (info-> append_len != 0 )
6011
+ if (append_len != 0 )
6041
6012
{
6042
6013
len = strlen (path);
6043
- memcpy (path + len, info-> append , info-> append_len + 1 );
6014
+ memcpy (path + len, append, append_len + 1 );
6044
6015
}
6045
6016
6046
6017
if (!is_directory (path))
6047
6018
return NULL ;
6048
6019
6049
- do_spec_1 (info-> option , 1 , NULL );
6050
- if (info-> separate_options )
6020
+ do_spec_1 (option, 1 , NULL );
6021
+ if (separate_options)
6051
6022
do_spec_1 (" " , 0 , NULL );
6052
6023
6053
- if (info-> append_len == 0 )
6024
+ if (append_len == 0 )
6054
6025
{
6055
6026
len = strlen (path);
6056
6027
save = path[len - 1 ];
@@ -6062,7 +6033,7 @@ spec_path (char *path, void *data)
6062
6033
do_spec_1 (" " , 0 , NULL );
6063
6034
6064
6035
/* Must not damage the original path. */
6065
- if (info-> append_len == 0 )
6036
+ if (append_len == 0 )
6066
6037
path[len - 1 ] = save;
6067
6038
6068
6039
return NULL ;
@@ -6250,7 +6221,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
6250
6221
that we search for startfiles. */
6251
6222
case ' D' :
6252
6223
{
6253
- struct spec_path_info info;
6224
+ struct spec_path info;
6254
6225
6255
6226
info.option = " -L" ;
6256
6227
info.append_len = 0 ;
@@ -6267,13 +6238,13 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
6267
6238
info.separate_options = false ;
6268
6239
info.realpaths = false ;
6269
6240
6270
- for_each_path (&startfile_prefixes, true , 0 , spec_path, & info);
6241
+ for_each_path (&startfile_prefixes, true , 0 , info);
6271
6242
}
6272
6243
break ;
6273
6244
6274
6245
case ' P' :
6275
6246
{
6276
- struct spec_path_info info;
6247
+ struct spec_path info;
6277
6248
6278
6249
info.option = RUNPATH_OPTION;
6279
6250
info.append_len = 0 ;
@@ -6282,7 +6253,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
6282
6253
/* We want to embed the actual paths that have the libraries. */
6283
6254
info.realpaths = true ;
6284
6255
6285
- for_each_path (&startfile_prefixes, true , 0 , spec_path, & info);
6256
+ for_each_path (&startfile_prefixes, true , 0 , info);
6286
6257
}
6287
6258
break ;
6288
6259
@@ -6561,7 +6532,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
6561
6532
6562
6533
case ' I' :
6563
6534
{
6564
- struct spec_path_info info;
6535
+ struct spec_path info;
6565
6536
6566
6537
if (multilib_dir)
6567
6538
{
@@ -6609,8 +6580,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
6609
6580
info.separate_options = true ;
6610
6581
info.realpaths = false ;
6611
6582
6612
- for_each_path (&include_prefixes, false , info.append_len ,
6613
- spec_path, &info);
6583
+ for_each_path (&include_prefixes, false , info.append_len , info);
6614
6584
6615
6585
info.append = " include-fixed" ;
6616
6586
if (*sysroot_hdrs_suffix_spec)
@@ -6623,14 +6593,13 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
6623
6593
info.append = concat (info.append , dir_separator_str,
6624
6594
multiarch_dir, NULL );
6625
6595
info.append_len = strlen (info.append );
6626
- for_each_path (&include_prefixes, false , info. append_len ,
6627
- spec_path, & info);
6596
+ for_each_path (&include_prefixes, false ,
6597
+ info. append_len , info);
6628
6598
6629
6599
info.append = " include-fixed" ;
6630
6600
}
6631
6601
info.append_len = strlen (info.append );
6632
- for_each_path (&include_prefixes, false , info.append_len ,
6633
- spec_path, &info);
6602
+ for_each_path (&include_prefixes, false , info.append_len , info);
6634
6603
}
6635
6604
break ;
6636
6605
0 commit comments