From 3988bd13988aad72ec979beb2361e8738584926b Mon Sep 17 00:00:00 2001 From: Balazs Benics Date: Fri, 27 May 2022 11:15:23 +0200 Subject: [PATCH] [llvm][clang][bolt][NFC] Use llvm::less_first() when applicable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One could reuse this functor instead of rolling out your own version. There were a couple other cases where the code was similar, but not quite the same, such as it might have an assertion in the lambda or other constructs. Thus, I've not touched any of those, as it might change the behavior in some way. As per https://discourse.llvm.org/t/submitting-simple-nfc-patches/62640/3?u=steakhal Chris Lattner > LLVM intentionally has a “yes, you can apply common sense judgement to > things” policy when it comes to code review. If you are doing mechanical > patches (e.g. adopting less_first) that apply to the entire monorepo, > then you don’t need everyone in the monorepo to sign off on it. Having > some +1 validation from someone is useful, but you don’t need everyone > whose code you touch to weigh in. Differential Revision: https://reviews.llvm.org/D126068 --- bolt/lib/Passes/LongJmp.cpp | 28 ++++++------------- clang/lib/Frontend/FrontendAction.cpp | 6 +--- clang/lib/Sema/SemaDeclCXX.cpp | 3 +- clang/lib/Sema/SemaStmtAsm.cpp | 4 +-- .../Frontend/AnalyzerHelpFlags.cpp | 5 +--- .../Introspection/IntrospectionTest.cpp | 4 +-- llvm/include/llvm/ExecutionEngine/Orc/Core.h | 6 +--- llvm/lib/IR/Attributes.cpp | 12 ++------ llvm/lib/ObjCopy/MachO/MachOWriter.cpp | 4 +-- llvm/tools/dsymutil/DebugMap.cpp | 4 +-- .../llvm-reduce/deltas/ReduceAttributes.cpp | 6 ++-- 11 files changed, 20 insertions(+), 62 deletions(-) diff --git a/bolt/lib/Passes/LongJmp.cpp b/bolt/lib/Passes/LongJmp.cpp index 24139f2b9ee1..b0cc441023eb 100644 --- a/bolt/lib/Passes/LongJmp.cpp +++ b/bolt/lib/Passes/LongJmp.cpp @@ -91,15 +91,10 @@ LongJmpPass::createNewStub(BinaryBasicBlock &SourceBB, const MCSymbol *TgtSym, // Register this in stubs maps auto registerInMap = [&](StubGroupsTy &Map) { StubGroupTy &StubGroup = Map[TgtSym]; - StubGroup.insert( - std::lower_bound( - StubGroup.begin(), StubGroup.end(), - std::make_pair(AtAddress, nullptr), - [&](const std::pair &LHS, - const std::pair &RHS) { - return LHS.first < RHS.first; - }), - std::make_pair(AtAddress, StubBB.get())); + StubGroup.insert(std::lower_bound(StubGroup.begin(), StubGroup.end(), + std::make_pair(AtAddress, nullptr), + llvm::less_first()), + std::make_pair(AtAddress, StubBB.get())); }; Stubs[&Func].insert(StubBB.get()); @@ -129,12 +124,9 @@ BinaryBasicBlock *LongJmpPass::lookupStubFromGroup( const StubGroupTy &Candidates = CandidatesIter->second; if (Candidates.empty()) return nullptr; - auto Cand = std::lower_bound( - Candidates.begin(), Candidates.end(), std::make_pair(DotAddress, nullptr), - [&](const std::pair &LHS, - const std::pair &RHS) { - return LHS.first < RHS.first; - }); + auto Cand = + std::lower_bound(Candidates.begin(), Candidates.end(), + std::make_pair(DotAddress, nullptr), llvm::less_first()); if (Cand == Candidates.end()) return nullptr; if (Cand != Candidates.begin()) { @@ -259,11 +251,7 @@ void LongJmpPass::updateStubGroups() { for (auto &KeyVal : StubGroups) { for (StubTy &Elem : KeyVal.second) Elem.first = BBAddresses[Elem.second]; - std::sort(KeyVal.second.begin(), KeyVal.second.end(), - [&](const std::pair &LHS, - const std::pair &RHS) { - return LHS.first < RHS.first; - }); + std::sort(KeyVal.second.begin(), KeyVal.second.end(), llvm::less_first()); } }; diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp index 68c70dda12b9..6b1f6364b13c 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -413,11 +413,7 @@ static std::error_code collectModuleHeaderIncludes( // Sort header paths and make the header inclusion order deterministic // across different OSs and filesystems. - llvm::sort(Headers.begin(), Headers.end(), []( - const std::pair &LHS, - const std::pair &RHS) { - return LHS.first < RHS.first; - }); + llvm::sort(Headers.begin(), Headers.end(), llvm::less_first()); for (auto &H : Headers) { // Include this header as part of the umbrella directory. Module->addTopHeader(H.second); diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index c470a46f5847..569b226da923 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -5430,8 +5430,7 @@ static void DiagnoseBaseOrMemInitializerOrder( return; // Sort based on the ideal order, first in the pair. - llvm::sort(CorrelatedInitOrder, - [](auto &LHS, auto &RHS) { return LHS.first < RHS.first; }); + llvm::sort(CorrelatedInitOrder, llvm::less_first()); // Introduce a new scope as SemaDiagnosticBuilder needs to be destroyed to // emit the diagnostic before we can try adding notes. diff --git a/clang/lib/Sema/SemaStmtAsm.cpp b/clang/lib/Sema/SemaStmtAsm.cpp index fbca36b1216a..f3c4b4fd5830 100644 --- a/clang/lib/Sema/SemaStmtAsm.cpp +++ b/clang/lib/Sema/SemaStmtAsm.cpp @@ -717,9 +717,7 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, std::make_pair(Names[i]->getName(), Exprs[i])); // Sort NamedOperandList. std::stable_sort(NamedOperandList.begin(), NamedOperandList.end(), - [](const NamedOperand &LHS, const NamedOperand &RHS) { - return LHS.first < RHS.first; - }); + llvm::less_first()); // Find adjacent duplicate operand. SmallVector::iterator Found = std::adjacent_find(begin(NamedOperandList), end(NamedOperandList), diff --git a/clang/lib/StaticAnalyzer/Frontend/AnalyzerHelpFlags.cpp b/clang/lib/StaticAnalyzer/Frontend/AnalyzerHelpFlags.cpp index eb6014a0629d..7cd15f0f6595 100644 --- a/clang/lib/StaticAnalyzer/Frontend/AnalyzerHelpFlags.cpp +++ b/clang/lib/StaticAnalyzer/Frontend/AnalyzerHelpFlags.cpp @@ -101,10 +101,7 @@ OPTIONS: #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE }; - llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS, - const OptionAndDescriptionTy &RHS) { - return LHS.first < RHS.first; - }); + llvm::sort(PrintableOptions, llvm::less_first()); for (const auto &Pair : PrintableOptions) { AnalyzerOptions::printFormattedEntry(out, Pair, /*InitialPad*/ 2, diff --git a/clang/unittests/Introspection/IntrospectionTest.cpp b/clang/unittests/Introspection/IntrospectionTest.cpp index 69e461609e7d..9f76568c8ec3 100644 --- a/clang/unittests/Introspection/IntrospectionTest.cpp +++ b/clang/unittests/Introspection/IntrospectionTest.cpp @@ -299,9 +299,7 @@ STRING_LOCATION_STDPAIR(MethodDecl, getTypeSpecStartLoc()) auto ExpectedRanges = FormatExpected(Result.RangeAccessors); - llvm::sort(ExpectedRanges, [](const auto &LHS, const auto &RHS) { - return LHS.first < RHS.first; - }); + llvm::sort(ExpectedRanges, llvm::less_first()); // clang-format off EXPECT_EQ( diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Core.h b/llvm/include/llvm/ExecutionEngine/Orc/Core.h index a51776880cde..df2826b50784 100644 --- a/llvm/include/llvm/ExecutionEngine/Orc/Core.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/Core.h @@ -339,11 +339,7 @@ public: /// Sort the lookup set by pointer value. This sort is fast but sensitive to /// allocation order and so should not be used where a consistent order is /// required. - void sortByAddress() { - llvm::sort(Symbols, [](const value_type &LHS, const value_type &RHS) { - return LHS.first < RHS.first; - }); - } + void sortByAddress() { llvm::sort(Symbols, llvm::less_first()); } /// Sort the lookup set lexicographically. This sort is slow but the order /// is unaffected by allocation order. diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp index 1aa2d44308c4..31141cfa3efb 100644 --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -1018,11 +1018,7 @@ AttributeList::get(LLVMContext &C, if (Attrs.empty()) return {}; - assert(llvm::is_sorted(Attrs, - [](const std::pair &LHS, - const std::pair &RHS) { - return LHS.first < RHS.first; - }) && + assert(llvm::is_sorted(Attrs, llvm::less_first()) && "Misordered Attributes list!"); assert(llvm::all_of(Attrs, [](const std::pair &Pair) { @@ -1055,11 +1051,7 @@ AttributeList::get(LLVMContext &C, if (Attrs.empty()) return {}; - assert(llvm::is_sorted(Attrs, - [](const std::pair &LHS, - const std::pair &RHS) { - return LHS.first < RHS.first; - }) && + assert(llvm::is_sorted(Attrs, llvm::less_first()) && "Misordered Attributes list!"); assert(llvm::none_of(Attrs, [](const std::pair &Pair) { diff --git a/llvm/lib/ObjCopy/MachO/MachOWriter.cpp b/llvm/lib/ObjCopy/MachO/MachOWriter.cpp index a94764a9a034..9b1fd58cc482 100644 --- a/llvm/lib/ObjCopy/MachO/MachOWriter.cpp +++ b/llvm/lib/ObjCopy/MachO/MachOWriter.cpp @@ -634,9 +634,7 @@ void MachOWriter::writeTail() { } } - llvm::sort(Queue, [](const WriteOperation &LHS, const WriteOperation &RHS) { - return LHS.first < RHS.first; - }); + llvm::sort(Queue, llvm::less_first()); for (auto WriteOp : Queue) (this->*WriteOp.second)(); diff --git a/llvm/tools/dsymutil/DebugMap.cpp b/llvm/tools/dsymutil/DebugMap.cpp index 605c1317b9c8..79b6cb2c80a2 100644 --- a/llvm/tools/dsymutil/DebugMap.cpp +++ b/llvm/tools/dsymutil/DebugMap.cpp @@ -62,9 +62,7 @@ void DebugMapObject::print(raw_ostream &OS) const { Entries.reserve(Symbols.getNumItems()); for (const auto &Sym : Symbols) Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue())); - llvm::sort(Entries, [](const Entry &LHS, const Entry &RHS) { - return LHS.first < RHS.first; - }); + llvm::sort(Entries, llvm::less_first()); for (const auto &Sym : Entries) { if (Sym.second.ObjectAddress) OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress)); diff --git a/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp b/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp index 3a2397464be7..e4570dd32aca 100644 --- a/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp +++ b/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp @@ -158,10 +158,8 @@ AttributeList convertAttributeRefVecToAttributeList( V.first, convertAttributeRefToAttributeSet(C, V.second)); }); - sort(SetVec, [](const std::pair &LHS, - const std::pair &RHS) { - return LHS.first < RHS.first; // All values are unique. - }); + // All values are unique. + sort(SetVec, llvm::less_first()); return AttributeList::get(C, SetVec); }