-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDiagnosticGroups.cpp
235 lines (205 loc) · 9.52 KB
/
DiagnosticGroups.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//===--- DiagnosticGroups.cpp - 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.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/DiagnosticGroups.h"
#include <unordered_set>
namespace swift {
namespace {
template <size_t SupergroupsSize, size_t SubgroupsSize, size_t DiagnosticsSize>
struct GroupConnections {
std::array<DiagGroupID, SupergroupsSize> supergroups;
std::array<DiagGroupID, SubgroupsSize> subgroups;
std::array<DiagID, DiagnosticsSize> diagnostics;
constexpr GroupConnections(
const std::array<DiagGroupID, SupergroupsSize> &supergroups,
const std::array<DiagGroupID, SubgroupsSize> &subgroups,
const std::array<DiagID, DiagnosticsSize> &diagnostics)
: supergroups(supergroups), subgroups(subgroups),
diagnostics(diagnostics) {}
};
// This CTAD is needed in C++17 only. Remove after update to C++20.
template <size_t SupergroupsSize, size_t SubgroupsSize, size_t DiagnosticsSize>
GroupConnections(const std::array<DiagGroupID, SupergroupsSize> &,
const std::array<DiagGroupID, SubgroupsSize> &,
const std::array<DiagID, DiagnosticsSize> &)
-> GroupConnections<SupergroupsSize, SubgroupsSize, DiagnosticsSize>;
constexpr const auto diagnosticGroupConnections = [] {
constexpr auto sizes = [] {
std::array<size_t, DiagGroupsCount> supergroupsCount{};
std::array<size_t, DiagGroupsCount> subgroupsCount{};
std::array<size_t, DiagGroupsCount> diagnosticsCount{};
// Count edges for each diagnostic group
#define GROUP_LINK(Parent, Child) \
subgroupsCount[(size_t)DiagGroupID::Parent]++; \
supergroupsCount[(size_t)DiagGroupID::Child]++;
#include "swift/AST/DiagnosticGroups.def"
// Count attached diagnostic IDs for each diagnostic group
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
diagnosticsCount[(size_t)DiagGroupID::Group]++;
#include "swift/AST/DiagnosticsAll.def"
return std::tuple{supergroupsCount, subgroupsCount, diagnosticsCount};
}();
constexpr auto supergroupsCount = std::get<0>(sizes);
constexpr auto subgroupsCount = std::get<1>(sizes);
constexpr auto diagnosticsCount = std::get<2>(sizes);
// Declare all edges
#define GROUP(Name, Version) \
std::array<DiagGroupID, supergroupsCount[(size_t)DiagGroupID::Name]> \
Name##_supergroups{}; \
std::array<DiagGroupID, subgroupsCount[(size_t)DiagGroupID::Name]> \
Name##_subgroups{}; \
std::array<DiagID, diagnosticsCount[(size_t)DiagGroupID::Name]> \
Name##_diagnostics{}; \
[[maybe_unused]] size_t Name##_supergroupsIndex = 0; \
[[maybe_unused]] size_t Name##_subgroupsIndex = 0; \
[[maybe_unused]] size_t Name##_diagnosticsIndex = 0;
#include "swift/AST/DiagnosticGroups.def"
// Bind all groups to each other
#define GROUP_LINK(Parent, Child) \
Parent##_subgroups[Parent##_subgroupsIndex++] = DiagGroupID::Child; \
Child##_supergroups[Child##_supergroupsIndex++] = DiagGroupID::Parent;
#include "swift/AST/DiagnosticGroups.def"
// Bind all diagnostics to their groups
#define DIAG(KIND, ID, Group, Options, Text, Signature) \
Group##_diagnostics[Group##_diagnosticsIndex++] = DiagID::ID;
#include "swift/AST/DiagnosticsAll.def"
// Produce the resulting structure with all the edges
#define GROUP(Name, Version) \
GroupConnections(Name##_supergroups, Name##_subgroups, Name##_diagnostics),
return std::tuple{
#include "swift/AST/DiagnosticGroups.def"
};
}();
std::unordered_map<std::string_view, DiagGroupID> nameToIDMap{
#define GROUP(Name, Version) {#Name, DiagGroupID::Name},
#include "swift/AST/DiagnosticGroups.def"
};
void traverseDepthFirst(DiagGroupID id,
std::unordered_set<DiagGroupID> &visited,
llvm::function_ref<void(const DiagGroupInfo &)> func) {
if (visited.insert(id).second) {
const auto &info = getDiagGroupInfoByID(id);
func(info);
for (const auto subgroup : info.subgroups) {
traverseDepthFirst(subgroup, visited, func);
}
}
}
} // end anonymous namespace
constexpr const std::array<DiagGroupInfo, DiagGroupsCount> diagnosticGroupsInfo{
#define GROUP(Name, Version) \
DiagGroupInfo{ \
DiagGroupID::Name, \
#Name, \
#Version, \
llvm::ArrayRef<DiagGroupID>( \
std::get<(size_t)DiagGroupID::Name>(diagnosticGroupConnections) \
.supergroups), \
llvm::ArrayRef<DiagGroupID>( \
std::get<(size_t)DiagGroupID::Name>(diagnosticGroupConnections) \
.subgroups), \
llvm::ArrayRef<DiagID>( \
std::get<(size_t)DiagGroupID::Name>(diagnosticGroupConnections) \
.diagnostics)},
#include "swift/AST/DiagnosticGroups.def"
};
const DiagGroupInfo &getDiagGroupInfoByID(DiagGroupID id) {
return diagnosticGroupsInfo[(size_t)id];
}
std::optional<DiagGroupID> getDiagGroupIDByName(std::string_view name) {
auto it = nameToIDMap.find(name);
if (it == nameToIDMap.end())
return std::nullopt;
return it->second;
}
void DiagGroupInfo::traverseDepthFirst(
llvm::function_ref<void(const DiagGroupInfo &)> func) const {
std::unordered_set<DiagGroupID> visited;
::swift::traverseDepthFirst(id, visited, func);
}
namespace validation {
template <typename F, size_t... Ints>
constexpr void unfold(F &&function, std::index_sequence<Ints...>) {
(function(std::integral_constant<size_t, Ints>{}), ...);
}
template <size_t Size, typename F>
constexpr void constexpr_for(F &&function) {
unfold(function, std::make_index_sequence<Size>());
}
template <size_t Group>
constexpr bool
hasCycleFromGroup(std::array<bool, DiagGroupsCount> &visited,
std::array<bool, DiagGroupsCount> &recursionStack) {
if (!visited[Group]) {
visited[Group] = true;
recursionStack[Group] = true;
constexpr auto subgroups =
std::get<Group>(diagnosticGroupConnections).subgroups;
bool isCycleFound = false;
constexpr_for<subgroups.size()>([&](auto i) {
constexpr auto subgroup = (size_t)subgroups[i];
if (!visited[subgroup] &&
hasCycleFromGroup<subgroup>(visited, recursionStack))
isCycleFound = true;
else if (recursionStack[subgroup])
isCycleFound = true;
});
if (isCycleFound)
return true;
}
recursionStack[Group] = false;
return false;
}
constexpr bool hasCycle() {
std::array<bool, DiagGroupsCount> recursionStack{};
std::array<bool, DiagGroupsCount> visited{};
bool isCycleFound = false;
constexpr_for<DiagGroupsCount>([&](auto i) {
if (!visited[i] && hasCycleFromGroup<i>(visited, recursionStack))
isCycleFound = true;
});
return isCycleFound;
}
template <DiagGroupID Child, DiagGroupID Parent>
constexpr bool isGroupInSupergroup() {
for (const auto group :
std::get<(size_t)Child>(diagnosticGroupConnections).supergroups)
if (group == Parent)
return true;
return false;
}
// Check for isGroupInSupergroup itself
static_assert(!isGroupInSupergroup<DiagGroupID::no_group,
DiagGroupID::DeprecatedDeclaration>() &&
"Bug in isGroupInSupergroup");
static_assert(!hasCycle(), "Diagnostic groups graph has a cycle!");
// Sanity check for the "no_group" group
static_assert((uint16_t)DiagGroupID::no_group == 0, "0 isn't no_group");
static_assert(std::get<0>(diagnosticGroupConnections).supergroups.size() == 0,
"no_group isn't a top-level group");
static_assert(std::get<0>(diagnosticGroupConnections).subgroups.size() == 0,
"no_group shouldn't have subgroups");
// Check groups have associated diagnostics
#define CHECK_NOT_EMPTY(Group) \
static_assert( \
std::get<(uint16_t)DiagGroupID::Group>(diagnosticGroupConnections) \
.diagnostics.size() > 0, \
"'" #Group "' group shouldn't be empty.");
CHECK_NOT_EMPTY(DeprecatedDeclaration)
CHECK_NOT_EMPTY(UnknownWarningGroup)
#undef CHECK_NOT_EMPTY
} // end namespace validation
} // end namespace swift