Skip to content

Commit 207cbbd

Browse files
authored
DiagnosticHandler: refactor error checking (#75889)
In LLVMContext::diagnose, set `HasErrors` for `DS_Error` so that all derived `DiagnosticHandler` have correct `HasErrors` information. An alternative is to set `HasErrors` in `DiagnosticHandler::handleDiagnostics`, but all derived `handleDiagnostics` would have to call the base function.
1 parent 47413bb commit 207cbbd

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,6 @@ void BackendConsumer::anchor() { }
418418
} // namespace clang
419419

420420
bool ClangDiagnosticHandler::handleDiagnostics(const DiagnosticInfo &DI) {
421-
if (DI.getSeverity() == DS_Error)
422-
HasErrors = true;
423421
BackendCon->DiagnosticHandlerImpl(DI);
424422
return true;
425423
}

llvm/lib/IR/LLVMContext.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,13 @@ void LLVMContext::diagnose(const DiagnosticInfo &DI) {
256256
RS->emit(*OptDiagBase);
257257

258258
// If there is a report handler, use it.
259-
if (pImpl->DiagHandler &&
260-
(!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
261-
pImpl->DiagHandler->handleDiagnostics(DI))
262-
return;
259+
if (pImpl->DiagHandler) {
260+
if (DI.getSeverity() == DS_Error)
261+
pImpl->DiagHandler->HasErrors = true;
262+
if ((!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
263+
pImpl->DiagHandler->handleDiagnostics(DI))
264+
return;
265+
}
263266

264267
if (!isDiagnosticEnabled(DI))
265268
return;

llvm/tools/llc/llc.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,12 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
307307
}
308308

309309
struct LLCDiagnosticHandler : public DiagnosticHandler {
310-
bool *HasError;
311-
LLCDiagnosticHandler(bool *HasErrorPtr) : HasError(HasErrorPtr) {}
312310
bool handleDiagnostics(const DiagnosticInfo &DI) override {
311+
DiagnosticHandler::handleDiagnostics(DI);
313312
if (DI.getKind() == llvm::DK_SrcMgr) {
314313
const auto &DISM = cast<DiagnosticInfoSrcMgr>(DI);
315314
const SMDiagnostic &SMD = DISM.getSMDiag();
316315

317-
if (SMD.getKind() == SourceMgr::DK_Error)
318-
*HasError = true;
319-
320316
SMD.print(nullptr, errs());
321317

322318
// For testing purposes, we print the LocCookie here.
@@ -326,9 +322,6 @@ struct LLCDiagnosticHandler : public DiagnosticHandler {
326322
return true;
327323
}
328324

329-
if (DI.getSeverity() == DS_Error)
330-
*HasError = true;
331-
332325
if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
333326
if (!Remark->isEnabled())
334327
return true;
@@ -413,9 +406,7 @@ int main(int argc, char **argv) {
413406
Context.setDiscardValueNames(DiscardValueNames);
414407

415408
// Set a diagnostic handler that doesn't exit on the first error
416-
bool HasError = false;
417-
Context.setDiagnosticHandler(
418-
std::make_unique<LLCDiagnosticHandler>(&HasError));
409+
Context.setDiagnosticHandler(std::make_unique<LLCDiagnosticHandler>());
419410

420411
Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
421412
setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
@@ -757,9 +748,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
757748

758749
PM.run(*M);
759750

760-
auto HasError =
761-
((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError;
762-
if (*HasError)
751+
if (Context.getDiagHandlerPtr()->HasErrors)
763752
return 1;
764753

765754
// Compare the two outputs and make sure they're the same

0 commit comments

Comments
 (0)