fold: fix use of protected_set_expr_location_unshare
[official-gcc.git] / gcc / file-prefix-map.cc
blob40b10edcf928cafdb93bd56f5e346a1beeba8397
1 /* Implementation of file prefix remapping support (-f*-prefix-map options).
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
9 This program 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.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21 #include "diagnostic.h"
22 #include "file-prefix-map.h"
24 /* Structure recording the mapping from source file and directory names at
25 compile time to those to be embedded in the compilation result (debug
26 information, the __FILE__ macro expansion, etc). */
27 struct file_prefix_map
29 const char *old_prefix;
30 const char *new_prefix;
31 size_t old_len;
32 size_t new_len;
33 struct file_prefix_map *next;
36 /* Record a file prefix mapping in the specified map. ARG is the argument to
37 -f*-prefix-map and must be of the form OLD=NEW. OPT is the option name
38 for diagnostics. */
39 static void
40 add_prefix_map (file_prefix_map *&maps, const char *arg, const char *opt)
42 file_prefix_map *map;
43 const char *p;
45 /* Note: looking for the last '='. The thinking is we can control the paths
46 inside our projects but not where the users build them. */
47 p = strrchr (arg, '=');
48 if (!p)
50 error ("invalid argument %qs to %qs", arg, opt);
51 return;
53 map = XNEW (file_prefix_map);
54 map->old_prefix = xstrndup (arg, p - arg);
55 map->old_len = p - arg;
56 p++;
57 map->new_prefix = xstrdup (p);
58 map->new_len = strlen (p);
59 map->next = maps;
60 maps = map;
63 /* Perform user-specified mapping of filename prefixes. Return the
64 GC-allocated new name corresponding to FILENAME or FILENAME if no
65 remapping was performed. */
67 static const char *
68 remap_filename (file_prefix_map *maps, const char *filename)
70 file_prefix_map *map;
71 char *s;
72 const char *name;
73 char *realname;
74 size_t name_len;
76 if (!filename || lbasename (filename) == filename)
77 return filename;
79 realname = lrealpath (filename);
81 for (map = maps; map; map = map->next)
82 if (filename_ncmp (realname, map->old_prefix, map->old_len) == 0)
83 break;
84 if (!map)
86 free (realname);
87 return filename;
89 name = realname + map->old_len;
90 name_len = strlen (name) + 1;
92 s = (char *) ggc_alloc_atomic (name_len + map->new_len);
93 memcpy (s, map->new_prefix, map->new_len);
94 memcpy (s + map->new_len, name, name_len);
95 free (realname);
96 return s;
99 /* NOTE: if adding another -f*-prefix-map option then don't forget to
100 ignore it in DW_AT_producer (dwarf2out.cc). */
102 /* Linked lists of file_prefix_map structures. */
103 static file_prefix_map *macro_prefix_maps; /* -fmacro-prefix-map */
104 static file_prefix_map *debug_prefix_maps; /* -fdebug-prefix-map */
105 static file_prefix_map *profile_prefix_maps; /* -fprofile-prefix-map */
107 /* Record a file prefix mapping for -fmacro-prefix-map. */
108 void
109 add_macro_prefix_map (const char *arg)
111 add_prefix_map (macro_prefix_maps, arg, "-fmacro-prefix-map");
114 /* Record a file prefix mapping for -fdebug-prefix-map. */
115 void
116 add_debug_prefix_map (const char *arg)
118 add_prefix_map (debug_prefix_maps, arg, "-fdebug-prefix-map");
121 /* Record a file prefix mapping for all -f*-prefix-map. */
122 void
123 add_file_prefix_map (const char *arg)
125 add_prefix_map (macro_prefix_maps, arg, "-ffile-prefix-map");
126 add_prefix_map (debug_prefix_maps, arg, "-ffile-prefix-map");
127 add_prefix_map (profile_prefix_maps, arg, "-ffile-prefix-map");
130 /* Record a file prefix mapping for -fprofile-prefix-map. */
131 void
132 add_profile_prefix_map (const char *arg)
134 add_prefix_map (profile_prefix_maps, arg, "-fprofile-prefix-map");
137 /* Remap using -fmacro-prefix-map. Return the GC-allocated new name
138 corresponding to FILENAME or FILENAME if no remapping was performed. */
139 const char *
140 remap_macro_filename (const char *filename)
142 return remap_filename (macro_prefix_maps, filename);
145 /* Remap using -fdebug-prefix-map. Return the GC-allocated new name
146 corresponding to FILENAME or FILENAME if no remapping was performed. */
147 const char *
148 remap_debug_filename (const char *filename)
150 return remap_filename (debug_prefix_maps, filename);
153 /* Remap using -fprofile-prefix-map. Return the GC-allocated new name
154 corresponding to FILENAME or FILENAME if no remapping was performed. */
155 const char *
156 remap_profile_filename (const char *filename)
158 return remap_filename (profile_prefix_maps, filename);