xref: /llvm-project/llvm/lib/DebugInfo/LogicalView/Core/LVSort.cpp (revision 0060c54e0da6d1429875da2d30895faa7562b706)
1 //===-- LVSort.cpp --------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Support for LVObject sorting.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/DebugInfo/LogicalView/Core/LVSort.h"
14 #include "llvm/DebugInfo/LogicalView/Core/LVReader.h"
15 #include <string>
16 
17 using namespace llvm;
18 using namespace llvm::logicalview;
19 
20 #define DEBUG_TYPE "Sort"
21 
22 //===----------------------------------------------------------------------===//
23 // Callback functions to sort objects.
24 //===----------------------------------------------------------------------===//
25 // Callback comparator based on kind.
26 LVSortValue llvm::logicalview::compareKind(const LVObject *LHS,
27                                            const LVObject *RHS) {
28   return std::string(LHS->kind()) < std::string(RHS->kind());
29 }
30 
31 // Callback comparator based on line.
32 LVSortValue llvm::logicalview::compareLine(const LVObject *LHS,
33                                            const LVObject *RHS) {
34   return LHS->getLineNumber() < RHS->getLineNumber();
35 }
36 
37 // Callback comparator based on name.
38 LVSortValue llvm::logicalview::compareName(const LVObject *LHS,
39                                            const LVObject *RHS) {
40   return LHS->getName() < RHS->getName();
41 }
42 
43 // Callback comparator based on DIE offset.
44 LVSortValue llvm::logicalview::compareOffset(const LVObject *LHS,
45                                              const LVObject *RHS) {
46   return LHS->getOffset() < RHS->getOffset();
47 }
48 
49 // Callback comparator for Range compare.
50 LVSortValue llvm::logicalview::compareRange(const LVObject *LHS,
51                                             const LVObject *RHS) {
52   if (LHS->getLowerAddress() < RHS->getLowerAddress())
53     return true;
54 
55   // If the lower address is the same, use the upper address value in
56   // order to put first the smallest interval.
57   if (LHS->getLowerAddress() == RHS->getLowerAddress())
58     return LHS->getUpperAddress() < RHS->getUpperAddress();
59 
60   return false;
61 }
62 
63 // Callback comparator based on multiple keys (First: Kind).
64 LVSortValue llvm::logicalview::sortByKind(const LVObject *LHS,
65                                           const LVObject *RHS) {
66   // Order in which the object attributes are used for comparison:
67   // kind, name, line number, offset.
68   std::tuple<std::string, StringRef, uint32_t, LVOffset> Left(
69       LHS->kind(), LHS->getName(), LHS->getLineNumber(), LHS->getOffset());
70   std::tuple<std::string, StringRef, uint32_t, LVOffset> Right(
71       RHS->kind(), RHS->getName(), RHS->getLineNumber(), RHS->getOffset());
72   return Left < Right;
73 }
74 
75 // Callback comparator based on multiple keys (First: Line).
76 LVSortValue llvm::logicalview::sortByLine(const LVObject *LHS,
77                                           const LVObject *RHS) {
78   // Order in which the object attributes are used for comparison:
79   // line number, name, kind, offset.
80   std::tuple<uint32_t, StringRef, std::string, LVOffset> Left(
81       LHS->getLineNumber(), LHS->getName(), LHS->kind(), LHS->getOffset());
82   std::tuple<uint32_t, StringRef, std::string, LVOffset> Right(
83       RHS->getLineNumber(), RHS->getName(), RHS->kind(), RHS->getOffset());
84   return Left < Right;
85 }
86 
87 // Callback comparator based on multiple keys (First: Name).
88 LVSortValue llvm::logicalview::sortByName(const LVObject *LHS,
89                                           const LVObject *RHS) {
90   // Order in which the object attributes are used for comparison:
91   // name, line number, kind, offset.
92   std::tuple<StringRef, uint32_t, std::string, LVOffset> Left(
93       LHS->getName(), LHS->getLineNumber(), LHS->kind(), LHS->getOffset());
94   std::tuple<StringRef, uint32_t, std::string, LVOffset> Right(
95       RHS->getName(), RHS->getLineNumber(), RHS->kind(), RHS->getOffset());
96   return Left < Right;
97 }
98 
99 LVSortFunction llvm::logicalview::getSortFunction() {
100   using LVSortInfo = std::map<LVSortMode, LVSortFunction>;
101   static LVSortInfo SortInfo = {
102       {LVSortMode::None, nullptr},         {LVSortMode::Kind, sortByKind},
103       {LVSortMode::Line, sortByLine},      {LVSortMode::Name, sortByName},
104       {LVSortMode::Offset, compareOffset},
105   };
106 
107   LVSortFunction SortFunction = nullptr;
108   LVSortInfo::iterator Iter = SortInfo.find(options().getSortMode());
109   if (Iter != SortInfo.end())
110     SortFunction = Iter->second;
111   return SortFunction;
112 }
113