Add error for invalid SDKNodeKind

This commit is contained in:
Brent Royal-Gordon
2018-08-17 00:06:11 -07:00
parent ec87277e56
commit 88ac6f11a2
6 changed files with 22 additions and 9 deletions

View File

@@ -94,9 +94,11 @@ ERROR(function_type_no_parens,none,
"single argument function types require parentheses", ())
// FIXME: Used by swift-api-digester. Don't want to set up a separate diagnostics
// file just for one error.
// file just for two errors.
ERROR(sdk_node_unrecognized_key,none,
"unrecognized key '%0' in SDK node", (StringRef))
ERROR(sdk_node_unrecognized_node_kind,none,
"unrecognized SDK node kind '%0'", (StringRef))
//------------------------------------------------------------------------------
// MARK: Circular reference diagnostics

View File

@@ -33,7 +33,7 @@ enum class SDKNodeKind: uint8_t {
#include "DigesterEnums.def"
};
SDKNodeKind parseSDKNodeKind(StringRef Content);
Optional<SDKNodeKind> parseSDKNodeKind(StringRef Content);
enum class NodeAnnotation: uint8_t{
#define NODE_ANNOTATION(NAME) NAME,

View File

@@ -39,10 +39,11 @@ operator<<(raw_ostream &Out, const NodeAnnotation Value) {
llvm_unreachable("Undefined SDK node kind.");
}
SDKNodeKind swift::ide::api::parseSDKNodeKind(StringRef Content) {
return llvm::StringSwitch<SDKNodeKind>(Content)
Optional<SDKNodeKind> swift::ide::api::parseSDKNodeKind(StringRef Content) {
return llvm::StringSwitch<Optional<SDKNodeKind>>(Content)
#define NODE_KIND(NAME, VALUE) .Case(#VALUE, SDKNodeKind::NAME)
#include "swift/IDE/DigesterEnums.def"
.Default(None)
;
}
@@ -326,7 +327,7 @@ serializeDiffItem(llvm::BumpPtrAllocator &Alloc,
switch (parseDiffItemKind(DiffItemKind)) {
case APIDiffItemKind::ADK_CommonDiffItem: {
return new (Alloc.Allocate<CommonDiffItem>())
CommonDiffItem(parseSDKNodeKind(NodeKind),
CommonDiffItem(*parseSDKNodeKind(NodeKind),
parseSDKNodeAnnotation(NodeAnnotation), ChildIndex,
LeftUsr, RightUsr, LeftComment, RightComment, ModuleName);
}

View File

@@ -2,6 +2,8 @@
"kind": "Root",
"name": "TopLevel",
"printedName": "TopLevel",
"children": [],
"badKey": ["foo", "bar", "baz"]
"badKey": ["foo", "bar", "baz"],
"children": [
{ "kind": "Zyzyx" }
]
}

View File

@@ -1,7 +1,8 @@
// REQUIRES: OS=macosx
// RUN: not %api-digester -deserialize-sdk -input-paths %S/diagnostics.json -o - 2>&1 | %FileCheck %s
// CHECK: diagnostics.json:6:3: error: unrecognized key 'badKey' in SDK node
// CHECK: diagnostics.json:5:3: error: unrecognized key 'badKey' in SDK node
// CHECK: diagnostics.json:7:15: error: unrecognized SDK node kind 'Zyzyx'
// Make sure we don't try to output a result:
// CHECK-NOT: "kind": "Root",

View File

@@ -1057,7 +1057,14 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
if (auto keyKind = parseKeyKind(keyString)) {
switch(*keyKind) {
case KeyKind::KK_kind:
Kind = parseSDKNodeKind(GetScalarString(Pair.getValue()));
if (auto parsedKind = parseSDKNodeKind(GetScalarString(Pair.getValue()))) {
Kind = *parsedKind;
} else {
auto range = convertRange(Pair.getValue()->getSourceRange());
Ctx.getDiags().diagnose(range.Start, diag::sdk_node_unrecognized_node_kind,
GetScalarString(Pair.getValue()))
.highlight(range);
}
break;
case KeyKind::KK_name:
Info.Name = GetScalarString(Pair.getValue());