Skip to content

Commit 2052c26

Browse files
committed
[NFC] Un-nest DiagnosticState::Behavior
1 parent 8b34d8b commit 2052c26

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,18 @@ namespace swift {
342342
return ActorIsolationVal;
343343
}
344344
};
345-
345+
346+
/// Describes the current behavior to take with a diagnostic
347+
enum class DiagnosticBehavior : uint8_t {
348+
Unspecified,
349+
Ignore,
350+
Note,
351+
Remark,
352+
Warning,
353+
Error,
354+
Fatal,
355+
};
356+
346357
struct DiagnosticFormatOptions {
347358
const std::string OpeningQuotationMark;
348359
const std::string ClosingQuotationMark;
@@ -597,19 +608,6 @@ namespace swift {
597608
/// Class to track, map, and remap diagnostic severity and fatality
598609
///
599610
class DiagnosticState {
600-
public:
601-
/// Describes the current behavior to take with a diagnostic
602-
enum class Behavior : uint8_t {
603-
Unspecified,
604-
Ignore,
605-
Note,
606-
Remark,
607-
Warning,
608-
Error,
609-
Fatal,
610-
};
611-
612-
private:
613611
/// Whether we should continue to emit diagnostics, even after a
614612
/// fatal error
615613
bool showDiagnosticsAfterFatalError = false;
@@ -627,17 +625,17 @@ namespace swift {
627625
bool anyErrorOccurred = false;
628626

629627
/// Track the previous emitted Behavior, useful for notes
630-
Behavior previousBehavior = Behavior::Unspecified;
628+
DiagnosticBehavior previousBehavior = DiagnosticBehavior::Unspecified;
631629

632630
/// Track settable, per-diagnostic state that we store
633-
std::vector<Behavior> perDiagnosticBehavior;
631+
std::vector<DiagnosticBehavior> perDiagnosticBehavior;
634632

635633
public:
636634
DiagnosticState();
637635

638636
/// Figure out the Behavior for the given diagnostic, taking current
639637
/// state such as fatality into account.
640-
Behavior determineBehavior(DiagID id);
638+
DiagnosticBehavior determineBehavior(DiagID id);
641639

642640
bool hadAnyError() const { return anyErrorOccurred; }
643641
bool hasFatalErrorOccurred() const { return fatalErrorOccurred; }
@@ -663,7 +661,7 @@ namespace swift {
663661
}
664662

665663
/// Set per-diagnostic behavior
666-
void setDiagnosticBehavior(DiagID id, Behavior behavior) {
664+
void setDiagnosticBehavior(DiagID id, DiagnosticBehavior behavior) {
667665
perDiagnosticBehavior[(unsigned)id] = behavior;
668666
}
669667

@@ -810,7 +808,7 @@ namespace swift {
810808
}
811809

812810
void ignoreDiagnostic(DiagID id) {
813-
state.setDiagnosticBehavior(id, DiagnosticState::Behavior::Ignore);
811+
state.setDiagnosticBehavior(id, DiagnosticBehavior::Ignore);
814812
}
815813

816814
void resetHadAnyError() {
@@ -1107,8 +1105,8 @@ namespace swift {
11071105

11081106
for (auto &diagnostic : diagnostics) {
11091107
auto behavior = Engine.state.determineBehavior(diagnostic.getID());
1110-
if (behavior == DiagnosticState::Behavior::Fatal ||
1111-
behavior == DiagnosticState::Behavior::Error)
1108+
if (behavior == DiagnosticBehavior::Fatal ||
1109+
behavior == DiagnosticBehavior::Error)
11121110
return true;
11131111
}
11141112

lib/AST/DiagnosticEngine.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ static constexpr auto educationalNotes = _EducationalNotes.value;
137137

138138
DiagnosticState::DiagnosticState() {
139139
// Initialize our per-diagnostic state to default
140-
perDiagnosticBehavior.resize(LocalDiagID::NumDiags, Behavior::Unspecified);
140+
perDiagnosticBehavior.resize(LocalDiagID::NumDiags,
141+
DiagnosticBehavior::Unspecified);
141142
}
142143

143144
static CharSourceRange toCharSourceRange(SourceManager &SM, SourceRange SR) {
@@ -778,20 +779,20 @@ void DiagnosticEngine::formatDiagnosticText(
778779
}
779780
}
780781

781-
static DiagnosticKind toDiagnosticKind(DiagnosticState::Behavior behavior) {
782+
static DiagnosticKind toDiagnosticKind(DiagnosticBehavior behavior) {
782783
switch (behavior) {
783-
case DiagnosticState::Behavior::Unspecified:
784+
case DiagnosticBehavior::Unspecified:
784785
llvm_unreachable("unspecified behavior");
785-
case DiagnosticState::Behavior::Ignore:
786+
case DiagnosticBehavior::Ignore:
786787
llvm_unreachable("trying to map an ignored diagnostic");
787-
case DiagnosticState::Behavior::Error:
788-
case DiagnosticState::Behavior::Fatal:
788+
case DiagnosticBehavior::Error:
789+
case DiagnosticBehavior::Fatal:
789790
return DiagnosticKind::Error;
790-
case DiagnosticState::Behavior::Note:
791+
case DiagnosticBehavior::Note:
791792
return DiagnosticKind::Note;
792-
case DiagnosticState::Behavior::Warning:
793+
case DiagnosticBehavior::Warning:
793794
return DiagnosticKind::Warning;
794-
case DiagnosticState::Behavior::Remark:
795+
case DiagnosticBehavior::Remark:
795796
return DiagnosticKind::Remark;
796797
}
797798

@@ -807,17 +808,17 @@ llvm::cl::opt<bool> AssertOnError("swift-diagnostics-assert-on-error",
807808
llvm::cl::opt<bool> AssertOnWarning("swift-diagnostics-assert-on-warning",
808809
llvm::cl::init(false));
809810

810-
DiagnosticState::Behavior DiagnosticState::determineBehavior(DiagID id) {
811-
auto set = [this](DiagnosticState::Behavior lvl) {
812-
if (lvl == Behavior::Fatal) {
811+
DiagnosticBehavior DiagnosticState::determineBehavior(DiagID id) {
812+
auto set = [this](DiagnosticBehavior lvl) {
813+
if (lvl == DiagnosticBehavior::Fatal) {
813814
fatalErrorOccurred = true;
814815
anyErrorOccurred = true;
815-
} else if (lvl == Behavior::Error) {
816+
} else if (lvl == DiagnosticBehavior::Error) {
816817
anyErrorOccurred = true;
817818
}
818819

819820
assert((!AssertOnError || !anyErrorOccurred) && "We emitted an error?!");
820-
assert((!AssertOnWarning || (lvl != Behavior::Warning)) &&
821+
assert((!AssertOnWarning || (lvl != DiagnosticBehavior::Warning)) &&
821822
"We emitted a warning?!");
822823
previousBehavior = lvl;
823824
return lvl;
@@ -837,39 +838,40 @@ DiagnosticState::Behavior DiagnosticState::determineBehavior(DiagID id) {
837838
// 1) If current state dictates a certain behavior, follow that
838839

839840
// Notes relating to ignored diagnostics should also be ignored
840-
if (previousBehavior == Behavior::Ignore && isNote)
841-
return set(Behavior::Ignore);
841+
if (previousBehavior == DiagnosticBehavior::Ignore && isNote)
842+
return set(DiagnosticBehavior::Ignore);
842843

843844
// Suppress diagnostics when in a fatal state, except for follow-on notes
844845
if (fatalErrorOccurred)
845846
if (!showDiagnosticsAfterFatalError && !isNote)
846-
return set(Behavior::Ignore);
847+
return set(DiagnosticBehavior::Ignore);
847848

848849
// 2) If the user provided a behavior for this specific diagnostic, follow
849850
// that
850851

851-
if (perDiagnosticBehavior[(unsigned)id] != Behavior::Unspecified)
852+
if (perDiagnosticBehavior[(unsigned)id] != DiagnosticBehavior::Unspecified)
852853
return set(perDiagnosticBehavior[(unsigned)id]);
853854

854855
// 3) If the user provided a behavior for this diagnostic's kind, follow
855856
// that
856857
if (diagInfo.kind == DiagnosticKind::Warning) {
857858
if (suppressWarnings)
858-
return set(Behavior::Ignore);
859+
return set(DiagnosticBehavior::Ignore);
859860
if (warningsAsErrors)
860-
return set(Behavior::Error);
861+
return set(DiagnosticBehavior::Error);
861862
}
862863

863864
// 4) Otherwise remap the diagnostic kind
864865
switch (diagInfo.kind) {
865866
case DiagnosticKind::Note:
866-
return set(Behavior::Note);
867+
return set(DiagnosticBehavior::Note);
867868
case DiagnosticKind::Error:
868-
return set(diagInfo.isFatal ? Behavior::Fatal : Behavior::Error);
869+
return set(diagInfo.isFatal ? DiagnosticBehavior::Fatal
870+
: DiagnosticBehavior::Error);
869871
case DiagnosticKind::Warning:
870-
return set(Behavior::Warning);
872+
return set(DiagnosticBehavior::Warning);
871873
case DiagnosticKind::Remark:
872-
return set(Behavior::Remark);
874+
return set(DiagnosticBehavior::Remark);
873875
}
874876

875877
llvm_unreachable("Unhandled DiagnosticKind in switch.");
@@ -910,7 +912,7 @@ static AccessLevel getBufferAccessLevel(const Decl *decl) {
910912
Optional<DiagnosticInfo>
911913
DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
912914
auto behavior = state.determineBehavior(diagnostic.getID());
913-
if (behavior == DiagnosticState::Behavior::Ignore)
915+
if (behavior == DiagnosticBehavior::Ignore)
914916
return None;
915917

916918
// Figure out the source location.

0 commit comments

Comments
 (0)