Skip to content

Commit eb7a26d

Browse files
committed
[Visual C++] Provide alternative to array designators.
Even if they are supported in GCC and Clang, C99 array designators are not part of the current C++ standard (they will be in C++20), so they are not supported in Visual C++. This commit provides an alternative for Visual C++ (which should work in other compilers) which doesn't use array designators. I think Clang 10 will start printing a warning about this incorrect usage.
1 parent 1ea23d9 commit eb7a26d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lib/AST/DiagnosticEngine.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,25 @@ static constexpr const char *const fixItStrings[] = {
121121
nullptr};
122122
#include "swift/AST/EducationalNotes.def"
123123

124-
static constexpr const char *const *educationalNotes[LocalDiagID::NumDiags]{
125-
[LocalDiagID::invalid_diagnostic] = {},
124+
// NOTE: sadly, while GCC and Clang support array designators in C++, they are
125+
// not part of the standard at the moment, so Visual C++ doesn't support them.
126+
// This construct allows us to provide a constexpr array initialized to empty
127+
// values except in the cases that EducationalNotes.def are provided, similar to
128+
// what the C array would have looked like.
129+
template<int N>
130+
struct EducationalNotes {
131+
constexpr EducationalNotes() : value() {
132+
for (auto i = 0; i < N; ++i) value[i] = {};
126133
#define EDUCATIONAL_NOTES(DIAG, ...) \
127-
[LocalDiagID::DIAG] = DIAG##_educationalNotes,
134+
value[LocalDiagID::DIAG] = DIAG##_educationalNotes;
128135
#include "swift/AST/EducationalNotes.def"
136+
}
137+
const char *const *value[N];
129138
};
130139

140+
static constexpr EducationalNotes<LocalDiagID::NumDiags> _EducationalNotes = EducationalNotes<LocalDiagID::NumDiags>();
141+
static constexpr auto educationalNotes = _EducationalNotes.value;
142+
131143
DiagnosticState::DiagnosticState() {
132144
// Initialize our per-diagnostic state to default
133145
perDiagnosticBehavior.resize(LocalDiagID::NumDiags, Behavior::Unspecified);

0 commit comments

Comments
 (0)