v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
source-positions.h
Go to the documentation of this file.
1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_TORQUE_SOURCE_POSITIONS_H_
6#define V8_TORQUE_SOURCE_POSITIONS_H_
7
8#include <iostream>
9
10#include "src/base/contextual.h"
11
12namespace v8 {
13namespace internal {
14namespace torque {
15
16struct SourcePosition;
17
18class SourceId {
19 public:
20 static SourceId Invalid() { return SourceId(-1); }
21 bool IsValid() const { return id_ != -1; }
22 int operator==(const SourceId& s) const { return id_ == s.id_; }
23 bool operator<(const SourceId& s) const { return id_ < s.id_; }
24
25 private:
26 explicit SourceId(int id) : id_(id) {}
27 int id_;
28 friend struct SourcePosition;
29 friend class SourceFileMap;
30};
31
33 static constexpr int kUnknownOffset = -1;
34
35 int offset;
36 int line;
37 int column;
38
39 static LineAndColumn Invalid() { return {-1, -1, -1}; }
40 static LineAndColumn WithUnknownOffset(int line, int column) {
41 return {kUnknownOffset, line, column};
42 }
43
44 bool operator==(const LineAndColumn& other) const {
45 if (offset == kUnknownOffset || other.offset == kUnknownOffset) {
46 return line == other.line && column == other.column;
47 }
48 DCHECK_EQ(offset == other.offset,
49 line == other.line && column == other.column);
50 return offset == other.offset;
51 }
52 bool operator!=(const LineAndColumn& other) const {
53 return !operator==(other);
54 }
55};
56
61
67
69 return start.line == pos.start.line && source == pos.source;
70 }
71
73 if (pos.line < start.line || pos.line > end.line) return false;
74
75 if (pos.line == start.line && pos.column < start.column) return false;
76 if (pos.line == end.line && pos.column >= end.column) return false;
77 return true;
78 }
79
80 bool operator==(const SourcePosition& pos) const {
81 return source == pos.source && start == pos.start && end == pos.end;
82 }
83 bool operator!=(const SourcePosition& pos) const { return !(*this == pos); }
84};
85
88
90 : public base::ContextualClass<SourceFileMap> {
91 public:
92 explicit SourceFileMap(std::string v8_root) : v8_root_(std::move(v8_root)) {}
93 static const std::string& PathFromV8Root(SourceId file);
94 static std::string PathFromV8RootWithoutExtension(SourceId file);
95 static std::string AbsolutePath(SourceId file);
96 static SourceId AddSource(std::string path);
97 static SourceId GetSourceId(const std::string& path);
98 static std::vector<SourceId> AllSources();
99 static bool FileRelativeToV8RootExists(const std::string& path);
100
101 private:
102 std::vector<std::string> sources_;
103 std::string v8_root_;
104};
105
108 std::to_string(pos.start.line + 1) + ":" +
109 std::to_string(pos.start.column + 1);
110}
111
112inline std::ostream& operator<<(std::ostream& out, SourcePosition pos) {
113 return out << "https://source.chromium.org/chromium/chromium/src/+/main:v8/"
115 << "?l=" << (pos.start.line + 1)
116 << "&c=" << (pos.start.column + 1);
117}
118
119} // namespace torque
120} // namespace internal
121} // namespace v8
122
123#endif // V8_TORQUE_SOURCE_POSITIONS_H_
SourcePosition pos
static const std::string & PathFromV8Root(SourceId file)
std::vector< std::string > sources_
int operator==(const SourceId &s) const
bool operator<(const SourceId &s) const
#define DECLARE_CONTEXTUAL_VARIABLE(VarName,...)
Definition contextual.h:88
STL namespace.
std::ostream & operator<<(std::ostream &os, Identifier *id)
Definition ast.h:263
std::string PositionAsString(SourcePosition pos)
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
static LineAndColumn WithUnknownOffset(int line, int column)
bool operator!=(const LineAndColumn &other) const
bool operator==(const LineAndColumn &other) const
bool Contains(LineAndColumn pos) const
bool CompareStartIgnoreColumn(const SourcePosition &pos) const
bool operator==(const SourcePosition &pos) const
bool operator!=(const SourcePosition &pos) const