Skip to content

Commit 9029742

Browse files
Jaroslav SevcikTeemperor
authored andcommitted
Data formatters: Look through array element typedefs
Summary: Motivation: When formatting an array of typedefed chars, we would like to display the array as a string. The string formatter currently does not trigger because the formatter lookup does not resolve typedefs for array elements (this behavior is inconsistent with pointers, for those we do look through pointee typedefs). This patch tries to make the array formatter lookup somewhat consistent with the pointer formatter lookup. Reviewers: teemperor, clayborg Reviewed By: teemperor, clayborg Subscribers: clayborg, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D72133
1 parent f3849f7 commit 9029742

File tree

6 files changed

+54
-6
lines changed

6 files changed

+54
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import lldb
2+
from lldbsuite.test.lldbtest import *
3+
import lldbsuite.test.lldbutil as lldbutil
4+
5+
6+
class ArrayTypedefTestCase(TestBase):
7+
8+
mydir = TestBase.compute_mydir(__file__)
9+
NO_DEBUG_INFO_TESTCASE = True
10+
11+
def test_array_typedef(self):
12+
self.build()
13+
lldbutil.run_to_source_breakpoint(self, "// break here",
14+
lldb.SBFileSpec("main.cpp", False))
15+
self.expect("expr str", substrs=['"abcd"'])
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
typedef char MCHAR;
2+
3+
int main() {
4+
MCHAR str[5] = "abcd";
5+
return 0; // break here
6+
}
7+

lldb/source/API/SBType.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ SBType SBType::GetArrayElementType() {
212212

213213
if (!IsValid())
214214
return LLDB_RECORD_RESULT(SBType());
215-
return LLDB_RECORD_RESULT(SBType(TypeImplSP(
216-
new TypeImpl(m_opaque_sp->GetCompilerType(true).GetArrayElementType()))));
215+
CompilerType canonical_type =
216+
m_opaque_sp->GetCompilerType(true).GetCanonicalType();
217+
return LLDB_RECORD_RESULT(
218+
SBType(TypeImplSP(new TypeImpl(canonical_type.GetArrayElementType()))));
217219
}
218220

219221
SBType SBType::GetArrayType(uint64_t size) {

lldb/source/DataFormatters/FormatManager.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,33 @@ void FormatManager::GetPossibleMatches(
230230
if (non_ptr_type.IsTypedefType()) {
231231
CompilerType deffed_pointed_type =
232232
non_ptr_type.GetTypedefedType().GetPointerType();
233+
const bool stripped_typedef = true;
233234
GetPossibleMatches(
234235
valobj, deffed_pointed_type,
235236
reason | lldb_private::eFormatterChoiceCriterionNavigatedTypedefs,
236237
use_dynamic, entries, did_strip_ptr, did_strip_ref,
237-
true); // this is not exactly the usual meaning of stripping typedefs
238+
stripped_typedef); // this is not exactly the usual meaning of
239+
// stripping typedefs
240+
}
241+
}
242+
243+
// For arrays with typedef-ed elements, we add a candidate with the typedef
244+
// stripped.
245+
uint64_t array_size;
246+
if (compiler_type.IsArrayType(nullptr, &array_size, nullptr)) {
247+
CompilerType element_type = compiler_type.GetArrayElementType();
248+
if (element_type.IsTypedefType()) {
249+
// Get the stripped element type and compute the stripped array type
250+
// from it.
251+
CompilerType deffed_array_type =
252+
element_type.GetTypedefedType().GetArrayType(array_size);
253+
const bool stripped_typedef = true;
254+
GetPossibleMatches(
255+
valobj, deffed_array_type,
256+
reason | lldb_private::eFormatterChoiceCriterionNavigatedTypedefs,
257+
use_dynamic, entries, did_strip_ptr, did_strip_ref,
258+
stripped_typedef); // this is not exactly the usual meaning of
259+
// stripping typedefs
238260
}
239261
}
240262

lldb/source/Symbol/ClangASTContext.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3924,16 +3924,15 @@ CompilerType
39243924
ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
39253925
uint64_t *stride) {
39263926
if (type) {
3927-
clang::QualType qual_type(GetCanonicalQualType(type));
3927+
clang::QualType qual_type(GetQualType(type));
39283928

39293929
const clang::Type *array_eletype =
39303930
qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
39313931

39323932
if (!array_eletype)
39333933
return CompilerType();
39343934

3935-
CompilerType element_type =
3936-
GetType(array_eletype->getCanonicalTypeUnqualified());
3935+
CompilerType element_type = GetType(clang::QualType(array_eletype, 0));
39373936

39383937
// TODO: the real stride will be >= this value.. find the real one!
39393938
if (stride)

0 commit comments

Comments
 (0)