value flow: assume that value of global const variable is not changed
This commit is contained in:
parent
a26c5736da
commit
d085705e8f
|
@ -50,7 +50,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
|
||||||
unsigned int varid;
|
unsigned int varid;
|
||||||
MathLib::bigint num;
|
MathLib::bigint num;
|
||||||
const Variable *var;
|
const Variable *var;
|
||||||
if (tok->isComparisonOp() && tok->astOperand1() && tok->astOperand2()) {
|
if (tok->isComparisonOp() && tok->astOperand2()) {
|
||||||
if (tok->astOperand1()->isName() && tok->astOperand2()->isNumber()) {
|
if (tok->astOperand1()->isName() && tok->astOperand2()->isNumber()) {
|
||||||
varid = tok->astOperand1()->varId();
|
varid = tok->astOperand1()->varId();
|
||||||
var = tok->astOperand1()->variable();
|
var = tok->astOperand1()->variable();
|
||||||
|
@ -78,8 +78,8 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog
|
||||||
if (varid == 0U)
|
if (varid == 0U)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// bailout: global variables
|
// bailout: global non-const variables
|
||||||
if (var && var->isGlobal()) {
|
if (var && var->isGlobal() && !var->isConst()) {
|
||||||
if (settings->debugwarnings)
|
if (settings->debugwarnings)
|
||||||
bailout(tokenlist, errorLogger, tok, "global variable " + var->nameToken()->str());
|
bailout(tokenlist, errorLogger, tok, "global variable " + var->nameToken()->str());
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -97,6 +97,13 @@ private:
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(true, testValueOfX(code, 2U, 0));
|
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: ?:
|
||||||
bailout("void f(int x) {\n"
|
bailout("void f(int x) {\n"
|
||||||
" y = ((x<0) ? x : ((x==2)?3:4));\n"
|
" y = ((x<0) ? x : ((x==2)?3:4));\n"
|
||||||
|
|
Loading…
Reference in New Issue