[Diagnostics] Add InFlightDiagnostic::warnUntilSwiftVersion to limit the

diagnostic behavior to a warning until the specified language version.

This helper can be used to stage in fixes for stricter diagnostics
as warnings until the next major language version.
This commit is contained in:
Holly Borla
2021-07-07 12:50:14 -07:00
parent 7a35c478b6
commit a06c4afcfa
3 changed files with 23 additions and 0 deletions

View File

@@ -22,6 +22,7 @@
#include "swift/AST/DeclNameLoc.h" #include "swift/AST/DeclNameLoc.h"
#include "swift/AST/DiagnosticConsumer.h" #include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/TypeLoc.h" #include "swift/AST/TypeLoc.h"
#include "swift/Basic/Version.h"
#include "swift/Localization/LocalizationFormat.h" #include "swift/Localization/LocalizationFormat.h"
#include "llvm/ADT/BitVector.h" #include "llvm/ADT/BitVector.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
@@ -528,6 +529,12 @@ namespace swift {
/// emitted as a warning, but a note will still be emitted as a note. /// emitted as a warning, but a note will still be emitted as a note.
InFlightDiagnostic &limitBehavior(DiagnosticBehavior limit); InFlightDiagnostic &limitBehavior(DiagnosticBehavior limit);
/// Limit the diagnostic behavior to warning until the specified version.
///
/// This helps stage in fixes for stricter diagnostics as warnings
/// until the next major language version.
InFlightDiagnostic &warnUntilSwiftVersion(unsigned majorVersion);
/// Wraps this diagnostic in another diagnostic. That is, \p wrapper will be /// Wraps this diagnostic in another diagnostic. That is, \p wrapper will be
/// emitted in place of the diagnostic that otherwise would have been /// emitted in place of the diagnostic that otherwise would have been
/// emitted. /// emitted.
@@ -803,6 +810,10 @@ namespace swift {
/// Path to diagnostic documentation directory. /// Path to diagnostic documentation directory.
std::string diagnosticDocumentationPath = ""; std::string diagnosticDocumentationPath = "";
/// The Swift language version. This is used to limit diagnostic behavior
/// until a specific language version, e.g. Swift 6.
version::Version languageVersion;
/// Whether we are actively pretty-printing a declaration as part of /// Whether we are actively pretty-printing a declaration as part of
/// diagnostics. /// diagnostics.
bool IsPrettyPrintingDecl = false; bool IsPrettyPrintingDecl = false;
@@ -865,6 +876,8 @@ namespace swift {
bool isPrettyPrintingDecl() const { return IsPrettyPrintingDecl; } bool isPrettyPrintingDecl() const { return IsPrettyPrintingDecl; }
void setLanguageVersion(version::Version v) { languageVersion = v; }
void setLocalization(StringRef locale, StringRef path) { void setLocalization(StringRef locale, StringRef path) {
assert(!locale.empty()); assert(!locale.empty());
assert(!path.empty()); assert(!path.empty());

View File

@@ -319,6 +319,14 @@ InFlightDiagnostic::limitBehavior(DiagnosticBehavior limit) {
return *this; return *this;
} }
InFlightDiagnostic &
InFlightDiagnostic::warnUntilSwiftVersion(unsigned majorVersion) {
if (!Engine->languageVersion.isVersionAtLeast(majorVersion))
this->limitBehavior(DiagnosticBehavior::Warning);
return *this;
}
InFlightDiagnostic & InFlightDiagnostic &
InFlightDiagnostic::wrapIn(const Diagnostic &wrapper) { InFlightDiagnostic::wrapIn(const Diagnostic &wrapper) {
// Save current active diagnostic into WrappedDiagnostics, ignoring state // Save current active diagnostic into WrappedDiagnostics, ignoring state

View File

@@ -451,6 +451,8 @@ void CompilerInstance::setUpDiagnosticOptions() {
} }
Diagnostics.setDiagnosticDocumentationPath( Diagnostics.setDiagnosticDocumentationPath(
Invocation.getDiagnosticOptions().DiagnosticDocumentationPath); Invocation.getDiagnosticOptions().DiagnosticDocumentationPath);
Diagnostics.setLanguageVersion(
Invocation.getLangOptions().EffectiveLanguageVersion);
if (!Invocation.getDiagnosticOptions().LocalizationCode.empty()) { if (!Invocation.getDiagnosticOptions().LocalizationCode.empty()) {
Diagnostics.setLocalization( Diagnostics.setLocalization(
Invocation.getDiagnosticOptions().LocalizationCode, Invocation.getDiagnosticOptions().LocalizationCode,