1*38fd1498Szrj /* Implementation of file prefix remapping support (-f*-prefix-map options).
2*38fd1498Szrj    Copyright (C) 2017 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj    This program is free software; you can redistribute it and/or modify it
5*38fd1498Szrj    under the terms of the GNU General Public License as published by the
6*38fd1498Szrj    Free Software Foundation; either version 3, or (at your option) any
7*38fd1498Szrj    later version.
8*38fd1498Szrj 
9*38fd1498Szrj    This program is distributed in the hope that it will be useful,
10*38fd1498Szrj    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*38fd1498Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*38fd1498Szrj    GNU General Public License for more details.
13*38fd1498Szrj 
14*38fd1498Szrj    You should have received a copy of the GNU General Public License
15*38fd1498Szrj    along with this program; see the file COPYING3.  If not see
16*38fd1498Szrj    <http://www.gnu.org/licenses/>.  */
17*38fd1498Szrj 
18*38fd1498Szrj #include "config.h"
19*38fd1498Szrj #include "system.h"
20*38fd1498Szrj #include "coretypes.h"
21*38fd1498Szrj #include "diagnostic.h"
22*38fd1498Szrj #include "file-prefix-map.h"
23*38fd1498Szrj 
24*38fd1498Szrj /* Structure recording the mapping from source file and directory names at
25*38fd1498Szrj    compile time to those to be embedded in the compilation result (debug
26*38fd1498Szrj    information, the __FILE__ macro expansion, etc).  */
27*38fd1498Szrj struct file_prefix_map
28*38fd1498Szrj {
29*38fd1498Szrj   const char *old_prefix;
30*38fd1498Szrj   const char *new_prefix;
31*38fd1498Szrj   size_t old_len;
32*38fd1498Szrj   size_t new_len;
33*38fd1498Szrj   struct file_prefix_map *next;
34*38fd1498Szrj };
35*38fd1498Szrj 
36*38fd1498Szrj /* Record a file prefix mapping in the specified map.  ARG is the argument to
37*38fd1498Szrj    -f*-prefix-map and must be of the form OLD=NEW.  OPT is the option name
38*38fd1498Szrj    for diagnostics.  */
39*38fd1498Szrj static void
add_prefix_map(file_prefix_map * & maps,const char * arg,const char * opt)40*38fd1498Szrj add_prefix_map (file_prefix_map *&maps, const char *arg, const char *opt)
41*38fd1498Szrj {
42*38fd1498Szrj   file_prefix_map *map;
43*38fd1498Szrj   const char *p;
44*38fd1498Szrj 
45*38fd1498Szrj   /* Note: looking for the last '='. The thinking is we can control the paths
46*38fd1498Szrj      inside our projects but not where the users build them.  */
47*38fd1498Szrj   p = strrchr (arg, '=');
48*38fd1498Szrj   if (!p)
49*38fd1498Szrj     {
50*38fd1498Szrj       error ("invalid argument %qs to %qs", arg, opt);
51*38fd1498Szrj       return;
52*38fd1498Szrj     }
53*38fd1498Szrj   map = XNEW (file_prefix_map);
54*38fd1498Szrj   map->old_prefix = xstrndup (arg, p - arg);
55*38fd1498Szrj   map->old_len = p - arg;
56*38fd1498Szrj   p++;
57*38fd1498Szrj   map->new_prefix = xstrdup (p);
58*38fd1498Szrj   map->new_len = strlen (p);
59*38fd1498Szrj   map->next = maps;
60*38fd1498Szrj   maps = map;
61*38fd1498Szrj }
62*38fd1498Szrj 
63*38fd1498Szrj /* Perform user-specified mapping of filename prefixes.  Return the
64*38fd1498Szrj    GC-allocated new name corresponding to FILENAME or FILENAME if no
65*38fd1498Szrj    remapping was performed.  */
66*38fd1498Szrj 
67*38fd1498Szrj static const char *
remap_filename(file_prefix_map * maps,const char * filename)68*38fd1498Szrj remap_filename (file_prefix_map *maps, const char *filename)
69*38fd1498Szrj {
70*38fd1498Szrj   file_prefix_map *map;
71*38fd1498Szrj   char *s;
72*38fd1498Szrj   const char *name;
73*38fd1498Szrj   size_t name_len;
74*38fd1498Szrj 
75*38fd1498Szrj   for (map = maps; map; map = map->next)
76*38fd1498Szrj     if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
77*38fd1498Szrj       break;
78*38fd1498Szrj   if (!map)
79*38fd1498Szrj     return filename;
80*38fd1498Szrj   name = filename + map->old_len;
81*38fd1498Szrj   name_len = strlen (name) + 1;
82*38fd1498Szrj 
83*38fd1498Szrj   s = (char *) ggc_alloc_atomic (name_len + map->new_len);
84*38fd1498Szrj   memcpy (s, map->new_prefix, map->new_len);
85*38fd1498Szrj   memcpy (s + map->new_len, name, name_len);
86*38fd1498Szrj   return s;
87*38fd1498Szrj }
88*38fd1498Szrj 
89*38fd1498Szrj /* NOTE: if adding another -f*-prefix-map option then don't forget to
90*38fd1498Szrj    ignore it in DW_AT_producer (dwarf2out.c).  */
91*38fd1498Szrj 
92*38fd1498Szrj /* Linked lists of file_prefix_map structures.  */
93*38fd1498Szrj static file_prefix_map *macro_prefix_maps; /* -fmacro-prefix-map  */
94*38fd1498Szrj static file_prefix_map *debug_prefix_maps; /* -fdebug-prefix-map  */
95*38fd1498Szrj 
96*38fd1498Szrj /* Record a file prefix mapping for -fmacro-prefix-map.  */
97*38fd1498Szrj void
add_macro_prefix_map(const char * arg)98*38fd1498Szrj add_macro_prefix_map (const char *arg)
99*38fd1498Szrj {
100*38fd1498Szrj   add_prefix_map (macro_prefix_maps, arg, "-fmacro-prefix-map");
101*38fd1498Szrj }
102*38fd1498Szrj 
103*38fd1498Szrj /* Record a file prefix mapping for -fdebug-prefix-map.  */
104*38fd1498Szrj void
add_debug_prefix_map(const char * arg)105*38fd1498Szrj add_debug_prefix_map (const char *arg)
106*38fd1498Szrj {
107*38fd1498Szrj   add_prefix_map (debug_prefix_maps, arg, "-fdebug-prefix-map");
108*38fd1498Szrj }
109*38fd1498Szrj 
110*38fd1498Szrj /* Record a file prefix mapping for all -f*-prefix-map.  */
111*38fd1498Szrj void
add_file_prefix_map(const char * arg)112*38fd1498Szrj add_file_prefix_map (const char *arg)
113*38fd1498Szrj {
114*38fd1498Szrj   add_prefix_map (macro_prefix_maps, arg, "-ffile-prefix-map");
115*38fd1498Szrj   add_prefix_map (debug_prefix_maps, arg, "-ffile-prefix-map");
116*38fd1498Szrj }
117*38fd1498Szrj 
118*38fd1498Szrj /* Remap using -fmacro-prefix-map.  Return the GC-allocated new name
119*38fd1498Szrj    corresponding to FILENAME or FILENAME if no remapping was performed.  */
120*38fd1498Szrj const char *
remap_macro_filename(const char * filename)121*38fd1498Szrj remap_macro_filename (const char *filename)
122*38fd1498Szrj {
123*38fd1498Szrj   return remap_filename (macro_prefix_maps, filename);
124*38fd1498Szrj }
125*38fd1498Szrj 
126*38fd1498Szrj /* Remap using -fdebug-prefix-map.  Return the GC-allocated new name
127*38fd1498Szrj    corresponding to FILENAME or FILENAME if no remapping was performed.  */
128*38fd1498Szrj const char *
remap_debug_filename(const char * filename)129*38fd1498Szrj remap_debug_filename (const char *filename)
130*38fd1498Szrj {
131*38fd1498Szrj   return remap_filename (debug_prefix_maps, filename);
132*38fd1498Szrj }
133