Skip to content

[PrintAsObjC] Special-case <os/object.h> types, like Dispatch. #6544

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
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
47 changes: 42 additions & 5 deletions lib/PrintAsObjC/PrintAsObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/Module.h"
#include "clang/Lex/Lexer.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/STLExtras.h"
Expand Down Expand Up @@ -125,6 +126,38 @@ static bool looksLikeInitMethod(ObjCSelector selector) {
return !(firstPiece.size() > 4 && clang::isLowercase(firstPiece[4]));
}

/// Returns the name of an <os/object.h> type minus the leading "OS_",
/// or an empty string if \p decl is not an <os/object.h> type.
static StringRef maybeGetOSObjectBaseName(const clang::NamedDecl *decl) {
StringRef name = decl->getName();
if (!name.consume_front("OS_"))
return StringRef();

clang::SourceLocation loc = decl->getLocation();
if (!loc.isMacroID())
return StringRef();

// Hack: check to see if the name came from a macro in <os/object.h>.
clang::SourceManager &sourceMgr = decl->getASTContext().getSourceManager();
clang::SourceLocation expansionLoc =
sourceMgr.getImmediateExpansionRange(loc).first;
clang::SourceLocation spellingLoc = sourceMgr.getSpellingLoc(expansionLoc);

if (!sourceMgr.getFilename(spellingLoc).endswith("/os/object.h"))
return StringRef();

return name;
}

/// Returns true if \p decl represents an <os/object.h> type.
static bool isOSObjectType(const clang::Decl *decl) {
auto *named = dyn_cast_or_null<clang::NamedDecl>(decl);
if (!named)
return false;
return !maybeGetOSObjectBaseName(named).empty();
}


namespace {
using DelayedMemberSet = llvm::SmallSetVector<const ValueDecl *, 32>;

Expand Down Expand Up @@ -1320,18 +1353,21 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
assert(CD->isObjC());
auto clangDecl = dyn_cast_or_null<clang::NamedDecl>(CD->getClangDecl());
if (clangDecl) {
if (isa<clang::ObjCInterfaceDecl>(clangDecl)) {
// Hack for <os/object.h> types, which use classes in Swift but
// protocols in Objective-C, and a typedef to hide the difference.
StringRef osObjectName = maybeGetOSObjectBaseName(clangDecl);
if (!osObjectName.empty()) {
os << osObjectName << "_t";
} else if (isa<clang::ObjCInterfaceDecl>(clangDecl)) {
os << clangDecl->getName() << " *";
printNullability(optionalKind);
} else {
maybePrintTagKeyword(CD);
os << clangDecl->getName();
printNullability(optionalKind);
}
} else {
os << getNameForObjC(CD) << " *";
printNullability(optionalKind);
}
printNullability(optionalKind);
}

void visitProtocolType(ProtocolType *PT,
Expand Down Expand Up @@ -1799,7 +1835,8 @@ class ModuleWriter {

bool forwardDeclare(const ClassDecl *CD) {
if (!CD->isObjC() ||
CD->getForeignClassKind() == ClassDecl::ForeignKind::CFType) {
CD->getForeignClassKind() == ClassDecl::ForeignKind::CFType ||
isOSObjectType(CD->getClangDecl())) {
return false;
}
forwardDeclare(CD, [&]{ os << "@class " << getNameForObjC(CD) << ";\n"; });
Expand Down
16 changes: 16 additions & 0 deletions test/PrintAsObjC/dispatch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: %target-swift-frontend -typecheck %s -parse-as-library -emit-objc-header-path %t/swift.h
// RUN: %FileCheck %s < %t/swift.h

// REQUIRES: objc_interop

import Foundation

// CHECK: @import Dispatch;

// CHECK-LABEL: @interface Test : NSObject{{$}}
public class Test : NSObject {
// CHECK-NEXT: - (void)thank:(dispatch_queue_t _Nonnull)queue;
public func thank(_ queue: DispatchQueue) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

// CHECK-NEXT: init
} // CHECK-NEXT: @end