Skip to content

Commit db81593

Browse files
authored
Merge pull request #65559 from DougGregor/macro-attached-to-accessors-mangle
2 parents 441b151 + 3547855 commit db81593

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4062,7 +4062,42 @@ std::string ASTMangler::mangleAttachedMacroExpansion(
40624062
// dependencies.
40634063
const Decl *attachedTo = decl;
40644064
DeclBaseName attachedToName;
4065-
if (auto valueDecl = dyn_cast<ValueDecl>(decl)) {
4065+
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
4066+
auto storage = accessor->getStorage();
4067+
appendContextOf(storage);
4068+
4069+
// Introduce an identifier mangling that includes var/subscript, accessor
4070+
// kind, and static.
4071+
// FIXME: THIS IS A HACK. We need something different.
4072+
{
4073+
llvm::SmallString<16> name;
4074+
{
4075+
llvm::raw_svector_ostream out(name);
4076+
out << storage->getName().getBaseName().userFacingName()
4077+
<< "__";
4078+
if (isa<VarDecl>(storage)) {
4079+
out << "v";
4080+
} else {
4081+
assert(isa<SubscriptDecl>(storage));
4082+
out << "i";
4083+
}
4084+
4085+
out << getCodeForAccessorKind(accessor->getAccessorKind());
4086+
if (storage->isStatic())
4087+
out << "Z";
4088+
}
4089+
4090+
attachedToName = decl->getASTContext().getIdentifier(name);
4091+
}
4092+
4093+
appendDeclName(storage, attachedToName);
4094+
4095+
// For member attribute macros, the attribute is attached to the enclosing
4096+
// declaration.
4097+
if (role == MacroRole::MemberAttribute) {
4098+
attachedTo = storage->getDeclContext()->getAsDecl();
4099+
}
4100+
} else if (auto valueDecl = dyn_cast<ValueDecl>(decl)) {
40664101
appendContextOf(valueDecl);
40674102

40684103
// Mangle the name, replacing special names with their user-facing names.

lib/AST/Decl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10552,6 +10552,9 @@ DeclContext *
1055210552
MacroDiscriminatorContext::getInnermostMacroContext(DeclContext *dc) {
1055310553
switch (dc->getContextKind()) {
1055410554
case DeclContextKind::SubscriptDecl:
10555+
// For a subscript, return its parent context.
10556+
return getInnermostMacroContext(dc->getParent());
10557+
1055510558
case DeclContextKind::EnumElementDecl:
1055610559
case DeclContextKind::AbstractFunctionDecl:
1055710560
case DeclContextKind::SerializedLocal:

lib/Sema/TypeCheckStorage.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,15 +3319,6 @@ static void finishNSManagedImplInfo(VarDecl *var,
33193319
}
33203320
}
33213321

3322-
static Expr *getParentExecutableInitializer(VarDecl *var) {
3323-
if (auto *PBD = var->getParentPatternBinding()) {
3324-
const auto i = PBD->getPatternEntryIndexForVarDecl(var);
3325-
return PBD->getExecutableInit(i);
3326-
}
3327-
3328-
return nullptr;
3329-
}
3330-
33313322
static void finishStorageImplInfo(AbstractStorageDecl *storage,
33323323
StorageImplInfo &info) {
33333324
auto dc = storage->getDeclContext();

test/Macros/macro_expand_attributes.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,15 @@ struct OldStorage {
111111
// The deprecation warning below comes from the deprecation attribute
112112
// introduced by @wrapStoredProperties on OldStorage.
113113
_ = OldStorage(x: 5).x // expected-warning{{'x' is deprecated: hands off my data}}
114+
115+
@wrapStoredProperties(#"available(*, deprecated, message: "hands off my data")"#)
116+
class C2: P {
117+
var x: Int = 0
118+
var y: Int = 0
119+
120+
var squareOfLength: Int {
121+
return x*x + y*y // expected-warning 4{{hands off my data}}
122+
}
123+
124+
var blah: Int { squareOfLength }
125+
}

0 commit comments

Comments
 (0)