PR c++/83634
[official-gcc.git] / gcc / go / go-linemap.cc
blob2accb95e5c476d66e83360f6ae8385c12863adf0
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 int
38 location_line(Location);
40 protected:
41 Location
42 get_predeclared_location();
44 Location
45 get_unknown_location();
47 bool
48 is_predeclared(Location);
50 bool
51 is_unknown(Location);
53 private:
54 // Whether we are currently reading a file.
55 bool in_file_;
58 Linemap* Linemap::instance_ = NULL;
60 // Start getting locations from a new file.
62 void
63 Gcc_linemap::start_file(const char *file_name, unsigned line_begin)
65 if (this->in_file_)
66 linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
67 linemap_add(line_table, LC_ENTER, 0, file_name, line_begin);
68 this->in_file_ = true;
71 // Stringify a location
73 std::string
74 Gcc_linemap::to_string(Location location)
76 const line_map_ordinary *lmo;
77 source_location resolved_location;
79 // Screen out unknown and predeclared locations; produce output
80 // only for simple file:line locations.
81 resolved_location =
82 linemap_resolve_location (line_table, location.gcc_location(),
83 LRK_SPELLING_LOCATION, &lmo);
84 if (lmo == NULL || resolved_location < RESERVED_LOCATION_COUNT)
85 return "";
86 const char *path = LINEMAP_FILE (lmo);
87 if (!path)
88 return "";
90 // Strip the source file down to the base file, to reduce clutter.
91 std::stringstream ss;
92 ss << lbasename(path) << ":" << SOURCE_LINE (lmo, location.gcc_location());
93 return ss.str();
96 // Return the line number for a given location (for debugging dumps)
97 int
98 Gcc_linemap::location_line(Location loc)
100 return LOCATION_LINE(loc.gcc_location());
103 // Stop getting locations.
105 void
106 Gcc_linemap::stop()
108 linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
109 this->in_file_ = false;
112 // Start a new line.
114 void
115 Gcc_linemap::start_line(unsigned lineno, unsigned linesize)
117 linemap_line_start(line_table, lineno, linesize);
120 // Get a location.
122 Location
123 Gcc_linemap::get_location(unsigned column)
125 return Location(linemap_position_for_column(line_table, column));
128 // Get the unknown location.
130 Location
131 Gcc_linemap::get_unknown_location()
133 return Location(UNKNOWN_LOCATION);
136 // Get the predeclared location.
138 Location
139 Gcc_linemap::get_predeclared_location()
141 return Location(BUILTINS_LOCATION);
144 // Return whether a location is the predeclared location.
146 bool
147 Gcc_linemap::is_predeclared(Location loc)
149 return loc.gcc_location() == BUILTINS_LOCATION;
152 // Return whether a location is the unknown location.
154 bool
155 Gcc_linemap::is_unknown(Location loc)
157 return loc.gcc_location() == UNKNOWN_LOCATION;
160 // Return the Linemap to use for the gcc backend.
162 Linemap*
163 go_get_linemap()
165 return new Gcc_linemap;