@@ -51,13 +51,16 @@ struct NestedCountedRegion : public coverage::CountedRegion {
5151 // Contains the path to default and expanded branches.
5252 // Size is 1 for default branches and greater 1 for expanded branches.
5353 std::vector<LineColPair> NestedPath;
54+ // Contains the original index of this element used to keep the original order
55+ // in case of equal nested path.
56+ unsigned Position;
5457 // Indicates whether this item should be ignored at rendering.
5558 bool Ignore = false ;
5659
5760 NestedCountedRegion (llvm::coverage::CountedRegion Region,
58- std::vector<LineColPair> NestedPath)
61+ std::vector<LineColPair> NestedPath, unsigned Position )
5962 : llvm::coverage::CountedRegion(std::move(Region)),
60- NestedPath (std::move(NestedPath)) {}
63+ NestedPath (std::move(NestedPath)), Position(Position) {}
6164
6265 // Returns the root line of the branch.
6366 unsigned getEffectiveLine () const { return NestedPath.front ().first ; }
@@ -95,7 +98,8 @@ void renderLineExecutionCounts(raw_ostream &OS,
9598std::vector<NestedCountedRegion>
9699collectNestedBranches (const coverage::CoverageMapping &Coverage,
97100 ArrayRef<llvm::coverage::ExpansionRecord> Expansions,
98- std::vector<LineColPair> &NestedPath) {
101+ std::vector<LineColPair> &NestedPath,
102+ unsigned &PositionCounter) {
99103 std::vector<NestedCountedRegion> Branches;
100104 for (const auto &Expansion : Expansions) {
101105 auto ExpansionCoverage = Coverage.getCoverageForExpansion (Expansion);
@@ -105,15 +109,16 @@ collectNestedBranches(const coverage::CoverageMapping &Coverage,
105109
106110 // Recursively collect branches from nested expansions.
107111 auto NestedExpansions = ExpansionCoverage.getExpansions ();
108- auto NestedExBranches =
109- collectNestedBranches (Coverage, NestedExpansions, NestedPath);
112+ auto NestedExBranches = collectNestedBranches (Coverage, NestedExpansions,
113+ NestedPath, PositionCounter );
110114 append_range (Branches, NestedExBranches);
111115
112116 // Add branches from this level of expansion.
113117 auto ExBranches = ExpansionCoverage.getBranches ();
114118 for (auto &B : ExBranches)
115119 if (B.FileID == Expansion.FileID ) {
116- Branches.push_back (NestedCountedRegion (B, NestedPath));
120+ Branches.push_back (
121+ NestedCountedRegion (B, NestedPath, PositionCounter++));
117122 }
118123
119124 NestedPath.pop_back ();
@@ -128,9 +133,11 @@ void appendNestedCountedRegions(const std::vector<CountedRegion> &Src,
128133 return !Region.TrueFolded || !Region.FalseFolded ;
129134 });
130135 Dst.reserve (Dst.size () + Src.size ());
136+ unsigned PositionCounter = Dst.size ();
131137 std::transform (Unfolded.begin (), Unfolded.end (), std::back_inserter (Dst),
132- [=](auto &Region) {
133- return NestedCountedRegion (Region, {Region.startLoc ()});
138+ [=, &PositionCounter](auto &Region) {
139+ return NestedCountedRegion (Region, {Region.startLoc ()},
140+ PositionCounter++);
134141 });
135142}
136143
@@ -143,10 +150,18 @@ void appendNestedCountedRegions(const std::vector<NestedCountedRegion> &Src,
143150 std::copy (Unfolded.begin (), Unfolded.end (), std::back_inserter (Dst));
144151}
145152
153+ bool sortLine (const llvm::coverage::CountedRegion &I,
154+ const llvm::coverage::CountedRegion &J) {
155+ return (I.LineStart < J.LineStart ) ||
156+ ((I.LineStart == J.LineStart ) && (I.ColumnStart < J.ColumnStart ));
157+ }
158+
146159bool sortNested (const NestedCountedRegion &I, const NestedCountedRegion &J) {
147160 // This sorts each element by line and column.
148161 // Implies that all elements are first sorted by getEffectiveLine().
149- return I.NestedPath < J.NestedPath ;
162+ // Use original position if NestedPath is equal.
163+ return std::tie (I.NestedPath , I.Position ) <
164+ std::tie (J.NestedPath , J.Position );
150165}
151166
152167void combineInstanceCounts (std::vector<NestedCountedRegion> &Branches) {
@@ -180,8 +195,9 @@ void renderBranchExecutionCounts(raw_ostream &OS,
180195
181196 // Recursively collect branches for all file expansions.
182197 std::vector<LineColPair> NestedPath;
183- std::vector<NestedCountedRegion> ExBranches =
184- collectNestedBranches (Coverage, FileCoverage.getExpansions (), NestedPath);
198+ unsigned PositionCounter = 0 ;
199+ std::vector<NestedCountedRegion> ExBranches = collectNestedBranches (
200+ Coverage, FileCoverage.getExpansions (), NestedPath, PositionCounter);
185201
186202 // Append Expansion Branches to Source Branches.
187203 appendNestedCountedRegions (ExBranches, Branches);
0 commit comments