Skip to content

Commit 803d56b

Browse files
authored
Merge pull request #40743 from compnerd/quoting
DependencyScan: properly escape `\` in JSON strings
2 parents 0387e1c + 9da156e commit 803d56b

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
336349
template <typename T>
337350
void writeJSONSingleField(llvm::raw_ostream &out, StringRef fieldName,
@@ -342,14 +355,14 @@ void writeJSONSingleField(llvm::raw_ostream &out, StringRef fieldName,
342355
void writeJSONValue(llvm::raw_ostream &out, StringRef value,
343356
unsigned indentLevel) {
344357
out << "\"";
345-
out << value;
358+
out << quote(value);
346359
out << "\"";
347360
}
348361

349362
void 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";

test/ScanDependencies/escaped.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -scan-dependencies %s -I %S\\Inputs -o - | %FileCheck %s
3+
4+
// We want to explicitly use the Windows path separator
5+
// REQUIRES: OS=windows-msvc
6+
7+
import A
8+
9+
// CHECK: "modulePath": "escaped.swiftmodule",
10+
// CHECK-NEXT: "sourceFiles": [
11+
// CHECK-NEXT: "{{.*}}\\test\\ScanDependencies\\escaped.swift"
12+
// CHECK-NEXT: ],

0 commit comments

Comments
 (0)