Skip to content

Commit 3b6d605

Browse files
authored
Merge pull request #37858 from bnbarham/ignore-invalid-loc
[AST] Do not return invalid SourceLocs from Decl::getLoc
2 parents 38c544a + fff50ba commit 3b6d605

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/AST/Decl.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,18 @@ const ExternalSourceLocs *Decl::getSerializedLocs() const {
636636
return &NullLocs;
637637
}
638638

639+
CharSourceRange BufferRange = SM.getRangeForBuffer(BufferID);
639640
auto ResolveLoc = [&](const ExternalSourceLocs::RawLoc &Raw) -> SourceLoc {
641+
// If the underlying source has been updated and the swiftsourceinfo hasn't,
642+
// make sure we don't produce invalid source locations. Ideally would check
643+
// the file hasn't been modified.
644+
if (Raw.Offset > BufferRange.getByteLength())
645+
return SourceLoc();
646+
640647
// If the decl had a presumed loc, create its virtual file so that
641-
// getPresumedLineAndColForLoc works from serialized locations as well.
648+
// getPresumedLineAndColForLoc works from serialized locations as well. No
649+
// need to check the buffer range, the directive must be before the location
650+
// itself.
642651
if (Raw.Directive.isValid()) {
643652
auto &LD = Raw.Directive;
644653
SourceLoc Loc = SM.getLocForOffset(BufferID, LD.Offset);

test/diagnostics/multi-module-diagnostics.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ open class SubClass: ParentClass {
5959
// CHECK-OUTOFDATE-NOT: moda.ParentClass:{{.*}}: note:
6060
// CHECK-OUTOFDATE: moda.swift:{{.*}}: note:
6161

62+
// Underlying file is empty, the locations are now completely invalid (ie. not
63+
// within the buffer). Make sure there's no crash and that we fallback to using
64+
// the generated source.
65+
// RUN: rm %t/moda.swift
66+
// RUN: touch %t/moda.swift
67+
// RUN: not %target-swift-frontend -typecheck -I %t/mods -D MODB %s 2>&1 | %FileCheck -check-prefix=CHECK-EMPTY %s
68+
// CHECK-EMPTY: moda.ParentClass:{{.*}}: note:
69+
6270
// The file and line from a location directive should be used whether or not it
6371
// exists - the actual source still comes from the original file, so that's what
6472
// matters in terms of whether generated code is used or not

0 commit comments

Comments
 (0)