Skip to content

[4.0jun23][Clang importer/module printing] Correctly print NS_ERROR_ENUMs. #10651

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
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: 8 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2272,7 +2272,14 @@ void ClangModuleUnit::getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {
owner.importDecl(decl, owner.CurrentVersion);
if (!importedDecl) continue;

auto ext = dyn_cast<ExtensionDecl>(importedDecl->getDeclContext());
// Find the enclosing extension, if there is one.
ExtensionDecl *ext = nullptr;
for (auto importedDC = importedDecl->getDeclContext();
!importedDC->isModuleContext();
importedDC = importedDC->getParent()) {
ext = dyn_cast<ExtensionDecl>(importedDC);
if (ext) break;
}
if (!ext) continue;

if (knownExtensions.insert(ext).second)
Expand Down
14 changes: 12 additions & 2 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7921,8 +7921,18 @@ ClangImporter::Implementation::loadAllMembers(Decl *D, uint64_t extra) {

// Then try to import the decl under the specified name.
auto *member = importDecl(decl, nameVersion);
if (!member || member->getDeclContext() != ext)
return;
if (!member) return;

// Find the member that will land in an extension context.
while (!isa<ExtensionDecl>(member->getDeclContext())) {
auto nominal = dyn_cast<NominalTypeDecl>(member->getDeclContext());
if (!nominal) return;

member = nominal;
if (member->hasClangNode()) return;
}

if (member->getDeclContext() != ext) return;
ext->addMember(member);

for (auto alternate : getAlternateDecls(member)) {
Expand Down
61 changes: 41 additions & 20 deletions lib/IDE/ModuleInterfacePrinting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,49 @@ swift::ide::collectModuleGroups(ModuleDecl *M, std::vector<StringRef> &Scratch)
return llvm::makeArrayRef(Scratch);
}

/// Determine whether the given extension has a Clang node that
/// created it (vs. being a Swift extension).
static bool extensionHasClangNode(ExtensionDecl *ext) {
// If it has a Clang node (directly),
if (ext->hasClangNode()) return true;
/// Retrieve the effective Clang node for the given declaration, which
/// copes with the odd case of imported Error enums.
static ClangNode getEffectiveClangNode(const Decl *decl) {
// Directly...
if (auto clangNode = decl->getClangNode())
return clangNode;

// Or via the nested "Code" enum.
if (auto nominal =
const_cast<NominalTypeDecl *>(dyn_cast<NominalTypeDecl>(decl))) {
auto &ctx = nominal->getASTContext();
for (auto code : nominal->lookupDirect(ctx.Id_Code,
/*ignoreNewExtensions=*/true)) {
if (auto clangDecl = code->getClangDecl())
return clangDecl;
}
}

// If it has a global imported as a member.
auto members = ext->getMembers();
if (members.empty()) return false;
return members.front()->hasClangNode();
return ClangNode();
}

/// Retrieve the Clang node for the given extension, if it has one.
/// created it (vs. being a Swift extension).
static ClangNode extensionGetClangNode(ExtensionDecl *ext) {
// If it has a Clang node (directly),
if (ext->hasClangNode()) return ext->getClangNode();

// If it has a global imported as a member.
auto members = ext->getMembers();
if (members.empty()) return ClangNode();
return members.front()->getClangNode();
// Check whether it was syntheszed into a module-scope context.
if (!isa<ClangModuleUnit>(ext->getModuleScopeContext()))
return ClangNode();

// It may have a global imported as a member.
for (auto member : ext->getMembers()) {
if (auto clangNode = getEffectiveClangNode(member))
return clangNode;
}

return ClangNode();
}

/// Determine whether the given extension has a Clang node that
/// created it (vs. being a Swift extension).
static bool extensionHasClangNode(ExtensionDecl *ext) {
return static_cast<bool>(extensionGetClangNode(ext));
}

Optional<StringRef>
Expand Down Expand Up @@ -393,8 +414,8 @@ void swift::ide::printSubmoduleInterface(
}
};

if (D->hasClangNode()) {
addToClangDecls(D, D->getClangNode());
if (auto clangNode = getEffectiveClangNode(D)) {
addToClangDecls(D, clangNode);
continue;
}

Expand Down Expand Up @@ -739,8 +760,8 @@ void swift::ide::printHeaderInterface(
std::sort(ClangDecls.begin(), ClangDecls.end(),
[&](Decl *LHS, Decl *RHS) -> bool {
return ClangSM.isBeforeInTranslationUnit(
LHS->getClangNode().getLocation(),
RHS->getClangNode().getLocation());
getEffectiveClangNode(LHS).getLocation(),
getEffectiveClangNode(RHS).getLocation());
});

ASTPrinter *PrinterToUse = &Printer;
Expand Down Expand Up @@ -789,7 +810,7 @@ void ClangCommentPrinter::printDeclPre(const Decl *D,
// single line.
// FIXME: we should fix that, since it also affects struct members, etc.
if (!isa<ParamDecl>(D)) {
if (auto ClangN = D->getClangNode()) {
if (auto ClangN = getEffectiveClangNode(D)) {
printCommentsUntil(ClangN);
if (shouldPrintNewLineBefore(ClangN)) {
*this << "\n";
Expand All @@ -813,7 +834,7 @@ void ClangCommentPrinter::printDeclPost(const Decl *D,
*this << " " << ASTPrinter::sanitizeUtf8(CommentText);
}
PendingComments.clear();
if (auto ClangN = D->getClangNode())
if (auto ClangN = getEffectiveClangNode(D))
updateLastEntityLine(ClangN.getSourceRange().getEnd());
}

Expand Down
11 changes: 11 additions & 0 deletions test/IDE/print_clang_decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@
// FOUNDATION-NEXT: @available(*, unavailable, message: "Zone-based memory management is unavailable")
// FOUNDATION-NEXT: NSSetZoneName(_ zone: NSZone, _ name: String)

// FOUNDATION-LABEL: struct FictionalServerError
// FOUNDATION: enum Code
// FOUNDATION: case meltedDown
// FOUNDATION: static var meltedDown: FictionalServerError.Code

// FOUNDATION-LABEL: extension NSLaundromat {
// FOUNDATION-NEXT: struct Error
// FOUNDATION: enum Code
// FOUNDATION: case tooMuchSoap
// FOUNDATION: static var tooMuchSoap: NSLaundromat.Error.Code { get }

// CTYPESBITS-NOT: FooStruct1
// CTYPESBITS: {{^}}typealias DWORD = Int32{{$}}
// CTYPESBITS-NEXT: {{^}}var MY_INT: Int32 { get }{{$}}
Expand Down
10 changes: 10 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/Foundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -1100,3 +1100,13 @@ typedef enum __attribute__((ns_error_domain(FictionalServerErrorDomain))) Fictio
- (void)bleach:(Coat <Garment, Cotton> * _Nonnull)garment;
- (Coat <Garment> * _Nonnull)dry;
@end

@interface NSLaundromat : NSObject
@end

extern NSString * const NSLaundryErrorDomain;

typedef enum __attribute__((ns_error_domain(NSLaundryErrorDomain))) __attribute__((swift_name("NSLaundromat.Error"))) NSLaundryErrorCode {
NSLaundryErrorTooMuchSoap = 1,
NSLaundryErrorCatInWasher = 2
};