Skip to content

[lld] Migrate away from PointerUnion::dyn_cast (NFC) #123691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lld/MachO/Arch/ARM64Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ void macho::reportUnalignedLdrStr(void *loc, const lld::macho::Reloc &r,
uint64_t off = reinterpret_cast<const uint8_t *>(loc) - in.bufferStart;
const InputSection *isec = offsetToInputSection(&off);
std::string locStr = isec ? isec->getLocation(off) : "(invalid location)";
::reportUnalignedLdrStr(locStr, va, align, r.referent.dyn_cast<Symbol *>());
::reportUnalignedLdrStr(locStr, va, align,
llvm::dyn_cast_if_present<Symbol *>(r.referent));
}

void macho::reportUnalignedLdrStr(void *loc, lld::macho::SymbolDiagnostic d,
Expand Down
2 changes: 1 addition & 1 deletion lld/MachO/BPSectionOrderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class BPSectionMacho : public BPSectionBase {
if (isec)
kind = uint64_t(isec->kind());

if (auto *sym = reloc.referent.dyn_cast<Symbol *>()) {
if (auto *sym = llvm::dyn_cast_if_present<Symbol *>(reloc.referent)) {
kind = (kind << 8) | uint8_t(sym->kind());
if (auto *d = llvm::dyn_cast<Defined>(sym))
value = d->value;
Expand Down
4 changes: 2 additions & 2 deletions lld/MachO/ICF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void ICF::run() {
parallelForEach(icfInputs, [&](ConcatInputSection *isec) {
uint32_t hash = isec->icfEqClass[icfPass % 2];
for (const Reloc &r : isec->relocs) {
if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
if (auto *sym = dyn_cast_if_present<Symbol *>(r.referent)) {
if (auto *defined = dyn_cast<Defined>(sym)) {
if (defined->isec()) {
if (auto *referentIsec =
Expand Down Expand Up @@ -473,7 +473,7 @@ void macho::markAddrSigSymbols() {
const InputSection *isec = addrSigSection->subsections[0].isec;

for (const Reloc &r : isec->relocs) {
if (auto *sym = r.referent.dyn_cast<Symbol *>())
if (auto *sym = dyn_cast_if_present<Symbol *>(r.referent))
markSymAsAddrSig(sym);
else
error(toString(isec) + ": unexpected section relocation");
Expand Down
11 changes: 6 additions & 5 deletions lld/MachO/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,8 @@ void ObjFile::registerCompactUnwind(Section &compactUnwindSection) {
continue;
}
uint64_t add = r.addend;
if (auto *sym = cast_or_null<Defined>(r.referent.dyn_cast<Symbol *>())) {
if (auto *sym = cast_or_null<Defined>(
dyn_cast_if_present<Symbol *>(r.referent))) {
// Check whether the symbol defined in this file is the prevailing one.
// Skip if it is e.g. a weak def that didn't prevail.
if (sym->getFile() != this) {
Expand All @@ -1172,8 +1173,8 @@ void ObjFile::registerCompactUnwind(Section &compactUnwindSection) {
add += sym->value;
referentIsec = cast<ConcatInputSection>(sym->isec());
} else {
referentIsec =
cast<ConcatInputSection>(r.referent.dyn_cast<InputSection *>());
referentIsec = cast<ConcatInputSection>(
dyn_cast_if_present<InputSection *>(r.referent));
}
// Unwind info lives in __DATA, and finalization of __TEXT will occur
// before finalization of __DATA. Moreover, the finalization of unwind
Expand Down Expand Up @@ -1339,8 +1340,8 @@ targetSymFromCanonicalSubtractor(const InputSection *isec,
// Note: pcSym may *not* be exactly at the PC; there's usually a non-zero
// addend.
auto *pcSym = cast<Defined>(cast<macho::Symbol *>(subtrahend.referent));
Defined *target =
cast_or_null<Defined>(minuend.referent.dyn_cast<macho::Symbol *>());
Defined *target = cast_or_null<Defined>(
dyn_cast_if_present<macho::Symbol *>(minuend.referent));
if (!pcSym) {
auto *targetIsec =
cast<ConcatInputSection>(cast<InputSection *>(minuend.referent));
Expand Down
7 changes: 4 additions & 3 deletions lld/MachO/InputSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@ void ConcatInputSection::writeTo(uint8_t *buf) {
const Symbol *fromSym = cast<Symbol *>(r.referent);
const Reloc &minuend = relocs[++i];
uint64_t minuendVA;
if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>())
if (const Symbol *toSym = dyn_cast_if_present<Symbol *>(minuend.referent))
minuendVA = toSym->getVA() + minuend.addend;
else {
auto *referentIsec = cast<InputSection *>(minuend.referent);
assert(!::shouldOmitFromOutput(referentIsec));
minuendVA = referentIsec->getVA(minuend.addend);
}
referentVA = minuendVA - fromSym->getVA();
} else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
} else if (auto *referentSym = dyn_cast_if_present<Symbol *>(r.referent)) {
if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
!referentSym->isInGot())
target->relaxGotLoad(loc, r.type);
Expand All @@ -259,7 +259,8 @@ void ConcatInputSection::writeTo(uint8_t *buf) {
writeChainedFixup(loc, referentSym, r.addend);
continue;
}
} else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
} else if (auto *referentIsec =
dyn_cast_if_present<InputSection *>(r.referent)) {
assert(!::shouldOmitFromOutput(referentIsec));
referentVA = referentIsec->getVA(r.addend);

Expand Down
4 changes: 2 additions & 2 deletions lld/MachO/MarkLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void MarkLiveImpl<RecordWhyLive>::markTransitively() {

// Mark all symbols listed in the relocation table for this section.
for (const Reloc &r : isec->relocs) {
if (auto *s = r.referent.dyn_cast<Symbol *>())
if (auto *s = dyn_cast_if_present<Symbol *>(r.referent))
addSym(s, entry);
else
enqueue(cast<InputSection *>(r.referent), r.addend, entry);
Expand All @@ -175,7 +175,7 @@ void MarkLiveImpl<RecordWhyLive>::markTransitively() {
continue;

for (const Reloc &r : isec->relocs) {
if (auto *s = r.referent.dyn_cast<Symbol *>()) {
if (auto *s = dyn_cast_if_present<Symbol *>(r.referent)) {
if (s->isLive()) {
InputSection *referentIsec = nullptr;
if (auto *d = dyn_cast<Defined>(s))
Expand Down
8 changes: 4 additions & 4 deletions lld/MachO/ObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ ObjcCategoryMerger::tryGetSymbolAtIsecOffset(const ConcatInputSection *isec,
if (!reloc)
return nullptr;

Symbol *sym = reloc->referent.dyn_cast<Symbol *>();
Symbol *sym = dyn_cast_if_present<Symbol *>(reloc->referent);

if (reloc->addend && sym) {
assert(isa<Defined>(sym) && "Expected defined for non-zero addend");
Expand Down Expand Up @@ -769,8 +769,8 @@ bool ObjcCategoryMerger::parsePointerListInfo(const ConcatInputSection *isec,
const Reloc *reloc = ptrListSym->isec()->getRelocAt(off);
assert(reloc && "No reloc found at pointer list offset");

auto *listSym =
dyn_cast_or_null<Defined>(reloc->referent.dyn_cast<Symbol *>());
auto *listSym = dyn_cast_or_null<Defined>(
dyn_cast_if_present<Symbol *>(reloc->referent));
// Sometimes, the reloc points to a StringPiece (InputSection + addend)
// instead of a symbol.
// TODO: Skip these cases for now, but we should fix this.
Expand Down Expand Up @@ -1158,7 +1158,7 @@ DenseSet<const Symbol *> ObjcCategoryMerger::collectNlCategories() {
continue;

for (auto &r : sec->relocs) {
const Symbol *sym = r.referent.dyn_cast<Symbol *>();
const Symbol *sym = dyn_cast_if_present<Symbol *>(r.referent);
nlCategories.insert(sym);
}
}
Expand Down
6 changes: 3 additions & 3 deletions lld/MachO/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static_assert(sizeof(void *) != 8 || sizeof(Reloc) == 24,
"Try to minimize Reloc's size; we create many instances");

InputSection *Reloc::getReferentInputSection() const {
if (const auto *sym = referent.dyn_cast<Symbol *>()) {
if (const auto *sym = dyn_cast_if_present<Symbol *>(referent)) {
if (const auto *d = dyn_cast<Defined>(sym))
return d->isec();
return nullptr;
Expand All @@ -32,7 +32,7 @@ InputSection *Reloc::getReferentInputSection() const {
}

StringRef Reloc::getReferentString() const {
if (auto *isec = referent.dyn_cast<InputSection *>()) {
if (auto *isec = dyn_cast_if_present<InputSection *>(referent)) {
const auto *cisec = dyn_cast<CStringInputSection>(isec);
assert(cisec && "referent must be a CStringInputSection");
return cisec->getStringRefAtOffset(addend);
Expand Down Expand Up @@ -123,7 +123,7 @@ void macho::reportRangeError(void *loc, const Reloc &r, const Twine &v,
uint64_t off = reinterpret_cast<const uint8_t *>(loc) - in.bufferStart;
const InputSection *isec = offsetToInputSection(&off);
std::string locStr = isec ? isec->getLocation(off) : "(invalid location)";
if (auto *sym = r.referent.dyn_cast<Symbol *>())
if (auto *sym = dyn_cast_if_present<Symbol *>(r.referent))
hint = "; references " + toString(*sym);
error(locStr + ": relocation " + target->getRelocAttrs(r.type).name +
" is out of range: " + v + " is not in [" + Twine(min) + ", " +
Expand Down
6 changes: 3 additions & 3 deletions lld/MachO/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ void ObjCSelRefsHelper::initialize() {
// might be aggregated.
assert(isec->relocs.size() == 1);
auto Reloc = isec->relocs[0];
if (const auto *sym = Reloc.referent.dyn_cast<Symbol *>()) {
if (const auto *sym = dyn_cast_if_present<Symbol *>(Reloc.referent)) {
if (const auto *d = dyn_cast<Defined>(sym)) {
auto *cisec = cast<CStringInputSection>(d->isec());
auto methname = cisec->getStringRefAtOffset(d->value);
Expand Down Expand Up @@ -1958,7 +1958,7 @@ void InitOffsetsSection::writeTo(uint8_t *buf) const {
// FIXME: Add function specified by -init when that argument is implemented.
for (ConcatInputSection *isec : sections) {
for (const Reloc &rel : isec->relocs) {
const Symbol *referent = rel.referent.dyn_cast<Symbol *>();
const Symbol *referent = dyn_cast_if_present<Symbol *>(rel.referent);
assert(referent && "section relocation should have been rejected");
uint64_t offset = referent->getVA() - in.header->addr;
// FIXME: Can we handle this gracefully?
Expand Down Expand Up @@ -1994,7 +1994,7 @@ void InitOffsetsSection::setUp() {
error(isec->getLocation(rel.offset) +
": unexpected section relocation");

Symbol *sym = rel.referent.dyn_cast<Symbol *>();
Symbol *sym = dyn_cast_if_present<Symbol *>(rel.referent);
if (auto *undefined = dyn_cast<Undefined>(sym))
treatUndefinedSymbol(*undefined, isec, rel.offset);
if (needsBinding(sym))
Expand Down
6 changes: 3 additions & 3 deletions lld/MachO/UnwindInfoSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void UnwindInfoSectionImpl::prepareRelocations(ConcatInputSection *isec) {
// Since compact unwind sections aren't part of the inputSections vector,
// they don't get canonicalized by scanRelocations(), so we have to do the
// canonicalization here.
if (auto *referentIsec = r.referent.dyn_cast<InputSection *>())
if (auto *referentIsec = dyn_cast_if_present<InputSection *>(r.referent))
r.referent = referentIsec->canonical();

// Functions and LSDA entries always reside in the same object file as the
Expand All @@ -249,7 +249,7 @@ void UnwindInfoSectionImpl::prepareRelocations(ConcatInputSection *isec) {
if (r.offset != cuLayout.personalityOffset)
continue;

if (auto *s = r.referent.dyn_cast<Symbol *>()) {
if (auto *s = dyn_cast_if_present<Symbol *>(r.referent)) {
// Personality functions are nearly always system-defined (e.g.,
// ___gxx_personality_v0 for C++) and relocated as dylib symbols. When an
// application provides its own personality function, it might be
Expand Down Expand Up @@ -294,7 +294,7 @@ void UnwindInfoSectionImpl::prepareRelocations(ConcatInputSection *isec) {
continue;
}

if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
if (auto *referentIsec = dyn_cast_if_present<InputSection *>(r.referent)) {
assert(!isCoalescedWeak(referentIsec));
// Personality functions can be referenced via section relocations
// if they live in the same object file. Create placeholder synthetic
Expand Down
7 changes: 4 additions & 3 deletions lld/MachO/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ void Writer::scanRelocations() {

// Canonicalize the referent so that later accesses in Writer won't
// have to worry about it.
if (auto *referentIsec = r.referent.dyn_cast<InputSection *>())
if (auto *referentIsec = dyn_cast_if_present<InputSection *>(r.referent))
r.referent = referentIsec->canonical();

if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
Expand All @@ -721,11 +721,12 @@ void Writer::scanRelocations() {
++it;
// Canonicalize the referent so that later accesses in Writer won't
// have to worry about it.
if (auto *referentIsec = it->referent.dyn_cast<InputSection *>())
if (auto *referentIsec =
dyn_cast_if_present<InputSection *>(it->referent))
it->referent = referentIsec->canonical();
continue;
}
if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
if (auto *sym = dyn_cast_if_present<Symbol *>(r.referent)) {
if (auto *undefined = dyn_cast<Undefined>(sym))
treatUndefinedSymbol(*undefined, isec, r.offset);
// treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check.
Expand Down
Loading