Skip to content

Commit 14e78e1

Browse files
authored
greatly improved error handling in Cppcheck project file parsing / some cleanups (#4752)
1 parent 0dddba3 commit 14e78e1

16 files changed

Lines changed: 301 additions & 111 deletions

cli/cmdlineparser.cpp

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -603,25 +603,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
603603
else if (std::strncmp(argv[i], "--platform=", 11) == 0) {
604604
const std::string platform(11+argv[i]);
605605

606-
if (platform == "win32A")
607-
mSettings->platform(Settings::Win32A);
608-
else if (platform == "win32W")
609-
mSettings->platform(Settings::Win32W);
610-
else if (platform == "win64")
611-
mSettings->platform(Settings::Win64);
612-
else if (platform == "unix32")
613-
mSettings->platform(Settings::Unix32);
614-
else if (platform == "unix64")
615-
mSettings->platform(Settings::Unix64);
616-
else if (platform == "native")
617-
mSettings->platform(Settings::Native);
618-
else if (platform == "unspecified")
619-
mSettings->platform(Settings::Unspecified);
620-
else if (!mSettings->loadPlatformFile(argv[0], platform, mSettings->verbose)) {
621-
std::string message("unrecognized platform: \"");
622-
message += platform;
623-
message += "\".";
624-
printError(message);
606+
std::string errstr;
607+
const std::vector<std::string> paths = {argv[0]};
608+
if (!mSettings->platform(platform, errstr, paths)) {
609+
printError(errstr);
625610
return false;
626611
}
627612

@@ -669,7 +654,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
669654
// --project
670655
else if (std::strncmp(argv[i], "--project=", 10) == 0) {
671656
mSettings->checkAllConfigurations = false; // Can be overridden with --max-configs or --force
672-
const std::string projectFile = argv[i]+10;
657+
std::string projectFile = argv[i]+10;
673658
ImportProject::Type projType = mSettings->project.import(projectFile, mSettings);
674659
mSettings->project.projectType = projType;
675660
if (projType == ImportProject::Type::CPPCHECK_GUI) {
@@ -680,32 +665,27 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
680665
const auto& excludedPaths = mSettings->project.guiProject.excludedPaths;
681666
std::copy(excludedPaths.cbegin(), excludedPaths.cend(), std::back_inserter(mIgnoredPaths));
682667

683-
const std::string platform(mSettings->project.guiProject.platform);
684-
685-
if (platform == "win32A")
686-
mSettings->platform(Settings::Win32A);
687-
else if (platform == "win32W")
688-
mSettings->platform(Settings::Win32W);
689-
else if (platform == "win64")
690-
mSettings->platform(Settings::Win64);
691-
else if (platform == "unix32")
692-
mSettings->platform(Settings::Unix32);
693-
else if (platform == "unix64")
694-
mSettings->platform(Settings::Unix64);
695-
else if (platform == "native")
696-
mSettings->platform(Settings::Native);
697-
else if (platform == "unspecified" || platform == "Unspecified" || platform.empty())
698-
;
699-
else if (!mSettings->loadPlatformFile(projectFile.c_str(), platform, mSettings->verbose) && !mSettings->loadPlatformFile(argv[0], platform, mSettings->verbose)) {
700-
std::string message("unrecognized platform: \"");
701-
message += platform;
702-
message += "\".";
703-
printError(message);
704-
return false;
668+
std::string platform(mSettings->project.guiProject.platform);
669+
670+
// keep existing platform from command-line intact
671+
if (!platform.empty()) {
672+
if (platform == "Unspecified") {
673+
printMessage("'Unspecified' is a deprecated platform type and will be removed in Cppcheck 2.14. Please use 'unspecified' instead.");
674+
platform = "unspecified";
675+
}
676+
677+
std::string errstr;
678+
const std::vector<std::string> paths = {projectFile, argv[0]};
679+
if (!mSettings->platform(platform, errstr, paths)) {
680+
printError(errstr);
681+
return false;
682+
}
705683
}
706684

707-
if (!mSettings->project.guiProject.projectFile.empty())
685+
if (!mSettings->project.guiProject.projectFile.empty()) {
686+
projectFile = mSettings->project.guiProject.projectFile;
708687
projType = mSettings->project.import(mSettings->project.guiProject.projectFile, mSettings);
688+
}
709689
}
710690
if (projType == ImportProject::Type::VS_SLN || projType == ImportProject::Type::VS_VCXPROJ) {
711691
if (mSettings->project.guiProject.analyzeAllVsConfigs == "false")

gui/mainwindow.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
257257
// For other platforms default to unspecified/default which means the
258258
// platform Cppcheck GUI was compiled on.
259259
#if defined(_WIN32)
260-
const Settings::PlatformType defaultPlatform = Settings::Win32W;
260+
const cppcheck::Platform::PlatformType defaultPlatform = cppcheck::Platform::Win32W;
261261
#else
262-
const Settings::PlatformType defaultPlatform = Settings::Unspecified;
262+
const cppcheck::Platform::PlatformType defaultPlatform = cppcheck::Platform::Unspecified;
263263
#endif
264-
Platform &platform = mPlatforms.get((Settings::PlatformType)mSettings->value(SETTINGS_CHECKED_PLATFORM, defaultPlatform).toInt());
264+
Platform &platform = mPlatforms.get((cppcheck::Platform::PlatformType)mSettings->value(SETTINGS_CHECKED_PLATFORM, defaultPlatform).toInt());
265265
platform.mActMainWindow->setChecked(true);
266266

267267
mNetworkAccessManager = new QNetworkAccessManager(this);
@@ -477,7 +477,7 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLibrary, cons
477477
p.ignorePaths(v);
478478

479479
if (!mProjectFile->getAnalyzeAllVsConfigs()) {
480-
const Settings::PlatformType platform = (Settings::PlatformType) mSettings->value(SETTINGS_CHECKED_PLATFORM, 0).toInt();
480+
const cppcheck::Platform::PlatformType platform = (cppcheck::Platform::PlatformType) mSettings->value(SETTINGS_CHECKED_PLATFORM, 0).toInt();
481481
p.selectOneVsConfig(platform);
482482
}
483483
} else {
@@ -1908,7 +1908,7 @@ void MainWindow::selectPlatform()
19081908
{
19091909
QAction *action = qobject_cast<QAction *>(sender());
19101910
if (action) {
1911-
const Settings::PlatformType platform = (Settings::PlatformType) action->data().toInt();
1911+
const cppcheck::Platform::PlatformType platform = (cppcheck::Platform::PlatformType) action->data().toInt();
19121912
mSettings->setValue(SETTINGS_CHECKED_PLATFORM, platform);
19131913
}
19141914
}

gui/platforms.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Platforms::Platforms(QObject *parent)
2424
init();
2525
}
2626

27-
void Platforms::add(const QString &title, Settings::PlatformType platform)
27+
void Platforms::add(const QString &title, cppcheck::Platform::PlatformType platform)
2828
{
2929
Platform plat;
3030
plat.mTitle = title;
@@ -35,20 +35,20 @@ void Platforms::add(const QString &title, Settings::PlatformType platform)
3535

3636
void Platforms::init()
3737
{
38-
add(tr("Native"), Settings::Native);
39-
add(tr("Unix 32-bit"), Settings::Unix32);
40-
add(tr("Unix 64-bit"), Settings::Unix64);
41-
add(tr("Windows 32-bit ANSI"), Settings::Win32A);
42-
add(tr("Windows 32-bit Unicode"), Settings::Win32W);
43-
add(tr("Windows 64-bit"), Settings::Win64);
38+
add(tr("Native"), cppcheck::Platform::Native);
39+
add(tr("Unix 32-bit"), cppcheck::Platform::Unix32);
40+
add(tr("Unix 64-bit"), cppcheck::Platform::Unix64);
41+
add(tr("Windows 32-bit ANSI"), cppcheck::Platform::Win32A);
42+
add(tr("Windows 32-bit Unicode"), cppcheck::Platform::Win32W);
43+
add(tr("Windows 64-bit"), cppcheck::Platform::Win64);
4444
}
4545

4646
int Platforms::getCount() const
4747
{
4848
return mPlatforms.count();
4949
}
5050

51-
Platform& Platforms::get(Settings::PlatformType platform)
51+
Platform& Platforms::get(cppcheck::Platform::PlatformType platform)
5252
{
5353
QList<Platform>::iterator iter = mPlatforms.begin();
5454
while (iter != mPlatforms.end()) {

gui/platforms.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#ifndef PLATFORMS_H
2020
#define PLATFORMS_H
2121

22-
#include "settings.h"
22+
#include "platform.h"
2323

2424
#include <QList>
2525
#include <QObject>
@@ -35,7 +35,7 @@ class QAction;
3535
*/
3636
struct Platform {
3737
QString mTitle; /**< Text visible in the GUI. */
38-
Settings::PlatformType mType; /**< Type in the core. */
38+
cppcheck::Platform::PlatformType mType; /**< Type in the core. */
3939
QAction *mActMainWindow; /**< Pointer to main window action item. */
4040
};
4141

@@ -47,10 +47,10 @@ class Platforms : public QObject {
4747

4848
public:
4949
explicit Platforms(QObject *parent = nullptr);
50-
void add(const QString &title, Settings::PlatformType platform);
50+
void add(const QString &title, cppcheck::Platform::PlatformType platform);
5151
int getCount() const;
5252
void init();
53-
Platform& get(Settings::PlatformType platform);
53+
Platform& get(cppcheck::Platform::PlatformType platform);
5454

5555
QList<Platform> mPlatforms;
5656
};

gui/projectfile.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void ProjectFile::clear()
5353
mRootPath.clear();
5454
mBuildDir.clear();
5555
mImportProject.clear();
56-
mAnalyzeAllVsConfigs = true;
5756
mIncludeDirs.clear();
5857
mDefines.clear();
5958
mUndefines.clear();

lib/checktype.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static const struct CWE CWE190(190U); // Integer Overflow or Wraparound
5757
void CheckType::checkTooBigBitwiseShift()
5858
{
5959
// unknown sizeof(int) => can't run this checker
60-
if (mSettings->platformType == Settings::Unspecified)
60+
if (mSettings->platformType == cppcheck::Platform::Unspecified)
6161
return;
6262

6363
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
@@ -160,7 +160,7 @@ void CheckType::tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, con
160160
void CheckType::checkIntegerOverflow()
161161
{
162162
// unknown sizeof(int) => can't run this checker
163-
if (mSettings->platformType == Settings::Unspecified || mSettings->int_bit >= MathLib::bigint_bits)
163+
if (mSettings->platformType == cppcheck::Platform::Unspecified || mSettings->int_bit >= MathLib::bigint_bits)
164164
return;
165165

166166
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
@@ -436,7 +436,7 @@ void CheckType::checkFloatToIntegerOverflow(const Token *tok, const ValueType *v
436436
floatToIntegerOverflowError(tok, f);
437437
else if ((-f.floatValue) > std::exp2(mSettings->long_long_bit - 1))
438438
floatToIntegerOverflowError(tok, f);
439-
else if (mSettings->platformType != Settings::Unspecified) {
439+
else if (mSettings->platformType != cppcheck::Platform::Unspecified) {
440440
int bits = 0;
441441
if (vtint->type == ValueType::Type::CHAR)
442442
bits = mSettings->char_bit;

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ unsigned int CppCheck::check(const ImportProject::FileSettings &fs)
586586
temp.mSettings.standards.setCPP(fs.standard);
587587
else if (!fs.standard.empty())
588588
temp.mSettings.standards.setC(fs.standard);
589-
if (fs.platformType != Settings::Unspecified)
589+
if (fs.platformType != cppcheck::Platform::Unspecified)
590590
temp.mSettings.platform(fs.platformType);
591591
if (mSettings.clang) {
592592
temp.mSettings.includePaths.insert(temp.mSettings.includePaths.end(), fs.systemIncludePaths.cbegin(), fs.systemIncludePaths.cend());

0 commit comments

Comments
 (0)