Skip to content

Commit 88ac6f1

Browse files
committed
Add error for invalid SDKNodeKind
1 parent ec87277 commit 88ac6f1

File tree

6 files changed

+22
-9
lines changed

6 files changed

+22
-9
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ ERROR(function_type_no_parens,none,
9494
"single argument function types require parentheses", ())
9595

9696
// FIXME: Used by swift-api-digester. Don't want to set up a separate diagnostics
97-
// file just for one error.
97+
// file just for two errors.
9898
ERROR(sdk_node_unrecognized_key,none,
9999
"unrecognized key '%0' in SDK node", (StringRef))
100+
ERROR(sdk_node_unrecognized_node_kind,none,
101+
"unrecognized SDK node kind '%0'", (StringRef))
100102

101103
//------------------------------------------------------------------------------
102104
// MARK: Circular reference diagnostics

include/swift/IDE/APIDigesterData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ enum class SDKNodeKind: uint8_t {
3333
#include "DigesterEnums.def"
3434
};
3535

36-
SDKNodeKind parseSDKNodeKind(StringRef Content);
36+
Optional<SDKNodeKind> parseSDKNodeKind(StringRef Content);
3737

3838
enum class NodeAnnotation: uint8_t{
3939
#define NODE_ANNOTATION(NAME) NAME,

lib/IDE/APIDigesterData.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ operator<<(raw_ostream &Out, const NodeAnnotation Value) {
3939
llvm_unreachable("Undefined SDK node kind.");
4040
}
4141

42-
SDKNodeKind swift::ide::api::parseSDKNodeKind(StringRef Content) {
43-
return llvm::StringSwitch<SDKNodeKind>(Content)
42+
Optional<SDKNodeKind> swift::ide::api::parseSDKNodeKind(StringRef Content) {
43+
return llvm::StringSwitch<Optional<SDKNodeKind>>(Content)
4444
#define NODE_KIND(NAME, VALUE) .Case(#VALUE, SDKNodeKind::NAME)
4545
#include "swift/IDE/DigesterEnums.def"
46+
.Default(None)
4647
;
4748
}
4849

@@ -326,7 +327,7 @@ serializeDiffItem(llvm::BumpPtrAllocator &Alloc,
326327
switch (parseDiffItemKind(DiffItemKind)) {
327328
case APIDiffItemKind::ADK_CommonDiffItem: {
328329
return new (Alloc.Allocate<CommonDiffItem>())
329-
CommonDiffItem(parseSDKNodeKind(NodeKind),
330+
CommonDiffItem(*parseSDKNodeKind(NodeKind),
330331
parseSDKNodeAnnotation(NodeAnnotation), ChildIndex,
331332
LeftUsr, RightUsr, LeftComment, RightComment, ModuleName);
332333
}

test/api-digester/diagnostics.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"kind": "Root",
33
"name": "TopLevel",
44
"printedName": "TopLevel",
5-
"children": [],
6-
"badKey": ["foo", "bar", "baz"]
5+
"badKey": ["foo", "bar", "baz"],
6+
"children": [
7+
{ "kind": "Zyzyx" }
8+
]
79
}

test/api-digester/diagnostics.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// REQUIRES: OS=macosx
22
// RUN: not %api-digester -deserialize-sdk -input-paths %S/diagnostics.json -o - 2>&1 | %FileCheck %s
33

4-
// CHECK: diagnostics.json:6:3: error: unrecognized key 'badKey' in SDK node
4+
// CHECK: diagnostics.json:5:3: error: unrecognized key 'badKey' in SDK node
5+
// CHECK: diagnostics.json:7:15: error: unrecognized SDK node kind 'Zyzyx'
56

67
// Make sure we don't try to output a result:
78
// CHECK-NOT: "kind": "Root",

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,14 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
10571057
if (auto keyKind = parseKeyKind(keyString)) {
10581058
switch(*keyKind) {
10591059
case KeyKind::KK_kind:
1060-
Kind = parseSDKNodeKind(GetScalarString(Pair.getValue()));
1060+
if (auto parsedKind = parseSDKNodeKind(GetScalarString(Pair.getValue()))) {
1061+
Kind = *parsedKind;
1062+
} else {
1063+
auto range = convertRange(Pair.getValue()->getSourceRange());
1064+
Ctx.getDiags().diagnose(range.Start, diag::sdk_node_unrecognized_node_kind,
1065+
GetScalarString(Pair.getValue()))
1066+
.highlight(range);
1067+
}
10611068
break;
10621069
case KeyKind::KK_name:
10631070
Info.Name = GetScalarString(Pair.getValue());

0 commit comments

Comments
 (0)