Skip to content

Commit

Permalink
[MachO] Move type size asserts to source files. NFC
Browse files Browse the repository at this point in the history
As discussed in https://reviews.llvm.org/D113809#3128636. It's a bit
unfortunate to move the asserts away from the structs whose sizes
they're checking, but it's a far better developer experience when one of
the asserts is violated, because you get a single error instead of every
single source file including the header erroring out.
  • Loading branch information
smeenai committed Nov 17, 2021
1 parent bbccf49 commit 01510ac
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
5 changes: 5 additions & 0 deletions lld/MachO/InputSection.cpp
Expand Up @@ -26,6 +26,11 @@ using namespace llvm::support;
using namespace lld;
using namespace lld::macho;

// Verify ConcatInputSection's size on 64-bit builds.
static_assert(sizeof(void *) != 8 || sizeof(ConcatInputSection) == 120,
"Try to minimize ConcatInputSection's size, we create many "
"instances of it");

std::vector<ConcatInputSection *> macho::inputSections;

uint64_t InputSection::getFileSize() const {
Expand Down
5 changes: 0 additions & 5 deletions lld/MachO/InputSection.h
Expand Up @@ -149,11 +149,6 @@ class ConcatInputSection final : public InputSection {
uint64_t outSecOff = 0;
};

// Verify ConcatInputSection's size on 64-bit builds.
static_assert(sizeof(void *) != 8 || sizeof(ConcatInputSection) == 120,
"Try to minimize ConcatInputSection's size, we create many "
"instances of it");

// Helper functions to make it easy to sprinkle asserts.

inline bool shouldOmitFromOutput(InputSection *isec) {
Expand Down
3 changes: 3 additions & 0 deletions lld/MachO/Relocations.cpp
Expand Up @@ -17,6 +17,9 @@ using namespace llvm;
using namespace lld;
using namespace lld::macho;

static_assert(sizeof(void *) != 8 || sizeof(Reloc) == 24,
"Try to minimize Reloc's size; we create many instances");
bool macho::validateSymbolRelocation(const Symbol *sym,
const InputSection *isec, const Reloc &r) {
const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
Expand Down
3 changes: 0 additions & 3 deletions lld/MachO/Relocations.h
Expand Up @@ -63,9 +63,6 @@ struct Reloc {
llvm::PointerUnion<Symbol *, InputSection *> referent = nullptr;
};

static_assert(sizeof(void *) != 8 || sizeof(Reloc) == 24,
"Try to minimize Reloc's size; we create many instances");

bool validateSymbolRelocation(const Symbol *, const InputSection *,
const Reloc &);

Expand Down
13 changes: 13 additions & 0 deletions lld/MachO/Symbols.cpp
Expand Up @@ -14,6 +14,19 @@ using namespace llvm;
using namespace lld;
using namespace lld::macho;

static_assert(sizeof(void *) != 8 || sizeof(Symbol) == 48,
"Try to minimize Symbol's size; we create many instances");
// The Microsoft ABI doesn't support using parent class tail padding for child
// members, hence the _MSC_VER check.
#if !defined(_MSC_VER)
static_assert(sizeof(void *) != 8 || sizeof(Defined) == 80,
"Try to minimize Defined's size; we create many instances");
#endif
static_assert(sizeof(SymbolUnion) == sizeof(Defined),
"Defined should be the largest Symbol kind");
// Returns a symbol for an error message.
static std::string demangle(StringRef symName) {
if (config->demangle)
Expand Down
13 changes: 0 additions & 13 deletions lld/MachO/Symbols.h
Expand Up @@ -109,9 +109,6 @@ class Symbol {
bool used : 1;
};

static_assert(sizeof(void *) != 8 || sizeof(Symbol) == 48,
"Try to minimize Symbol's size; we create many instances");
class Defined : public Symbol {
public:
Defined(StringRefZ name, InputFile *file, InputSection *isec, uint64_t value,
Expand Down Expand Up @@ -174,13 +171,6 @@ class Defined : public Symbol {
ConcatInputSection *compactUnwind = nullptr;
};

// The Microsoft ABI doesn't support using parent class tail padding for child
// members, hence the _MSC_VER check.
#if !defined(_MSC_VER)
static_assert(sizeof(void *) != 8 || sizeof(Defined) == 80,
"Try to minimize Defined's size; we create many instances");
#endif
// This enum does double-duty: as a symbol property, it indicates whether & how
// a dylib symbol is referenced. As a DylibFile property, it indicates the kind
// of referenced symbols contained within the file. If there are both weak
Expand Down Expand Up @@ -307,9 +297,6 @@ union SymbolUnion {
alignas(LazySymbol) char e[sizeof(LazySymbol)];
};

static_assert(sizeof(SymbolUnion) == sizeof(Defined),
"Defined should be the largest Symbol kind");
template <typename T, typename... ArgT>
T *replaceSymbol(Symbol *s, ArgT &&...arg) {
static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
Expand Down

0 comments on commit 01510ac

Please sign in to comment.