Eliminate source_location in favor of location_t
[official-gcc.git] / gcc / go / go-linemap.cc
blob1d72e79647d80146f283e7caa189509881f01c5b
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"
9 #include "go-gcc.h"
11 // This class implements the Linemap interface defined by the
12 // frontend.
14 class Gcc_linemap : public Linemap
16 public:
17 Gcc_linemap()
18 : Linemap(),
19 in_file_(false)
20 { }
22 void
23 start_file(const char* file_name, unsigned int line_begin);
25 void
26 start_line(unsigned int line_number, unsigned int line_size);
28 Location
29 get_location(unsigned int column);
31 void
32 stop();
34 std::string
35 to_string(Location);
37 std::string
38 location_file(Location);
40 int
41 location_line(Location);
43 protected:
44 Location
45 get_predeclared_location();
47 Location
48 get_unknown_location();
50 bool
51 is_predeclared(Location);
53 bool
54 is_unknown(Location);
56 private:
57 // Whether we are currently reading a file.
58 bool in_file_;
61 Linemap* Linemap::instance_ = NULL;
63 // Start getting locations from a new file.
65 void
66 Gcc_linemap::start_file(const char *file_name, unsigned line_begin)
68 if (this->in_file_)
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
76 std::string
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.
84 resolved_location =
85 linemap_resolve_location (line_table, location.gcc_location(),
86 LRK_SPELLING_LOCATION, &lmo);
87 if (lmo == NULL || resolved_location < RESERVED_LOCATION_COUNT)
88 return "";
89 const char *path = LINEMAP_FILE (lmo);
90 if (!path)
91 return "";
93 // Strip the source file down to the base file, to reduce clutter.
94 std::stringstream ss;
95 ss << lbasename(path) << ":" << SOURCE_LINE (lmo, location.gcc_location());
96 return ss.str();
99 // Return the file name for a given location.
101 std::string
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.
117 void
118 Gcc_linemap::stop()
120 linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
121 this->in_file_ = false;
124 // Start a new line.
126 void
127 Gcc_linemap::start_line(unsigned lineno, unsigned linesize)
129 linemap_line_start(line_table, lineno, linesize);
132 // Get a location.
134 Location
135 Gcc_linemap::get_location(unsigned column)
137 return Location(linemap_position_for_column(line_table, column));
140 // Get the unknown location.
142 Location
143 Gcc_linemap::get_unknown_location()
145 return Location(UNKNOWN_LOCATION);
148 // Get the predeclared location.
150 Location
151 Gcc_linemap::get_predeclared_location()
153 return Location(BUILTINS_LOCATION);
156 // Return whether a location is the predeclared location.
158 bool
159 Gcc_linemap::is_predeclared(Location loc)
161 return loc.gcc_location() == BUILTINS_LOCATION;
164 // Return whether a location is the unknown location.
166 bool
167 Gcc_linemap::is_unknown(Location loc)
169 return loc.gcc_location() == UNKNOWN_LOCATION;
172 // Return the Linemap to use for the gcc backend.
174 Linemap*
175 go_get_linemap()
177 return new Gcc_linemap;