Skip to content

Commit 021123d

Browse files
cleanup: Move formatting function to core::Loc for reuse.
1 parent 91258a4 commit 021123d

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

cfg/CFG.cc

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,16 @@ string CFG::toString(const core::GlobalState &gs) const {
238238
return to_string(buf);
239239
}
240240

241-
string locToString(const core::GlobalState &gs, core::Loc loc) {
242-
if (!loc.exists() || loc.empty()) {
243-
return " @ <>";
244-
}
245-
auto [start, end] = loc.position(gs);
246-
return start.line == end.line ? fmt::format(" @ {}:{}-{}", start.line, start.column, end.column)
247-
: fmt::format(" @ {}:{}-{}:{}", start.line, start.column, end.line, end.column);
248-
}
249-
250241
string CFG::toTextualString(const core::GlobalState &gs, optional<core::FileRef> file) const {
251242
fmt::memory_buffer buf;
252243
string symbolName = this->symbol.showFullName(gs);
253244
if (file) {
254245
auto method = this->symbol.data(gs);
255246
if (method->nameLoc.exists() && !method->nameLoc.empty()) {
256-
fmt::format_to(std::back_inserter(buf), "method{} {} {{\n\n",
257-
locToString(gs, core::Loc(file.value(), method->nameLoc)), symbolName);
247+
fmt::format_to(std::back_inserter(buf), "method @ {} {} {{\n\n",
248+
core::Loc(file.value(), method->nameLoc).showRawLineColumn(gs), symbolName);
258249
} else {
259-
fmt::format_to(std::back_inserter(buf), "method{} (full) {} {{\n\n", locToString(gs, method->loc()),
250+
fmt::format_to(std::back_inserter(buf), "method @ {} (full) {} {{\n\n", method->loc().showRawLineColumn(gs),
260251
symbolName);
261252
}
262253
} else {
@@ -399,7 +390,7 @@ string BasicBlock::toTextualString(const core::GlobalState &gs, optional<core::F
399390
for (const Binding &exp : this->exprs) {
400391
string positionText = "";
401392
if (file) {
402-
positionText = locToString(gs, core::Loc(file.value(), exp.loc));
393+
positionText = fmt::format(" @ {}", core::Loc(file.value(), exp.loc).showRawLineColumn(gs));
403394
}
404395

405396
fmt::format_to(std::back_inserter(buf), " {}{} = {}\n", exp.bind.toString(gs, cfg), positionText,

core/Loc.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,18 @@ string Loc::showRaw(const GlobalState &gs) const {
235235
return fmt::format("Loc {{file={} start={}:{} end={}:{}}}", path, start.line, start.column, end.line, end.column);
236236
}
237237

238+
string Loc::showRawLineColumn(const core::GlobalState &gs) const {
239+
if (!this->exists()) {
240+
return "<>";
241+
}
242+
if (this->empty()) {
243+
return "<_>";
244+
}
245+
auto [start, end] = this->position(gs);
246+
return start.line == end.line ? fmt::format("{}:{}-{}", start.line, start.column, end.column)
247+
: fmt::format("{}:{}-{}:{}", start.line, start.column, end.line, end.column);
248+
}
249+
238250
string Loc::filePosToString(const GlobalState &gs, bool showFull) const {
239251
stringstream buf;
240252
if (!file().exists()) {

core/Loc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class Loc final {
9999
return toStringWithTabs(gs);
100100
}
101101
std::string showRaw(const GlobalState &gs) const;
102+
std::string showRawLineColumn(const GlobalState &gs) const;
102103
std::string filePosToString(const GlobalState &gs, bool showFull = false) const;
103104
std::optional<std::string_view> source(const GlobalState &gs) const;
104105

0 commit comments

Comments
 (0)