forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceMap.cpp
More file actions
71 lines (64 loc) · 2.17 KB
/
SourceMap.cpp
File metadata and controls
71 lines (64 loc) · 2.17 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/SourceMap/SourceMap.h"
using namespace hermes;
namespace hermes {
llvh::Optional<SourceMapTextLocation> SourceMap::getLocationForAddress(
uint32_t line,
uint32_t column) const {
auto seg = this->getSegmentForAddress(line, column);
// Unmapped location
if (!seg.hasValue() || !seg->representedLocation.hasValue()) {
return llvh::None;
}
// parseSegment() should have validated this.
assert(
(size_t)seg->representedLocation->sourceIndex < sources_.size() &&
"SourceIndex is out-of-range.");
std::string fileName =
getSourceFullPath(seg->representedLocation->sourceIndex);
return SourceMapTextLocation{
std::move(fileName),
(uint32_t)seg->representedLocation->lineIndex + 1,
(uint32_t)seg->representedLocation->columnIndex + 1};
}
llvh::Optional<SourceMap::Segment> SourceMap::getSegmentForAddress(
uint32_t line,
uint32_t column) const {
if (line == 0 || line > lines_.size()) {
return llvh::None;
}
// line is 1-based.
uint32_t lineIndex = line - 1;
auto &segments = lines_[lineIndex];
if (segments.empty()) {
return llvh::None;
}
assert(column >= 1 && "the column argument to this function is 1-based");
uint32_t columnIndex = column - 1;
// Algorithm: we wanted to locate the segment covering
// the needle(`column`) -- segment.generatedColumn <= column.
// We achieve it by binary searching the first sentinel
// segment strictly greater than needle(`column`) and then move backward
// one slot.
auto segIter = std::upper_bound(
segments.begin(),
segments.end(),
columnIndex,
[](uint32_t column, const Segment &seg) {
return column < (uint32_t)seg.generatedColumn;
});
// The found sentinel segment is the first one. No covering segment.
if (segIter == segments.begin()) {
return llvh::None;
}
// Move back one slot.
const Segment &target =
segIter == segments.end() ? segments.back() : *(--segIter);
return target;
}
} // namespace hermes