Skip to content

Commit afaf147

Browse files
committed
Value flow: refactorings, write debug warning if iterating too far
1 parent 86201c1 commit afaf147

4 files changed

Lines changed: 43 additions & 18 deletions

File tree

lib/checkother.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2158,8 +2158,10 @@ void CheckOther::checkZeroDivision()
21582158
std::list<ValueFlow::Value>::const_iterator it;
21592159
for (it = values.begin(); it != values.end(); ++it) {
21602160
if (it->intvalue == 0) {
2161-
if (!it->link || _settings->isEnabled("warning"))
2161+
if (it->condition == NULL)
21622162
zerodivError(tok);
2163+
else if (_settings->isEnabled("warning"))
2164+
zerodivcondError(it->condition,tok);
21632165
}
21642166
}
21652167
}
@@ -2176,6 +2178,11 @@ void CheckOther::checkZeroDivisionOrUselessCondition()
21762178
{
21772179
if (!_settings->isEnabled("warning"))
21782180
return;
2181+
2182+
// Use experimental checking instead based on value flow analysis
2183+
if (_settings->valueFlow)
2184+
return;
2185+
21792186
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
21802187
const std::size_t numberOfFunctions = symbolDatabase->functionScopes.size();
21812188
for (std::size_t functionIndex = 0; functionIndex < numberOfFunctions; ++functionIndex) {

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ bool Tokenizer::tokenize(std::istream &code,
16051605
list.createAst();
16061606

16071607
if (_settings->valueFlow)
1608-
ValueFlow::setValues(list.front());
1608+
ValueFlow::setValues(&list, _errorLogger, _settings);
16091609

16101610
return true;
16111611
}

lib/valueflow.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,39 @@
1717
*/
1818

1919
#include "valueflow.h"
20-
#include "token.h"
20+
#include "errorlogger.h"
2121
#include "mathlib.h"
22+
#include "settings.h"
23+
#include "symboldatabase.h"
24+
#include "token.h"
25+
#include "tokenlist.h"
2226

23-
static void valueFlowBeforeCondition(Token *tokens)
27+
static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings)
2428
{
25-
for (Token *tok = tokens; tok; tok = tok->next()) {
29+
for (Token *tok = tokenlist->front(); tok; tok = tok->next()) {
2630
unsigned int varid;
2731
MathLib::bigint num;
32+
const Variable *var;
2833
if (Token::Match(tok, "==|!=|>=|<=") && tok->astOperand1() && tok->astOperand2()) {
2934
if (tok->astOperand1()->isName() && tok->astOperand2()->isNumber()) {
3035
varid = tok->astOperand1()->varId();
36+
var = tok->astOperand1()->variable();
3137
num = MathLib::toLongNumber(tok->astOperand2()->str());
3238
} else if (tok->astOperand1()->isNumber() && tok->astOperand2()->isName()) {
3339
varid = tok->astOperand2()->varId();
40+
var = tok->astOperand2()->variable();
3441
num = MathLib::toLongNumber(tok->astOperand1()->str());
3542
} else {
3643
continue;
3744
}
3845
} else if (Token::Match(tok->previous(), "if|while ( %var% %oror%|&&|)") ||
3946
Token::Match(tok, "%oror%|&& %var% %oror%|&&|)")) {
4047
varid = tok->next()->varId();
48+
var = tok->next()->variable();
4149
num = 0;
4250
} else if (tok->str() == "!" && tok->astOperand1() && tok->astOperand1()->isName()) {
4351
varid = tok->astOperand1()->varId();
52+
var = tok->astOperand1()->variable();
4453
num = 0;
4554
} else {
4655
continue;
@@ -50,26 +59,33 @@ static void valueFlowBeforeCondition(Token *tokens)
5059
continue;
5160

5261
struct ValueFlow::Value val;
53-
val.link = tok;
62+
val.condition = tok;
5463
val.intvalue = num;
5564

56-
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous()) {
57-
if (tok2->varId() == varid)
65+
for (Token *tok2 = tok->previous(); ; tok2 = tok2->previous()) {
66+
if (!tok2) {
67+
if (settings->debugwarnings) {
68+
std::list<ErrorLogger::ErrorMessage::FileLocation> callstack;
69+
callstack.push_back(ErrorLogger::ErrorMessage::FileLocation(tok,tokenlist));
70+
ErrorLogger::ErrorMessage errmsg(callstack, Severity::debug, "iterated too far", "debugValueFlowBeforeCondition", false);
71+
errorLogger->reportErr(errmsg);
72+
}
73+
break;
74+
}
75+
76+
if (tok2->varId() == varid) {
5877
tok2->values.push_back(val);
59-
if (tok2->str() == "{") {
60-
if (!Token::simpleMatch(tok2->previous(), ") {"))
61-
break;
62-
if (!Token::simpleMatch(tok2->previous()->link()->previous(), "if ("))
78+
if (var && tok2 == var->nameToken())
6379
break;
6480
}
6581
}
6682
}
6783
}
6884

69-
void ValueFlow::setValues(Token *tokens)
85+
void ValueFlow::setValues(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings)
7086
{
71-
for (Token *tok = tokens; tok; tok = tok->next())
87+
for (Token *tok = tokenlist->front(); tok; tok = tok->next())
7288
tok->values.clear();
7389

74-
valueFlowBeforeCondition(tokens);
90+
valueFlowBeforeCondition(tokenlist, errorLogger, settings);
7591
}

lib/valueflow.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222
//---------------------------------------------------------------------------
2323

2424
class Token;
25+
class TokenList;
26+
class ErrorLogger;
27+
class Settings;
2528

2629
namespace ValueFlow {
27-
2830
struct Value {
29-
const Token *link;
31+
const Token *condition;
3032
long long intvalue;
3133
};
3234

33-
void setValues(Token *tokens);
35+
void setValues(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings);
3436
}
3537

3638
#endif // valueflowH

0 commit comments

Comments
 (0)