value flow: assume that value of global const variable is not changed

This commit is contained in:
Daniel Marjamäki 2014-01-10 05:47:56 +01:00
parent a26c5736da
commit d085705e8f
2 changed files with 10 additions and 3 deletions

View File

@ -50,7 +50,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
unsigned int varid;
MathLib::bigint num;
const Variable *var;
if (tok->isComparisonOp() && tok->astOperand1() && tok->astOperand2()) {
if (tok->isComparisonOp() && tok->astOperand2()) {
if (tok->astOperand1()->isName() && tok->astOperand2()->isNumber()) {
varid = tok->astOperand1()->varId();
var = tok->astOperand1()->variable();
@ -78,8 +78,8 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
if (varid == 0U)
continue;
// bailout: global variables
if (var && var->isGlobal()) {
// bailout: global non-const variables
if (var && var->isGlobal() && !var->isConst()) {
if (settings->debugwarnings)
bailout(tokenlist, errorLogger, tok, "global variable " + var->nameToken()->str());
continue;

View File

@ -97,6 +97,13 @@ private:
"}";
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
code = "extern const int x;\n"
"void f() {\n"
" int a = x;\n"
" if (x == 123) {}\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
// bailout: ?:
bailout("void f(int x) {\n"
" y = ((x<0) ? x : ((x==2)?3:4));\n"