Ensure that freestanding macros get consistent discriminators

Due to the duality between the expression and declaration forms of
freestanding macros, we could end up assigning two different discriminators
to what is effectively the same freestanding macro expansion. Across
different source files, this could lead to inconsistent discriminators in
different translation units. Unify the storage of the discriminator to
avoid this issue.

Fixes rdar://116259748
This commit is contained in:
Doug Gregor
2024-01-10 20:13:58 -08:00
parent ac465ea8a1
commit b31133e67d
9 changed files with 48 additions and 86 deletions

View File

@@ -11680,7 +11680,6 @@ MacroExpansionDecl::MacroExpansionDecl(DeclContext *dc,
MacroExpansionInfo *info)
: Decl(DeclKind::MacroExpansion, dc),
FreestandingMacroExpansion(FreestandingMacroKind::Decl, info) {
Bits.MacroExpansionDecl.Discriminator = InvalidDiscriminator;
}
MacroExpansionDecl *
@@ -11704,23 +11703,6 @@ MacroExpansionDecl::create(
return new (ctx) MacroExpansionDecl(dc, info);
}
unsigned MacroExpansionDecl::getDiscriminator() const {
if (getRawDiscriminator() != InvalidDiscriminator)
return getRawDiscriminator();
auto mutableThis = const_cast<MacroExpansionDecl *>(this);
auto dc = getDeclContext();
ASTContext &ctx = dc->getASTContext();
auto discriminatorContext =
MacroDiscriminatorContext::getParentOf(mutableThis);
mutableThis->setDiscriminator(
ctx.getNextMacroDiscriminator(
discriminatorContext, getMacroName().getBaseName()));
assert(getRawDiscriminator() != InvalidDiscriminator);
return getRawDiscriminator();
}
void MacroExpansionDecl::forEachExpandedNode(
llvm::function_ref<void(ASTNode)> callback
) const {