@@ -8353,6 +8353,53 @@ static bool importAsUnsafe(ClangImporter::Implementation &impl,
8353
8353
return false;
8354
8354
}
8355
8355
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
+
8356
8403
void
8357
8404
ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
8358
8405
auto ClangDecl =
@@ -8490,53 +8537,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
8490
8537
continue;
8491
8538
}
8492
8539
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());
8540
8541
}
8541
8542
8542
8543
if (seenUnsafe || importAsUnsafe(*this, ClangDecl, MappedDecl)) {
@@ -8683,17 +8684,7 @@ void ClangImporter::Implementation::importBoundsAttributes(
8683
8684
}
8684
8685
}
8685
8686
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);
8697
8688
}
8698
8689
8699
8690
static bool isUsingMacroName(clang::SourceManager &SM,
0 commit comments