Skip to content

Commit 3266fd5

Browse files
committed
ABI/API checker: add a version number for the json descriptors of APIs
1 parent 6a65a01 commit 3266fd5

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

include/swift/IDE/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ KEY_STRING_ARR(ToolArgs, tool_arguments)
156156

157157
KEY_UINT(SelfIndex, selfIndex)
158158
KEY_UINT(FixedBinaryOrder, fixedbinaryorder)
159+
KEY_UINT(JsonFormatVer, json_format_version)
159160

160161
KEY(children)
161162
KEY(conformances)

test/api-digester/Outputs/cake-abi.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,5 +1753,6 @@
17531753
}
17541754
]
17551755
}
1756-
]
1756+
],
1757+
"json_format_version": 1
17571758
}

test/api-digester/Outputs/cake.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1621,5 +1621,6 @@
16211621
}
16221622
]
16231623
}
1624-
]
1624+
],
1625+
"json_format_version": 1
16251626
}

test/api-digester/Outputs/clang-module-dump.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,6 @@
168168
"ObjC"
169169
]
170170
}
171-
]
171+
],
172+
"json_format_version": 1
172173
}

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ SDKNode::SDKNode(SDKNodeInitInfo Info, SDKNodeKind Kind): Ctx(Info.Ctx),
8080
Name(Info.Name), PrintedName(Info.PrintedName), TheKind(unsigned(Kind)) {}
8181

8282
SDKNodeRoot::SDKNodeRoot(SDKNodeInitInfo Info): SDKNode(Info, SDKNodeKind::Root),
83-
ToolArgs(Info.ToolArgs) {}
83+
ToolArgs(Info.ToolArgs),
84+
JsonFormatVer(Info.JsonFormatVer.hasValue() ? *Info.JsonFormatVer : DIGESTER_JSON_DEFAULT_VERSION) {}
8485

8586
SDKNodeDecl::SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind)
8687
: SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.Usr),
@@ -369,6 +370,7 @@ SDKNode *SDKNodeRoot::getInstance(SDKContext &Ctx) {
369370
Info.Name = Ctx.buffer("TopLevel");
370371
Info.PrintedName = Ctx.buffer("TopLevel");
371372
Info.ToolArgs = Ctx.getOpts().ToolArgs;
373+
Info.JsonFormatVer = DIGESTER_JSON_VERSION;
372374
return Info.createSDKNode(SDKNodeKind::Root);
373375
}
374376

@@ -1833,6 +1835,7 @@ void SDKNode::jsonize(json::Output &out) {
18331835

18341836
void SDKNodeRoot::jsonize(json::Output &out) {
18351837
SDKNode::jsonize(out);
1838+
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_json_format_version).data(), JsonFormatVer);
18361839
if (!Ctx.getOpts().AvoidToolArgs)
18371840
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_tool_arguments).data(), ToolArgs);
18381841
}

tools/swift-api-digester/ModuleAnalyzerNodes.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class Output;
5858
namespace ide {
5959
namespace api {
6060

61+
/// Serialized json format version number.
62+
///
63+
/// When the json format changes in a way that requires version-specific handling, this number should be incremented.
64+
/// This ensures we could have backward compatibility so that version changes in the format won't stop the checker from working.
65+
const uint8_t DIGESTER_JSON_VERSION = 1; // Adding digester_version to the format
66+
const uint8_t DIGESTER_JSON_DEFAULT_VERSION = 0; // Use this version number for files before we have a version number in json.
67+
6168
class SDKNode;
6269
typedef SDKNode* NodePtr;
6370
typedef std::map<NodePtr, NodePtr> ParentMap;
@@ -387,13 +394,14 @@ class SDKNodeRoot: public SDKNode {
387394
llvm::StringMap<llvm::SmallSetVector<SDKNodeDecl*, 2>> DescendantDeclTable;
388395
/// The tool invocation arguments to generate this root node. We shouldn't need APIs for it.
389396
std::vector<StringRef> ToolArgs;
390-
397+
uint8_t JsonFormatVer;
391398
public:
392399
SDKNodeRoot(SDKNodeInitInfo Info);
393400
static SDKNode *getInstance(SDKContext &Ctx);
394401
static bool classof(const SDKNode *N);
395402
void registerDescendant(SDKNode *D);
396403
virtual void jsonize(json::Output &Out) override;
404+
Optional<uint8_t> getJsonFormatVersion() const { return JsonFormatVer; }
397405
ArrayRef<SDKNodeDecl*> getDescendantsByUsr(StringRef Usr) {
398406
return DescendantDeclTable[Usr].getArrayRef();
399407
}

0 commit comments

Comments
 (0)