Skip to content

Commit 4d197c8

Browse files
authored
[llvm-debuginfo-analyzer] Add --output-sort=(none|id) option (#145761)
- The output for `--output-sort=id` matches `--output-sort=offset` for the available readers. Tests were updated accordingly. - For `--output-sort=none`, and per `LVReader::sortScopes()`, `LVScope::sort()` is called on the root scope. `LVScope::sort()` has no effect if `getSortFunction() == nullptr`, and thus the elements are currently traversed in the order in which they were initially added. This should change, however, after `LVScope::Children` is removed.
1 parent f9c9968 commit 4d197c8

File tree

7 files changed

+53
-4
lines changed

7 files changed

+53
-4
lines changed

llvm/docs/CommandGuide/llvm-debuginfo-analyzer.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ output for a single compilation unit.
365365

366366
.. code-block:: text
367367
368+
=none: Unsorted output (i.e. as read from input).
369+
=id: Sort by unique element ID.
368370
=kind: Sort by element kind.
369371
=line: Sort by element line number.
370372
=name: Sort by element name.

llvm/include/llvm/DebugInfo/LogicalView/Core/LVSort.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class LVObject;
2323
// Object Sorting Mode.
2424
enum class LVSortMode {
2525
None = 0, // No given sort.
26+
ID, // Sort by ID.
2627
Kind, // Sort by kind.
2728
Line, // Sort by line.
2829
Name, // Sort by name.
@@ -38,6 +39,7 @@ using LVSortFunction = LVSortValue (*)(const LVObject *LHS,
3839
LLVM_ABI LVSortFunction getSortFunction();
3940

4041
// Comparator functions that can be used for sorting.
42+
LLVM_ABI LVSortValue compareID(const LVObject *LHS, const LVObject *RHS);
4143
LLVM_ABI LVSortValue compareKind(const LVObject *LHS, const LVObject *RHS);
4244
LLVM_ABI LVSortValue compareLine(const LVObject *LHS, const LVObject *RHS);
4345
LLVM_ABI LVSortValue compareName(const LVObject *LHS, const LVObject *RHS);

llvm/lib/DebugInfo/LogicalView/Core/LVSort.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ using namespace llvm::logicalview;
2222
//===----------------------------------------------------------------------===//
2323
// Callback functions to sort objects.
2424
//===----------------------------------------------------------------------===//
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+
2531
// Callback comparator based on kind.
2632
LVSortValue llvm::logicalview::compareKind(const LVObject *LHS,
2733
const LVObject *RHS) {
@@ -99,9 +105,9 @@ LVSortValue llvm::logicalview::sortByName(const LVObject *LHS,
99105
LVSortFunction llvm::logicalview::getSortFunction() {
100106
using LVSortInfo = std::map<LVSortMode, LVSortFunction>;
101107
static LVSortInfo SortInfo = {
102-
{LVSortMode::None, nullptr}, {LVSortMode::Kind, sortByKind},
103-
{LVSortMode::Line, sortByLine}, {LVSortMode::Name, sortByName},
104-
{LVSortMode::Offset, compareOffset},
108+
{LVSortMode::None, nullptr}, {LVSortMode::ID, compareID},
109+
{LVSortMode::Kind, sortByKind}, {LVSortMode::Line, sortByLine},
110+
{LVSortMode::Name, sortByName}, {LVSortMode::Offset, compareOffset},
105111
};
106112

107113
LVSortFunction SortFunction = nullptr;

llvm/test/tools/llvm-debuginfo-analyzer/COFF/01-coff-print-basic-details.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424
; RUN: %p/Inputs/test-codeview-clang.o 2>&1 | \
2525
; RUN: FileCheck --strict-whitespace -check-prefix=ONE %s
2626

27+
; If `--output-sort=id`, elements are iterated in the order in which they were
28+
; added (which matches the increasing offset of the reference output).
29+
; RUN: llvm-debuginfo-analyzer --attribute=level,format \
30+
; RUN: --output-sort=id \
31+
; RUN: --print=scopes,symbols,types,lines,instructions \
32+
; RUN: %p/Inputs/test-codeview-clang.o 2>&1 | \
33+
; RUN: FileCheck --strict-whitespace -check-prefix=ONE %s
34+
35+
; If `--output-sort=none`, `LVScope::Children` is not sorted; it, however,
36+
; reflects the order in which elements were added (same as `--output-sort=id`).
37+
; This is expected to change once #69160 is resolved though.
38+
; RUN: llvm-debuginfo-analyzer --attribute=level,format \
39+
; RUN: --output-sort=none \
40+
; RUN: --print=scopes,symbols,types,lines,instructions \
41+
; RUN: %p/Inputs/test-codeview-clang.o 2>&1 | \
42+
; RUN: FileCheck --strict-whitespace -check-prefix=ONE %s
43+
2744
; RUN: llvm-debuginfo-analyzer --attribute=level,format \
2845
; RUN: --output-sort=offset \
2946
; RUN: --print=elements \

llvm/test/tools/llvm-debuginfo-analyzer/DWARF/01-dwarf-print-basic-details.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424
; RUN: %p/Inputs/test-dwarf-clang.o 2>&1 | \
2525
; RUN: FileCheck --strict-whitespace -check-prefix=ONE %s
2626

27+
; If `--output-sort=id`, elements are iterated in the order in which they
28+
; were added (which matches the increasing offset of the reference output).
29+
; RUN: llvm-debuginfo-analyzer --attribute=level,format \
30+
; RUN: --output-sort=id \
31+
; RUN: --print=scopes,symbols,types,lines,instructions \
32+
; RUN: %p/Inputs/test-dwarf-clang.o 2>&1 | \
33+
; RUN: FileCheck --strict-whitespace -check-prefix=ONE %s
34+
35+
; If `--output-sort=none`, `LVScope::Children` is not sorted; it, however,
36+
; reflects the order in which elements were added (same as `--output-sort=id`).
37+
; This is expected to change once #69160 is resolved though.
38+
; RUN: llvm-debuginfo-analyzer --attribute=level,format \
39+
; RUN: --output-sort=none \
40+
; RUN: --print=scopes,symbols,types,lines,instructions \
41+
; RUN: %p/Inputs/test-dwarf-clang.o 2>&1 | \
42+
; RUN: FileCheck --strict-whitespace -check-prefix=ONE %s
43+
2744
; RUN: llvm-debuginfo-analyzer --attribute=level,format \
2845
; RUN: --output-sort=offset \
2946
; RUN: --print=elements \

llvm/test/tools/llvm-debuginfo-analyzer/cmdline.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ HELP-ALL: --output-file=<filename> - Redirect output to the specified file.
110110
HELP-ALL: --output-folder=<pathname> - Folder name for view splitting.
111111
HELP-ALL: --output-level=<N> - Only print to a depth of N elements.
112112
HELP-ALL: --output-sort=<value> - Primary key when ordering logical view (default: line).
113+
HELP-ALL: =none - Unsorted output (i.e. as read from input).
114+
HELP-ALL: =id - Sort by unique element ID.
113115
HELP-ALL: =kind - Sort by element kind.
114116
HELP-ALL: =line - Sort by element line number.
115117
HELP-ALL: =name - Sort by element name.

llvm/tools/llvm-debuginfo-analyzer/Options.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ static cl::opt<LVSortMode, true> OutputSort(
198198
"output-sort", cl::cat(OutputCategory),
199199
cl::desc("Primary key when ordering logical view (default: line)."),
200200
cl::Hidden, cl::ZeroOrMore,
201-
values(clEnumValN(LVSortMode::Kind, "kind", "Sort by element kind."),
201+
values(clEnumValN(LVSortMode::None, "none",
202+
"Unsorted output (i.e. as read from input)."),
203+
clEnumValN(LVSortMode::ID, "id", "Sort by unique element ID."),
204+
clEnumValN(LVSortMode::Kind, "kind", "Sort by element kind."),
202205
clEnumValN(LVSortMode::Line, "line", "Sort by element line number."),
203206
clEnumValN(LVSortMode::Name, "name", "Sort by element name."),
204207
clEnumValN(LVSortMode::Offset, "offset", "Sort by element offset.")),

0 commit comments

Comments
 (0)