Skip to content

Commit 004ada9

Browse files
committed
Add errors for other invalid strings in digester
Specifically, TypeAttrKinds, DeclAttrKinds, and DeclKinds.
1 parent 8a192ea commit 004ada9

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

include/swift/AST/DiagnosticsCommon.def

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,17 @@ 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 two errors.
97+
// file just for a few errors.
9898
ERROR(sdk_node_unrecognized_key,none,
9999
"unrecognized key '%0' in SDK node", (StringRef))
100100
ERROR(sdk_node_unrecognized_node_kind,none,
101101
"unrecognized SDK node kind '%0'", (StringRef))
102+
ERROR(sdk_node_unrecognized_type_attr_kind,none,
103+
"unrecognized type attribute '%0' in SDK node", (StringRef))
104+
ERROR(sdk_node_unrecognized_decl_attr_kind,none,
105+
"unrecognized declaration attribute '%0' in SDK node", (StringRef))
106+
ERROR(sdk_node_unrecognized_decl_kind,none,
107+
"unrecognized declaration kind '%0' in SDK node", (StringRef))
102108

103109
//------------------------------------------------------------------------------
104110
// MARK: Circular reference diagnostics

test/api-digester/diagnostics.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"printedName": "TopLevel",
55
"badKey": ["foo", "bar", "baz"],
66
"children": [
7-
{ "kind": "Zyzyx" }
7+
{
8+
"kind": "Zyzyx",
9+
"typeAttributes": ["autoclosure", "fnord", "inout", "Available"],
10+
"declAttributes": ["Available", "Fnord", "ObjC", "inout"],
11+
"declKind": "Subroutine"
12+
}
813
]
914
}

test/api-digester/diagnostics.swift

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

44
// CHECK: diagnostics.json:5:3: error: unrecognized key 'badKey' in SDK node
5-
// CHECK: diagnostics.json:7:15: error: unrecognized SDK node kind 'Zyzyx'
5+
// CHECK: diagnostics.json:8:15: error: unrecognized SDK node kind 'Zyzyx'
6+
// CHECK: diagnostics.json:9:41: error: unrecognized type attribute 'fnord' in SDK node
7+
// CHECK: diagnostics.json:9:59: error: unrecognized type attribute 'Available' in SDK node
8+
// CHECK: diagnostics.json:10:39: error: unrecognized declaration attribute 'Fnord' in SDK node
9+
// CHECK: diagnostics.json:10:56: error: unrecognized declaration attribute 'inout' in SDK node
10+
// CHECK: diagnostics.json:11:19: error: unrecognized declaration kind 'Subroutine' in SDK node
611

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

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,36 +1139,47 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
11391139
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
11401140
std::transform(Seq->begin(), Seq->end(),
11411141
std::back_inserter(Info.TypeAttrs),
1142-
[](llvm::yaml::Node &N) {
1142+
[&](llvm::yaml::Node &N) {
11431143
auto Result = llvm::StringSwitch<TypeAttrKind>(GetScalarString(&N))
11441144
#define TYPE_ATTR(X) .Case(#X, TypeAttrKind::TAK_##X)
11451145
#include "swift/AST/Attr.def"
11461146
.Default(TypeAttrKind::TAK_Count);
1147-
assert(Result != TypeAttrKind::TAK_Count);
1147+
if (Result == TAK_Count)
1148+
Ctx.diagnose(&N, diag::sdk_node_unrecognized_type_attr_kind,
1149+
GetScalarString(&N));
11481150
return Result;
11491151
});
11501152
break;
11511153
}
11521154
case KeyKind::KK_declAttributes: {
11531155
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
11541156
std::transform(Seq->begin(), Seq->end(), std::back_inserter(Info.DeclAttrs),
1155-
[](llvm::yaml::Node &N) {
1157+
[&](llvm::yaml::Node &N) {
11561158
auto Result = llvm::StringSwitch<DeclAttrKind>(GetScalarString(&N))
11571159
#define DECL_ATTR(_, NAME, ...) .Case(#NAME, DeclAttrKind::DAK_##NAME)
11581160
#include "swift/AST/Attr.def"
11591161
.Default(DeclAttrKind::DAK_Count);
1160-
assert(Result != DeclAttrKind::DAK_Count);
1162+
if (Result == DAK_Count)
1163+
Ctx.diagnose(&N, diag::sdk_node_unrecognized_decl_attr_kind,
1164+
GetScalarString(&N));
11611165
return Result;
11621166
});
11631167
break;
11641168
}
1165-
case KeyKind::KK_declKind:
1166-
Info.DKind = llvm::StringSwitch<DeclKind>(GetScalarString(Pair.getValue()))
1169+
case KeyKind::KK_declKind: {
1170+
auto dKind = llvm::StringSwitch<Optional<DeclKind>>(
1171+
GetScalarString(Pair.getValue()))
11671172
#define DECL(X, PARENT) .Case(#X, DeclKind::X)
11681173
#include "swift/AST/DeclNodes.def"
1169-
;
1174+
.Default(None);
1175+
if (dKind)
1176+
Info.DKind = *dKind;
1177+
else
1178+
Ctx.diagnose(Pair.getValue(), diag::sdk_node_unrecognized_decl_kind,
1179+
GetScalarString(Pair.getValue()));
11701180
break;
11711181
}
1182+
}
11721183
}
11731184
else {
11741185
Ctx.diagnose(Pair.getKey(), diag::sdk_node_unrecognized_key,

0 commit comments

Comments
 (0)