2 #include <c10/util/Exception.h> 3 #include <torch/csrc/jit/source_location.h> 15 SourceRange(std::shared_ptr<std::string> file_,
size_t start_,
size_t end_)
16 : file_(std::move(file_)), start_(start_), end_(end_) {}
17 const std::string text()
const {
18 return file().substr(start(), end() - start());
21 return end() - start();
24 static const size_t CONTEXT = 10;
25 void highlight(std::ostream& out)
const override {
26 const std::string& str = file();
27 size_t begin_line = start();
28 size_t end_line = start();
29 while (begin_line > 0 && str[begin_line - 1] !=
'\n')
31 while (end_line < str.size() && str[end_line] !=
'\n')
33 AT_ASSERT(begin_line == 0 || str[begin_line - 1] ==
'\n');
34 AT_ASSERT(end_line == str.size() || str[end_line] ==
'\n');
36 size_t begin_highlight = begin_line;
38 for (
size_t i = 0; begin_highlight > 0; --begin_highlight) {
39 if (str[begin_highlight - 1] ==
'\n')
44 AT_ASSERT(begin_highlight == 0 || str[begin_highlight - 1] ==
'\n');
46 size_t end_highlight =
48 for (
size_t i = 0; end_highlight < str.size(); ++end_highlight) {
49 if (str[end_highlight] ==
'\n')
54 AT_ASSERT(end_highlight == str.size() || str[end_highlight] ==
'\n');
56 out << str.substr(begin_highlight, end_line - begin_highlight) <<
"\n";
57 out << std::string(start() - begin_line,
' ');
58 size_t len = std::min(size(), end_line - start());
59 out << std::string(len,
'~')
60 << (len < size() ?
"... <--- HERE" :
" <--- HERE");
61 out << str.substr(end_line, end_highlight - end_line);
62 if (!str.empty() && str.back() !=
'\n')
65 const std::string& file()
const {
68 const std::shared_ptr<std::string>& file_ptr()
const {
71 size_t start()
const {
79 std::shared_ptr<std::string> file_;
Represents a location in source code (for debugging).