Skip to content

Commit 64e8976

Browse files
committed
[NFC] AST: Define NumDiagIDs instead of redefining the enum
1 parent b31b90d commit 64e8976

File tree

4 files changed

+59
-71
lines changed

4 files changed

+59
-71
lines changed

include/swift/APIDigester/ModuleDiagsConsumer.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -18,12 +18,10 @@
1818
#ifndef __SWIFT_MODULE_DIFFER_DIAGS_CONSUMER_H__
1919
#define __SWIFT_MODULE_DIFFER_DIAGS_CONSUMER_H__
2020

21-
#include "llvm/ADT/MapVector.h"
22-
#include "swift/Basic/LLVM.h"
2321
#include "swift/AST/DiagnosticConsumer.h"
2422
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
25-
26-
#include "llvm/Support/raw_ostream.h"
23+
#include "llvm/ADT/MapVector.h"
24+
#include "llvm/ADT/StringSet.h"
2725
#include <set>
2826

2927
namespace swift {

include/swift/AST/DiagnosticList.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -18,6 +18,7 @@
1818
#define SWIFT_DIAGNOSTICLIST_H
1919

2020
#include <cstdint>
21+
#include <type_traits>
2122

2223
namespace swift {
2324

@@ -28,10 +29,14 @@ namespace swift {
2829
enum class DiagID : uint32_t {
2930
#define DIAG(KIND, ID, Group, Options, Text, Signature) ID,
3031
#include "swift/AST/DiagnosticsAll.def"
32+
NumDiagsHandle
3133
};
3234
static_assert(static_cast<uint32_t>(swift::DiagID::invalid_diagnostic) == 0,
3335
"0 is not the invalid diagnostic ID");
3436

37+
constexpr auto NumDiagIDs =
38+
static_cast<std::underlying_type_t<DiagID>>(DiagID::NumDiagsHandle);
39+
3540
enum class FixItID : uint32_t {
3641
#define DIAG(KIND, ID, Group, Options, Text, Signature)
3742
#define FIXIT(ID, Text, Signature) ID,

lib/APIDigester/ModuleDiagsConsumer.cpp

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -15,91 +15,81 @@
1515
//
1616
//===----------------------------------------------------------------------===//
1717

18-
#include "swift/AST/DiagnosticEngine.h"
19-
#include "swift/AST/DiagnosticsModuleDiffer.h"
2018
#include "swift/APIDigester/ModuleDiagsConsumer.h"
19+
#include "swift/AST/DiagnosticEngine.h"
20+
#include "swift/AST/DiagnosticList.h"
2121
#include "swift/Basic/Assertions.h"
2222

2323
using namespace swift;
2424

25-
namespace {
26-
// Reproduce the DiagIDs, as we want both the size and access to the raw ids
27-
// themselves.
28-
enum LocalDiagID : uint32_t {
29-
#define DIAG(KIND, ID, Group, Options, Text, Signature) ID,
30-
#include "swift/AST/DiagnosticsAll.def"
31-
NumDiags
32-
};
33-
34-
static StringRef getCategoryName(uint32_t ID) {
25+
static StringRef getCategoryName(DiagID ID) {
3526
switch(ID) {
36-
case LocalDiagID::removed_decl:
27+
case DiagID::removed_decl:
3728
return "/* Removed Decls */";
38-
case LocalDiagID::moved_decl:
39-
case LocalDiagID::decl_kind_changed:
29+
case DiagID::moved_decl:
30+
case DiagID::decl_kind_changed:
4031
return "/* Moved Decls */";
41-
case LocalDiagID::renamed_decl:
42-
case LocalDiagID::objc_name_change:
32+
case DiagID::renamed_decl:
33+
case DiagID::objc_name_change:
4334
return "/* Renamed Decls */";
44-
case LocalDiagID::decl_attr_change:
45-
case LocalDiagID::decl_new_attr:
46-
case LocalDiagID::func_self_access_change:
47-
case LocalDiagID::new_decl_without_intro:
35+
case DiagID::decl_attr_change:
36+
case DiagID::decl_new_attr:
37+
case DiagID::func_self_access_change:
38+
case DiagID::new_decl_without_intro:
4839
return "/* Decl Attribute changes */";
49-
case LocalDiagID::default_arg_removed:
50-
case LocalDiagID::decl_type_change:
51-
case LocalDiagID::func_type_escaping_changed:
52-
case LocalDiagID::param_ownership_change:
53-
case LocalDiagID::type_witness_change:
40+
case DiagID::default_arg_removed:
41+
case DiagID::decl_type_change:
42+
case DiagID::func_type_escaping_changed:
43+
case DiagID::param_ownership_change:
44+
case DiagID::type_witness_change:
5445
return "/* Type Changes */";
55-
case LocalDiagID::raw_type_change:
46+
case DiagID::raw_type_change:
5647
return "/* RawRepresentable Changes */";
57-
case LocalDiagID::generic_sig_change:
58-
case LocalDiagID::demangled_name_changed:
48+
case DiagID::generic_sig_change:
49+
case DiagID::demangled_name_changed:
5950
return "/* Generic Signature Changes */";
60-
case LocalDiagID::enum_case_added:
61-
case LocalDiagID::decl_added:
62-
case LocalDiagID::decl_reorder:
63-
case LocalDiagID::var_has_fixed_order_change:
64-
case LocalDiagID::func_has_fixed_order_change:
51+
case DiagID::enum_case_added:
52+
case DiagID::decl_added:
53+
case DiagID::decl_reorder:
54+
case DiagID::var_has_fixed_order_change:
55+
case DiagID::func_has_fixed_order_change:
6556
return "/* Fixed-layout Type Changes */";
66-
case LocalDiagID::conformance_added:
67-
case LocalDiagID::conformance_removed:
68-
case LocalDiagID::optional_req_changed:
69-
case LocalDiagID::existing_conformance_added:
57+
case DiagID::conformance_added:
58+
case DiagID::conformance_removed:
59+
case DiagID::optional_req_changed:
60+
case DiagID::existing_conformance_added:
7061
return "/* Protocol Conformance Change */";
71-
case LocalDiagID::default_associated_type_removed:
72-
case LocalDiagID::protocol_req_added:
73-
case LocalDiagID::decl_new_witness_table_entry:
62+
case DiagID::default_associated_type_removed:
63+
case DiagID::protocol_req_added:
64+
case DiagID::decl_new_witness_table_entry:
7465
return "/* Protocol Requirement Change */";
75-
case LocalDiagID::super_class_removed:
76-
case LocalDiagID::super_class_changed:
77-
case LocalDiagID::no_longer_open:
78-
case LocalDiagID::desig_init_added:
79-
case LocalDiagID::added_invisible_designated_init:
80-
case LocalDiagID::not_inheriting_convenience_inits:
66+
case DiagID::super_class_removed:
67+
case DiagID::super_class_changed:
68+
case DiagID::no_longer_open:
69+
case DiagID::desig_init_added:
70+
case DiagID::added_invisible_designated_init:
71+
case DiagID::not_inheriting_convenience_inits:
8172
return "/* Class Inheritance Change */";
8273
default:
8374
return "/* Others */";
8475
}
8576
}
86-
}
8777

8878
swift::ide::api::
8979
ModuleDifferDiagsConsumer::ModuleDifferDiagsConsumer(bool DiagnoseModuleDiff,
9080
llvm::raw_ostream &OS):
9181
PrintingDiagnosticConsumer(OS), OS(OS),
9282
DiagnoseModuleDiff(DiagnoseModuleDiff) {
9383
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
94-
auto ID = getCategoryName(LocalDiagID::ID); \
84+
auto ID = getCategoryName(DiagID::ID); \
9585
assert(!ID.empty()); \
9686
AllDiags[ID] = std::set<std::string>();
9787
#include "swift/AST/DiagnosticsModuleDiffer.def"
9888
}
9989

10090
void swift::ide::api::ModuleDifferDiagsConsumer::handleDiagnostic(
10191
SourceManager &SM, const DiagnosticInfo &Info) {
102-
auto Category = getCategoryName((uint32_t)Info.ID);
92+
auto Category = getCategoryName(Info.ID);
10393
if (Category.empty()) {
10494
PrintingDiagnosticConsumer::handleDiagnostic(SM, Info);
10595
return;

lib/AST/DiagnosticEngine.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -16,13 +16,14 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "swift/AST/DiagnosticEngine.h"
19-
#include "swift/AST/DiagnosticsFrontend.h"
2019
#include "swift/AST/ASTContext.h"
2120
#include "swift/AST/ASTPrinter.h"
2221
#include "swift/AST/Decl.h"
2322
#include "swift/AST/DiagnosticGroups.h"
23+
#include "swift/AST/DiagnosticList.h"
2424
#include "swift/AST/DiagnosticSuppression.h"
2525
#include "swift/AST/DiagnosticsCommon.h"
26+
#include "swift/AST/DiagnosticsFrontend.h"
2627
#include "swift/AST/Expr.h"
2728
#include "swift/AST/Module.h"
2829
#include "swift/AST/Pattern.h"
@@ -99,14 +100,6 @@ struct StoredDiagnosticInfo {
99100
opts == DiagnosticOptions::Deprecation,
100101
opts == DiagnosticOptions::NoUsage, groupID) {}
101102
};
102-
103-
// Reproduce the DiagIDs, as we want both the size and access to the raw ids
104-
// themselves.
105-
enum LocalDiagID : uint32_t {
106-
#define DIAG(KIND, ID, Group, Options, Text, Signature) ID,
107-
#include "swift/AST/DiagnosticsAll.def"
108-
NumDiags
109-
};
110103
} // end anonymous namespace
111104

112105
// TODO: categorization
@@ -126,7 +119,7 @@ static const constexpr StoredDiagnosticInfo storedDiagnosticInfos[] = {
126119
#include "swift/AST/DiagnosticsAll.def"
127120
};
128121
static_assert(sizeof(storedDiagnosticInfos) / sizeof(StoredDiagnosticInfo) ==
129-
LocalDiagID::NumDiags,
122+
NumDiagIDs,
130123
"array size mismatch");
131124

132125
static constexpr const char * const diagnosticStrings[] = {
@@ -163,18 +156,20 @@ struct EducationalNotes {
163156
constexpr EducationalNotes() : value() {
164157
for (auto i = 0; i < N; ++i) value[i] = {};
165158
#define EDUCATIONAL_NOTES(DIAG, ...) \
166-
value[LocalDiagID::DIAG] = DIAG##_educationalNotes;
159+
value[static_cast<std::underlying_type_t<DiagID>>(DiagID::DIAG)] = \
160+
DIAG##_educationalNotes;
167161
#include "swift/AST/EducationalNotes.def"
168162
}
169163
const char *const *value[N];
170164
};
171165

172-
static constexpr EducationalNotes<LocalDiagID::NumDiags> _EducationalNotes = EducationalNotes<LocalDiagID::NumDiags>();
166+
static constexpr EducationalNotes<NumDiagIDs> _EducationalNotes =
167+
EducationalNotes<NumDiagIDs>();
173168
static constexpr auto educationalNotes = _EducationalNotes.value;
174169

175170
DiagnosticState::DiagnosticState() {
176171
// Initialize our ignored diagnostics to default
177-
ignoredDiagnostics.resize(LocalDiagID::NumDiags);
172+
ignoredDiagnostics.resize(NumDiagIDs);
178173
// Initialize warningsAsErrors to default
179174
warningsAsErrors.resize(DiagGroupsCount);
180175
}

0 commit comments

Comments
 (0)