Skip to content

[Diagnostics] Add -no-warning-as-error to except a specific warning from being treated as an error #74466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/ASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ struct BridgedPatternBindingEntry {
// NOTE: This must be the same underlying value as C++ 'swift::DiagID' defined
// in 'DiagnosticList.cpp'.
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagID : uint32_t {
#define DIAG(KIND, ID, Options, Text, Signature) BridgedDiagID_##ID,
#define DIAG(KIND, ID, Group, Options, Text, Signature) BridgedDiagID_##ID,
#include "swift/AST/DiagnosticsAll.def"
};

Expand Down
67 changes: 67 additions & 0 deletions include/swift/AST/DefineDiagnosticGroupsMacros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//===--- DefineDiagnosticGroupsMacros.def -----------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines macros defining diagnostic groups.
//
//===----------------------------------------------------------------------===//

// Define macros
#if defined(DEFINE_DIAGNOSTIC_GROUPS_MACROS) && \
!defined(UNDEFINE_DIAGNOSTIC_GROUPS_MACROS)

#undef DEFINE_DIAGNOSTIC_GROUPS_MACROS

#if !(defined(GROUP) || defined(GROUP_LINK))
#error No reqired macros defined. Define at least one of the following macros: GROUP(Name, DocsFile), GROUP_LINK(Parent, Child)
#endif

// GROUP macro:
// Declares a diagnostic group.
// Parameters:
// Name - group name as it appears in DiagGroupID enum and DiagGroupInfo.name
// DocsFile - file with a human readable description for the group located in
// userdocs/diagnostic_groups
#ifndef GROUP
#define GROUP(Name, DocsFile)
#endif

// GROUP_LINK macro:
// Establishes an edge in the diagnostic group graph between
// a supergroup(Parent) and its subgroup(Child).
// Parameters:
// Parent - parent group name
// Child - child group name
#ifndef GROUP_LINK
#define GROUP_LINK(Parent, Child)
#endif

// Undefine macros
#elif defined(UNDEFINE_DIAGNOSTIC_GROUPS_MACROS) && \
!defined(DEFINE_DIAGNOSTIC_GROUPS_MACROS)

#undef UNDEFINE_DIAGNOSTIC_GROUPS_MACROS

#ifdef GROUP
#undef GROUP
#else
#error Trying to undefine the diagnostic groups macros, but GROUP macro wasn't defined
#endif

#ifdef GROUP_LINK
#undef GROUP_LINK
#else
#error Trying to undefine the diagnostic groups macros, but GROUP_LINK macro wasn't defined
#endif

#else
#error Invalid DefineDiagnosticGroupsMacros.h inclusion
#endif
26 changes: 19 additions & 7 deletions include/swift/AST/DefineDiagnosticMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,39 @@
// Define macros
#ifdef DEFINE_DIAGNOSTIC_MACROS

#if !(defined(DIAG) || (defined(ERROR) && defined(WARNING) && defined(NOTE) && \
defined(REMARK)))
#error Must define either DIAG or the set {ERROR,WARNING,NOTE,REMARK}
#if !(defined(DIAG) || (defined(GROUPED_ERROR) && defined(GROUPED_WARNING) && \
defined(NOTE) && defined(REMARK)))
#error Must define either DIAG or the set {GROUPED_ERROR,GROUPED_WARNING,NOTE,REMARK}
#endif

#ifndef GROUPED_ERROR
#define GROUPED_ERROR(ID, Group, Options, Text, Signature) \
DIAG(ERROR, ID, Group, Options, Text, Signature)
#endif

#ifndef ERROR
#define ERROR(ID, Options, Text, Signature) \
DIAG(ERROR, ID, Options, Text, Signature)
GROUPED_ERROR(ID, no_group, Options, Text, Signature)
#endif

#ifndef GROUPED_WARNING
#define GROUPED_WARNING(ID, Group, Options, Text, Signature) \
DIAG(WARNING, ID, Group, Options, Text, Signature)
#endif

#ifndef WARNING
#define WARNING(ID, Options, Text, Signature) \
DIAG(WARNING, ID, Options, Text, Signature)
GROUPED_WARNING(ID, no_group, Options, Text, Signature)
#endif

#ifndef NOTE
#define NOTE(ID, Options, Text, Signature) \
DIAG(NOTE, ID, Options, Text, Signature)
DIAG(NOTE, ID, no_group, Options, Text, Signature)
#endif

#ifndef REMARK
#define REMARK(ID, Options, Text, Signature) \
DIAG(REMARK, ID, Options, Text, Signature)
DIAG(REMARK, ID, no_group, Options, Text, Signature)
#endif

#ifndef FIXIT
Expand All @@ -61,7 +71,9 @@
#undef REMARK
#undef NOTE
#undef WARNING
#undef GROUPED_WARNING
#undef ERROR
#undef GROUPED_ERROR
#undef FIXIT

#endif
Expand Down
36 changes: 26 additions & 10 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "swift/AST/TypeLoc.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/Version.h"
#include "swift/Basic/WarningAsErrorRule.h"
#include "swift/Localization/LocalizationFormat.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -851,8 +852,8 @@ namespace swift {
/// Don't emit any remarks
bool suppressRemarks = false;

/// Emit all warnings as errors
bool warningsAsErrors = false;
/// Treat these warnings as errors. Indicies here corespond to DiagID enum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typos "indicies" (for indices) and "corespond"

llvm::BitVector warningsAsErrors;

/// Whether a fatal error has occurred
bool fatalErrorOccurred = false;
Expand Down Expand Up @@ -893,9 +894,22 @@ namespace swift {
void setSuppressRemarks(bool val) { suppressRemarks = val; }
bool getSuppressRemarks() const { return suppressRemarks; }

/// Whether to treat warnings as errors
void setWarningsAsErrors(bool val) { warningsAsErrors = val; }
bool getWarningsAsErrors() const { return warningsAsErrors; }
/// Whether a warning should be upgraded to an error or not
void setWarningAsErrorForDiagID(DiagID id, bool value) {
warningsAsErrors[(unsigned)id] = value;
}
bool getWarningAsErrorForDiagID(DiagID id) {
return warningsAsErrors[(unsigned)id];
}

/// Whether all warnings should be upgraded to errors or not
void setAllWarningsAsErrors(bool value) {
if (value) {
warningsAsErrors.set();
} else {
warningsAsErrors.reset();
}
}

void resetHadAnyError() {
anyErrorOccurred = false;
Expand Down Expand Up @@ -1105,11 +1119,13 @@ namespace swift {
return state.getSuppressRemarks();
}

/// Whether to treat warnings as errors
void setWarningsAsErrors(bool val) { state.setWarningsAsErrors(val); }
bool getWarningsAsErrors() const {
return state.getWarningsAsErrors();
}
/// Apply rules specifing what warnings should or shouldn't be treated as
/// errors. For group rules the string is either a group name defined by
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no "or" to go with the "either here".

/// DiagnosticGroups.def
/// Rules are applied in order they appear in the vector.
/// In case the vector contains rules affecting the same diagnostic ID
/// the last rule wins.
void setWarningsAsErrorsRules(const std::vector<WarningAsErrorRule> &rules);

/// Whether to print diagnostic names after their messages
void setPrintDiagnosticNames(bool val) {
Expand Down
33 changes: 33 additions & 0 deletions include/swift/AST/DiagnosticGroups.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===--- DiagnosticGroups.def - Diagnostic Groups ---------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines diagnostic groups and links between them.
//
//===----------------------------------------------------------------------===//

#define DEFINE_DIAGNOSTIC_GROUPS_MACROS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not critical now, but at some point we'd want a big comment here documenting what it means to define a new group.

#include "swift/AST/DefineDiagnosticGroupsMacros.h"

// GROUP(Name, DocsFile)
// GROUP_LINK(Parent, Child)

GROUP(no_group, "")

GROUP(deprecated, "deprecated.md")
GROUP_LINK(deprecated, availability_deprecated)

GROUP(availability_deprecated, "availability_deprecated.md")

GROUP(unknown_warning_group, "unknown_warning_group.md")

#define UNDEFINE_DIAGNOSTIC_GROUPS_MACROS
#include "swift/AST/DefineDiagnosticGroupsMacros.h"
59 changes: 59 additions & 0 deletions include/swift/AST/DiagnosticGroups.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===--- DiagnosticGroups.h - Diagnostic Groups -----------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the diagnostic groups enumaration, group graph
// and auxilary functions.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_DIAGNOSTICGROUPS_H
#define SWIFT_DIAGNOSTICGROUPS_H

#include "swift/AST/DiagnosticList.h"
#include "llvm/ADT/ArrayRef.h"
#include <array>
#include <string_view>
#include <unordered_map>

namespace swift {

enum class DiagGroupID : uint16_t {
#define GROUP(Name, Version) Name,
#include "swift/AST/DiagnosticGroups.def"
};

constexpr const auto DiagGroupsCount = [] {
size_t count = 0;
#define GROUP(Name, Version) count++;
#include "DiagnosticGroups.def"
return count;
}();

struct DiagGroupInfo {
DiagGroupID id;
std::string_view name;
std::string_view version;
llvm::ArrayRef<DiagGroupID> supergroups;
llvm::ArrayRef<DiagGroupID> subgroups;
llvm::ArrayRef<DiagID> diagnostics;

void traverseDepthFirst(
llvm::function_ref<void(const DiagGroupInfo &)> func) const;
};

extern const std::array<DiagGroupInfo, DiagGroupsCount> diagnosticGroupsInfo;
const DiagGroupInfo &getDiagGroupInfoByID(DiagGroupID id);
std::optional<DiagGroupID> getDiagGroupIDByName(std::string_view name);

} // end namespace swift

#endif /* SWIFT_DIAGNOSTICGROUPS_H */
43 changes: 43 additions & 0 deletions include/swift/AST/DiagnosticList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===--- DiagnosticList.h - Diagnostic Definitions --------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines all of the diagnostics emitted by Swift.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_DIAGNOSTICLIST_H
#define SWIFT_DIAGNOSTICLIST_H

#include <cstdint>

namespace swift {

/// Enumeration describing all of possible diagnostics.
///
/// Each of the diagnostics described in Diagnostics.def has an entry in
/// this enumeration type that uniquely identifies it.
enum class DiagID : uint32_t {
#define DIAG(KIND, ID, Group, Options, Text, Signature) ID,
#include "swift/AST/DiagnosticsAll.def"
};
static_assert(static_cast<uint32_t>(swift::DiagID::invalid_diagnostic) == 0,
"0 is not the invalid diagnostic ID");

enum class FixItID : uint32_t {
#define DIAG(KIND, ID, Group, Options, Text, Signature)
#define FIXIT(ID, Text, Signature) ID,
#include "swift/AST/DiagnosticsAll.def"
};

} // end namespace swift

#endif /* SWIFT_DIAGNOSTICLIST_H */
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
namespace swift {
namespace diag {
// Declare common diagnostics objects with their appropriate types.
#define DIAG(KIND,ID,Options,Text,Signature) \
extern detail::DiagWithArguments<void Signature>::type ID;
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
extern detail::DiagWithArguments<void Signature>::type ID;
#include "DiagnosticsClangImporter.def"
}
}
Expand Down
8 changes: 4 additions & 4 deletions include/swift/AST/DiagnosticsCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ namespace swift {
using DeclAttribute = const DeclAttribute *;

// Declare common diagnostics objects with their appropriate types.
#define DIAG(KIND,ID,Options,Text,Signature) \
extern detail::DiagWithArguments<void Signature>::type ID;
#define FIXIT(ID, Text, Signature) \
extern detail::StructuredFixItWithArguments<void Signature>::type ID;
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
extern detail::DiagWithArguments<void Signature>::type ID;
#define FIXIT(ID, Text, Signature) \
extern detail::StructuredFixItWithArguments<void Signature>::type ID;
#include "DiagnosticsCommon.def"
} // end namespace diag
} // end namespace swift
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
namespace swift {
namespace diag {
// Declare common diagnostics objects with their appropriate types.
#define DIAG(KIND,ID,Options,Text,Signature) \
extern detail::DiagWithArguments<void Signature>::type ID;
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
extern detail::DiagWithArguments<void Signature>::type ID;
#include "DiagnosticsDriver.def"
}
}
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ ERROR(error_missing_arg_value,none,
(StringRef, unsigned))
ERROR(error_unknown_arg,none,
"unknown argument: '%0'", (StringRef))
GROUPED_WARNING(unknown_warning_group, unknown_warning_group, none,
"unknown warning group: '%0'", (StringRef))
ERROR(error_invalid_arg_value,none,
"invalid value '%1' in '%0'", (StringRef, StringRef))
ERROR(error_invalid_arg_combination,none,
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsFrontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
namespace swift {
namespace diag {
// Declare common diagnostics objects with their appropriate types.
#define DIAG(KIND,ID,Options,Text,Signature) \
extern detail::DiagWithArguments<void Signature>::type ID;
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
extern detail::DiagWithArguments<void Signature>::type ID;
#include "DiagnosticsFrontend.def"
}
}
Expand Down
Loading