From 2b0745531590299ad11b449e02e6abc878505b7a Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 3 Feb 2017 22:27:05 +0000 Subject: [PATCH] Simplify. NFC. Now that each OutputSectionCommand maps to just one OutputSection, we can remove a few std::vectors. llvm-svn: 294060 --- lld/ELF/LinkerScript.cpp | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 34d6be22bcf0..3a9dc9fde331 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -512,13 +512,16 @@ template void LinkerScript::process(BaseCommand &Base) { } template -static std::vector -findSections(StringRef Name, const std::vector &Sections) { +static OutputSectionBase * +findSection(StringRef Name, const std::vector &Sections) { + auto End = Sections.end(); + auto HasName = [=](OutputSectionBase *Sec) { return Sec->getName() == Name; }; + auto I = std::find_if(Sections.begin(), End, HasName); std::vector Ret; - for (OutputSectionBase *Sec : Sections) - if (Sec->getName() == Name) - Ret.push_back(Sec); - return Ret; + if (I == End) + return nullptr; + assert(std::find_if(I + 1, End, HasName) == End); + return *I; } // This function searches for a memory region to place the given output @@ -563,12 +566,10 @@ template void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) { if (Cmd->LMAExpr) LMAOffset = Cmd->LMAExpr(Dot) - Dot; - std::vector Sections = - findSections(Cmd->Name, *OutputSections); - if (Sections.empty()) + OutputSectionBase *Sec = findSection(Cmd->Name, *OutputSections); + if (!Sec) return; - OutputSectionBase *Sec = Sections[0]; // Try and find an appropriate memory region to assign offsets in. CurMemRegion = findMemoryRegion(Cmd, Sec); if (CurMemRegion) @@ -584,8 +585,6 @@ void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) { .base(); for (auto I = Cmd->Commands.begin(); I != E; ++I) process(**I); - for (OutputSectionBase *Base : Sections) - switchTo(Base); flush(); std::for_each(E, Cmd->Commands.end(), [this](std::unique_ptr &B) { process(*B.get()); }); @@ -602,7 +601,7 @@ template void LinkerScript::removeEmptyCommands() { Opt.Commands.begin(), Opt.Commands.end(), [&](const std::unique_ptr &Base) { if (auto *Cmd = dyn_cast(Base.get())) - return findSections(Cmd->Name, *OutputSections).empty(); + return !findSection(Cmd->Name, *OutputSections); return false; }); Opt.Commands.erase(Pos, Opt.Commands.end()); @@ -626,11 +625,10 @@ template void LinkerScript::adjustSectionsBeforeSorting() { auto *Cmd = dyn_cast(Base.get()); if (!Cmd) continue; - std::vector Secs = - findSections(Cmd->Name, *OutputSections); - if (!Secs.empty()) { - Flags = Secs[0]->Flags; - Type = Secs[0]->Type; + if (OutputSectionBase *Sec = + findSection(Cmd->Name, *OutputSections)) { + Flags = Sec->Flags; + Type = Sec->Type; continue; }