This commit is contained in:
orbitcowboy 2016-12-05 16:47:05 +01:00
commit 76b31302eb
6 changed files with 28 additions and 19 deletions

View File

@ -664,6 +664,7 @@ void CheckCondition::checkIncorrectLogicOperator()
const bool printWarning = _settings->isEnabled("warning"); const bool printWarning = _settings->isEnabled("warning");
if (!printWarning && !printStyle) if (!printWarning && !printStyle)
return; return;
const bool printInconclusive = _settings->inconclusive;
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size(); 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 to 'A && B'
// 'A || (!A && B)' is equivalent with 'A || B' // 'A || (!A && B)' is equivalent to 'A || B'
if (printStyle && if (printStyle &&
((tok->str() == "||" && tok->astOperand2()->str() == "&&") || ((tok->str() == "||" && tok->astOperand2()->str() == "&&") ||
(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 expr1(tok->astOperand1()->expressionString());
std::string expr2(tok->astOperand2()->astOperand1()->expressionString()); std::string expr2(tok->astOperand2()->astOperand1()->expressionString());
std::string expr3(tok->astOperand2()->astOperand2()->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.length() + expr2.length() + expr3.length() > 50U) {
if (expr1[0] == '!' && expr2[0] != '!') { 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 cond1 = expr1 + " " + tok->str() + " (" + expr2 + " " + tok->astOperand2()->str() + " " + expr3 + ")";
const std::string cond2 = expr1 + " " + tok->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; continue;
} }
} }
@ -739,7 +749,7 @@ void CheckCondition::checkIncorrectLogicOperator()
if (!parseComparison(comp2, &not2, &op2, &value2, &expr2, &inconclusive)) if (!parseComparison(comp2, &not2, &op2, &value2, &expr2, &inconclusive))
continue; continue;
if (inconclusive && !_settings->inconclusive) if (inconclusive && !printInconclusive)
continue; continue;
if (isSameExpression(_tokenizer->isCPP(), true, comp1, comp2, _settings->library.functionpure)) if (isSameExpression(_tokenizer->isCPP(), true, comp1, comp2, _settings->library.functionpure))

View File

@ -71,8 +71,8 @@ void ImportProject::FileSettings::setDefines(std::string defs)
} }
while (defs.find(";;") != std::string::npos) while (defs.find(";;") != std::string::npos)
defs.erase(defs.find(";;"),1); defs.erase(defs.find(";;"),1);
if (!defs.empty() && defs[defs.size()-1U] == ';') if (!defs.empty() && defs.back() == ';')
defs.erase(defs.size()-1U); defs.pop_back();
bool eq = false; bool eq = false;
for (std::size_t pos = 0; pos < defs.size(); ++pos) { for (std::size_t pos = 0; pos < defs.size(); ++pos) {
if (defs[pos] == '(' || defs[pos] == '=') if (defs[pos] == '(' || defs[pos] == '=')
@ -124,14 +124,14 @@ void ImportProject::FileSettings::setIncludePaths(const std::string &basepath, c
continue; continue;
std::string s(Path::fromNativeSeparators(*it)); std::string s(Path::fromNativeSeparators(*it));
if (s[0] == '/' || (s.size() > 1U && s.compare(1,2,":/") == 0)) { if (s[0] == '/' || (s.size() > 1U && s.compare(1,2,":/") == 0)) {
if (s[s.size()-1U] != '/') if (s.back() != '/')
s += '/'; s += '/';
I.push_back(s); I.push_back(s);
continue; continue;
} }
if (s[s.size()-1U] == '/') // this is a temporary hack, simplifyPath can crash if path ends with '/' if (s.back() == '/') // this is a temporary hack, simplifyPath can crash if path ends with '/'
s.erase(s.size() - 1U); s.pop_back();
if (s.find("$(")==std::string::npos) { if (s.find("$(")==std::string::npos) {
s = Path::simplifyPath(basepath + s); s = Path::simplifyPath(basepath + s);
@ -155,7 +155,7 @@ void ImportProject::import(const std::string &filename)
importCompileCommands(fin); importCompileCommands(fin);
} else if (filename.find(".sln") != std::string::npos) { } else if (filename.find(".sln") != std::string::npos) {
std::string path(Path::getPathFromFilename(Path::fromNativeSeparators(filename))); std::string path(Path::getPathFromFilename(Path::fromNativeSeparators(filename)));
if (!path.empty() && path[path.size()-1U] != '/') if (!path.empty() && path.back() != '/')
path += '/'; path += '/';
importSln(fin,path); importSln(fin,path);
} else if (filename.find(".vcxproj") != std::string::npos) { } else if (filename.find(".vcxproj") != std::string::npos) {

View File

@ -87,7 +87,7 @@ Library::Error Library::load(const char exename[], const char path[])
while (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND && !cfgfolders.empty()) { while (error == tinyxml2::XML_ERROR_FILE_NOT_FOUND && !cfgfolders.empty()) {
const std::string cfgfolder(cfgfolders.front()); const std::string cfgfolder(cfgfolders.front());
cfgfolders.pop_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); const std::string filename(cfgfolder + sep + fullfilename);
error = doc.LoadFile(filename.c_str()); error = doc.LoadFile(filename.c_str());
if (error != tinyxml2::XML_ERROR_FILE_NOT_FOUND) if (error != tinyxml2::XML_ERROR_FILE_NOT_FOUND)

View File

@ -524,7 +524,7 @@ MathLib::bigint MathLib::toLongNumber(const std::string & str)
return static_cast<bigint>(doubleval); return static_cast<bigint>(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)); return characterLiteralToLongNumber(str.substr(1,str.size()-2));
} }

View File

@ -88,8 +88,7 @@ std::string Settings::addEnabled(const std::string &str)
} }
if (str == "all") { if (str == "all") {
std::set<std::string>::const_iterator it; for (std::set<std::string>::const_iterator it = id.cbegin(); it != id.cend(); ++it) {
for (it = id.begin(); it != id.end(); ++it) {
if (*it == "internal") if (*it == "internal")
continue; continue;

View File

@ -215,12 +215,12 @@ private:
// This function ensure that test works with different compilers. Floats can // This function ensure that test works with different compilers. Floats can
// be stringified differently. // be stringified differently.
std::string removeFloat(std::string errmsg) { std::string removeFloat(const std::string& msg) {
std::string::size_type pos1 = errmsg.find("float ("); std::string::size_type pos1 = msg.find("float (");
std::string::size_type pos2 = errmsg.find(") conversion"); std::string::size_type pos2 = msg.find(") conversion");
if (pos1 == std::string::npos || pos2 == std::string::npos || pos1 > pos2) if (pos1 == std::string::npos || pos2 == std::string::npos || pos1 > pos2)
return errmsg; return msg;
return errmsg.substr(0,pos1+7) + errmsg.substr(pos2); return msg.substr(0,pos1+7) + msg.substr(pos2);
} }
void checkFloatToIntegerOverflow() { void checkFloatToIntegerOverflow() {