Skip to content

Commit

Permalink
Convert the few remaining ARRAYSIZE_UNSAFE -> arraysize.
Browse files Browse the repository at this point in the history
There are still ARRAYSIZE_UNSAFEs in ppapi/tests/test_audio.cc, but it
defines it itself, and it might be compiled by some older NaCl toolchain
that doesn't support C++11. third_party/npapi/npspy/common/format.cpp
also has its own ARRAYSIZE_UNSAFE, but I'm hoping to delete npspy
completely.)

I'll remove ARRAYSIZE_UNSAFE from base/macro.h separately, since it's
quite likely we have deps that use our base (and those deps may use it).

R=ben@chromium.org
TBR=armansito@chromium.org
BUG=423134

Review URL: https://codereview.chromium.org/663673002

Cr-Commit-Position: refs/heads/master@{#300034}
  • Loading branch information
viettrungluu-cr authored and Commit bot committed Oct 17, 2014
1 parent 7cbf7ae commit 2560ad8
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 70 deletions.
4 changes: 2 additions & 2 deletions android_webview/browser/scoped_app_gl_state_restore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ ScopedAppGLStateRestoreImpl::ScopedAppGLStateRestoreImpl(
glBindVertexArrayOES(0);
}

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(vertex_attrib_); ++i) {
for (size_t i = 0; i < arraysize(vertex_attrib_); ++i) {
glGetVertexAttribiv(
i, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &vertex_attrib_[i].enabled);
glGetVertexAttribiv(
Expand Down Expand Up @@ -309,7 +309,7 @@ ScopedAppGLStateRestoreImpl::~ScopedAppGLStateRestoreImpl() {
if (g_supports_oes_vertex_array_object)
glBindVertexArrayOES(0);

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(vertex_attrib_); ++i) {
for (size_t i = 0; i < arraysize(vertex_attrib_); ++i) {
glBindBuffer(GL_ARRAY_BUFFER,
vertex_attrib_[i].vertex_attrib_array_buffer_binding);
glVertexAttribPointer(i,
Expand Down
2 changes: 1 addition & 1 deletion chromeos/network/network_cert_migrator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class NetworkCertMigrator::MigrationTask
shill::kOpenVPNCaCertPemProperty, UMA_NETWORK_TYPE_OPENVPN },
};

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kNssPemMap); ++i) {
for (size_t i = 0; i < arraysize(kNssPemMap); ++i) {
const base::DictionaryValue* dict = &shill_properties;
if (kNssPemMap[i].read_prefix) {
shill_properties.GetDictionaryWithoutPathExpansion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) {
NetworkChangeNotifier::CONNECTION_2G }
};

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(type_mappings); ++i) {
for (size_t i = 0; i < arraysize(type_mappings); ++i) {
NetworkChangeNotifier::ConnectionType type =
NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
type_mappings[i].shill_type, type_mappings[i].technology);
Expand Down
6 changes: 3 additions & 3 deletions crypto/hmac_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ TEST(HMACTest, RFC2202TestCases) {
"\xBB\xFF\x1A\x91" }
};

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
for (size_t i = 0; i < arraysize(cases); ++i) {
crypto::HMAC hmac(crypto::HMAC::SHA1);
ASSERT_TRUE(hmac.Init(reinterpret_cast<const unsigned char*>(cases[i].key),
cases[i].key_len));
Expand Down Expand Up @@ -239,7 +239,7 @@ TEST(HMACTest, HMACObjectReuse) {
ASSERT_TRUE(
hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey),
kSimpleKeyLength));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSimpleHmacCases); ++i) {
for (size_t i = 0; i < arraysize(kSimpleHmacCases); ++i) {
std::string data_string(kSimpleHmacCases[i].data,
kSimpleHmacCases[i].data_len);
unsigned char digest[kSHA1DigestSize];
Expand All @@ -254,7 +254,7 @@ TEST(HMACTest, Verify) {
hmac.Init(reinterpret_cast<const unsigned char*>(kSimpleKey),
kSimpleKeyLength));
const char empty_digest[kSHA1DigestSize] = { 0 };
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSimpleHmacCases); ++i) {
for (size_t i = 0; i < arraysize(kSimpleHmacCases); ++i) {
// Expected results
EXPECT_TRUE(hmac.Verify(
base::StringPiece(kSimpleHmacCases[i].data,
Expand Down
2 changes: 1 addition & 1 deletion device/bluetooth/bluetooth_uuid_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ TEST(BluetoothUUIDTest, BluetoothUUID_CaseInsensitive) {
{ "00001aBc-0000-1000-8000-00805F9b34fB", k128Bit },
};

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
for (size_t i = 0; i < arraysize(test_cases); ++i) {
SCOPED_TRACE("Input UUID: " + test_cases[i].input_uuid);
BluetoothUUID uuid(test_cases[i].input_uuid);
EXPECT_TRUE(uuid.IsValid());
Expand Down
10 changes: 5 additions & 5 deletions device/hid/hid_report_descriptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ TEST_F(HidReportDescriptorTest, ValidateDetails_Digitizer) {
digitizer.report_ids.insert(3);
HidCollectionInfo expected[] = {digitizer};
ValidateDetails(std::vector<HidCollectionInfo>(
expected, expected + ARRAYSIZE_UNSAFE(expected)),
expected, expected + arraysize(expected)),
true,
6,
0,
Expand All @@ -370,7 +370,7 @@ TEST_F(HidReportDescriptorTest, ValidateDetails_Keyboard) {
keyboard.usage = HidUsageAndPage(0x06, HidUsageAndPage::kPageGenericDesktop);
HidCollectionInfo expected[] = {keyboard};
ValidateDetails(std::vector<HidCollectionInfo>(
expected, expected + ARRAYSIZE_UNSAFE(expected)),
expected, expected + arraysize(expected)),
false,
8,
1,
Expand All @@ -389,7 +389,7 @@ TEST_F(HidReportDescriptorTest, ValidateDetails_Monitor) {
monitor.report_ids.insert(5);
HidCollectionInfo expected[] = {monitor};
ValidateDetails(std::vector<HidCollectionInfo>(
expected, expected + ARRAYSIZE_UNSAFE(expected)),
expected, expected + arraysize(expected)),
true,
0,
0,
Expand All @@ -403,7 +403,7 @@ TEST_F(HidReportDescriptorTest, ValidateDetails_Mouse) {
mouse.usage = HidUsageAndPage(0x02, HidUsageAndPage::kPageGenericDesktop);
HidCollectionInfo expected[] = {mouse};
ValidateDetails(std::vector<HidCollectionInfo>(
expected, expected + ARRAYSIZE_UNSAFE(expected)),
expected, expected + arraysize(expected)),
false,
3,
0,
Expand All @@ -426,7 +426,7 @@ TEST_F(HidReportDescriptorTest, ValidateDetails_LogitechUnifyingReceiver) {

HidCollectionInfo expected[] = {hidpp_short, hidpp_long, hidpp_dj};
ValidateDetails(std::vector<HidCollectionInfo>(
expected, expected + ARRAYSIZE_UNSAFE(expected)),
expected, expected + arraysize(expected)),
true,
31,
31,
Expand Down
6 changes: 3 additions & 3 deletions gin/converter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ TEST_F(ConverterTest, Bool) {
{ Undefined(instance_->isolate()).As<Value>(), false },
};

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) {
for (size_t i = 0; i < arraysize(test_data); ++i) {
bool result = false;
EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(),
test_data[i].input, &result));
Expand All @@ -72,7 +72,7 @@ TEST_F(ConverterTest, Int32) {
HandleScope handle_scope(instance_->isolate());

int test_data_to[] = {-1, 0, 1};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_to); ++i) {
for (size_t i = 0; i < arraysize(test_data_to); ++i) {
EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), test_data_to[i])
->StrictEquals(
Integer::New(instance_->isolate(), test_data_to[i])));
Expand All @@ -98,7 +98,7 @@ TEST_F(ConverterTest, Int32) {
{ v8::Undefined(instance_->isolate()).As<Value>(), false, 0 },
};

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data_from); ++i) {
for (size_t i = 0; i < arraysize(test_data_from); ++i) {
int32_t result = std::numeric_limits<int32_t>::min();
bool success = Converter<int32_t>::FromV8(instance_->isolate(),
test_data_from[i].input, &result);
Expand Down
6 changes: 3 additions & 3 deletions google_apis/drive/drive_api_url_generator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ TEST_F(DriveApiUrlGeneratorTest, GetFilePatchUrl) {
{ true, false, "?setModifiedDate=true&updateViewedDate=false" },
};

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestPatterns); ++i) {
for (size_t i = 0; i < arraysize(kTestPatterns); ++i) {
EXPECT_EQ(
"https://www.googleapis.com/drive/v2/files/0ADK06pfg" +
kTestPatterns[i].expected_query,
Expand Down Expand Up @@ -188,7 +188,7 @@ TEST_F(DriveApiUrlGeneratorTest, GetFilesListUrl) {
{ 10, "token", "query", "?maxResults=10&pageToken=token&q=query" },
};

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestPatterns); ++i) {
for (size_t i = 0; i < arraysize(kTestPatterns); ++i) {
EXPECT_EQ(
"https://www.googleapis.com/drive/v2/files" +
kTestPatterns[i].expected_query,
Expand Down Expand Up @@ -288,7 +288,7 @@ TEST_F(DriveApiUrlGeneratorTest, GetChangesListUrl) {
"&startChangeId=12345" },
};

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestPatterns); ++i) {
for (size_t i = 0; i < arraysize(kTestPatterns); ++i) {
EXPECT_EQ(
"https://www.googleapis.com/drive/v2/changes" +
kTestPatterns[i].expected_query,
Expand Down
4 changes: 2 additions & 2 deletions mojo/examples/window_manager/debug_panel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void DebugPanel::Layout(views::View* view) {
navigation_target_new_,
navigation_target_source_,
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(radios); ++i) {
for (size_t i = 0; i < arraysize(radios); ++i) {
radios[i]->SetBounds(kControlBorderInset, y, w,
radios[i]->GetPreferredSize().height());
y += radios[i]->height();
Expand All @@ -111,7 +111,7 @@ void DebugPanel::Layout(views::View* view) {
close_last_,
cross_app_,
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(buttons); ++i) {
for (size_t i = 0; i < arraysize(buttons); ++i) {
buttons[i]->SetBounds(kControlBorderInset, y, w,
buttons[i]->GetPreferredSize().height());
y += buttons[i]->height();
Expand Down
49 changes: 1 addition & 48 deletions third_party/cld/base/basictypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ const int64 kint64max = (( int64) GG_LONGLONG(0x7FFFFFFFFFFFFFFF));
// The expression is a compile-time constant, and therefore can be
// used in defining new arrays, for example. If you use arraysize on
// a pointer by mistake, you will get a compile-time error.
//
// One caveat is that arraysize() doesn't accept any array of an
// anonymous type or a type defined inside a function. In these rare
// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
// due to a limitation in C++'s template system. The limitation might
// eventually be removed, but it hasn't happened yet.

// This template function declaration is used in defining arraysize.
// Note that the function doesn't need an implementation, as we only
Expand All @@ -121,47 +115,6 @@ char (&ArraySizeHelper(const T (&array)[N]))[N];

#define arraysize(array) (sizeof(ArraySizeHelper(array)))

// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
// but can be used on anonymous types or types defined inside
// functions. It's less safe than arraysize as it accepts some
// (although not all) pointers. Therefore, you should use arraysize
// whenever possible.
//
// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
// size_t.
//
// ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
//
// "warning: division by zero in ..."
//
// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
//
// The following comments are on the implementation details, and can
// be ignored by the users.
//
// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
// the array) and sizeof(*(arr)) (the # of bytes in one array
// element). If the former is divisible by the latter, perhaps arr is
// indeed an array, in which case the division result is the # of
// elements in the array. Otherwise, arr cannot possibly be an array,
// and we generate a compiler error to prevent the code from
// compiling.
//
// Since the size of bool is implementation-defined, we need to cast
// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
// result has type size_t.
//
// This macro is not perfect as it wrongfully accepts certain
// pointers, namely where the pointer size is divisible by the pointee
// size. Since all our code has to go through a 32-bit compiler,
// where a pointer is 4 bytes, this means all pointers to a type whose
// size is 3 or greater than 4 will be (righteously) rejected.

#define ARRAYSIZE_UNSAFE(a) \
((sizeof(a) / sizeof(*(a))) / \
static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))


// Use implicit_cast as a safe version of static_cast or const_cast
// for upcasting in the type hierarchy (i.e. casting a pointer to Foo
Expand Down Expand Up @@ -189,7 +142,7 @@ inline To implicit_cast(From const &f) {
// expression is true. For example, you could use it to verify the
// size of a static array:
//
// COMPILE_ASSERT(ARRAYSIZE_UNSAFE(content_type_names) == CONTENT_NUM_TYPES,
// COMPILE_ASSERT(arraysize(content_type_names) == CONTENT_NUM_TYPES,
// content_type_names_incorrect_size);
//
// or to make sure a struct is smaller than a certain size:
Expand Down
2 changes: 1 addition & 1 deletion tools/gn/label_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TEST(Label, Resolve) {

Label default_toolchain(SourceDir("//t/"), "d");

for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
for (size_t i = 0; i < arraysize(cases); i++) {
const ParseDepStringCase& cur = cases[i];

std::string location, name;
Expand Down

0 comments on commit 2560ad8

Please sign in to comment.