Skip to content

Commit 7a21445

Browse files
committed
[Driver] Show incremental is off with bad JSON
SR-2855 suggests `-driver-show-incremental` not only print information about why certain files are included in incremental compilation, but also print out why incremental compilation may be disabled altogether. Add a message for one such reason: when the build record file is malformed.
1 parent 3eab2dc commit 7a21445

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

lib/Driver/Driver.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ class Driver::InputInfoMap
194194
};
195195
using InputInfoMap = Driver::InputInfoMap;
196196

197+
static bool failedToReadOutOfDateMap(bool ShowIncrementalBuildDecisions,
198+
StringRef buildRecordPath,
199+
StringRef reason = "") {
200+
if (ShowIncrementalBuildDecisions) {
201+
llvm::outs() << "Incremental compilation has been disabled due to "
202+
<< "malformed build record file '" << buildRecordPath << "'.";
203+
if (!reason.empty()) {
204+
llvm::outs() << " " << reason;
205+
}
206+
llvm::outs() << "\n";
207+
}
208+
return true;
209+
}
210+
197211
static bool populateOutOfDateMap(InputInfoMap &map, StringRef argsHashStr,
198212
const InputFileList &inputs,
199213
StringRef buildRecordPath,
@@ -211,11 +225,13 @@ static bool populateOutOfDateMap(InputInfoMap &map, StringRef argsHashStr,
211225

212226
auto I = stream.begin();
213227
if (I == stream.end() || !I->getRoot())
214-
return true;
228+
return failedToReadOutOfDateMap(ShowIncrementalBuildDecisions,
229+
buildRecordPath);
215230

216231
auto *topLevelMap = dyn_cast<yaml::MappingNode>(I->getRoot());
217232
if (!topLevelMap)
218-
return true;
233+
return failedToReadOutOfDateMap(ShowIncrementalBuildDecisions,
234+
buildRecordPath);
219235
SmallString<64> scratch;
220236

221237
llvm::StringMap<InputInfo> previousInputs;
@@ -269,8 +285,12 @@ static bool populateOutOfDateMap(InputInfoMap &map, StringRef argsHashStr,
269285
using compilation_record::TopLevelKey;
270286
if (keyStr == compilation_record::getName(TopLevelKey::Version)) {
271287
auto *value = dyn_cast<yaml::ScalarNode>(i->getValue());
272-
if (!value)
273-
return true;
288+
if (!value) {
289+
auto reason = ("Malformed value for key '" + keyStr + "'.")
290+
.toStringRef(scratch);
291+
return failedToReadOutOfDateMap(ShowIncrementalBuildDecisions,
292+
buildRecordPath, reason);
293+
}
274294

275295
// NB: We check against
276296
// swift::version::Version::getCurrentLanguageVersion() here because any
@@ -289,17 +309,25 @@ static bool populateOutOfDateMap(InputInfoMap &map, StringRef argsHashStr,
289309

290310
} else if (keyStr == compilation_record::getName(TopLevelKey::BuildTime)) {
291311
auto *value = dyn_cast<yaml::SequenceNode>(i->getValue());
292-
if (!value)
293-
return true;
312+
if (!value) {
313+
auto reason = ("Malformed value for key '" + keyStr + "'.")
314+
.toStringRef(scratch);
315+
return failedToReadOutOfDateMap(ShowIncrementalBuildDecisions,
316+
buildRecordPath, reason);
317+
}
294318
llvm::sys::TimeValue timeVal;
295319
if (readTimeValue(i->getValue(), timeVal))
296320
return true;
297321
map[nullptr] = { InputInfo::NeedsCascadingBuild, timeVal };
298322

299323
} else if (keyStr == compilation_record::getName(TopLevelKey::Inputs)) {
300324
auto *inputMap = dyn_cast<yaml::MappingNode>(i->getValue());
301-
if (!inputMap)
302-
return true;
325+
if (!inputMap) {
326+
auto reason = ("Malformed value for key '" + keyStr + "'.")
327+
.toStringRef(scratch);
328+
return failedToReadOutOfDateMap(ShowIncrementalBuildDecisions,
329+
buildRecordPath, reason);
330+
}
303331

304332
// FIXME: LLVM's YAML support does incremental parsing in such a way that
305333
// for-range loops break.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Test that when:
2+
//
3+
// 1. Using -incremental -v -driver-show-incremental, and...
4+
// 2. ...the build record file does not contain valid JSON...
5+
//
6+
// ...then the driver prints a message indicating that incremental compilation
7+
// is disabled.
8+
9+
10+
// RUN: rm -rf %t && cp -r %S/Inputs/one-way/ %t
11+
// RUN: %S/Inputs/touch.py 443865900 %t/*
12+
13+
// RUN: echo '{version: "'$(%swiftc_driver_plain -version | head -n1)'", inputs: {"./main.swift": [443865900, 0], "./other.swift": [443865900, 0]}}' > %t/main~buildrecord.swiftdeps
14+
// RUN: cd %t && %swiftc_driver -driver-use-frontend-path %S/Inputs/update-dependencies.py -c ./main.swift ./other.swift -module-name main -incremental -v -driver-show-incremental -output-file-map %t/output.json | %FileCheck --check-prefix CHECK-INCREMENTAL %s
15+
// CHECK-INCREMENTAL-NOT: Incremental compilation has been disabled
16+
// CHECK-INCREMENTAL: Queuing main.swift (initial)
17+
18+
// RUN: rm %t/main~buildrecord.swiftdeps && touch %t/main~buildrecord.swiftdeps
19+
// RUN: cd %t && %swiftc_driver -driver-use-frontend-path %S/Inputs/update-dependencies.py -g -c ./main.swift ./other.swift -module-name main -incremental -v -driver-show-incremental -output-file-map %t/output.json | %FileCheck --check-prefix CHECK-MALFORMED %s
20+
21+
// RUN: echo 'foo' > %t/main~buildrecord.swiftdeps
22+
// RUN: cd %t && %swiftc_driver -driver-use-frontend-path %S/Inputs/update-dependencies.py -g -c ./main.swift ./other.swift -module-name main -incremental -v -driver-show-incremental -output-file-map %t/output.json | %FileCheck --check-prefix CHECK-MALFORMED %s
23+
24+
// CHECK-MALFORMED: Incremental compilation has been disabled{{.*}}malformed build record file
25+
// CHECK-MALFORMED-NOT: Queuing main.swift (initial)
26+
27+
// RUN: echo '{version, inputs: {"./main.swift": [443865900, 0], "./other.swift": [443865900, 0]}}' > %t/main~buildrecord.swiftdeps
28+
// RUN: cd %t && %swiftc_driver -driver-use-frontend-path %S/Inputs/update-dependencies.py -g -c ./main.swift ./other.swift -module-name main -incremental -v -driver-show-incremental -output-file-map %t/output.json | %FileCheck --check-prefix CHECK-MISSING-KEY %s
29+
30+
// RUN: echo '{version: "'$(%swiftc_driver_plain -version | head -n1)'", inputs}' > %t/main~buildrecord.swiftdeps
31+
// RUN: cd %t && %swiftc_driver -driver-use-frontend-path %S/Inputs/update-dependencies.py -g -c ./main.swift ./other.swift -module-name main -incremental -v -driver-show-incremental -output-file-map %t/output.json | %FileCheck --check-prefix CHECK-MISSING-KEY %s
32+
33+
// CHECK-MISSING-KEY: Incremental compilation has been disabled{{.*}}malformed build record file{{.*}}Malformed value for key
34+
// CHECK-MISSING-KEY-NOT: Queuing main.swift (initial)
35+

0 commit comments

Comments
 (0)