Skip to content

Commit cb26838

Browse files
authored
Merge pull request #37237 from bnbarham/cherry-rdar77350048
[Serialization] Add warning when .swiftsourceinfo is malformed
2 parents a6346f8 + e9728bd commit cb26838

File tree

6 files changed

+49
-0
lines changed

6 files changed

+49
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,8 @@ ERROR(serialization_error_type,Fatal,
806806
ERROR(serialization_allowing_error_type,none,
807807
"allowing deserialization of error type '%0' in module '%1'",
808808
(StringRef, StringRef))
809+
WARNING(serialization_malformed_sourceinfo,none,
810+
"unable to use malformed module source info '%0'", (StringRef))
809811

810812
ERROR(reserved_member_name,none,
811813
"type member must not be named %0, since it would conflict with the"

lib/Serialization/ModuleFile.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,13 @@ class ModuleFile
476476
/// \c true if this module has incremental dependency information.
477477
bool hasIncrementalInfo() const { return Core->hasIncrementalInfo(); }
478478

479+
/// \c true if this module has a corresponding .swiftsourceinfo file.
480+
bool hasSourceInfoFile() const { return Core->hasSourceInfoFile(); }
481+
482+
/// \c true if this module has information from a corresponding
483+
/// .swiftsourceinfo file (ie. the file exists and has been read).
484+
bool hasSourceInfo() const { return Core->hasSourceInfo(); }
485+
479486
/// Associates this module file with the AST node representing it.
480487
///
481488
/// Checks that the file is compatible with the AST module it's being loaded

lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,3 +1490,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
14901490
return;
14911491
}
14921492
}
1493+
1494+
bool ModuleFileSharedCore::hasSourceInfo() const {
1495+
return !!DeclUSRsTable;
1496+
}

lib/Serialization/ModuleFileSharedCore.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,13 @@ class ModuleFileSharedCore {
518518
/// Returns \c true if this module file contains a section with incremental
519519
/// information.
520520
bool hasIncrementalInfo() const { return HasIncrementalInfo; }
521+
522+
/// Returns \c true if a corresponding .swiftsourceinfo has been found.
523+
bool hasSourceInfoFile() const { return !!ModuleSourceInfoInputBuffer; }
524+
525+
/// Returns \c true if a corresponding .swiftsourceinfo has been found *and
526+
/// read*.
527+
bool hasSourceInfo() const;
521528
};
522529

523530
template <typename T, typename RawData>

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,16 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
686686
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
687687
bool isFramework) {
688688
assert(moduleInputBuffer);
689+
690+
// The buffers are moved into the shared core, so grab their IDs now in case
691+
// they're needed for diagnostics later.
689692
StringRef moduleBufferID = moduleInputBuffer->getBufferIdentifier();
690693
StringRef moduleDocBufferID;
691694
if (moduleDocInputBuffer)
692695
moduleDocBufferID = moduleDocInputBuffer->getBufferIdentifier();
696+
StringRef moduleSourceInfoID;
697+
if (moduleSourceInfoInputBuffer)
698+
moduleSourceInfoID = moduleSourceInfoInputBuffer->getBufferIdentifier();
693699

694700
if (moduleInputBuffer->getBufferSize() % 4 != 0) {
695701
if (diagLoc)
@@ -742,6 +748,12 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
742748
(Ctx.LangOpts.AllowModuleWithCompilerErrors &&
743749
(loadInfo.status == serialization::Status::TargetTooNew ||
744750
loadInfo.status == serialization::Status::TargetIncompatible))) {
751+
if (loadedModuleFile->hasSourceInfoFile() &&
752+
!loadedModuleFile->hasSourceInfo())
753+
Ctx.Diags.diagnose(diagLocOrInvalid,
754+
diag::serialization_malformed_sourceinfo,
755+
moduleSourceInfoID);
756+
745757
Ctx.bumpGeneration();
746758
LoadedModuleFiles.emplace_back(std::move(loadedModuleFile),
747759
Ctx.getCurrentGeneration());
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -o %t %S/../Inputs/empty.swift
3+
// RUN: %target-swift-frontend -typecheck -I %t %s
4+
5+
// RUN: touch %t/empty.swiftsourceinfo
6+
// RUN: %target-swift-frontend -typecheck -I %t %s -verify
7+
8+
// RUN: echo -n 'a' > %t/empty.swiftsourceinfo
9+
// RUN: %target-swift-frontend -typecheck -I %t %s -verify
10+
11+
// RUN: echo -n 'abcd' > %t/empty.swiftsourceinfo
12+
// RUN: %target-swift-frontend -typecheck -I %t %s -verify
13+
14+
// RUN: echo -n 'abcde' > %t/empty.swiftsourceinfo
15+
// RUN: %target-swift-frontend -typecheck -I %t %s -verify
16+
17+
import empty // expected-warning{{unable to use malformed module source info}}

0 commit comments

Comments
 (0)