From: Fangrui Song Date: Wed, 24 Apr 2019 14:43:05 +0000 (+0000) Subject: Use llvm::stable_sort X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=329eb5c6baeca0a1bd6b1a5bd55e41de786f39fa;p=clang Use llvm::stable_sort git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@359098 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/DeclObjC.cpp b/lib/AST/DeclObjC.cpp index 300eac749b..bf748fbab8 100644 --- a/lib/AST/DeclObjC.cpp +++ b/lib/AST/DeclObjC.cpp @@ -1641,7 +1641,7 @@ ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() { if (!layout.empty()) { // Order synthesized ivars by their size. - std::stable_sort(layout.begin(), layout.end()); + llvm::stable_sort(layout); unsigned Ix = 0, EIx = layout.size(); if (!data().IvarList) { data().IvarList = layout[0].Ivar; Ix++; diff --git a/lib/AST/RecordLayoutBuilder.cpp b/lib/AST/RecordLayoutBuilder.cpp index 3f2fbaa04b..e83376f66e 100644 --- a/lib/AST/RecordLayoutBuilder.cpp +++ b/lib/AST/RecordLayoutBuilder.cpp @@ -3287,10 +3287,10 @@ static void DumpRecordLayout(raw_ostream &OS, const RecordDecl *RD, } // Sort nvbases by offset. - std::stable_sort(Bases.begin(), Bases.end(), - [&](const CXXRecordDecl *L, const CXXRecordDecl *R) { - return Layout.getBaseClassOffset(L) < Layout.getBaseClassOffset(R); - }); + llvm::stable_sort( + Bases, [&](const CXXRecordDecl *L, const CXXRecordDecl *R) { + return Layout.getBaseClassOffset(L) < Layout.getBaseClassOffset(R); + }); // Dump (non-virtual) bases for (const CXXRecordDecl *Base : Bases) { diff --git a/lib/AST/VTableBuilder.cpp b/lib/AST/VTableBuilder.cpp index 5c84fefe23..5c79ea37ab 100644 --- a/lib/AST/VTableBuilder.cpp +++ b/lib/AST/VTableBuilder.cpp @@ -3188,8 +3188,8 @@ void VFTableBuilder::dumpLayout(raw_ostream &Out) { const CXXMethodDecl *MD = MethodNameAndDecl.second; ThunkInfoVectorTy ThunksVector = Thunks[MD]; - std::stable_sort(ThunksVector.begin(), ThunksVector.end(), - [](const ThunkInfo &LHS, const ThunkInfo &RHS) { + llvm::stable_sort(ThunksVector, [](const ThunkInfo &LHS, + const ThunkInfo &RHS) { // Keep different thunks with the same adjustments in the order they // were put into the vector. return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return); diff --git a/lib/Analysis/CloneDetection.cpp b/lib/Analysis/CloneDetection.cpp index e62731050b..74328e8ae6 100644 --- a/lib/Analysis/CloneDetection.cpp +++ b/lib/Analysis/CloneDetection.cpp @@ -367,11 +367,7 @@ void RecursiveCloneTypeIIHashConstraint::constrain( } // Sort hash_codes in StmtsByHash. - std::stable_sort(StmtsByHash.begin(), StmtsByHash.end(), - [](std::pair LHS, - std::pair RHS) { - return LHS.first < RHS.first; - }); + llvm::stable_sort(StmtsByHash, llvm::less_first()); // Check for each StmtSequence if its successor has the same hash value. // We don't check the last StmtSequence as it has no successor. diff --git a/lib/CodeGen/CGBlocks.cpp b/lib/CodeGen/CGBlocks.cpp index 731735841e..2a317fc956 100644 --- a/lib/CodeGen/CGBlocks.cpp +++ b/lib/CodeGen/CGBlocks.cpp @@ -671,7 +671,7 @@ static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF, // Sort the layout by alignment. We have to use a stable sort here // to get reproducible results. There should probably be an // llvm::array_pod_stable_sort. - std::stable_sort(layout.begin(), layout.end()); + llvm::stable_sort(layout); // Needed for blocks layout info. info.BlockHeaderForcedGapOffset = info.BlockSize; diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index 0772dcccc5..4c0256fe38 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -459,7 +459,7 @@ bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD, CharUnits BaseOffset = Layout.getBaseClassOffset(BD); Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo)); } - std::stable_sort(Bases.begin(), Bases.end()); + llvm::stable_sort(Bases); for (unsigned I = 0, N = Bases.size(); I != N; ++I) { BaseInfo &Base = Bases[I]; diff --git a/lib/CodeGen/CGOpenMPRuntime.cpp b/lib/CodeGen/CGOpenMPRuntime.cpp index 1e1251efea..c3f60d7f60 100644 --- a/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/lib/CodeGen/CGOpenMPRuntime.cpp @@ -4698,11 +4698,6 @@ emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, return TaskPrivatesMap; } -static bool stable_sort_comparator(const PrivateDataTy P1, - const PrivateDataTy P2) { - return P1.first > P2.first; -} - /// Emit initialization for private variables in task-based directives. static void emitPrivatesInit(CodeGenFunction &CGF, const OMPExecutableDirective &D, @@ -4956,7 +4951,9 @@ CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc, /*PrivateElemInit=*/nullptr)); ++I; } - std::stable_sort(Privates.begin(), Privates.end(), stable_sort_comparator); + llvm::stable_sort(Privates, [](PrivateDataTy L, PrivateDataTy R) { + return L.first > R.first; + }); QualType KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); // Build type kmp_routine_entry_t (if not built yet). emitKmpRoutineEntryT(KmpInt32Ty); diff --git a/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp b/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp index f4b044744f..ca1e9311b6 100644 --- a/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp +++ b/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp @@ -221,16 +221,13 @@ static const ValueDecl *getPrivateItem(const Expr *RefExpr) { return cast(ME->getMemberDecl()->getCanonicalDecl()); } -typedef std::pair VarsDataTy; -static bool stable_sort_comparator(const VarsDataTy P1, const VarsDataTy P2) { - return P1.first > P2.first; -} static RecordDecl *buildRecordForGlobalizedVars( ASTContext &C, ArrayRef EscapedDecls, ArrayRef EscapedDeclsForTeams, llvm::SmallDenseMap &MappedDeclsFields, int BufSize) { + using VarsDataTy = std::pair; if (EscapedDecls.empty() && EscapedDeclsForTeams.empty()) return nullptr; SmallVector GlobalizedVars; @@ -242,8 +239,10 @@ static RecordDecl *buildRecordForGlobalizedVars( D); for (const ValueDecl *D : EscapedDeclsForTeams) GlobalizedVars.emplace_back(C.getDeclAlign(D), D); - std::stable_sort(GlobalizedVars.begin(), GlobalizedVars.end(), - stable_sort_comparator); + llvm::stable_sort(GlobalizedVars, [](VarsDataTy L, VarsDataTy R) { + return L.first > R.first; + }); + // Build struct _globalized_locals_ty { // /* globalized vars */[WarSize] align (max(decl_align, // GlobalMemoryAlignment)) diff --git a/lib/CodeGen/CGRecordLayoutBuilder.cpp b/lib/CodeGen/CGRecordLayoutBuilder.cpp index d40d3554f3..b5102bb154 100644 --- a/lib/CodeGen/CGRecordLayoutBuilder.cpp +++ b/lib/CodeGen/CGRecordLayoutBuilder.cpp @@ -273,7 +273,7 @@ void CGRecordLowering::lower(bool NVBaseType) { if (!NVBaseType) accumulateVBases(); } - std::stable_sort(Members.begin(), Members.end()); + llvm::stable_sort(Members); Members.push_back(StorageInfo(Size, getIntNType(8))); clipTailPadding(); determinePacked(NVBaseType); @@ -658,7 +658,7 @@ void CGRecordLowering::insertPadding() { Pad = Padding.begin(), PadEnd = Padding.end(); Pad != PadEnd; ++Pad) Members.push_back(StorageInfo(Pad->first, getByteArrayType(Pad->second))); - std::stable_sort(Members.begin(), Members.end()); + llvm::stable_sort(Members); } void CGRecordLowering::fillOutputFields() { diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 427eabc36d..e51963aa9a 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -2574,10 +2574,9 @@ void CodeGenModule::emitMultiVersionFunctions() { ResolverFunc->setComdat( getModule().getOrInsertComdat(ResolverFunc->getName())); - std::stable_sort( - Options.begin(), Options.end(), - [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS, - const CodeGenFunction::MultiVersionResolverOption &RHS) { + llvm::stable_sort( + Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS, + const CodeGenFunction::MultiVersionResolverOption &RHS) { return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS); }); CodeGenFunction CGF(*this); diff --git a/lib/CodeGen/CoverageMappingGen.cpp b/lib/CodeGen/CoverageMappingGen.cpp index 10ad117bd4..ad014b5a17 100644 --- a/lib/CodeGen/CoverageMappingGen.cpp +++ b/lib/CodeGen/CoverageMappingGen.cpp @@ -243,7 +243,7 @@ public: ++Depth; FileLocs.push_back(std::make_pair(Loc, Depth)); } - std::stable_sort(FileLocs.begin(), FileLocs.end(), llvm::less_second()); + llvm::stable_sort(FileLocs, llvm::less_second()); for (const auto &FL : FileLocs) { SourceLocation Loc = FL.first; diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 0cba87316e..2772db0a11 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1780,11 +1780,10 @@ static void sortCppIncludes(const FormatStyle &Style, SmallVector Indices; for (unsigned i = 0, e = Includes.size(); i != e; ++i) Indices.push_back(i); - std::stable_sort( - Indices.begin(), Indices.end(), [&](unsigned LHSI, unsigned RHSI) { - return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) < - std::tie(Includes[RHSI].Category, Includes[RHSI].Filename); - }); + llvm::stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) { + return std::tie(Includes[LHSI].Category, Includes[LHSI].Filename) < + std::tie(Includes[RHSI].Category, Includes[RHSI].Filename); + }); // The index of the include on which the cursor will be put after // sorting/deduplicating. unsigned CursorIndex; diff --git a/lib/Format/SortJavaScriptImports.cpp b/lib/Format/SortJavaScriptImports.cpp index b8ce91455d..5be243f4c0 100644 --- a/lib/Format/SortJavaScriptImports.cpp +++ b/lib/Format/SortJavaScriptImports.cpp @@ -141,10 +141,9 @@ public: SmallVector Indices; for (unsigned i = 0, e = References.size(); i != e; ++i) Indices.push_back(i); - std::stable_sort(Indices.begin(), Indices.end(), - [&](unsigned LHSI, unsigned RHSI) { - return References[LHSI] < References[RHSI]; - }); + llvm::stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) { + return References[LHSI] < References[RHSI]; + }); bool ReferencesInOrder = std::is_sorted(Indices.begin(), Indices.end()); std::string ReferencesText; @@ -246,9 +245,8 @@ private: // Sort the individual symbols within the import. // E.g. `import {b, a} from 'x';` -> `import {a, b} from 'x';` SmallVector Symbols = Reference.Symbols; - std::stable_sort( - Symbols.begin(), Symbols.end(), - [&](const JsImportedSymbol &LHS, const JsImportedSymbol &RHS) { + llvm::stable_sort( + Symbols, [&](const JsImportedSymbol &LHS, const JsImportedSymbol &RHS) { return LHS.Symbol.compare_lower(RHS.Symbol) < 0; }); if (Symbols == Reference.Symbols) { diff --git a/lib/Format/UsingDeclarationsSorter.cpp b/lib/Format/UsingDeclarationsSorter.cpp index 623cbbf4a1..b6559db61d 100644 --- a/lib/Format/UsingDeclarationsSorter.cpp +++ b/lib/Format/UsingDeclarationsSorter.cpp @@ -127,8 +127,7 @@ void endUsingDeclarationBlock( } SmallVector SortedUsingDeclarations( UsingDeclarations->begin(), UsingDeclarations->end()); - std::stable_sort(SortedUsingDeclarations.begin(), - SortedUsingDeclarations.end()); + llvm::stable_sort(SortedUsingDeclarations); SortedUsingDeclarations.erase( std::unique(SortedUsingDeclarations.begin(), SortedUsingDeclarations.end(), diff --git a/lib/Index/CommentToXML.cpp b/lib/Index/CommentToXML.cpp index 6041f5b4cd..55923d679f 100644 --- a/lib/Index/CommentToXML.cpp +++ b/lib/Index/CommentToXML.cpp @@ -188,11 +188,8 @@ FullCommentParts::FullCommentParts(const FullComment *C, // Sort params in order they are declared in the function prototype. // Unresolved parameters are put at the end of the list in the same order // they were seen in the comment. - std::stable_sort(Params.begin(), Params.end(), - ParamCommandCommentCompareIndex()); - - std::stable_sort(TParams.begin(), TParams.end(), - TParamCommandCommentComparePosition()); + llvm::stable_sort(Params, ParamCommandCommentCompareIndex()); + llvm::stable_sort(TParams, TParamCommandCommentComparePosition()); } void printHTMLStartTagComment(const HTMLStartTagComment *C, diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 8d953308d8..c2d80409f1 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -4789,22 +4789,19 @@ typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate; static void mergeCandidatesWithResults( Sema &SemaRef, SmallVectorImpl &Results, OverloadCandidateSet &CandidateSet, SourceLocation Loc) { - if (!CandidateSet.empty()) { - // Sort the overload candidate set by placing the best overloads first. - std::stable_sort( - CandidateSet.begin(), CandidateSet.end(), - [&](const OverloadCandidate &X, const OverloadCandidate &Y) { - return isBetterOverloadCandidate(SemaRef, X, Y, Loc, - CandidateSet.getKind()); - }); - - // Add the remaining viable overload candidates as code-completion results. - for (OverloadCandidate &Candidate : CandidateSet) { - if (Candidate.Function && Candidate.Function->isDeleted()) - continue; - if (Candidate.Viable) - Results.push_back(ResultCandidate(Candidate.Function)); - } + // Sort the overload candidate set by placing the best overloads first. + llvm::stable_sort(CandidateSet, [&](const OverloadCandidate &X, + const OverloadCandidate &Y) { + return isBetterOverloadCandidate(SemaRef, X, Y, Loc, + CandidateSet.getKind()); + }); + + // Add the remaining viable overload candidates as code-completion results. + for (OverloadCandidate &Candidate : CandidateSet) { + if (Candidate.Function && Candidate.Function->isDeleted()) + continue; + if (Candidate.Viable) + Results.push_back(ResultCandidate(Candidate.Function)); } } diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index eea9298a56..73f3e75c80 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -10768,8 +10768,8 @@ void OverloadCandidateSet::NoteCandidates( } } - std::stable_sort(Cands.begin(), Cands.end(), - CompareOverloadCandidatesForDisplay(S, OpLoc, Args.size(), Kind)); + llvm::stable_sort( + Cands, CompareOverloadCandidatesForDisplay(S, OpLoc, Args.size(), Kind)); bool ReportedAmbiguousConversions = false; diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 07af218b77..9055eb7d71 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -938,7 +938,7 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, bool ShouldCheckConstantCond = HasConstantCond; // Sort all the scalar case values so we can easily detect duplicates. - std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); + llvm::stable_sort(CaseVals, CmpCaseVals); if (!CaseVals.empty()) { for (unsigned i = 0, e = CaseVals.size(); i != e; ++i) { @@ -986,7 +986,7 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, if (!CaseRanges.empty()) { // Sort all the case ranges by their low value so we can easily detect // overlaps between ranges. - std::stable_sort(CaseRanges.begin(), CaseRanges.end()); + llvm::stable_sort(CaseRanges); // Scan the ranges, computing the high values and removing empty ranges. std::vector HiVals; @@ -1105,7 +1105,7 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, AdjustAPSInt(Val, CondWidth, CondIsSigned); EnumVals.push_back(std::make_pair(Val, EDI)); } - std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); + llvm::stable_sort(EnumVals, CmpEnumVals); auto EI = EnumVals.begin(), EIEnd = std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); @@ -1256,7 +1256,7 @@ Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType, } if (EnumVals.empty()) return; - std::stable_sort(EnumVals.begin(), EnumVals.end(), CmpEnumVals); + llvm::stable_sort(EnumVals, CmpEnumVals); EnumValsTy::iterator EIend = std::unique(EnumVals.begin(), EnumVals.end(), EqEnumVals); diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index 3d3b771125..32e57eb925 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -275,10 +275,7 @@ namespace { /// Extract and remove the Attr* for a given attributed type. const Attr *takeAttrForAttributedType(const AttributedType *AT) { if (!AttrsForTypesSorted) { - std::stable_sort(AttrsForTypes.begin(), AttrsForTypes.end(), - [](const TypeAttrPair &A, const TypeAttrPair &B) { - return A.first < B.first; - }); + llvm::stable_sort(AttrsForTypes, llvm::less_first()); AttrsForTypesSorted = true; } diff --git a/utils/TableGen/NeonEmitter.cpp b/utils/TableGen/NeonEmitter.cpp index ecd7c3fde9..5cb688061d 100644 --- a/utils/TableGen/NeonEmitter.cpp +++ b/utils/TableGen/NeonEmitter.cpp @@ -2456,9 +2456,7 @@ void NeonEmitter::run(raw_ostream &OS) { for (auto *I : Defs) I->indexBody(); - std::stable_sort( - Defs.begin(), Defs.end(), - [](const Intrinsic *A, const Intrinsic *B) { return *A < *B; }); + llvm::stable_sort(Defs, llvm::less_ptr()); // Only emit a def when its requirements have been met. // FIXME: This loop could be made faster, but it's fast enough for now. @@ -2565,9 +2563,7 @@ void NeonEmitter::runFP16(raw_ostream &OS) { for (auto *I : Defs) I->indexBody(); - std::stable_sort( - Defs.begin(), Defs.end(), - [](const Intrinsic *A, const Intrinsic *B) { return *A < *B; }); + llvm::stable_sort(Defs, llvm::less_ptr()); // Only emit a def when its requirements have been met. // FIXME: This loop could be made faster, but it's fast enough for now.