LLDB  mainline
CxxStringTypes.cpp
Go to the documentation of this file.
1 //===-- CxxStringTypes.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 #include "CxxStringTypes.h"
10 
11 #include "llvm/Support/ConvertUTF.h"
12 
14 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Host/Time.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/Thread.h"
25 #include "lldb/Utility/Endian.h"
26 #include "lldb/Utility/Status.h"
27 #include "lldb/Utility/Stream.h"
28 
29 #include <algorithm>
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 using namespace lldb_private::formatters;
34 
36 
37 static constexpr std::pair<const char *, Format>
39  switch (ElemType) {
40  case StringElementType::UTF8:
41  return std::make_pair("u8", lldb::eFormatUnicode8);
42  case StringElementType::UTF16:
43  return std::make_pair("u", lldb::eFormatUnicode16);
44  case StringElementType::UTF32:
45  return std::make_pair("U", lldb::eFormatUnicode32);
46  default:
47  return std::make_pair(nullptr, lldb::eFormatInvalid);
48  }
49 }
50 
51 template <StringElementType ElemType>
52 static bool CharStringSummaryProvider(ValueObject &valobj, Stream &stream) {
53  Address valobj_addr = GetArrayAddressOrPointerValue(valobj);
54  if (!valobj_addr.IsValid())
55  return false;
56 
58  options.SetLocation(valobj_addr);
59  options.SetTargetSP(valobj.GetTargetSP());
60  options.SetStream(&stream);
61  options.SetPrefixToken(getElementTraits(ElemType).first);
62 
63  if (!StringPrinter::ReadStringAndDumpToStream<ElemType>(options))
64  stream.Printf("Summary Unavailable");
65 
66  return true;
67 }
68 
69 template <StringElementType ElemType>
70 static bool CharSummaryProvider(ValueObject &valobj, Stream &stream) {
71  DataExtractor data;
72  Status error;
73  valobj.GetData(data, error);
74 
75  if (error.Fail())
76  return false;
77 
78  std::string value;
80 
81  constexpr auto ElemTraits = getElementTraits(ElemType);
82  valobj.GetValueAsCString(ElemTraits.second, value);
83 
84  if (!value.empty())
85  stream.Printf("%s ", value.c_str());
86 
87  options.SetData(std::move(data));
88  options.SetStream(&stream);
89  options.SetPrefixToken(ElemTraits.first);
90  options.SetQuote('\'');
91  options.SetSourceSize(1);
92  options.SetBinaryZeroIsTerminator(false);
93 
94  return StringPrinter::ReadBufferAndDumpToStream<ElemType>(options);
95 }
96 
98  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
99  return CharStringSummaryProvider<StringElementType::UTF8>(valobj, stream);
100 }
101 
103  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
104  return CharStringSummaryProvider<StringElementType::UTF16>(valobj, stream);
105 }
106 
108  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
109  return CharStringSummaryProvider<StringElementType::UTF32>(valobj, stream);
110 }
111 
113  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
114  Address valobj_addr = GetArrayAddressOrPointerValue(valobj);
115  if (!valobj_addr.IsValid())
116  return false;
117 
118  // Get a wchar_t basic type from the current type system
119  CompilerType wchar_compiler_type =
121 
122  if (!wchar_compiler_type)
123  return false;
124 
125  // Safe to pass nullptr for exe_scope here.
126  llvm::Optional<uint64_t> size = wchar_compiler_type.GetBitSize(nullptr);
127  if (!size)
128  return false;
129  const uint32_t wchar_size = *size;
130 
132  options.SetLocation(valobj_addr);
133  options.SetTargetSP(valobj.GetTargetSP());
134  options.SetStream(&stream);
135  options.SetPrefixToken("L");
136 
137  switch (wchar_size) {
138  case 8:
139  return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8>(
140  options);
141  case 16:
142  return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16>(
143  options);
144  case 32:
145  return StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32>(
146  options);
147  default:
148  stream.Printf("size for wchar_t is not valid");
149  return true;
150  }
151  return true;
152 }
153 
155  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
156  return CharSummaryProvider<StringElementType::UTF8>(valobj, stream);
157 }
158 
160  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
161  return CharSummaryProvider<StringElementType::UTF16>(valobj, stream);
162 }
163 
165  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
166  return CharSummaryProvider<StringElementType::UTF32>(valobj, stream);
167 }
168 
170  ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
171  DataExtractor data;
172  Status error;
173  valobj.GetData(data, error);
174 
175  if (error.Fail())
176  return false;
177 
178  // Get a wchar_t basic type from the current type system
179  CompilerType wchar_compiler_type =
181 
182  if (!wchar_compiler_type)
183  return false;
184 
185  // Safe to pass nullptr for exe_scope here.
186  llvm::Optional<uint64_t> size = wchar_compiler_type.GetBitSize(nullptr);
187  if (!size)
188  return false;
189  const uint32_t wchar_size = *size;
190 
192  options.SetData(std::move(data));
193  options.SetStream(&stream);
194  options.SetPrefixToken("L");
195  options.SetQuote('\'');
196  options.SetSourceSize(1);
197  options.SetBinaryZeroIsTerminator(false);
198 
199  switch (wchar_size) {
200  case 8:
201  return StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF8>(
202  options);
203  case 16:
204  return StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16>(
205  options);
206  case 32:
207  return StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF32>(
208  options);
209  default:
210  stream.Printf("size for wchar_t is not valid");
211  return true;
212  }
213  return true;
214 }
lldb_private::ValueObject::GetData
virtual uint64_t GetData(DataExtractor &data, Status &error)
Definition: ValueObject.cpp:765
lldb_private::formatters::StringPrinter::DumpToStreamOptions::SetPrefixToken
void SetPrefixToken(const std::string &p)
Definition: StringPrinter.h:37
lldb_private::formatters::Char32SummaryProvider
bool Char32SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: CxxStringTypes.cpp:164
lldb_private::formatters::StringPrinter::ReadStringAndDumpToStreamOptions::SetLocation
void SetLocation(Address l)
Definition: StringPrinter.h:108
lldb_private::Address::IsValid
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:345
lldb_private::formatters::WCharSummaryProvider
bool WCharSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: CxxStringTypes.cpp:169
lldb_private::formatters::Char16StringSummaryProvider
bool Char16StringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: CxxStringTypes.cpp:102
lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions
Definition: StringPrinter.h:127
lldb_private::formatters::Char8SummaryProvider
bool Char8SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: CxxStringTypes.cpp:154
SectionLoadList.h
lldb_private::Stream
Definition: Stream.h:28
lldb_private::formatters::StringPrinter::DumpToStreamOptions::SetSourceSize
void SetSourceSize(uint32_t s)
Definition: StringPrinter.h:53
getElementTraits
static constexpr std::pair< const char *, Format > getElementTraits(StringElementType ElemType)
Definition: CxxStringTypes.cpp:38
lldb_private::formatters::StringPrinter::ReadStringAndDumpToStreamOptions::SetTargetSP
void SetTargetSP(lldb::TargetSP t)
Definition: StringPrinter.h:112
lldb_private::formatters::StringPrinter::DumpToStreamOptions::SetBinaryZeroIsTerminator
void SetBinaryZeroIsTerminator(bool e)
Definition: StringPrinter.h:61
Target.h
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb::eFormatUnicode8
@ eFormatUnicode8
Definition: lldb-enumerations.h:201
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::formatters::StringPrinter::DumpToStreamOptions::SetQuote
void SetQuote(char q)
Definition: StringPrinter.h:49
TypeSystemClang.h
CxxStringTypes.h
lldb_private::formatters::Char8StringSummaryProvider
bool Char8StringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: CxxStringTypes.cpp:97
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
Thread.h
ValueObject.h
Time.h
ValueObjectConstResult.h
lldb_private::formatters::Char32StringSummaryProvider
bool Char32StringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: CxxStringTypes.cpp:107
lldb::eFormatUnicode16
@ eFormatUnicode16
Definition: lldb-enumerations.h:175
lldb_private::ValueObject::GetValueAsCString
virtual const char * GetValueAsCString()
Definition: ValueObject.cpp:1049
ProcessStructReader.h
lldb_private::Status
Definition: Status.h:44
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
TypeSummary.h
StringElementType
StringPrinter::StringElementType StringElementType
Definition: CxxStringTypes.cpp:35
lldb_private::formatters::WCharStringSummaryProvider
bool WCharStringSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: CxxStringTypes.cpp:112
uint32_t
lldb_private::formatters::StringPrinter::StringElementType
StringElementType
Definition: StringPrinter.h:23
lldb_private::Address
Definition: Address.h:59
lldb_private::CompilerType::GetBitSize
llvm::Optional< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
Definition: CompilerType.cpp:482
lldb_private::formatters::GetArrayAddressOrPointerValue
Address GetArrayAddressOrPointerValue(ValueObject &valobj)
Definition: FormattersHelpers.cpp:135
lldb_private::ValueObject::GetCompilerType
CompilerType GetCompilerType()
Definition: ValueObject.h:352
lldb_private::TypeSummaryOptions
Definition: TypeSummary.h:26
lldb_private::formatters::StringPrinter::ReadStringAndDumpToStreamOptions
Definition: StringPrinter.h:102
lldb_private::CompilerType::GetBasicTypeFromAST
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
Definition: CompilerType.cpp:474
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
Status.h
lldb_private::formatters
Definition: CXXFunctionPointer.h:15
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::formatters::Char16SummaryProvider
bool Char16SummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
Definition: CxxStringTypes.cpp:159
lldb::eFormatUnicode32
@ eFormatUnicode32
Definition: lldb-enumerations.h:176
lldb_private::ValueObject::GetTargetSP
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions::SetData
void SetData(DataExtractor &&d)
Definition: StringPrinter.h:136
StringPrinter.h
Stream.h
lldb_private::formatters::StringPrinter::DumpToStreamOptions::SetStream
void SetStream(Stream *s)
Definition: StringPrinter.h:33
FormattersHelpers.h
lldb
Definition: SBAddress.h:15
Endian.h
lldb::eBasicTypeWChar
@ eBasicTypeWChar
Definition: lldb-enumerations.h:755
DataBufferHeap.h
lldb::eFormatInvalid
@ eFormatInvalid
Definition: lldb-enumerations.h:157
CharStringSummaryProvider
static bool CharStringSummaryProvider(ValueObject &valobj, Stream &stream)
Definition: CxxStringTypes.cpp:52
CharSummaryProvider
static bool CharSummaryProvider(ValueObject &valobj, Stream &stream)
Definition: CxxStringTypes.cpp:70