Skip to content

doc-serialization: diagnose missing group-info file or corrupted one. #22791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/AST/DiagnosticsCommon.def
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ ERROR(not_implemented,none,
ERROR(error_opening_output,none,
"error opening '%0' for output: %1", (StringRef, StringRef))

ERROR(cannot_find_group_info_file,none,
"cannot find group info file at path: '%0'", (StringRef))

ERROR(cannot_parse_group_info_file,none,
"cannot parse group info file at path: '%0'", (StringRef))

ERROR(error_no_group_info,none,
"no group info found for file: '%0'", (StringRef))

Expand Down
24 changes: 16 additions & 8 deletions lib/Serialization/SerializeDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ using pFileNameToGroupNameMap = std::unique_ptr<FileNameToGroupNameMap>;

namespace {
class YamlGroupInputParser {
ASTContext &Ctx;
StringRef RecordPath;
static constexpr const char * const Separator = "/";

Expand Down Expand Up @@ -86,12 +87,20 @@ class YamlGroupInputParser {
}

public:
YamlGroupInputParser(StringRef RecordPath): RecordPath(RecordPath) {}
YamlGroupInputParser(ASTContext &Ctx, StringRef RecordPath):
Ctx(Ctx), RecordPath(RecordPath) {}

FileNameToGroupNameMap* getParsedMap() {
return AllMaps[RecordPath].get();
}

bool diagnoseGroupInfoFile(bool FileMissing = false) {
Ctx.Diags.diagnose(SourceLoc(),
FileMissing ? diag::cannot_find_group_info_file:
diag::cannot_parse_group_info_file, RecordPath);
return true;
}

// Parse the Yaml file that contains the group information.
// True on failure; false on success.
bool parse() {
Expand All @@ -102,32 +111,31 @@ class YamlGroupInputParser {

auto Buffer = llvm::MemoryBuffer::getFile(RecordPath);
if (!Buffer) {
// The group info file does not exist.
return true;
return diagnoseGroupInfoFile(/*Missing File*/true);
}
llvm::SourceMgr SM;
llvm::yaml::Stream YAMLStream(Buffer.get()->getMemBufferRef(), SM);
llvm::yaml::document_iterator I = YAMLStream.begin();
if (I == YAMLStream.end()) {
// Cannot parse correctly.
return true;
return diagnoseGroupInfoFile();
}
llvm::yaml::Node *Root = I->getRoot();
if (!Root) {
// Cannot parse correctly.
return true;
return diagnoseGroupInfoFile();
}

// The format is a map of ("group0" : ["file1", "file2"]), meaning all
// symbols from file1 and file2 belong to "group0".
auto *Map = dyn_cast<llvm::yaml::MappingNode>(Root);
if (!Map) {
return true;
return diagnoseGroupInfoFile();
}
pFileNameToGroupNameMap pMap(new FileNameToGroupNameMap());
std::string Empty;
if (parseRoot(*pMap, Root, Empty))
return true;
return diagnoseGroupInfoFile();

// Save the parsed map to the owner.
AllMaps[RecordPath] = std::move(pMap);
Expand Down Expand Up @@ -168,7 +176,7 @@ class DeclGroupNameContext {
StringRef FullPath =
Ctx.SourceMgr.getIdentifierForBuffer(PathOp.getValue());
if (!pMap) {
YamlGroupInputParser Parser(RecordPath);
YamlGroupInputParser Parser(Ctx, RecordPath);
if (!Parser.parse()) {

// Get the file-name to group map if parsing correctly.
Expand Down
3 changes: 3 additions & 0 deletions test/Serialization/Inputs/corrupted_group_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
corrupteddata
}
10 changes: 10 additions & 0 deletions test/Serialization/group_info_diags.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %empty-directory(%t)
// RUN: not %target-swift-frontend -emit-module %s -module-name HasArray -o %t -module-cache-path %t/mcp -group-info-path %t/nonexist.json -emit-module-doc &> %t/result.txt
// RUN: %FileCheck %s -check-prefix=MISSING < %t/result.txt
// RUN: not %target-swift-frontend -emit-module %s -module-name HasArray -o %t -module-cache-path %t/mcp -group-info-path %S/Inputs/corrupted_group_info.json -emit-module-doc &> %t/result.txt
// RUN: %FileCheck %s -check-prefix=CORRUPTED < %t/result.txt

public protocol P {}

// MISSING: error: cannot find group info file at path
// CORRUPTED: error: cannot parse group info file at path