| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
// rust-linemap.cc -- GCC implementation of Linemap. |
| 20 |
|
| 21 |
#include "rust-linemap.h" |
| 22 |
|
| 23 |
Linemap *Linemap::instance_ = NULL; |
| 24 |
|
| 25 |
// Start getting locations from a new file. |
| 26 |
|
| 27 |
void |
| 28 |
Linemap::start_file (const char *file_name, unsigned line_begin) |
| 29 |
{ |
| 30 |
if (this->in_file_) |
| 31 |
linemap_add (line_table, LC_LEAVE, 0, NULL, 0); |
| 32 |
linemap_add (line_table, LC_ENTER, 0, file_name, line_begin); |
| 33 |
this->in_file_ = true; |
| 34 |
} |
| 35 |
|
| 36 |
// Stringify a location |
| 37 |
|
| 38 |
std::string |
| 39 |
Linemap::location_to_string (location_t location) |
| 40 |
{ |
| 41 |
const line_map_ordinary *lmo; |
| 42 |
location_t resolved_location; |
| 43 |
|
| 44 |
// Screen out unknown and predeclared locations; produce output |
| 45 |
// only for simple file:line locations. |
| 46 |
resolved_location = linemap_resolve_location (line_table, location, |
| 47 |
LRK_SPELLING_LOCATION, &lmo); |
| 48 |
if (lmo == NULL || resolved_location < RESERVED_LOCATION_COUNT) |
| 49 |
return ""; |
| 50 |
const char *path = LINEMAP_FILE (lmo); |
| 51 |
if (!path) |
| 52 |
return ""; |
| 53 |
|
| 54 |
// Strip the source file down to the base file, to reduce clutter. |
| 55 |
std::stringstream ss; |
| 56 |
ss << lbasename (path) << ":" << SOURCE_LINE (lmo, location) << ":" |
| 57 |
<< SOURCE_COLUMN (lmo, location); |
| 58 |
return ss.str (); |
| 59 |
} |
| 60 |
|
| 61 |
// Stop getting locations. |
| 62 |
|
| 63 |
void |
| 64 |
Linemap::stop () |
| 65 |
{ |
| 66 |
linemap_add (line_table, LC_LEAVE, 0, NULL, 0); |
| 67 |
this->in_file_ = false; |
| 68 |
} |
| 69 |
|
| 70 |
// Return the Linemap to use for the gcc backend. |
| 71 |
|
| 72 |
Linemap * |
| 73 |
rust_get_linemap () |
| 74 |
{ |
| 75 |
return new Linemap; |
| 76 |
} |