Skip to content

Commit 11a5d13

Browse files
committed
refactor shared attribute parsing
1 parent d5df5d7 commit 11a5d13

File tree

1 file changed

+38
-54
lines changed

1 file changed

+38
-54
lines changed

lib/ClangImporter/ImportDecl.cpp

+38-54
Original file line numberDiff line numberDiff line change
@@ -8279,6 +8279,41 @@ static bool importAsUnsafe(ClangImporter::Implementation &impl,
82798279
return false;
82808280
}
82818281

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+
82828317
void
82838318
ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
82848319
auto ClangDecl =
@@ -8297,7 +8332,6 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
82978332

82988333
std::optional<const clang::SwiftAttrAttr *> seenMainActorAttr;
82998334
const clang::SwiftAttrAttr *seenMutabilityAttr = nullptr;
8300-
PatternBindingInitializer *initContext = nullptr;
83018335

83028336
auto importAttrsFromDecl = [&](const clang::NamedDecl *ClangDecl) {
83038337
//
@@ -8417,37 +8451,8 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
84178451
continue;
84188452
}
84198453

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());
84518456
if (hadError) {
84528457
// Complain about the unhandled attribute or modifier.
84538458
HeaderLoc attrLoc(swiftAttr->getLocation());
@@ -8602,28 +8607,7 @@ void ClangImporter::Implementation::importBoundsAttributes(
86028607
}
86038608
}
86048609

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);
86278611
if (hadError) {
86288612
HeaderLoc attrLoc(ClangDecl->getLocation());
86298613
diagnose(attrLoc, diag::clang_pointer_bounds_unhandled,

0 commit comments

Comments
 (0)