Skip to content

Commit 4e9545e

Browse files
authored
[clang-cache] Report scan diagnostics (#9035) (#9110)
Diagnostics from dependency scanning daemon aren't making their way to the end-user and the feedback is vague: ``` fatal error: CAS-based dependency scan failed: depscan daemon failed: failed to get include-tree ``` This patch extends the daemon protocol to send the diagnostics back and prints them to standard error output. rdar://116549128 (cherry picked from commit 2fdda43)
1 parent 79ba6fd commit 4e9545e

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// REQUIRES: system-darwin, clang-cc1daemon
2+
3+
// RUN: rm -rf %t && mkdir -p %t
4+
5+
// RUN: not env LLVM_CACHE_CAS_PATH=%t/cas cache-build-session %clang-cache \
6+
// RUN: %clang -fsyntax-only -x c %s \
7+
// RUN: 2>&1 | FileCheck %s
8+
9+
#include "missing.h"
10+
// CHECK: fatal error: 'missing.h' file not found

clang/tools/driver/cc1depscanProtocol.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,11 @@ CC1DepScanDProtocol::getScanResult(llvm::StringSaver &Saver, ResultKind &Result,
308308
if (Error E = getResultKind(Result))
309309
return E;
310310

311-
if (Result == ErrorResult)
312-
return getString(Saver, FailedReason);
311+
if (Result == ErrorResult) {
312+
if (Error E = getString(Saver, FailedReason))
313+
return E;
314+
return getString(Saver, DiagnosticOutput);
315+
}
313316

314317
if (Result == InvalidResult) {
315318
FailedReason = "invalid scan result";
@@ -334,10 +337,14 @@ llvm::Error CC1DepScanDProtocol::putScanResultSuccess(
334337
return putString(DiagnosticOutput);
335338
}
336339

337-
llvm::Error CC1DepScanDProtocol::putScanResultFailed(StringRef Reason) {
340+
llvm::Error
341+
CC1DepScanDProtocol::putScanResultFailed(StringRef Reason,
342+
StringRef DiagnosticOutput) {
338343
if (Error E = putResultKind(ErrorResult))
339344
return E;
340-
return putString(Reason);
345+
if (Error E = putString(Reason))
346+
return E;
347+
return putString(DiagnosticOutput);
341348
}
342349

343350
#endif /* LLVM_ON_UNIX */

clang/tools/driver/cc1depscanProtocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class CC1DepScanDProtocol {
140140
llvm::Error getCommand(llvm::StringSaver &Saver, StringRef &WorkingDirectory,
141141
SmallVectorImpl<const char *> &Args);
142142

143-
llvm::Error putScanResultFailed(StringRef Reason);
143+
llvm::Error putScanResultFailed(StringRef Reason, StringRef DiagnosticOutput);
144144
llvm::Error putScanResultSuccess(StringRef RootID,
145145
ArrayRef<const char *> Args,
146146
StringRef DiagnosticOutput);

clang/tools/driver/cc1depscan_main.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static Expected<llvm::cas::CASID> scanAndUpdateCC1InlineWithTool(
370370
static llvm::Expected<llvm::cas::CASID> scanAndUpdateCC1UsingDaemon(
371371
const char *Exec, ArrayRef<const char *> OldArgs,
372372
StringRef WorkingDirectory, SmallVectorImpl<const char *> &NewArgs,
373-
bool &DiagnosticErrorOccurred, StringRef Path,
373+
StringRef &DiagnosticOutput, StringRef Path,
374374
const DepscanSharing &Sharing,
375375
llvm::function_ref<const char *(const Twine &)> SaveArg,
376376
llvm::cas::ObjectStore &CAS) {
@@ -394,7 +394,6 @@ static llvm::Expected<llvm::cas::CASID> scanAndUpdateCC1UsingDaemon(
394394
llvm::BumpPtrAllocator Alloc;
395395
llvm::StringSaver Saver(Alloc);
396396
SmallVector<const char *> RawNewArgs;
397-
StringRef DiagnosticOutput;
398397
CC1DepScanDProtocol::ResultKind Result;
399398
StringRef FailedReason;
400399
StringRef RootID;
@@ -411,11 +410,6 @@ static llvm::Expected<llvm::cas::CASID> scanAndUpdateCC1UsingDaemon(
411410
for (int I = 0, E = RawNewArgs.size(); I != E; ++I)
412411
NewArgs[I] = SaveArg(RawNewArgs[I]);
413412

414-
DiagnosticErrorOccurred = !DiagnosticOutput.empty();
415-
if (DiagnosticErrorOccurred) {
416-
llvm::errs() << DiagnosticOutput;
417-
}
418-
419413
return CAS.parseID(RootID);
420414
}
421415

@@ -510,19 +504,22 @@ static int scanAndUpdateCC1(const char *Exec, ArrayRef<const char *> OldArgs,
510504
if (ProduceIncludeTree)
511505
Sharing.CASArgs.push_back("-fdepscan-include-tree");
512506

507+
StringRef DiagnosticOutput;
513508
bool DiagnosticErrorOccurred = false;
514509
auto ScanAndUpdate = [&]() {
515510
if (std::optional<std::string> DaemonPath =
516511
makeDepscanDaemonPath(Mode, Sharing))
517512
return scanAndUpdateCC1UsingDaemon(Exec, OldArgs, WorkingDirectory,
518-
NewArgs, DiagnosticErrorOccurred,
519-
*DaemonPath, Sharing, SaveArg, *DB);
513+
NewArgs, DiagnosticOutput, *DaemonPath,
514+
Sharing, SaveArg, *DB);
520515
return scanAndUpdateCC1Inline(Exec, OldArgs, WorkingDirectory, NewArgs,
521516
ProduceIncludeTree, DiagnosticErrorOccurred,
522517
SaveArg, CASOpts, DB, Cache);
523518
};
524519
if (llvm::Error E = ScanAndUpdate().moveInto(RootID)) {
525520
Diag.Report(diag::err_cas_depscan_failed) << std::move(E);
521+
if (!DiagnosticOutput.empty())
522+
llvm::errs() << DiagnosticOutput;
526523
return 1;
527524
}
528525
return DiagnosticErrorOccurred;
@@ -959,7 +956,8 @@ int ScanServer::listen() {
959956
*Tool, *DiagsConsumer, &DiagsOS, Argv0, Args, WorkingDirectory,
960957
NewArgs, *CAS, [&](const Twine &T) { return Saver.save(T).data(); });
961958
if (!RootID) {
962-
consumeError(Comms.putScanResultFailed(toString(RootID.takeError())));
959+
consumeError(Comms.putScanResultFailed(toString(RootID.takeError()),
960+
DiagsOS.str()));
963961
SharedOS.applyLocked([&](raw_ostream &OS) {
964962
printScannedCC1(OS);
965963
OS << I << ": failed to create compiler invocation\n";

0 commit comments

Comments
 (0)