From 1049973cef5e38818ec0346480dd61a68ecb3eea Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 16 Sep 2025 00:48:53 +0200 Subject: [PATCH 1/2] fixed some `-Wnrvo` Clang compiler warnings [skip ci] --- lib/checkbufferoverrun.cpp | 6 +-- lib/checkstl.cpp | 2 +- lib/errorlogger.cpp | 76 ++++++++++++++++---------------- lib/fwdanalysis.cpp | 10 +++-- lib/library.cpp | 4 +- lib/mathlib.cpp | 6 +-- lib/programmemory.cpp | 24 +++++----- lib/symboldatabase.cpp | 5 ++- lib/token.cpp | 8 ++-- lib/valueflow.cpp | 89 +++++++++++++++++++++----------------- lib/vf_analyzers.cpp | 12 ++--- tools/dmake/dmake.cpp | 14 +++--- 12 files changed, 140 insertions(+), 116 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 337f2b1de3f..dc0169e13b0 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -284,9 +284,9 @@ static std::vector getOverrunIndexValues(const Token* tok, overflow = true; indexValues.push_back(values.front()); } - if (overflow) - return indexValues; - return {}; + if (!overflow) + indexValues.clear(); + return indexValues; } void CheckBufferOverrun::arrayIndex() diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index a88591a904a..fbd2048f441 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -216,7 +216,7 @@ static std::string indexValueString(const ValueFlow::Value& indexValue, const st indexString += "+" + MathLib::toString(indexValue.intvalue); } if (indexValue.bound == ValueFlow::Value::Bound::Lower) - return "greater or equal to " + indexString; + indexString = "greater or equal to " + indexString; return indexString; } diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 47157f7a504..6ff3e0a6eef 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -1055,47 +1055,49 @@ std::string getGuideline(const std::string &errId, ReportType reportType, const std::map &guidelineMapping, Severity severity) { - std::string guideline; + { + std::string guideline; - switch (reportType) { - case ReportType::autosar: - if (errId.rfind("premium-autosar-", 0) == 0) { - guideline = errId.substr(16); + switch (reportType) { + case ReportType::autosar: + if (errId.rfind("premium-autosar-", 0) == 0) { + guideline = errId.substr(16); + break; + } + if (errId.rfind("premium-misra-cpp-2008-", 0) == 0) + guideline = "M" + errId.substr(23); + break; + case ReportType::certC: + case ReportType::certCpp: + if (errId.rfind("premium-cert-", 0) == 0) { + guideline = errId.substr(13); + std::transform(guideline.begin(), guideline.end(), + guideline.begin(), static_cast(std::toupper)); + } + break; + case ReportType::misraC2012: + case ReportType::misraC2023: + case ReportType::misraC2025: + if (errId.rfind("misra-c20", 0) == 0 || errId.rfind("premium-misra-c-20", 0) == 0) + guideline = errId.substr(errId.rfind('-') + 1); + break; + case ReportType::misraCpp2008: + if (errId.rfind("premium-misra-cpp-2008", 0) == 0) + guideline = errId.substr(23); + break; + case ReportType::misraCpp2023: + if (errId.rfind("premium-misra-cpp-2023", 0) == 0) + guideline = errId.substr(errId.rfind('-') + 1); + break; + default: break; } - if (errId.rfind("premium-misra-cpp-2008-", 0) == 0) - guideline = "M" + errId.substr(23); - break; - case ReportType::certC: - case ReportType::certCpp: - if (errId.rfind("premium-cert-", 0) == 0) { - guideline = errId.substr(13); - std::transform(guideline.begin(), guideline.end(), - guideline.begin(), static_cast(std::toupper)); - } - break; - case ReportType::misraC2012: - case ReportType::misraC2023: - case ReportType::misraC2025: - if (errId.rfind("misra-c20", 0) == 0 || errId.rfind("premium-misra-c-20", 0) == 0) - guideline = errId.substr(errId.rfind('-') + 1); - break; - case ReportType::misraCpp2008: - if (errId.rfind("premium-misra-cpp-2008", 0) == 0) - guideline = errId.substr(23); - break; - case ReportType::misraCpp2023: - if (errId.rfind("premium-misra-cpp-2023", 0) == 0) - guideline = errId.substr(errId.rfind('-') + 1); - break; - default: - break; - } - if (!guideline.empty()) { - if (errId.find("-dir-") != std::string::npos) - guideline = "Dir " + guideline; - return guideline; + if (!guideline.empty()) { + if (errId.find("-dir-") != std::string::npos) + guideline = "Dir " + guideline; + return guideline; + } } auto it = guidelineMapping.find(errId); diff --git a/lib/fwdanalysis.cpp b/lib/fwdanalysis.cpp index 11434e5356d..c3d8c8e16c6 100644 --- a/lib/fwdanalysis.cpp +++ b/lib/fwdanalysis.cpp @@ -139,10 +139,12 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token * if (!opTok) opTok = tok->next(); std::pair startEndTokens = opTok->findExpressionStartEndTokens(); - FwdAnalysis::Result result = - checkRecursive(expr, startEndTokens.first, startEndTokens.second->next(), exprVarIds, local, true, depth); - if (result.type != Result::Type::NONE) - return result; + { + FwdAnalysis::Result result = + checkRecursive(expr, startEndTokens.first, startEndTokens.second->next(), exprVarIds, local, true, depth); + if (result.type != Result::Type::NONE) + return result; + } // #9167: if the return is inside an inner class, it does not tell us anything if (!inInnerClass) { diff --git a/lib/library.cpp b/lib/library.cpp index 3554d5cbdd4..9822b42ddd7 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1213,8 +1213,8 @@ std::string Library::getFunctionName(const Token *ftok) const const Token * tok = ftok->astParent()->isUnaryOp("&") ? ftok->astParent()->astOperand1() : ftok->next()->astOperand1(); std::string ret = getFunctionName(tok, error); if (error) - return {}; - if (startsWith(ret, "::")) + ret.clear(); + else if (startsWith(ret, "::")) ret.erase(0, 2); return ret; } diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 54240fa4f2f..e098bee6973 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -564,9 +564,9 @@ template<> std::string MathLib::toString(double value) result << value; std::string s = result.str(); if (s == "-0") - return "0.0"; - if (s.find_first_of(".e") == std::string::npos) - return s + ".0"; + s = "0.0"; + else if (s.find_first_of(".e") == std::string::npos) + s += ".0"; return s; } diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 55419879055..c72ae0a52e8 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -1395,17 +1395,19 @@ namespace { return unknown(); std::unordered_map condValues = executeAll(conditions1, &b); bool allNegated = true; - ValueFlow::Value negatedValue = unknown(); - for (const auto& p : condValues) { - const ValueFlow::Value& v = p.second; - if (isTrueOrFalse(v, b)) - return v; - allNegated &= isTrueOrFalse(v, !b); - if (allNegated && negatedValue.isUninitValue()) - negatedValue = v; + { + ValueFlow::Value negatedValue = unknown(); + for (const auto& p : condValues) { + const ValueFlow::Value& v = p.second; + if (isTrueOrFalse(v, b)) + return v; + allNegated &= isTrueOrFalse(v, !b); + if (allNegated && negatedValue.isUninitValue()) + negatedValue = v; + } + if (condValues.size() == conditions1.size() && allNegated) + return negatedValue; } - if (condValues.size() == conditions1.size() && allNegated) - return negatedValue; if (n > 4) return unknown(); if (!sortConditions(conditions1)) @@ -1805,7 +1807,7 @@ namespace { if (elseStart) result = execute(elseStart->scope()); } else { - return {unknown()}; + result = {unknown()}; } if (!result.empty()) return result; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 646ca5a0380..63f6c188a4c 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -8300,10 +8300,13 @@ bool ValueType::fromLibraryType(const std::string &typestr, const Settings &sett std::string ValueType::dump() const { + if (type == UNKNOWN_TYPE) + return ""; + std::string ret; switch (type) { case UNKNOWN_TYPE: - return ""; + break; // never happens case NONSTD: ret += "valueType-type=\"nonstd\""; break; diff --git a/lib/token.cpp b/lib/token.cpp index 1735e2eac8c..cb712a14e62 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2438,9 +2438,11 @@ std::pair Token::typeDecl(const Token* tok, bool poi varTok = varTok->next(); while (Token::Match(varTok, "%name% ::")) varTok = varTok->tokAt(2); - std::pair r = typeDecl(varTok); - if (r.first) - return r; + { + std::pair r = typeDecl(varTok); + if (r.first) + return r; + } if (pointedToType && tok2->astOperand1() && Token::simpleMatch(tok2, "new")) { if (Token::simpleMatch(tok2->astOperand1(), "(")) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 1f725673874..28e910a387c 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -781,9 +781,11 @@ static void valueFlowTypeTraits(TokenList& tokenlist, const Settings& settings) eval["is_reference"] = [&](const std::vector>& args) { if (args.size() != 1) return ValueFlow::Value::unknown(); - ValueFlow::Value isRValue = eval["is_rvalue_reference"](args); - if (isRValue.isUninitValue() || isRValue.intvalue == 1) - return isRValue; + { + ValueFlow::Value isRValue = eval["is_rvalue_reference"](args); + if (isRValue.isUninitValue() || isRValue.intvalue == 1) + return isRValue; + } return eval["is_lvalue_reference"](args); }; for (Token* tok = tokenlist.front(); tok; tok = tok->next()) { @@ -5116,21 +5118,22 @@ static void valueFlowCondition(const ValuePtr& handler, struct SimpleConditionHandler : ConditionHandler { std::vector parse(const Token* tok, const Settings& /*settings*/) const override { - - std::vector conds; - parseCompareEachInt(tok, [&](const Token* vartok, ValueFlow::Value true_value, ValueFlow::Value false_value) { - if (vartok->hasKnownIntValue()) - return; - if (vartok->str() == "=" && vartok->astOperand1() && vartok->astOperand2()) - vartok = vartok->astOperand1(); - Condition cond; - cond.true_values.push_back(std::move(true_value)); - cond.false_values.push_back(std::move(false_value)); - cond.vartok = vartok; - conds.push_back(std::move(cond)); - }); - if (!conds.empty()) - return conds; + { + std::vector conds; + parseCompareEachInt(tok, [&](const Token* vartok, ValueFlow::Value true_value, ValueFlow::Value false_value) { + if (vartok->hasKnownIntValue()) + return; + if (vartok->str() == "=" && vartok->astOperand1() && vartok->astOperand2()) + vartok = vartok->astOperand1(); + Condition cond; + cond.true_values.push_back(std::move(true_value)); + cond.false_values.push_back(std::move(false_value)); + cond.vartok = vartok; + conds.push_back(std::move(cond)); + }); + if (!conds.empty()) + return conds; + } const Token* vartok = nullptr; @@ -6637,9 +6640,11 @@ static std::vector getContainerSizeFromConstructorArgs(const s } else if (astIsContainer(args[0]) && args.size() == 1) { // copy constructor return getContainerValues(args[0]); } else if (isIteratorPair(args)) { - std::vector result = getContainerValues(args[0]); - if (!result.empty()) - return result; + { + std::vector result = getContainerValues(args[0]); + if (!result.empty()) + return result; + } // (ptr, ptr + size) if (astIsPointer(args[0]) && args[0]->exprId() != 0) { // (ptr, ptr) is empty @@ -6940,21 +6945,23 @@ static void valueFlowContainerSize(const TokenList& tokenlist, struct ContainerConditionHandler : ConditionHandler { std::vector parse(const Token* tok, const Settings& settings) const override { - std::vector conds; - parseCompareEachInt(tok, [&](const Token* vartok, ValueFlow::Value true_value, ValueFlow::Value false_value) { - vartok = settings.library.getContainerFromYield(vartok, Library::Container::Yield::SIZE); - if (!vartok) - return; - true_value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; - false_value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; - Condition cond; - cond.true_values.push_back(std::move(true_value)); - cond.false_values.push_back(std::move(false_value)); - cond.vartok = vartok; - conds.push_back(std::move(cond)); - }); - if (!conds.empty()) - return conds; + { + std::vector conds; + parseCompareEachInt(tok, [&](const Token* vartok, ValueFlow::Value true_value, ValueFlow::Value false_value) { + vartok = settings.library.getContainerFromYield(vartok, Library::Container::Yield::SIZE); + if (!vartok) + return; + true_value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; + false_value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; + Condition cond; + cond.true_values.push_back(std::move(true_value)); + cond.false_values.push_back(std::move(false_value)); + cond.vartok = vartok; + conds.push_back(std::move(cond)); + }); + if (!conds.empty()) + return conds; + } const Token* vartok = nullptr; @@ -7645,10 +7652,12 @@ std::vector ValueFlow::isOutOfBounds(const Value& size, const ValueFlow::Value inBoundsValue = inferCondition("<", indexTok, size.intvalue); if (inBoundsValue.isKnown() && inBoundsValue.intvalue != 0) return {}; - std::vector result = isOutOfBoundsImpl(size, indexTok, false); - if (!result.empty()) - return result; + { + std::vector result = isOutOfBoundsImpl(size, indexTok, false); + if (!result.empty()) + return result; + } if (!possible) - return result; + return {}; return isOutOfBoundsImpl(size, indexTok, true); } diff --git a/lib/vf_analyzers.cpp b/lib/vf_analyzers.cpp index fc64f237c39..b62e9053614 100644 --- a/lib/vf_analyzers.cpp +++ b/lib/vf_analyzers.cpp @@ -581,9 +581,11 @@ struct ValueFlowAnalyzer : Analyzer { (Token::Match(parent, "*|[") || (parent && parent->originalName() == "->")) && getIndirect(tok) <= 0) return Action::Read; - Action w = isWritable(tok, d); - if (w != Action::None) - return w; + { + Action w = isWritable(tok, d); + if (w != Action::None) + return w; + } // Check for modifications by function calls return isModified(tok); @@ -624,7 +626,7 @@ struct ValueFlowAnalyzer : Analyzer { if (Token::Match(tok->astParent(), "%assign%") && astIsLHS(tok)) a |= Action::Invalid; if (inconclusiveRef && a.isModified()) - return Action::Inconclusive; + a = Action::Inconclusive; return a; } if (la.isRead()) { @@ -636,7 +638,7 @@ struct ValueFlowAnalyzer : Analyzer { inconclusive |= inconclusiveRef; Action a = isAliasModified(tok); if (inconclusive && a.isModified()) - return Action::Inconclusive; + a = Action::Inconclusive; return a; } if (isSameSymbolicValue(ref)) diff --git a/tools/dmake/dmake.cpp b/tools/dmake/dmake.cpp index 740b265d6c5..9695b82194a 100644 --- a/tools/dmake/dmake.cpp +++ b/tools/dmake/dmake.cpp @@ -168,12 +168,14 @@ static void compilefiles(std::ostream &fout, const std::vector &fil static std::string getCppFiles(std::vector &files, const std::string &path, bool recursive) { std::list filelist; - const std::set extra; - const std::vector masks; - const PathMatch matcher(masks, Path::getCurrentPath()); - std::string err = FileLister::addFiles(filelist, path, extra, recursive, matcher); - if (!err.empty()) - return err; + { + const std::set extra; + const std::vector masks; + const PathMatch matcher(masks, Path::getCurrentPath()); + std::string err = FileLister::addFiles(filelist, path, extra, recursive, matcher); + if (!err.empty()) + return err; + } // add *.cpp files to the "files" vector.. for (const auto& file : filelist) { From e04cb02f38eea43b7b4e04e16575f7e28e34b4e6 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 20 Oct 2025 01:18:41 +0200 Subject: [PATCH 2/2] programmemory.cpp: fixed `variableScope` selfcheck warning --- lib/programmemory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index c72ae0a52e8..57956673dd5 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -1394,8 +1394,8 @@ namespace { if (conditions1.empty()) return unknown(); std::unordered_map condValues = executeAll(conditions1, &b); - bool allNegated = true; { + bool allNegated = true; ValueFlow::Value negatedValue = unknown(); for (const auto& p : condValues) { const ValueFlow::Value& v = p.second;