Skip to content

api-digester: dumping mutating attributes for func declarations #5278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/api-digester/Inputs/cake.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public struct S1 {
public func foo1() {}
mutating public func foo2() {}
}
67 changes: 67 additions & 0 deletions test/api-digester/Outputs/cake.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"kind": "Root",
"name": "TopLevel",
"printedName": "TopLevel",
"children": [
{
"kind": "TypeDecl",
"name": "S1",
"printedName": "S1",
"declKind": "Struct",
"usr": "s:V4cake2S1",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "Function",
"name": "foo1",
"printedName": "foo1()",
"declKind": "Func",
"usr": "s:FV4cake2S14foo1FT_T_",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeNominal",
"name": "Void",
"printedName": "()"
}
]
},
{
"kind": "Function",
"name": "foo2",
"printedName": "foo2()",
"declKind": "Func",
"usr": "s:FV4cake2S14foo2FT_T_",
"location": "",
"moduleName": "cake",
"mutating": true,
"children": [
{
"kind": "TypeNominal",
"name": "Void",
"printedName": "()"
}
]
},
{
"kind": "Constructor",
"name": "init",
"printedName": "init()",
"declKind": "Constructor",
"usr": "s:FV4cake2S1cFT_S0_",
"location": "",
"moduleName": "cake",
"children": [
{
"kind": "TypeNominal",
"name": "S1",
"printedName": "S1"
}
]
}
]
}
]
}
7 changes: 7 additions & 0 deletions test/api-digester/dump-module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: rm -rf %t.mod && mkdir -p %t.mod
// RUN: rm -rf %t.sdk && mkdir -p %t.sdk
// RUN: rm -rf %t.module-cache && mkdir -p %t.module-cache
// RUN: %swift -emit-module -o %t.mod/cake.swiftmodule %S/Inputs/cake.swift -parse-as-library
// 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
// RUN: diff -u %t.dump.json %S/Outputs/cake.json
// RUN: %api-digester -diagnose-sdk --input-paths %t.dump.json -input-paths %S/Outputs/cake.json
1 change: 1 addition & 0 deletions tools/swift-api-digester/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ KEY(children)
KEY(printedName)
KEY(moduleName)
KEY(throwing)
KEY(mutating)
KEY(typeAttributes)
KEY(declAttributes)
KEY(declKind)
Expand Down
22 changes: 21 additions & 1 deletion tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ ResourceDir("resource-dir",
static llvm::cl::list<std::string>
FrameworkPaths("F", llvm::cl::desc("add a directory to the framework search path"));

static llvm::cl::list<std::string>
ModuleInputPaths("I", llvm::cl::desc("add a module for input"));

static llvm::cl::opt<bool>
AbortOnModuleLoadFailure("abort-on-module-fail",
llvm::cl::desc("Abort if a module failed to load"));
Expand Down Expand Up @@ -258,6 +261,7 @@ struct SDKNodeInitInfo {
StringRef Location;
StringRef ModuleName;
bool IsThrowing = false;
bool IsMutating = false;
Optional<uint8_t> SelfIndex;
std::vector<SDKDeclAttrKind> DeclAttrs;
std::vector<TypeAttrKind> TypeAttrs;
Expand Down Expand Up @@ -680,15 +684,18 @@ class SDKNodeVar : public SDKNodeDecl {

class SDKNodeAbstractFunc : public SDKNodeDecl {
const bool IsThrowing;
const bool IsMutating;
const Optional<uint8_t> SelfIndex;

protected:
SDKNodeAbstractFunc(SDKNodeInitInfo Info, SDKNodeKind Kind) :
SDKNodeDecl(Info, Kind),
IsThrowing(Info.IsThrowing),
IsMutating(Info.IsMutating),
SelfIndex(Info.SelfIndex){}
public:
bool isThrowing() const { return IsThrowing; }
bool isMutating() const { return IsMutating; }
uint8_t getSelfIndex() const { return SelfIndex.getValue(); }
Optional<uint8_t> getSelfIndexOptional() const { return SelfIndex; }
bool hasSelfIndex() const { return SelfIndex.hasValue(); }
Expand Down Expand Up @@ -797,6 +804,8 @@ NodeUniquePtr SDKNode::constructSDKNode(llvm::yaml::MappingNode *Node) {
Info.ModuleName = GetScalarString(Pair.getValue());
} else if (Key == Key_throwing) {
Info.IsThrowing = true;
} else if (Key == Key_mutating) {
Info.IsMutating = true;
} else if (Key == Key_typeAttributes) {
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
for (auto It = Seq->begin(); It != Seq->end(); ++ It) {
Expand Down Expand Up @@ -943,6 +952,13 @@ static bool isFuncThrowing(ValueDecl *VD) {
return false;
}

static bool isFuncMutating(ValueDecl *VD) {
if (auto AF = dyn_cast<FuncDecl>(VD)) {
return AF->isMutating();
}
return false;
}

static Optional<uint8_t> getSelfIndex(ValueDecl *VD) {
if (auto AF = dyn_cast<AbstractFunctionDecl>(VD)) {
if (AF->isImportAsInstanceMember())
Expand All @@ -962,7 +978,8 @@ SDKNodeInitInfo::SDKNodeInitInfo(ValueDecl *VD) :
PrintedName(getPrintedName(VD)), DKind(VD->getKind()),
USR(calculateUsr(VD)), Location(calculateLocation(VD)),
ModuleName(VD->getModuleContext()->getName().str()),
IsThrowing(isFuncThrowing(VD)), SelfIndex(getSelfIndex(VD)) {
IsThrowing(isFuncThrowing(VD)), IsMutating(isFuncMutating(VD)),
SelfIndex(getSelfIndex(VD)) {
if (VD->getAttrs().getDeprecated(VD->getASTContext()))
DeclAttrs.push_back(SDKDeclAttrKind::DAK_deprecated);
}
Expand Down Expand Up @@ -1293,6 +1310,8 @@ namespace swift {
if (auto F = dyn_cast<SDKNodeAbstractFunc>(value.get())) {
if (bool isThrowing = F->isThrowing())
out.mapRequired(Key_throwing, isThrowing);
if (bool isMutating = F->isMutating())
out.mapRequired(Key_mutating, isMutating);
if (F->hasSelfIndex()) {
auto Index = F->getSelfIndex();
out.mapRequired(Key_selfIndex, Index);
Expand Down Expand Up @@ -3303,6 +3322,7 @@ static int prepareForDump(const char *Main,
InitInvok.setRuntimeResourcePath(options::ResourceDir);
}
InitInvok.setFrameworkSearchPaths(options::FrameworkPaths);
InitInvok.setImportSearchPaths(options::ModuleInputPaths);

if (!options::ModuleList.empty()) {
if (readFileLineByLine(options::ModuleList, Modules))
Expand Down