Skip to content

Commit ed0f1f2

Browse files
committed
[NFC] Rework DiagnosticEngine::ignoreDiagnostic()
This feature was implemented with a per-diagnostic table of override DiagnosticBehaviors. That table was able to represent not only that a diagnostic should be ignored, but also many other states that were difficult to integrate with a feature I’m trying to introduce. Simplify the implementation so it can only represent ignoring a diagnostic, which has a much cleaner interaction with the new feature.
1 parent 2052c26 commit ed0f1f2

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/DiagnosticConsumer.h"
2424
#include "swift/AST/TypeLoc.h"
2525
#include "swift/Localization/LocalizationFormat.h"
26+
#include "llvm/ADT/BitVector.h"
2627
#include "llvm/ADT/StringRef.h"
2728
#include "llvm/ADT/StringSet.h"
2829
#include "llvm/Support/Allocator.h"
@@ -627,8 +628,8 @@ namespace swift {
627628
/// Track the previous emitted Behavior, useful for notes
628629
DiagnosticBehavior previousBehavior = DiagnosticBehavior::Unspecified;
629630

630-
/// Track settable, per-diagnostic state that we store
631-
std::vector<DiagnosticBehavior> perDiagnosticBehavior;
631+
/// Track which diagnostics should be ignored.
632+
llvm::BitVector ignoredDiagnostics;
632633

633634
public:
634635
DiagnosticState();
@@ -660,9 +661,9 @@ namespace swift {
660661
fatalErrorOccurred = false;
661662
}
662663

663-
/// Set per-diagnostic behavior
664-
void setDiagnosticBehavior(DiagID id, DiagnosticBehavior behavior) {
665-
perDiagnosticBehavior[(unsigned)id] = behavior;
664+
/// Set whether a diagnostic should be ignored.
665+
void setIgnoredDiagnostic(DiagID id, bool ignored) {
666+
ignoredDiagnostics[(unsigned)id] = ignored;
666667
}
667668

668669
private:
@@ -808,7 +809,7 @@ namespace swift {
808809
}
809810

810811
void ignoreDiagnostic(DiagID id) {
811-
state.setDiagnosticBehavior(id, DiagnosticBehavior::Ignore);
812+
state.setIgnoredDiagnostic(id, true);
812813
}
813814

814815
void resetHadAnyError() {

lib/AST/DiagnosticEngine.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ static constexpr EducationalNotes<LocalDiagID::NumDiags> _EducationalNotes = Edu
136136
static constexpr auto educationalNotes = _EducationalNotes.value;
137137

138138
DiagnosticState::DiagnosticState() {
139-
// Initialize our per-diagnostic state to default
140-
perDiagnosticBehavior.resize(LocalDiagID::NumDiags,
141-
DiagnosticBehavior::Unspecified);
139+
// Initialize our ignored diagnostics to default
140+
ignoredDiagnostics.resize(LocalDiagID::NumDiags);
142141
}
143142

144143
static CharSourceRange toCharSourceRange(SourceManager &SM, SourceRange SR) {
@@ -826,8 +825,7 @@ DiagnosticBehavior DiagnosticState::determineBehavior(DiagID id) {
826825

827826
// We determine how to handle a diagnostic based on the following rules
828827
// 1) If current state dictates a certain behavior, follow that
829-
// 2) If the user provided a behavior for this specific diagnostic, follow
830-
// that
828+
// 2) If the user ignored this specific diagnostic, follow that
831829
// 3) If the user provided a behavior for this diagnostic's kind, follow
832830
// that
833831
// 4) Otherwise remap the diagnostic kind
@@ -846,11 +844,9 @@ DiagnosticBehavior DiagnosticState::determineBehavior(DiagID id) {
846844
if (!showDiagnosticsAfterFatalError && !isNote)
847845
return set(DiagnosticBehavior::Ignore);
848846

849-
// 2) If the user provided a behavior for this specific diagnostic, follow
850-
// that
851-
852-
if (perDiagnosticBehavior[(unsigned)id] != DiagnosticBehavior::Unspecified)
853-
return set(perDiagnosticBehavior[(unsigned)id]);
847+
// 2) If the user ignored this specific diagnostic, follow that
848+
if (ignoredDiagnostics[(unsigned)diag.getID()])
849+
return set(DiagnosticBehavior::Ignore);
854850

855851
// 3) If the user provided a behavior for this diagnostic's kind, follow
856852
// that

0 commit comments

Comments
 (0)