Skip to content

ClangImporter: teach clang importer to import Clang SPI symbols and model them similarly as Swift SPIs #39068

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 1 commit into from
Aug 28, 2021
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 include/swift/Strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ constexpr static const StringLiteral DEFAULT_ACTOR_STORAGE_FIELD_NAME =
/// The name of the Builtin type prefix
constexpr static const StringLiteral BUILTIN_TYPE_NAME_PREFIX = "Builtin.";

/// The default SPI group name to associate with Clang SPIs.
constexpr static const StringLiteral CLANG_MODULE_DEFUALT_SPI_GROUP_NAME =
"OBJC_DEFAULT_SPI_GROUP";

/// A composition class containing a StringLiteral for the names of
/// Swift builtins. The reason we use this is to ensure that we when
/// necessary slice off the "Builtin." prefix from these names in a
Expand Down
26 changes: 26 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8545,6 +8545,21 @@ Optional<bool> swift::importer::isMainActorAttr(
return None;
}

static bool isUsingMacroName(clang::SourceManager &SM,
clang::SourceLocation loc,
StringRef MacroName) {
if (!loc.isMacroID())
return false;
auto Sloc = SM.getExpansionLoc(loc);
if (Sloc.isInvalid())
return false;
auto Eloc = Sloc.getLocWithOffset(MacroName.size());
if (Eloc.isInvalid())
return false;
StringRef content(SM.getCharacterData(Sloc), MacroName.size());
return content == MacroName;
}

/// Import Clang attributes as Swift attributes.
void ClangImporter::Implementation::importAttributes(
const clang::NamedDecl *ClangDecl,
Expand Down Expand Up @@ -8668,6 +8683,17 @@ void ClangImporter::Implementation::importAttributes(
AnyUnavailable = true;
}

if (isUsingMacroName(getClangASTContext().getSourceManager(),
avail->getLoc(), "SPI_AVAILABLE") ||
isUsingMacroName(getClangASTContext().getSourceManager(),
avail->getLoc(), "__SPI_AVAILABLE")) {
// The decl has been marked as SPI in the header by using the SPI macro,
// thus we add the SPI attribute to it with a default group name.
MappedDecl->getAttrs().add(SPIAccessControlAttr::create(SwiftContext,
SourceLoc(), SourceRange(),
SwiftContext.getIdentifier(CLANG_MODULE_DEFUALT_SPI_GROUP_NAME)));
}

StringRef message = avail->getMessage();

llvm::VersionTuple deprecated = avail->getDeprecated();
Expand Down
1 change: 1 addition & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation

/// The DWARF importer delegate, if installed.
DWARFImporterDelegate *DWARFImporter = nullptr;

public:
/// Only used for testing.
void setDWARFImporterDelegate(DWARFImporterDelegate &delegate);
Expand Down
18 changes: 18 additions & 0 deletions lib/Sema/ImportResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Parse/Parser.h"
#include "swift/Subsystems.h"
#include "swift/Strings.h"
#include "clang/Basic/Module.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/TinyPtrVector.h"
Expand Down Expand Up @@ -317,6 +318,23 @@ void ImportResolver::bindImport(UnboundImport &&I) {
ID.get()->setModule(nullptr);
return;
}
// If the imported module is a clang module, add an implicit import statement
// to request the SPIs from the module.
if (M->isNonSwiftModule() && ID &&
!ID.get()->getAttrs().hasAttribute<SPIAccessControlAttr>()) {
ImportDecl *id = ID.get();
auto *newId = ImportDecl::create(id->getASTContext(), id->getDeclContext(),
SourceLoc(), id->getImportKind(), SourceLoc(), id->getImportPath());
// Copy all the existing attribute from the actual import statement.
llvm::for_each(id->getAttrs(),
[&](DeclAttribute *attr) {newId->getAttrs().add(attr);});
// Add SPI attribute with the default group name.
newId->getAttrs().add(SPIAccessControlAttr::create(id->getASTContext(),
SourceLoc(), SourceRange(),
{ ctx.getIdentifier(CLANG_MODULE_DEFUALT_SPI_GROUP_NAME) }));
// So we'll resolve the new import.
unboundImports.push_back(UnboundImport(newId));
}

auto topLevelModule = I.getTopLevelModule(M, SF);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#import <Foundation/Foundation.h>

#ifdef SPI_AVAILABLE
#undef SPI_AVAILABLE
#define SPI_AVAILABLE API_AVAILABLE
#endif

#ifdef __SPI_AVAILABLE
#undef __SPI_AVAILABLE
#define __SPI_AVAILABLE API_AVAILABLE
#endif

SPI_AVAILABLE(macos(10.7))
@interface SPIInterface1
@end

__SPI_AVAILABLE(macos(10.7))
@interface SPIInterface2
@end

@interface SharedInterface
+ (NSInteger)foo SPI_AVAILABLE(macos(10.7));
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
framework module SPIContainer {
umbrella header "SPIContainer.h"
export *
module * {
export *
}
}
15 changes: 15 additions & 0 deletions test/ClangImporter/availability_spi_as_unavailable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// REQUIRES: OS=macosx
// RUN: %target-swift-frontend -typecheck %s -F %S/Inputs/frameworks -verify

import SPIContainer

@_spi(a) public let a: SPIInterface1
@_spi(a) public let b: SPIInterface2

public let c: SPIInterface1 // expected-error{{cannot use class 'SPIInterface1' here; it is an SPI imported from 'SPIContainer'}}
public let d: SPIInterface2 // expected-error{{cannot use class 'SPIInterface2' here; it is an SPI imported from 'SPIContainer'}}

@inlinable
public func inlinableUsingSPI() {
SharedInterface.foo() // expected-error{{class method 'foo()' cannot be used in an '@inlinable' function because it is an SPI imported from 'SPIContainer'}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// CHECK-DUMP: IMPORTED_MODULE
// CHECK-DUMP-SAME: 'Module'
// CHECK-DUMP: IMPORTED_MODULE
// CHECK-DUMP-SAME: 'Swift'
// CHECK-DUMP: 'Swift'


import Module
Expand Down