@@ -332,6 +332,19 @@ static void discoverCrosssImportOverlayDependencies(
332332 allModules.end (), action);
333333}
334334
335+ namespace {
336+ std::string quote (StringRef unquoted) {
337+ llvm::SmallString<128 > buffer;
338+ llvm::raw_svector_ostream os (buffer);
339+ for (const auto ch : unquoted) {
340+ if (ch == ' \\ ' )
341+ os << ' \\ ' ;
342+ os << ch;
343+ }
344+ return buffer.str ().str ();
345+ }
346+ }
347+
335348// / Write a single JSON field.
336349template <typename T>
337350void writeJSONSingleField (llvm::raw_ostream &out, StringRef fieldName,
@@ -342,14 +355,14 @@ void writeJSONSingleField(llvm::raw_ostream &out, StringRef fieldName,
342355void writeJSONValue (llvm::raw_ostream &out, StringRef value,
343356 unsigned indentLevel) {
344357 out << " \" " ;
345- out << value;
358+ out << quote ( value) ;
346359 out << " \" " ;
347360}
348361
349362void writeJSONValue (llvm::raw_ostream &out, swiftscan_string_ref_t value,
350363 unsigned indentLevel) {
351364 out << " \" " ;
352- out << get_C_string (value);
365+ out << quote ( get_C_string (value) );
353366 out << " \" " ;
354367}
355368
@@ -615,7 +628,7 @@ static void writeJSON(llvm::raw_ostream &out,
615628 const auto &arg =
616629 get_C_string (swiftTextualDeps->command_line ->strings [i]);
617630 out.indent (6 * 2 );
618- out << " \" " << arg << " \" " ;
631+ out << " \" " << quote ( arg) << " \" " ;
619632 if (i != count - 1 )
620633 out << " ," ;
621634 out << " \n " ;
@@ -630,7 +643,7 @@ static void writeJSON(llvm::raw_ostream &out,
630643 const auto &candidate = get_C_string (
631644 swiftTextualDeps->compiled_module_candidates ->strings [i]);
632645 out.indent (6 * 2 );
633- out << " \" " << candidate << " \" " ;
646+ out << " \" " << quote ( candidate) << " \" " ;
634647 if (i != count - 1 )
635648 out << " ," ;
636649 out << " \n " ;
@@ -654,7 +667,7 @@ static void writeJSON(llvm::raw_ostream &out,
654667 const auto &arg =
655668 get_C_string (swiftTextualDeps->extra_pcm_args ->strings [i]);
656669 out.indent (6 * 2 );
657- out << " \" " << arg << " \" " ;
670+ out << " \" " << quote ( arg) << " \" " ;
658671 if (i != count - 1 )
659672 out << " ," ;
660673 out << " \n " ;
0 commit comments