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
|
// 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 <cstdint>
#include <cstring>
#include <tuple>
// No rewrite expected.
extern const int kPropertyVisitedIDs[];
namespace ns1 {
struct Type1 {
int value;
};
} // namespace ns1
int UnsafeIndex(); // This function might return an out-of-bound index.
struct Component {
// Expected rewrite:
// std::array<int, 10> values;
int values[10];
};
void fct() {
// Expected rewrite:
// auto buf = std::to_array<int>({1, 2, 3, 4});
int buf[] = {1, 2, 3, 4};
buf[UnsafeIndex()] = 11;
Component component;
// Triggers the rewrite of Component::values
component.values[UnsafeIndex()]++;
// Expected rewrite:
// std::array<int, 5> buf2 = {1, 1, 1, 1, 1};
int buf2[5] = {1, 1, 1, 1, 1};
buf2[UnsafeIndex()] = 11;
constexpr int size = 5;
// Expected rewrite:
// constexpr std::array<int, size> buf3 = {1, 1, 1, 1, 1};
constexpr int buf3[size] = {1, 1, 1, 1, 1};
(void)buf3[UnsafeIndex()];
// Expected rewrite:
// std::array<int, buf3[0]> buf4;
int buf4[buf3[0]];
buf4[UnsafeIndex()] = 11;
// Expected rewrite:
// auto buf5 = std::to_array<ns1::Type1>({{1}, {1}, {1}, {1}, {1}});
ns1::Type1 buf5[] = {{1}, {1}, {1}, {1}, {1}};
buf5[UnsafeIndex()].value = 11;
// Expected rewrite:
// auto buf6 = std::to_array<uint16_t>({1, 1, 1});
uint16_t buf6[] = {1, 1, 1};
buf6[UnsafeIndex()] = 1;
// Expected rewrite:
// std::array<int (*)(int), 16> buf7 = {};
int (*buf7[16])(int) = {};
buf7[UnsafeIndex()] = nullptr;
// Expected rewrite:
// std::array<int (**)[], 16> buf8 = {};
int (**buf8[16])[] = {};
buf8[UnsafeIndex()] = nullptr;
using Arr = int (**)[];
// Expected rewrite:
// std::array<Arr, buf3[0]> buf9 = {};
Arr buf9[buf3[0]] = {};
buf9[UnsafeIndex()] = nullptr;
// Expected rewrite:
// static auto buf10 = std::to_array<const volatile char*>({"1", "2", "3"});
volatile static const char* buf10[] = {"1", "2", "3"};
buf10[UnsafeIndex()] = nullptr;
std::ignore = kPropertyVisitedIDs[UnsafeIndex()];
}
void sizeof_array_expr() {
// Expected rewrite:
// auto buf = std::to_array<int>({1});
int buf[]{1};
std::ignore = buf[UnsafeIndex()];
// Expected rewrite:
// std::ignore = base::SpanificationSizeofForStdArray(buf);
std::ignore = sizeof buf;
// Expected rewrite:
// std::ignore = base::SpanificationSizeofForStdArray(buf);
std::ignore = sizeof(buf);
// Expected rewrite:
// std::ignore = sizeof buf[0];
std::ignore = sizeof *buf;
std::ignore = sizeof buf[0];
}
// Test for crbug.com/383424943.
void crbug_383424943() {
// No rewrite expected.
int buf[]{1};
// Using sizeof was causing buf to be rewritten.
memset(buf, 'x', sizeof(buf));
}
// Expected rewrite:
// void c_ptr_param(base::span<int> ptr)
void c_ptr_param(int* ptr) {
std::ignore = ptr[UnsafeIndex()];
}
// Expected rewrite:
// void c_array_param(base::span<int, 1 + 2> arr)
void c_array_param(int arr[1 + 2]) {
std::ignore = arr[UnsafeIndex()];
}
// Expected rewrite:
// void c_array_nosize_param(base::span<int> arr)
void c_array_nosize_param(int arr[]) {
std::ignore = arr[UnsafeIndex()];
}
void test_func_params() {
// Expected rewrite:
// auto arr = std::to_array<int>({1, 2, 3});
int arr[] = {1, 2, 3};
std::ignore = arr[UnsafeIndex()];
c_ptr_param(arr);
c_array_param(arr);
c_array_nosize_param(arr);
}
struct ProgramInfo {
// Expected rewrite:
// mutable std::array<int, 10> filename_offsets;
mutable int filename_offsets[10];
};
void test_with_mutable() {
const ProgramInfo info;
info.filename_offsets[UnsafeIndex()] = 0xdead;
}
|