2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
26
26
#include < assert.h>
27
27
#include < functional>
28
28
29
+ namespace llvm {
30
+ class SMLoc ;
31
+ }
32
+
29
33
namespace swift {
30
- class SourceManager ;
34
+ class SourceManager ;
31
35
32
- // / SourceLoc in swift is just an SMLoc . We define it as a different type
33
- // / (instead of as a typedef) just to remove the "getFromPointer" methods and
34
- // / enforce purity in the Swift codebase.
36
+ // / ` SourceLoc` just wraps a `const char *` . We define it as a different type
37
+ // / (instead of as a typedef) from `llvm::SMLoc` to enforce purity in the
38
+ // / Swift codebase.
35
39
class SourceLoc {
36
40
friend class SourceManager ;
37
41
friend class SourceRange ;
38
42
friend class CharSourceRange ;
39
43
friend class DiagnosticConsumer ;
40
44
41
- llvm::SMLoc Value ;
45
+ const char *Pointer = nullptr ;
42
46
43
47
public:
44
48
SourceLoc () {}
45
- explicit SourceLoc (llvm::SMLoc Value) : Value(Value) {}
46
-
47
- bool isValid () const { return Value.isValid (); }
49
+
50
+ static SourceLoc getFromPointer (const char *Pointer) {
51
+ SourceLoc Loc;
52
+ Loc.Pointer = Pointer;
53
+ return Loc;
54
+ }
55
+
56
+ const char *getPointer () const { return Pointer; }
57
+
58
+ bool isValid () const { return Pointer != nullptr ; }
48
59
bool isInvalid () const { return !isValid (); }
49
60
50
61
// / An explicit bool operator so one can check if a SourceLoc is valid in an
@@ -53,14 +64,15 @@ class SourceLoc {
53
64
// / if (auto x = getSourceLoc()) { ... }
54
65
explicit operator bool () const { return isValid (); }
55
66
56
- bool operator ==(const SourceLoc &RHS) const { return RHS.Value == Value; }
67
+ operator llvm::SMLoc () const { return llvm::SMLoc::getFromPointer (Pointer); }
68
+
69
+ bool operator ==(const SourceLoc &RHS) const { return RHS.Pointer == Pointer; }
57
70
bool operator !=(const SourceLoc &RHS) const { return !operator ==(RHS); }
58
-
71
+
59
72
// / Return a source location advanced a specified number of bytes.
60
73
SourceLoc getAdvancedLoc (int ByteOffset) const {
61
74
assert (isValid () && " Can't advance an invalid location" );
62
- return SourceLoc (
63
- llvm::SMLoc::getFromPointer (Value.getPointer () + ByteOffset));
75
+ return SourceLoc::getFromPointer (Pointer + ByteOffset);
64
76
}
65
77
66
78
SourceLoc getAdvancedLocOrInvalid (int ByteOffset) const {
@@ -69,7 +81,7 @@ class SourceLoc {
69
81
return SourceLoc ();
70
82
}
71
83
72
- const void *getOpaquePointerValue () const { return Value. getPointer () ; }
84
+ const void *getOpaquePointerValue () const { return Pointer ; }
73
85
74
86
// / Print out the SourceLoc. If this location is in the same buffer
75
87
// / as specified by \c LastBufferID, then we don't print the filename. If
@@ -88,13 +100,13 @@ class SourceLoc {
88
100
89
101
SWIFT_DEBUG_DUMPER (dump(const SourceManager &SM));
90
102
91
- friend size_t hash_value (SourceLoc loc) {
92
- return reinterpret_cast <uintptr_t >(loc.getOpaquePointerValue ());
93
- }
103
+ friend size_t hash_value (SourceLoc loc) {
104
+ return reinterpret_cast <uintptr_t >(loc.getOpaquePointerValue ());
105
+ }
94
106
95
- friend void simple_display (raw_ostream &OS, const SourceLoc &loc) {
96
- // Nothing meaningful to print.
97
- }
107
+ friend void simple_display (raw_ostream &OS, const SourceLoc &loc) {
108
+ // Nothing meaningful to print.
109
+ }
98
110
};
99
111
100
112
// / SourceRange in swift is a pair of locations. However, note that the end
@@ -210,27 +222,27 @@ class CharSourceRange {
210
222
bool contains (SourceLoc loc) const {
211
223
auto less = std::less<const char *>();
212
224
auto less_equal = std::less_equal<const char *>();
213
- return less_equal (getStart ().Value . getPointer () , loc.Value . getPointer () ) &&
214
- less (loc.Value . getPointer () , getEnd ().Value . getPointer () );
225
+ return less_equal (getStart ().Pointer , loc.Pointer ) &&
226
+ less (loc.Pointer , getEnd ().Pointer );
215
227
}
216
228
217
229
bool contains (CharSourceRange Other) const {
218
230
auto less_equal = std::less_equal<const char *>();
219
231
return contains (Other.getStart ()) &&
220
- less_equal (Other.getEnd ().Value . getPointer () , getEnd ().Value . getPointer () );
232
+ less_equal (Other.getEnd ().Pointer , getEnd ().Pointer );
221
233
}
222
234
223
235
// / expands *this to cover Other
224
236
void widen (CharSourceRange Other) {
225
- auto Diff = Other.getEnd ().Value . getPointer () - getEnd ().Value . getPointer () ;
237
+ auto Diff = Other.getEnd ().Pointer - getEnd ().Pointer ;
226
238
if (Diff > 0 ) {
227
239
ByteLength += Diff;
228
240
}
229
- const auto MyStartPtr = getStart ().Value . getPointer () ;
230
- Diff = MyStartPtr - Other.getStart ().Value . getPointer () ;
241
+ const auto MyStartPtr = getStart ().Pointer ;
242
+ Diff = MyStartPtr - Other.getStart ().Pointer ;
231
243
if (Diff > 0 ) {
232
244
ByteLength += Diff;
233
- Start = SourceLoc ( llvm::SMLoc:: getFromPointer (MyStartPtr - Diff) );
245
+ Start = SourceLoc:: getFromPointer (MyStartPtr - Diff);
234
246
}
235
247
}
236
248
@@ -239,9 +251,7 @@ class CharSourceRange {
239
251
return contains (Other.getStart ()) || Other.contains (getStart ());
240
252
}
241
253
242
- StringRef str () const {
243
- return StringRef (Start.Value .getPointer (), ByteLength);
244
- }
254
+ StringRef str () const { return StringRef (Start.Pointer , ByteLength); }
245
255
246
256
// / Return the length of this valid range in bytes. Can be zero.
247
257
unsigned getByteLength () const {
@@ -272,15 +282,15 @@ template <typename T, typename Enable> struct DenseMapInfo;
272
282
273
283
template <> struct DenseMapInfo <swift::SourceLoc> {
274
284
static swift::SourceLoc getEmptyKey () {
275
- return swift::SourceLoc (
276
- SMLoc::getFromPointer ( DenseMapInfo<const char *>::getEmptyKey () ));
285
+ return swift::SourceLoc::getFromPointer (
286
+ DenseMapInfo<const char *>::getEmptyKey ());
277
287
}
278
288
279
289
static swift::SourceLoc getTombstoneKey () {
280
290
// Make this different from empty key. See for context:
281
291
// http://lists.llvm.org/pipermail/llvm-dev/2015-July/088744.html
282
- return swift::SourceLoc (
283
- SMLoc::getFromPointer ( DenseMapInfo<const char *>::getTombstoneKey () ));
292
+ return swift::SourceLoc::getFromPointer (
293
+ DenseMapInfo<const char *>::getTombstoneKey ());
284
294
}
285
295
286
296
static unsigned getHashValue (const swift::SourceLoc &Val) {
@@ -296,15 +306,15 @@ template <> struct DenseMapInfo<swift::SourceLoc> {
296
306
297
307
template <> struct DenseMapInfo <swift::SourceRange> {
298
308
static swift::SourceRange getEmptyKey () {
299
- return swift::SourceRange (swift::SourceLoc (
300
- SMLoc::getFromPointer ( DenseMapInfo<const char *>::getEmptyKey () )));
309
+ return swift::SourceRange (swift::SourceLoc::getFromPointer (
310
+ DenseMapInfo<const char *>::getEmptyKey ()));
301
311
}
302
312
303
313
static swift::SourceRange getTombstoneKey () {
304
314
// Make this different from empty key. See for context:
305
315
// http://lists.llvm.org/pipermail/llvm-dev/2015-July/088744.html
306
- return swift::SourceRange (swift::SourceLoc (
307
- SMLoc::getFromPointer ( DenseMapInfo<const char *>::getTombstoneKey () )));
316
+ return swift::SourceRange (swift::SourceLoc::getFromPointer (
317
+ DenseMapInfo<const char *>::getTombstoneKey ()));
308
318
}
309
319
310
320
static unsigned getHashValue (const swift::SourceRange &Val) {
0 commit comments