Skip to content

Commit f48da45

Browse files
author
Gabor Horvath
committed
[cxx-interop] Configure requires ObjC from frontend option
We sometimes don't have the information in the modulemaps whether a module requires ObjC or not. This info is useful for reverse interop. This PR introduces a frontend flag to have a comma separated list of modules that we should import as if they had "requires ObjC" in their modulemaps.
1 parent ab12392 commit f48da45

File tree

7 files changed

+40
-13
lines changed

7 files changed

+40
-13
lines changed

include/swift/AST/SwiftNameTranslation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_NAME_TRANSLATION_H
1414
#define SWIFT_NAME_TRANSLATION_H
1515

16+
#include "swift/AST/ASTContext.h"
1617
#include "swift/AST/AttrKind.h"
1718
#include "swift/AST/Decl.h"
1819
#include "swift/AST/DiagnosticEngine.h"
@@ -113,7 +114,7 @@ inline bool isExposableToCxx(
113114
}
114115

115116
bool isObjCxxOnly(const ValueDecl *VD);
116-
bool isObjCxxOnly(const clang::Decl *D);
117+
bool isObjCxxOnly(const clang::Decl *D, const ASTContext &ctx);
117118

118119
/// Returns true if the given value decl D is visible to C++ of its
119120
/// own accord (i.e. without considering its context)

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ namespace swift {
632632
/// All block list configuration files to be honored in this compilation.
633633
std::vector<std::string> BlocklistConfigFilePaths;
634634

635+
/// List of top level modules to be considered as if they had require ObjC
636+
/// in their module map.
637+
llvm::SmallVector<StringRef> ModulesRequiringObjC;
638+
635639
/// Whether to ignore checks that a module is resilient during
636640
/// type-checking, SIL verification, and IR emission,
637641
bool BypassResilienceChecks = false;

lib/AST/SwiftNameTranslation.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "clang/AST/DeclObjC.h"
3131
#include "clang/Basic/Module.h"
32+
#include "llvm/ADT/STLExtras.h"
3233
#include "llvm/ADT/SmallString.h"
3334
#include <optional>
3435

@@ -242,10 +243,12 @@ swift::cxx_translation::getNameForCxx(const ValueDecl *VD,
242243
namespace {
243244
struct ObjCTypeWalker : TypeWalker {
244245
bool hadObjCType = false;
246+
const ASTContext &ctx;
247+
ObjCTypeWalker(const ASTContext &ctx) : ctx(ctx) {}
245248
Action walkToTypePre(Type ty) override {
246249
if (auto *nominal = ty->getNominalOrBoundGenericNominal()) {
247250
if (auto clangDecl = nominal->getClangDecl()) {
248-
if (cxx_translation::isObjCxxOnly(clangDecl)) {
251+
if (cxx_translation::isObjCxxOnly(clangDecl, ctx)) {
249252
hadObjCType = true;
250253
return Action::Stop;
251254
}
@@ -257,19 +260,29 @@ struct ObjCTypeWalker : TypeWalker {
257260
} // namespace
258261

259262
bool swift::cxx_translation::isObjCxxOnly(const ValueDecl *VD) {
260-
ObjCTypeWalker walker;
263+
ObjCTypeWalker walker{VD->getASTContext()};
261264
VD->getInterfaceType().walk(walker);
262265
return walker.hadObjCType;
263266
}
264267

265-
bool swift::cxx_translation::isObjCxxOnly(const clang::Decl *D) {
268+
bool swift::cxx_translation::isObjCxxOnly(const clang::Decl *D,
269+
const ASTContext &ctx) {
266270
// By default, we import all modules in Obj-C++ mode, so there is no robust
267-
// way to tell if something is coming from an Obj-C module. The best
268-
// approximation is to check if something is a framework module as most of
269-
// those are written in Obj-C. Ideally, we want to add `requires ObjC` to all
270-
// ObjC modules and respect that here to make this completely precise.
271-
return D->getASTContext().getLangOpts().ObjC &&
272-
D->getOwningModule()->isPartOfFramework();
271+
// way to tell if something is coming from an Obj-C module. Use the
272+
// requirements and the language options to check if we should actually
273+
// consider the module to have ObjC constructs.
274+
const auto &langOpts = D->getASTContext().getLangOpts();
275+
auto clangModule = D->getOwningModule()->getTopLevelModule();
276+
bool requiresObjC = false;
277+
for (auto req : clangModule->Requirements)
278+
if (req.RequiredState && req.FeatureName == "objc")
279+
requiresObjC = true;
280+
return langOpts.ObjC &&
281+
(requiresObjC ||
282+
llvm::any_of(ctx.LangOpts.ModulesRequiringObjC,
283+
[clangModule](StringRef moduleName) {
284+
return clangModule->getFullModuleName() == moduleName;
285+
}));
273286
}
274287

275288
swift::cxx_translation::DeclRepresentation

lib/Frontend/CompilerInvocation.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,8 @@ static bool ParseEnabledFeatureArgs(LangOptions &Opts, ArgList &Args,
847847
// Collect some special case pseudo-features which should be processed
848848
// separately.
849849
if (argValue.starts_with("StrictConcurrency") ||
850-
argValue.starts_with("AvailabilityMacro=")) {
850+
argValue.starts_with("AvailabilityMacro=") ||
851+
argValue.starts_with("RequiresObjC=")) {
851852
if (isEnableFeatureFlag)
852853
psuedoFeatures.push_back(argValue);
853854
continue;
@@ -974,6 +975,11 @@ static bool ParseEnabledFeatureArgs(LangOptions &Opts, ArgList &Args,
974975
Opts.AvailabilityMacros.push_back(availability.str());
975976
continue;
976977
}
978+
979+
if (featureName->starts_with("RequiresObjC")) {
980+
auto modules = featureName->split("=").second;
981+
modules.split(Opts.ModulesRequiringObjC, ",");
982+
}
977983
}
978984

979985
// Map historical flags over to experimental features. We do this for all

lib/PrintAsClang/PrintClangValueType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "OutputLanguageMode.h"
1717
#include "PrimitiveTypeMapping.h"
1818
#include "SwiftToClangInteropContext.h"
19+
#include "swift/AST/ASTContext.h"
1920
#include "swift/AST/Decl.h"
2021
#include "swift/AST/SwiftNameTranslation.h"
2122
#include "swift/AST/Type.h"
@@ -645,7 +646,7 @@ void ClangValueTypePrinter::printTypeGenericTraits(
645646

646647
bool objCxxOnly = false;
647648
if (const auto *clangDecl = typeDecl->getClangDecl()) {
648-
if (cxx_translation::isObjCxxOnly(clangDecl))
649+
if (cxx_translation::isObjCxxOnly(clangDecl, typeDecl->getASTContext()))
649650
objCxxOnly = true;
650651
}
651652

test/Interop/SwiftToCxx/stdlib/foundation-type-not-exposed-by-default-to-cxx.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
22

3-
// RUN: %target-swift-frontend %s -module-name UseFoundation -enable-experimental-cxx-interop -typecheck -verify -emit-clang-header-path %t/UseFoundation.h
3+
// RUN: %target-swift-frontend %s -module-name UseFoundation -enable-experimental-cxx-interop -typecheck -verify -emit-clang-header-path %t/UseFoundation.h -enable-experimental-feature RequiresObjC=CoreGraphics,Foundation
44
// RUN: %FileCheck %s < %t/UseFoundation.h
55

66
// RUN: %check-interop-cxx-header-in-clang(%t/UseFoundation.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY)

test/Misc/verify-swift-feature-testing.test-sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ EXCEPTIONAL_FILES = [
2121
pathlib.Path("test/ModuleInterface/swift-export-as.swift"),
2222
# Uses the pseudo-feature AvailabilityMacro=
2323
pathlib.Path("test/Availability/availability_define.swift"),
24+
# Uses the pseudo-feature RequiresObjC=
25+
pathlib.Path("test/Interop/SwiftToCxx/stdlib/foundation-type-not-exposed-by-default-to-cxx.swift"),
2426
# Tests behavior when you try to use a feature without enabling it
2527
pathlib.Path("test/attr/feature_requirement.swift"),
2628
# Tests completion with features both enabled and disabled

0 commit comments

Comments
 (0)