Skip to content

Commit 08e339b

Browse files
authored
Merge pull request #74466 from DmT021/wp/no-warning-as-error
[Diagnostics] Add -no-warning-as-error to except a specific warning from being treated as an error
2 parents f69fe00 + 070c77e commit 08e339b

40 files changed

+858
-123
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ struct BridgedPatternBindingEntry {
382382
// NOTE: This must be the same underlying value as C++ 'swift::DiagID' defined
383383
// in 'DiagnosticList.cpp'.
384384
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagID : uint32_t {
385-
#define DIAG(KIND, ID, Options, Text, Signature) BridgedDiagID_##ID,
385+
#define DIAG(KIND, ID, Group, Options, Text, Signature) BridgedDiagID_##ID,
386386
#include "swift/AST/DiagnosticsAll.def"
387387
};
388388

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===--- DefineDiagnosticGroupsMacros.def -----------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines macros defining diagnostic groups.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
// Define macros
18+
#if defined(DEFINE_DIAGNOSTIC_GROUPS_MACROS) && \
19+
!defined(UNDEFINE_DIAGNOSTIC_GROUPS_MACROS)
20+
21+
#undef DEFINE_DIAGNOSTIC_GROUPS_MACROS
22+
23+
#if !(defined(GROUP) || defined(GROUP_LINK))
24+
#error No reqired macros defined. Define at least one of the following macros: GROUP(Name, DocsFile), GROUP_LINK(Parent, Child)
25+
#endif
26+
27+
// GROUP macro:
28+
// Declares a diagnostic group.
29+
// Parameters:
30+
// Name - group name as it appears in DiagGroupID enum and DiagGroupInfo.name
31+
// DocsFile - file with a human readable description for the group located in
32+
// userdocs/diagnostic_groups
33+
#ifndef GROUP
34+
#define GROUP(Name, DocsFile)
35+
#endif
36+
37+
// GROUP_LINK macro:
38+
// Establishes an edge in the diagnostic group graph between
39+
// a supergroup(Parent) and its subgroup(Child).
40+
// Parameters:
41+
// Parent - parent group name
42+
// Child - child group name
43+
#ifndef GROUP_LINK
44+
#define GROUP_LINK(Parent, Child)
45+
#endif
46+
47+
// Undefine macros
48+
#elif defined(UNDEFINE_DIAGNOSTIC_GROUPS_MACROS) && \
49+
!defined(DEFINE_DIAGNOSTIC_GROUPS_MACROS)
50+
51+
#undef UNDEFINE_DIAGNOSTIC_GROUPS_MACROS
52+
53+
#ifdef GROUP
54+
#undef GROUP
55+
#else
56+
#error Trying to undefine the diagnostic groups macros, but GROUP macro wasn't defined
57+
#endif
58+
59+
#ifdef GROUP_LINK
60+
#undef GROUP_LINK
61+
#else
62+
#error Trying to undefine the diagnostic groups macros, but GROUP_LINK macro wasn't defined
63+
#endif
64+
65+
#else
66+
#error Invalid DefineDiagnosticGroupsMacros.h inclusion
67+
#endif

include/swift/AST/DefineDiagnosticMacros.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,39 @@
1717
// Define macros
1818
#ifdef DEFINE_DIAGNOSTIC_MACROS
1919

20-
#if !(defined(DIAG) || (defined(ERROR) && defined(WARNING) && defined(NOTE) && \
21-
defined(REMARK)))
22-
#error Must define either DIAG or the set {ERROR,WARNING,NOTE,REMARK}
20+
#if !(defined(DIAG) || (defined(GROUPED_ERROR) && defined(GROUPED_WARNING) && \
21+
defined(NOTE) && defined(REMARK)))
22+
#error Must define either DIAG or the set {GROUPED_ERROR,GROUPED_WARNING,NOTE,REMARK}
23+
#endif
24+
25+
#ifndef GROUPED_ERROR
26+
#define GROUPED_ERROR(ID, Group, Options, Text, Signature) \
27+
DIAG(ERROR, ID, Group, Options, Text, Signature)
2328
#endif
2429

2530
#ifndef ERROR
2631
#define ERROR(ID, Options, Text, Signature) \
27-
DIAG(ERROR, ID, Options, Text, Signature)
32+
GROUPED_ERROR(ID, no_group, Options, Text, Signature)
33+
#endif
34+
35+
#ifndef GROUPED_WARNING
36+
#define GROUPED_WARNING(ID, Group, Options, Text, Signature) \
37+
DIAG(WARNING, ID, Group, Options, Text, Signature)
2838
#endif
2939

3040
#ifndef WARNING
3141
#define WARNING(ID, Options, Text, Signature) \
32-
DIAG(WARNING, ID, Options, Text, Signature)
42+
GROUPED_WARNING(ID, no_group, Options, Text, Signature)
3343
#endif
3444

3545
#ifndef NOTE
3646
#define NOTE(ID, Options, Text, Signature) \
37-
DIAG(NOTE, ID, Options, Text, Signature)
47+
DIAG(NOTE, ID, no_group, Options, Text, Signature)
3848
#endif
3949

4050
#ifndef REMARK
4151
#define REMARK(ID, Options, Text, Signature) \
42-
DIAG(REMARK, ID, Options, Text, Signature)
52+
DIAG(REMARK, ID, no_group, Options, Text, Signature)
4353
#endif
4454

4555
#ifndef FIXIT
@@ -61,7 +71,9 @@
6171
#undef REMARK
6272
#undef NOTE
6373
#undef WARNING
74+
#undef GROUPED_WARNING
6475
#undef ERROR
76+
#undef GROUPED_ERROR
6577
#undef FIXIT
6678

6779
#endif

include/swift/AST/DiagnosticEngine.h

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/TypeLoc.h"
2525
#include "swift/Basic/Statistic.h"
2626
#include "swift/Basic/Version.h"
27+
#include "swift/Basic/WarningAsErrorRule.h"
2728
#include "swift/Localization/LocalizationFormat.h"
2829
#include "llvm/ADT/BitVector.h"
2930
#include "llvm/ADT/StringRef.h"
@@ -867,8 +868,8 @@ namespace swift {
867868
/// Don't emit any remarks
868869
bool suppressRemarks = false;
869870

870-
/// Emit all warnings as errors
871-
bool warningsAsErrors = false;
871+
/// Treat these warnings as errors. Indicies here corespond to DiagID enum
872+
llvm::BitVector warningsAsErrors;
872873

873874
/// Whether a fatal error has occurred
874875
bool fatalErrorOccurred = false;
@@ -909,9 +910,22 @@ namespace swift {
909910
void setSuppressRemarks(bool val) { suppressRemarks = val; }
910911
bool getSuppressRemarks() const { return suppressRemarks; }
911912

912-
/// Whether to treat warnings as errors
913-
void setWarningsAsErrors(bool val) { warningsAsErrors = val; }
914-
bool getWarningsAsErrors() const { return warningsAsErrors; }
913+
/// Whether a warning should be upgraded to an error or not
914+
void setWarningAsErrorForDiagID(DiagID id, bool value) {
915+
warningsAsErrors[(unsigned)id] = value;
916+
}
917+
bool getWarningAsErrorForDiagID(DiagID id) {
918+
return warningsAsErrors[(unsigned)id];
919+
}
920+
921+
/// Whether all warnings should be upgraded to errors or not
922+
void setAllWarningsAsErrors(bool value) {
923+
if (value) {
924+
warningsAsErrors.set();
925+
} else {
926+
warningsAsErrors.reset();
927+
}
928+
}
915929

916930
void resetHadAnyError() {
917931
anyErrorOccurred = false;
@@ -1121,11 +1135,13 @@ namespace swift {
11211135
return state.getSuppressRemarks();
11221136
}
11231137

1124-
/// Whether to treat warnings as errors
1125-
void setWarningsAsErrors(bool val) { state.setWarningsAsErrors(val); }
1126-
bool getWarningsAsErrors() const {
1127-
return state.getWarningsAsErrors();
1128-
}
1138+
/// Apply rules specifing what warnings should or shouldn't be treated as
1139+
/// errors. For group rules the string is either a group name defined by
1140+
/// DiagnosticGroups.def
1141+
/// Rules are applied in order they appear in the vector.
1142+
/// In case the vector contains rules affecting the same diagnostic ID
1143+
/// the last rule wins.
1144+
void setWarningsAsErrorsRules(const std::vector<WarningAsErrorRule> &rules);
11291145

11301146
/// Whether to print diagnostic names after their messages
11311147
void setPrintDiagnosticNames(bool val) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- DiagnosticGroups.def - Diagnostic Groups ---------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines diagnostic groups and links between them.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#define DEFINE_DIAGNOSTIC_GROUPS_MACROS
18+
#include "swift/AST/DefineDiagnosticGroupsMacros.h"
19+
20+
// GROUP(Name, DocsFile)
21+
// GROUP_LINK(Parent, Child)
22+
23+
GROUP(no_group, "")
24+
25+
GROUP(deprecated, "deprecated.md")
26+
GROUP_LINK(deprecated, availability_deprecated)
27+
28+
GROUP(availability_deprecated, "availability_deprecated.md")
29+
30+
GROUP(unknown_warning_group, "unknown_warning_group.md")
31+
32+
#define UNDEFINE_DIAGNOSTIC_GROUPS_MACROS
33+
#include "swift/AST/DefineDiagnosticGroupsMacros.h"

include/swift/AST/DiagnosticGroups.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===--- DiagnosticGroups.h - Diagnostic Groups -----------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the diagnostic groups enumaration, group graph
14+
// and auxilary functions.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_DIAGNOSTICGROUPS_H
19+
#define SWIFT_DIAGNOSTICGROUPS_H
20+
21+
#include "swift/AST/DiagnosticList.h"
22+
#include "llvm/ADT/ArrayRef.h"
23+
#include <array>
24+
#include <string_view>
25+
#include <unordered_map>
26+
27+
namespace swift {
28+
29+
enum class DiagGroupID : uint16_t {
30+
#define GROUP(Name, Version) Name,
31+
#include "swift/AST/DiagnosticGroups.def"
32+
};
33+
34+
constexpr const auto DiagGroupsCount = [] {
35+
size_t count = 0;
36+
#define GROUP(Name, Version) count++;
37+
#include "DiagnosticGroups.def"
38+
return count;
39+
}();
40+
41+
struct DiagGroupInfo {
42+
DiagGroupID id;
43+
std::string_view name;
44+
std::string_view version;
45+
llvm::ArrayRef<DiagGroupID> supergroups;
46+
llvm::ArrayRef<DiagGroupID> subgroups;
47+
llvm::ArrayRef<DiagID> diagnostics;
48+
49+
void traverseDepthFirst(
50+
llvm::function_ref<void(const DiagGroupInfo &)> func) const;
51+
};
52+
53+
extern const std::array<DiagGroupInfo, DiagGroupsCount> diagnosticGroupsInfo;
54+
const DiagGroupInfo &getDiagGroupInfoByID(DiagGroupID id);
55+
std::optional<DiagGroupID> getDiagGroupIDByName(std::string_view name);
56+
57+
} // end namespace swift
58+
59+
#endif /* SWIFT_DIAGNOSTICGROUPS_H */

include/swift/AST/DiagnosticList.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===--- DiagnosticList.h - Diagnostic Definitions --------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines all of the diagnostics emitted by Swift.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_DIAGNOSTICLIST_H
18+
#define SWIFT_DIAGNOSTICLIST_H
19+
20+
#include <cstdint>
21+
22+
namespace swift {
23+
24+
/// Enumeration describing all of possible diagnostics.
25+
///
26+
/// Each of the diagnostics described in Diagnostics.def has an entry in
27+
/// this enumeration type that uniquely identifies it.
28+
enum class DiagID : uint32_t {
29+
#define DIAG(KIND, ID, Group, Options, Text, Signature) ID,
30+
#include "swift/AST/DiagnosticsAll.def"
31+
};
32+
static_assert(static_cast<uint32_t>(swift::DiagID::invalid_diagnostic) == 0,
33+
"0 is not the invalid diagnostic ID");
34+
35+
enum class FixItID : uint32_t {
36+
#define DIAG(KIND, ID, Group, Options, Text, Signature)
37+
#define FIXIT(ID, Text, Signature) ID,
38+
#include "swift/AST/DiagnosticsAll.def"
39+
};
40+
41+
} // end namespace swift
42+
43+
#endif /* SWIFT_DIAGNOSTICLIST_H */

include/swift/AST/DiagnosticsClangImporter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
namespace swift {
2424
namespace diag {
2525
// Declare common diagnostics objects with their appropriate types.
26-
#define DIAG(KIND,ID,Options,Text,Signature) \
27-
extern detail::DiagWithArguments<void Signature>::type ID;
26+
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
27+
extern detail::DiagWithArguments<void Signature>::type ID;
2828
#include "DiagnosticsClangImporter.def"
2929
}
3030
}

include/swift/AST/DiagnosticsCommon.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ namespace swift {
5656
using DeclAttribute = const DeclAttribute *;
5757

5858
// Declare common diagnostics objects with their appropriate types.
59-
#define DIAG(KIND,ID,Options,Text,Signature) \
60-
extern detail::DiagWithArguments<void Signature>::type ID;
61-
#define FIXIT(ID, Text, Signature) \
62-
extern detail::StructuredFixItWithArguments<void Signature>::type ID;
59+
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
60+
extern detail::DiagWithArguments<void Signature>::type ID;
61+
#define FIXIT(ID, Text, Signature) \
62+
extern detail::StructuredFixItWithArguments<void Signature>::type ID;
6363
#include "DiagnosticsCommon.def"
6464
} // end namespace diag
6565
} // end namespace swift

include/swift/AST/DiagnosticsDriver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
namespace swift {
2626
namespace diag {
2727
// Declare common diagnostics objects with their appropriate types.
28-
#define DIAG(KIND,ID,Options,Text,Signature) \
29-
extern detail::DiagWithArguments<void Signature>::type ID;
28+
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
29+
extern detail::DiagWithArguments<void Signature>::type ID;
3030
#include "DiagnosticsDriver.def"
3131
}
3232
}

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ ERROR(error_missing_arg_value,none,
8989
(StringRef, unsigned))
9090
ERROR(error_unknown_arg,none,
9191
"unknown argument: '%0'", (StringRef))
92+
GROUPED_WARNING(unknown_warning_group, unknown_warning_group, none,
93+
"unknown warning group: '%0'", (StringRef))
9294
ERROR(error_invalid_arg_value,none,
9395
"invalid value '%1' in '%0'", (StringRef, StringRef))
9496
ERROR(error_invalid_arg_combination,none,

include/swift/AST/DiagnosticsFrontend.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
namespace swift {
2424
namespace diag {
2525
// Declare common diagnostics objects with their appropriate types.
26-
#define DIAG(KIND,ID,Options,Text,Signature) \
27-
extern detail::DiagWithArguments<void Signature>::type ID;
26+
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
27+
extern detail::DiagWithArguments<void Signature>::type ID;
2828
#include "DiagnosticsFrontend.def"
2929
}
3030
}

0 commit comments

Comments
 (0)