From 7197456c23da1d4f1d6a4700908eb7b6003f309b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Mon, 5 Dec 2016 13:17:58 +0100 Subject: [PATCH 1/2] CheckCondition::checkIncorrectLogicOperator(): if we encounter a condition like 'A && (!A || B)', print the actual equivalent ('A && B') in the --verbose message. --- lib/checkcondition.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 148aeb606..0c1105115 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -664,6 +664,7 @@ void CheckCondition::checkIncorrectLogicOperator() const bool printWarning = _settings->isEnabled("warning"); if (!printWarning && !printStyle) return; + const bool printInconclusive = _settings->inconclusive; const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const std::size_t functions = symbolDatabase->functionScopes.size(); @@ -684,8 +685,8 @@ void CheckCondition::checkIncorrectLogicOperator() } - // 'A && (!A || B)' is equivalent with 'A && B' - // 'A || (!A && B)' is equivalent with 'A || B' + // 'A && (!A || B)' is equivalent to 'A && B' + // 'A || (!A && B)' is equivalent to 'A || B' if (printStyle && ((tok->str() == "||" && tok->astOperand2()->str() == "&&") || (tok->str() == "&&" && tok->astOperand2()->str() == "||"))) { @@ -694,6 +695,10 @@ void CheckCondition::checkIncorrectLogicOperator() std::string expr1(tok->astOperand1()->expressionString()); std::string expr2(tok->astOperand2()->astOperand1()->expressionString()); std::string expr3(tok->astOperand2()->astOperand2()->expressionString()); + // make copy for later because the original string might get overwritten + const std::string expr1VerboseMsg = expr1; + const std::string expr2VerboseMsg = expr2; + const std::string expr3VerboseMsg = expr3; if (expr1.length() + expr2.length() + expr3.length() > 50U) { if (expr1[0] == '!' && expr2[0] != '!') { @@ -710,7 +715,12 @@ void CheckCondition::checkIncorrectLogicOperator() const std::string cond1 = expr1 + " " + tok->str() + " (" + expr2 + " " + tok->astOperand2()->str() + " " + expr3 + ")"; const std::string cond2 = expr1 + " " + tok->str() + " " + expr3; - redundantConditionError(tok, tok2->expressionString() + ". '" + cond1 + "' is equivalent to '" + cond2 + "'", false); + const std::string cond1VerboseMsg = expr1VerboseMsg + " " + tok->str() + " " + expr2VerboseMsg + " " + tok->astOperand2()->str() + " " + expr3VerboseMsg; + const std::string cond2VerboseMsg = expr1VerboseMsg + " " + tok->str() + " " + expr3VerboseMsg; + // for the --verbose message, transform the actual condition and print it + const std::string msg = tok2->expressionString() + ". '" + cond1 + "' is equivalent to '" + cond2 + "'\n" + "The condition '" + cond1VerboseMsg + "' is equivalent to '" + cond2VerboseMsg + "'."; + redundantConditionError(tok, msg, false); continue; } } @@ -739,7 +749,7 @@ void CheckCondition::checkIncorrectLogicOperator() if (!parseComparison(comp2, ¬2, &op2, &value2, &expr2, &inconclusive)) continue; - if (inconclusive && !_settings->inconclusive) + if (inconclusive && !printInconclusive) continue; if (isSameExpression(_tokenizer->isCPP(), true, comp1, comp2, _settings->library.functionpure)) From d665641a76ef54be63ca6b4812946e906b1138ff Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 5 Dec 2016 14:48:16 +0100 Subject: [PATCH 2/2] Refactorizations: - use std::string::pop_back() and std::string::back() - pass argument as const std::string& - Moved iterator into for loop head --- lib/importproject.cpp | 12 ++++++------ lib/library.cpp | 2 +- lib/mathlib.cpp | 2 +- lib/settings.cpp | 3 +-- test/testtype.cpp | 10 +++++----- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 76358a5e7..4a2442dc8 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -71,8 +71,8 @@ void ImportProject::FileSettings::setDefines(std::string defs) } while (defs.find(";;") != std::string::npos) defs.erase(defs.find(";;"),1); - if (!defs.empty() && defs[defs.size()-1U] == ';') - defs.erase(defs.size()-1U); + if (!defs.empty() && defs.back() == ';') + defs.pop_back(); bool eq = false; for (std::size_t pos = 0; pos < defs.size(); ++pos) { if (defs[pos] == '(' || defs[pos] == '=') @@ -124,14 +124,14 @@ void ImportProject::FileSettings::setIncludePaths(const std::string &basepath, c continue; std::string s(Path::fromNativeSeparators(*it)); if (s[0] == '/' || (s.size() > 1U && s.compare(1,2,":/") == 0)) { - if (s[s.size()-1U] != '/') + if (s.back() != '/') s += '/'; I.push_back(s); continue; } - if (s[s.size()-1U] == '/') // this is a temporary hack, simplifyPath can crash if path ends with '/' - s.erase(s.size() - 1U); + if (s.back() == '/') // this is a temporary hack, simplifyPath can crash if path ends with '/' + s.pop_back(); if (s.find("$(")==std::string::npos) { s = Path::simplifyPath(basepath + s); @@ -155,7 +155,7 @@ void ImportProject::import(const std::string &filename) importCompileCommands(fin); } else if (filename.find(".sln") != std::string::npos) { std::string path(Path::getPathFromFilename(Path::fromNativeSeparators(filename))); - if (!path.empty() && path[path.size()-1U] != '/') + if (!path.empty() && path.back() != '/') path += '/'; importSln(fin,path); } else if (filename.find(".vcxproj") != std::string::npos) { diff --git a/lib/library.cpp b/lib/library.cpp index 1472f2494..eaf454a1f 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -87,7 +87,7 @@ Library::Error Library::load(const char exename[], const char path[]) while (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND && !cfgfolders.empty()) { const std::string cfgfolder(cfgfolders.front()); cfgfolders.pop_front(); - const char *sep = (!cfgfolder.empty() && cfgfolder[cfgfolder.size()-1U]=='/' ? "" : "/"); + const char *sep = (!cfgfolder.empty() && cfgfolder.back()=='/' ? "" : "/"); const std::string filename(cfgfolder + sep + fullfilename); error = doc.LoadFile(filename.c_str()); if (error != tinyxml2::XML_ERROR_FILE_NOT_FOUND) diff --git a/lib/mathlib.cpp b/lib/mathlib.cpp index 332c02922..4221fa6c0 100644 --- a/lib/mathlib.cpp +++ b/lib/mathlib.cpp @@ -524,7 +524,7 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str) return static_cast(doubleval); } - if (str[0] == '\'' && str.size() >= 3U && str[str.size()-1U] == '\'') { + if (str[0] == '\'' && str.size() >= 3U && str.back() == '\'') { return characterLiteralToLongNumber(str.substr(1,str.size()-2)); } diff --git a/lib/settings.cpp b/lib/settings.cpp index 41ecc3dc9..0199ab6d2 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -88,8 +88,7 @@ std::string Settings::addEnabled(const std::string &str) } if (str == "all") { - std::set::const_iterator it; - for (it = id.begin(); it != id.end(); ++it) { + for (std::set::const_iterator it = id.cbegin(); it != id.cend(); ++it) { if (*it == "internal") continue; diff --git a/test/testtype.cpp b/test/testtype.cpp index 1e6889314..d4e20db28 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -215,12 +215,12 @@ private: // This function ensure that test works with different compilers. Floats can // be stringified differently. - std::string removeFloat(std::string errmsg) { - std::string::size_type pos1 = errmsg.find("float ("); - std::string::size_type pos2 = errmsg.find(") conversion"); + std::string removeFloat(const std::string& msg) { + std::string::size_type pos1 = msg.find("float ("); + std::string::size_type pos2 = msg.find(") conversion"); if (pos1 == std::string::npos || pos2 == std::string::npos || pos1 > pos2) - return errmsg; - return errmsg.substr(0,pos1+7) + errmsg.substr(pos2); + return msg; + return msg.substr(0,pos1+7) + msg.substr(pos2); } void checkFloatToIntegerOverflow() {