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.
This commit is contained in:
Hamish Knight
2025-07-02 17:46:10 +01:00
parent 98b1494b0c
commit a18f72c29a
2 changed files with 17 additions and 9 deletions

View File

@@ -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];
}

View File

@@ -1287,7 +1287,8 @@ llvm::cl::opt<bool> AssertOnError("swift-diagnostics-assert-on-error",
llvm::cl::opt<bool> 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<DiagnosticInfo>
DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic,
bool includeDiagnosticName) {
auto behavior = state.determineBehavior(diagnostic);
state.updateFor(behavior);
if (behavior == DiagnosticBehavior::Ignore)
return std::nullopt;