@@ -378,6 +378,71 @@ namespace swift {
378
378
return fixItReplaceChars (Start, End, {});
379
379
}
380
380
};
381
+
382
+ // / \brief Class to track, map, and remap diagnostic severity and fatality
383
+ // /
384
+ class DiagnosticState {
385
+ public:
386
+ // / \brief Describes the current behavior to take with a diagnostic
387
+ enum class Behavior {
388
+ Unspecified,
389
+ Ignore,
390
+ Note,
391
+ Warn,
392
+ Err,
393
+ Fatal,
394
+ };
395
+
396
+ private:
397
+ // / \brief Whether we should continue to emit diagnostics, even after a
398
+ // / fatal error
399
+ bool showDiagnosticsAfterFatalError = false ;
400
+
401
+ // / \brief Don't emit any warnings
402
+ bool ignoreAllWarnings = false ;
403
+
404
+ // / \brief Whether a fatal error has occurred
405
+ bool fatalErrorOccurred = false ;
406
+
407
+ // / \brief Whether any error diagnostics have been emitted.
408
+ bool anyErrorOccurred = false ;
409
+
410
+ // / \brief Track the previous emitted Behavior, useful for notes
411
+ Behavior previousBehavior = Behavior::Unspecified;
412
+
413
+ public:
414
+ DiagnosticState () {}
415
+
416
+ // / \brief Figure out the Behavior for the given diagnostic
417
+ Behavior getBehavior (const Diagnostic &);
418
+
419
+ bool hadAnyError () const { return anyErrorOccurred; }
420
+ bool hasFatalErrorOccurred () const { return fatalErrorOccurred; }
421
+
422
+ void setShowDiagnosticsAfterFatalError (bool val = true ) {
423
+ showDiagnosticsAfterFatalError = val;
424
+ }
425
+
426
+ // / \brief Whether to skip emitting warnings
427
+ void setIgnoreAllWarnings (bool val) { ignoreAllWarnings = val; }
428
+ bool getIgnoreAllWarnings () const { return ignoreAllWarnings; }
429
+
430
+ void resetHadAnyError () {
431
+ anyErrorOccurred = false ;
432
+ fatalErrorOccurred = false ;
433
+ }
434
+
435
+ private:
436
+ // / \returns true if diagnostic is marked as fatal.
437
+ bool isDiagnosticFatal (DiagID ID) const ;
438
+
439
+ // Make the state movable only
440
+ DiagnosticState (const DiagnosticState &) = delete ;
441
+ const DiagnosticState &operator =(const DiagnosticState &) = delete ;
442
+
443
+ DiagnosticState (DiagnosticState &&) = default ;
444
+ DiagnosticState &operator =(DiagnosticState &&) = default ;
445
+ };
381
446
382
447
// / \brief Class responsible for formatting diagnostics and presenting them
383
448
// / to the user.
@@ -390,19 +455,7 @@ namespace swift {
390
455
// / emitting diagnostics.
391
456
SmallVector<DiagnosticConsumer *, 2 > Consumers;
392
457
393
- // / HadAnyError - True if any error diagnostics have been emitted.
394
- bool HadAnyError;
395
-
396
- enum class FatalErrorState {
397
- None,
398
- JustEmitted,
399
- Fatal
400
- };
401
-
402
- // / Sticky flag set to \c true when a fatal error is emitted.
403
- FatalErrorState FatalState = FatalErrorState::None;
404
-
405
- bool ShowDiagnosticsAfterFatalError = false ;
458
+ DiagnosticState state;
406
459
407
460
// / \brief The currently active diagnostic, if there is one.
408
461
Optional<Diagnostic> ActiveDiagnostic;
@@ -424,25 +477,28 @@ namespace swift {
424
477
425
478
public:
426
479
explicit DiagnosticEngine (SourceManager &SourceMgr)
427
- : SourceMgr(SourceMgr), HadAnyError( false ), ActiveDiagnostic() {
480
+ : SourceMgr(SourceMgr), state( ), ActiveDiagnostic() {
428
481
}
429
482
430
483
// / hadAnyError - return true if any *error* diagnostics have been emitted.
431
- bool hadAnyError () const {
432
- return HadAnyError;
433
- }
484
+ bool hadAnyError () const { return state.hadAnyError (); }
434
485
435
486
bool hasFatalErrorOccurred () const {
436
- return FatalState != FatalErrorState::None ;
487
+ return state. hasFatalErrorOccurred () ;
437
488
}
438
489
439
- void setShowDiagnosticsAfterFatalError (bool Val = true ) {
440
- ShowDiagnosticsAfterFatalError = Val;
490
+ void setShowDiagnosticsAfterFatalError (bool val = true ) {
491
+ state.setShowDiagnosticsAfterFatalError (val);
492
+ }
493
+
494
+ // / \brief Whether to skip emitting warnings
495
+ void setIgnoreAllWarnings (bool val) { state.setIgnoreAllWarnings (val); }
496
+ bool getIgnoreAllWarnings () const {
497
+ return state.getIgnoreAllWarnings ();
441
498
}
442
499
443
500
void resetHadAnyError () {
444
- HadAnyError = false ;
445
- FatalState = FatalErrorState::None;
501
+ state.resetHadAnyError ();
446
502
}
447
503
448
504
// / \brief Add an additional DiagnosticConsumer to receive diagnostics.
@@ -572,10 +628,7 @@ namespace swift {
572
628
573
629
// / \returns true if diagnostic is marked with PointsToFirstBadToken
574
630
// / option.
575
- bool isDiagnosticPointsToFirstBadToken (DiagID ID) const ;
576
-
577
- // / \returns true if diagnostic is marked as fatal.
578
- bool isDiagnosticFatal (DiagID ID) const ;
631
+ bool isDiagnosticPointsToFirstBadToken (DiagID id) const ;
579
632
580
633
private:
581
634
// / \brief Flush the active diagnostic.
0 commit comments