Skip to content

Commit

Permalink
fixed some "Variable can be made constexpr" Rider warnings (danmar#5615)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Nov 3, 2023
1 parent 9280b5d commit 63e00ea
Show file tree
Hide file tree
Showing 39 changed files with 600 additions and 600 deletions.
4 changes: 2 additions & 2 deletions cli/cppcheckexecutorsig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@

// TODO: __USE_DYNAMIC_STACK_SIZE is dependent on the features.h include and not a built-in compiler define, so it might be problematic to depend on it
#ifdef __USE_DYNAMIC_STACK_SIZE
static const size_t MYSTACKSIZE = 16*1024+32768; // wild guess about a reasonable buffer
static constexpr size_t MYSTACKSIZE = 16*1024+32768; // wild guess about a reasonable buffer
#else
static const size_t MYSTACKSIZE = 16*1024+SIGSTKSZ; // wild guess about a reasonable buffer
static constexpr size_t MYSTACKSIZE = 16*1024+SIGSTKSZ; // wild guess about a reasonable buffer
#endif
static char mytstack[MYSTACKSIZE]= {0}; // alternative stack for signal handler
static bool bStackBelowHeap=false; // lame attempt to locate heap vs. stack address space. See CppCheckExecutor::check_wrapper()
Expand Down
2 changes: 1 addition & 1 deletion cli/stacktrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void print_stacktrace(FILE* output, bool demangling, int maxdepth, bool lowMem)
const int fd = fileno(output);
void *callstackArray[32]= {nullptr}; // the less resources the better...
const int currentdepth = backtrace(callstackArray, (int)getArrayLength(callstackArray));
const int offset=2; // some entries on top are within our own exception handling code or libc
constexpr int offset=2; // some entries on top are within our own exception handling code or libc
if (maxdepth<0)
maxdepth=currentdepth-offset;
else
Expand Down
5 changes: 3 additions & 2 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,14 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
mUI->mActionEnforceCpp->setActionGroup(mSelectLanguageActions);
mUI->mActionAutoDetectLanguage->setActionGroup(mSelectLanguageActions);

// TODO: we no longer default to a Windows platform in CLI - so we should probably also get rid of this in the GUI
// For Windows platforms default to Win32 checked platform.
// For other platforms default to unspecified/default which means the
// platform Cppcheck GUI was compiled on.
#if defined(_WIN32)
const Platform::Type defaultPlatform = Platform::Type::Win32W;
constexpr Platform::Type defaultPlatform = Platform::Type::Win32W;
#else
const Platform::Type defaultPlatform = Platform::Type::Unspecified;
constexpr Platform::Type defaultPlatform = Platform::Type::Unspecified;
#endif
PlatformData &platform = mPlatforms.get((Platform::Type)mSettings->value(SETTINGS_CHECKED_PLATFORM, defaultPlatform).toInt());
platform.mActMainWindow->setChecked(true);
Expand Down
16 changes: 8 additions & 8 deletions gui/projectfiledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@
#include <QVariant>
#include <QtCore>

static const char ADDON_MISRA[] = "misra";
static const char CODING_STANDARD_MISRA_C_2023[] = "misra-c-2023";
static const char CODING_STANDARD_MISRA_CPP_2008[] = "misra-cpp-2008";
static const char CODING_STANDARD_CERT_C[] = "cert-c-2016";
static const char CODING_STANDARD_CERT_CPP[] = "cert-cpp-2016";
static const char CODING_STANDARD_AUTOSAR[] = "autosar";
static constexpr char ADDON_MISRA[] = "misra";
static constexpr char CODING_STANDARD_MISRA_C_2023[] = "misra-c-2023";
static constexpr char CODING_STANDARD_MISRA_CPP_2008[] = "misra-cpp-2008";
static constexpr char CODING_STANDARD_CERT_C[] = "cert-c-2016";
static constexpr char CODING_STANDARD_CERT_CPP[] = "cert-cpp-2016";
static constexpr char CODING_STANDARD_AUTOSAR[] = "autosar";

/** Return paths from QListWidget */
static QStringList getPaths(const QListWidget *list)
Expand All @@ -77,7 +77,7 @@ static QStringList getPaths(const QListWidget *list)
}

/** Platforms shown in the platform combobox */
static const Platform::Type builtinPlatforms[] = {
static constexpr Platform::Type builtinPlatforms[] = {
Platform::Type::Native,
Platform::Type::Win32A,
Platform::Type::Win32W,
Expand All @@ -86,7 +86,7 @@ static const Platform::Type builtinPlatforms[] = {
Platform::Type::Unix64
};

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

QStringList ProjectFileDialog::getProjectConfigs(const QString &fileName)
{
Expand Down
34 changes: 17 additions & 17 deletions gui/resultstree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,25 @@
#include <QVariantMap>
#include <Qt>

static const char COLUMN[] = "column";
static const char CWE[] = "cwe";
static const char ERRORID[] = "id";
static const char FILENAME[] = "file";
static const char FILE0[] = "file0";
static const char HASH[] = "hash";
static const char HIDE[] = "hide";
static const char INCONCLUSIVE[] = "inconclusive";
static const char LINE[] = "line";
static const char MESSAGE[] = "message";
static const char SEVERITY[] = "severity";
static const char SINCEDATE[] = "sinceDate";
static const char SYMBOLNAMES[] = "symbolNames";
static const char SUMMARY[] = "summary";
static const char TAGS[] = "tags";
static constexpr char COLUMN[] = "column";
static constexpr char CWE[] = "cwe";
static constexpr char ERRORID[] = "id";
static constexpr char FILENAME[] = "file";
static constexpr char FILE0[] = "file0";
static constexpr char HASH[] = "hash";
static constexpr char HIDE[] = "hide";
static constexpr char INCONCLUSIVE[] = "inconclusive";
static constexpr char LINE[] = "line";
static constexpr char MESSAGE[] = "message";
static constexpr char SEVERITY[] = "severity";
static constexpr char SINCEDATE[] = "sinceDate";
static constexpr char SYMBOLNAMES[] = "symbolNames";
static constexpr char SUMMARY[] = "summary";
static constexpr char TAGS[] = "tags";

// These must match column headers given in ResultsTree::translate()
static const int COLUMN_SINCE_DATE = 6;
static const int COLUMN_TAGS = 7;
static constexpr int COLUMN_SINCE_DATE = 6;
static constexpr int COLUMN_TAGS = 7;

ResultsTree::ResultsTree(QWidget * parent) :
QTreeView(parent)
Expand Down
2 changes: 1 addition & 1 deletion gui/settingsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ protected slots:
private:
void manageStyleControls();

static const int mLangCodeRole = Qt::UserRole;
static constexpr int mLangCodeRole = Qt::UserRole;

bool mPremium;
};
Expand Down
4 changes: 2 additions & 2 deletions gui/xmlreport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
#include <QStringRef>
#endif

static const char ResultElementName[] = "results";
static const char VersionAttribute[] = "version";
static constexpr char ResultElementName[] = "results";
static constexpr char VersionAttribute[] = "version";

XmlReport::XmlReport(const QString &filename) :
Report(filename)
Expand Down
4 changes: 2 additions & 2 deletions lib/calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ R calculate(const std::string& s, const T& x, const T& y, bool* error = nullptr)
auto wrap = [](T z) {
return R{z};
};
const MathLib::bigint maxBitsShift = sizeof(MathLib::bigint) * 8;
constexpr MathLib::bigint maxBitsShift = sizeof(MathLib::bigint) * 8;
// For portability we cannot shift signed integers by 63 bits
const MathLib::bigint maxBitsSignedShift = maxBitsShift - 1;
constexpr MathLib::bigint maxBitsSignedShift = maxBitsShift - 1;
switch (MathLib::encodeMultiChar(s)) {
case '+':
return wrap(x + y);
Expand Down
3 changes: 1 addition & 2 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,7 @@ void CheckClass::missingMemberCopyError(const Token *tok, Function::Type functio
const std::string message =
"$symbol:" + classname + "::" + varname + "\n" +
"Member variable '$symbol' is not assigned in the " + ctor + " constructor. Should it be " + action;
const char id[] = "missingMemberCopy";
reportError(tok, Severity::warning, id, message, CWE398, Certainty::inconclusive);
reportError(tok, Severity::warning, "missingMemberCopy", message, CWE398, Certainty::inconclusive);
}

void CheckClass::operatorEqVarError(const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive)
Expand Down
8 changes: 4 additions & 4 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ static const CWE CWE672(672U);
static const CWE CWE415(415U);

// Hardcoded allocation types (not from library)
static const int NEW_ARRAY = -2;
static const int NEW = -1;
static constexpr int NEW_ARRAY = -2;
static constexpr int NEW = -1;

static const std::array<std::pair<std::string, std::string>, 4> alloc_failed_conds {{{"==", "0"}, {"<", "0"}, {"==", "-1"}, {"<=", "-1"}}};
static const std::array<std::pair<std::string, std::string>, 4> alloc_success_conds {{{"!=", "0"}, {">", "0"}, {"!=", "-1"}, {">=", "0"}}};
Expand Down Expand Up @@ -304,9 +304,9 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
static const nonneg int recursiveLimit = 300;
#elif defined(__MINGW32__)
// testrunner crashes with stack overflow in CI
static const nonneg int recursiveLimit = 600;
static constexpr nonneg int recursiveLimit = 600;
#else
static const nonneg int recursiveLimit = 1000;
static constexpr nonneg int recursiveLimit = 1000;
#endif
if (++recursiveCount > recursiveLimit) // maximum number of "else if ()"
throw InternalError(startToken, "Internal limit: CheckLeakAutoVar::checkScope() Maximum recursive count of 1000 reached.", InternalError::LIMIT);
Expand Down
12 changes: 6 additions & 6 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3960,9 +3960,9 @@ void CheckOther::checkOverlappingWrite()
if (nonOverlappingData->sizeArg <= 0 || nonOverlappingData->sizeArg > args.size()) {
if (nonOverlappingData->sizeArg == -1) {
ErrorPath errorPath;
const bool macro = true;
const bool pure = true;
const bool follow = true;
constexpr bool macro = true;
constexpr bool pure = true;
constexpr bool follow = true;
if (!isSameExpression(mTokenizer->isCPP(), macro, ptr1, ptr2, mSettings->library, pure, follow, &errorPath))
continue;
overlappingWriteFunction(tok);
Expand All @@ -3985,9 +3985,9 @@ void CheckOther::checkOverlappingWrite()
continue;

ErrorPath errorPath;
const bool macro = true;
const bool pure = true;
const bool follow = true;
constexpr bool macro = true;
constexpr bool pure = true;
constexpr bool follow = true;
if (!isSameExpression(mTokenizer->isCPP(), macro, buf1, buf2, mSettings->library, pure, follow, &errorPath))
continue;
overlappingWriteFunction(tok);
Expand Down
2 changes: 1 addition & 1 deletion lib/checkstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void CheckString::checkAlwaysTrueOrFalseStringCompare()

void CheckString::alwaysTrueFalseStringCompareError(const Token *tok, const std::string& str1, const std::string& str2)
{
const std::size_t stringLen = 10;
constexpr std::size_t stringLen = 10;
const std::string string1 = (str1.size() < stringLen) ? str1 : (str1.substr(0, stringLen-2) + "..");
const std::string string2 = (str2.size() < stringLen) ? str2 : (str2.substr(0, stringLen-2) + "..");

Expand Down
4 changes: 2 additions & 2 deletions lib/checktype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void CheckType::checkTooBigBitwiseShift()

void CheckType::tooBigBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits)
{
const char id[] = "shiftTooManyBits";
constexpr char id[] = "shiftTooManyBits";

if (!tok) {
reportError(tok, Severity::error, id, "Shifting 32-bit value by 40 bits is undefined behaviour", CWE758, Certainty::normal);
Expand All @@ -131,7 +131,7 @@ void CheckType::tooBigBitwiseShiftError(const Token *tok, int lhsbits, const Val

void CheckType::tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits)
{
const char id[] = "shiftTooManyBitsSigned";
constexpr char id[] = "shiftTooManyBitsSigned";

const bool cpp14 = mSettings->standards.cpp >= Standards::CPP14;

Expand Down
10 changes: 5 additions & 5 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@

class SymbolDatabase;

static const char Version[] = CPPCHECK_VERSION_STRING;
static const char ExtraVersion[] = "";
static constexpr char Version[] = CPPCHECK_VERSION_STRING;
static constexpr char ExtraVersion[] = "";

static const char FILELIST[] = "cppcheck-addon-ctu-file-list";
static constexpr char FILELIST[] = "cppcheck-addon-ctu-file-list";

static TimerResults s_timerResults;

Expand Down Expand Up @@ -1636,9 +1636,9 @@ void CppCheck::analyseClangTidy(const FileSettings &fileSettings)
const std::string allDefines = getDefinesFlags(fileSettings.defines);

#ifdef _WIN32
const char exe[] = "clang-tidy.exe";
constexpr char exe[] = "clang-tidy.exe";
#else
const char exe[] = "clang-tidy";
constexpr char exe[] = "clang-tidy";
#endif

const std::string args = "-quiet -checks=*,-clang-analyzer-*,-llvm* \"" + fileSettings.filename + "\" -- " + allIncludes + allDefines;
Expand Down
30 changes: 15 additions & 15 deletions lib/ctu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@
#include <tinyxml2.h>
//---------------------------------------------------------------------------

static const char ATTR_CALL_ID[] = "call-id";
static const char ATTR_CALL_FUNCNAME[] = "call-funcname";
static const char ATTR_CALL_ARGNR[] = "call-argnr";
static const char ATTR_CALL_ARGEXPR[] = "call-argexpr";
static const char ATTR_CALL_ARGVALUETYPE[] = "call-argvaluetype";
static const char ATTR_CALL_ARGVALUE[] = "call-argvalue";
static const char ATTR_WARNING[] = "warning";
static const char ATTR_LOC_FILENAME[] = "file";
static const char ATTR_LOC_LINENR[] = "line";
static const char ATTR_LOC_COLUMN[] = "col";
static const char ATTR_INFO[] = "info";
static const char ATTR_MY_ID[] = "my-id";
static const char ATTR_MY_ARGNR[] = "my-argnr";
static const char ATTR_MY_ARGNAME[] = "my-argname";
static const char ATTR_VALUE[] = "value";
static constexpr char ATTR_CALL_ID[] = "call-id";
static constexpr char ATTR_CALL_FUNCNAME[] = "call-funcname";
static constexpr char ATTR_CALL_ARGNR[] = "call-argnr";
static constexpr char ATTR_CALL_ARGEXPR[] = "call-argexpr";
static constexpr char ATTR_CALL_ARGVALUETYPE[] = "call-argvaluetype";
static constexpr char ATTR_CALL_ARGVALUE[] = "call-argvalue";
static constexpr char ATTR_WARNING[] = "warning";
static constexpr char ATTR_LOC_FILENAME[] = "file";
static constexpr char ATTR_LOC_LINENR[] = "line";
static constexpr char ATTR_LOC_COLUMN[] = "col";
static constexpr char ATTR_INFO[] = "info";
static constexpr char ATTR_MY_ID[] = "my-id";
static constexpr char ATTR_MY_ARGNR[] = "my-argnr";
static constexpr char ATTR_MY_ARGNAME[] = "my-argname";
static constexpr char ATTR_VALUE[] = "value";

int CTU::maxCtuDepth = 2;

Expand Down
16 changes: 8 additions & 8 deletions lib/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ static constexpr bool caseInsensitiveFilesystem()
std::string Path::toNativeSeparators(std::string path)
{
#if defined(_WIN32)
const char separ = '/';
const char native = '\\';
constexpr char separ = '/';
constexpr char native = '\\';
#else
const char separ = '\\';
const char native = '/';
constexpr char separ = '\\';
constexpr char native = '/';
#endif
std::replace(path.begin(), path.end(), separ, native);
return path;
}

std::string Path::fromNativeSeparators(std::string path)
{
const char nonnative = '\\';
const char newsepar = '/';
constexpr char nonnative = '\\';
constexpr char newsepar = '/';
std::replace(path.begin(), path.end(), nonnative, newsepar);
return path;
}
Expand Down Expand Up @@ -250,9 +250,9 @@ std::string Path::getAbsoluteFilePath(const std::string& filePath)
std::string Path::stripDirectoryPart(const std::string &file)
{
#if defined(_WIN32) && !defined(__MINGW32__)
const char native = '\\';
constexpr char native = '\\';
#else
const char native = '/';
constexpr char native = '/';
#endif

const std::string::size_type p = file.rfind(native);
Expand Down
2 changes: 1 addition & 1 deletion lib/summaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void Summaries::loadReturn(const std::string &buildDir, std::set<std::string> &s
std::string line;
while (std::getline(fin, line)) {
// Get function name
const std::string::size_type pos1 = 0;
constexpr std::string::size_type pos1 = 0;
const std::string::size_type pos2 = line.find(' ', pos1);
const std::string functionName = (pos2 == std::string::npos) ? line : line.substr(0, pos2);
std::vector<std::string> call = getSummaryData(line, "call");
Expand Down
2 changes: 1 addition & 1 deletion lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3763,7 +3763,7 @@ void TemplateSimplifier::simplifyTemplates(const std::time_t maxtime)
mTokenizer.calculateScopes();

unsigned int passCount = 0;
const unsigned int passCountMax = 10;
constexpr unsigned int passCountMax = 10;
for (; passCount < passCountMax; ++passCount) {
if (passCount) {
// it may take more than one pass to simplify type aliases
Expand Down
2 changes: 1 addition & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
// How many compileExpression recursions are allowed?
// For practical code this could be endless. But in some special torture test
// there needs to be a limit.
static const int AST_MAX_DEPTH = 150;
static constexpr int AST_MAX_DEPTH = 150;


TokenList::TokenList(const Settings* settings) :
Expand Down
6 changes: 3 additions & 3 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ static ValueFlow::Value castValue(ValueFlow::Value value, const ValueType::Sign
}
}
if (bit < MathLib::bigint_bits) {
const MathLib::biguint one = 1;
constexpr MathLib::biguint one = 1;
value.intvalue &= (one << bit) - 1;
if (sign == ValueType::Sign::SIGNED && value.intvalue & (one << (bit - 1))) {
value.intvalue |= ~((one << bit) - 1ULL);
Expand Down Expand Up @@ -3158,7 +3158,7 @@ struct ExpressionAnalyzer : SingleValueFlowAnalyzer {
}

void setupExprVarIds(const Token* start, int depth = 0) {
const int maxDepth = 4;
constexpr int maxDepth = 4;
if (depth > maxDepth)
return;
visitAstNodes(start, [&](const Token* tok) {
Expand Down Expand Up @@ -8459,7 +8459,7 @@ struct IteratorConditionHandler : SimpleConditionHandler {
if (!tok->astOperand1() || !tok->astOperand2())
return {};

const ValueFlow::Value::ValueKind kind = ValueFlow::Value::ValueKind::Known;
constexpr ValueFlow::Value::ValueKind kind = ValueFlow::Value::ValueKind::Known;
std::list<ValueFlow::Value> values = getIteratorValues(tok->astOperand1()->values(), &kind);
if (!values.empty()) {
cond.vartok = tok->astOperand2();
Expand Down
Loading

0 comments on commit 63e00ea

Please sign in to comment.