Line data Source code
1 : // go-linemap.cc -- GCC implementation of Linemap.
2 :
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.
6 :
7 : #include "go-linemap.h"
8 :
9 : #include "go-gcc.h"
10 :
11 : // This class implements the Linemap interface defined by the
12 : // frontend.
13 :
14 : class Gcc_linemap : public Linemap
15 : {
16 : public:
17 4646 : Gcc_linemap()
18 4646 : : Linemap(),
19 9292 : in_file_(false)
20 : { }
21 :
22 : void
23 : start_file(const char* file_name, unsigned int line_begin);
24 :
25 : void
26 : start_line(unsigned int line_number, unsigned int line_size);
27 :
28 : Location
29 : get_location(unsigned int column);
30 :
31 : void
32 : stop();
33 :
34 : std::string
35 : to_string(Location);
36 :
37 : std::string
38 : location_file(Location);
39 :
40 : int
41 : location_line(Location);
42 :
43 : protected:
44 : Location
45 : get_predeclared_location();
46 :
47 : Location
48 : get_unknown_location();
49 :
50 : bool
51 : is_predeclared(Location);
52 :
53 : bool
54 : is_unknown(Location);
55 :
56 : private:
57 : // Whether we are currently reading a file.
58 : bool in_file_;
59 : };
60 :
61 : Linemap* Linemap::instance_ = NULL;
62 :
63 : // Start getting locations from a new file.
64 :
65 : void
66 25096 : Gcc_linemap::start_file(const char *file_name, unsigned line_begin)
67 : {
68 25096 : if (this->in_file_)
69 18485 : linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
70 25096 : linemap_add(line_table, LC_ENTER, 0, file_name, line_begin);
71 25096 : this->in_file_ = true;
72 25096 : }
73 :
74 : // Stringify a location
75 :
76 : std::string
77 0 : Gcc_linemap::to_string(Location location)
78 : {
79 0 : const line_map_ordinary *lmo;
80 0 : location_t resolved_location;
81 :
82 : // Screen out unknown and predeclared locations; produce output
83 : // only for simple file:line locations.
84 0 : resolved_location =
85 0 : linemap_resolve_location (line_table, location.gcc_location(),
86 : LRK_SPELLING_LOCATION, &lmo);
87 0 : if (lmo == NULL || resolved_location < RESERVED_LOCATION_COUNT)
88 0 : return "";
89 0 : const char *path = LINEMAP_FILE (lmo);
90 0 : if (!path)
91 0 : return "";
92 :
93 : // Strip the source file down to the base file, to reduce clutter.
94 0 : std::stringstream ss;
95 0 : ss << lbasename(path) << ":" << SOURCE_LINE (lmo, location.gcc_location());
96 0 : return ss.str();
97 0 : }
98 :
99 : // Return the file name for a given location.
100 :
101 : std::string
102 16707 : Gcc_linemap::location_file(Location loc)
103 : {
104 16707 : return LOCATION_FILE(loc.gcc_location());
105 : }
106 :
107 : // Return the line number for a given location.
108 :
109 : int
110 122751 : Gcc_linemap::location_line(Location loc)
111 : {
112 122751 : return LOCATION_LINE(loc.gcc_location());
113 : }
114 :
115 : // Stop getting locations.
116 :
117 : void
118 4646 : Gcc_linemap::stop()
119 : {
120 4646 : linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
121 4646 : this->in_file_ = false;
122 4646 : }
123 :
124 : // Start a new line.
125 :
126 : void
127 3867257 : Gcc_linemap::start_line(unsigned lineno, unsigned linesize)
128 : {
129 3867257 : linemap_line_start(line_table, lineno, linesize);
130 3867257 : }
131 :
132 : // Get a location.
133 :
134 : Location
135 30894537 : Gcc_linemap::get_location(unsigned column)
136 : {
137 30894537 : return Location(linemap_position_for_column(line_table, column));
138 : }
139 :
140 : // Get the unknown location.
141 :
142 : Location
143 11104600 : Gcc_linemap::get_unknown_location()
144 : {
145 11104600 : return Location(UNKNOWN_LOCATION);
146 : }
147 :
148 : // Get the predeclared location.
149 :
150 : Location
151 6028024 : Gcc_linemap::get_predeclared_location()
152 : {
153 6028024 : return Location(BUILTINS_LOCATION);
154 : }
155 :
156 : // Return whether a location is the predeclared location.
157 :
158 : bool
159 9163351 : Gcc_linemap::is_predeclared(Location loc)
160 : {
161 9163351 : return loc.gcc_location() == BUILTINS_LOCATION;
162 : }
163 :
164 : // Return whether a location is the unknown location.
165 :
166 : bool
167 643030 : Gcc_linemap::is_unknown(Location loc)
168 : {
169 643030 : return loc.gcc_location() == UNKNOWN_LOCATION;
170 : }
171 :
172 : // Return the Linemap to use for the gcc backend.
173 :
174 : Linemap*
175 4646 : go_get_linemap()
176 : {
177 4646 : return new Gcc_linemap;
178 : }
|