Skip to content

Commit 6e710b2

Browse files
authored
Merge pull request #36594 from beccadax/nonstandard-library
Preload standard library in ModuleInterfaceBuilder
2 parents 0bc679c + 28c260e commit 6e710b2

File tree

9 files changed

+86
-0
lines changed

9 files changed

+86
-0
lines changed

include/swift/Basic/SourceManager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ class SourceManager {
290290
bool isLocInVirtualFile(SourceLoc Loc) const {
291291
return getVirtualFile(Loc) != nullptr;
292292
}
293+
294+
/// Return a SourceLoc in \c this corresponding to \p otherLoc, which must be
295+
/// owned by \p otherManager. Returns an invalid SourceLoc if it cannot be
296+
/// translated.
297+
SourceLoc getLocForForeignLoc(SourceLoc otherLoc, SourceManager &otherMgr);
293298
};
294299

295300
} // end namespace swift

lib/Basic/SourceLoc.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,27 @@ SourceManager::getLocFromExternalSource(StringRef Path, unsigned Line,
395395
return SourceLoc();
396396
return getLocForOffset(BufferId, *Offset);
397397
}
398+
399+
SourceLoc
400+
SourceManager::getLocForForeignLoc(SourceLoc otherLoc,
401+
SourceManager &otherMgr) {
402+
if (&otherMgr == this || otherLoc.isInvalid())
403+
return otherLoc;
404+
405+
assert(otherMgr.isOwning(otherLoc));
406+
407+
if (auto otherBufferID = otherMgr.findBufferContainingLocInternal(otherLoc)) {
408+
auto offset = otherMgr.getLocOffsetInBuffer(otherLoc, *otherBufferID);
409+
410+
auto otherBufferName = otherMgr.getIdentifierForBuffer(*otherBufferID);
411+
auto thisBufferID = getIDForBufferIdentifier(otherBufferName);
412+
if (!thisBufferID) {
413+
thisBufferID = addMemBufferCopy(
414+
otherMgr.getEntireTextForBuffer(*otherBufferID), otherBufferName);
415+
}
416+
417+
return getLocForOffset(*thisBufferID, offset);
418+
}
419+
420+
return SourceLoc();
421+
}

lib/Frontend/DiagnosticVerifier.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,22 @@ void DiagnosticVerifier::handleDiagnostic(SourceManager &SM,
972972
CapturedDiagnostics.emplace_back(message, StringRef(), Info.Kind, Info.Loc,
973973
0, 0, fixIts, eduNotes);
974974
}
975+
976+
// If this diagnostic came from a different SourceManager (as can happen
977+
// while compiling a module interface), translate its SourceLocs to match the
978+
// verifier's SourceManager.
979+
if (&SM != &(this->SM)) {
980+
auto &capturedDiag = CapturedDiagnostics.back();
981+
auto &correctSM = this->SM;
982+
983+
capturedDiag.Loc = correctSM.getLocForForeignLoc(capturedDiag.Loc, SM);
984+
for (auto &fixIt : capturedDiag.FixIts) {
985+
auto newStart = correctSM.getLocForForeignLoc(fixIt.getRange().getStart(),
986+
SM);
987+
fixIt.getRange() = CharSourceRange(newStart,
988+
fixIt.getRange().getByteLength());
989+
}
990+
}
975991
}
976992

977993
/// Once all diagnostics have been captured, perform verification.

lib/Frontend/Frontend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ bool CompilerInstance::setupDiagnosticVerifierIfNeeded() {
309309
Diagnostics.diagnose(SourceLoc(), diag::error_open_input_file,
310310
filename, result.getError().message());
311311
hadError |= true;
312+
continue;
312313
}
313314

314315
auto bufferID = SourceMgr.addNewSourceBuffer(std::move(result.get()));

lib/Frontend/ModuleInterfaceBuilder.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
187187
InputInfo.getPrimarySpecificPaths().SupplementaryOutputs;
188188
StringRef OutPath = OutputInfo.ModuleOutputPath;
189189

190+
// Bail out if we're going to use the standard library but can't load it. If
191+
// we don't do this before we try to build the interface, we could end up
192+
// trying to rebuild a broken standard library dozens of times due to
193+
// multiple calls to `ASTContext::getStdlibModule()`.
194+
if (SubInstance.loadStdlibIfNeeded())
195+
return std::make_error_code(std::errc::not_supported);
196+
190197
// Build the .swiftmodule; this is a _very_ abridged version of the logic
191198
// in performCompile in libFrontendTool, specialized, to just the one
192199
// module-serialization task we're trying to do here.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -target x86_64-apple-macos10.9 -module-name BadStdlib
3+
4+
// no-error@-3
5+
6+
// Tests whether -compile-module-from-interface correctly stops early when the
7+
// standard library module interface is broken, rather than trying to limp along
8+
// without a standard library, which tends to cause ClangImporter crashes (among
9+
// other things.)
10+
11+
// RUN: %empty-directory(%t)
12+
// RUN: %target-swift-frontend(mock-sdk: -sdk %/S/Inputs/BadStdlib.sdk -module-cache-path %/t/module-cache -resource-dir %/S/Inputs/BadStdlib.sdk) -compile-module-from-interface -o %/t/BadStdlib.swiftmodule %s -verify -verify-additional-file %/S/Inputs/BadStdlib.sdk/usr/lib/swift/Swift.swiftmodule/x86_64-apple-macos.swiftinterface
13+
14+
import ClangMod
15+
16+
public func useHasPointer(_: HasPointer)
17+
18+
// FIXME: SR-14489
19+
// UNSUPPORTED: windows
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
typedef int * HasPointer;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module HasPointer {
2+
header "HasPointer.h"
3+
export *
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -target x86_64-apple-macos10.9 -module-name Swift -parse-stdlib
3+
4+
// If the test fails, this error will be emitted twice, not once.
5+
// expected-error@-4 {{failed to build module 'Swift' for importation due to the errors above}}
6+
7+
public struct BadType {
8+
public var property: UndeclaredType { get set } // expected-error {{cannot find type 'UndeclaredType' in scope}}
9+
}

0 commit comments

Comments
 (0)