Skip to content

Commit 671782d

Browse files
James Reedfacebook-github-bot
authored andcommitted
Refactor file:line:col to be less ugly (#22177)
Summary: Pull Request resolved: #22177 ghimport-source-id: e35f068 Test Plan: Imported from OSS Differential Revision: D15981424 Pulled By: jamesr66a fbshipit-source-id: b7748c5cfd4f8ea594314cb601a2b8045173700a
1 parent dff2c07 commit 671782d

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

torch/csrc/jit/ir.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,13 @@ std::ostream& Node::print(
251251
}
252252

253253
// In debug print, append file:line:col as a comment after each node
254-
if (print_source_locations && source_range_ &&
255-
source_range_->source()->filename()) {
256-
const auto& range = sourceRange();
257-
const auto& source = range.source();
258-
auto lineno = source->lineno_for_offset(range.start());
259-
auto col_offset = (int)range.start() - (int)source->offset_for_line(lineno);
260-
out << " # " << source->filename().value() << ":"
261-
<< source->lineno_to_source_lineno(lineno) << ":" << col_offset;
254+
if (print_source_locations) {
255+
if (auto file_line_col = sourceRange().file_line_col()) {
256+
std::string filename;
257+
size_t line, col;
258+
std::tie(filename, line, col) = *file_line_col;
259+
out << " # " << filename << ":" << line << ":" << col;
260+
}
262261
}
263262

264263
out << "\n";

torch/csrc/jit/source_range.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,11 @@ C10_EXPORT void SourceRange::highlight(std::ostream& out) const {
4242
}
4343
AT_ASSERT(end_highlight == str.size() || str[end_highlight] == '\n');
4444

45-
if (source_->filename()) {
46-
auto lineno = source_->lineno_for_offset(start());
47-
auto col_offset = (int)start() -
48-
(int)source_->offset_for_line(lineno);
49-
out << "at " << *source_->filename() << ":"
50-
<< source_->lineno_to_source_lineno(lineno) << ":" << col_offset
51-
<< "\n";
45+
if (auto flc = file_line_col()) {
46+
std::string filename;
47+
size_t line, col;
48+
std::tie(filename, line, col) = *flc;
49+
out << "at " << filename << ":" << line << ":" << col << "\n";
5250
}
5351
out << str.substr(begin_highlight, end_line - begin_highlight) << "\n";
5452
out << std::string(start() - begin_line, ' ');

torch/csrc/jit/source_range.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ struct CAFFE2_API SourceRange {
117117
return ss.str();
118118
}
119119

120+
c10::optional<std::tuple<std::string, size_t, size_t>> file_line_col() const {
121+
if (!source_ || !source()->filename()) {
122+
return c10::nullopt;
123+
}
124+
125+
auto lineno = source_->lineno_for_offset(start_);
126+
auto col_offset = (int)start_ - (int)source_->offset_for_line(lineno);
127+
// TODO: c10::optional<>::value returns an rvalue ref so can't use it here??
128+
return std::make_tuple<std::string, size_t, size_t>(
129+
source_->filename().value_or(""),
130+
source_->lineno_to_source_lineno(lineno),
131+
(size_t)col_offset);
132+
}
133+
120134
private:
121135
std::shared_ptr<Source> source_;
122136
size_t start_;

0 commit comments

Comments
 (0)