Skip to content

Commit 06a5670

Browse files
committed
Basic: Untie swift::SourceLoc from llvm::SMLoc
Storing a `llvm::SMLoc` is a superfluous indirection, and getting rid of it enables us to unconditionally import `SourceLoc` into Swift.
1 parent 8cbf5a2 commit 06a5670

16 files changed

+116
-116
lines changed

include/swift/APIDigester/ModuleAnalyzerNodes.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -242,7 +242,9 @@ class SDKContext {
242242
void diagnose(YAMLNodeTy node, Diag<ArgTypes...> ID,
243243
typename detail::PassArgument<ArgTypes>::type... args) {
244244
auto smRange = node->getSourceRange();
245-
auto range = SourceRange(SourceLoc(smRange.Start), SourceLoc(smRange.End));
245+
auto range =
246+
SourceRange(SourceLoc::getFromPointer(smRange.Start.getPointer()),
247+
SourceLoc::getFromPointer(smRange.End.getPointer()));
246248
Diags.diagnose(range.Start, ID, std::forward<ArgTypes>(args)...)
247249
.highlight(range);
248250
}

include/swift/AST/UnsafeUse.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -247,14 +247,12 @@ class UnsafeUse {
247247
return getDecl()->getLoc();
248248

249249
case UnsafeConformance:
250-
return SourceLoc(
251-
llvm::SMLoc::getFromPointer(
252-
(const char *)storage.conformance.location));
250+
return SourceLoc::getFromPointer(
251+
(const char *)storage.conformance.location);
253252

254253
case TypeWitness:
255-
return SourceLoc(
256-
llvm::SMLoc::getFromPointer(
257-
(const char *)storage.typeWitness.location));
254+
return SourceLoc::getFromPointer(
255+
(const char *)storage.typeWitness.location);
258256

259257
case UnownedUnsafe:
260258
case ExclusivityUnchecked:
@@ -263,8 +261,7 @@ class UnsafeUse {
263261
case ReferenceToUnsafeStorage:
264262
case ReferenceToUnsafeThroughTypealias:
265263
case CallToUnsafe:
266-
return SourceLoc(
267-
llvm::SMLoc::getFromPointer((const char *)storage.entity.location));
264+
return SourceLoc::getFromPointer((const char *)storage.entity.location);
268265

269266
case CallArgument:
270267
return storage.callArgument.call->getLoc();

include/swift/Basic/BasicBridgingImpl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ BridgedSourceLoc::BridgedSourceLoc(swift::SourceLoc loc)
4646
: Raw(loc.getOpaquePointerValue()) {}
4747

4848
swift::SourceLoc BridgedSourceLoc::unbridged() const {
49-
return swift::SourceLoc(
50-
llvm::SMLoc::getFromPointer(static_cast<const char *>(Raw)));
49+
return swift::SourceLoc::getFromPointer(static_cast<const char *>(Raw));
5150
}
5251

5352
BridgedSourceLoc BridgedSourceLoc::advancedBy(size_t n) const {

include/swift/Basic/SourceLoc.h

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -26,25 +26,36 @@
2626
#include <assert.h>
2727
#include <functional>
2828

29+
namespace llvm {
30+
class SMLoc;
31+
}
32+
2933
namespace swift {
30-
class SourceManager;
34+
class SourceManager;
3135

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.
3539
class SourceLoc {
3640
friend class SourceManager;
3741
friend class SourceRange;
3842
friend class CharSourceRange;
3943
friend class DiagnosticConsumer;
4044

41-
llvm::SMLoc Value;
45+
const char *Pointer = nullptr;
4246

4347
public:
4448
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; }
4859
bool isInvalid() const { return !isValid(); }
4960

5061
/// An explicit bool operator so one can check if a SourceLoc is valid in an
@@ -53,14 +64,15 @@ class SourceLoc {
5364
/// if (auto x = getSourceLoc()) { ... }
5465
explicit operator bool() const { return isValid(); }
5566

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; }
5770
bool operator!=(const SourceLoc &RHS) const { return !operator==(RHS); }
58-
71+
5972
/// Return a source location advanced a specified number of bytes.
6073
SourceLoc getAdvancedLoc(int ByteOffset) const {
6174
assert(isValid() && "Can't advance an invalid location");
62-
return SourceLoc(
63-
llvm::SMLoc::getFromPointer(Value.getPointer() + ByteOffset));
75+
return SourceLoc::getFromPointer(Pointer + ByteOffset);
6476
}
6577

6678
SourceLoc getAdvancedLocOrInvalid(int ByteOffset) const {
@@ -69,7 +81,7 @@ class SourceLoc {
6981
return SourceLoc();
7082
}
7183

72-
const void *getOpaquePointerValue() const { return Value.getPointer(); }
84+
const void *getOpaquePointerValue() const { return Pointer; }
7385

7486
/// Print out the SourceLoc. If this location is in the same buffer
7587
/// as specified by \c LastBufferID, then we don't print the filename. If
@@ -88,13 +100,13 @@ class SourceLoc {
88100

89101
SWIFT_DEBUG_DUMPER(dump(const SourceManager &SM));
90102

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+
}
94106

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+
}
98110
};
99111

100112
/// SourceRange in swift is a pair of locations. However, note that the end
@@ -210,27 +222,27 @@ class CharSourceRange {
210222
bool contains(SourceLoc loc) const {
211223
auto less = std::less<const char *>();
212224
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);
215227
}
216228

217229
bool contains(CharSourceRange Other) const {
218230
auto less_equal = std::less_equal<const char *>();
219231
return contains(Other.getStart()) &&
220-
less_equal(Other.getEnd().Value.getPointer(), getEnd().Value.getPointer());
232+
less_equal(Other.getEnd().Pointer, getEnd().Pointer);
221233
}
222234

223235
/// expands *this to cover Other
224236
void widen(CharSourceRange Other) {
225-
auto Diff = Other.getEnd().Value.getPointer() - getEnd().Value.getPointer();
237+
auto Diff = Other.getEnd().Pointer - getEnd().Pointer;
226238
if (Diff > 0) {
227239
ByteLength += Diff;
228240
}
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;
231243
if (Diff > 0) {
232244
ByteLength += Diff;
233-
Start = SourceLoc(llvm::SMLoc::getFromPointer(MyStartPtr - Diff));
245+
Start = SourceLoc::getFromPointer(MyStartPtr - Diff);
234246
}
235247
}
236248

@@ -239,9 +251,7 @@ class CharSourceRange {
239251
return contains(Other.getStart()) || Other.contains(getStart());
240252
}
241253

242-
StringRef str() const {
243-
return StringRef(Start.Value.getPointer(), ByteLength);
244-
}
254+
StringRef str() const { return StringRef(Start.Pointer, ByteLength); }
245255

246256
/// Return the length of this valid range in bytes. Can be zero.
247257
unsigned getByteLength() const {
@@ -272,15 +282,15 @@ template <typename T, typename Enable> struct DenseMapInfo;
272282

273283
template <> struct DenseMapInfo<swift::SourceLoc> {
274284
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());
277287
}
278288

279289
static swift::SourceLoc getTombstoneKey() {
280290
// Make this different from empty key. See for context:
281291
// 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());
284294
}
285295

286296
static unsigned getHashValue(const swift::SourceLoc &Val) {
@@ -296,15 +306,15 @@ template <> struct DenseMapInfo<swift::SourceLoc> {
296306

297307
template <> struct DenseMapInfo<swift::SourceRange> {
298308
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()));
301311
}
302312

303313
static swift::SourceRange getTombstoneKey() {
304314
// Make this different from empty key. See for context:
305315
// 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()));
308318
}
309319

310320
static unsigned getHashValue(const swift::SourceRange &Val) {

include/swift/Basic/SourceManager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -311,7 +311,7 @@ class SourceManager {
311311

312312
/// Returns true if \c LHS is before \c RHS in the same source buffer.
313313
bool isBeforeInBuffer(SourceLoc LHS, SourceLoc RHS) const {
314-
return LHS.Value.getPointer() < RHS.Value.getPointer();
314+
return LHS.getPointer() < RHS.getPointer();
315315
}
316316

317317
/// Returns true if \c range contains the location \c loc. The location
@@ -485,7 +485,7 @@ class SourceManager {
485485
assert(Loc.isValid());
486486
int LineOffset = getLineOffset(Loc);
487487
int l, c;
488-
std::tie(l, c) = LLVMSourceMgr.getLineAndColumn(Loc.Value, BufferID);
488+
std::tie(l, c) = LLVMSourceMgr.getLineAndColumn(Loc, BufferID);
489489
assert(LineOffset+l > 0 && "bogus line offset");
490490
return { LineOffset + l, c };
491491
}
@@ -498,7 +498,7 @@ class SourceManager {
498498
std::pair<unsigned, unsigned>
499499
getLineAndColumnInBuffer(SourceLoc Loc, unsigned BufferID = 0) const {
500500
assert(Loc.isValid());
501-
return LLVMSourceMgr.getLineAndColumn(Loc.Value, BufferID);
501+
return LLVMSourceMgr.getLineAndColumn(Loc, BufferID);
502502
}
503503

504504
/// Returns the column for the given source location in the given buffer.

include/swift/Parse/Lexer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -414,7 +414,7 @@ class Lexer {
414414
static bool isOperator(StringRef string);
415415

416416
SourceLoc getLocForStartOfBuffer() const {
417-
return SourceLoc(llvm::SMLoc::getFromPointer(BufferStart));
417+
return SourceLoc::getFromPointer(BufferStart);
418418
}
419419

420420
/// StringSegment - A segment of a (potentially interpolated) string.
@@ -516,7 +516,7 @@ class Lexer {
516516
}
517517

518518
static SourceLoc getSourceLoc(const char *Loc) {
519-
return SourceLoc(llvm::SMLoc::getFromPointer(Loc));
519+
return SourceLoc::getFromPointer(Loc);
520520
}
521521

522522
/// Get the token that starts at the given location.

include/swift/Parse/Token.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -287,9 +287,7 @@ class Token {
287287

288288
/// getLoc - Return a source location identifier for the specified
289289
/// offset in the current file.
290-
SourceLoc getLoc() const {
291-
return SourceLoc(llvm::SMLoc::getFromPointer(Text.begin()));
292-
}
290+
SourceLoc getLoc() const { return SourceLoc::getFromPointer(Text.begin()); }
293291

294292
unsigned getLength() const { return Text.size(); }
295293

@@ -303,17 +301,15 @@ class Token {
303301

304302
CharSourceRange getCommentRange() const {
305303
if (CommentLength == 0)
306-
return CharSourceRange(SourceLoc(llvm::SMLoc::getFromPointer(Text.begin())),
307-
0);
304+
return CharSourceRange(SourceLoc::getFromPointer(Text.begin()), 0);
308305
auto TrimedComment = trimComment();
309-
return CharSourceRange(
310-
SourceLoc(llvm::SMLoc::getFromPointer(TrimedComment.begin())),
311-
TrimedComment.size());
306+
return CharSourceRange(SourceLoc::getFromPointer(TrimedComment.begin()),
307+
TrimedComment.size());
312308
}
313309

314310
SourceLoc getCommentStart() const {
315311
if (CommentLength == 0) return SourceLoc();
316-
return SourceLoc(llvm::SMLoc::getFromPointer(trimComment().begin()));
312+
return SourceLoc::getFromPointer(trimComment().begin());
317313
}
318314

319315
StringRef getRawText() const {

lib/AST/AccessNotes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -182,7 +182,7 @@ static void
182182
convertToErrorAndJoin(const llvm::SMDiagnostic &diag, void *ctxPtr) {
183183
ASTContext &ctx = *(ASTContext*)ctxPtr;
184184

185-
SourceLoc loc{diag.getLoc()};
185+
auto loc = SourceLoc::getFromPointer(diag.getLoc().getPointer());
186186
assert(ctx.SourceMgr.isOwning(loc));
187187

188188
switch (diag.getKind()) {

lib/AST/DiagnosticConsumer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -40,9 +40,7 @@ DiagnosticInfo::FixIt::FixIt(CharSourceRange R, StringRef Str,
4040
formatForFixIts());
4141
}
4242

43-
llvm::SMLoc DiagnosticConsumer::getRawLoc(SourceLoc loc) {
44-
return loc.Value;
45-
}
43+
llvm::SMLoc DiagnosticConsumer::getRawLoc(SourceLoc loc) { return loc; }
4644

4745
LLVM_ATTRIBUTE_UNUSED
4846
static bool hasDuplicateFileNames(

0 commit comments

Comments
 (0)