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
|
// 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 <cstdint>
#include <cstring>
#include <tuple>
#include "base/containers/auto_spanification_helper.h"
#include "base/containers/span.h"
// 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;
std::array<int, 10> values;
};
void fct() {
// Expected rewrite:
// auto buf = std::to_array<int>({1, 2, 3, 4});
auto buf = std::to_array<int>({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};
std::array<int, 5> buf2 = {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 std::array<int, size> buf3 = {1, 1, 1, 1, 1};
(void)buf3[UnsafeIndex()];
// Expected rewrite:
// std::array<int, buf3[0]> buf4;
std::array<int, buf3[0]> buf4;
buf4[UnsafeIndex()] = 11;
// Expected rewrite:
// auto buf5 = std::to_array<ns1::Type1>({{1}, {1}, {1}, {1}, {1}});
auto buf5 = std::to_array<ns1::Type1>({{1}, {1}, {1}, {1}, {1}});
buf5[UnsafeIndex()].value = 11;
// Expected rewrite:
// auto buf6 = std::to_array<uint16_t>({1, 1, 1});
auto buf6 = std::to_array<uint16_t>({1, 1, 1});
buf6[UnsafeIndex()] = 1;
// Expected rewrite:
// std::array<int (*)(int), 16> buf7 = {};
std::array<int (*)(int), 16> buf7 = {};
buf7[UnsafeIndex()] = nullptr;
// Expected rewrite:
// std::array<int (**)[], 16> buf8 = {};
std::array<int (**)[], 16> buf8 = {};
buf8[UnsafeIndex()] = nullptr;
using Arr = int (**)[];
// Expected rewrite:
// std::array<Arr, buf3[0]> buf9 = {};
std::array<Arr, buf3[0]> buf9 = {};
buf9[UnsafeIndex()] = nullptr;
// Expected rewrite:
// static auto buf10 = std::to_array<const volatile char*>({"1", "2", "3"});
static auto buf10 = std::to_array<const volatile char*>({"1", "2", "3"});
buf10[UnsafeIndex()] = nullptr;
std::ignore = kPropertyVisitedIDs[UnsafeIndex()];
}
void sizeof_array_expr() {
// Expected rewrite:
// auto buf = std::to_array<int>({1});
auto buf = std::to_array<int>({1});
std::ignore = buf[UnsafeIndex()];
// Expected rewrite:
// std::ignore = base::SpanificationSizeofForStdArray(buf);
std::ignore = base::SpanificationSizeofForStdArray(buf);
// Expected rewrite:
// std::ignore = base::SpanificationSizeofForStdArray(buf);
std::ignore = base::SpanificationSizeofForStdArray(buf);
// Expected rewrite:
// std::ignore = sizeof buf[0];
std::ignore = sizeof buf[0];
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(base::span<int> ptr) {
std::ignore = ptr[UnsafeIndex()];
}
// Expected rewrite:
// void c_array_param(base::span<int, 1 + 2> arr)
void c_array_param(base::span<int, 1 + 2> arr) {
std::ignore = arr[UnsafeIndex()];
}
// Expected rewrite:
// void c_array_nosize_param(base::span<int> arr)
void c_array_nosize_param(base::span<int> arr) {
std::ignore = arr[UnsafeIndex()];
}
void test_func_params() {
// Expected rewrite:
// auto arr = std::to_array<int>({1, 2, 3});
auto arr = std::to_array<int>({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 std::array<int, 10> filename_offsets;
};
void test_with_mutable() {
const ProgramInfo info;
info.filename_offsets[UnsafeIndex()] = 0xdead;
}
|