Skip to content

[Clang importer] Don't cache swift_attr source files that have CustomAttrs with arguments #77962

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 1 commit
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
10 changes: 9 additions & 1 deletion include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,17 @@ class DeclAttribute : public AttributeBase {

/// Create a copy of this attribute.
DeclAttribute *clone(ASTContext &ctx) const;

/// Determine whether we can clone this attribute.
bool canClone() const;
};

#define UNIMPLEMENTED_CLONE(AttrType) \
AttrType *clone(ASTContext &ctx) const { \
llvm_unreachable("unimplemented"); \
return nullptr; \
}
} \
bool canClone() const { return false; }

/// Describes a "simple" declaration attribute that carries no data.
template<DeclAttrKind Kind>
Expand Down Expand Up @@ -1916,9 +1920,13 @@ class CustomAttr final : public DeclAttribute {

/// Create a copy of this attribute.
CustomAttr *clone(ASTContext &ctx) const {
assert(argList == nullptr &&
"Cannot clone custom attribute with an argument list");
return create(ctx, AtLoc, getTypeExpr(), initContext, argList, isImplicit());
}

bool canClone() const { return argList == nullptr; }

private:
friend class CustomAttrNominalRequest;
void resetTypeInformation(TypeExpr *repr);
Expand Down
11 changes: 11 additions & 0 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,17 @@ DeclAttribute *DeclAttribute::clone(ASTContext &ctx) const {
}
}

bool DeclAttribute::canClone() const {
switch (getKind()) {
#define DECL_ATTR(_,CLASS, ...) \
case DeclAttrKind::CLASS: \
if (&CLASS##Attr::canClone == &DeclAttribute::canClone) \
return true; \
return static_cast<const CLASS##Attr *>(this)->canClone();
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, why this manual dynamic dispatch rather than a virtual function? Does DeclAttribute not have a vtable or something?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's right: there's no vtable, so I don't want to add one.

#include "swift/AST/DeclAttr.def"
}
}

const BackDeployedAttr *
DeclAttributes::getBackDeployed(const ASTContext &ctx,
bool forTargetVariant) const {
Expand Down
19 changes: 11 additions & 8 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8476,26 +8476,29 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
break;

for (auto attr : decl->getAttrs()) {
if (auto customAttr = dyn_cast<CustomAttr>(attr)) {
if (customAttr->getArgs() != nullptr) {
hasNonclonableAttribute = true;
break;
}
if (!attr->canClone()) {
hasNonclonableAttribute = true;
break;
}
}
}

// We cannot clone one of the attributes. Go back and build a new
// source file without caching it.
if (hasNonclonableAttribute) {
cached = false;
continue;
}
}

// Collect the attributes from the synthesized top-level declaration in
// the source file.
// the source file. If we're using a cached copy, clone the attribute.
for (auto decl : topLevelDecls) {
for (auto attr : decl->getAttrs()) {
MappedDecl->getAttrs().add(attr->clone(SwiftContext));
SmallVector<DeclAttribute *, 2> attrs(decl->getAttrs().begin(),
decl->getAttrs().end());
for (auto attr : attrs) {
MappedDecl->getAttrs().add(cached ? attr->clone(SwiftContext)
: attr);
}
}

Expand Down