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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <array>
#include <string>
#include <vector>
namespace {
struct Aggregate {
int a;
int b;
int c;
};
Aggregate Build(int a, int b, int c) {
return Aggregate{a, b, c};
}
} // namespace
int UnsafeIndex(); // This function might return an out-of-bound index.
struct Embedded {
// Expected rewrite:
// struct Values {
// int value;
// };
// std::array<Values, 3> values = {{{1}, {2}, {3}}};
struct Values {
int value;
};
std::array<Values, 3> values = {{{1}, {2}, {3}}};
};
void test_with_structs() {
// Expected rewrite:
// auto buf0 = std::to_array<Aggregate>({{13, 1, 7}, {14, 2, 5}, {15, 2, 4}});
auto buf0 = std::to_array<Aggregate>({{13, 1, 7}, {14, 2, 5}, {15, 2, 4}});
buf0[UnsafeIndex()].a = 0;
Embedded embedded;
// Triggers the rewrite of Embedded::values
embedded.values[UnsafeIndex()].value++;
// Expected rewrite:
// std::array<Aggregate, 2> buf1 = {
// Build(1, 2, 3),
// Build(4, 5, 6),
// };
std::array<Aggregate, 2> buf1 = {
Build(1, 2, 3),
Build(4, 5, 6),
};
buf1[UnsafeIndex()].a = 0;
// Expected rewrite:
// std::array<Aggregate, 3> buf2 = {{
// Build(1, 2, 3),
// {1, 2, 3},
// Build(4, 5, 6),
// }};
std::array<Aggregate, 3> buf2 = {{
Build(1, 2, 3),
{1, 2, 3},
Build(4, 5, 6),
}};
buf2[UnsafeIndex()].a = 0;
}
void test_with_arrays() {
// Expected rewrite:
// std::array<std::array<int, 3>, 3> buf0 = {{
// {0, 1, 2},
// {3, 4, 5},
// {6, 7, 8},
// }};
std::array<std::array<int, 3>, 3> buf0 = {{
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
}};
buf0[UnsafeIndex()][UnsafeIndex()] = 0;
// Since function returning array is not allowed, we don't need to
// test the following:
// int buf1[3][3] = {
// BuildArray(0, 1, 2)
// BuildArray(3, 4, 5)
// BuildArray(6, 7, 8)
// };
}
void test_with_strings() {
// Expected rewrite:
// auto buf0 = std::to_array<std::string>({"1", "2", "3"});
auto buf0 = std::to_array<std::string>({"1", "2", "3"});
buf0[UnsafeIndex()] = "4";
}
void test_with_const() {
// Expected rewrite:
// const auto data = std::to_array<bool>({false, true});
const auto data = std::to_array<bool>({false, true});
std::ignore = data[UnsafeIndex()];
}
void test_with_static() {
// Expected rewrite:
// static auto data = std::to_array<int>({1, 2, 3});
static auto data = std::to_array<int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_constexpr() {
// Expected rewrite:
// constexpr auto data = std::to_array<int>({1, 2, 3});
constexpr auto data = std::to_array<int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_constexpr_const() {
// Expected rewrite:
// constexpr auto data = std::to_array<int>({1, 2, 3});
constexpr auto data = std::to_array<int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_volatile() {
// Expected rewrite:
// auto data = std::to_array<volatile int>({1, 2, 3});
auto data = std::to_array<volatile int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_all_qualifiers() {
// Expected rewrite:
// static const auto data = std::to_array<volatile int>({1, 2, 3});
static const auto data = std::to_array<volatile int>({1, 2, 3});
std::ignore = data[UnsafeIndex()];
}
void test_with_const_char() {
// Expected rewrite:
// static auto data = std::to_array<const char*>({" B", " kB", " MB"});
static auto data = std::to_array<const char*>({" B", " kB", " MB"});
std::ignore = data[UnsafeIndex()];
}
void test_with_constant_const_char() {
// Expected rewrite:
// static const auto data = std::to_array<const char*>({" B", " kB", " MB"});
static const auto data = std::to_array<const char*>({" B", " kB", " MB"});
std::ignore = data[UnsafeIndex()];
}
void test_with_computed_size() {
// Expected rewrite:
// std::array<int, 2 + 2> data = {1, 2, 3, 4};
std::array<int, 2 + 2> data = {1, 2, 3, 4};
std::ignore = data[UnsafeIndex()];
}
void test_with_constexpr_size() {
constexpr int size = 2 + 2;
// Expected rewrite:
// std::array<int, size> data = {1, 2, 3, 4};
std::array<int, size> data = {1, 2, 3, 4};
std::ignore = data[UnsafeIndex()];
}
void test_with_const_size() {
const int size = 2 + 2;
// Expected rewrite:
// std::array<int, size> data = {1, 2, 3, 4};
std::array<int, size> data = {1, 2, 3, 4};
std::ignore = data[UnsafeIndex()];
}
void test_brace_elision_computed_size() {
constexpr int size = 2;
struct Aggregate {
int a;
int b;
};
// Expected rewrite:
// std::array<Aggregate, size> buffer = {{{1, 2}, {3, 4}}};
std::array<Aggregate, size> buffer = {{{1, 2}, {3, 4}}};
std::ignore = buffer[UnsafeIndex()];
}
void test_with_nested_vector() {
// Expected rewrite:
// auto data = std::to_array<std::vector<int>>({
// {1, 2, 3},
// {4, 5, 6},
// });
auto data = std::to_array<std::vector<int>>({
{1, 2, 3},
{4, 5, 6},
});
std::ignore = data[UnsafeIndex()];
}
void test_with_nested_vector_const_size() {
// Expected rewrite:
// std::array<std::vector<int>, 2> data = {{
// {1, 2, 3},
// {4, 5, 6},
// }};
std::array<std::vector<int>, 2> data = {{
{1, 2, 3},
{4, 5, 6},
}};
std::ignore = data[UnsafeIndex()];
}
|