Skip to content

Commit 29dd7f8

Browse files
authored
Merge pull request #22791 from nkcsgexi/serialization-group-diag
doc-serialization: diagnose missing group-info file or corrupted one.
2 parents 1f1e523 + ecd1e84 commit 29dd7f8

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ ERROR(not_implemented,none,
5050
ERROR(error_opening_output,none,
5151
"error opening '%0' for output: %1", (StringRef, StringRef))
5252

53+
ERROR(cannot_find_group_info_file,none,
54+
"cannot find group info file at path: '%0'", (StringRef))
55+
56+
ERROR(cannot_parse_group_info_file,none,
57+
"cannot parse group info file at path: '%0'", (StringRef))
58+
5359
ERROR(error_no_group_info,none,
5460
"no group info found for file: '%0'", (StringRef))
5561

lib/Serialization/SerializeDoc.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ using pFileNameToGroupNameMap = std::unique_ptr<FileNameToGroupNameMap>;
3838

3939
namespace {
4040
class YamlGroupInputParser {
41+
ASTContext &Ctx;
4142
StringRef RecordPath;
4243
static constexpr const char * const Separator = "/";
4344

@@ -86,12 +87,20 @@ class YamlGroupInputParser {
8687
}
8788

8889
public:
89-
YamlGroupInputParser(StringRef RecordPath): RecordPath(RecordPath) {}
90+
YamlGroupInputParser(ASTContext &Ctx, StringRef RecordPath):
91+
Ctx(Ctx), RecordPath(RecordPath) {}
9092

9193
FileNameToGroupNameMap* getParsedMap() {
9294
return AllMaps[RecordPath].get();
9395
}
9496

97+
bool diagnoseGroupInfoFile(bool FileMissing = false) {
98+
Ctx.Diags.diagnose(SourceLoc(),
99+
FileMissing ? diag::cannot_find_group_info_file:
100+
diag::cannot_parse_group_info_file, RecordPath);
101+
return true;
102+
}
103+
95104
// Parse the Yaml file that contains the group information.
96105
// True on failure; false on success.
97106
bool parse() {
@@ -102,32 +111,31 @@ class YamlGroupInputParser {
102111

103112
auto Buffer = llvm::MemoryBuffer::getFile(RecordPath);
104113
if (!Buffer) {
105-
// The group info file does not exist.
106-
return true;
114+
return diagnoseGroupInfoFile(/*Missing File*/true);
107115
}
108116
llvm::SourceMgr SM;
109117
llvm::yaml::Stream YAMLStream(Buffer.get()->getMemBufferRef(), SM);
110118
llvm::yaml::document_iterator I = YAMLStream.begin();
111119
if (I == YAMLStream.end()) {
112120
// Cannot parse correctly.
113-
return true;
121+
return diagnoseGroupInfoFile();
114122
}
115123
llvm::yaml::Node *Root = I->getRoot();
116124
if (!Root) {
117125
// Cannot parse correctly.
118-
return true;
126+
return diagnoseGroupInfoFile();
119127
}
120128

121129
// The format is a map of ("group0" : ["file1", "file2"]), meaning all
122130
// symbols from file1 and file2 belong to "group0".
123131
auto *Map = dyn_cast<llvm::yaml::MappingNode>(Root);
124132
if (!Map) {
125-
return true;
133+
return diagnoseGroupInfoFile();
126134
}
127135
pFileNameToGroupNameMap pMap(new FileNameToGroupNameMap());
128136
std::string Empty;
129137
if (parseRoot(*pMap, Root, Empty))
130-
return true;
138+
return diagnoseGroupInfoFile();
131139

132140
// Save the parsed map to the owner.
133141
AllMaps[RecordPath] = std::move(pMap);
@@ -168,7 +176,7 @@ class DeclGroupNameContext {
168176
StringRef FullPath =
169177
Ctx.SourceMgr.getIdentifierForBuffer(PathOp.getValue());
170178
if (!pMap) {
171-
YamlGroupInputParser Parser(RecordPath);
179+
YamlGroupInputParser Parser(Ctx, RecordPath);
172180
if (!Parser.parse()) {
173181

174182
// Get the file-name to group map if parsing correctly.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
corrupteddata
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %empty-directory(%t)
2+
// 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
3+
// RUN: %FileCheck %s -check-prefix=MISSING < %t/result.txt
4+
// 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
5+
// RUN: %FileCheck %s -check-prefix=CORRUPTED < %t/result.txt
6+
7+
public protocol P {}
8+
9+
// MISSING: error: cannot find group info file at path
10+
// CORRUPTED: error: cannot parse group info file at path

0 commit comments

Comments
 (0)