2014-01-04 20:57:02 +01:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2015-11-18 20:04:50 +01:00
* Copyright ( C ) 2007 - 2015 Cppcheck team .
2014-01-04 20:57:02 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "valueflow.h"
2015-11-11 13:45:28 +01:00
# include "astutils.h"
2014-01-05 20:06:46 +01:00
# include "errorlogger.h"
2014-01-04 20:57:02 +01:00
# include "mathlib.h"
2014-01-05 20:06:46 +01:00
# include "settings.h"
# include "symboldatabase.h"
# include "token.h"
# include "tokenlist.h"
2014-01-19 09:05:48 +01:00
# include <stack>
2014-01-06 16:37:52 +01:00
2015-09-12 19:49:02 +02:00
namespace {
struct ProgramMemory {
std : : map < unsigned int , MathLib : : bigint > intvalues ;
std : : map < unsigned int , const Token * > tokvalues ;
void setValue ( unsigned int varid , const ValueFlow : : Value & value ) {
if ( value . tokvalue ) {
intvalues . erase ( varid ) ;
tokvalues [ varid ] = value . tokvalue ;
} else {
intvalues [ varid ] = value . intvalue ;
tokvalues . erase ( varid ) ;
}
}
bool getIntValue ( unsigned int varid , MathLib : : bigint * result ) const {
const std : : map < unsigned int , MathLib : : bigint > : : const_iterator it = intvalues . find ( varid ) ;
if ( it ! = intvalues . end ( ) )
* result = it - > second ;
return ( it ! = intvalues . end ( ) ) ;
}
void setIntValue ( unsigned int varid , MathLib : : bigint value ) {
intvalues [ varid ] = value ;
tokvalues . erase ( varid ) ;
}
bool hasValue ( unsigned int varid ) {
return intvalues . find ( varid ) ! = intvalues . end ( ) | |
tokvalues . find ( varid ) ! = tokvalues . end ( ) ;
}
void swap ( ProgramMemory & pm ) {
intvalues . swap ( pm . intvalues ) ;
tokvalues . swap ( pm . tokvalues ) ;
}
void clear ( ) {
intvalues . clear ( ) ;
tokvalues . clear ( ) ;
}
bool empty ( ) const {
return intvalues . empty ( ) & & tokvalues . empty ( ) ;
}
} ;
}
2014-05-01 15:15:26 +02:00
static void execute ( const Token * expr ,
2015-09-12 19:49:02 +02:00
ProgramMemory * const programMemory ,
2014-05-01 15:15:26 +02:00
MathLib : : bigint * result ,
bool * error ) ;
2014-01-06 07:44:58 +01:00
static void bailout ( TokenList * tokenlist , ErrorLogger * errorLogger , const Token * tok , const std : : string & what )
{
std : : list < ErrorLogger : : ErrorMessage : : FileLocation > callstack ;
callstack . push_back ( ErrorLogger : : ErrorMessage : : FileLocation ( tok , tokenlist ) ) ;
ErrorLogger : : ErrorMessage errmsg ( callstack , Severity : : debug , " ValueFlow bailout: " + what , " valueFlowBailout " , false ) ;
errorLogger - > reportErr ( errmsg ) ;
}
2014-01-12 19:48:20 +01:00
static bool bailoutFunctionPar ( const Token * tok , const ValueFlow : : Value & value , const Settings * settings , bool * inconclusive )
2014-01-11 14:31:51 +01:00
{
2014-01-24 17:47:49 +01:00
if ( ! tok )
return false ;
// address of variable
2014-03-14 16:26:37 +01:00
const bool addressOf = tok & & Token : : simpleMatch ( tok - > previous ( ) , " & " ) ;
2014-01-24 17:47:49 +01:00
// passing variable to subfunction?
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok - > tokAt ( - 2 ) , " ) & %name% [,)] " ) & & Token : : Match ( tok - > linkAt ( - 2 ) - > previous ( ) , " [,(] ( " ) )
2014-01-24 17:47:49 +01:00
;
2015-01-31 10:50:39 +01:00
else if ( Token : : Match ( tok - > tokAt ( addressOf ? - 2 : - 1 ) , " [(,] &| %name% [,)] " ) )
2014-01-24 17:47:49 +01:00
;
else
2014-01-11 14:31:51 +01:00
return false ;
2014-04-01 07:06:20 +02:00
// reinterpret_cast etc..
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok - > tokAt ( - 3 ) , " > ( & %name% ) [,)] " ) & &
2014-04-01 07:06:20 +02:00
tok - > linkAt ( - 3 ) & &
Token : : Match ( tok - > linkAt ( - 3 ) - > tokAt ( - 2 ) , " [,(] %type% < " ) )
tok = tok - > linkAt ( - 3 ) ;
2014-01-11 14:31:51 +01:00
// goto start of function call and get argnr
unsigned int argnr = 0 ;
while ( tok & & tok - > str ( ) ! = " ( " ) {
if ( tok - > str ( ) = = " , " )
+ + argnr ;
else if ( tok - > str ( ) = = " ) " )
tok = tok - > link ( ) ;
tok = tok - > previous ( ) ;
}
2014-02-16 10:32:10 +01:00
tok = tok ? tok - > previous ( ) : nullptr ;
2015-01-31 10:50:39 +01:00
if ( ! Token : : Match ( tok , " %name% ( " ) )
2014-02-27 16:40:42 +01:00
return false ; // not a function => do not bailout
2014-01-11 14:31:51 +01:00
2014-01-12 18:19:00 +01:00
if ( ! tok - > function ( ) ) {
2014-02-27 16:40:42 +01:00
// if value is 0 and the library says 0 is invalid => do not bailout
2015-01-08 19:31:41 +01:00
if ( value . intvalue = = 0 & & settings - > library . isnullargbad ( tok , 1 + argnr ) )
2014-01-12 19:48:20 +01:00
return false ;
2014-03-18 17:04:33 +01:00
// addressOf => inconclusive
if ( ! addressOf ) {
2014-01-12 19:48:20 +01:00
* inconclusive = true ;
return false ;
}
return true ;
2014-01-12 18:19:00 +01:00
}
2014-01-11 14:31:51 +01:00
const Variable * arg = tok - > function ( ) - > getArgumentVar ( argnr ) ;
2014-01-13 05:52:28 +01:00
if ( addressOf & & ! ( arg & & arg - > isConst ( ) ) )
return true ;
2014-01-11 14:31:51 +01:00
return arg & & ! arg - > isConst ( ) & & arg - > isReference ( ) ;
}
2014-05-01 15:15:26 +02:00
/**
* Is condition always false when variable has given value ?
* \ param condition top ast token in condition
2014-09-03 20:38:41 +02:00
* \ param programMemory program memory
2014-05-01 15:15:26 +02:00
*/
2015-09-12 19:49:02 +02:00
static bool conditionIsFalse ( const Token * condition , const ProgramMemory & programMemory )
2014-05-01 15:15:26 +02:00
{
if ( ! condition )
return false ;
if ( condition - > str ( ) = = " && " ) {
2014-09-03 20:38:41 +02:00
const bool result1 = conditionIsFalse ( condition - > astOperand1 ( ) , programMemory ) ;
const bool result2 = result1 ? true : conditionIsFalse ( condition - > astOperand2 ( ) , programMemory ) ;
2014-05-01 15:15:26 +02:00
return result2 ;
}
2015-09-12 19:49:02 +02:00
ProgramMemory progmem ( programMemory ) ;
2014-05-01 15:15:26 +02:00
MathLib : : bigint result = 0 ;
bool error = false ;
2014-08-01 07:35:15 +02:00
execute ( condition , & progmem , & result , & error ) ;
2014-05-01 15:15:26 +02:00
return ! error & & result = = 0 ;
}
2014-06-25 16:00:56 +02:00
/**
* Is condition always true when variable has given value ?
* \ param condition top ast token in condition
2014-09-03 20:38:41 +02:00
* \ param programMemory program memory
2014-06-25 16:00:56 +02:00
*/
2015-09-12 19:49:02 +02:00
static bool conditionIsTrue ( const Token * condition , const ProgramMemory & programMemory )
2014-06-25 16:00:56 +02:00
{
if ( ! condition )
return false ;
if ( condition - > str ( ) = = " || " ) {
2014-09-04 21:27:33 +02:00
const bool result1 = conditionIsTrue ( condition - > astOperand1 ( ) , programMemory ) ;
const bool result2 = result1 ? true : conditionIsTrue ( condition - > astOperand2 ( ) , programMemory ) ;
2014-06-25 16:00:56 +02:00
return result2 ;
}
2015-09-12 19:49:02 +02:00
ProgramMemory progmem ( programMemory ) ;
2014-06-25 16:00:56 +02:00
bool error = false ;
2014-09-06 22:27:13 +02:00
MathLib : : bigint result = 0 ;
2014-08-01 07:35:15 +02:00
execute ( condition , & progmem , & result , & error ) ;
2014-06-25 16:00:56 +02:00
return ! error & & result = = 1 ;
}
2014-08-01 07:35:15 +02:00
/**
* Get program memory by looking backwards from given token .
*/
2015-09-12 19:49:02 +02:00
static ProgramMemory getProgramMemory ( const Token * tok , unsigned int varid , const ValueFlow : : Value & value )
2014-08-01 07:35:15 +02:00
{
2015-09-12 19:49:02 +02:00
ProgramMemory programMemory ;
programMemory . setValue ( varid , value ) ;
2015-02-08 19:20:05 +01:00
if ( value . varId )
2015-09-12 19:49:02 +02:00
programMemory . setIntValue ( value . varId , value . varvalue ) ;
const ProgramMemory programMemory1 ( programMemory ) ;
2014-08-01 07:35:15 +02:00
int indentlevel = 0 ;
for ( const Token * tok2 = tok ; tok2 ; tok2 = tok2 - > previous ( ) ) {
2014-08-27 16:59:18 +02:00
if ( Token : : Match ( tok2 , " [;{}] %varid% = %var% ; " , varid ) ) {
const Token * vartok = tok2 - > tokAt ( 3 ) ;
2015-09-12 19:49:02 +02:00
programMemory . setValue ( vartok - > varId ( ) , value ) ;
2014-08-27 16:59:18 +02:00
}
2015-05-24 17:02:00 +02:00
if ( Token : : Match ( tok2 , " [;{}] %var% = " ) ) {
const Token * vartok = tok2 - > next ( ) ;
2015-09-12 19:49:02 +02:00
if ( ! programMemory . hasValue ( vartok - > varId ( ) ) ) {
2015-05-24 17:02:00 +02:00
MathLib : : bigint result = 0 ;
bool error = false ;
execute ( tok2 - > tokAt ( 2 ) - > astOperand2 ( ) , & programMemory , & result , & error ) ;
if ( ! error )
2015-09-12 19:49:02 +02:00
programMemory . setIntValue ( vartok - > varId ( ) , result ) ;
2015-05-24 17:02:00 +02:00
}
}
2014-08-01 07:35:15 +02:00
if ( tok2 - > str ( ) = = " { " ) {
if ( indentlevel < = 0 )
break ;
- - indentlevel ;
}
if ( tok2 - > str ( ) = = " } " ) {
const Token * cond = tok2 - > link ( ) ;
cond = Token : : simpleMatch ( cond - > previous ( ) , " ) { " ) ? cond - > linkAt ( - 1 ) : nullptr ;
if ( cond & & conditionIsFalse ( cond - > astOperand2 ( ) , programMemory1 ) )
tok2 = cond - > previous ( ) ;
else if ( cond & & conditionIsTrue ( cond - > astOperand2 ( ) , programMemory1 ) ) {
+ + indentlevel ;
continue ;
} else
break ;
}
}
return programMemory ;
}
2014-01-19 09:05:48 +01:00
/**
* Should value be skipped because it ' s hidden inside & & | | or ? : expression .
* Example : ( ( x ! = NULL ) & & ( * x = = 123 ) )
* If ' valuetok ' points at the x in ' ( * x = = 123 ) ' . Then the ' & & ' will be returned .
* @ param valuetok original variable token
* @ return NULL = > don ' t skip , non - NULL = > The operator token that cause the skip . For instance the ' & & ' .
* */
static const Token * skipValueInConditionalExpression ( const Token * const valuetok )
2014-01-11 14:54:10 +01:00
{
2014-01-19 09:05:48 +01:00
// Walk up the ast
2014-01-19 09:31:40 +01:00
const Token * prev = valuetok ;
2014-01-19 09:05:48 +01:00
for ( const Token * tok = valuetok - > astParent ( ) ; tok ; tok = tok - > astParent ( ) ) {
2014-01-19 09:31:40 +01:00
const bool prevIsLhs = ( prev = = tok - > astOperand1 ( ) ) ;
prev = tok ;
if ( prevIsLhs | | ! Token : : Match ( tok , " %oror%|&&|?|: " ) )
2014-01-19 09:05:48 +01:00
continue ;
// Is variable protected in LHS..
std : : stack < const Token * > tokens ;
tokens . push ( tok - > astOperand1 ( ) ) ;
while ( ! tokens . empty ( ) ) {
const Token * const tok2 = tokens . top ( ) ;
tokens . pop ( ) ;
2015-09-27 13:29:28 +02:00
if ( ! tok2 | | tok2 - > str ( ) = = " . " )
2014-01-19 09:05:48 +01:00
continue ;
2015-09-27 13:29:28 +02:00
// A variable is seen..
if ( tok2 ! = valuetok & & tok2 - > variable ( ) & & ( tok2 - > varId ( ) = = valuetok - > varId ( ) | | ! tok2 - > variable ( ) - > isArgument ( ) ) ) {
// TODO: limit this bailout
2014-01-19 09:05:48 +01:00
return tok ;
2015-09-27 13:29:28 +02:00
}
2014-01-19 09:05:48 +01:00
tokens . push ( tok2 - > astOperand2 ( ) ) ;
tokens . push ( tok2 - > astOperand1 ( ) ) ;
}
2015-09-27 13:29:28 +02:00
2014-01-11 14:54:10 +01:00
}
2014-02-16 10:32:10 +01:00
return nullptr ;
2014-01-11 14:54:10 +01:00
}
2014-01-11 15:36:09 +01:00
static bool bailoutSelfAssignment ( const Token * const tok )
{
const Token * parent = tok ;
while ( parent ) {
const Token * op = parent ;
parent = parent - > astParent ( ) ;
// Assignment where lhs variable exists in rhs => return true
2014-02-16 10:32:10 +01:00
if ( parent ! = nullptr & &
2014-01-11 20:53:23 +01:00
parent - > astOperand2 ( ) = = op & &
2014-02-16 10:32:10 +01:00
parent - > astOperand1 ( ) ! = nullptr & &
2014-01-11 20:53:23 +01:00
parent - > str ( ) = = " = " ) {
for ( const Token * lhs = parent - > astOperand1 ( ) ; lhs ; lhs = lhs - > astOperand1 ( ) ) {
if ( lhs - > varId ( ) = = tok - > varId ( ) )
return true ;
if ( lhs - > astOperand2 ( ) & & lhs - > astOperand2 ( ) - > varId ( ) = = tok - > varId ( ) )
return true ;
}
}
2014-01-11 15:36:09 +01:00
}
return false ;
}
2014-08-26 18:48:11 +02:00
static bool isReturn ( const Token * tok )
{
2015-02-09 09:46:27 +01:00
if ( ! tok )
return false ;
const Token * prev = tok - > previous ( ) ;
if ( prev & & Token : : simpleMatch ( prev - > previous ( ) , " } ; " ) )
2015-02-07 10:45:30 +01:00
prev = prev - > previous ( ) ;
if ( Token : : simpleMatch ( prev , " } " ) ) {
if ( Token : : simpleMatch ( prev - > link ( ) - > tokAt ( - 2 ) , " } else { " ) )
return isReturn ( prev ) & & isReturn ( prev - > link ( ) - > tokAt ( - 2 ) ) ;
if ( Token : : simpleMatch ( prev - > link ( ) - > previous ( ) , " ) { " ) & &
Token : : simpleMatch ( prev - > link ( ) - > linkAt ( - 1 ) - > previous ( ) , " switch ( " ) & &
! Token : : findsimplematch ( prev - > link ( ) , " break " , prev ) ) {
return true ;
}
2015-02-09 09:46:27 +01:00
} else if ( Token : : simpleMatch ( prev , " ; " ) ) {
2014-08-26 18:48:11 +02:00
// noreturn function
2015-01-31 10:50:39 +01:00
if ( Token : : simpleMatch ( prev - > previous ( ) , " ) ; " ) & & Token : : Match ( prev - > linkAt ( - 1 ) - > tokAt ( - 2 ) , " [;{}] %name% ( " ) )
2014-08-26 18:48:11 +02:00
return true ;
2015-01-01 14:29:49 +01:00
// return/goto statement
2014-08-26 18:48:11 +02:00
prev = prev - > previous ( ) ;
2015-08-11 11:21:03 +02:00
while ( prev & & ! Token : : Match ( prev , " ;|{|}|return|goto|throw|continue|break " ) )
2014-08-26 18:48:11 +02:00
prev = prev - > previous ( ) ;
2015-02-07 10:45:30 +01:00
return prev & & prev - > isName ( ) ;
2014-08-26 18:48:11 +02:00
}
return false ;
}
2015-07-17 09:46:31 +02:00
/** Add token value. Return true if value is added. */
static bool addValue ( Token * tok , const ValueFlow : : Value & value )
2014-01-18 19:30:44 +01:00
{
2015-10-04 10:33:43 +02:00
if ( value . isKnown ( ) ) {
// Clear all other values since value is known
tok - > values . clear ( ) ;
}
2015-10-19 19:36:46 +02:00
// Don't handle more than 10 values for performance reasons
// TODO: add setting?
if ( tok - > values . size ( ) > = 10U )
return false ;
2014-01-18 19:30:44 +01:00
// if value already exists, don't add it again
std : : list < ValueFlow : : Value > : : iterator it ;
for ( it = tok - > values . begin ( ) ; it ! = tok - > values . end ( ) ; + + it ) {
2014-08-03 20:11:22 +02:00
// different intvalue => continue
if ( it - > intvalue ! = value . intvalue )
continue ;
// different tokvalue => continue
if ( ( it - > tokvalue = = nullptr ) ! = ( value . tokvalue = = nullptr ) )
continue ;
if ( ( value . tokvalue ! = nullptr ) & & ( it - > tokvalue ! = value . tokvalue ) & & ( it - > tokvalue - > str ( ) ! = value . tokvalue - > str ( ) ) )
continue ;
// same value, but old value is inconclusive so replace it
if ( it - > inconclusive & & ! value . inconclusive ) {
* it = value ;
2015-07-17 09:46:31 +02:00
if ( it - > varId = = 0 )
it - > varId = tok - > varId ( ) ;
2014-08-03 20:11:22 +02:00
break ;
2014-01-18 19:30:44 +01:00
}
2014-08-03 20:11:22 +02:00
// Same value already exists, don't add new value
2015-07-17 09:46:31 +02:00
return false ;
2014-01-18 19:30:44 +01:00
}
2015-07-17 09:46:31 +02:00
// Add value
2014-01-18 19:30:44 +01:00
if ( it = = tok - > values . end ( ) ) {
2015-07-17 09:46:31 +02:00
ValueFlow : : Value v ( value ) ;
if ( v . varId = = 0 )
v . varId = tok - > varId ( ) ;
tok - > values . push_back ( v ) ;
2014-01-18 19:30:44 +01:00
}
2015-07-17 09:46:31 +02:00
return true ;
}
/** set ValueFlow value and perform calculations if possible */
static void setTokenValue ( Token * tok , const ValueFlow : : Value & value )
{
if ( ! addValue ( tok , value ) )
return ;
2014-01-18 19:30:44 +01:00
Token * parent = const_cast < Token * > ( tok - > astParent ( ) ) ;
2015-02-24 15:57:39 +01:00
if ( ! parent )
return ;
2014-01-20 22:26:55 +01:00
// Cast..
2015-02-24 15:57:39 +01:00
if ( parent - > str ( ) = = " ( " & & tok = = parent - > link ( ) - > next ( ) ) {
2014-01-20 22:26:55 +01:00
setTokenValue ( parent , value ) ;
}
2015-07-02 20:11:27 +02:00
else if ( parent - > str ( ) = = " : " ) {
setTokenValue ( parent , value ) ;
}
2015-07-04 11:17:38 +02:00
else if ( parent - > str ( ) = = " ? " & & tok - > str ( ) = = " : " & & tok = = parent - > astOperand2 ( ) ) {
2015-07-02 20:11:27 +02:00
// is condition only depending on 1 variable?
std : : stack < const Token * > tokens ;
tokens . push ( parent - > astOperand1 ( ) ) ;
2015-09-10 16:59:20 +02:00
unsigned int varId = 0 ;
2015-07-02 20:11:27 +02:00
while ( ! tokens . empty ( ) ) {
const Token * t = tokens . top ( ) ;
tokens . pop ( ) ;
if ( ! t )
continue ;
tokens . push ( t - > astOperand1 ( ) ) ;
tokens . push ( t - > astOperand2 ( ) ) ;
if ( t - > varId ( ) ) {
2015-09-10 16:59:20 +02:00
if ( varId > 0 | | value . varId ! = 0U )
2015-07-02 20:11:27 +02:00
return ;
2015-09-10 16:59:20 +02:00
varId = t - > varId ( ) ;
2015-07-02 20:11:27 +02:00
} else if ( t - > str ( ) = = " ( " & & Token : : Match ( t - > previous ( ) , " %name% " ) )
return ; // function call
}
ValueFlow : : Value v ( value ) ;
2015-07-12 19:35:47 +02:00
v . conditional = true ;
2015-07-25 19:36:29 +02:00
v . changeKnownToPossible ( ) ;
2015-07-17 15:22:24 +02:00
2015-09-10 16:59:20 +02:00
if ( varId )
v . varId = varId ;
2015-07-02 20:11:27 +02:00
setTokenValue ( parent , v ) ;
}
2014-01-20 22:26:55 +01:00
// Calculations..
2015-10-14 10:44:04 +02:00
else if ( ( parent - > isArithmeticalOp ( ) | | parent - > isComparisonOp ( ) | | ( parent - > tokType ( ) = = Token : : eBitOp ) ) & &
2015-07-26 11:27:52 +02:00
parent - > astOperand1 ( ) & &
parent - > astOperand2 ( ) ) {
2015-07-18 15:03:57 +02:00
const bool known = ( ( parent - > astOperand1 ( ) - > values . size ( ) = = 1U & &
2015-07-25 19:36:29 +02:00
parent - > astOperand1 ( ) - > values . front ( ) . isKnown ( ) ) | |
2015-07-18 15:03:57 +02:00
( parent - > astOperand2 ( ) - > values . size ( ) = = 1U & &
2015-07-25 19:36:29 +02:00
parent - > astOperand2 ( ) - > values . front ( ) . isKnown ( ) ) ) ;
2014-01-18 19:30:44 +01:00
std : : list < ValueFlow : : Value > : : const_iterator value1 , value2 ;
for ( value1 = parent - > astOperand1 ( ) - > values . begin ( ) ; value1 ! = parent - > astOperand1 ( ) - > values . end ( ) ; + + value1 ) {
2015-08-14 20:46:13 +02:00
if ( value1 - > tokvalue & & ( ! parent - > isComparisonOp ( ) | | value1 - > tokvalue - > tokType ( ) ! = Token : : eString ) )
2015-08-02 18:12:03 +02:00
continue ;
2014-01-18 19:30:44 +01:00
for ( value2 = parent - > astOperand2 ( ) - > values . begin ( ) ; value2 ! = parent - > astOperand2 ( ) - > values . end ( ) ; + + value2 ) {
2015-08-14 20:46:13 +02:00
if ( value2 - > tokvalue & & ( ! parent - > isComparisonOp ( ) | | value2 - > tokvalue - > tokType ( ) ! = Token : : eString | | value1 - > tokvalue ) )
2015-08-02 18:12:03 +02:00
continue ;
2015-07-18 15:03:57 +02:00
if ( known | | value1 - > varId = = 0U | | value2 - > varId = = 0U | |
2015-08-02 18:12:03 +02:00
( value1 - > varId = = value2 - > varId & & value1 - > varvalue = = value2 - > varvalue & & ! value1 - > tokvalue & & ! value2 - > tokvalue ) ) {
2014-01-18 19:30:44 +01:00
ValueFlow : : Value result ( 0 ) ;
result . condition = value1 - > condition ? value1 - > condition : value2 - > condition ;
result . inconclusive = value1 - > inconclusive | value2 - > inconclusive ;
result . varId = ( value1 - > varId ! = 0U ) ? value1 - > varId : value2 - > varId ;
2014-01-20 21:45:30 +01:00
result . varvalue = ( result . varId = = value1 - > varId ) ? value1 - > intvalue : value2 - > intvalue ;
2015-07-16 17:33:16 +02:00
if ( value1 - > valueKind = = value2 - > valueKind )
result . valueKind = value1 - > valueKind ;
2014-01-18 19:30:44 +01:00
switch ( parent - > str ( ) [ 0 ] ) {
case ' + ' :
result . intvalue = value1 - > intvalue + value2 - > intvalue ;
setTokenValue ( parent , result ) ;
break ;
case ' - ' :
result . intvalue = value1 - > intvalue - value2 - > intvalue ;
setTokenValue ( parent , result ) ;
break ;
case ' * ' :
result . intvalue = value1 - > intvalue * value2 - > intvalue ;
setTokenValue ( parent , result ) ;
break ;
case ' / ' :
if ( value2 - > intvalue = = 0 )
break ;
result . intvalue = value1 - > intvalue / value2 - > intvalue ;
setTokenValue ( parent , result ) ;
break ;
case ' % ' :
if ( value2 - > intvalue = = 0 )
break ;
result . intvalue = value1 - > intvalue % value2 - > intvalue ;
setTokenValue ( parent , result ) ;
break ;
2015-07-26 11:27:52 +02:00
case ' = ' :
if ( parent - > str ( ) = = " == " ) {
2015-08-02 18:12:03 +02:00
if ( value1 - > tokvalue | | value2 - > tokvalue )
result . intvalue = 0 ;
else
result . intvalue = value1 - > intvalue = = value2 - > intvalue ;
2015-07-26 11:27:52 +02:00
setTokenValue ( parent , result ) ;
}
break ;
case ' ! ' :
if ( parent - > str ( ) = = " != " ) {
2015-08-02 18:12:03 +02:00
if ( value1 - > tokvalue | | value2 - > tokvalue )
result . intvalue = 1 ;
else
result . intvalue = value1 - > intvalue ! = value2 - > intvalue ;
2015-07-26 11:27:52 +02:00
setTokenValue ( parent , result ) ;
}
break ;
case ' > ' :
2015-08-02 18:12:03 +02:00
if ( value1 - > tokvalue | | value2 - > tokvalue )
break ;
2015-07-26 11:27:52 +02:00
if ( parent - > str ( ) = = " > " )
result . intvalue = value1 - > intvalue > value2 - > intvalue ;
else if ( parent - > str ( ) = = " >= " )
result . intvalue = value1 - > intvalue > = value2 - > intvalue ;
else
break ;
setTokenValue ( parent , result ) ;
break ;
case ' < ' :
2015-08-02 18:12:03 +02:00
if ( value1 - > tokvalue | | value2 - > tokvalue )
break ;
2015-07-26 11:27:52 +02:00
if ( parent - > str ( ) = = " < " )
result . intvalue = value1 - > intvalue < value2 - > intvalue ;
else if ( parent - > str ( ) = = " <= " )
result . intvalue = value1 - > intvalue < = value2 - > intvalue ;
else
break ;
setTokenValue ( parent , result ) ;
break ;
2015-10-14 10:44:04 +02:00
case ' & ' :
result . intvalue = value1 - > intvalue & value2 - > intvalue ;
setTokenValue ( parent , result ) ;
break ;
case ' | ' :
result . intvalue = value1 - > intvalue | value2 - > intvalue ;
setTokenValue ( parent , result ) ;
break ;
case ' ^ ' :
result . intvalue = value1 - > intvalue ^ value2 - > intvalue ;
setTokenValue ( parent , result ) ;
break ;
2015-07-26 11:27:52 +02:00
default :
// unhandled operator, do nothing
break ;
2014-01-18 19:30:44 +01:00
}
}
}
}
}
2015-02-24 15:57:39 +01:00
2015-07-16 21:17:44 +02:00
// !
else if ( parent - > str ( ) = = " ! " ) {
std : : list < ValueFlow : : Value > : : const_iterator it ;
for ( it = tok - > values . begin ( ) ; it ! = tok - > values . end ( ) ; + + it ) {
if ( it - > tokvalue )
continue ;
ValueFlow : : Value v ( * it ) ;
v . intvalue = ! v . intvalue ;
setTokenValue ( parent , v ) ;
}
}
2015-02-24 15:57:39 +01:00
// Array element
else if ( parent - > str ( ) = = " [ " & & parent - > astOperand1 ( ) & & parent - > astOperand2 ( ) ) {
std : : list < ValueFlow : : Value > : : const_iterator value1 , value2 ;
for ( value1 = parent - > astOperand1 ( ) - > values . begin ( ) ; value1 ! = parent - > astOperand1 ( ) - > values . end ( ) ; + + value1 ) {
if ( ! value1 - > tokvalue )
continue ;
for ( value2 = parent - > astOperand2 ( ) - > values . begin ( ) ; value2 ! = parent - > astOperand2 ( ) - > values . end ( ) ; + + value2 ) {
if ( value2 - > tokvalue )
continue ;
if ( value1 - > varId = = 0U | | value2 - > varId = = 0U | |
( value1 - > varId = = value2 - > varId & & value1 - > varvalue = = value2 - > varvalue ) ) {
ValueFlow : : Value result ( 0 ) ;
result . condition = value1 - > condition ? value1 - > condition : value2 - > condition ;
result . inconclusive = value1 - > inconclusive | value2 - > inconclusive ;
result . varId = ( value1 - > varId ! = 0U ) ? value1 - > varId : value2 - > varId ;
result . varvalue = ( result . varId = = value1 - > varId ) ? value1 - > intvalue : value2 - > intvalue ;
2015-11-12 16:14:27 +01:00
if ( value1 - > valueKind = = value2 - > valueKind )
result . valueKind = value1 - > valueKind ;
2015-08-14 20:46:13 +02:00
if ( value1 - > tokvalue - > tokType ( ) = = Token : : eString ) {
2015-02-24 15:57:39 +01:00
const std : : string s = value1 - > tokvalue - > strValue ( ) ;
const MathLib : : bigint index = value2 - > intvalue ;
2015-11-12 16:14:27 +01:00
if ( index = = s . size ( ) ) {
result . intvalue = 0 ;
setTokenValue ( parent , result ) ;
} else if ( index > = 0 & & index < s . size ( ) ) {
2015-02-24 15:57:39 +01:00
result . intvalue = s [ index ] ;
setTokenValue ( parent , result ) ;
}
} else if ( value1 - > tokvalue - > str ( ) = = " { " ) {
MathLib : : bigint index = value2 - > intvalue ;
const Token * element = value1 - > tokvalue - > next ( ) ;
while ( index > 0 & & element - > str ( ) ! = " } " ) {
if ( element - > str ( ) = = " , " )
- - index ;
if ( Token : : Match ( element , " [{}()[]] " ) )
break ;
element = element - > next ( ) ;
}
if ( Token : : Match ( element , " %num% [,}] " ) ) {
result . intvalue = MathLib : : toLongNumber ( element - > str ( ) ) ;
setTokenValue ( parent , result ) ;
}
}
}
}
}
}
2014-01-18 19:30:44 +01:00
}
static void valueFlowNumber ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2015-11-20 10:10:38 +01:00
if ( ( tok - > isNumber ( ) & & MathLib : : isInt ( tok - > str ( ) ) ) | | ( tok - > tokType ( ) = = Token : : eChar ) ) {
2015-07-16 17:33:16 +02:00
ValueFlow : : Value value ( MathLib : : toLongNumber ( tok - > str ( ) ) ) ;
2015-07-25 19:36:29 +02:00
value . setKnown ( ) ;
2015-07-16 17:33:16 +02:00
setTokenValue ( tok , value ) ;
}
2014-01-18 19:30:44 +01:00
}
2015-07-14 18:02:26 +02:00
if ( tokenlist - > isCPP ( ) ) {
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2015-07-16 17:33:16 +02:00
if ( tok - > isName ( ) & & ! tok - > varId ( ) & & Token : : Match ( tok , " false|true " ) ) {
ValueFlow : : Value value ( tok - > str ( ) = = " true " ) ;
2015-07-25 19:36:29 +02:00
value . setKnown ( ) ;
2015-07-16 17:33:16 +02:00
setTokenValue ( tok , value ) ;
}
2015-07-14 18:02:26 +02:00
}
}
2014-01-18 19:30:44 +01:00
}
2014-08-03 20:11:22 +02:00
static void valueFlowString ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2015-08-14 20:46:13 +02:00
if ( tok - > tokType ( ) = = Token : : eString ) {
2014-08-03 20:11:22 +02:00
ValueFlow : : Value strvalue ;
strvalue . tokvalue = tok ;
2015-07-25 19:36:29 +02:00
strvalue . setKnown ( ) ;
2014-08-03 20:11:22 +02:00
setTokenValue ( tok , strvalue ) ;
}
2015-02-24 15:57:39 +01:00
}
}
static void valueFlowArray ( TokenList * tokenlist )
{
std : : map < unsigned int , const Token * > constantArrays ;
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2015-08-28 14:27:56 +02:00
if ( tok - > varId ( ) > 0U ) {
const std : : map < unsigned int , const Token * > : : const_iterator it = constantArrays . find ( tok - > varId ( ) ) ;
if ( it ! = constantArrays . end ( ) ) {
ValueFlow : : Value value ;
value . tokvalue = it - > second ;
2015-11-07 15:07:26 +01:00
value . setKnown ( ) ;
setTokenValue ( tok , value ) ;
}
// pointer = array
else if ( tok - > variable ( ) & &
tok - > variable ( ) - > isArray ( ) & &
Token : : simpleMatch ( tok - > astParent ( ) , " = " ) & &
tok = = tok - > astParent ( ) - > astOperand2 ( ) & &
tok - > astParent ( ) - > astOperand1 ( ) & &
tok - > astParent ( ) - > astOperand1 ( ) - > variable ( ) & &
tok - > astParent ( ) - > astOperand1 ( ) - > variable ( ) - > isPointer ( ) ) {
ValueFlow : : Value value ;
value . tokvalue = tok ;
value . setKnown ( ) ;
2015-08-28 14:27:56 +02:00
setTokenValue ( tok , value ) ;
}
continue ;
}
2015-02-24 15:57:39 +01:00
if ( Token : : Match ( tok , " const %type% %var% [ %num%| ] = { " ) ) {
const Token * vartok = tok - > tokAt ( 2 ) ;
const Token * rhstok = vartok - > next ( ) - > link ( ) - > tokAt ( 2 ) ;
constantArrays [ vartok - > varId ( ) ] = rhstok ;
tok = rhstok - > link ( ) ;
2015-08-28 14:27:56 +02:00
continue ;
2015-02-24 15:57:39 +01:00
}
2015-02-07 18:14:22 +01:00
2015-11-07 15:07:26 +01:00
else if ( Token : : Match ( tok , " const char %var% [ %num%| ] = %str% ; " ) ) {
2015-02-07 18:14:22 +01:00
const Token * vartok = tok - > tokAt ( 2 ) ;
2015-02-09 08:23:36 +01:00
const Token * strtok = vartok - > next ( ) - > link ( ) - > tokAt ( 2 ) ;
2015-02-24 15:57:39 +01:00
constantArrays [ vartok - > varId ( ) ] = strtok ;
tok = strtok - > next ( ) ;
2015-08-28 14:27:56 +02:00
continue ;
2015-02-07 18:14:22 +01:00
}
2014-08-03 20:11:22 +02:00
}
}
static void valueFlowPointerAlias ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
// not address of
if ( tok - > str ( ) ! = " & " | | tok - > astOperand2 ( ) )
continue ;
// parent should be a '='
if ( ! Token : : simpleMatch ( tok - > astParent ( ) , " = " ) )
continue ;
// child should be some buffer or variable
2015-11-15 23:38:23 +01:00
const Token * vartok = tok - > astOperand1 ( ) ;
while ( vartok ) {
if ( vartok - > str ( ) = = " [ " )
vartok = vartok - > astOperand1 ( ) ;
else if ( vartok - > str ( ) = = " . " | | vartok - > str ( ) = = " :: " )
vartok = vartok - > astOperand2 ( ) ;
else
break ;
}
if ( ! ( vartok & & vartok - > variable ( ) & & ! vartok - > variable ( ) - > isPointer ( ) ) )
2014-08-03 20:11:22 +02:00
continue ;
ValueFlow : : Value value ;
value . tokvalue = tok ;
setTokenValue ( tok , value ) ;
}
}
2014-04-14 06:45:39 +02:00
static void valueFlowBitAnd ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) ! = " & " )
continue ;
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
MathLib : : bigint number ;
if ( MathLib : : isInt ( tok - > astOperand1 ( ) - > str ( ) ) )
number = MathLib : : toLongNumber ( tok - > astOperand1 ( ) - > str ( ) ) ;
else if ( MathLib : : isInt ( tok - > astOperand2 ( ) - > str ( ) ) )
number = MathLib : : toLongNumber ( tok - > astOperand2 ( ) - > str ( ) ) ;
else
continue ;
int bit = 0 ;
while ( bit < = 60 & & ( ( 1LL < < bit ) < number ) )
+ + bit ;
if ( ( 1LL < < bit ) = = number ) {
setTokenValue ( tok , ValueFlow : : Value ( 0 ) ) ;
setTokenValue ( tok , ValueFlow : : Value ( number ) ) ;
}
}
}
2015-07-20 19:45:38 +02:00
static void valueFlowReverse ( TokenList * tokenlist ,
Token * tok ,
const Token * const varToken ,
ValueFlow : : Value val ,
ValueFlow : : Value val2 ,
ErrorLogger * errorLogger ,
const Settings * settings )
{
const MathLib : : bigint num = val . intvalue ;
const Variable * const var = varToken - > variable ( ) ;
2015-11-05 19:27:10 +01:00
if ( ! var )
return ;
2015-07-20 19:45:38 +02:00
const unsigned int varid = varToken - > varId ( ) ;
const Token * const startToken = var - > nameToken ( ) ;
for ( Token * tok2 = tok - > previous ( ) ; ; tok2 = tok2 - > previous ( ) ) {
if ( ! tok2 | |
tok2 = = startToken | |
( tok2 - > str ( ) = = " { " & & tok2 - > scope ( ) - > type = = Scope : : ScopeType : : eFunction ) ) {
break ;
}
if ( tok2 - > varId ( ) = = varid ) {
// bailout: assignment
if ( Token : : Match ( tok2 - > previous ( ) , " !!* %name% = " ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " assignment of " + tok2 - > str ( ) ) ;
break ;
}
// increment/decrement
if ( Token : : Match ( tok2 - > previous ( ) , " [;{}] %name% ++|-- ; " ) )
val . intvalue + = ( tok2 - > strAt ( 1 ) = = " ++ " ) ? - 1 : 1 ;
else if ( Token : : Match ( tok2 - > tokAt ( - 2 ) , " [ ; { } ] + + | - - % name % ; " ))
val . intvalue + = ( tok2 - > strAt ( - 1 ) = = " ++ " ) ? - 1 : 1 ;
else if ( Token : : Match ( tok2 - > previous ( ) , " ++|-- %name% " ) | | Token : : Match ( tok2 , " %name% ++|-- " ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " increment/decrement of " + tok2 - > str ( ) ) ;
break ;
}
// bailout: variable is used in rhs in assignment to itself
if ( bailoutSelfAssignment ( tok2 ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + tok2 - > str ( ) + " is used in rhs in assignment to itself " ) ;
break ;
}
if ( Token : : Match ( tok2 - > previous ( ) , " sizeof|. " ) ) {
const Token * prev = tok2 - > previous ( ) ;
while ( Token : : Match ( prev , " %name%|. " ) & & prev - > str ( ) ! = " sizeof " )
prev = prev - > previous ( ) ;
if ( prev & & prev - > str ( ) = = " sizeof " )
continue ;
}
// assigned by subfunction?
bool inconclusive = false ;
if ( bailoutFunctionPar ( tok2 , val2 . condition ? val2 : val , settings , & inconclusive ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " possible assignment of " + tok2 - > str ( ) + " by subfunction " ) ;
break ;
}
val . inconclusive | = inconclusive ;
val2 . inconclusive | = inconclusive ;
// skip if variable is conditionally used in ?: expression
if ( const Token * parent = skipValueInConditionalExpression ( tok2 ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
tok2 ,
" no simplification of " + tok2 - > str ( ) + " within " + ( Token : : Match ( parent , " [?:] " ) ? " ?: " : parent - > str ( ) ) + " expression " ) ;
continue ;
}
setTokenValue ( tok2 , val ) ;
if ( val2 . condition )
setTokenValue ( tok2 , val2 ) ;
2015-11-05 19:27:10 +01:00
if ( tok2 = = var - > nameToken ( ) )
2015-07-20 19:45:38 +02:00
break ;
}
// skip sizeof..
if ( tok2 - > str ( ) = = " ) " & & Token : : Match ( tok2 - > link ( ) - > previous ( ) , " typeof|sizeof ( " ) )
tok2 = tok2 - > link ( ) ;
// goto label
if ( Token : : Match ( tok2 , " [;{}] %name% : " ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 - > next ( ) , " variable " + var - > name ( ) + " stopping on goto label " ) ;
break ;
}
if ( tok2 - > str ( ) = = " } " ) {
const Token * vartok = Token : : findmatch ( tok2 - > link ( ) , " %varid% " , tok2 , varid ) ;
while ( Token : : Match ( vartok , " %name% = %num% ; " ) & & ! vartok - > tokAt ( 2 ) - > getValue ( num ) )
vartok = Token : : findmatch ( vartok - > next ( ) , " %varid% " , tok2 , varid ) ;
if ( vartok ) {
if ( settings - > debugwarnings ) {
std : : string errmsg = " variable " ;
2015-11-05 19:27:10 +01:00
errmsg + = var - > name ( ) + " " ;
2015-07-20 19:45:38 +02:00
errmsg + = " stopping on } " ;
bailout ( tokenlist , errorLogger , tok2 , errmsg ) ;
}
break ;
} else {
tok2 = tok2 - > link ( ) ;
}
} else if ( tok2 - > str ( ) = = " { " ) {
// if variable is assigned in loop don't look before the loop
if ( tok2 - > previous ( ) & &
( Token : : simpleMatch ( tok2 - > previous ( ) , " do " ) | |
( tok2 - > strAt ( - 1 ) = = " ) " & & Token : : Match ( tok2 - > linkAt ( - 1 ) - > previous ( ) , " for|while ( " ) ) ) ) {
const Token * start = tok2 ;
const Token * end = start - > link ( ) ;
if ( isVariableChanged ( start , end , varid ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " is assigned in loop. so valueflow analysis bailout when start of loop is reached. " ) ;
break ;
}
}
// Global variable : stop when leaving the function scope
if ( ! var - > isLocal ( ) ) {
if ( ! Token : : Match ( tok2 - > previous ( ) , " )|else|do { " ) )
break ;
2015-08-28 14:16:10 +02:00
if ( ( tok2 - > previous ( ) - > str ( ) = = " ) " ) & &
2015-07-20 19:45:38 +02:00
! Token : : Match ( tok2 - > linkAt ( - 1 ) - > previous ( ) , " if|for|while ( " ) )
break ;
}
} else if ( tok2 - > str ( ) = = " ; " ) {
const Token * parent = tok2 - > previous ( ) ;
while ( parent & & ! Token : : Match ( parent , " return|break|continue|goto " ) )
parent = parent - > astParent ( ) ;
// reaching a break/continue/return
if ( parent ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " stopping on " + parent - > str ( ) ) ;
break ;
}
}
}
}
2015-02-01 12:10:20 +01:00
static void valueFlowBeforeCondition ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-01-04 20:57:02 +01:00
{
2015-02-01 12:10:20 +01:00
const std : : size_t functions = symboldatabase - > functionScopes . size ( ) ;
for ( std : : size_t i = 0 ; i < functions ; + + i ) {
const Scope * scope = symboldatabase - > functionScopes [ i ] ;
for ( Token * tok = const_cast < Token * > ( scope - > classStart ) ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
2015-07-20 19:45:38 +02:00
MathLib : : bigint num = 0 ;
const Token * vartok = nullptr ;
2015-02-01 12:10:20 +01:00
if ( tok - > isComparisonOp ( ) & & tok - > astOperand1 ( ) & & tok - > astOperand2 ( ) ) {
if ( tok - > astOperand1 ( ) - > isName ( ) & & tok - > astOperand2 ( ) - > isNumber ( ) ) {
2015-07-20 19:45:38 +02:00
vartok = tok - > astOperand1 ( ) ;
2015-02-01 12:10:20 +01:00
num = MathLib : : toLongNumber ( tok - > astOperand2 ( ) - > str ( ) ) ;
} else if ( tok - > astOperand1 ( ) - > isNumber ( ) & & tok - > astOperand2 ( ) - > isName ( ) ) {
2015-07-20 19:45:38 +02:00
vartok = tok - > astOperand2 ( ) ;
2015-02-01 12:10:20 +01:00
num = MathLib : : toLongNumber ( tok - > astOperand1 ( ) - > str ( ) ) ;
} else {
continue ;
}
} else if ( Token : : Match ( tok - > previous ( ) , " if|while ( %name% %oror%|&&|) " ) | |
Token : : Match ( tok , " %oror%|&& %name% %oror%|&&|) " ) ) {
2015-07-20 19:45:38 +02:00
vartok = tok - > next ( ) ;
2015-02-01 12:10:20 +01:00
num = 0 ;
} else if ( tok - > str ( ) = = " ! " & & tok - > astOperand1 ( ) & & tok - > astOperand1 ( ) - > isName ( ) ) {
2015-07-20 19:45:38 +02:00
vartok = tok - > astOperand1 ( ) ;
2015-02-01 12:10:20 +01:00
num = 0 ;
2014-01-04 20:57:02 +01:00
} else {
continue ;
}
2015-07-20 19:45:38 +02:00
unsigned int varid = vartok - > varId ( ) ;
const Variable * const var = vartok - > variable ( ) ;
2015-02-01 12:10:20 +01:00
if ( varid = = 0U | | ! var )
2014-01-15 17:32:14 +01:00
continue ;
2014-01-14 17:57:50 +01:00
2015-02-01 12:10:20 +01:00
// bailout: global non-const variables
if ( ! ( var - > isLocal ( ) | | var - > isArgument ( ) ) & & ! var - > isConst ( ) ) {
2014-01-15 17:32:14 +01:00
if ( settings - > debugwarnings )
2015-02-01 12:10:20 +01:00
bailout ( tokenlist , errorLogger , tok , " global variable " + var - > name ( ) ) ;
2014-08-17 20:32:17 +02:00
continue ;
2014-01-05 20:06:46 +01:00
}
2014-01-10 16:51:58 +01:00
2015-02-01 12:10:20 +01:00
// bailout: for/while-condition, variable is changed in while loop
for ( const Token * tok2 = tok ; tok2 ; tok2 = tok2 - > astParent ( ) ) {
if ( tok2 - > astParent ( ) | | tok2 - > str ( ) ! = " ( " | | ! Token : : simpleMatch ( tok2 - > link ( ) , " ) { " ) )
continue ;
2014-01-06 07:44:58 +01:00
2015-02-01 12:10:20 +01:00
// Variable changed in 3rd for-expression
if ( Token : : simpleMatch ( tok2 - > previous ( ) , " for ( " ) ) {
2015-10-15 21:27:55 +02:00
if ( tok2 - > astOperand2 ( ) & & tok2 - > astOperand2 ( ) - > astOperand2 ( ) & & isVariableChanged ( tok2 - > astOperand2 ( ) - > astOperand2 ( ) , tok2 - > link ( ) , varid ) ) {
2015-02-01 12:10:20 +01:00
varid = 0U ;
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok , " variable " + var - > name ( ) + " used in loop " ) ;
}
2014-01-17 19:28:28 +01:00
}
2015-02-01 12:10:20 +01:00
// Variable changed in loop code
if ( Token : : Match ( tok2 - > previous ( ) , " for|while ( " ) ) {
const Token * const start = tok2 - > link ( ) - > next ( ) ;
const Token * const end = start - > link ( ) ;
2014-01-11 15:36:09 +01:00
2015-02-01 12:10:20 +01:00
if ( isVariableChanged ( start , end , varid ) ) {
varid = 0U ;
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok , " variable " + var - > name ( ) + " used in loop " ) ;
}
2014-04-29 20:09:11 +02:00
}
2015-02-01 12:10:20 +01:00
// if,macro => bailout
else if ( Token : : simpleMatch ( tok2 - > previous ( ) , " if ( " ) & & tok2 - > previous ( ) - > isExpandedMacro ( ) ) {
varid = 0U ;
2014-01-11 14:31:51 +01:00
if ( settings - > debugwarnings )
2015-02-01 12:10:20 +01:00
bailout ( tokenlist , errorLogger , tok , " variable " + var - > name ( ) + " , condition is defined in macro " ) ;
2014-01-11 14:31:51 +01:00
}
2015-02-01 12:10:20 +01:00
}
if ( varid = = 0U )
continue ;
2014-01-11 14:31:51 +01:00
2015-02-01 12:10:20 +01:00
// extra logic for unsigned variables 'i>=1' => possible value can also be 0
ValueFlow : : Value val ( tok , num ) ;
val . varId = varid ;
if ( Token : : Match ( tok , " <|> " ) ) {
if ( num ! = 0 )
continue ;
if ( ! var - > typeStartToken ( ) - > isUnsigned ( ) )
2014-01-08 06:39:15 +01:00
continue ;
2014-01-04 20:57:02 +01:00
}
2015-02-01 12:10:20 +01:00
ValueFlow : : Value val2 ;
if ( num = = 1U & & Token : : Match ( tok , " <=|>= " ) ) {
if ( var - > typeStartToken ( ) - > isUnsigned ( ) ) {
val2 = ValueFlow : : Value ( tok , 0 ) ;
val2 . varId = varid ;
}
2014-04-28 06:21:48 +02:00
}
2015-07-20 19:45:38 +02:00
valueFlowReverse ( tokenlist ,
tok ,
vartok ,
val ,
val2 ,
errorLogger ,
settings ) ;
2015-02-01 12:10:20 +01:00
2014-01-04 20:57:02 +01:00
}
}
}
2014-09-04 17:52:14 +02:00
static void removeValues ( std : : list < ValueFlow : : Value > & values , const std : : list < ValueFlow : : Value > & valuesToRemove )
{
for ( std : : list < ValueFlow : : Value > : : iterator it = values . begin ( ) ; it ! = values . end ( ) ; ) {
bool found = false ;
for ( std : : list < ValueFlow : : Value > : : const_iterator it2 = valuesToRemove . begin ( ) ; it2 ! = valuesToRemove . end ( ) ; + + it2 ) {
if ( it - > intvalue = = it2 - > intvalue ) {
found = true ;
break ;
}
}
if ( found )
values . erase ( it + + ) ;
else
2014-09-04 18:08:56 +02:00
+ + it ;
2014-09-04 17:52:14 +02:00
}
}
2015-05-24 17:02:00 +02:00
static void valueFlowAST ( Token * tok , unsigned int varid , const ValueFlow : : Value & value )
{
if ( ! tok )
return ;
if ( tok - > varId ( ) = = varid )
setTokenValue ( tok , value ) ;
valueFlowAST ( const_cast < Token * > ( tok - > astOperand1 ( ) ) , varid , value ) ;
valueFlowAST ( const_cast < Token * > ( tok - > astOperand2 ( ) ) , varid , value ) ;
}
2015-07-17 20:30:34 +02:00
/** if known variable is changed in loop body, change it to a possible value */
2015-07-29 11:55:07 +02:00
static void handleKnownValuesInLoop ( const Token * startToken ,
const Token * endToken ,
2015-07-17 20:30:34 +02:00
std : : list < ValueFlow : : Value > * values ,
unsigned int varid )
{
bool isChanged = false ;
for ( std : : list < ValueFlow : : Value > : : iterator it = values - > begin ( ) ; it ! = values - > end ( ) ; + + it ) {
2015-07-25 19:36:29 +02:00
if ( it - > isKnown ( ) ) {
2015-07-17 20:30:34 +02:00
if ( ! isChanged ) {
2015-07-29 11:55:07 +02:00
if ( ! isVariableChanged ( startToken , endToken , varid ) )
2015-07-17 20:30:34 +02:00
break ;
isChanged = true ;
}
2015-07-25 19:36:29 +02:00
it - > setPossible ( ) ;
2015-07-17 20:30:34 +02:00
}
}
}
2014-06-18 05:51:23 +02:00
static bool valueFlowForward ( Token * const startToken ,
2014-06-15 16:47:01 +02:00
const Token * const endToken ,
const Variable * const var ,
const unsigned int varid ,
std : : list < ValueFlow : : Value > values ,
const bool constValue ,
TokenList * const tokenlist ,
ErrorLogger * const errorLogger ,
const Settings * const settings )
{
int indentlevel = 0 ;
unsigned int number_of_if = 0 ;
int varusagelevel = - 1 ;
bool returnStatement = false ; // current statement is a return, stop analysis at the ";"
2014-08-05 08:28:46 +02:00
bool read = false ; // is variable value read?
2014-06-15 16:47:01 +02:00
for ( Token * tok2 = startToken ; tok2 & & tok2 ! = endToken ; tok2 = tok2 - > next ( ) ) {
if ( indentlevel > = 0 & & tok2 - > str ( ) = = " { " )
+ + indentlevel ;
2014-08-27 16:59:18 +02:00
else if ( indentlevel > = 0 & & tok2 - > str ( ) = = " } " ) {
2014-06-15 16:47:01 +02:00
- - indentlevel ;
2015-01-06 14:12:35 +01:00
if ( indentlevel < = 0 & & isReturn ( tok2 ) & & Token : : Match ( tok2 - > link ( ) - > previous ( ) , " else|) { " ) ) {
2015-01-05 16:39:47 +01:00
const Token * condition = tok2 - > link ( ) ;
const bool iselse = Token : : simpleMatch ( condition - > tokAt ( - 2 ) , " } else { " ) ;
if ( iselse )
condition = condition - > linkAt ( - 2 ) ;
if ( condition & & Token : : simpleMatch ( condition - > previous ( ) , " ) { " ) )
condition = condition - > linkAt ( - 1 ) - > astOperand2 ( ) ;
else
condition = nullptr ;
2014-08-27 17:27:34 +02:00
if ( ! condition ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " valueFlowForward, bailing out since it's unknown if conditional return is executed " ) ;
return false ;
}
2014-08-27 16:59:18 +02:00
bool bailoutflag = false ;
for ( std : : list < ValueFlow : : Value > : : const_iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
2015-01-05 16:39:47 +01:00
if ( ! iselse & & conditionIsTrue ( condition , getProgramMemory ( condition - > astParent ( ) , varid , * it ) ) ) {
bailoutflag = true ;
break ;
}
if ( iselse & & conditionIsFalse ( condition , getProgramMemory ( condition - > astParent ( ) , varid , * it ) ) ) {
2014-08-27 16:59:18 +02:00
bailoutflag = true ;
break ;
}
}
if ( bailoutflag ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " valueFlowForward, conditional return is assumed to be executed " ) ;
return false ;
}
}
}
2014-06-15 16:47:01 +02:00
2015-11-08 14:08:47 +01:00
if ( Token : : Match ( tok2 , " [;{}] %name% : " ) | | tok2 - > str ( ) = = " case " ) {
2015-07-26 17:05:21 +02:00
for ( std : : list < ValueFlow : : Value > : : iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it )
it - > changeKnownToPossible ( ) ;
tok2 = tok2 - > tokAt ( 2 ) ;
continue ;
}
2014-06-15 16:47:01 +02:00
if ( Token : : Match ( tok2 , " sizeof|typeof|typeid ( " ) )
tok2 = tok2 - > linkAt ( 1 ) ;
2014-06-25 16:00:56 +02:00
else if ( Token : : simpleMatch ( tok2 , " else { " ) ) {
// Should scope be skipped because variable value is checked?
bool skipelse = false ;
const Token * condition = tok2 - > linkAt ( - 1 ) ;
condition = condition ? condition - > linkAt ( - 1 ) : nullptr ;
condition = condition ? condition - > astOperand2 ( ) : nullptr ;
for ( std : : list < ValueFlow : : Value > : : iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
2014-08-01 07:35:15 +02:00
if ( conditionIsTrue ( condition , getProgramMemory ( tok2 , varid , * it ) ) ) {
2014-06-25 16:00:56 +02:00
skipelse = true ;
break ;
}
}
if ( skipelse ) {
tok2 = tok2 - > linkAt ( 1 ) ;
continue ;
}
}
2015-07-17 20:30:34 +02:00
else if ( Token : : simpleMatch ( tok2 , " do { " ) ) {
2015-07-29 11:55:07 +02:00
const Token * start = tok2 - > next ( ) ;
const Token * end = start - > link ( ) ;
if ( Token : : simpleMatch ( end , " } while ( " ) )
end = end - > linkAt ( 2 ) ;
handleKnownValuesInLoop ( start , end , & values , varid ) ;
2015-07-17 20:30:34 +02:00
}
2014-06-15 16:47:01 +02:00
// conditional block of code that assigns variable..
2015-01-31 10:50:39 +01:00
else if ( Token : : Match ( tok2 , " %name% ( " ) & & Token : : simpleMatch ( tok2 - > linkAt ( 1 ) , " ) { " )) {
2014-10-11 17:48:51 +02:00
// is variable changed in condition?
if ( isVariableChanged ( tok2 - > next ( ) , tok2 - > next ( ) - > link ( ) , varid ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " valueFlowForward, assignment in condition " ) ;
return false ;
}
2015-07-17 10:24:24 +02:00
// if known variable is changed in loop body, change it to a possible value..
2015-07-29 11:55:07 +02:00
handleKnownValuesInLoop ( tok2 , tok2 - > linkAt ( 1 ) - > linkAt ( 1 ) , & values , varid ) ;
2015-07-17 10:24:24 +02:00
2015-02-01 15:05:00 +01:00
// Set values in condition
for ( Token * tok3 = tok2 - > tokAt ( 2 ) ; tok3 ! = tok2 - > next ( ) - > link ( ) ; tok3 = tok3 - > next ( ) ) {
if ( tok3 - > varId ( ) = = varid ) {
for ( std : : list < ValueFlow : : Value > : : const_iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it )
setTokenValue ( tok3 , * it ) ;
2015-07-12 19:35:47 +02:00
} else if ( Token : : Match ( tok3 , " %oror%|&&|? " ) ) {
2015-02-01 15:05:00 +01:00
break ;
}
}
2015-10-27 12:33:46 +01:00
const Token * const condTok = tok2 - > next ( ) - > astOperand2 ( ) ;
const bool condAlwaysTrue = ( condTok & & condTok - > values . size ( ) = = 1U & & condTok - > values . front ( ) . isKnown ( ) & & condTok - > values . front ( ) . intvalue ! = 0 ) ;
2014-06-15 16:47:01 +02:00
// Should scope be skipped because variable value is checked?
2014-09-04 17:52:14 +02:00
std : : list < ValueFlow : : Value > truevalues ;
2015-05-25 23:15:59 +02:00
for ( std : : list < ValueFlow : : Value > : : const_iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
2015-10-27 12:33:46 +01:00
if ( condAlwaysTrue | | ! conditionIsFalse ( condTok , getProgramMemory ( tok2 , varid , * it ) ) )
2014-09-04 17:52:14 +02:00
truevalues . push_back ( * it ) ;
2014-06-15 16:47:01 +02:00
}
2015-10-27 12:33:46 +01:00
if ( truevalues . size ( ) ! = values . size ( ) | | condAlwaysTrue ) {
2014-09-04 17:52:14 +02:00
// '{'
Token * const startToken1 = tok2 - > linkAt ( 1 ) - > next ( ) ;
valueFlowForward ( startToken1 - > next ( ) ,
startToken1 - > link ( ) ,
var ,
varid ,
truevalues ,
constValue ,
tokenlist ,
errorLogger ,
settings ) ;
if ( isVariableChanged ( startToken1 , startToken1 - > link ( ) , varid ) )
removeValues ( values , truevalues ) ;
2014-06-15 16:47:01 +02:00
// goto '}'
2014-09-04 17:52:14 +02:00
tok2 = startToken1 - > link ( ) ;
2015-10-27 12:33:46 +01:00
if ( condAlwaysTrue & & isReturn ( tok2 ) )
return false ;
2014-06-15 16:47:01 +02:00
continue ;
}
Token * const start = tok2 - > linkAt ( 1 ) - > next ( ) ;
Token * const end = start - > link ( ) ;
bool varusage = ( indentlevel > = 0 & & constValue & & number_of_if = = 0U ) ?
isVariableChanged ( start , end , varid ) :
( nullptr ! = Token : : findmatch ( start , " %varid% " , end , varid ) ) ;
2014-08-05 08:28:46 +02:00
if ( ! read ) {
read = bool ( nullptr ! = Token : : findmatch ( tok2 , " %varid% !!= " , end , varid ) ) ;
}
2014-06-15 16:47:01 +02:00
if ( varusage ) {
varusagelevel = indentlevel ;
2014-07-16 09:12:56 +02:00
if ( indentlevel < 0 & & tok2 - > str ( ) = = " switch " )
return false ;
2014-06-15 16:47:01 +02:00
// TODO: don't check noreturn scopes
2014-08-05 08:28:46 +02:00
if ( read & & ( number_of_if > 0U | | Token : : findmatch ( tok2 , " %varid% " , start , varid ) ) ) {
2014-08-16 18:32:25 +02:00
// Set values in condition
const Token * const condend = tok2 - > linkAt ( 1 ) ;
for ( Token * condtok = tok2 ; condtok ! = condend ; condtok = condtok - > next ( ) ) {
if ( condtok - > varId ( ) = = varid ) {
std : : list < ValueFlow : : Value > : : const_iterator it ;
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it )
setTokenValue ( condtok , * it ) ;
}
if ( Token : : Match ( condtok , " %oror%|&& " ) )
break ;
}
2014-06-15 16:47:01 +02:00
if ( settings - > debugwarnings )
2014-08-26 11:08:21 +02:00
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " is assigned in conditional code " ) ;
2014-06-18 05:51:23 +02:00
return false ;
2014-06-15 16:47:01 +02:00
}
if ( var - > isStatic ( ) ) {
if ( settings - > debugwarnings )
2014-08-26 11:08:21 +02:00
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " bailout when conditional code that contains var is seen " ) ;
2014-06-18 05:51:23 +02:00
return false ;
2014-06-15 16:47:01 +02:00
}
// Remove conditional values
std : : list < ValueFlow : : Value > : : iterator it ;
for ( it = values . begin ( ) ; it ! = values . end ( ) ; ) {
if ( it - > condition | | it - > conditional )
values . erase ( it + + ) ;
2015-07-16 21:08:32 +02:00
else {
2015-07-25 19:36:29 +02:00
it - > changeKnownToPossible ( ) ;
2014-06-15 16:47:01 +02:00
+ + it ;
2015-07-16 21:08:32 +02:00
}
2014-06-15 16:47:01 +02:00
}
}
2015-01-06 14:12:35 +01:00
// stop after conditional noreturn scopes that are executed
if ( isReturn ( end ) ) {
std : : list < ValueFlow : : Value > : : iterator it ;
for ( it = values . begin ( ) ; it ! = values . end ( ) ; ) {
if ( conditionIsTrue ( tok2 - > next ( ) - > astOperand2 ( ) , getProgramMemory ( tok2 , varid , * it ) ) )
values . erase ( it + + ) ;
else
2015-01-06 15:16:29 +01:00
+ + it ;
2015-01-06 14:12:35 +01:00
}
if ( values . empty ( ) )
return false ;
}
2014-06-15 16:47:01 +02:00
// noreturn scopes..
if ( ( number_of_if > 0 | | Token : : findmatch ( tok2 , " %varid% " , start , varid ) ) & &
2014-06-18 05:51:23 +02:00
( Token : : findmatch ( start , " return|continue|break|throw " , end ) | |
( Token : : simpleMatch ( end , " } else { " ) & & Token : : findmatch ( end , " return|continue|break|throw " , end - > linkAt ( 2 ) ) ) ) ) {
2014-06-15 16:47:01 +02:00
if ( settings - > debugwarnings )
2014-08-26 11:08:21 +02:00
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " . noreturn conditional scope. " ) ;
2014-06-18 05:51:23 +02:00
return false ;
2014-06-15 16:47:01 +02:00
}
if ( isVariableChanged ( start , end , varid ) ) {
2014-08-05 08:28:46 +02:00
if ( ( ! read | | number_of_if = = 0 ) & &
2014-06-15 16:47:01 +02:00
Token : : simpleMatch ( tok2 , " if ( " ) & &
! ( Token : : simpleMatch ( end , " } else { " ) & &
( Token : : findmatch ( end , " %varid% " , end - > linkAt ( 2 ) , varid ) | |
2014-06-18 05:51:23 +02:00
Token : : findmatch ( end , " return|continue|break|throw " , end - > linkAt ( 2 ) ) ) ) ) {
2014-06-15 16:47:01 +02:00
+ + number_of_if ;
tok2 = end ;
} else {
2014-09-23 16:06:02 +02:00
// loop that conditionally set variable and then break => either loop condition is
// redundant or the variable can be unchanged after the loop.
bool loopCondition = false ;
if ( Token : : simpleMatch ( tok2 , " while ( " ) & & Token : : Match ( tok2 - > next ( ) - > astOperand2 ( ) , " %op% " ) )
loopCondition = true ;
else if ( Token : : simpleMatch ( tok2 , " for ( " ) & &
Token : : simpleMatch ( tok2 - > next ( ) - > astOperand2 ( ) , " ; " ) & &
Token : : simpleMatch ( tok2 - > next ( ) - > astOperand2 ( ) - > astOperand2 ( ) , " ; " ) & &
Token : : Match ( tok2 - > next ( ) - > astOperand2 ( ) - > astOperand2 ( ) - > astOperand1 ( ) , " %op% " ) )
loopCondition = true ;
2015-09-16 12:15:07 +02:00
bool bail = true ;
2014-09-23 16:06:02 +02:00
if ( loopCondition ) {
const Token * tok3 = Token : : findmatch ( start , " %varid% " , end , varid ) ;
if ( Token : : Match ( tok3 , " %varid% = " , varid ) & &
tok3 - > scope ( ) - > classEnd & &
Token : : Match ( tok3 - > scope ( ) - > classEnd - > tokAt ( - 3 ) , " [;}] break ; " ) & &
! Token : : findmatch ( tok3 - > next ( ) , " %varid% " , end , varid ) ) {
bail = false ;
tok2 = end ;
}
}
if ( bail ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " is assigned in conditional code " ) ;
return false ;
}
2014-06-15 16:47:01 +02:00
}
}
}
else if ( tok2 - > str ( ) = = " } " & & indentlevel = = varusagelevel ) {
+ + number_of_if ;
// Set "conditional" flag for all values
std : : list < ValueFlow : : Value > : : iterator it ;
2015-07-16 17:33:16 +02:00
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
2014-06-15 16:47:01 +02:00
it - > conditional = true ;
2015-07-25 19:36:29 +02:00
it - > changeKnownToPossible ( ) ;
2015-07-16 17:33:16 +02:00
}
2014-06-15 16:47:01 +02:00
if ( Token : : simpleMatch ( tok2 , " } else { " ) )
tok2 = tok2 - > linkAt ( 2 ) ;
}
2015-07-27 06:43:43 +02:00
else if ( Token : : Match ( tok2 , " break|continue|goto " ) ) {
const Scope * scope = tok2 - > scope ( ) ;
if ( indentlevel > 0 ) {
const Token * tok3 = tok2 - > tokAt ( 2 ) ;
int indentlevel2 = indentlevel ;
while ( indentlevel2 > 0 & &
tok3 - > str ( ) = = " } " & &
Token : : Match ( tok3 - > link ( ) - > previous ( ) , " !!) " ) ) {
indentlevel2 - - ;
tok3 = tok3 - > next ( ) ;
if ( tok3 & & tok3 - > str ( ) = = " ; " )
tok3 = tok3 - > next ( ) ;
}
if ( indentlevel2 > 0 )
continue ;
scope = tok3 - > scope ( ) ;
indentlevel = 0 ;
}
2014-06-30 17:56:42 +02:00
if ( tok2 - > str ( ) = = " break " ) {
if ( scope & & scope - > type = = Scope : : eSwitch ) {
tok2 = const_cast < Token * > ( scope - > classEnd ) ;
2015-07-27 06:43:43 +02:00
if ( tok2 = = endToken )
break ;
2014-07-16 09:12:56 +02:00
- - indentlevel ;
2015-07-20 09:36:56 +02:00
for ( std : : list < ValueFlow : : Value > : : iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
2015-07-25 19:36:29 +02:00
it - > changeKnownToPossible ( ) ;
2015-07-20 09:36:56 +02:00
}
2014-06-30 17:56:42 +02:00
continue ;
}
}
2014-06-15 16:47:01 +02:00
if ( settings - > debugwarnings )
2014-08-26 11:08:21 +02:00
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " . noreturn conditional scope. " ) ;
2014-06-18 05:51:23 +02:00
return false ;
2014-06-15 16:47:01 +02:00
}
2014-06-18 05:51:23 +02:00
else if ( indentlevel < = 0 & & Token : : Match ( tok2 , " return|throw " ) )
2014-06-15 16:47:01 +02:00
returnStatement = true ;
else if ( returnStatement & & tok2 - > str ( ) = = " ; " )
2014-06-18 05:51:23 +02:00
return false ;
2014-06-15 16:47:01 +02:00
2015-05-24 17:02:00 +02:00
// If a ? is seen and it's known that the condition is true/false..
else if ( tok2 - > str ( ) = = " ? " ) {
const Token * condition = tok2 - > astOperand1 ( ) ;
2015-11-09 11:21:07 +01:00
const Token * op2 = tok2 - > astOperand2 ( ) ;
if ( ! condition | | ! op2 ) // Ticket #6713
2015-05-30 20:55:36 +02:00
continue ;
2015-11-30 16:15:58 +01:00
if ( condition - > values . size ( ) = = 1U & & condition - > values . front ( ) . isKnown ( ) & & ! condition - > values . front ( ) . tokvalue ) {
const ValueFlow : : Value & condValue = condition - > values . front ( ) ;
const Token * expr = ( condValue . intvalue ! = 0 ) ? op2 - > astOperand1 ( ) : op2 - > astOperand2 ( ) ;
std : : list < ValueFlow : : Value > : : const_iterator it ;
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it )
valueFlowAST ( const_cast < Token * > ( expr ) , varid , * it ) ;
} else {
std : : list < ValueFlow : : Value > : : const_iterator it ;
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
const ProgramMemory programMemory ( getProgramMemory ( tok2 , varid , * it ) ) ;
if ( conditionIsTrue ( condition , programMemory ) )
valueFlowAST ( const_cast < Token * > ( op2 - > astOperand1 ( ) ) , varid , * it ) ;
else if ( conditionIsFalse ( condition , programMemory ) )
valueFlowAST ( const_cast < Token * > ( op2 - > astOperand2 ( ) ) , varid , * it ) ;
else
valueFlowAST ( const_cast < Token * > ( op2 ) , varid , * it ) ;
}
2015-05-24 17:02:00 +02:00
}
// Skip conditional expressions..
while ( tok2 - > astOperand1 ( ) | | tok2 - > astOperand2 ( ) ) {
if ( tok2 - > astOperand2 ( ) )
tok2 = const_cast < Token * > ( tok2 - > astOperand2 ( ) ) ;
else if ( tok2 - > isUnaryPreOp ( ) )
tok2 = const_cast < Token * > ( tok2 - > astOperand1 ( ) ) ;
else
break ;
}
tok2 = tok2 - > next ( ) ;
}
2014-06-15 16:47:01 +02:00
if ( tok2 - > varId ( ) = = varid ) {
// bailout: assignment
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok2 - > previous ( ) , " !!* %name% %op% " ) & & tok2 - > next ( ) - > isAssignmentOp ( ) ) {
2014-06-18 21:07:01 +02:00
// simplify rhs
for ( Token * tok3 = tok2 - > tokAt ( 2 ) ; tok3 ; tok3 = tok3 - > next ( ) ) {
if ( tok3 - > varId ( ) = = varid ) {
std : : list < ValueFlow : : Value > : : const_iterator it ;
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it )
2014-09-04 17:52:14 +02:00
setTokenValue ( tok3 , * it ) ;
2014-06-18 21:07:01 +02:00
} else if ( Token : : Match ( tok3 , " ++|--|?|:|; " ) )
break ;
}
2014-06-15 16:47:01 +02:00
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " assignment of " + tok2 - > str ( ) ) ;
2014-06-18 05:51:23 +02:00
return false ;
2014-06-15 16:47:01 +02:00
}
// bailout: possible assignment using >>
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok2 - > previous ( ) , " >> %name% >>|; " ) ) {
2014-06-15 16:47:01 +02:00
const Token * parent = tok2 - > previous ( ) ;
2015-05-13 13:40:40 +02:00
do {
2014-06-15 16:47:01 +02:00
parent = parent - > astParent ( ) ;
2015-05-13 13:40:40 +02:00
} while ( Token : : simpleMatch ( parent , " >> " ) ) ;
2014-06-15 16:47:01 +02:00
if ( ! parent ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " Possible assignment of " + tok2 - > str ( ) + " using >> " ) ;
2014-06-18 05:51:23 +02:00
return false ;
2014-06-15 16:47:01 +02:00
}
}
// skip if variable is conditionally used in ?: expression
if ( const Token * parent = skipValueInConditionalExpression ( tok2 ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
tok2 ,
" no simplification of " + tok2 - > str ( ) + " within " + ( Token : : Match ( parent , " [?:] " ) ? " ?: " : parent - > str ( ) ) + " expression " ) ;
2014-08-24 08:50:01 +02:00
const Token * astTop = parent - > astTop ( ) ;
if ( Token : : simpleMatch ( astTop - > astOperand1 ( ) , " for ( " ) )
tok2 = const_cast < Token * > ( astTop - > link ( ) ) ;
2014-06-15 16:47:01 +02:00
continue ;
}
{
2015-07-12 19:35:47 +02:00
// Is variable usage protected by && || ?:
const Token * tok3 = tok2 ;
const Token * parent = tok3 - > astParent ( ) ;
while ( parent & & ! Token : : Match ( parent , " %oror%|&&|: " ) ) {
tok3 = parent ;
parent = parent - > astParent ( ) ;
}
const bool conditional = parent & & ( parent - > str ( ) = = " : " | | parent - > astOperand2 ( ) = = tok3 ) ;
2014-06-15 16:47:01 +02:00
std : : list < ValueFlow : : Value > : : const_iterator it ;
2015-07-12 19:35:47 +02:00
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
if ( ! conditional | | ! it - > conditional )
setTokenValue ( tok2 , * it ) ;
}
2014-06-15 16:47:01 +02:00
}
2015-05-02 17:30:09 +02:00
// increment/decrement
if ( Token : : Match ( tok2 - > previous ( ) , " ++|-- %name% " ) | | Token : : Match ( tok2 , " %name% ++|-- " ) ) {
std : : list < ValueFlow : : Value > : : iterator it ;
// Erase values that are not int values..
for ( it = values . begin ( ) ; it ! = values . end ( ) ; ) {
if ( it - > tokvalue )
it = values . erase ( it ) ;
else
+ + it ;
}
if ( values . empty ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " increment/decrement of " + tok2 - > str ( ) ) ;
return false ;
}
2015-08-11 16:20:30 +02:00
const bool pre = Token : : Match ( tok2 - > previous ( ) , " ++|-- " ) ;
Token * const op = pre ? tok2 - > previous ( ) : tok2 - > next ( ) ;
const bool inc = ( op - > str ( ) = = " ++ " ) ;
2015-05-02 17:30:09 +02:00
// Perform increment/decrement..
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
if ( ! pre )
setTokenValue ( op , * it ) ;
it - > intvalue + = ( inc ? 1 : - 1 ) ;
if ( pre )
setTokenValue ( op , * it ) ;
}
}
2014-10-20 15:54:02 +02:00
// bailout if address of var is taken..
if ( tok2 - > astParent ( ) & & tok2 - > astParent ( ) - > str ( ) = = " & " & & ! tok2 - > astParent ( ) - > astOperand2 ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " Taking address of " + tok2 - > str ( ) ) ;
return false ;
}
// bailout if reference is created..
2015-01-31 10:50:39 +01:00
if ( tok2 - > astParent ( ) & & Token : : Match ( tok2 - > astParent ( ) - > tokAt ( - 2 ) , " & %name% = " ) ) {
2014-10-20 15:54:02 +02:00
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " Reference of " + tok2 - > str ( ) ) ;
return false ;
}
2014-06-15 16:47:01 +02:00
// assigned by subfunction?
bool inconclusive = false ;
if ( bailoutFunctionPar ( tok2 , ValueFlow : : Value ( ) , settings , & inconclusive ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " possible assignment of " + tok2 - > str ( ) + " by subfunction " ) ;
2014-06-18 05:51:23 +02:00
return false ;
2014-06-15 16:47:01 +02:00
}
if ( inconclusive ) {
std : : list < ValueFlow : : Value > : : iterator it ;
2015-07-29 10:49:17 +02:00
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
2014-06-15 16:47:01 +02:00
it - > inconclusive = true ;
2015-07-29 10:49:17 +02:00
it - > changeKnownToPossible ( ) ;
}
2014-06-15 16:47:01 +02:00
}
}
2015-07-26 19:28:42 +02:00
// Lambda function
if ( Token : : simpleMatch ( tok2 , " = [ " ) & &
Token : : simpleMatch ( tok2 - > linkAt ( 1 ) , " ] ( " ) & &
Token : : simpleMatch ( tok2 - > linkAt ( 1 ) - > linkAt ( 1 ) , " ) { " ) ) {
const Token * bodyStart = tok2 - > linkAt ( 1 ) - > linkAt ( 1 ) - > next ( ) ;
if ( isVariableChanged ( bodyStart , bodyStart - > link ( ) , varid ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " valueFlowForward, " + var - > name ( ) + " is changed in lambda function " ) ;
return false ;
}
}
2014-06-15 16:47:01 +02:00
}
2014-06-18 05:51:23 +02:00
return true ;
2014-06-15 16:47:01 +02:00
}
2015-02-01 12:10:20 +01:00
static void valueFlowAfterAssign ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-01-21 21:13:49 +01:00
{
2015-02-01 12:10:20 +01:00
const std : : size_t functions = symboldatabase - > functionScopes . size ( ) ;
for ( std : : size_t i = 0 ; i < functions ; + + i ) {
const Scope * scope = symboldatabase - > functionScopes [ i ] ;
for ( Token * tok = const_cast < Token * > ( scope - > classStart ) ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
// Assignment
if ( ( tok - > str ( ) ! = " = " ) | | ( tok - > astParent ( ) ) )
continue ;
2014-01-21 21:13:49 +01:00
2015-02-01 12:10:20 +01:00
// Lhs should be a variable
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand1 ( ) - > varId ( ) )
continue ;
const unsigned int varid = tok - > astOperand1 ( ) - > varId ( ) ;
const Variable * var = tok - > astOperand1 ( ) - > variable ( ) ;
if ( ! var | | ( ! var - > isLocal ( ) & & ! var - > isArgument ( ) ) )
continue ;
2014-07-15 10:36:13 +02:00
2015-02-01 12:10:20 +01:00
const Token * const endOfVarScope = var - > typeStartToken ( ) - > scope ( ) - > classEnd ;
2014-01-21 21:13:49 +01:00
2015-02-01 12:10:20 +01:00
// Rhs values..
if ( ! tok - > astOperand2 ( ) | | tok - > astOperand2 ( ) - > values . empty ( ) )
continue ;
2014-01-21 21:13:49 +01:00
2015-07-17 20:48:37 +02:00
std : : list < ValueFlow : : Value > values = tok - > astOperand2 ( ) - > values ;
2015-02-01 12:10:20 +01:00
const bool constValue = tok - > astOperand2 ( ) - > isNumber ( ) ;
2015-07-17 20:48:37 +02:00
// Static variable initialisation?
if ( var - > isStatic ( ) & & var - > nameToken ( ) = = tok - > astOperand1 ( ) ) {
for ( std : : list < ValueFlow : : Value > : : iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
2015-07-25 19:36:29 +02:00
it - > changeKnownToPossible ( ) ;
2015-07-17 20:48:37 +02:00
}
}
2015-02-01 12:10:20 +01:00
valueFlowForward ( tok , endOfVarScope , var , varid , values , constValue , tokenlist , errorLogger , settings ) ;
}
2014-06-15 16:47:01 +02:00
}
}
2014-02-22 12:09:54 +01:00
2015-02-01 12:10:20 +01:00
static void valueFlowAfterCondition ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-06-15 16:47:01 +02:00
{
2015-02-01 12:10:20 +01:00
const std : : size_t functions = symboldatabase - > functionScopes . size ( ) ;
for ( std : : size_t i = 0 ; i < functions ; + + i ) {
const Scope * scope = symboldatabase - > functionScopes [ i ] ;
for ( Token * tok = const_cast < Token * > ( scope - > classStart ) ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
const Token * vartok , * numtok ;
// Comparison
if ( Token : : Match ( tok , " ==|!=|>=|<= " ) ) {
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
if ( tok - > astOperand1 ( ) - > isNumber ( ) ) {
numtok = tok - > astOperand1 ( ) ;
vartok = tok - > astOperand2 ( ) ;
} else {
numtok = tok - > astOperand2 ( ) ;
vartok = tok - > astOperand1 ( ) ;
}
if ( vartok - > str ( ) = = " = " & & vartok - > astOperand1 ( ) & & vartok - > astOperand2 ( ) )
vartok = vartok - > astOperand1 ( ) ;
if ( ! vartok - > isName ( ) | | ! numtok - > isNumber ( ) | | ! MathLib : : isInt ( numtok - > str ( ) ) )
continue ;
} else if ( tok - > str ( ) = = " ! " ) {
vartok = tok - > astOperand1 ( ) ;
numtok = nullptr ;
if ( ! vartok | | ! vartok - > isName ( ) )
continue ;
} else if ( tok - > isName ( ) & &
( Token : : Match ( tok - > astParent ( ) , " %oror%|&& " ) | |
Token : : Match ( tok - > tokAt ( - 2 ) , " if|while ( %var% [)=] " ) ) ) {
vartok = tok ;
numtok = nullptr ;
2014-06-16 16:39:41 +02:00
2014-08-16 18:32:25 +02:00
} else {
2015-02-01 12:10:20 +01:00
continue ;
2014-06-16 16:39:41 +02:00
}
2015-02-01 12:10:20 +01:00
const unsigned int varid = vartok - > varId ( ) ;
if ( varid = = 0U )
2014-06-16 16:39:41 +02:00
continue ;
2015-02-01 12:10:20 +01:00
const Variable * var = vartok - > variable ( ) ;
if ( ! var | | ! ( var - > isLocal ( ) | | var - > isArgument ( ) ) )
2014-06-16 16:39:41 +02:00
continue ;
2015-02-01 12:10:20 +01:00
std : : list < ValueFlow : : Value > values ;
values . push_back ( ValueFlow : : Value ( tok , numtok ? MathLib : : toLongNumber ( numtok - > str ( ) ) : 0LL ) ) ;
if ( Token : : Match ( tok - > astParent ( ) , " %oror%|&& " ) ) {
Token * parent = const_cast < Token * > ( tok - > astParent ( ) ) ;
const std : : string & op ( parent - > str ( ) ) ;
if ( parent - > astOperand1 ( ) = = tok & &
( ( op = = " && " & & Token : : Match ( tok , " ==|>=|<=|! " ) ) | |
( op = = " || " & & Token : : Match ( tok , " %name%|!= " ) ) ) ) {
2015-02-09 12:40:17 +01:00
for ( ; parent & & parent - > str ( ) = = op ; parent = const_cast < Token * > ( parent - > astParent ( ) ) ) {
2015-02-01 12:10:20 +01:00
std : : stack < Token * > tokens ;
tokens . push ( const_cast < Token * > ( parent - > astOperand2 ( ) ) ) ;
2015-02-09 12:40:17 +01:00
bool assign = false ;
2015-02-01 12:10:20 +01:00
while ( ! tokens . empty ( ) ) {
Token * rhstok = tokens . top ( ) ;
tokens . pop ( ) ;
if ( ! rhstok )
continue ;
tokens . push ( const_cast < Token * > ( rhstok - > astOperand1 ( ) ) ) ;
tokens . push ( const_cast < Token * > ( rhstok - > astOperand2 ( ) ) ) ;
if ( rhstok - > varId ( ) = = varid )
setTokenValue ( rhstok , values . front ( ) ) ;
if ( Token : : Match ( rhstok , " ++|--|= " ) & & Token : : Match ( rhstok - > astOperand1 ( ) , " %varid% " , varid ) ) {
assign = true ;
break ;
}
2014-06-19 17:29:41 +02:00
}
2015-02-09 12:40:17 +01:00
if ( assign )
break ;
2015-02-01 12:10:20 +01:00
while ( parent - > astParent ( ) & & parent = = parent - > astParent ( ) - > astOperand2 ( ) )
parent = const_cast < Token * > ( parent - > astParent ( ) ) ;
2014-06-19 17:29:41 +02:00
}
}
}
2014-07-07 17:48:58 +02:00
2015-02-01 12:10:20 +01:00
const Token * top = tok - > astTop ( ) ;
if ( top & & Token : : Match ( top - > previous ( ) , " if|while ( " ) & & ! top - > previous ( ) - > isExpandedMacro ( ) ) {
// does condition reassign variable?
if ( tok ! = top - > astOperand2 ( ) & &
Token : : Match ( top - > astOperand2 ( ) , " %oror%|&& " ) & &
isVariableChanged ( top , top - > link ( ) , varid ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok , " assignment in condition " ) ;
2014-06-22 19:13:15 +02:00
continue ;
2014-06-18 05:51:23 +02:00
}
2014-08-26 18:48:11 +02:00
2015-02-01 12:10:20 +01:00
// start token of conditional code
Token * startToken = nullptr ;
// based on the comparison, should we check the if or while?
int codeblock = 0 ;
if ( Token : : Match ( tok , " ==|>=|<=|! " ) )
codeblock = 1 ;
else if ( Token : : Match ( tok , " %name%|!= " ) )
codeblock = 2 ;
// determine startToken based on codeblock
if ( codeblock > 0 ) {
// if astParent is "!" we need to invert codeblock
const Token * parent = tok - > astParent ( ) ;
while ( parent & & parent - > str ( ) = = " && " )
parent = parent - > astParent ( ) ;
if ( parent & & parent - > str ( ) = = " ! " )
codeblock = ( codeblock = = 1 ) ? 2 : 1 ;
// convert codeblock to a startToken
if ( codeblock = = 1 & & Token : : simpleMatch ( top - > link ( ) , " ) { " ) )
startToken = top - > link ( ) - > next ( ) ;
else if ( Token : : simpleMatch ( top - > link ( ) - > linkAt ( 1 ) , " } else { " ))
startToken = top - > link ( ) - > linkAt ( 1 ) - > tokAt ( 2 ) ;
}
2014-08-26 18:48:11 +02:00
2015-07-28 08:58:05 +02:00
if ( startToken ) {
if ( ! valueFlowForward ( startToken - > next ( ) , startToken - > link ( ) , var , varid , values , true , tokenlist , errorLogger , settings ) )
2015-07-28 12:47:08 +02:00
continue ;
if ( isVariableChanged ( startToken , startToken - > link ( ) , varid ) ) {
2015-07-28 08:58:05 +02:00
// TODO: The endToken should not be startToken->link() in the valueFlowForward call
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , startToken - > link ( ) , " valueFlowAfterCondition: " + var - > name ( ) + " is changed in conditional block " ) ;
2015-07-28 12:47:08 +02:00
continue ;
}
2015-07-28 08:58:05 +02:00
}
2015-02-01 12:10:20 +01:00
// After conditional code..
2015-07-28 08:58:05 +02:00
if ( Token : : simpleMatch ( top - > link ( ) , " ) { " ) ) {
2015-02-01 12:10:20 +01:00
Token * after = top - > link ( ) - > linkAt ( 1 ) ;
std : : string unknownFunction ;
if ( settings - > library . isScopeNoReturn ( after , & unknownFunction ) ) {
if ( settings - > debugwarnings & & ! unknownFunction . empty ( ) )
2014-06-18 05:51:23 +02:00
bailout ( tokenlist , errorLogger , after , " possible noreturn scope " ) ;
continue ;
}
2014-08-26 18:48:11 +02:00
2015-02-01 12:10:20 +01:00
bool isreturn = ( codeblock = = 1 & & isReturn ( after ) ) ;
if ( Token : : simpleMatch ( after , " } else { " ) ) {
after = after - > linkAt ( 2 ) ;
if ( Token : : simpleMatch ( after - > tokAt ( - 2 ) , " ) ; } " ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , after , " possible noreturn scope " ) ;
continue ;
}
isreturn | = ( codeblock = = 2 & & isReturn ( after ) ) ;
}
if ( ! isreturn ) {
// TODO: constValue could be true if there are no assignments in the conditional blocks and
// perhaps if there are no && and no || in the condition
bool constValue = false ;
valueFlowForward ( after - > next ( ) , top - > scope ( ) - > classEnd , var , varid , values , constValue , tokenlist , errorLogger , settings ) ;
}
2014-10-17 06:50:33 +02:00
}
2014-06-18 05:51:23 +02:00
}
2014-01-21 21:13:49 +01:00
}
}
}
2014-03-17 18:43:47 +01:00
static void execute ( const Token * expr ,
2015-09-12 19:49:02 +02:00
ProgramMemory * const programMemory ,
2014-03-17 18:43:47 +01:00
MathLib : : bigint * result ,
bool * error )
2014-01-07 19:20:56 +01:00
{
2014-03-17 18:43:47 +01:00
if ( ! expr )
* error = true ;
2015-12-01 07:49:19 +01:00
else if ( expr - > values . size ( ) = = 1U & & expr - > values . front ( ) . isKnown ( ) & & ! expr - > values . front ( ) . tokvalue ) {
* result = expr - > values . front ( ) . intvalue ;
}
2014-07-17 08:44:55 +02:00
else if ( expr - > isNumber ( ) ) {
2014-03-17 18:43:47 +01:00
* result = MathLib : : toLongNumber ( expr - > str ( ) ) ;
2014-07-17 08:44:55 +02:00
if ( MathLib : : isFloat ( expr - > str ( ) ) )
* error = true ;
}
2014-03-17 18:43:47 +01:00
else if ( expr - > varId ( ) > 0 ) {
2015-09-12 19:49:02 +02:00
if ( ! programMemory - > getIntValue ( expr - > varId ( ) , result ) )
2014-03-17 18:43:47 +01:00
* error = true ;
}
2014-01-07 19:20:56 +01:00
2014-03-17 18:43:47 +01:00
else if ( expr - > isComparisonOp ( ) ) {
2014-03-30 10:22:06 +02:00
MathLib : : bigint result1 ( 0 ) , result2 ( 0 ) ;
2014-03-17 18:43:47 +01:00
execute ( expr - > astOperand1 ( ) , programMemory , & result1 , error ) ;
execute ( expr - > astOperand2 ( ) , programMemory , & result2 , error ) ;
if ( expr - > str ( ) = = " < " )
* result = result1 < result2 ;
else if ( expr - > str ( ) = = " <= " )
* result = result1 < = result2 ;
else if ( expr - > str ( ) = = " > " )
* result = result1 > result2 ;
else if ( expr - > str ( ) = = " >= " )
* result = result1 > = result2 ;
else if ( expr - > str ( ) = = " == " )
* result = result1 = = result2 ;
else if ( expr - > str ( ) = = " != " )
* result = result1 ! = result2 ;
}
else if ( expr - > str ( ) = = " = " ) {
execute ( expr - > astOperand2 ( ) , programMemory , result , error ) ;
2014-04-28 15:54:54 +02:00
if ( ! * error & & expr - > astOperand1 ( ) & & expr - > astOperand1 ( ) - > varId ( ) )
2015-09-12 19:49:02 +02:00
programMemory - > setIntValue ( expr - > astOperand1 ( ) - > varId ( ) , * result ) ;
2014-03-17 18:43:47 +01:00
else
* error = true ;
}
2014-09-14 11:26:16 +02:00
else if ( Token : : Match ( expr , " ++|-- " ) ) {
2014-03-17 18:43:47 +01:00
if ( ! expr - > astOperand1 ( ) | | expr - > astOperand1 ( ) - > varId ( ) = = 0U )
* error = true ;
else {
2015-09-12 19:49:02 +02:00
std : : map < unsigned int , MathLib : : bigint > : : iterator var = programMemory - > intvalues . find ( expr - > astOperand1 ( ) - > varId ( ) ) ;
if ( var = = programMemory - > intvalues . end ( ) )
2014-03-17 18:43:47 +01:00
* error = true ;
else {
2014-03-25 18:22:22 +01:00
if ( var - > second = = 0 & &
expr - > str ( ) = = " -- " & &
expr - > astOperand1 ( ) - > variable ( ) & &
expr - > astOperand1 ( ) - > variable ( ) - > typeStartToken ( ) - > isUnsigned ( ) )
* error = true ; // overflow
2014-03-24 00:16:02 +01:00
* result = var - > second + ( expr - > str ( ) = = " ++ " ? 1 : - 1 ) ;
2014-03-17 18:43:47 +01:00
var - > second = * result ;
}
2014-03-16 08:38:52 +01:00
}
2014-03-17 18:43:47 +01:00
}
2014-03-22 19:02:33 +01:00
else if ( expr - > isArithmeticalOp ( ) & & expr - > astOperand1 ( ) & & expr - > astOperand2 ( ) ) {
2014-03-30 10:22:06 +02:00
MathLib : : bigint result1 ( 0 ) , result2 ( 0 ) ;
2014-03-22 19:02:33 +01:00
execute ( expr - > astOperand1 ( ) , programMemory , & result1 , error ) ;
execute ( expr - > astOperand2 ( ) , programMemory , & result2 , error ) ;
if ( expr - > str ( ) = = " + " )
* result = result1 + result2 ;
else if ( expr - > str ( ) = = " - " )
* result = result1 - result2 ;
2015-11-27 21:45:08 +01:00
else if ( expr - > str ( ) = = " * " ) {
if ( result2 & & ( result1 > std : : numeric_limits < MathLib : : bigint > : : max ( ) / result2 ) )
* error = true ;
else
* result = result1 * result2 ;
} else if ( result2 = = 0 )
2014-03-23 20:37:56 +01:00
* error = true ;
2014-03-22 19:02:33 +01:00
else if ( expr - > str ( ) = = " / " )
* result = result1 / result2 ;
else if ( expr - > str ( ) = = " % " )
* result = result1 % result2 ;
}
2014-03-17 18:43:47 +01:00
else if ( expr - > str ( ) = = " && " ) {
2014-03-24 06:48:06 +01:00
bool error1 = false ;
execute ( expr - > astOperand1 ( ) , programMemory , result , & error1 ) ;
if ( ! error1 & & * result = = 0 )
2014-03-17 18:43:47 +01:00
* result = 0 ;
else {
2014-03-24 06:48:06 +01:00
bool error2 = false ;
execute ( expr - > astOperand2 ( ) , programMemory , result , & error2 ) ;
if ( error1 & & error2 )
* error = true ;
if ( error2 )
2014-03-17 18:43:47 +01:00
* result = 1 ;
2014-03-24 06:48:06 +01:00
else
* result = ! ! * result ;
2014-01-07 19:20:56 +01:00
}
2014-03-17 18:43:47 +01:00
}
2014-01-07 19:20:56 +01:00
2014-03-17 18:43:47 +01:00
else if ( expr - > str ( ) = = " || " ) {
execute ( expr - > astOperand1 ( ) , programMemory , result , error ) ;
if ( * result = = 0 & & * error = = false )
execute ( expr - > astOperand2 ( ) , programMemory , result , error ) ;
}
2014-01-07 19:20:56 +01:00
2014-07-30 18:12:33 +02:00
else if ( expr - > str ( ) = = " ! " ) {
execute ( expr - > astOperand1 ( ) , programMemory , result , error ) ;
* result = ! ( * result ) ;
}
2014-08-17 06:42:16 +02:00
else if ( expr - > str ( ) = = " , " & & expr - > astOperand1 ( ) & & expr - > astOperand2 ( ) ) {
execute ( expr - > astOperand1 ( ) , programMemory , result , error ) ;
execute ( expr - > astOperand2 ( ) , programMemory , result , error ) ;
}
2015-02-07 18:14:22 +01:00
else if ( expr - > str ( ) = = " [ " & & expr - > astOperand1 ( ) & & expr - > astOperand2 ( ) ) {
2015-12-01 07:49:19 +01:00
const Token * tokvalue = nullptr ;
std : : map < unsigned int , const Token * > : : iterator var = programMemory - > tokvalues . find ( expr - > astOperand1 ( ) - > varId ( ) ) ;
if ( var ! = programMemory - > tokvalues . end ( ) ) {
tokvalue = var - > second ;
} else {
if ( expr - > astOperand1 ( ) - > values . size ( ) ! = 1U ) {
* error = true ;
return ;
}
tokvalue = expr - > astOperand1 ( ) - > values . front ( ) . tokvalue ;
2015-02-07 18:14:22 +01:00
}
2015-12-01 07:49:19 +01:00
if ( ! tokvalue | | ! tokvalue - > isLiteral ( ) ) {
2015-02-07 18:14:22 +01:00
* error = true ;
return ;
}
2015-12-01 07:49:19 +01:00
const std : : string strValue = tokvalue - > strValue ( ) ;
2015-02-07 18:14:22 +01:00
MathLib : : bigint index = 0 ;
execute ( expr - > astOperand2 ( ) , programMemory , & index , error ) ;
2015-07-04 13:19:21 +02:00
if ( index > = 0 & & index < strValue . size ( ) )
2015-02-07 18:14:22 +01:00
* result = strValue [ index ] ;
2015-07-04 13:19:21 +02:00
else if ( index = = strValue . size ( ) )
2015-02-07 18:14:22 +01:00
* result = 0 ;
else
* error = true ;
}
2014-03-17 18:43:47 +01:00
else
* error = true ;
}
2014-08-17 10:40:22 +02:00
static bool valueFlowForLoop1 ( const Token * tok , unsigned int * const varid , MathLib : : bigint * const num1 , MathLib : : bigint * const num2 , MathLib : : bigint * const numAfter )
2014-03-17 18:43:47 +01:00
{
tok = tok - > tokAt ( 2 ) ;
2014-12-30 18:50:22 +01:00
if ( ! Token : : Match ( tok , " %type%| %var% = " ) )
2014-03-17 18:43:47 +01:00
return false ;
2014-09-16 08:17:06 +02:00
const Token * const vartok = Token : : Match ( tok , " %var% = " ) ? tok : tok - > next ( ) ;
2014-03-17 18:43:47 +01:00
* varid = vartok - > varId ( ) ;
2015-03-24 14:01:59 +01:00
tok = vartok - > tokAt ( 2 ) ;
const Token * const num1tok = Token : : Match ( tok , " %num% ; " ) ? tok : nullptr ;
2014-03-29 20:20:22 +01:00
if ( num1tok )
* num1 = MathLib : : toLongNumber ( num1tok - > str ( ) ) ;
2015-01-31 10:50:39 +01:00
while ( Token : : Match ( tok , " %name%|%num%|%or%|+|-|*|/|&|[|]|( " ) )
2014-03-29 20:20:22 +01:00
tok = ( tok - > str ( ) = = " ( " ) ? tok - > link ( ) - > next ( ) : tok - > next ( ) ;
if ( ! tok | | tok - > str ( ) ! = " ; " )
return false ;
tok = tok - > next ( ) ;
2014-03-17 18:43:47 +01:00
const Token * num2tok = nullptr ;
if ( Token : : Match ( tok , " %varid% <|<=|!= " , vartok - > varId ( ) ) ) {
tok = tok - > next ( ) ;
num2tok = tok - > astOperand2 ( ) ;
if ( num2tok & & num2tok - > str ( ) = = " ( " & & ! num2tok - > astOperand2 ( ) )
num2tok = num2tok - > astOperand1 ( ) ;
2014-12-30 19:56:47 +01:00
if ( ! Token : : Match ( num2tok , " %num% ;|%oror% " ) ) // TODO: || enlarges the scope of the condition, so it should not cause FP, but it should no lnger be part of this pattern as soon as valueFlowForLoop2 can handle an unknown RHS of || better
2014-03-17 18:43:47 +01:00
num2tok = 0 ;
}
if ( ! num2tok )
return false ;
2014-04-14 22:46:51 +02:00
* num2 = MathLib : : toLongNumber ( num2tok - > str ( ) ) - ( ( tok - > str ( ) = = " <= " ) ? 0 : 1 ) ;
2014-12-30 19:56:47 +01:00
* numAfter = * num2 + 1 ;
2014-03-29 20:20:22 +01:00
if ( ! num1tok )
* num1 = * num2 ;
2014-03-17 18:43:47 +01:00
while ( tok & & tok - > str ( ) ! = " ; " )
tok = tok - > next ( ) ;
2014-12-30 18:50:22 +01:00
if ( ! Token : : Match ( tok , " ; %varid% ++ ) { " , vartok - > varId ( ) ) & & ! Token : : Match ( tok , " ; ++ %varid% ) { " , vartok - > varId ( ) ) )
2014-03-17 18:43:47 +01:00
return false ;
return true ;
}
static bool valueFlowForLoop2 ( const Token * tok ,
2015-09-12 19:49:02 +02:00
ProgramMemory * memory1 ,
ProgramMemory * memory2 ,
ProgramMemory * memoryAfter )
2014-03-17 18:43:47 +01:00
{
2015-06-07 11:25:33 +02:00
// for ( firstExpression ; secondExpression ; thirdExpression )
2014-03-17 18:43:47 +01:00
const Token * firstExpression = tok - > next ( ) - > astOperand2 ( ) - > astOperand1 ( ) ;
const Token * secondExpression = tok - > next ( ) - > astOperand2 ( ) - > astOperand2 ( ) - > astOperand1 ( ) ;
const Token * thirdExpression = tok - > next ( ) - > astOperand2 ( ) - > astOperand2 ( ) - > astOperand2 ( ) ;
2015-09-12 19:49:02 +02:00
ProgramMemory programMemory ;
2014-03-30 10:22:06 +02:00
MathLib : : bigint result ( 0 ) ;
2014-03-17 18:43:47 +01:00
bool error = false ;
execute ( firstExpression , & programMemory , & result , & error ) ;
if ( error )
return false ;
execute ( secondExpression , & programMemory , & result , & error ) ;
2014-06-30 07:26:48 +02:00
if ( error ) {
// If a variable is reassigned in second expression, return false
std : : stack < const Token * > tokens ;
tokens . push ( secondExpression ) ;
while ( ! tokens . empty ( ) ) {
const Token * t = tokens . top ( ) ;
tokens . pop ( ) ;
if ( ! t )
continue ;
2015-09-12 19:49:02 +02:00
if ( t - > str ( ) = = " = " & & t - > astOperand1 ( ) & & programMemory . hasValue ( t - > astOperand1 ( ) - > varId ( ) ) )
2014-06-30 07:26:48 +02:00
// TODO: investigate what variable is assigned.
return false ;
tokens . push ( t - > astOperand1 ( ) ) ;
tokens . push ( t - > astOperand2 ( ) ) ;
}
}
2014-03-24 00:16:02 +01:00
2015-09-12 19:49:02 +02:00
ProgramMemory startMemory ( programMemory ) ;
ProgramMemory endMemory ;
2014-03-17 18:43:47 +01:00
unsigned int maxcount = 10000 ;
while ( result ! = 0 & & ! error & & - - maxcount ) {
endMemory = programMemory ;
execute ( thirdExpression , & programMemory , & result , & error ) ;
if ( ! error )
execute ( secondExpression , & programMemory , & result , & error ) ;
}
memory1 - > swap ( startMemory ) ;
2014-08-17 10:40:22 +02:00
if ( ! error ) {
2014-03-24 00:16:02 +01:00
memory2 - > swap ( endMemory ) ;
2014-08-17 10:40:22 +02:00
memoryAfter - > swap ( programMemory ) ;
}
2014-03-17 18:43:47 +01:00
2014-03-24 00:16:02 +01:00
return true ;
2014-03-17 18:43:47 +01:00
}
static void valueFlowForLoopSimplify ( Token * const bodyStart , const unsigned int varid , const MathLib : : bigint value , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
const Token * const bodyEnd = bodyStart - > link ( ) ;
// Is variable modified inside for loop
2014-04-20 14:21:43 +02:00
if ( isVariableChanged ( bodyStart , bodyEnd , varid ) )
2014-03-17 18:43:47 +01:00
return ;
for ( Token * tok2 = bodyStart - > next ( ) ; tok2 ! = bodyEnd ; tok2 = tok2 - > next ( ) ) {
if ( tok2 - > varId ( ) = = varid ) {
const Token * parent = tok2 - > astParent ( ) ;
while ( parent ) {
const Token * const p = parent ;
parent = parent - > astParent ( ) ;
2014-09-03 12:17:34 +02:00
if ( ! parent | | parent - > str ( ) = = " : " )
2014-03-17 18:43:47 +01:00
break ;
2014-09-03 12:17:34 +02:00
if ( parent - > str ( ) = = " ? " ) {
2014-03-25 18:22:22 +01:00
if ( parent - > astOperand2 ( ) ! = p )
2015-12-05 20:55:26 +01:00
parent = nullptr ;
2014-03-25 18:22:22 +01:00
break ;
}
2014-03-15 11:29:33 +01:00
}
2014-03-17 18:43:47 +01:00
if ( parent ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " For loop variable " + tok2 - > str ( ) + " stopping on ? " ) ;
continue ;
}
ValueFlow : : Value value1 ( value ) ;
value1 . varId = tok2 - > varId ( ) ;
setTokenValue ( tok2 , value1 ) ;
2014-03-16 08:38:52 +01:00
}
2014-01-07 19:20:56 +01:00
2014-08-01 16:12:57 +02:00
if ( Token : : Match ( tok2 , " %oror%|&& " ) ) {
2015-09-12 19:49:02 +02:00
const ProgramMemory programMemory ( getProgramMemory ( tok2 - > astTop ( ) , varid , ValueFlow : : Value ( value ) ) ) ;
2014-08-01 16:12:57 +02:00
if ( ( tok2 - > str ( ) = = " && " & & conditionIsFalse ( tok2 - > astOperand1 ( ) , programMemory ) ) | |
( tok2 - > str ( ) = = " || " & & conditionIsTrue ( tok2 - > astOperand1 ( ) , programMemory ) ) ) {
// Skip second expression..
const Token * parent = tok2 ;
while ( parent & & parent - > str ( ) = = tok2 - > str ( ) )
parent = parent - > astParent ( ) ;
2014-08-13 04:03:17 +02:00
// Jump to end of condition
2014-08-13 05:36:17 +02:00
if ( parent & & parent - > str ( ) = = " ( " ) {
2014-08-01 16:12:57 +02:00
tok2 = parent - > link ( ) ;
2014-08-13 05:36:17 +02:00
// cast
if ( Token : : simpleMatch ( tok2 , " ) ( " ) )
tok2 = tok2 - > linkAt ( 1 ) ;
}
2014-08-01 16:12:57 +02:00
}
}
2015-03-08 18:18:09 +01:00
if ( ( tok2 - > str ( ) = = " && " & & conditionIsFalse ( tok2 - > astOperand1 ( ) , getProgramMemory ( tok2 - > astTop ( ) , varid , ValueFlow : : Value ( value ) ) ) ) | |
( tok2 - > str ( ) = = " || " & & conditionIsTrue ( tok2 - > astOperand1 ( ) , getProgramMemory ( tok2 - > astTop ( ) , varid , ValueFlow : : Value ( value ) ) ) ) )
2014-08-01 16:12:57 +02:00
break ;
2014-05-19 21:44:00 +02:00
else if ( Token : : simpleMatch ( tok2 , " ) { " ) && Token::findmatch(tok2->link(), " % varid % " , tok2, varid)) {
2014-03-25 21:40:36 +01:00
if ( Token : : findmatch ( tok2 , " continue|break|return " , tok2 - > linkAt ( 1 ) , varid ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " For loop variable bailout on conditional continue|break|return " ) ;
break ;
}
2014-03-17 18:43:47 +01:00
if ( settings - > debugwarnings )
2014-03-25 21:40:36 +01:00
bailout ( tokenlist , errorLogger , tok2 , " For loop variable skipping conditional scope " ) ;
tok2 = tok2 - > next ( ) - > link ( ) ;
2014-05-19 21:44:00 +02:00
if ( Token : : simpleMatch ( tok2 , " } else { " ) ) {
2014-03-25 21:40:36 +01:00
if ( Token : : findmatch ( tok2 , " continue|break|return " , tok2 - > linkAt ( 2 ) , varid ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " For loop variable bailout on conditional continue|break|return " ) ;
break ;
}
tok2 = tok2 - > linkAt ( 2 ) ;
}
2014-03-17 18:43:47 +01:00
}
}
}
2014-08-17 10:40:22 +02:00
static void valueFlowForLoopSimplifyAfter ( Token * fortok , unsigned int varid , const MathLib : : bigint num , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
const Token * vartok = nullptr ;
for ( const Token * tok = fortok ; tok ; tok = tok - > next ( ) ) {
if ( tok - > varId ( ) = = varid ) {
vartok = tok ;
break ;
}
}
if ( ! vartok | | ! vartok - > variable ( ) )
return ;
const Variable * var = vartok - > variable ( ) ;
const Token * endToken = nullptr ;
if ( var - > isLocal ( ) )
endToken = var - > typeStartToken ( ) - > scope ( ) - > classEnd ;
else
endToken = fortok - > scope ( ) - > classEnd ;
std : : list < ValueFlow : : Value > values ;
2015-03-08 18:18:09 +01:00
values . push_back ( ValueFlow : : Value ( num ) ) ;
2014-08-17 10:40:22 +02:00
2015-01-05 14:00:12 +01:00
valueFlowForward ( fortok - > linkAt ( 1 ) - > linkAt ( 1 ) - > next ( ) ,
2014-08-17 10:40:22 +02:00
endToken ,
var ,
varid ,
values ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
}
2015-02-01 12:10:20 +01:00
static void valueFlowForLoop ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-03-17 18:43:47 +01:00
{
2015-02-01 12:10:20 +01:00
for ( std : : list < Scope > : : const_iterator scope = symboldatabase - > scopeList . begin ( ) ; scope ! = symboldatabase - > scopeList . end ( ) ; + + scope ) {
if ( scope - > type ! = Scope : : eFor )
2014-03-17 18:43:47 +01:00
continue ;
2014-03-16 08:38:52 +01:00
2015-02-01 12:10:20 +01:00
Token * tok = const_cast < Token * > ( scope - > classDef ) ;
Token * const bodyStart = const_cast < Token * > ( scope - > classStart ) ;
2014-03-16 08:38:52 +01:00
2015-02-01 12:26:46 +01:00
if ( ! Token : : simpleMatch ( tok - > next ( ) - > astOperand2 ( ) , " ; " ) | |
! Token : : simpleMatch ( tok - > next ( ) - > astOperand2 ( ) - > astOperand2 ( ) , " ; " ) )
continue ;
2014-03-30 10:22:06 +02:00
unsigned int varid ( 0 ) ;
2014-08-17 10:40:22 +02:00
MathLib : : bigint num1 ( 0 ) , num2 ( 0 ) , numAfter ( 0 ) ;
2014-03-16 08:38:52 +01:00
2014-08-17 10:40:22 +02:00
if ( valueFlowForLoop1 ( tok , & varid , & num1 , & num2 , & numAfter ) ) {
2014-12-30 18:50:22 +01:00
if ( num1 < = num2 ) {
valueFlowForLoopSimplify ( bodyStart , varid , num1 , tokenlist , errorLogger , settings ) ;
valueFlowForLoopSimplify ( bodyStart , varid , num2 , tokenlist , errorLogger , settings ) ;
valueFlowForLoopSimplifyAfter ( tok , varid , numAfter , tokenlist , errorLogger , settings ) ;
} else
valueFlowForLoopSimplifyAfter ( tok , varid , num1 , tokenlist , errorLogger , settings ) ;
2014-03-17 18:43:47 +01:00
} else {
2015-09-12 19:49:02 +02:00
ProgramMemory mem1 , mem2 , memAfter ;
2014-08-17 10:40:22 +02:00
if ( valueFlowForLoop2 ( tok , & mem1 , & mem2 , & memAfter ) ) {
2014-03-17 18:43:47 +01:00
std : : map < unsigned int , MathLib : : bigint > : : const_iterator it ;
2015-09-12 19:49:02 +02:00
for ( it = mem1 . intvalues . begin ( ) ; it ! = mem1 . intvalues . end ( ) ; + + it )
2014-03-17 18:43:47 +01:00
valueFlowForLoopSimplify ( bodyStart , it - > first , it - > second , tokenlist , errorLogger , settings ) ;
2015-09-12 19:49:02 +02:00
for ( it = mem2 . intvalues . begin ( ) ; it ! = mem2 . intvalues . end ( ) ; + + it )
2014-03-17 18:43:47 +01:00
valueFlowForLoopSimplify ( bodyStart , it - > first , it - > second , tokenlist , errorLogger , settings ) ;
2015-09-12 19:49:02 +02:00
for ( it = memAfter . intvalues . begin ( ) ; it ! = memAfter . intvalues . end ( ) ; + + it )
2014-08-17 10:40:22 +02:00
valueFlowForLoopSimplifyAfter ( tok , it - > first , it - > second , tokenlist , errorLogger , settings ) ;
2014-01-07 19:20:56 +01:00
}
}
}
}
2015-02-01 15:05:00 +01:00
static void valueFlowInjectParameter ( TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings , const Variable * arg , const Scope * functionScope , const std : : list < ValueFlow : : Value > & argvalues )
{
// Is argument passed by value or const reference, and is it a known non-class type?
if ( arg - > isReference ( ) & & ! arg - > isConst ( ) & & ! arg - > isClass ( ) )
return ;
// Set value in function scope..
const unsigned int varid2 = arg - > declarationId ( ) ;
if ( ! varid2 )
return ;
valueFlowForward ( const_cast < Token * > ( functionScope - > classStart - > next ( ) ) , functionScope - > classEnd , arg , varid2 , argvalues , true , tokenlist , errorLogger , settings ) ;
}
2015-07-20 19:45:38 +02:00
static void valueFlowSwitchVariable ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
{
for ( std : : list < Scope > : : iterator scope = symboldatabase - > scopeList . begin ( ) ; scope ! = symboldatabase - > scopeList . end ( ) ; + + scope ) {
if ( scope - > type ! = Scope : : ScopeType : : eSwitch )
continue ;
if ( ! Token : : Match ( scope - > classDef , " switch ( %var% ) { " ) )
continue ;
const Token * vartok = scope - > classDef - > tokAt ( 2 ) ;
2015-08-12 09:57:36 +02:00
const Variable * var = vartok - > variable ( ) ;
if ( ! var )
2015-07-20 19:45:38 +02:00
continue ;
// bailout: global non-const variables
if ( ! ( var - > isLocal ( ) | | var - > isArgument ( ) ) & & ! var - > isConst ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , vartok , " switch variable " + var - > name ( ) + " is global " ) ;
continue ;
}
for ( Token * tok = scope - > classStart - > next ( ) ; tok ! = scope - > classEnd ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) = = " { " ) {
tok = tok - > link ( ) ;
continue ;
}
if ( Token : : Match ( tok , " case %num% : " ) ) {
std : : list < ValueFlow : : Value > values ;
values . push_back ( ValueFlow : : Value ( MathLib : : toLongNumber ( tok - > next ( ) - > str ( ) ) ) ) ;
2015-07-29 19:54:57 +02:00
values . back ( ) . condition = tok ;
2015-07-27 14:57:02 +02:00
while ( Token : : Match ( tok - > tokAt ( 3 ) , " ;| case %num% : " ) ) {
2015-07-20 19:45:38 +02:00
tok = tok - > tokAt ( 3 ) ;
2015-07-27 14:57:02 +02:00
if ( ! tok - > isName ( ) )
tok = tok - > next ( ) ;
2015-07-20 19:45:38 +02:00
values . push_back ( ValueFlow : : Value ( MathLib : : toLongNumber ( tok - > next ( ) - > str ( ) ) ) ) ;
2015-07-29 19:54:57 +02:00
values . back ( ) . condition = tok ;
2015-07-20 19:45:38 +02:00
}
for ( std : : list < ValueFlow : : Value > : : const_iterator val = values . begin ( ) ; val ! = values . end ( ) ; + + val ) {
valueFlowReverse ( tokenlist ,
const_cast < Token * > ( scope - > classDef ) ,
vartok ,
* val ,
ValueFlow : : Value ( ) ,
errorLogger ,
settings ) ;
}
valueFlowForward ( tok , vartok - > variable ( ) - > scope ( ) - > classEnd , vartok - > variable ( ) , vartok - > varId ( ) , values , false , tokenlist , errorLogger , settings ) ;
}
}
}
}
2014-01-06 16:37:52 +01:00
static void valueFlowSubFunction ( TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2015-01-31 10:50:39 +01:00
if ( ! Token : : Match ( tok , " %name% ( " ) )
2014-01-08 16:17:47 +01:00
continue ;
2014-08-06 06:33:06 +02:00
const Function * const currentFunction = tok - > function ( ) ;
if ( ! currentFunction )
2014-01-06 16:37:52 +01:00
continue ;
// Function scope..
2014-08-06 06:33:06 +02:00
const Scope * const functionScope = currentFunction - > functionScope ;
2014-01-06 16:37:52 +01:00
if ( ! functionScope )
continue ;
2014-08-06 06:33:06 +02:00
unsigned int argnr = 0U ;
for ( const Token * argtok = tok - > tokAt ( 2 ) ; argtok ; argtok = argtok - > nextArgument ( ) ) {
// Get function argument
const Variable * const arg = currentFunction - > getArgumentVar ( argnr + + ) ;
if ( ! arg )
2014-01-08 06:04:51 +01:00
break ;
2014-08-06 06:33:06 +02:00
std : : list < ValueFlow : : Value > argvalues ;
// passing value(s) to function
2015-01-31 10:50:39 +01:00
if ( ! argtok - > values . empty ( ) & & Token : : Match ( argtok , " %name%|%num%|%str% [,)] " ) )
2014-08-06 06:33:06 +02:00
argvalues = argtok - > values ;
else {
// bool operator => values 1/0 are passed to function..
const Token * op = argtok ;
while ( op & & op - > astParent ( ) & & ! Token : : Match ( op - > astParent ( ) , " [(,] " ) )
op = op - > astParent ( ) ;
if ( Token : : Match ( op , " %comp%|%oror%|&&|! " ) ) {
argvalues . clear ( ) ;
argvalues . push_back ( ValueFlow : : Value ( 0 ) ) ;
argvalues . push_back ( ValueFlow : : Value ( 1 ) ) ;
} else if ( Token : : Match ( op , " %cop% " ) & & ! op - > values . empty ( ) ) {
argvalues = op - > values ;
} else {
// possible values are unknown..
continue ;
}
}
2015-07-16 20:49:14 +02:00
// passed values are not "known"..
for ( std : : list < ValueFlow : : Value > : : iterator it = argvalues . begin ( ) ; it ! = argvalues . end ( ) ; + + it ) {
2015-07-25 19:36:29 +02:00
it - > changeKnownToPossible ( ) ;
2015-07-16 20:49:14 +02:00
}
2015-02-01 15:05:00 +01:00
valueFlowInjectParameter ( tokenlist , errorLogger , settings , arg , functionScope , argvalues ) ;
}
}
}
2014-08-06 06:33:06 +02:00
2015-02-01 15:05:00 +01:00
static void valueFlowFunctionDefaultParameter ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
{
if ( ! tokenlist - > isCPP ( ) )
return ;
2014-08-06 06:33:06 +02:00
2015-02-01 15:05:00 +01:00
const std : : size_t functions = symboldatabase - > functionScopes . size ( ) ;
for ( std : : size_t i = 0 ; i < functions ; + + i ) {
const Scope * scope = symboldatabase - > functionScopes [ i ] ;
const Function * function = scope - > function ;
if ( ! function )
continue ;
for ( std : : size_t arg = function - > minArgCount ( ) ; arg < function - > argCount ( ) ; arg + + ) {
const Variable * var = function - > getArgumentVar ( arg ) ;
if ( var & & var - > hasDefault ( ) & & Token : : Match ( var - > nameToken ( ) , " %var% = %num%|%str% [,)] " ) ) {
2015-07-17 16:05:07 +02:00
const std : : list < ValueFlow : : Value > & values = var - > nameToken ( ) - > tokAt ( 2 ) - > values ;
std : : list < ValueFlow : : Value > argvalues ;
for ( std : : list < ValueFlow : : Value > : : const_iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
ValueFlow : : Value v ( * it ) ;
v . defaultArg = true ;
2015-07-25 19:36:29 +02:00
v . changeKnownToPossible ( ) ;
if ( v . isPossible ( ) )
2015-07-17 16:05:07 +02:00
argvalues . push_back ( v ) ;
}
if ( ! argvalues . empty ( ) )
valueFlowInjectParameter ( tokenlist , errorLogger , settings , var , scope , argvalues ) ;
2014-01-06 16:37:52 +01:00
}
}
}
}
2014-06-29 18:04:38 +02:00
static bool constval ( const Token * tok )
{
return tok & & tok - > values . size ( ) = = 1U & & tok - > values . front ( ) . varId = = 0U ;
}
static void valueFlowFunctionReturn ( TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) ! = " ( " | | ! tok - > astOperand1 ( ) | | ! tok - > astOperand1 ( ) - > function ( ) )
continue ;
// Arguments..
std : : vector < MathLib : : bigint > parvalues ;
{
const Token * partok = tok - > astOperand2 ( ) ;
while ( partok & & partok - > str ( ) = = " , " & & constval ( partok - > astOperand2 ( ) ) )
partok = partok - > astOperand1 ( ) ;
if ( ! constval ( partok ) )
continue ;
parvalues . push_back ( partok - > values . front ( ) . intvalue ) ;
partok = partok - > astParent ( ) ;
while ( partok & & partok - > str ( ) = = " , " ) {
parvalues . push_back ( partok - > astOperand2 ( ) - > values . front ( ) . intvalue ) ;
partok = partok - > astParent ( ) ;
}
if ( partok ! = tok )
continue ;
}
2015-02-01 12:33:53 +01:00
// Get scope and args of function
const Function * const function = tok - > astOperand1 ( ) - > function ( ) ;
const Scope * const functionScope = function - > functionScope ;
if ( ! functionScope | | ! Token : : simpleMatch ( functionScope - > classStart , " { return " ) ) {
if ( functionScope & & settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok , " function return; nontrivial function body " ) ;
continue ;
}
2015-09-12 19:49:02 +02:00
ProgramMemory programMemory ;
2014-07-07 21:25:30 +02:00
for ( std : : size_t i = 0 ; i < parvalues . size ( ) ; + + i ) {
2014-06-29 18:04:38 +02:00
const Variable * const arg = function - > getArgumentVar ( i ) ;
2015-01-31 10:50:39 +01:00
if ( ! arg | | ! Token : : Match ( arg - > typeStartToken ( ) , " %type% %name% ,|) " ) ) {
2014-06-29 18:04:38 +02:00
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok , " function return; unhandled argument type " ) ;
programMemory . clear ( ) ;
break ;
}
2015-09-12 19:49:02 +02:00
programMemory . setIntValue ( arg - > declarationId ( ) , parvalues [ i ] ) ;
2014-06-29 18:04:38 +02:00
}
if ( programMemory . empty ( ) )
continue ;
// Determine return value of subfunction..
MathLib : : bigint result = 0 ;
bool error = false ;
execute ( functionScope - > classStart - > next ( ) - > astOperand1 ( ) ,
& programMemory ,
& result ,
& error ) ;
if ( ! error )
setTokenValue ( tok , ValueFlow : : Value ( result ) ) ;
}
}
2015-02-01 12:10:20 +01:00
void ValueFlow : : setValues ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-01-04 20:57:02 +01:00
{
2014-01-05 20:06:46 +01:00
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) )
2014-01-04 20:57:02 +01:00
tok - > values . clear ( ) ;
2014-01-18 19:30:44 +01:00
valueFlowNumber ( tokenlist ) ;
2014-08-03 20:11:22 +02:00
valueFlowString ( tokenlist ) ;
2015-02-24 15:57:39 +01:00
valueFlowArray ( tokenlist ) ;
2014-08-03 20:11:22 +02:00
valueFlowPointerAlias ( tokenlist ) ;
2014-06-29 18:04:38 +02:00
valueFlowFunctionReturn ( tokenlist , errorLogger , settings ) ;
2014-04-14 06:45:39 +02:00
valueFlowBitAnd ( tokenlist ) ;
2015-02-01 12:10:20 +01:00
valueFlowForLoop ( tokenlist , symboldatabase , errorLogger , settings ) ;
valueFlowBeforeCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
valueFlowAfterAssign ( tokenlist , symboldatabase , errorLogger , settings ) ;
valueFlowAfterCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
2015-07-20 19:45:38 +02:00
valueFlowSwitchVariable ( tokenlist , symboldatabase , errorLogger , settings ) ;
2014-01-06 16:37:52 +01:00
valueFlowSubFunction ( tokenlist , errorLogger , settings ) ;
2015-02-01 15:05:00 +01:00
valueFlowFunctionDefaultParameter ( tokenlist , symboldatabase , errorLogger , settings ) ;
2014-01-04 20:57:02 +01:00
}
2015-07-29 19:54:57 +02:00
std : : string ValueFlow : : eitherTheConditionIsRedundant ( const Token * condition )
{
if ( ! condition )
return " Either the condition is redundant " ;
if ( condition - > str ( ) = = " case " ) {
std : : string expr ;
for ( const Token * tok = condition ; tok & & tok - > str ( ) ! = " : " ; tok = tok - > next ( ) ) {
expr + = tok - > str ( ) ;
if ( Token : : Match ( tok , " %name%|%num% %name%|%num% " ) )
expr + = ' ' ;
}
return " Either the switch case ' " + expr + " ' is redundant " ;
}
return " Either the condition ' " + condition - > expressionString ( ) + " ' is redundant " ;
}