Skip to content

Commit 3243d4a

Browse files
authored
Merge pull request swiftlang#40412 from apple/es-format
Fix JSON formatting for capturedPCMArgs in clang dependency graph
2 parents 3e56d23 + 13c5a63 commit 3243d4a

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ static void discoverCrosssImportOverlayDependencies(
336336
template <typename T>
337337
void writeJSONSingleField(llvm::raw_ostream &out, StringRef fieldName,
338338
const T &value, unsigned indentLevel,
339-
bool trailingComma);
339+
bool trailingComma, bool nested = false);
340340

341341
/// Write a string value as JSON.
342342
void writeJSONValue(llvm::raw_ostream &out, StringRef value,
@@ -442,11 +442,31 @@ void writeJSONValue(llvm::raw_ostream &out, const std::vector<T> &values,
442442
template <typename T>
443443
void writeJSONSingleField(llvm::raw_ostream &out, StringRef fieldName,
444444
const T &value, unsigned indentLevel,
445-
bool trailingComma) {
445+
bool trailingComma, bool nested) {
446446
out.indent(indentLevel * 2);
447447
writeJSONValue(out, fieldName, indentLevel);
448448
out << ": ";
449-
writeJSONValue(out, value, indentLevel);
449+
auto updatedIndentLevel = indentLevel;
450+
451+
if (nested) {
452+
// This is a hack to "fix" a format for a value that should be a nested
453+
// set of strings. Currently only capturedPCMArgs (clang) is expected to
454+
// in the nested format, which supposedly only contains one set of strings.
455+
// Adjust the indentation to account for the nested brackets.
456+
updatedIndentLevel += 1;
457+
out << "[\n";
458+
out.indent(updatedIndentLevel * 2);
459+
}
460+
461+
writeJSONValue(out, value, updatedIndentLevel);
462+
463+
if (nested) {
464+
// If nested, add an extra closing brack with a correct indentation.
465+
out << "\n";
466+
out.indent(indentLevel * 2);
467+
out << "]";
468+
}
469+
450470
if (trailingComma)
451471
out << ",";
452472
out << "\n";
@@ -720,7 +740,7 @@ static void writeJSON(llvm::raw_ostream &out,
720740

721741
// Captured PCM arguments.
722742
writeJSONSingleField(out, "capturedPCMArgs", clangDeps->captured_pcm_args, 5,
723-
/*trailingComma=*/false);
743+
/*trailingComma=*/false, /*nested=*/true);
724744
}
725745

726746
out.indent(4 * 2);

test/ScanDependencies/module_deps.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ import SubE
137137
// CHECK-NEXT: "C"
138138

139139
// CHECK: "capturedPCMArgs": [
140-
// CHECK-NEXT: "-Xcc",
141-
// CHECK-NEXT: "-fapinotes-swift-version=4"
140+
// CHECK-NEXT: [,
141+
// CHECK-NEXT: "-Xcc",
142+
// CHECK-NEXT: "-fapinotes-swift-version=4"
143+
// CHECK-NEXT: ]
142144
// CHECK-NEXT: ]
143145

144146
/// --------Swift module E

0 commit comments

Comments
 (0)