From a18f72c29a521c6eb2fadc72e78d12923cc3f43c Mon Sep 17 00:00:00 2001 From: Hamish Knight Date: Wed, 2 Jul 2025 17:46:10 +0100 Subject: [PATCH] Constify `DiagnosticState::determineBehavior` Split out the state mutation into a new `updateFor` function that we call for diagnostic emission, allowing `DiagnosticTransaction::hasErrors` to query the behavior without mutating any state. --- include/swift/AST/DiagnosticEngine.h | 7 +++++-- lib/AST/DiagnosticEngine.cpp | 19 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/include/swift/AST/DiagnosticEngine.h b/include/swift/AST/DiagnosticEngine.h index e926e97b012..ac0d1a27400 100644 --- a/include/swift/AST/DiagnosticEngine.h +++ b/include/swift/AST/DiagnosticEngine.h @@ -618,7 +618,10 @@ namespace swift { /// Figure out the Behavior for the given diagnostic, taking current /// state such as fatality into account. - DiagnosticBehavior determineBehavior(const Diagnostic &diag); + DiagnosticBehavior determineBehavior(const Diagnostic &diag) const; + + /// Updates the diagnostic state for a diagnostic to emit. + void updateFor(DiagnosticBehavior behavior); bool hadAnyError() const { return anyErrorOccurred; } bool hasFatalErrorOccurred() const { return fatalErrorOccurred; } @@ -646,7 +649,7 @@ namespace swift { /// Returns a Boolean value indicating whether warnings belonging to the /// diagnostic group identified by `id` should be escalated to errors. - bool getWarningsAsErrorsForDiagGroupID(DiagGroupID id) { + bool getWarningsAsErrorsForDiagGroupID(DiagGroupID id) const { return warningsAsErrors[(unsigned)id]; } diff --git a/lib/AST/DiagnosticEngine.cpp b/lib/AST/DiagnosticEngine.cpp index c6db906680f..c2c2e23871e 100644 --- a/lib/AST/DiagnosticEngine.cpp +++ b/lib/AST/DiagnosticEngine.cpp @@ -1287,7 +1287,8 @@ llvm::cl::opt AssertOnError("swift-diagnostics-assert-on-error", llvm::cl::opt AssertOnWarning("swift-diagnostics-assert-on-warning", llvm::cl::init(false)); -DiagnosticBehavior DiagnosticState::determineBehavior(const Diagnostic &diag) { +DiagnosticBehavior +DiagnosticState::determineBehavior(const Diagnostic &diag) const { // We determine how to handle a diagnostic based on the following rules // 1) Map the diagnostic to its "intended" behavior, applying the behavior // limit for this particular emission @@ -1334,21 +1335,23 @@ DiagnosticBehavior DiagnosticState::determineBehavior(const Diagnostic &diag) { if (suppressRemarks) lvl = DiagnosticBehavior::Ignore; } + return lvl; +} - // 5) Update current state for use during the next diagnostic - if (lvl == DiagnosticBehavior::Fatal) { +void DiagnosticState::updateFor(DiagnosticBehavior behavior) { + // Update current state for use during the next diagnostic + if (behavior == DiagnosticBehavior::Fatal) { fatalErrorOccurred = true; anyErrorOccurred = true; - } else if (lvl == DiagnosticBehavior::Error) { + } else if (behavior == DiagnosticBehavior::Error) { anyErrorOccurred = true; } ASSERT((!AssertOnError || !anyErrorOccurred) && "We emitted an error?!"); - ASSERT((!AssertOnWarning || (lvl != DiagnosticBehavior::Warning)) && + ASSERT((!AssertOnWarning || (behavior != DiagnosticBehavior::Warning)) && "We emitted a warning?!"); - previousBehavior = lvl; - return lvl; + previousBehavior = behavior; } void DiagnosticEngine::flushActiveDiagnostic() { @@ -1393,6 +1396,8 @@ std::optional DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic, bool includeDiagnosticName) { auto behavior = state.determineBehavior(diagnostic); + state.updateFor(behavior); + if (behavior == DiagnosticBehavior::Ignore) return std::nullopt;