Skip to content

Commit 2e4f606

Browse files
authored
cleaned up some array length related code (danmar#7283)
1 parent a57e265 commit 2e4f606

18 files changed

+397
-381
lines changed

gui/projectfiledialog.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "ui_projectfile.h"
3232

33+
#include <array>
3334
#include <list>
3435
#include <string>
3536
#include <utility>
@@ -79,7 +80,7 @@ static QStringList getPaths(const QListWidget *list)
7980
}
8081

8182
/** Platforms shown in the platform combobox */
82-
static constexpr Platform::Type builtinPlatforms[] = {
83+
static const std::array<Platform::Type, 6> builtinPlatforms = {
8384
Platform::Type::Native,
8485
Platform::Type::Win32A,
8586
Platform::Type::Win32W,
@@ -88,8 +89,6 @@ static constexpr Platform::Type builtinPlatforms[] = {
8889
Platform::Type::Unix64
8990
};
9091

91-
static constexpr int numberOfBuiltinPlatforms = sizeof(builtinPlatforms) / sizeof(builtinPlatforms[0]);
92-
9392
QStringList ProjectFileDialog::getProjectConfigs(const QString &fileName)
9493
{
9594
if (!fileName.endsWith(".sln") && !fileName.endsWith(".vcxproj"))
@@ -336,7 +335,7 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile)
336335
const QString& platform = projectFile->getPlatform();
337336
if (platform.endsWith(".xml")) {
338337
int i;
339-
for (i = numberOfBuiltinPlatforms; i < mUI->mComboBoxPlatform->count(); ++i) {
338+
for (i = builtinPlatforms.size(); i < mUI->mComboBoxPlatform->count(); ++i) {
340339
if (mUI->mComboBoxPlatform->itemText(i) == platform)
341340
break;
342341
}
@@ -348,12 +347,12 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile)
348347
}
349348
} else {
350349
int i;
351-
for (i = 0; i < numberOfBuiltinPlatforms; ++i) {
350+
for (i = 0; i < builtinPlatforms.size(); ++i) {
352351
const Platform::Type p = builtinPlatforms[i];
353352
if (platform == Platform::toString(p))
354353
break;
355354
}
356-
if (i < numberOfBuiltinPlatforms)
355+
if (i < builtinPlatforms.size())
357356
mUI->mComboBoxPlatform->setCurrentIndex(i);
358357
else
359358
mUI->mComboBoxPlatform->setCurrentIndex(-1);
@@ -482,7 +481,7 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const
482481
projectFile->setPlatform(mUI->mComboBoxPlatform->currentText());
483482
else {
484483
const int i = mUI->mComboBoxPlatform->currentIndex();
485-
if (i>=0 && i < numberOfBuiltinPlatforms)
484+
if (i>=0 && i < builtinPlatforms.size())
486485
projectFile->setPlatform(Platform::toString(builtinPlatforms[i]));
487486
else
488487
projectFile->setPlatform(QString());

lib/tokenize.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "vfvalue.h"
4040

4141
#include <algorithm>
42+
#include <array>
4243
#include <cassert>
4344
#include <cctype>
4445
#include <cstdlib>
@@ -7443,13 +7444,13 @@ void Tokenizer::simplifyStaticConst()
74437444
{
74447445
// This function will simplify the token list so that the qualifiers "extern", "static"
74457446
// and "const" appear in the same order as in the array below.
7446-
const std::string qualifiers[] = {"extern", "static", "const"};
7447+
static const std::array<std::string, 3> qualifiers = {"extern", "static", "const"};
74477448

74487449
// Move 'const' before all other qualifiers and types and then
74497450
// move 'static' before all other qualifiers and types, ...
74507451
for (Token *tok = list.front(); tok; tok = tok->next()) {
74517452
bool continue2 = false;
7452-
for (std::size_t i = 0; i < sizeof(qualifiers)/sizeof(qualifiers[0]); i++) {
7453+
for (std::size_t i = 0; i < qualifiers.size(); i++) {
74537454

74547455
// Keep searching for a qualifier
74557456
if (!tok->next() || tok->strAt(1) != qualifiers[i])

test/fixture.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ class TestFixture : public ErrorLogger {
215215

216216
SettingsBuilder& library(const char lib[]);
217217

218-
SettingsBuilder& libraryxml(const char xmldata[], std::size_t len);
218+
template<size_t size>
219+
SettingsBuilder& libraryxml(const char (&xmldata)[size]) {
220+
return libraryxml(xmldata, size-1);
221+
}
219222

220223
SettingsBuilder& platform(Platform::Type type);
221224

@@ -237,6 +240,8 @@ class TestFixture : public ErrorLogger {
237240
return std::move(settings);
238241
}
239242
private:
243+
SettingsBuilder& libraryxml(const char xmldata[], std::size_t len);
244+
240245
const TestFixture &fixture;
241246
Settings settings;
242247

test/testbufferoverrun.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3697,7 +3697,7 @@ class TestBufferOverrun : public TestFixture {
36973697
" <arg nr=\"2\"/>\n"
36983698
" </function>\n"
36993699
"</def>";
3700-
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
3700+
const Settings settings = settingsBuilder().libraryxml(xmldata).build();
37013701

37023702
// Attempt to get size from Cfg files, no false positives if size is not specified
37033703
check("void f() {\n"
@@ -4255,7 +4255,7 @@ class TestBufferOverrun : public TestFixture {
42554255
" <arg nr=\"3\"/>\n"
42564256
" </function>\n"
42574257
"</def>";
4258-
/*const*/ Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).severity(Severity::warning).build();
4258+
/*const*/ Settings settings = settingsBuilder().libraryxml(xmldata).severity(Severity::warning).build();
42594259
settings.platform.sizeof_wchar_t = 4;
42604260

42614261
check("void f() {\n"
@@ -4393,7 +4393,7 @@ class TestBufferOverrun : public TestFixture {
43934393
" <arg nr=\"3\"/>\n"
43944394
" </function>\n"
43954395
"</def>";
4396-
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
4396+
const Settings settings = settingsBuilder().libraryxml(xmldata).build();
43974397

43984398
check("void f() {\n"
43994399
" char c[7];\n"
@@ -4454,7 +4454,7 @@ class TestBufferOverrun : public TestFixture {
44544454
" </arg>\n"
44554455
" </function>\n"
44564456
"</def>";
4457-
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
4457+
const Settings settings = settingsBuilder().libraryxml(xmldata).build();
44584458

44594459
// formatstr..
44604460
check("void f() {\n"
@@ -4565,7 +4565,7 @@ class TestBufferOverrun : public TestFixture {
45654565
" <arg nr=\"4\"/>\n"
45664566
" </function>\n"
45674567
"</def>";
4568-
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
4568+
const Settings settings = settingsBuilder().libraryxml(xmldata).build();
45694569

45704570
check("void f() {\n"
45714571
" char c[5];\n"

test/testclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3447,7 +3447,7 @@ class TestClass : public TestFixture {
34473447
" <podtype name=\"std::uint8_t\" sign=\"u\" size=\"1\"/>\n"
34483448
" <podtype name=\"std::atomic_bool\"/>\n"
34493449
"</def>";
3450-
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
3450+
const Settings settings = settingsBuilder().libraryxml(xmldata).build();
34513451

34523452
checkNoMemset("class A {\n"
34533453
" std::array<int, 10> ints;\n"

0 commit comments

Comments
 (0)