2323
2424namespace lldb_vscode {
2525
26+ void EmplaceSafeString (llvm::json::Object &obj, llvm::StringRef key,
27+ llvm::StringRef str) {
28+ if (LLVM_LIKELY (llvm::json::isUTF8 (str)))
29+ obj.try_emplace (key, str.str ());
30+ else
31+ obj.try_emplace (key, llvm::json::fixUTF8 (str));
32+ }
33+
2634llvm::StringRef GetAsString (const llvm::json::Value &value) {
2735 if (auto s = value.getAsString ())
2836 return *s;
@@ -124,11 +132,11 @@ std::vector<std::string> GetStrings(const llvm::json::Object *obj,
124132
125133void SetValueForKey (lldb::SBValue &v, llvm::json::Object &object,
126134 llvm::StringRef key) {
127-
135+
128136 llvm::StringRef value = v.GetValue ();
129137 llvm::StringRef summary = v.GetSummary ();
130138 llvm::StringRef type_name = v.GetType ().GetDisplayTypeName ();
131-
139+
132140 std::string result;
133141 llvm::raw_string_ostream strm (result);
134142 if (!value.empty ()) {
@@ -144,7 +152,7 @@ void SetValueForKey(lldb::SBValue &v, llvm::json::Object &object,
144152 strm << " @ " << llvm::format_hex (address, 0 );
145153 }
146154 strm.flush ();
147- object. try_emplace ( key, result);
155+ EmplaceSafeString (object, key, result);
148156}
149157
150158void FillResponse (const llvm::json::Object &request,
@@ -153,7 +161,7 @@ void FillResponse(const llvm::json::Object &request,
153161 // to true by default.
154162 response.try_emplace (" type" , " response" );
155163 response.try_emplace (" seq" , (int64_t )0 );
156- response. try_emplace ( " command" , GetString (request, " command" ));
164+ EmplaceSafeString (response, " command" , GetString (request, " command" ));
157165 const int64_t seq = GetSigned (request, " seq" , 0 );
158166 response.try_emplace (" request_seq" , seq);
159167 response.try_emplace (" success" , true );
@@ -223,7 +231,7 @@ llvm::json::Value CreateScope(const llvm::StringRef name,
223231 int64_t variablesReference,
224232 int64_t namedVariables, bool expensive) {
225233 llvm::json::Object object;
226- object. try_emplace ( " name" , name.str ());
234+ EmplaceSafeString (object, " name" , name.str ());
227235 object.try_emplace (" variablesReference" , variablesReference);
228236 object.try_emplace (" expensive" , expensive);
229237 object.try_emplace (" namedVariables" , namedVariables);
@@ -357,7 +365,7 @@ llvm::json::Object CreateEventObject(const llvm::StringRef event_name) {
357365 llvm::json::Object event;
358366 event.try_emplace (" seq" , 0 );
359367 event.try_emplace (" type" , " event" );
360- event. try_emplace ( " event" , event_name);
368+ EmplaceSafeString (event, " event" , event_name);
361369 return event;
362370}
363371
@@ -388,8 +396,8 @@ llvm::json::Object CreateEventObject(const llvm::StringRef event_name) {
388396llvm::json::Value
389397CreateExceptionBreakpointFilter (const ExceptionBreakpoint &bp) {
390398 llvm::json::Object object;
391- object. try_emplace ( " filter" , bp.filter );
392- object. try_emplace ( " label" , bp.label );
399+ EmplaceSafeString (object, " filter" , bp.filter );
400+ EmplaceSafeString (object, " label" , bp.label );
393401 object.try_emplace (" default" , bp.default_value );
394402 return llvm::json::Value (std::move (object));
395403}
@@ -467,11 +475,11 @@ llvm::json::Value CreateSource(lldb::SBLineEntry &line_entry) {
467475 if (file.IsValid ()) {
468476 const char *name = file.GetFilename ();
469477 if (name)
470- object. try_emplace ( " name" , name);
478+ EmplaceSafeString (object, " name" , name);
471479 char path[PATH_MAX] = " " ;
472480 file.GetPath (path, sizeof (path));
473481 if (path[0 ]) {
474- object. try_emplace ( " path" , std::string (path));
482+ EmplaceSafeString (object, " path" , std::string (path));
475483 }
476484 }
477485 return llvm::json::Value (std::move (object));
@@ -517,7 +525,7 @@ llvm::json::Value CreateSource(lldb::SBFrame &frame, int64_t &disasm_line) {
517525 }
518526 const auto num_insts = insts.GetSize ();
519527 if (low_pc != LLDB_INVALID_ADDRESS && num_insts > 0 ) {
520- object. try_emplace ( " name" , frame.GetFunctionName ());
528+ EmplaceSafeString (object, " name" , frame.GetFunctionName ());
521529 SourceReference source;
522530 llvm::raw_string_ostream src_strm (source.content );
523531 std::string line;
@@ -540,8 +548,8 @@ llvm::json::Value CreateSource(lldb::SBFrame &frame, int64_t &disasm_line) {
540548 line.clear ();
541549 llvm::raw_string_ostream line_strm (line);
542550 line_strm << llvm::formatv (" {0:X+}: <{1}> {2} {3,12} {4}" , inst_addr,
543- inst_offset, llvm::fmt_repeat (' ' , spaces),
544- m, o);
551+ inst_offset, llvm::fmt_repeat (' ' , spaces), m,
552+ o);
545553
546554 // If there is a comment append it starting at column 60 or after one
547555 // space past the last char
@@ -626,7 +634,7 @@ llvm::json::Value CreateStackFrame(lldb::SBFrame &frame) {
626634 llvm::json::Object object;
627635 int64_t frame_id = MakeVSCodeFrameID (frame);
628636 object.try_emplace (" id" , frame_id);
629- object. try_emplace ( " name" , frame.GetFunctionName ());
637+ EmplaceSafeString (object, " name" , frame.GetFunctionName ());
630638 int64_t disasm_line = 0 ;
631639 object.try_emplace (" source" , CreateSource (frame, disasm_line));
632640
@@ -670,9 +678,9 @@ llvm::json::Value CreateThread(lldb::SBThread &thread) {
670678 std::string thread_with_name (thread_str);
671679 thread_with_name += ' ' ;
672680 thread_with_name += name;
673- object. try_emplace ( " name" , thread_with_name);
681+ EmplaceSafeString (object, " name" , thread_with_name);
674682 } else {
675- object. try_emplace ( " name" , std::string (thread_str));
683+ EmplaceSafeString (object, " name" , std::string (thread_str));
676684 }
677685 return llvm::json::Value (std::move (object));
678686}
@@ -749,7 +757,7 @@ llvm::json::Value CreateThreadStopped(lldb::SBThread &thread,
749757 ExceptionBreakpoint *exc_bp = g_vsc.GetExceptionBPFromStopReason (thread);
750758 if (exc_bp) {
751759 body.try_emplace (" reason" , " exception" );
752- body. try_emplace ( " description" , exc_bp->label );
760+ EmplaceSafeString (body, " description" , exc_bp->label );
753761 } else {
754762 body.try_emplace (" reason" , " breakpoint" );
755763 }
@@ -782,7 +790,7 @@ llvm::json::Value CreateThreadStopped(lldb::SBThread &thread,
782790 if (ObjectContainsKey (body, " description" )) {
783791 char description[1024 ];
784792 if (thread.GetStopDescription (description, sizeof (description))) {
785- body. try_emplace ( " description" , std::string (description));
793+ EmplaceSafeString (body, " description" , std::string (description));
786794 }
787795 }
788796 if (tid == g_vsc.focus_tid ) {
@@ -862,12 +870,12 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t variablesReference,
862870 int64_t varID, bool format_hex) {
863871 llvm::json::Object object;
864872 auto name = v.GetName ();
865- object. try_emplace ( " name" , name ? name : " <null>" );
873+ EmplaceSafeString (object, " name" , name ? name : " <null>" );
866874 if (format_hex)
867875 v.SetFormat (lldb::eFormatHex);
868876 SetValueForKey (v, object, " value" );
869877 auto type_cstr = v.GetType ().GetDisplayTypeName ();
870- object. try_emplace ( " type" , type_cstr ? type_cstr : NO_TYPENAME);
878+ EmplaceSafeString (object, " type" , type_cstr ? type_cstr : NO_TYPENAME);
871879 if (varID != INT64_MAX)
872880 object.try_emplace (" id" , varID);
873881 if (v.MightHaveChildren ())
@@ -878,7 +886,7 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t variablesReference,
878886 v.GetExpressionPath (evaluateStream);
879887 const char *evaluateName = evaluateStream.GetData ();
880888 if (evaluateName && evaluateName[0 ])
881- object. try_emplace ( " evaluateName" , std::string (evaluateName));
889+ EmplaceSafeString (object, " evaluateName" , std::string (evaluateName));
882890 return llvm::json::Value (std::move (object));
883891}
884892
0 commit comments