[PATCH] D91183: Added GDB pretty printer for StringMap
Moritz Sichert via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 10 12:21:41 PST 2020
MoritzS updated this revision to Diff 304300.
MoritzS added a comment.
- Added test case
- Use generator instead of iterator class
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D91183/new/
https://reviews.llvm.org/D91183
Files:
debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.cpp
debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.gdb
llvm/utils/gdb-scripts/prettyprinters.py
Index: llvm/utils/gdb-scripts/prettyprinters.py
===================================================================
--- llvm/utils/gdb-scripts/prettyprinters.py
+++ llvm/utils/gdb-scripts/prettyprinters.py
@@ -1,4 +1,5 @@
from __future__ import print_function
+import struct
import sys
import gdb.printing
@@ -200,6 +201,44 @@
def display_hint(self):
return 'map'
+class StringMapPrinter:
+ "Print a StringMap"
+
+ def __init__(self, val):
+ self.val = val
+
+ def children(self):
+ it = self.val['TheTable']
+ end = (it + self.val['NumBuckets'])
+ value_ty = self.val.type.template_argument(0)
+ entry_ty = gdb.lookup_type('llvm::StringMapEntry<{}>'.format(value_ty.name))
+ tombstone = ((1 << (64 - 3)) - 1) << 3
+
+ while it != end:
+ it_deref = it.dereference()
+ if it_deref == 0 or it_deref == tombstone:
+ it = it + 1
+ continue
+
+ entry_ptr = it_deref.cast(entry_ty.pointer())
+ entry = entry_ptr.dereference()
+
+ str_len = entry['keyLength']
+ str_data = (entry_ptr + 1).cast(gdb.lookup_type('char').const().pointer())
+ string_ref = gdb.Value(struct.pack('PN', int(str_data), int(str_len)), gdb.lookup_type('llvm::StringRef'))
+ yield 'key', string_ref
+
+ value = entry['second']
+ yield 'value', value
+
+ it = it + 1
+
+ def to_string(self):
+ return 'llvm::StringMap with %d elements' % (self.val['NumItems'])
+
+ def display_hint(self):
+ return 'map'
+
class TwinePrinter:
"Print a Twine"
@@ -442,6 +481,7 @@
pp.add_printer('llvm::Expected', '^llvm::Expected<.*>$', ExpectedPrinter)
pp.add_printer('llvm::Optional', '^llvm::Optional<.*>$', OptionalPrinter)
pp.add_printer('llvm::DenseMap', '^llvm::DenseMap<.*>$', DenseMapPrinter)
+pp.add_printer('llvm::StringMap', '^llvm::StringMap<.*>$', StringMapPrinter)
pp.add_printer('llvm::Twine', '^llvm::Twine$', TwinePrinter)
pp.add_printer('llvm::PointerIntPair', '^llvm::PointerIntPair<.*>$', make_pointer_int_pair_printer)
pp.add_printer('llvm::PointerUnion', '^llvm::PointerUnion<.*>$', make_pointer_union_printer)
Index: debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.gdb
===================================================================
--- debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.gdb
+++ debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.gdb
@@ -40,6 +40,9 @@
# CHECK: "\"foo\"\"bar\""
p Twine
+# CHECK: llvm::StringMap with 2 elements = {["foo"] = 123, ["bar"] = 456}
+p StringMap
+
# CHECK: {pointer = 0xabc, value = 1}
p PointerIntPair
Index: debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.cpp
===================================================================
--- debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.cpp
+++ debuginfo-tests/llvm-prettyprinters/gdb/llvm-support.cpp
@@ -5,6 +5,7 @@
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/ilist.h"
#include "llvm/Support/Error.h"
@@ -15,6 +16,7 @@
llvm::ArrayRef<int> ArrayRef(Array);
llvm::MutableArrayRef<int> MutableArrayRef(Array);
llvm::DenseMap<int, int> DenseMap = {{4, 5}, {6, 7}};
+llvm::StringMap<int> StringMap = {{"foo", 123}, {"bar", 456}};
llvm::Expected<int> ExpectedValue(8);
llvm::Expected<int> ExpectedError(llvm::createStringError({}, ""));
llvm::Optional<int> OptionalValue(9);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91183.304300.patch
Type: text/x-patch
Size: 3442 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201110/1360750c/attachment.bin>
More information about the llvm-commits
mailing list