Skip to content

ABIChecker: add an option to avoid diagnosing about certain given SPI groups #59273

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
Jun 5, 2022
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
9 changes: 9 additions & 0 deletions include/swift/APIDigester/ModuleAnalyzerNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ struct CheckerOptions {
bool Migrator;
StringRef LocationFilter;
std::vector<std::string> ToolArgs;
llvm::StringSet<> SPIGroupNamesToIgnore;
};

class SDKContext {
Expand Down Expand Up @@ -346,6 +347,7 @@ class SDKNodeDecl: public SDKNode {
StringRef Location;
StringRef ModuleName;
std::vector<DeclAttrKind> DeclAttributes;
std::vector<StringRef> SPIGroups;
bool IsImplicit;
bool IsStatic;
bool IsDeprecated;
Expand Down Expand Up @@ -373,6 +375,7 @@ class SDKNodeDecl: public SDKNode {
StringRef getModuleName() const {return ModuleName;}
StringRef getHeaderName() const;
ArrayRef<DeclAttrKind> getDeclAttributes() const;
ArrayRef<StringRef> getSPIGroups() const { return SPIGroups; }
bool hasAttributeChange(const SDKNodeDecl &Another) const;
swift::ReferenceOwnership getReferenceOwnership() const {
return swift::ReferenceOwnership(ReferenceOwnership);
Expand Down Expand Up @@ -415,6 +418,12 @@ class SDKNodeDecl: public SDKNode {
if (isObjc())
return;
}
// Don't emit SPIs if the group name is out-out.
for (auto spi: getSPIGroups()) {
if (Ctx.getOpts().SPIGroupNamesToIgnore.contains(spi)) {
return;
}
}
Ctx.getDiags(Loc).diagnose(Loc, ID, getScreenInfo(), std::move(Args)...);
}
};
Expand Down
1 change: 1 addition & 0 deletions include/swift/IDE/DigesterEnums.def
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ KEY_STRING(InitKind, init_kind)

KEY_STRING_ARR(SuperclassNames, superclassNames)
KEY_STRING_ARR(ToolArgs, tool_arguments)
KEY_STRING_ARR(SPIGroups, spi_group_names)

KEY_UINT(SelfIndex, selfIndex)
KEY_UINT(FixedBinaryOrder, fixedbinaryorder)
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,10 @@ def ignored_usrs: Separate<["-", "--"], "ignored-usrs">,
HelpText<"the file containing USRs of removed decls that the digester should ignore">,
MetaVarName<"<path>">;

def ignore_spi_groups : Separate<["-", "--"], "ignore-spi-group">,
Flags<[NoDriverOption, SwiftAPIDigesterOption]>,
HelpText<"SPI group name to not diagnose about">;

def protocol_requirement_allow_list: Separate<["-", "--"], "protocol-requirement-allow-list">,
Flags<[NoDriverOption, SwiftAPIDigesterOption, ArgumentIsPath]>,
HelpText<"File containing a new-line separated list of protocol names">,
Expand Down
12 changes: 10 additions & 2 deletions lib/APIDigester/ModuleAnalyzerNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ SDKNodeDecl::SDKNodeDecl(SDKNodeInitInfo Info, SDKNodeKind Kind)
: SDKNode(Info, Kind), DKind(Info.DKind), Usr(Info.Usr),
MangledName(Info.MangledName), Loc(Info.Loc),
Location(Info.Location), ModuleName(Info.ModuleName),
DeclAttributes(Info.DeclAttrs), IsImplicit(Info.IsImplicit),
DeclAttributes(Info.DeclAttrs),
SPIGroups(Info.SPIGroups),
IsImplicit(Info.IsImplicit),
IsStatic(Info.IsStatic), IsDeprecated(Info.IsDeprecated),
IsProtocolReq(Info.IsProtocolReq),
IsOverriding(Info.IsOverriding),
Expand Down Expand Up @@ -1436,7 +1438,12 @@ SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, Decl *D):
IsDeprecated(D->getAttrs().getDeprecated(D->getASTContext())),
IsABIPlaceholder(isABIPlaceholderRecursive(D)),
IsFromExtension(isDeclaredInExtension(D)),
DeclAttrs(collectDeclAttributes(D)) {}
DeclAttrs(collectDeclAttributes(D)) {
// Keep track of SPI group names
for (auto id: D->getSPIGroups()) {
SPIGroups.push_back(id.str());
}
}

SDKNodeInitInfo::SDKNodeInitInfo(SDKContext &Ctx, OperatorDecl *OD):
SDKNodeInitInfo(Ctx, cast<Decl>(OD)) {
Expand Down Expand Up @@ -2083,6 +2090,7 @@ void SDKNodeDecl::jsonize(json::Output &out) {
out.mapRequired(getKeyContent(Ctx, KeyKind::KK_ownership).data(), Raw);
}
output(out, KeyKind::KK_isFromExtension, IsFromExtension);
out.mapOptional(getKeyContent(Ctx, KeyKind::KK_spi_group_names).data(), SPIGroups);
}

void SDKNodeDeclAbstractFunc::jsonize(json::Output &out) {
Expand Down
3 changes: 2 additions & 1 deletion lib/DriverTool/swift_api_digester_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,8 @@ class SwiftAPIDigesterInvocation {
CompilerStyleDiags || !SerializedDiagPath.empty();
for (auto Arg : Args)
CheckerOpts.ToolArgs.push_back(Arg);

for(auto spi: ParsedArgs.getAllArgValues(OPT_ignore_spi_groups))
CheckerOpts.SPIGroupNamesToIgnore.insert(spi);
if (!SDK.empty()) {
auto Ver = getSDKBuildVersion(SDK);
if (!Ver.empty()) {
Expand Down
2 changes: 2 additions & 0 deletions test/api-digester/Inputs/cake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public func silgenNamedFunc() {}

@available(OSX 10.7, *)
@_originallyDefinedIn(module: "Bread", OSX 10.9)
@_spi(top_secret_1)
@_spi(top_secret_2)
public class SinkingClass {
public init() {}
}
10 changes: 10 additions & 0 deletions test/api-digester/Outputs/cake-abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,10 @@
"usr": "s:4cake12SinkingClassCACycfc",
"mangledName": "$s5Bread12SinkingClassCACycfc",
"moduleName": "cake",
"spi_group_names": [
"top_secret_2",
"top_secret_1"
],
"init_kind": "Designated"
}
],
Expand All @@ -1665,8 +1669,14 @@
"moduleName": "cake",
"intro_Macosx": "10.7",
"declAttributes": [
"SPIAccessControl",
"SPIAccessControl",
"OriginallyDefinedIn",
"Available"
],
"spi_group_names": [
"top_secret_2",
"top_secret_1"
]
},
{
Expand Down
10 changes: 10 additions & 0 deletions test/api-digester/Outputs/cake.json
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,10 @@
"usr": "s:4cake12SinkingClassCACycfc",
"mangledName": "$s5Bread12SinkingClassCACycfc",
"moduleName": "cake",
"spi_group_names": [
"top_secret_2",
"top_secret_1"
],
"init_kind": "Designated"
}
],
Expand All @@ -1526,8 +1530,14 @@
"moduleName": "cake",
"intro_Macosx": "10.7",
"declAttributes": [
"SPIAccessControl",
"SPIAccessControl",
"OriginallyDefinedIn",
"Available"
],
"spi_group_names": [
"top_secret_2",
"top_secret_1"
]
},
{
Expand Down
22 changes: 22 additions & 0 deletions test/api-digester/ignore-spi-by-given-group-name.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -emit-module -o %t/Foo.swiftmodule -emit-abi-descriptor-path %t/abi-before.json %s -enable-library-evolution -DBASELINE -emit-tbd-path %t/abi-before.tbd
// RUN: %target-swift-frontend -emit-module -o %t/Foo.swiftmodule -emit-abi-descriptor-path %t/abi-after.json %s -enable-library-evolution -emit-tbd-path %t/abi-after.tbd
// RUN: %api-digester -diagnose-sdk --input-paths %t/abi-before.json -input-paths %t/abi-after.json -abi -o %t/result.txt
// RUN: %FileCheck %s -check-prefix CHECK_INCLUDE_SPI < %t/result.txt

// RUN: %api-digester -diagnose-sdk --input-paths %t/abi-before.json -input-paths %t/abi-after.json -abi -o %t/result.txt -ignore-spi-group secret
// RUN: %FileCheck -check-prefix CHECK_EXCLUDE_SPI %s < %t/result.txt

#if BASELINE

@_spi(secret)
public func foo() {}

#else


#endif

// CHECK_INCLUDE_SPI: Func foo() has been removed
// CHECK_EXCLUDE_SPI-NOT: foo()