Skip to content

Commit 727bf80

Browse files
fix: Gracefully handle unexpanded make variable in compilation command (#368)
1 parent 812a4ff commit 727bf80

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

indexer/Worker.cc

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "indexer/AstConsumer.h"
1919
#include "indexer/CliOptions.h"
2020
#include "indexer/CompilationDatabase.h"
21+
#include "indexer/IpcMessages.h"
2122
#include "indexer/Logging.h"
2223
#include "indexer/Preprocessing.h"
2324
#include "indexer/Statistics.h"
@@ -269,6 +270,24 @@ void Worker::sendResult(JobId requestId, IndexJobResult &&result) {
269270
this->flushStreams();
270271
}
271272

273+
Worker::ReceiveStatus Worker::sendRequestAndReceive(
274+
JobId semaRequestId, std::string_view tuMainFilePath,
275+
SemanticAnalysisJobResult &&semaResult, IndexJobRequest &emitIndexRequest) {
276+
this->sendResult(semaRequestId,
277+
IndexJobResult{IndexJob::Kind::SemanticAnalysis,
278+
std::move(semaResult), EmitIndexJobResult{}});
279+
auto status = this->waitForRequest(emitIndexRequest);
280+
if (status != ReceiveStatus::OK) {
281+
return status;
282+
}
283+
if (emitIndexRequest.id == JobId::Shutdown()) {
284+
spdlog::warn("expected EmitIndex request for '{}' but got Shutdown signal",
285+
tuMainFilePath);
286+
std::exit(EXIT_FAILURE);
287+
}
288+
return status;
289+
}
290+
272291
Worker::ReceiveStatus Worker::processTranslationUnitAndRespond(
273292
IndexJobRequest &&semanticAnalysisRequest) {
274293
TRACE_EVENT_BEGIN(
@@ -284,6 +303,7 @@ Worker::ReceiveStatus Worker::processTranslationUnitAndRespond(
284303
Worker::ReceiveStatus innerStatus;
285304
JobId emitIndexRequestId;
286305
unsigned callbackInvoked = 0;
306+
287307
auto callback =
288308
[this, semaRequestId, &innerStatus, &emitIndexRequestId, &tuMainFilePath,
289309
&callbackInvoked](SemanticAnalysisJobResult &&semaResult,
@@ -302,21 +322,12 @@ Worker::ReceiveStatus Worker::processTranslationUnitAndRespond(
302322
}
303323
return true;
304324
}
305-
this->sendResult(semaRequestId,
306-
IndexJobResult{IndexJob::Kind::SemanticAnalysis,
307-
std::move(semaResult),
308-
EmitIndexJobResult{}});
309325
IndexJobRequest emitIndexRequest{};
310-
innerStatus = this->waitForRequest(emitIndexRequest);
326+
innerStatus = this->sendRequestAndReceive(
327+
semaRequestId, tuMainFilePath, std::move(semaResult), emitIndexRequest);
311328
if (innerStatus != ReceiveStatus::OK) {
312329
return false;
313330
}
314-
if (emitIndexRequest.id == JobId::Shutdown()) {
315-
spdlog::warn(
316-
"expected EmitIndex request for '{}' but got Shutdown signal",
317-
tuMainFilePath);
318-
std::exit(EXIT_FAILURE);
319-
}
320331
TRACE_EVENT_BEGIN(tracing::indexing, "worker.emitIndex",
321332
perfetto::Flow::Global(emitIndexRequest.id.traceId()));
322333
ENFORCE(emitIndexRequest.job.kind == IndexJob::Kind::EmitIndex,
@@ -330,16 +341,38 @@ Worker::ReceiveStatus Worker::processTranslationUnitAndRespond(
330341
};
331342
TuIndexingOutput tuIndexingOutput{};
332343
auto &semaDetails = semanticAnalysisRequest.job.semanticAnalysis;
344+
// deliberate copy
345+
std::vector<std::string> commandLine = semaDetails.command.CommandLine;
333346

334347
scip_clang::exceptionContext =
335348
fmt::format("processing {}", semaDetails.command.Filename);
336349
this->processTranslationUnit(std::move(semaDetails), callback,
337350
tuIndexingOutput);
338351
scip_clang::exceptionContext = "";
339352

340-
ENFORCE(callbackInvoked == 1,
341-
"callbackInvoked = {} for TU with main file '{}'", callbackInvoked,
342-
tuMainFilePath);
353+
if (callbackInvoked == 0) {
354+
spdlog::warn("failed to index '{}' as semantic analysis didn't run; retry "
355+
"running scip-clang with --show-compiler-diagnostics",
356+
tuMainFilePath);
357+
for (auto &arg : commandLine) {
358+
if (arg.starts_with("$(") && arg.ends_with(")")) {
359+
spdlog::info(
360+
"hint: found unexpanded '{}' in command line arguments for '{}'",
361+
arg, tuMainFilePath);
362+
}
363+
}
364+
// TODO: Add a different result kind indicating semantic analysis failure.
365+
// Keep going with no-ops so as to not create errors.
366+
IndexJobRequest emitIndexRequest{};
367+
innerStatus = this->sendRequestAndReceive(semaRequestId, tuMainFilePath,
368+
SemanticAnalysisJobResult{},
369+
emitIndexRequest);
370+
emitIndexRequestId = emitIndexRequest.id;
371+
} else {
372+
ENFORCE(callbackInvoked == 1,
373+
"callbackInvoked = {} for TU with main file '{}'", callbackInvoked,
374+
tuMainFilePath);
375+
}
343376
if (innerStatus != ReceiveStatus::OK) {
344377
return innerStatus;
345378
}

indexer/Worker.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ class Worker final {
115115

116116
ReceiveStatus
117117
processTranslationUnitAndRespond(IndexJobRequest &&semanticAnalysisRequest);
118+
119+
ReceiveStatus sendRequestAndReceive(JobId semaRequestId,
120+
std::string_view tuMainFilePath,
121+
SemanticAnalysisJobResult &&,
122+
IndexJobRequest &emitIndexRequest);
123+
118124
void emitIndex(google::protobuf::Message &&scipIndex,
119125
const StdPath &outputPath);
120126

0 commit comments

Comments
 (0)