Skip to content

Commit 58c6cfc

Browse files
committed
Extract cache checking to shared function
1 parent 6aec9ba commit 58c6cfc

File tree

2 files changed

+53
-58
lines changed

2 files changed

+53
-58
lines changed

lib/ClangImporter/ImportDecl.cpp

+49-58
Original file line numberDiff line numberDiff line change
@@ -8353,6 +8353,53 @@ static bool importAsUnsafe(ClangImporter::Implementation &impl,
83538353
return false;
83548354
}
83558355

8356+
void ClangImporter::Implementation::importNontrivialAttribute(
8357+
Decl *MappedDecl, llvm::StringRef AttrString) {
8358+
bool cached = true;
8359+
while (true) {
8360+
// Dig out a source file we can use for parsing.
8361+
auto &sourceFile = getClangSwiftAttrSourceFile(
8362+
*MappedDecl->getDeclContext()->getParentModule(), AttrString, cached);
8363+
8364+
auto topLevelDecls = sourceFile.getTopLevelDecls();
8365+
8366+
// If we're using the cached version, check whether we can correctly
8367+
// clone the attribute.
8368+
if (cached) {
8369+
bool hasNonclonableAttribute = false;
8370+
for (auto decl : topLevelDecls) {
8371+
if (hasNonclonableAttribute)
8372+
break;
8373+
8374+
for (auto attr : decl->getAttrs()) {
8375+
if (!attr->canClone()) {
8376+
hasNonclonableAttribute = true;
8377+
break;
8378+
}
8379+
}
8380+
}
8381+
8382+
// We cannot clone one of the attributes. Go back and build a new
8383+
// source file without caching it.
8384+
if (hasNonclonableAttribute) {
8385+
cached = false;
8386+
continue;
8387+
}
8388+
}
8389+
8390+
// Collect the attributes from the synthesized top-level declaration in
8391+
// the source file. If we're using a cached copy, clone the attribute.
8392+
for (auto decl : topLevelDecls) {
8393+
SmallVector<DeclAttribute *, 2> attrs(decl->getAttrs().begin(),
8394+
decl->getAttrs().end());
8395+
for (auto attr : attrs) {
8396+
MappedDecl->getAttrs().add(cached ? attr->clone(SwiftContext) : attr);
8397+
}
8398+
}
8399+
break;
8400+
}
8401+
}
8402+
83568403
void
83578404
ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
83588405
auto ClangDecl =
@@ -8490,53 +8537,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
84908537
continue;
84918538
}
84928539

8493-
bool cached = true;
8494-
while (true) {
8495-
// Dig out a source file we can use for parsing.
8496-
auto &sourceFile = getClangSwiftAttrSourceFile(
8497-
*MappedDecl->getDeclContext()->getParentModule(),
8498-
swiftAttr->getAttribute(),
8499-
cached);
8500-
8501-
auto topLevelDecls = sourceFile.getTopLevelDecls();
8502-
8503-
// If we're using the cached version, check whether we can correctly
8504-
// clone the attribute.
8505-
if (cached) {
8506-
bool hasNonclonableAttribute = false;
8507-
for (auto decl : topLevelDecls) {
8508-
if (hasNonclonableAttribute)
8509-
break;
8510-
8511-
for (auto attr : decl->getAttrs()) {
8512-
if (!attr->canClone()) {
8513-
hasNonclonableAttribute = true;
8514-
break;
8515-
}
8516-
}
8517-
}
8518-
8519-
// We cannot clone one of the attributes. Go back and build a new
8520-
// source file without caching it.
8521-
if (hasNonclonableAttribute) {
8522-
cached = false;
8523-
continue;
8524-
}
8525-
}
8526-
8527-
// Collect the attributes from the synthesized top-level declaration in
8528-
// the source file. If we're using a cached copy, clone the attribute.
8529-
for (auto decl : topLevelDecls) {
8530-
SmallVector<DeclAttribute *, 2> attrs(decl->getAttrs().begin(),
8531-
decl->getAttrs().end());
8532-
for (auto attr : attrs) {
8533-
MappedDecl->getAttrs().add(cached ? attr->clone(SwiftContext)
8534-
: attr);
8535-
}
8536-
}
8537-
8538-
break;
8539-
}
8540+
importNontrivialAttribute(MappedDecl, swiftAttr->getAttribute());
85408541
}
85418542

85428543
if (seenUnsafe || importAsUnsafe(*this, ClangDecl, MappedDecl)) {
@@ -8683,17 +8684,7 @@ void ClangImporter::Implementation::importBoundsAttributes(
86838684
}
86848685
}
86858686

8686-
// Dig out a source file we can use for parsing.
8687-
auto &sourceFile = getClangSwiftAttrSourceFile(
8688-
*MappedDecl->getDeclContext()->getParentModule(), MacroString);
8689-
8690-
// Collect the attributes from the synthesized top-level declaration in
8691-
// the source file.
8692-
auto topLevelDecls = sourceFile.getTopLevelDecls();
8693-
for (auto decl : topLevelDecls) {
8694-
for (auto attr : decl->getAttrs())
8695-
MappedDecl->getAttrs().add(attr->clone(SwiftContext));
8696-
}
8687+
importNontrivialAttribute(MappedDecl, MacroString);
86978688
}
86988689

86998690
static bool isUsingMacroName(clang::SourceManager &SM,

lib/ClangImporter/ImporterImpl.h

+4
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
10601060
SourceFile &getClangSwiftAttrSourceFile(
10611061
ModuleDecl &module, StringRef attributeText, bool cached);
10621062

1063+
/// Create attribute with given text and attach it to decl, creating or
1064+
/// retrieving a chached source file as needed.
1065+
void importNontrivialAttribute(Decl *MappedDecl, StringRef attributeText);
1066+
10631067
/// Utility function to import Clang attributes from a source Swift decl to
10641068
/// synthesized Swift decl.
10651069
///

0 commit comments

Comments
 (0)