Skip to content

Commit

Permalink
[lld-macho][NFC] when reasonable, replace auto keyword with type names
Browse files Browse the repository at this point in the history
lld policy discourages `auto`. Replace it with a type name whenever reasonable. Retain `auto` to avoid ...
* redundancy, as for decls such as `auto *t = mumble_cast<TYPE *>` or similar that specifies the result type on the RHS
* verbosity, as for iterators
* gratuitous suffering, as for lambdas

Along the way, add `const` when appropriate.

Note: a future diff will ...
* add more `const` qualifiers
* remove `opt::` when we are already `using llvm::opt`

Differential Revision: https://reviews.llvm.org/D98313
  • Loading branch information
gkmhub committed Mar 10, 2021
1 parent ee35784 commit fdc0c21
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
42 changes: 21 additions & 21 deletions lld/MachO/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ getSearchPaths(unsigned optionCode, opt::InputArgList &args,
if (args.hasArg(OPT_Z))
return paths;

for (auto const &path : systemPaths) {
for (auto root : roots) {
for (const StringRef &path : systemPaths) {
for (const StringRef &root : roots) {
SmallString<261> buffer(root);
path::append(buffer, path);
if (fs::is_directory(buffer))
Expand Down Expand Up @@ -252,7 +252,7 @@ static InputFile *addFile(StringRef path, bool forceLoadArchive,
MemoryBufferRef mbref = *buffer;
InputFile *newFile = nullptr;

auto magic = identify_magic(mbref.getBuffer());
file_magic magic = identify_magic(mbref.getBuffer());
switch (magic) {
case file_magic::archive: {
std::unique_ptr<object::Archive> file = CHECK(
Expand Down Expand Up @@ -368,10 +368,10 @@ void macho::parseLCLinkerOption(InputFile* f, unsigned argc, StringRef data) {
opt::InputArgList args = table.ParseArgs(argv, missingIndex, missingCount);
if (missingCount)
fatal(Twine(args.getArgString(missingIndex)) + ": missing argument");
for (auto *arg : args.filtered(OPT_UNKNOWN))
for (const Arg *arg : args.filtered(OPT_UNKNOWN))
error("unknown argument: " + arg->getAsString(args));

for (auto *arg : args) {
for (const Arg *arg : args) {
switch (arg->getOption().getID()) {
case OPT_l:
addLibrary(arg->getValue(), false);
Expand Down Expand Up @@ -493,7 +493,7 @@ static void initLLVM() {
}

static void compileBitcodeFiles() {
auto lto = make<BitcodeCompiler>();
auto *lto = make<BitcodeCompiler>();
for (InputFile *file : inputFiles)
if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file))
lto->add(*bitcodeFile);
Expand Down Expand Up @@ -663,7 +663,7 @@ static void warnIfUnimplementedOption(const opt::Option &opt) {
}

static const char *getReproduceOption(opt::InputArgList &args) {
if (auto *arg = args.getLastArg(OPT_reproduce))
if (const Arg *arg = args.getLastArg(OPT_reproduce))
return arg->getValue();
return getenv("LLD_REPRODUCE");
}
Expand Down Expand Up @@ -752,9 +752,9 @@ static void handleSymbolPatterns(opt::InputArgList &args,
SymbolPatterns &symbolPatterns,
unsigned singleOptionCode,
unsigned listFileOptionCode) {
for (opt::Arg *arg : args.filtered(singleOptionCode))
for (const Arg *arg : args.filtered(singleOptionCode))
symbolPatterns.insert(arg->getValue());
for (opt::Arg *arg : args.filtered(listFileOptionCode)) {
for (const Arg *arg : args.filtered(listFileOptionCode)) {
StringRef path = arg->getValue();
Optional<MemoryBufferRef> buffer = readFile(path);
if (!buffer) {
Expand Down Expand Up @@ -819,12 +819,12 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
/*file=*/nullptr,
/*isWeakRef=*/false);
for (auto *arg : args.filtered(OPT_u)) {
for (const Arg *arg : args.filtered(OPT_u)) {
config->explicitUndefineds.push_back(symtab->addUndefined(
arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
}

for (auto *arg : args.filtered(OPT_U))
for (const Arg *arg : args.filtered(OPT_U))
symtab->addDynamicLookup(arg->getValue());

config->outputFile = args.getLastArgValue(OPT_o, "a.out");
Expand Down Expand Up @@ -884,12 +884,12 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
error("invalid name for segment or section: " + s);
return s;
};
for (opt::Arg *arg : args.filtered(OPT_rename_section)) {
for (const Arg *arg : args.filtered(OPT_rename_section)) {
config->sectionRenameMap[{validName(arg->getValue(0)),
validName(arg->getValue(1))}] = {
validName(arg->getValue(2)), validName(arg->getValue(3))};
}
for (opt::Arg *arg : args.filtered(OPT_rename_segment)) {
for (const Arg *arg : args.filtered(OPT_rename_segment)) {
config->segmentRenameMap[validName(arg->getValue(0))] =
validName(arg->getValue(1));
}
Expand Down Expand Up @@ -926,8 +926,8 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,

// This loop should be reserved for options whose exact ordering matters.
// Other options should be handled via filtered() and/or getLastArg().
for (const auto &arg : args) {
const auto &opt = arg->getOption();
for (const Arg *arg : args) {
const Option &opt = arg->getOption();
warnIfDeprecatedOption(opt);
warnIfUnimplementedOption(opt);

Expand Down Expand Up @@ -964,7 +964,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,

// Now that all dylibs have been loaded, search for those that should be
// re-exported.
for (opt::Arg *arg : args.filtered(OPT_sub_library, OPT_sub_umbrella)) {
for (const Arg *arg : args.filtered(OPT_sub_library, OPT_sub_umbrella)) {
config->hasReexports = true;
StringRef searchName = arg->getValue();
std::vector<StringRef> extensions;
Expand All @@ -978,11 +978,11 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
}

// Parse LTO options.
if (auto *arg = args.getLastArg(OPT_mcpu))
if (const Arg *arg = args.getLastArg(OPT_mcpu))
parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
arg->getSpelling());

for (auto *arg : args.filtered(OPT_mllvm))
for (const Arg *arg : args.filtered(OPT_mllvm))
parseClangOption(arg->getValue(), arg->getSpelling());

compileBitcodeFiles();
Expand All @@ -998,7 +998,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
}
// FIXME: This prints symbols that are undefined both in input files and
// via -u flag twice.
for (const auto *undefined : config->explicitUndefineds) {
for (const Symbol *undefined : config->explicitUndefineds) {
if (isa<Undefined>(undefined)) {
error("undefined symbol: " + toString(*undefined) +
"\n>>> referenced by flag -u " + toString(*undefined));
Expand All @@ -1009,7 +1009,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
createSyntheticSections();
symtab->addDSOHandle(in.header);

for (opt::Arg *arg : args.filtered(OPT_sectcreate)) {
for (const Arg *arg : args.filtered(OPT_sectcreate)) {
StringRef segName = arg->getValue(0);
StringRef sectName = arg->getValue(1);
StringRef fileName = arg->getValue(2);
Expand All @@ -1021,7 +1021,7 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
// Initialize InputSections.
for (InputFile *file : inputFiles) {
for (SubsectionMap &map : file->subsections) {
for (auto &p : map) {
for (const auto &p : map) {
InputSection *isec = p.second;
inputSections.push_back(isec);
}
Expand Down
7 changes: 4 additions & 3 deletions lld/MachO/DriverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ MachOOptTable::MachOOptTable() : OptTable(optInfo) {}
// Set color diagnostics according to --color-diagnostics={auto,always,never}
// or --no-color-diagnostics flags.
static void handleColorDiagnostics(opt::InputArgList &args) {
auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
OPT_no_color_diagnostics);
const Arg *arg =
args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
OPT_no_color_diagnostics);
if (!arg)
return;
if (arg->getOption().getID() == OPT_color_diagnostics) {
Expand Down Expand Up @@ -121,7 +122,7 @@ std::string macho::createResponseFile(const opt::InputArgList &args) {
raw_svector_ostream os(data);

// Copy the command line to the output while rewriting paths.
for (auto *arg : args) {
for (const Arg *arg : args) {
switch (arg->getOption().getID()) {
case OPT_reproduce:
break;
Expand Down
8 changes: 4 additions & 4 deletions lld/MachO/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ int InputFile::idCount = 0;
// Open a given file path and return it as a memory-mapped file.
Optional<MemoryBufferRef> macho::readFile(StringRef path) {
// Open a file.
auto mbOrErr = MemoryBuffer::getFile(path);
if (auto ec = mbOrErr.getError()) {
ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = MemoryBuffer::getFile(path);
if (std::error_code ec = mbOrErr.getError()) {
error("cannot open " + path + ": " + ec.message());
return None;
}
Expand Down Expand Up @@ -749,7 +749,7 @@ DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
};
// TODO(compnerd) filter out symbols based on the target platform
// TODO: handle weak defs, thread locals
for (const auto symbol : interface.symbols()) {
for (const auto *symbol : interface.symbols()) {
if (!symbol->getArchitectures().has(config->target.Arch))
continue;

Expand All @@ -776,7 +776,7 @@ DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
interface.getParent() == nullptr ? &interface : interface.getParent();

for (InterfaceFileRef intfRef : interface.reexportedLibraries()) {
auto targets = intfRef.targets();
InterfaceFile::const_target_range targets = intfRef.targets();
if (is_contained(targets, config->target))
loadReexport(intfRef.getInstallName(), exportingFile, topLevel);
}
Expand Down
9 changes: 5 additions & 4 deletions lld/MachO/InputSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ void InputSection::writeTo(uint8_t *buf) {
memcpy(buf, data.data(), data.size());

for (size_t i = 0; i < relocs.size(); i++) {
auto *fromSym = target->hasAttr(relocs[i].type, RelocAttrBits::SUBTRAHEND)
? relocs[i++].referent.get<Symbol *>()
: nullptr;
const Symbol *fromSym =
target->hasAttr(relocs[i].type, RelocAttrBits::SUBTRAHEND)
? relocs[i++].referent.get<Symbol *>()
: nullptr;
const Reloc &r = relocs[i];
uint8_t *loc = buf + r.offset;
uint64_t referentVA = 0;
if (fromSym) {
auto *toSym = r.referent.get<Symbol *>();
const Symbol *toSym = r.referent.get<Symbol *>();
referentVA = toSym->getVA() - fromSym->getVA();
} else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
Expand Down
2 changes: 1 addition & 1 deletion lld/MachO/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static lto::Config createConfig() {
}

BitcodeCompiler::BitcodeCompiler() {
auto backend =
lto::ThinBackend backend =
lto::createInProcessThinBackend(heavyweight_hardware_concurrency());
ltoObj = std::make_unique<lto::LTO>(createConfig(), backend);
}
Expand Down
4 changes: 2 additions & 2 deletions lld/MachO/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Symbol *SymbolTable::addUndefined(StringRef name, InputFile *file,
bool wasInserted;
std::tie(s, wasInserted) = insert(name);

auto refState = isWeakRef ? RefState::Weak : RefState::Strong;
RefState refState = isWeakRef ? RefState::Weak : RefState::Strong;

if (wasInserted)
replaceSymbol<Undefined>(s, name, file, refState);
Expand Down Expand Up @@ -119,7 +119,7 @@ Symbol *SymbolTable::addDylib(StringRef name, DylibFile *file, bool isWeakDef,
bool wasInserted;
std::tie(s, wasInserted) = insert(name);

auto refState = RefState::Unreferenced;
RefState refState = RefState::Unreferenced;
if (!wasInserted) {
if (auto *defined = dyn_cast<Defined>(s)) {
if (isWeakDef && !defined->isWeakDef())
Expand Down
4 changes: 2 additions & 2 deletions lld/MachO/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,11 @@ void WeakBindingSection::finalizeContents() {
return a.target.getVA() < b.target.getVA();
});
for (const WeakBindingEntry &b : bindings) {
if (auto *isec = b.target.section.dyn_cast<const InputSection *>()) {
if (const auto *isec = b.target.section.dyn_cast<const InputSection *>()) {
encodeBinding(b.symbol, isec->parent, isec->outSecOff + b.target.offset,
b.addend, /*isWeakBinding=*/true, lastBinding, os);
} else {
auto *osec = b.target.section.get<const OutputSection *>();
const auto *osec = b.target.section.get<const OutputSection *>();
encodeBinding(b.symbol, osec, b.target.offset, b.addend,
/*isWeakBinding=*/true, lastBinding, os);
}
Expand Down
6 changes: 3 additions & 3 deletions lld/MachO/UnwindInfoSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void UnwindInfoSection::finalize() {

// Count frequencies of the folded encodings
EncodingMap encodingFrequencies;
for (auto cuPtrEntry : cuPtrVector)
for (const CompactUnwindEntry64 *cuPtrEntry : cuPtrVector)
encodingFrequencies[cuPtrEntry->encoding]++;

// Make a vector of encodings, sorted by descending frequency
Expand Down Expand Up @@ -316,7 +316,7 @@ void UnwindInfoSection::finalize() {
// If more entries fit in the regular format, we use that.
for (size_t i = 0; i < cuPtrVector.size();) {
secondLevelPages.emplace_back();
auto &page = secondLevelPages.back();
UnwindInfoSection::SecondLevelPage &page = secondLevelPages.back();
page.entryIndex = i;
uintptr_t functionAddressMax =
cuPtrVector[i]->functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK;
Expand All @@ -326,7 +326,7 @@ void UnwindInfoSection::finalize() {
sizeof(unwind_info_compressed_second_level_page_header) /
sizeof(uint32_t);
while (wordsRemaining >= 1 && i < cuPtrVector.size()) {
const auto *cuPtr = cuPtrVector[i];
const CompactUnwindEntry64 *cuPtr = cuPtrVector[i];
if (cuPtr->functionAddress >= functionAddressMax) {
break;
} else if (commonEncodingIndexes.count(cuPtr->encoding) ||
Expand Down

0 comments on commit fdc0c21

Please sign in to comment.