@@ -8279,6 +8279,41 @@ static bool importAsUnsafe(ClangImporter::Implementation &impl,
8279
8279
return false ;
8280
8280
}
8281
8281
8282
+ static bool applyImportedAttribute (ClangImporter::Implementation &Impl,
8283
+ Decl *MappedDecl, StringRef attr) {
8284
+ // Dig out a source file we can use for parsing.
8285
+ auto &sourceFile = Impl.getClangSwiftAttrSourceFile (
8286
+ *MappedDecl->getDeclContext ()->getParentModule (), attr);
8287
+
8288
+ // Spin up a parser.
8289
+ swift::Parser parser (sourceFile.getBufferID (), sourceFile,
8290
+ &Impl.SwiftContext .Diags , nullptr , nullptr );
8291
+ // Prime the lexer.
8292
+ parser.consumeTokenWithoutFeedingReceiver ();
8293
+
8294
+ bool hadError = false ;
8295
+ if (parser.Tok .is (tok::at_sign)) {
8296
+ SourceLoc atEndLoc = parser.Tok .getRange ().getEnd ();
8297
+ SourceLoc atLoc = parser.consumeToken (tok::at_sign);
8298
+ PatternBindingInitializer *initContext = nullptr ;
8299
+ hadError = parser
8300
+ .parseDeclAttribute (MappedDecl->getAttrs (), atLoc, atEndLoc,
8301
+ initContext,
8302
+ /* isFromClangAttribute=*/ true )
8303
+ .isError ();
8304
+ } else {
8305
+ SourceLoc staticLoc;
8306
+ StaticSpellingKind staticSpelling;
8307
+ hadError = parser
8308
+ .parseDeclModifierList (MappedDecl->getAttrs (), staticLoc,
8309
+ staticSpelling,
8310
+ /* isFromClangAttribute=*/ true )
8311
+ .isError ();
8312
+ }
8313
+
8314
+ return hadError;
8315
+ }
8316
+
8282
8317
void
8283
8318
ClangImporter::Implementation::importSwiftAttrAttributes (Decl *MappedDecl) {
8284
8319
auto ClangDecl =
@@ -8297,7 +8332,6 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
8297
8332
8298
8333
std::optional<const clang::SwiftAttrAttr *> seenMainActorAttr;
8299
8334
const clang::SwiftAttrAttr *seenMutabilityAttr = nullptr ;
8300
- PatternBindingInitializer *initContext = nullptr ;
8301
8335
8302
8336
auto importAttrsFromDecl = [&](const clang::NamedDecl *ClangDecl) {
8303
8337
//
@@ -8417,37 +8451,8 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
8417
8451
continue ;
8418
8452
}
8419
8453
8420
- // Dig out a source file we can use for parsing.
8421
- auto &sourceFile = getClangSwiftAttrSourceFile (
8422
- *MappedDecl->getDeclContext ()->getParentModule (),
8423
- swiftAttr->getAttribute ());
8424
-
8425
- // Spin up a parser.
8426
- swift::Parser parser (
8427
- sourceFile.getBufferID (), sourceFile, &SwiftContext.Diags ,
8428
- nullptr , nullptr );
8429
- // Prime the lexer.
8430
- parser.consumeTokenWithoutFeedingReceiver ();
8431
-
8432
- bool hadError = false ;
8433
- if (parser.Tok .is (tok::at_sign)) {
8434
- SourceLoc atEndLoc = parser.Tok .getRange ().getEnd ();
8435
- SourceLoc atLoc = parser.consumeToken (tok::at_sign);
8436
- hadError = parser
8437
- .parseDeclAttribute (MappedDecl->getAttrs (), atLoc,
8438
- atEndLoc, initContext,
8439
- /* isFromClangAttribute=*/ true )
8440
- .isError ();
8441
- } else {
8442
- SourceLoc staticLoc;
8443
- StaticSpellingKind staticSpelling;
8444
- hadError = parser
8445
- .parseDeclModifierList (MappedDecl->getAttrs (), staticLoc,
8446
- staticSpelling,
8447
- /* isFromClangAttribute=*/ true )
8448
- .isError ();
8449
- }
8450
-
8454
+ bool hadError =
8455
+ applyImportedAttribute (*this , MappedDecl, swiftAttr->getAttribute ());
8451
8456
if (hadError) {
8452
8457
// Complain about the unhandled attribute or modifier.
8453
8458
HeaderLoc attrLoc (swiftAttr->getLocation ());
@@ -8602,28 +8607,7 @@ void ClangImporter::Implementation::importBoundsAttributes(
8602
8607
}
8603
8608
}
8604
8609
8605
-
8606
- // Dig out a source file we can use for parsing.
8607
- auto &sourceFile = getClangSwiftAttrSourceFile (
8608
- *MappedDecl->getDeclContext ()->getParentModule (), MacroString);
8609
-
8610
- // Spin up a parser.
8611
- swift::Parser parser (sourceFile.getBufferID (), sourceFile,
8612
- &SwiftContext.Diags , nullptr , nullptr );
8613
- // Prime the lexer.
8614
- parser.consumeTokenWithoutFeedingReceiver ();
8615
-
8616
- bool hadError = false ;
8617
- assert (parser.Tok .is (tok::at_sign));
8618
- SourceLoc atEndLoc = parser.Tok .getRange ().getEnd ();
8619
- SourceLoc atLoc = parser.consumeToken (tok::at_sign);
8620
- DeclContext *DC = MappedDecl->getParent ();
8621
- auto initContext = PatternBindingInitializer::create (DC);
8622
- hadError = parser
8623
- .parseDeclAttribute (MappedDecl->getAttrs (), atLoc, atEndLoc,
8624
- initContext,
8625
- /* isFromClangAttribute=*/ true )
8626
- .isError ();
8610
+ bool hadError = applyImportedAttribute (*this , MappedDecl, MacroString);
8627
8611
if (hadError) {
8628
8612
HeaderLoc attrLoc (ClangDecl->getLocation ());
8629
8613
diagnose (attrLoc, diag::clang_pointer_bounds_unhandled,
0 commit comments