1 //===-- CxxStringTypes.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "CxxStringTypes.h"
12 #include "llvm/Support/ConvertUTF.h"
14 #include "lldb/Core/ValueObject.h"
15 #include "lldb/Core/ValueObjectConstResult.h"
16 #include "lldb/DataFormatters/FormattersHelpers.h"
17 #include "lldb/DataFormatters/StringPrinter.h"
18 #include "lldb/DataFormatters/TypeSummary.h"
19 #include "lldb/Host/Time.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Target/ProcessStructReader.h"
22 #include "lldb/Target/SectionLoadList.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/Thread.h"
25 #include "lldb/Utility/DataBufferHeap.h"
26 #include "lldb/Utility/Endian.h"
27 #include "lldb/Utility/Status.h"
28 #include "lldb/Utility/Stream.h"
33 using namespace lldb_private;
34 using namespace lldb_private::formatters;
36 bool lldb_private::formatters::Char16StringSummaryProvider(
37 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
38 ProcessSP process_sp = valobj.GetProcessSP();
42 lldb::addr_t valobj_addr = GetArrayAddressOrPointerValue(valobj);
43 if (valobj_addr == 0 || valobj_addr == LLDB_INVALID_ADDRESS)
46 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
47 options.SetLocation(valobj_addr);
48 options.SetProcessSP(process_sp);
49 options.SetStream(&stream);
50 options.SetPrefixToken("u");
52 if (!StringPrinter::ReadStringAndDumpToStream<
53 StringPrinter::StringElementType::UTF16>(options)) {
54 stream.Printf("Summary Unavailable");
61 bool lldb_private::formatters::Char32StringSummaryProvider(
62 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
63 ProcessSP process_sp = valobj.GetProcessSP();
67 lldb::addr_t valobj_addr = GetArrayAddressOrPointerValue(valobj);
68 if (valobj_addr == 0 || valobj_addr == LLDB_INVALID_ADDRESS)
71 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
72 options.SetLocation(valobj_addr);
73 options.SetProcessSP(process_sp);
74 options.SetStream(&stream);
75 options.SetPrefixToken("U");
77 if (!StringPrinter::ReadStringAndDumpToStream<
78 StringPrinter::StringElementType::UTF32>(options)) {
79 stream.Printf("Summary Unavailable");
86 bool lldb_private::formatters::WCharStringSummaryProvider(
87 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
88 ProcessSP process_sp = valobj.GetProcessSP();
92 lldb::addr_t valobj_addr = GetArrayAddressOrPointerValue(valobj);
93 if (valobj_addr == 0 || valobj_addr == LLDB_INVALID_ADDRESS)
96 // Get a wchar_t basic type from the current type system
97 CompilerType wchar_compiler_type =
98 valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeWChar);
100 if (!wchar_compiler_type)
103 const uint32_t wchar_size = wchar_compiler_type.GetBitSize(
104 nullptr); // Safe to pass NULL for exe_scope here
106 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
107 options.SetLocation(valobj_addr);
108 options.SetProcessSP(process_sp);
109 options.SetStream(&stream);
110 options.SetPrefixToken("L");
112 switch (wchar_size) {
114 return StringPrinter::ReadStringAndDumpToStream<
115 StringPrinter::StringElementType::UTF8>(options);
117 return StringPrinter::ReadStringAndDumpToStream<
118 StringPrinter::StringElementType::UTF16>(options);
120 return StringPrinter::ReadStringAndDumpToStream<
121 StringPrinter::StringElementType::UTF32>(options);
123 stream.Printf("size for wchar_t is not valid");
129 bool lldb_private::formatters::Char16SummaryProvider(
130 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
133 valobj.GetData(data, error);
139 valobj.GetValueAsCString(lldb::eFormatUnicode16, value);
141 stream.Printf("%s ", value.c_str());
143 StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj);
144 options.SetData(data);
145 options.SetStream(&stream);
146 options.SetPrefixToken("u");
147 options.SetQuote('\'');
148 options.SetSourceSize(1);
149 options.SetBinaryZeroIsTerminator(false);
151 return StringPrinter::ReadBufferAndDumpToStream<
152 StringPrinter::StringElementType::UTF16>(options);
155 bool lldb_private::formatters::Char32SummaryProvider(
156 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
159 valobj.GetData(data, error);
165 valobj.GetValueAsCString(lldb::eFormatUnicode32, value);
167 stream.Printf("%s ", value.c_str());
169 StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj);
170 options.SetData(data);
171 options.SetStream(&stream);
172 options.SetPrefixToken("U");
173 options.SetQuote('\'');
174 options.SetSourceSize(1);
175 options.SetBinaryZeroIsTerminator(false);
177 return StringPrinter::ReadBufferAndDumpToStream<
178 StringPrinter::StringElementType::UTF32>(options);
181 bool lldb_private::formatters::WCharSummaryProvider(
182 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
185 valobj.GetData(data, error);
190 // Get a wchar_t basic type from the current type system
191 CompilerType wchar_compiler_type =
192 valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeWChar);
194 if (!wchar_compiler_type)
197 const uint32_t wchar_size = wchar_compiler_type.GetBitSize(
198 nullptr); // Safe to pass NULL for exe_scope here
200 StringPrinter::ReadBufferAndDumpToStreamOptions options(valobj);
201 options.SetData(data);
202 options.SetStream(&stream);
203 options.SetPrefixToken("L");
204 options.SetQuote('\'');
205 options.SetSourceSize(1);
206 options.SetBinaryZeroIsTerminator(false);
208 switch (wchar_size) {
210 return StringPrinter::ReadBufferAndDumpToStream<
211 StringPrinter::StringElementType::UTF8>(options);
213 return StringPrinter::ReadBufferAndDumpToStream<
214 StringPrinter::StringElementType::UTF16>(options);
216 return StringPrinter::ReadBufferAndDumpToStream<
217 StringPrinter::StringElementType::UTF32>(options);
219 stream.Printf("size for wchar_t is not valid");