Merge pull request #37196 from bnbarham/warn-on-invalid-sourceinfo

[Serialization] Add warning when .swiftsourceinfo is malformed
This commit is contained in:
Ben Barham
2021-05-04 16:16:49 +10:00
committed by GitHub
6 changed files with 49 additions and 0 deletions

View File

@@ -799,6 +799,8 @@ ERROR(serialization_error_type,Fatal,
ERROR(serialization_allowing_error_type,none,
"allowing deserialization of error type '%0' in module '%1'",
(StringRef, StringRef))
WARNING(serialization_malformed_sourceinfo,none,
"unable to use malformed module source info '%0'", (StringRef))
ERROR(reserved_member_name,none,
"type member must not be named %0, since it would conflict with the"

View File

@@ -480,6 +480,13 @@ public:
/// \c true if this module has incremental dependency information.
bool hasIncrementalInfo() const { return Core->hasIncrementalInfo(); }
/// \c true if this module has a corresponding .swiftsourceinfo file.
bool hasSourceInfoFile() const { return Core->hasSourceInfoFile(); }
/// \c true if this module has information from a corresponding
/// .swiftsourceinfo file (ie. the file exists and has been read).
bool hasSourceInfo() const { return Core->hasSourceInfo(); }
/// Associates this module file with the AST node representing it.
///
/// Checks that the file is compatible with the AST module it's being loaded

View File

@@ -1496,3 +1496,7 @@ ModuleFileSharedCore::ModuleFileSharedCore(
return;
}
}
bool ModuleFileSharedCore::hasSourceInfo() const {
return !!DeclUSRsTable;
}

View File

@@ -521,6 +521,13 @@ public:
/// Returns \c true if this module file contains a section with incremental
/// information.
bool hasIncrementalInfo() const { return HasIncrementalInfo; }
/// Returns \c true if a corresponding .swiftsourceinfo has been found.
bool hasSourceInfoFile() const { return !!ModuleSourceInfoInputBuffer; }
/// Returns \c true if a corresponding .swiftsourceinfo has been found *and
/// read*.
bool hasSourceInfo() const;
};
template <typename T, typename RawData>

View File

@@ -687,10 +687,16 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
bool isFramework) {
assert(moduleInputBuffer);
// The buffers are moved into the shared core, so grab their IDs now in case
// they're needed for diagnostics later.
StringRef moduleBufferID = moduleInputBuffer->getBufferIdentifier();
StringRef moduleDocBufferID;
if (moduleDocInputBuffer)
moduleDocBufferID = moduleDocInputBuffer->getBufferIdentifier();
StringRef moduleSourceInfoID;
if (moduleSourceInfoInputBuffer)
moduleSourceInfoID = moduleSourceInfoInputBuffer->getBufferIdentifier();
if (moduleInputBuffer->getBufferSize() % 4 != 0) {
if (diagLoc)
@@ -743,6 +749,12 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
(Ctx.LangOpts.AllowModuleWithCompilerErrors &&
(loadInfo.status == serialization::Status::TargetTooNew ||
loadInfo.status == serialization::Status::TargetIncompatible))) {
if (loadedModuleFile->hasSourceInfoFile() &&
!loadedModuleFile->hasSourceInfo())
Ctx.Diags.diagnose(diagLocOrInvalid,
diag::serialization_malformed_sourceinfo,
moduleSourceInfoID);
Ctx.bumpGeneration();
LoadedModuleFiles.emplace_back(std::move(loadedModuleFile),
Ctx.getCurrentGeneration());

View File

@@ -0,0 +1,17 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t %S/../Inputs/empty.swift
// RUN: %target-swift-frontend -typecheck -I %t %s
// RUN: touch %t/empty.swiftsourceinfo
// RUN: %target-swift-frontend -typecheck -I %t %s -verify
// RUN: echo -n 'a' > %t/empty.swiftsourceinfo
// RUN: %target-swift-frontend -typecheck -I %t %s -verify
// RUN: echo -n 'abcd' > %t/empty.swiftsourceinfo
// RUN: %target-swift-frontend -typecheck -I %t %s -verify
// RUN: echo -n 'abcde' > %t/empty.swiftsourceinfo
// RUN: %target-swift-frontend -typecheck -I %t %s -verify
import empty // expected-warning{{unable to use malformed module source info}}