| 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 ID. |
| 26 | LVSortValue llvm::logicalview::compareID(const LVObject *LHS, |
| 27 | const LVObject *RHS) { |
| 28 | return LHS->getID() < RHS->getID(); |
| 29 | } |
| 30 | |
| 31 | // Callback comparator based on kind. |
| 32 | LVSortValue llvm::logicalview::compareKind(const LVObject *LHS, |
| 33 | const LVObject *RHS) { |
| 34 | return std::string(LHS->kind()) < std::string(RHS->kind()); |
| 35 | } |
| 36 | |
| 37 | // Callback comparator based on line. |
| 38 | LVSortValue llvm::logicalview::compareLine(const LVObject *LHS, |
| 39 | const LVObject *RHS) { |
| 40 | return LHS->getLineNumber() < RHS->getLineNumber(); |
| 41 | } |
| 42 | |
| 43 | // Callback comparator based on name. |
| 44 | LVSortValue llvm::logicalview::compareName(const LVObject *LHS, |
| 45 | const LVObject *RHS) { |
| 46 | return LHS->getName() < RHS->getName(); |
| 47 | } |
| 48 | |
| 49 | // Callback comparator based on DIE offset. |
| 50 | LVSortValue llvm::logicalview::compareOffset(const LVObject *LHS, |
| 51 | const LVObject *RHS) { |
| 52 | return LHS->getOffset() < RHS->getOffset(); |
| 53 | } |
| 54 | |
| 55 | // Callback comparator for Range compare. |
| 56 | LVSortValue llvm::logicalview::compareRange(const LVObject *LHS, |
| 57 | const LVObject *RHS) { |
| 58 | if (LHS->getLowerAddress() < RHS->getLowerAddress()) |
| 59 | return true; |
| 60 | |
| 61 | // If the lower address is the same, use the upper address value in |
| 62 | // order to put first the smallest interval. |
| 63 | if (LHS->getLowerAddress() == RHS->getLowerAddress()) |
| 64 | return LHS->getUpperAddress() < RHS->getUpperAddress(); |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // Callback comparator based on multiple keys (First: Kind). |
| 70 | LVSortValue llvm::logicalview::sortByKind(const LVObject *LHS, |
| 71 | const LVObject *RHS) { |
| 72 | // Order in which the object attributes are used for comparison: |
| 73 | // kind, name, line number, offset. |
| 74 | std::tuple<std::string, StringRef, uint32_t, LVOffset> Left( |
| 75 | LHS->kind(), LHS->getName(), LHS->getLineNumber(), LHS->getOffset()); |
| 76 | std::tuple<std::string, StringRef, uint32_t, LVOffset> Right( |
| 77 | RHS->kind(), RHS->getName(), RHS->getLineNumber(), RHS->getOffset()); |
| 78 | return Left < Right; |
| 79 | } |
| 80 | |
| 81 | // Callback comparator based on multiple keys (First: Line). |
| 82 | LVSortValue llvm::logicalview::sortByLine(const LVObject *LHS, |
| 83 | const LVObject *RHS) { |
| 84 | // Order in which the object attributes are used for comparison: |
| 85 | // line number, name, kind, offset. |
| 86 | std::tuple<uint32_t, StringRef, std::string, LVOffset> Left( |
| 87 | LHS->getLineNumber(), LHS->getName(), LHS->kind(), LHS->getOffset()); |
| 88 | std::tuple<uint32_t, StringRef, std::string, LVOffset> Right( |
| 89 | RHS->getLineNumber(), RHS->getName(), RHS->kind(), RHS->getOffset()); |
| 90 | return Left < Right; |
| 91 | } |
| 92 | |
| 93 | // Callback comparator based on multiple keys (First: Name). |
| 94 | LVSortValue llvm::logicalview::sortByName(const LVObject *LHS, |
| 95 | const LVObject *RHS) { |
| 96 | // Order in which the object attributes are used for comparison: |
| 97 | // name, line number, kind, offset. |
| 98 | std::tuple<StringRef, uint32_t, std::string, LVOffset> Left( |
| 99 | LHS->getName(), LHS->getLineNumber(), LHS->kind(), LHS->getOffset()); |
| 100 | std::tuple<StringRef, uint32_t, std::string, LVOffset> Right( |
| 101 | RHS->getName(), RHS->getLineNumber(), RHS->kind(), RHS->getOffset()); |
| 102 | return Left < Right; |
| 103 | } |
| 104 | |
| 105 | LVSortFunction llvm::logicalview::getSortFunction() { |
| 106 | using LVSortInfo = std::map<LVSortMode, LVSortFunction>; |
| 107 | static LVSortInfo SortInfo = { |
| 108 | {LVSortMode::None, nullptr}, {LVSortMode::ID, compareID}, |
| 109 | {LVSortMode::Kind, sortByKind}, {LVSortMode::Line, sortByLine}, |
| 110 | {LVSortMode::Name, sortByName}, {LVSortMode::Offset, compareOffset}, |
| 111 | }; |
| 112 | |
| 113 | LVSortFunction SortFunction = nullptr; |
| 114 | LVSortInfo::iterator Iter = SortInfo.find(x: options().getSortMode()); |
| 115 | if (Iter != SortInfo.end()) |
| 116 | SortFunction = Iter->second; |
| 117 | return SortFunction; |
| 118 | } |
| 119 | |