Skip to content

Commit 85f2a1a

Browse files
committed
[Macros] Factor calls to ResolveMacroRequest into Decl::getResolvedMacro.
This prevents call-sites from making the mistake of passing an incorrect DeclContext to the request, which can cause re-typechecking issues.
1 parent f1953f3 commit 85f2a1a

File tree

9 files changed

+21
-48
lines changed

9 files changed

+21
-48
lines changed

Diff for: include/swift/AST/Attr.h

-4
Original file line numberDiff line numberDiff line change
@@ -1702,10 +1702,6 @@ class CustomAttr final : public DeclAttribute {
17021702
bool isArgUnsafe() const;
17031703
void setArgIsUnsafe(bool unsafe) { isArgUnsafeBit = unsafe; }
17041704

1705-
/// Whether this custom attribute is a macro attached to the given
1706-
/// declaration.
1707-
bool isAttachedMacro(const Decl *decl) const;
1708-
17091705
Expr *getSemanticInit() const { return semanticInit; }
17101706
void setSemanticInit(Expr *expr) { semanticInit = expr; }
17111707

Diff for: include/swift/AST/Decl.h

+4
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
883883
/// declaration.
884884
void forEachAttachedMacro(MacroRole role, MacroCallback) const;
885885

886+
/// Returns the resolved macro for the given custom attribute
887+
/// attached to this declaration.
888+
MacroDecl *getResolvedMacro(CustomAttr *attr) const;
889+
886890
/// Retrieve the discriminator for the given custom attribute that names
887891
/// an attached macro.
888892
unsigned getAttachedMacroDiscriminator(

Diff for: lib/AST/ASTMangler.cpp

+4-13
Original file line numberDiff line numberDiff line change
@@ -3801,12 +3801,7 @@ void ASTMangler::appendMacroExpansionContext(
38013801
outerExpansionDC = decl->getDeclContext();
38023802
discriminator = decl->getAttachedMacroDiscriminator(role, attr);
38033803

3804-
auto *macroDecl = evaluateOrDefault(
3805-
ctx.evaluator,
3806-
ResolveMacroRequest{const_cast<CustomAttr *>(attr),
3807-
outerExpansionDC},
3808-
nullptr);
3809-
if (macroDecl)
3804+
if (auto *macroDecl = decl->getResolvedMacro(attr))
38103805
baseName = macroDecl->getBaseName();
38113806
else
38123807
baseName = ctx.getIdentifier("__unknown_macro__");
@@ -3885,22 +3880,18 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
38853880
const Decl *decl, CustomAttr *attr, MacroRole role) {
38863881
beginMangling();
38873882

3888-
DeclContext *macroDeclContext = decl->getDeclContext();
3883+
const Decl *attachedTo = decl;
38893884
if (role == MacroRole::MemberAttribute) {
38903885
appendContextOf(cast<ValueDecl>(decl));
3891-
macroDeclContext = decl->getDeclContext()->getParent();
3886+
attachedTo = decl->getDeclContext()->getAsDecl();
38923887
} else if (auto valueDecl = dyn_cast<ValueDecl>(decl)) {
38933888
appendAnyDecl(valueDecl);
38943889
} else {
38953890
appendContext(decl->getDeclContext(), "");
38963891
}
38973892

38983893
StringRef macroName;
3899-
auto *macroDecl = evaluateOrDefault(
3900-
decl->getASTContext().evaluator,
3901-
ResolveMacroRequest{attr, macroDeclContext},
3902-
nullptr);
3903-
if (macroDecl)
3894+
if (auto *macroDecl = attachedTo->getResolvedMacro(attr))
39043895
macroName = macroDecl->getName().getBaseName().userFacingName();
39053896
else
39063897
macroName = "__unknown_macro__";

Diff for: lib/AST/Attr.cpp

-12
Original file line numberDiff line numberDiff line change
@@ -2376,18 +2376,6 @@ bool CustomAttr::isArgUnsafe() const {
23762376
return isArgUnsafeBit;
23772377
}
23782378

2379-
bool CustomAttr::isAttachedMacro(const Decl *decl) const {
2380-
auto &ctx = decl->getASTContext();
2381-
auto *dc = decl->getDeclContext();
2382-
2383-
auto *macroDecl = evaluateOrDefault(
2384-
ctx.evaluator,
2385-
ResolveMacroRequest{const_cast<CustomAttr *>(this), dc},
2386-
nullptr);
2387-
2388-
return macroDecl != nullptr;
2389-
}
2390-
23912379
MacroRoleAttr::MacroRoleAttr(SourceLoc atLoc, SourceRange range,
23922380
MacroSyntax syntax, SourceLoc lParenLoc,
23932381
MacroRole role,

Diff for: lib/AST/Decl.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -399,15 +399,9 @@ void Decl::visitAuxiliaryDecls(AuxiliaryDeclCallback callback) const {
399399

400400
void Decl::forEachAttachedMacro(MacroRole role,
401401
MacroCallback macroCallback) const {
402-
auto *dc = getDeclContext();
403-
auto &ctx = dc->getASTContext();
404-
405402
for (auto customAttrConst : getSemanticAttrs().getAttributes<CustomAttr>()) {
406403
auto customAttr = const_cast<CustomAttr *>(customAttrConst);
407-
auto *macroDecl = evaluateOrDefault(
408-
ctx.evaluator,
409-
ResolveMacroRequest{customAttr, dc},
410-
nullptr);
404+
auto *macroDecl = getResolvedMacro(customAttr);
411405

412406
if (!macroDecl)
413407
continue;
@@ -419,6 +413,13 @@ void Decl::forEachAttachedMacro(MacroRole role,
419413
}
420414
}
421415

416+
MacroDecl *Decl::getResolvedMacro(CustomAttr *customAttr) const {
417+
return evaluateOrDefault(
418+
getASTContext().evaluator,
419+
ResolveMacroRequest{customAttr, getDeclContext()},
420+
nullptr);
421+
}
422+
422423
unsigned Decl::getAttachedMacroDiscriminator(
423424
MacroRole role, const CustomAttr *attr
424425
) const {

Diff for: lib/IDE/SourceEntityWalker.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -696,10 +696,8 @@ bool SemaAnnotator::handleCustomAttributes(Decl *D) {
696696
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr, true>()) {
697697
if (auto *Repr = customAttr->getTypeRepr()) {
698698
// If this attribute resolves to a macro, index that.
699-
ASTContext &ctx = D->getASTContext();
700-
ResolveMacroRequest req{const_cast<CustomAttr *>(customAttr),
701-
D->getDeclContext()};
702-
if (auto macroDecl = evaluateOrDefault(ctx.evaluator, req, nullptr)) {
699+
auto *mutableAttr = const_cast<CustomAttr *>(customAttr);
700+
if (auto macroDecl = D->getResolvedMacro(mutableAttr)) {
703701
Type macroRefType = macroDecl->getDeclaredInterfaceType();
704702
if (!passReference(
705703
macroDecl, macroRefType, DeclNameLoc(Repr->getStartLoc()),

Diff for: lib/Sema/TypeCheckAttr.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -3567,9 +3567,7 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
35673567

35683568
if (!nominal) {
35693569
// Try resolving an attached macro attribute.
3570-
auto *macro = evaluateOrDefault(
3571-
Ctx.evaluator, ResolveMacroRequest{attr, dc},
3572-
nullptr);
3570+
auto *macro = D->getResolvedMacro(attr);
35733571
if (macro || !attr->isValid())
35743572
return;
35753573

Diff for: lib/Sema/TypeCheckMacros.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,7 @@ static bool isFromExpansionOfMacro(SourceFile *sourceFile, MacroDecl *macro,
458458
return true;
459459
} else if (auto *macroAttr = sourceFile->getAttachedMacroAttribute()) {
460460
auto *decl = expansion.dyn_cast<Decl *>();
461-
auto &ctx = decl->getASTContext();
462-
auto *macroDecl = evaluateOrDefault(ctx.evaluator,
463-
ResolveMacroRequest{macroAttr, decl->getDeclContext()},
464-
nullptr);
461+
auto *macroDecl = decl->getResolvedMacro(macroAttr);
465462
if (!macroDecl)
466463
return false;
467464

Diff for: lib/Serialization/Serialization.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2867,7 +2867,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
28672867
auto theAttr = cast<CustomAttr>(DA);
28682868

28692869
// Macro attributes are not serialized.
2870-
if (theAttr->isAttachedMacro(D))
2870+
if (D->getResolvedMacro(const_cast<CustomAttr *>(theAttr)))
28712871
return;
28722872

28732873
auto typeID = S.addTypeRef(theAttr->getType());

0 commit comments

Comments
 (0)