1 // go-linemap.cc -- GCC implementation of Linemap.
3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #include "go-linemap.h"
11 // This class implements the Linemap interface defined by the
14 class Gcc_linemap
: public Linemap
23 start_file(const char* file_name
, unsigned int line_begin
);
26 start_line(unsigned int line_number
, unsigned int line_size
);
29 get_location(unsigned int column
);
38 location_file(Location
);
41 location_line(Location
);
45 get_predeclared_location();
48 get_unknown_location();
51 is_predeclared(Location
);
57 // Whether we are currently reading a file.
61 Linemap
* Linemap::instance_
= NULL
;
63 // Start getting locations from a new file.
66 Gcc_linemap::start_file(const char *file_name
, unsigned line_begin
)
69 linemap_add(line_table
, LC_LEAVE
, 0, NULL
, 0);
70 linemap_add(line_table
, LC_ENTER
, 0, file_name
, line_begin
);
71 this->in_file_
= true;
74 // Stringify a location
77 Gcc_linemap::to_string(Location location
)
79 const line_map_ordinary
*lmo
;
80 location_t resolved_location
;
82 // Screen out unknown and predeclared locations; produce output
83 // only for simple file:line locations.
85 linemap_resolve_location (line_table
, location
.gcc_location(),
86 LRK_SPELLING_LOCATION
, &lmo
);
87 if (lmo
== NULL
|| resolved_location
< RESERVED_LOCATION_COUNT
)
89 const char *path
= LINEMAP_FILE (lmo
);
93 // Strip the source file down to the base file, to reduce clutter.
95 ss
<< lbasename(path
) << ":" << SOURCE_LINE (lmo
, location
.gcc_location());
99 // Return the file name for a given location.
102 Gcc_linemap::location_file(Location loc
)
104 return LOCATION_FILE(loc
.gcc_location());
107 // Return the line number for a given location.
110 Gcc_linemap::location_line(Location loc
)
112 return LOCATION_LINE(loc
.gcc_location());
115 // Stop getting locations.
120 linemap_add(line_table
, LC_LEAVE
, 0, NULL
, 0);
121 this->in_file_
= false;
127 Gcc_linemap::start_line(unsigned lineno
, unsigned linesize
)
129 linemap_line_start(line_table
, lineno
, linesize
);
135 Gcc_linemap::get_location(unsigned column
)
137 return Location(linemap_position_for_column(line_table
, column
));
140 // Get the unknown location.
143 Gcc_linemap::get_unknown_location()
145 return Location(UNKNOWN_LOCATION
);
148 // Get the predeclared location.
151 Gcc_linemap::get_predeclared_location()
153 return Location(BUILTINS_LOCATION
);
156 // Return whether a location is the predeclared location.
159 Gcc_linemap::is_predeclared(Location loc
)
161 return loc
.gcc_location() == BUILTINS_LOCATION
;
164 // Return whether a location is the unknown location.
167 Gcc_linemap::is_unknown(Location loc
)
169 return loc
.gcc_location() == UNKNOWN_LOCATION
;
172 // Return the Linemap to use for the gcc backend.
177 return new Gcc_linemap
;