Skip to content

Commit 401d3cc

Browse files
authored
Merge pull request #5278 from apple/api-digester
api-digester: dumping mutating attributes for func declarations
2 parents 4532e20 + 55f194b commit 401d3cc

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

test/api-digester/Inputs/cake.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public struct S1 {
2+
public func foo1() {}
3+
mutating public func foo2() {}
4+
}

test/api-digester/Outputs/cake.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"kind": "Root",
3+
"name": "TopLevel",
4+
"printedName": "TopLevel",
5+
"children": [
6+
{
7+
"kind": "TypeDecl",
8+
"name": "S1",
9+
"printedName": "S1",
10+
"declKind": "Struct",
11+
"usr": "s:V4cake2S1",
12+
"location": "",
13+
"moduleName": "cake",
14+
"children": [
15+
{
16+
"kind": "Function",
17+
"name": "foo1",
18+
"printedName": "foo1()",
19+
"declKind": "Func",
20+
"usr": "s:FV4cake2S14foo1FT_T_",
21+
"location": "",
22+
"moduleName": "cake",
23+
"children": [
24+
{
25+
"kind": "TypeNominal",
26+
"name": "Void",
27+
"printedName": "()"
28+
}
29+
]
30+
},
31+
{
32+
"kind": "Function",
33+
"name": "foo2",
34+
"printedName": "foo2()",
35+
"declKind": "Func",
36+
"usr": "s:FV4cake2S14foo2FT_T_",
37+
"location": "",
38+
"moduleName": "cake",
39+
"mutating": true,
40+
"children": [
41+
{
42+
"kind": "TypeNominal",
43+
"name": "Void",
44+
"printedName": "()"
45+
}
46+
]
47+
},
48+
{
49+
"kind": "Constructor",
50+
"name": "init",
51+
"printedName": "init()",
52+
"declKind": "Constructor",
53+
"usr": "s:FV4cake2S1cFT_S0_",
54+
"location": "",
55+
"moduleName": "cake",
56+
"children": [
57+
{
58+
"kind": "TypeNominal",
59+
"name": "S1",
60+
"printedName": "S1"
61+
}
62+
]
63+
}
64+
]
65+
}
66+
]
67+
}

test/api-digester/dump-module.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: rm -rf %t.mod && mkdir -p %t.mod
2+
// RUN: rm -rf %t.sdk && mkdir -p %t.sdk
3+
// RUN: rm -rf %t.module-cache && mkdir -p %t.module-cache
4+
// RUN: %swift -emit-module -o %t.mod/cake.swiftmodule %S/Inputs/cake.swift -parse-as-library
5+
// RUN: %api-digester -dump-sdk -module cake -o %t.dump.json -module-cache-path %t.module-cache -sdk %t.sdk -swift-version 3.0 -I %t.mod
6+
// RUN: diff -u %t.dump.json %S/Outputs/cake.json
7+
// RUN: %api-digester -diagnose-sdk --input-paths %t.dump.json -input-paths %S/Outputs/cake.json

tools/swift-api-digester/DigesterEnums.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ KEY(children)
6666
KEY(printedName)
6767
KEY(moduleName)
6868
KEY(throwing)
69+
KEY(mutating)
6970
KEY(typeAttributes)
7071
KEY(declAttributes)
7172
KEY(declKind)

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ ResourceDir("resource-dir",
9898
static llvm::cl::list<std::string>
9999
FrameworkPaths("F", llvm::cl::desc("add a directory to the framework search path"));
100100

101+
static llvm::cl::list<std::string>
102+
ModuleInputPaths("I", llvm::cl::desc("add a module for input"));
103+
101104
static llvm::cl::opt<bool>
102105
AbortOnModuleLoadFailure("abort-on-module-fail",
103106
llvm::cl::desc("Abort if a module failed to load"));
@@ -258,6 +261,7 @@ struct SDKNodeInitInfo {
258261
StringRef Location;
259262
StringRef ModuleName;
260263
bool IsThrowing = false;
264+
bool IsMutating = false;
261265
Optional<uint8_t> SelfIndex;
262266
std::vector<SDKDeclAttrKind> DeclAttrs;
263267
std::vector<TypeAttrKind> TypeAttrs;
@@ -680,15 +684,18 @@ class SDKNodeVar : public SDKNodeDecl {
680684

681685
class SDKNodeAbstractFunc : public SDKNodeDecl {
682686
const bool IsThrowing;
687+
const bool IsMutating;
683688
const Optional<uint8_t> SelfIndex;
684689

685690
protected:
686691
SDKNodeAbstractFunc(SDKNodeInitInfo Info, SDKNodeKind Kind) :
687692
SDKNodeDecl(Info, Kind),
688693
IsThrowing(Info.IsThrowing),
694+
IsMutating(Info.IsMutating),
689695
SelfIndex(Info.SelfIndex){}
690696
public:
691697
bool isThrowing() const { return IsThrowing; }
698+
bool isMutating() const { return IsMutating; }
692699
uint8_t getSelfIndex() const { return SelfIndex.getValue(); }
693700
Optional<uint8_t> getSelfIndexOptional() const { return SelfIndex; }
694701
bool hasSelfIndex() const { return SelfIndex.hasValue(); }
@@ -797,6 +804,8 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
797804
Info.ModuleName = GetScalarString(Pair.getValue());
798805
} else if (Key == Key_throwing) {
799806
Info.IsThrowing = true;
807+
} else if (Key == Key_mutating) {
808+
Info.IsMutating = true;
800809
} else if (Key == Key_typeAttributes) {
801810
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
802811
for (auto It = Seq->begin(); It != Seq->end(); ++ It) {
@@ -943,6 +952,13 @@ static bool isFuncThrowing(ValueDecl *VD) {
943952
return false;
944953
}
945954

955+
static bool isFuncMutating(ValueDecl *VD) {
956+
if (auto AF = dyn_cast<FuncDecl>(VD)) {
957+
return AF->isMutating();
958+
}
959+
return false;
960+
}
961+
946962
static Optional<uint8_t> getSelfIndex(ValueDecl *VD) {
947963
if (auto AF = dyn_cast<AbstractFunctionDecl>(VD)) {
948964
if (AF->isImportAsInstanceMember())
@@ -962,7 +978,8 @@ SDKNodeInitInfo::SDKNodeInitInfo(ValueDecl *VD) :
962978
PrintedName(getPrintedName(VD)), DKind(VD->getKind()),
963979
USR(calculateUsr(VD)), Location(calculateLocation(VD)),
964980
ModuleName(VD->getModuleContext()->getName().str()),
965-
IsThrowing(isFuncThrowing(VD)), SelfIndex(getSelfIndex(VD)) {
981+
IsThrowing(isFuncThrowing(VD)), IsMutating(isFuncMutating(VD)),
982+
SelfIndex(getSelfIndex(VD)) {
966983
if (VD->getAttrs().getDeprecated(VD->getASTContext()))
967984
DeclAttrs.push_back(SDKDeclAttrKind::DAK_deprecated);
968985
}
@@ -1293,6 +1310,8 @@ namespace swift {
12931310
if (auto F = dyn_cast<SDKNodeAbstractFunc>(value.get())) {
12941311
if (bool isThrowing = F->isThrowing())
12951312
out.mapRequired(Key_throwing, isThrowing);
1313+
if (bool isMutating = F->isMutating())
1314+
out.mapRequired(Key_mutating, isMutating);
12961315
if (F->hasSelfIndex()) {
12971316
auto Index = F->getSelfIndex();
12981317
out.mapRequired(Key_selfIndex, Index);
@@ -3303,6 +3322,7 @@ static int prepareForDump(const char *Main,
33033322
InitInvok.setRuntimeResourcePath(options::ResourceDir);
33043323
}
33053324
InitInvok.setFrameworkSearchPaths(options::FrameworkPaths);
3325+
InitInvok.setImportSearchPaths(options::ModuleInputPaths);
33063326

33073327
if (!options::ModuleList.empty()) {
33083328
if (readFileLineByLine(options::ModuleList, Modules))

0 commit comments

Comments
 (0)