Skip to content

Commit

Permalink
cleaned up some array length related code (#7283)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Feb 14, 2025
1 parent a57e265 commit 2e4f606
Show file tree
Hide file tree
Showing 18 changed files with 397 additions and 381 deletions.
13 changes: 6 additions & 7 deletions gui/projectfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "ui_projectfile.h"

#include <array>
#include <list>
#include <string>
#include <utility>
Expand Down Expand Up @@ -79,7 +80,7 @@ static QStringList getPaths(const QListWidget *list)
}

/** Platforms shown in the platform combobox */
static constexpr Platform::Type builtinPlatforms[] = {
static const std::array<Platform::Type, 6> builtinPlatforms = {
Platform::Type::Native,
Platform::Type::Win32A,
Platform::Type::Win32W,
Expand All @@ -88,8 +89,6 @@ static constexpr Platform::Type builtinPlatforms[] = {
Platform::Type::Unix64
};

static constexpr int numberOfBuiltinPlatforms = sizeof(builtinPlatforms) / sizeof(builtinPlatforms[0]);

QStringList ProjectFileDialog::getProjectConfigs(const QString &fileName)
{
if (!fileName.endsWith(".sln") && !fileName.endsWith(".vcxproj"))
Expand Down Expand Up @@ -336,7 +335,7 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile)
const QString& platform = projectFile->getPlatform();
if (platform.endsWith(".xml")) {
int i;
for (i = numberOfBuiltinPlatforms; i < mUI->mComboBoxPlatform->count(); ++i) {
for (i = builtinPlatforms.size(); i < mUI->mComboBoxPlatform->count(); ++i) {
if (mUI->mComboBoxPlatform->itemText(i) == platform)
break;
}
Expand All @@ -348,12 +347,12 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile)
}
} else {
int i;
for (i = 0; i < numberOfBuiltinPlatforms; ++i) {
for (i = 0; i < builtinPlatforms.size(); ++i) {
const Platform::Type p = builtinPlatforms[i];
if (platform == Platform::toString(p))
break;
}
if (i < numberOfBuiltinPlatforms)
if (i < builtinPlatforms.size())
mUI->mComboBoxPlatform->setCurrentIndex(i);
else
mUI->mComboBoxPlatform->setCurrentIndex(-1);
Expand Down Expand Up @@ -482,7 +481,7 @@ void ProjectFileDialog::saveToProjectFile(ProjectFile *projectFile) const
projectFile->setPlatform(mUI->mComboBoxPlatform->currentText());
else {
const int i = mUI->mComboBoxPlatform->currentIndex();
if (i>=0 && i < numberOfBuiltinPlatforms)
if (i>=0 && i < builtinPlatforms.size())
projectFile->setPlatform(Platform::toString(builtinPlatforms[i]));
else
projectFile->setPlatform(QString());
Expand Down
5 changes: 3 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "vfvalue.h"

#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cstdlib>
Expand Down Expand Up @@ -7443,13 +7444,13 @@ void Tokenizer::simplifyStaticConst()
{
// This function will simplify the token list so that the qualifiers "extern", "static"
// and "const" appear in the same order as in the array below.
const std::string qualifiers[] = {"extern", "static", "const"};
static const std::array<std::string, 3> qualifiers = {"extern", "static", "const"};

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

// Keep searching for a qualifier
if (!tok->next() || tok->strAt(1) != qualifiers[i])
Expand Down
7 changes: 6 additions & 1 deletion test/fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ class TestFixture : public ErrorLogger {

SettingsBuilder& library(const char lib[]);

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

SettingsBuilder& platform(Platform::Type type);

Expand All @@ -237,6 +240,8 @@ class TestFixture : public ErrorLogger {
return std::move(settings);
}
private:
SettingsBuilder& libraryxml(const char xmldata[], std::size_t len);

const TestFixture &fixture;
Settings settings;

Expand Down
10 changes: 5 additions & 5 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3697,7 +3697,7 @@ class TestBufferOverrun : public TestFixture {
" <arg nr=\"2\"/>\n"
" </function>\n"
"</def>";
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
const Settings settings = settingsBuilder().libraryxml(xmldata).build();

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

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

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

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

check("void f() {\n"
" char c[5];\n"
Expand Down
2 changes: 1 addition & 1 deletion test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3447,7 +3447,7 @@ class TestClass : public TestFixture {
" <podtype name=\"std::uint8_t\" sign=\"u\" size=\"1\"/>\n"
" <podtype name=\"std::atomic_bool\"/>\n"
"</def>";
const Settings settings = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
const Settings settings = settingsBuilder().libraryxml(xmldata).build();

checkNoMemset("class A {\n"
" std::array<int, 10> ints;\n"
Expand Down
Loading

0 comments on commit 2e4f606

Please sign in to comment.