Repository navigation
Expand file tree
/
Copy pathlocation.cpp
More file actions
177 lines (154 loc) · 4.75 KB
/
Copy pathlocation.cpp
File metadata and controls
177 lines (154 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <algorithm>
#include <sstream>
#include "ast/context.h"
#include "ast/location.h"
namespace bpftrace::ast {
std::string SourceLocation::filename() const
{
if (source_) {
return source_->filename;
}
return "";
}
std::string SourceLocation::source_location() const
{
std::stringstream ss;
if (source_) {
ss << source_->filename << ":";
}
if (begin.line != end.line) {
ss << begin.line << "-" << end.line;
return ss.str();
}
ss << begin.line << ":";
ss << begin.column << "-" << end.column;
return ss.str();
}
std::ostream &operator<<(std::ostream &out, const SourceLocation &loc)
{
out << loc.source_location();
return out;
}
SourceLocation operator+(const SourceLocation &orig,
const SourceLocation &other)
{
// The parser constructs source locations that don't have any source. These
// are quickly joined into locations that do have source, so we allow for this
// and quickly converge to what is expected.
auto source = orig.source_;
if (source == nullptr && other.source_ != nullptr) {
source = other.source_;
} else if (other.source_ != nullptr) {
assert(source == other.source_);
}
auto result = SourceLocation(source);
result.begin.line = std::min(orig.begin.line, other.begin.line);
result.end.line = std::max(orig.end.line, other.end.line);
if (orig.begin.line < other.begin.line) {
result.begin.column = orig.begin.column;
} else if (orig.begin.line == other.begin.line) {
result.begin.column = std::min(orig.begin.column, other.begin.column);
} else {
result.begin.column = other.begin.column;
}
if (orig.end.line < other.end.line) {
result.end.column = other.end.column;
} else if (orig.end.line == other.end.line) {
result.end.column = std::max(orig.end.column, other.end.column);
} else {
result.end.column = orig.end.column;
}
return result;
}
std::vector<std::string> SourceLocation::source_context() const
{
std::vector<std::string> result;
// Is there source available?
if (!source_ || begin.line == 0) {
return result;
}
// Multi-lines just include all context.
if (begin.line != end.line) {
assert(begin.line < end.line);
for (int i = begin.line; i <= end.line; i++) {
assert(i <= static_cast<int>(source_->lines_.size()));
result.push_back(source_->lines_[i - 1]);
}
return result;
}
// Single line includes just the relevant context.
if (begin.line > static_cast<int>(source_->lines_.size())) {
return result; // Nothing available.
}
auto &srcline = source_->lines_[begin.line - 1];
std::stringstream orig;
for (auto c : srcline) {
if (c == '\t')
orig << " ";
else
orig << c;
}
result.emplace_back(orig.str());
std::stringstream select;
for (int x = 0; x < static_cast<int>(srcline.size()) && x < end.column - 1;
x++) {
char marker = x < begin.column - 1 ? ' ' : '~';
if (srcline[x] == '\t') {
select << std::string(4, marker);
} else {
select << marker;
}
}
result.emplace_back(select.str());
return result;
}
Location operator+(const Location &orig, const Location &expansion)
{
if (expansion == nullptr) {
return orig;
}
if (orig == nullptr) {
return expansion;
}
auto nlink = std::make_shared<LocationChain>(expansion->current);
nlink->parent.emplace(LocationChain::Context(Location(orig)));
nlink->parent->msg << "expanded from";
return nlink;
}
std::strong_ordering operator<=>(const SourceLocation::Position &lhs,
const SourceLocation::Position &rhs)
{
if (auto cmp = lhs.line <=> rhs.line; cmp != 0) {
return cmp;
}
return lhs.column <=> rhs.column;
}
std::strong_ordering operator<=>(const SourceLocation &lhs,
const SourceLocation &rhs)
{
if (auto cmp = lhs.begin <=> rhs.begin; cmp != 0) {
return cmp;
}
// If begin positions are equal, compare by span length. The shortest spans
// are considered to be the first nodes here. This allows us to use
// lower_bound matching to find the first reasonable matching node for
// metadata.
auto lhs_span_lines = lhs.end.line - lhs.begin.line;
auto rhs_span_lines = rhs.end.line - rhs.begin.line;
if (auto cmp = lhs_span_lines <=> rhs_span_lines; cmp != 0) {
return cmp;
}
auto lhs_span_cols = lhs.end.column - lhs.begin.column;
auto rhs_span_cols = rhs.end.column - rhs.begin.column;
return lhs_span_cols <=> rhs_span_cols;
}
bool operator==(const SourceLocation::Position &lhs,
const SourceLocation::Position &rhs)
{
return lhs.line == rhs.line && lhs.column == rhs.column;
}
bool operator==(const SourceLocation &lhs, const SourceLocation &rhs)
{
return lhs.begin == rhs.begin && lhs.end == rhs.end;
}
} // namespace bpftrace::ast