Skip to content

Commit

Permalink
DiagnosticHandler: refactor error checking (#75889)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MaskRay committed Dec 20, 2023
1 parent 47413bb commit 207cbbd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
2 changes: 0 additions & 2 deletions clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,6 @@ void BackendConsumer::anchor() { }
} // namespace clang

bool ClangDiagnosticHandler::handleDiagnostics(const DiagnosticInfo &DI) {
if (DI.getSeverity() == DS_Error)
HasErrors = true;
BackendCon->DiagnosticHandlerImpl(DI);
return true;
}
Expand Down
11 changes: 7 additions & 4 deletions llvm/lib/IR/LLVMContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,13 @@ void LLVMContext::diagnose(const DiagnosticInfo &DI) {
RS->emit(*OptDiagBase);

// If there is a report handler, use it.
if (pImpl->DiagHandler &&
(!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
pImpl->DiagHandler->handleDiagnostics(DI))
return;
if (pImpl->DiagHandler) {
if (DI.getSeverity() == DS_Error)
pImpl->DiagHandler->HasErrors = true;
if ((!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
pImpl->DiagHandler->handleDiagnostics(DI))
return;
}

if (!isDiagnosticEnabled(DI))
return;
Expand Down
17 changes: 3 additions & 14 deletions llvm/tools/llc/llc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,12 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
}

struct LLCDiagnosticHandler : public DiagnosticHandler {
bool *HasError;
LLCDiagnosticHandler(bool *HasErrorPtr) : HasError(HasErrorPtr) {}
bool handleDiagnostics(const DiagnosticInfo &DI) override {
DiagnosticHandler::handleDiagnostics(DI);
if (DI.getKind() == llvm::DK_SrcMgr) {
const auto &DISM = cast<DiagnosticInfoSrcMgr>(DI);
const SMDiagnostic &SMD = DISM.getSMDiag();

if (SMD.getKind() == SourceMgr::DK_Error)
*HasError = true;

SMD.print(nullptr, errs());

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

if (DI.getSeverity() == DS_Error)
*HasError = true;

if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
if (!Remark->isEnabled())
return true;
Expand Down Expand Up @@ -413,9 +406,7 @@ int main(int argc, char **argv) {
Context.setDiscardValueNames(DiscardValueNames);

// Set a diagnostic handler that doesn't exit on the first error
bool HasError = false;
Context.setDiagnosticHandler(
std::make_unique<LLCDiagnosticHandler>(&HasError));
Context.setDiagnosticHandler(std::make_unique<LLCDiagnosticHandler>());

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

PM.run(*M);

auto HasError =
((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError;
if (*HasError)
if (Context.getDiagHandlerPtr()->HasErrors)
return 1;

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

0 comments on commit 207cbbd

Please sign in to comment.