Skip to content

Commit a18f72c

Browse files
committed
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.
1 parent 98b1494 commit a18f72c

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,10 @@ namespace swift {
618618

619619
/// Figure out the Behavior for the given diagnostic, taking current
620620
/// state such as fatality into account.
621-
DiagnosticBehavior determineBehavior(const Diagnostic &diag);
621+
DiagnosticBehavior determineBehavior(const Diagnostic &diag) const;
622+
623+
/// Updates the diagnostic state for a diagnostic to emit.
624+
void updateFor(DiagnosticBehavior behavior);
622625

623626
bool hadAnyError() const { return anyErrorOccurred; }
624627
bool hasFatalErrorOccurred() const { return fatalErrorOccurred; }
@@ -646,7 +649,7 @@ namespace swift {
646649

647650
/// Returns a Boolean value indicating whether warnings belonging to the
648651
/// diagnostic group identified by `id` should be escalated to errors.
649-
bool getWarningsAsErrorsForDiagGroupID(DiagGroupID id) {
652+
bool getWarningsAsErrorsForDiagGroupID(DiagGroupID id) const {
650653
return warningsAsErrors[(unsigned)id];
651654
}
652655

lib/AST/DiagnosticEngine.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,8 @@ llvm::cl::opt<bool> AssertOnError("swift-diagnostics-assert-on-error",
12871287
llvm::cl::opt<bool> AssertOnWarning("swift-diagnostics-assert-on-warning",
12881288
llvm::cl::init(false));
12891289

1290-
DiagnosticBehavior DiagnosticState::determineBehavior(const Diagnostic &diag) {
1290+
DiagnosticBehavior
1291+
DiagnosticState::determineBehavior(const Diagnostic &diag) const {
12911292
// We determine how to handle a diagnostic based on the following rules
12921293
// 1) Map the diagnostic to its "intended" behavior, applying the behavior
12931294
// limit for this particular emission
@@ -1334,21 +1335,23 @@ DiagnosticBehavior DiagnosticState::determineBehavior(const Diagnostic &diag) {
13341335
if (suppressRemarks)
13351336
lvl = DiagnosticBehavior::Ignore;
13361337
}
1338+
return lvl;
1339+
}
13371340

1338-
// 5) Update current state for use during the next diagnostic
1339-
if (lvl == DiagnosticBehavior::Fatal) {
1341+
void DiagnosticState::updateFor(DiagnosticBehavior behavior) {
1342+
// Update current state for use during the next diagnostic
1343+
if (behavior == DiagnosticBehavior::Fatal) {
13401344
fatalErrorOccurred = true;
13411345
anyErrorOccurred = true;
1342-
} else if (lvl == DiagnosticBehavior::Error) {
1346+
} else if (behavior == DiagnosticBehavior::Error) {
13431347
anyErrorOccurred = true;
13441348
}
13451349

13461350
ASSERT((!AssertOnError || !anyErrorOccurred) && "We emitted an error?!");
1347-
ASSERT((!AssertOnWarning || (lvl != DiagnosticBehavior::Warning)) &&
1351+
ASSERT((!AssertOnWarning || (behavior != DiagnosticBehavior::Warning)) &&
13481352
"We emitted a warning?!");
13491353

1350-
previousBehavior = lvl;
1351-
return lvl;
1354+
previousBehavior = behavior;
13521355
}
13531356

13541357
void DiagnosticEngine::flushActiveDiagnostic() {
@@ -1393,6 +1396,8 @@ std::optional<DiagnosticInfo>
13931396
DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic,
13941397
bool includeDiagnosticName) {
13951398
auto behavior = state.determineBehavior(diagnostic);
1399+
state.updateFor(behavior);
1400+
13961401
if (behavior == DiagnosticBehavior::Ignore)
13971402
return std::nullopt;
13981403

0 commit comments

Comments
 (0)