Skip to content

Commit f9859a5

Browse files
authored
Merge pull request swiftlang#37196 from bnbarham/warn-on-invalid-sourceinfo
[Serialization] Add warning when .swiftsourceinfo is malformed
2 parents 1263da1 + bfb9205 commit f9859a5

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
@@ -799,6 +799,8 @@ ERROR(serialization_error_type,Fatal,
799799
ERROR(serialization_allowing_error_type,none,
800800
"allowing deserialization of error type '%0' in module '%1'",
801801
(StringRef, StringRef))
802+
WARNING(serialization_malformed_sourceinfo,none,
803+
"unable to use malformed module source info '%0'", (StringRef))
802804

803805
ERROR(reserved_member_name,none,
804806
"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
@@ -480,6 +480,13 @@ class ModuleFile
480480
/// \c true if this module has incremental dependency information.
481481
bool hasIncrementalInfo() const { return Core->hasIncrementalInfo(); }
482482

483+
/// \c true if this module has a corresponding .swiftsourceinfo file.
484+
bool hasSourceInfoFile() const { return Core->hasSourceInfoFile(); }
485+
486+
/// \c true if this module has information from a corresponding
487+
/// .swiftsourceinfo file (ie. the file exists and has been read).
488+
bool hasSourceInfo() const { return Core->hasSourceInfo(); }
489+
483490
/// Associates this module file with the AST node representing it.
484491
///
485492
/// 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
@@ -1496,3 +1496,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
14961496
return;
14971497
}
14981498
}
1499+
1500+
bool ModuleFileSharedCore::hasSourceInfo() const {
1501+
return !!DeclUSRsTable;
1502+
}

lib/Serialization/ModuleFileSharedCore.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,13 @@ class ModuleFileSharedCore {
521521
/// Returns \c true if this module file contains a section with incremental
522522
/// information.
523523
bool hasIncrementalInfo() const { return HasIncrementalInfo; }
524+
525+
/// Returns \c true if a corresponding .swiftsourceinfo has been found.
526+
bool hasSourceInfoFile() const { return !!ModuleSourceInfoInputBuffer; }
527+
528+
/// Returns \c true if a corresponding .swiftsourceinfo has been found *and
529+
/// read*.
530+
bool hasSourceInfo() const;
524531
};
525532

526533
template <typename T, typename RawData>

lib/Serialization/SerializedModuleLoader.cpp

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

695701
if (moduleInputBuffer->getBufferSize() % 4 != 0) {
696702
if (diagLoc)
@@ -743,6 +749,12 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
743749
(Ctx.LangOpts.AllowModuleWithCompilerErrors &&
744750
(loadInfo.status == serialization::Status::TargetTooNew ||
745751
loadInfo.status == serialization::Status::TargetIncompatible))) {
752+
if (loadedModuleFile->hasSourceInfoFile() &&
753+
!loadedModuleFile->hasSourceInfo())
754+
Ctx.Diags.diagnose(diagLocOrInvalid,
755+
diag::serialization_malformed_sourceinfo,
756+
moduleSourceInfoID);
757+
746758
Ctx.bumpGeneration();
747759
LoadedModuleFiles.emplace_back(std::move(loadedModuleFile),
748760
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)