2525#include " swift/Basic/PrintDiagnosticNamesMode.h"
2626#include " swift/Basic/Statistic.h"
2727#include " swift/Basic/Version.h"
28- #include " swift/Basic/WarningAsErrorRule .h"
28+ #include " swift/Basic/WarningGroupBehaviorRule .h"
2929#include " swift/Localization/LocalizationFormat.h"
3030#include " llvm/ADT/BitVector.h"
3131#include " llvm/ADT/StringRef.h"
@@ -628,13 +628,10 @@ namespace swift {
628628 // / Don't emit any remarks
629629 bool suppressRemarks = false ;
630630
631- // / A mapping from `DiagGroupID` identifiers to Boolean values indicating
632- // / whether warnings belonging to the respective diagnostic groups should be
633- // / escalated to errors.
634- llvm::BitVector warningsAsErrors;
635-
636- // / Track which diagnostic group (`DiagGroupID`) warnings should be ignored.
637- llvm::BitVector ignoredDiagnosticGroups;
631+ // / A mapping from `DiagGroupID` identifiers to `WarningGroupBehavior`
632+ // / values indicating how warnings belonging to the respective diagnostic groups
633+ // / should be emitted.
634+ std::unordered_map<DiagGroupID, WarningGroupBehavior> warningGroupBehaviorMap;
638635
639636 // / For compiler-internal purposes only, track which diagnostics should
640637 // / be ignored completely. For example, this is used by LLDB to
@@ -657,7 +654,15 @@ namespace swift {
657654
658655 // / Figure out the Behavior for the given diagnostic, taking current
659656 // / state such as fatality into account.
660- DiagnosticBehavior determineBehavior (const Diagnostic &diag) const ;
657+ DiagnosticBehavior determineBehavior (const Diagnostic &diag,
658+ SourceManager &sourceMgr) const ;
659+
660+ // / If this diagnostic is a warning belonging to a diagnostic group,
661+ // / figure out if there is a source-level (`@warn`) control for this group
662+ // / for this diagnostic's source location.
663+ std::optional<DiagnosticBehavior>
664+ determineUserControlledWarningBehavior (const Diagnostic &diag,
665+ SourceManager &sourceMgr) const ;
661666
662667 // / Updates the diagnostic state for a diagnostic to emit.
663668 void updateFor (DiagnosticBehavior behavior);
@@ -684,43 +689,24 @@ namespace swift {
684689 void setSuppressRemarks (bool val) { suppressRemarks = val; }
685690 bool getSuppressRemarks () const { return suppressRemarks; }
686691
687- // / Sets whether warnings belonging to the diagnostic group identified by
688- // / `id` should be escalated to errors.
689- void setWarningsAsErrorsForDiagGroupID (DiagGroupID id, bool value) {
690- warningsAsErrors[(unsigned )id] = value;
691- }
692-
693- // / Returns a Boolean value indicating whether warnings belonging to the
694- // / diagnostic group identified by `id` should be escalated to errors.
695- bool getWarningsAsErrorsForDiagGroupID (DiagGroupID id) const {
696- return warningsAsErrors[(unsigned )id];
697- }
692+ // / Configure the command-line warning group handling
693+ // / rules (`-Werrr`,`-Wwarning`,`-warnings-as-errors`)
694+ void setWarningGroupControlRules (
695+ const llvm::SmallVector<WarningGroupBehaviorRule, 4 > &rules);
698696
699- // / Whether all warnings should be upgraded to errors or not.
700- void setAllWarningsAsErrors (bool value) {
701- // This works as intended because every diagnostic belongs to either a
702- // custom group or the top-level `DiagGroupID::no_group`, which is also
703- // a group.
704- if (value) {
705- warningsAsErrors.set ();
706- } else {
707- warningsAsErrors.reset ();
708- }
697+ const std::unordered_map<DiagGroupID, WarningGroupBehavior> &
698+ getWarningGroupBehaviorControlMap () const {
699+ return warningGroupBehaviorMap;
709700 }
710701
711702 void resetHadAnyError () {
712703 anyErrorOccurred = false ;
713704 fatalErrorOccurred = false ;
714705 }
715706
716- // / Set whether a diagnostic group should be ignored.
717- void setIgnoredDiagnosticGroup (DiagGroupID id, bool ignored) {
718- ignoredDiagnosticGroups[(unsigned )id] = ignored;
719- }
720-
721707 // / Query whether a specific diagnostic group is ignored.
722708 bool isIgnoredDiagnosticGroup (DiagGroupID id) const {
723- return ignoredDiagnosticGroups[( unsigned )id] ;
709+ return warningGroupBehaviorMap. at (id) == WarningGroupBehavior::Ignored ;
724710 }
725711
726712 // / Set a specific diagnostic to be ignored by the compiler.
@@ -738,11 +724,10 @@ namespace swift {
738724 std::swap (suppressWarnings, other.suppressWarnings );
739725 std::swap (suppressNotes, other.suppressNotes );
740726 std::swap (suppressRemarks, other.suppressRemarks );
741- std::swap (warningsAsErrors , other.warningsAsErrors );
727+ std::swap (warningGroupBehaviorMap , other.warningGroupBehaviorMap );
742728 std::swap (fatalErrorOccurred, other.fatalErrorOccurred );
743729 std::swap (anyErrorOccurred, other.anyErrorOccurred );
744730 std::swap (previousBehavior, other.previousBehavior );
745- std::swap (ignoredDiagnosticGroups, other.ignoredDiagnosticGroups );
746731 }
747732
748733 private:
@@ -951,7 +936,15 @@ namespace swift {
951936 // / Rules are applied in order they appear in the vector.
952937 // / In case the vector contains rules affecting the same diagnostic ID
953938 // / the last rule wins.
954- void setWarningsAsErrorsRules (const std::vector<WarningAsErrorRule> &rules);
939+ void setWarningGroupControlRules (
940+ const llvm::SmallVector<WarningGroupBehaviorRule, 4 > &rules) {
941+ state.setWarningGroupControlRules (rules);
942+ }
943+
944+ const std::unordered_map<DiagGroupID, WarningGroupBehavior> &
945+ getWarningGroupBehaviorControlMap () const {
946+ return state.getWarningGroupBehaviorControlMap ();
947+ }
955948
956949 // / Whether to print diagnostic names after their messages
957950 void setPrintDiagnosticNamesMode (PrintDiagnosticNamesMode val) {
@@ -1366,7 +1359,8 @@ namespace swift {
13661359 Engine.TentativeDiagnostics .end ());
13671360
13681361 for (auto &diagnostic : diagnostics) {
1369- auto behavior = Engine.state .determineBehavior (diagnostic.Diag );
1362+ auto behavior = Engine.state .determineBehavior (diagnostic.Diag ,
1363+ Engine.SourceMgr );
13701364 if (behavior == DiagnosticBehavior::Fatal ||
13711365 behavior == DiagnosticBehavior::Error)
13721366 return true ;
0 commit comments