[Coverage] Make sorting criteria for CounterMappingRegions local.
authorIgor Kudrin <ikudrin.dev@gmail.com>
Wed, 31 Aug 2016 07:01:17 +0000 (07:01 +0000)
committerIgor Kudrin <ikudrin.dev@gmail.com>
Wed, 31 Aug 2016 07:01:17 +0000 (07:01 +0000)
Move the comparison function into the only place there it is used,
i.e. the call to std::stable_sort in CoverageMappingWriter::write().

Add sorting by region kinds as it is required to ensure stable order
in our tests and to simplify D23987.

Differential Revision: https://reviews.llvm.org/D24034

llvm-svn: 280198

llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
llvm/lib/ProfileData/Coverage/CoverageMappingWriter.cpp

index 30571ce..89883e9 100644 (file)
@@ -245,12 +245,6 @@ struct CounterMappingRegion {
     return std::pair<unsigned, unsigned>(LineEnd, ColumnEnd);
   }
 
-  bool operator<(const CounterMappingRegion &Other) const {
-    if (FileID != Other.FileID)
-      return FileID < Other.FileID;
-    return startLoc() < Other.startLoc();
-  }
-
   bool contains(const CounterMappingRegion &Other) const {
     if (FileID != Other.FileID)
       return false;
index 8ff90d6..8235633 100644 (file)
@@ -108,8 +108,16 @@ static void writeCounter(ArrayRef<CounterExpression> Expressions, Counter C,
 
 void CoverageMappingWriter::write(raw_ostream &OS) {
   // Sort the regions in an ascending order by the file id and the starting
-  // location.
-  std::stable_sort(MappingRegions.begin(), MappingRegions.end());
+  // location. Sort by region kinds to ensure stable order for tests.
+  std::stable_sort(
+      MappingRegions.begin(), MappingRegions.end(),
+      [](const CounterMappingRegion &LHS, const CounterMappingRegion &RHS) {
+        if (LHS.FileID != RHS.FileID)
+          return LHS.FileID < RHS.FileID;
+        if (LHS.startLoc() != RHS.startLoc())
+          return LHS.startLoc() < RHS.startLoc();
+        return LHS.Kind < RHS.Kind;
+      });
 
   // Write out the fileid -> filename mapping.
   encodeULEB128(VirtualFileMapping.size(), OS);