Skip to content

Commit

Permalink
project Adopt more C++23 features, like std::views
Browse files Browse the repository at this point in the history
Also avoid taking refs to small objects like `ADT::Range` and `ADT::MemoryMap`
  • Loading branch information
suhas-pai committed Jan 6, 2025
1 parent 5772203 commit 9b6322b
Show file tree
Hide file tree
Showing 54 changed files with 690 additions and 506 deletions.
2 changes: 1 addition & 1 deletion include/ADT/AddressResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace ADT {
std::variant<BindParseError, RebaseParseError, PatchParseError>;

static auto
FromLoadCommands(const ADT::MemoryMap &Map,
FromLoadCommands(const ADT::MemoryMap Map,
const MachO::Header &Header,
const MachO::DyldInfoCommand *DyldInfo,
const MachO::LinkeditDataCommand *ChainedFixups,
Expand Down
2 changes: 1 addition & 1 deletion include/ADT/DeVirtualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ADT {
void **EndOut = nullptr) const noexcept -> void * = 0;

[[nodiscard]] virtual auto
getMapForVmRange(const ADT::Range &Range,
getMapForVmRange(const ADT::Range Range,
bool IgnoreSectionBounds = false) const noexcept
-> std::optional<ADT::MemoryMap> = 0;

Expand Down
5 changes: 4 additions & 1 deletion include/ADT/Tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,10 @@ namespace ADT {
auto Node = this->Current;
this->Current = nullptr;

for (; DepthLevel != 0; DepthLevel--, Node = Node->parent()) {
for (;
this->DepthLevel != 0;
this->DepthLevel--, Node = Node->parent())
{
if (const auto PrevSibling = Node->prevSibling()) {
Current = PrevSibling->get(PrevSibling);
break;
Expand Down
79 changes: 68 additions & 11 deletions include/ADT/Trie.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ namespace ADT {
std::unique_ptr<IterateInfo> Info;
std::unique_ptr<StackInfo> NextStack;

TrieParser &Parser;
TrieParser *Parser;

void SetupInfoForNewStack() noexcept {
this->Info->stringRef().append(NextStack->node().prefix());
Expand Down Expand Up @@ -465,11 +465,11 @@ namespace ADT {

// Prepare the next-stack before returning.
const auto Error =
Parser.ParseNextNode(this->Begin,
Ptr,
this->End,
this->Info->rangeListRef(),
&this->NextStack->node());
Parser->ParseNextNode(this->Begin,
Ptr,
this->End,
this->Info->rangeListRef(),
&this->NextStack->node());

UpdateOffset();
if (Error != Error::None) {
Expand All @@ -496,6 +496,11 @@ namespace ADT {
return Error::None;
}
public:
using difference_type = std::ptrdiff_t;
using value_type = IterateInfo;

explicit Iterator() noexcept = default;

explicit
Iterator(uint8_t *const Begin,
uint8_t *const End,
Expand All @@ -504,7 +509,7 @@ namespace ADT {
const ParseOptions &Options = ParseOptions()) noexcept
: Begin(Begin), End(End),
Info(std::make_unique<IterateInfo>(ExportInfoParser)),
Parser(Parser)
Parser(&Parser)
{
Info->setMaxDepth(Options.MaxDepth);

Expand Down Expand Up @@ -594,6 +599,34 @@ namespace ADT {
return this->Info.get();
}

[[nodiscard]] constexpr
auto operator<=>(const Iterator &Other) const noexcept = default;

[[nodiscard]]
inline auto operator==(const Iterator &Other) const noexcept {
if (this->Begin != Other.Begin && this->End != Other.End) {
return false;
}

if (this->Info.StackList.empty()) {
if (Other.Info.StackList.empty()) {
return true;
} else {
return false;
}
} else if (Other.Info.StackList.empty()) {
return false;
}

const auto &Stack = this->Info.StackList.back();
const auto &OtherStack = Other.Info.StackList.back();

const auto &Node = Stack.node();
const auto &OtherNode = OtherStack.node();

return Node.offset() == OtherNode.offset();
}

[[nodiscard]]
inline auto operator==(const IteratorEnd &) const noexcept {
return this->isAtEnd();
Expand Down Expand Up @@ -629,6 +662,11 @@ namespace ADT {
return *this;
}
public:
using difference_type = std::ptrdiff_t;
using value_type = IterateInfo;

ExportIterator() noexcept = default;

explicit
ExportIterator(
uint8_t *Begin,
Expand All @@ -647,7 +685,7 @@ namespace ADT {

explicit
ExportIterator(
const ADT::MemoryMap &Map,
const ADT::MemoryMap Map,
TrieParser &TrieParser,
T &ExportInfoParser,
const ParseOptions &Options = ParseOptions()) noexcept
Expand All @@ -660,6 +698,10 @@ namespace ADT {
}
}

inline ExportIterator(const ExportIterator &Other) noexcept {
this->Iter = Other.Iter;
}

[[nodiscard]] inline auto &info() noexcept {
return this->Iter.info();
}
Expand Down Expand Up @@ -703,7 +745,7 @@ namespace ADT {
return this->Iter.info();
}

[[nodiscard]] inline const auto &operator*() const noexcept {
[[nodiscard]] inline auto &operator*() const noexcept {
return this->Iter.info();
}

Expand All @@ -715,6 +757,21 @@ namespace ADT {
return &this->Iter.info();
}

inline auto operator=(const ExportIterator &Other) noexcept
-> decltype(*this)
{
this->Iter = Other.Iter;
return *this;
}

[[nodiscard]] constexpr auto
operator<=>(const ExportIterator &Other) const noexcept = default;

[[nodiscard]]
inline auto operator==(const Iterator &Other) const noexcept {
return this->Iter == Other.Iter;
}

[[nodiscard]]
inline auto operator==(const IteratorEnd &End) const noexcept {
return this->Iter== End;
Expand All @@ -741,7 +798,7 @@ namespace ADT {
ExportInfoParser(ExportInfoParser) {};

constexpr explicit
Trie(const ADT::MemoryMap &Map,
Trie(const ADT::MemoryMap Map,
TrieParser &TrieParser,
T &ExportInfoParser) noexcept
: Begin(Map.base<uint8_t>()),
Expand Down Expand Up @@ -803,7 +860,7 @@ namespace ADT {
ExportInfoParser(ExportInfoParser) {}

explicit
ExportMap(const ADT::MemoryMap &Map,
ExportMap(const ADT::MemoryMap Map,
TrieParser &Parser,
T &ExportInfoParser) noexcept
: Begin(Map.base<uint8_t>()),
Expand Down
1 change: 1 addition & 0 deletions include/Dyld3/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <optional>
#include <string_view>

#include "Utils/Assert.h"

namespace Dyld3 {
Expand Down
2 changes: 1 addition & 1 deletion include/DyldSharedCache/DeVirtualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace DyldSharedCache {
void **EndOut = nullptr) const noexcept override;

[[nodiscard]] std::optional<ADT::MemoryMap>
getMapForVmRange(const ADT::Range &Range,
getMapForVmRange(const ADT::Range Range,
bool IgnoreProtBounds = false) const noexcept override;

[[nodiscard]] constexpr auto map() const noexcept {
Expand Down
12 changes: 8 additions & 4 deletions include/DyldSharedCache/Headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,6 @@ namespace DyldSharedCache {
[[nodiscard]] constexpr auto hasSubCacheArray() const noexcept;
};

struct SlideInfoBase {
uint32_t Version;
};

enum class SlideInfoVersion {
V1 = 1,
V2,
Expand All @@ -495,6 +491,14 @@ namespace DyldSharedCache {
V5
};

struct SlideInfoBase {
uint32_t Version;

[[nodiscard]] constexpr auto version() const noexcept {
return SlideInfoVersion(this->Version);
}
};

struct SlideInfoV1 : public SlideInfoBase {
struct Entry {
uint8_t Bits[4096 / (8 * 4)];
Expand Down
2 changes: 1 addition & 1 deletion include/DyldSharedCache/ProgramTrie.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace DyldSharedCache {
: ADT::Trie<DylibIndexInfo>(Begin, End, TrieParser, Info) {}

explicit
ProgramTrieMap(const ADT::MemoryMap &Map,
ProgramTrieMap(const ADT::MemoryMap Map,
ADT::TrieParser &TrieParser) noexcept
: ADT::Trie<DylibIndexInfo>(Map, TrieParser, Info) {}

Expand Down
1 change: 1 addition & 0 deletions include/Mach/Machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <optional>
#include <string_view>

#include "Utils/Assert.h"

namespace Mach {
Expand Down
11 changes: 5 additions & 6 deletions include/MachO/BindInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ namespace MachO {
bool Is64Bit : 1;
public:
explicit
BindOpcodeListBase(const ADT::MemoryMap &Map,
BindOpcodeListBase(const ADT::MemoryMap Map,
const bool Is64Bit) noexcept
: Begin(Map.base<const BindByte>()), End(Map.end<const BindByte>()),
Is64Bit(Is64Bit) {}
Expand Down Expand Up @@ -1179,7 +1179,7 @@ namespace MachO {
};

for (; !this->isAtEnd(); this->Iter++) {
if (Iter->hasError()) {
if (this->Iter->hasError()) {
return this->Iter->error();
}

Expand Down Expand Up @@ -1380,8 +1380,8 @@ namespace MachO {
Is64Bit(Is64Bit) {}

constexpr explicit
BindActionListBase(const ADT::MemoryMap &Map,
const ADT::Range &Range,
BindActionListBase(const ADT::MemoryMap Map,
const ADT::Range Range,
const SegmentList &SegList,
const bool Is64Bit) noexcept
: Map(Map.base<uint8_t>()), SegList(SegList),
Expand Down Expand Up @@ -1461,7 +1461,7 @@ namespace MachO {
}

[[nodiscard]] inline auto
getMapForVmRange(ADT::Range &VmRange,
getMapForVmRange(ADT::Range VmRange,
const SegmentList &SegmentList,
UnorderedMap &MapOut) const noexcept
-> BindOpcodeParseResult
Expand Down Expand Up @@ -1541,4 +1541,3 @@ namespace MachO {
using LazyBindActionList = BindActionListBase<BindInfoKind::Lazy>;
using WeakBindActionList = BindActionListBase<BindInfoKind::Weak>;
}

4 changes: 2 additions & 2 deletions include/MachO/DeVirtualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace MachO {
MachO::SegmentList SegmentList;
public:
explicit
DeVirtualizer(const ADT::MemoryMap &Map,
DeVirtualizer(const ADT::MemoryMap Map,
MachO::SegmentList &SegmentList) noexcept
: Map(Map), SegmentList(SegmentList) {}

Expand All @@ -37,7 +37,7 @@ namespace MachO {
void **EndOut = nullptr) const noexcept override;

[[nodiscard]] std::optional<ADT::MemoryMap>
getMapForVmRange(const ADT::Range &Range,
getMapForVmRange(const ADT::Range Range,
bool IgnoreProtBounds = false) const noexcept override;
};
}
2 changes: 1 addition & 1 deletion include/MachO/ExportTrie.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ namespace MachO {
: ADT::Trie<ExportTrieExportInfo>(Begin, End, TrieParser, ExportInfo) {}

explicit
ExportTrieMap(const ADT::MemoryMap &Map,
ExportTrieMap(const ADT::MemoryMap Map,
ADT::TrieParser &TrieParser) noexcept
: ADT::Trie<ExportTrieExportInfo>(Map, TrieParser, ExportInfo) {}

Expand Down
12 changes: 6 additions & 6 deletions include/MachO/LoadCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ namespace MachO {
}

[[nodiscard]]
constexpr static auto KindGetDesc(const Kind Kind) noexcept
constexpr static auto KindGetDesc(const Kind Kind) noexcept
-> std::optional<std::string_view>
{
switch (Kind) {
Expand Down Expand Up @@ -2278,7 +2278,7 @@ namespace MachO {
}

[[nodiscard]] inline auto
symbolEntryList(const ADT::MemoryMap &Map,
symbolEntryList(const ADT::MemoryMap Map,
const bool IsBigEndian) const noexcept
-> std::optional<std::span<const Entry>>
{
Expand All @@ -2287,7 +2287,7 @@ namespace MachO {
}

[[nodiscard]] inline auto
symbolEntryList(const ADT::MemoryMap &Map,
symbolEntryList(const ADT::MemoryMap Map,
const bool IsBigEndian) noexcept
-> std::optional<std::span<Entry>>
{
Expand All @@ -2296,7 +2296,7 @@ namespace MachO {
}

[[nodiscard]] inline auto
symbolEntryList64(const ADT::MemoryMap &Map,
symbolEntryList64(const ADT::MemoryMap Map,
const bool IsBigEndian) const noexcept
-> std::optional<std::span<const Entry64>>
{
Expand All @@ -2305,7 +2305,7 @@ namespace MachO {
}

[[nodiscard]] inline auto
symbolEntryList64(const ADT::MemoryMap &Map,
symbolEntryList64(const ADT::MemoryMap Map,
const bool IsBigEndian) noexcept
-> std::optional<std::span<Entry64>>
{
Expand All @@ -2314,7 +2314,7 @@ namespace MachO {
}

[[nodiscard]] inline auto
stringTable(const ADT::MemoryMap &Map, const bool IsBigEndian) noexcept
stringTable(const ADT::MemoryMap Map, const bool IsBigEndian) noexcept
-> std::optional<std::span<const char>>
{
return Map.getRange<const char>(this->strRange(IsBigEndian));
Expand Down
2 changes: 2 additions & 0 deletions include/MachO/LoadCommandsMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#pragma once
#include <ranges>

#include "ADT/MemoryMap.h"
#include "LoadCommands.h"
Expand Down Expand Up @@ -158,4 +159,5 @@ namespace MachO {
};

static_assert(std::forward_iterator<LoadCommandsMap::Iterator>);
static_assert(std::ranges::forward_range<LoadCommandsMap>);
}
Loading

0 comments on commit 9b6322b

Please sign in to comment.