2014-01-04 20:57:02 +01:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2018-01-14 15:37:52 +01:00
* Copyright ( C ) 2007 - 2018 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/>.
*/
2018-05-05 23:03:04 +02:00
/**
* @ brief This is the ValueFlow component in Cppcheck .
*
2018-05-07 22:06:23 +02:00
* Each @ sa Token in the token list has a list of values . These are
2018-05-05 23:03:04 +02:00
* the " possible " values for the Token at runtime .
*
* In the - - debug and - - debug - normal output you can see the ValueFlow data . For example :
*
* int f ( )
* {
* int x = 10 ;
* return 4 * x + 2 ;
* }
*
* The - - debug - normal output says :
*
* # # Value flow
* Line 3
* 10 always 10
* Line 4
* 4 always 4
* * always 40
* x always 10
* + always 42
* 2 always 2
*
* All value flow analysis is executed in the ValueFlow : : setValues ( ) function . The ValueFlow analysis is executed after the tokenizer / ast / symboldatabase / etc . .
* The ValueFlow analysis is done in a series of valueFlow * function calls , where each such function call can only use results from previous function calls .
* The function calls should be arranged so that valueFlow * that do not require previous ValueFlow information should be first .
*
* Type of analysis
* = = = = = = = = = = = = = = = =
*
* This is " flow sensitive " value flow analysis . We _usually_ track the value for 1 variable at a time .
*
* How are calculations handled
* = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*
* Here is an example code :
*
* x = 3 + 4 ;
*
* The valueFlowNumber set the values for the " 3 " and " 4 " tokens by calling setTokenValue ( ) .
* The setTokenValue ( ) handle the calculations automatically . When both " 3 " and " 4 " have values , the " + " can be calculated . setTokenValue ( ) recursively calls itself when parents in calculations can be calculated .
*
* Forward / Reverse flow analysis
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*
* In forward value flow analysis we know a value and see what happens when we are stepping the program forward . Like normal execution .
* The valueFlowForward is used in this analysis .
*
* In reverse value flow analysis we know the value of a variable at line X . And try to " execute backwards " to determine possible values before line X .
* The valueFlowReverse is used in this analysis .
*
*
*/
2014-01-04 20:57:02 +01:00
# include "valueflow.h"
2017-05-27 04:33:47 +02:00
2015-11-11 13:45:28 +01:00
# include "astutils.h"
2014-01-05 20:06:46 +01:00
# include "errorlogger.h"
2017-05-27 04:33:47 +02:00
# include "library.h"
2014-01-04 20:57:02 +01:00
# include "mathlib.h"
2017-05-27 04:33:47 +02:00
# include "platform.h"
2014-01-05 20:06:46 +01:00
# include "settings.h"
2017-05-27 04:33:47 +02:00
# include "standards.h"
2014-01-05 20:06:46 +01:00
# include "symboldatabase.h"
# include "token.h"
# include "tokenlist.h"
2017-05-23 15:01:56 +02:00
# include "utils.h"
2017-10-05 23:03:13 +02:00
# include "path.h"
2017-05-27 04:33:47 +02:00
# include <algorithm>
# include <cstddef>
2018-08-10 18:05:23 +02:00
# include <functional>
# include <iterator>
2017-05-27 04:33:47 +02:00
# include <limits>
# include <map>
# include <set>
2014-01-19 09:05:48 +01:00
# include <stack>
2017-05-27 04:33:47 +02:00
# include <vector>
2014-01-06 16:37:52 +01:00
2015-09-12 19:49:02 +02:00
namespace {
struct ProgramMemory {
2016-11-14 06:42:52 +01:00
std : : map < unsigned int , ValueFlow : : Value > values ;
2015-09-12 19:49:02 +02:00
void setValue ( unsigned int varid , const ValueFlow : : Value & value ) {
2016-11-14 06:42:52 +01:00
values [ varid ] = value ;
2015-09-12 19:49:02 +02:00
}
bool getIntValue ( unsigned int varid , MathLib : : bigint * result ) const {
2016-11-14 06:42:52 +01:00
const std : : map < unsigned int , ValueFlow : : Value > : : const_iterator it = values . find ( varid ) ;
2017-05-22 15:22:38 +02:00
const bool found = it ! = values . end ( ) & & it - > second . isIntValue ( ) ;
2016-11-14 06:42:52 +01:00
if ( found )
* result = it - > second . intvalue ;
return found ;
2015-09-12 19:49:02 +02:00
}
void setIntValue ( unsigned int varid , MathLib : : bigint value ) {
2016-11-14 06:42:52 +01:00
values [ varid ] = ValueFlow : : Value ( value ) ;
}
bool getTokValue ( unsigned int varid , const Token * * result ) const {
const std : : map < unsigned int , ValueFlow : : Value > : : const_iterator it = values . find ( varid ) ;
2017-05-22 15:22:38 +02:00
const bool found = it ! = values . end ( ) & & it - > second . isTokValue ( ) ;
2016-11-14 06:42:52 +01:00
if ( found )
* result = it - > second . tokvalue ;
return found ;
2015-09-12 19:49:02 +02:00
}
bool hasValue ( unsigned int varid ) {
2016-11-14 06:42:52 +01:00
return values . find ( varid ) ! = values . end ( ) ;
2015-09-12 19:49:02 +02:00
}
void swap ( ProgramMemory & pm ) {
2016-11-14 06:42:52 +01:00
values . swap ( pm . values ) ;
2015-09-12 19:49:02 +02:00
}
void clear ( ) {
2016-11-14 06:42:52 +01:00
values . clear ( ) ;
2015-09-12 19:49:02 +02:00
}
bool empty ( ) const {
2016-11-14 06:42:52 +01:00
return values . empty ( ) ;
2015-09-12 19:49:02 +02:00
}
} ;
}
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 ) ;
2017-10-05 23:03:13 +02:00
static void bailoutInternal ( TokenList * tokenlist , ErrorLogger * errorLogger , const Token * tok , const std : : string & what , const std : : string & file , int line , const std : : string & function )
2014-01-06 07:44:58 +01:00
{
2018-04-09 09:54:39 +02:00
std : : list < ErrorLogger : : ErrorMessage : : FileLocation > callstack ( 1 , ErrorLogger : : ErrorMessage : : FileLocation ( tok , tokenlist ) ) ;
2017-10-05 23:03:13 +02:00
ErrorLogger : : ErrorMessage errmsg ( callstack , tokenlist - > getSourceFilePath ( ) , Severity : : debug ,
2017-10-05 23:01:42 +02:00
Path : : stripDirectoryPart ( file ) + " : " + MathLib : : toString ( line ) + " : " + function + " bailout: " + what , " valueFlowBailout " , false ) ;
2014-01-06 07:44:58 +01:00
errorLogger - > reportErr ( errmsg ) ;
}
2017-10-05 23:03:13 +02:00
# if (defined __cplusplus) && __cplusplus >= 201103L
# define bailout(tokenlist, errorLogger, tok, what) bailoutInternal(tokenlist, errorLogger, tok, what, __FILE__, __LINE__, __func__)
# elif (defined __GNUC__) || (defined __clang__) || (defined _MSC_VER)
# define bailout(tokenlist, errorLogger, tok, what) bailoutInternal(tokenlist, errorLogger, tok, what, __FILE__, __LINE__, __FUNCTION__)
# else
# define bailout(tokenlist, errorLogger, tok, what) bailoutInternal(tokenlist, errorLogger, tok, what, __FILE__, __LINE__, "(valueFlow)")
# endif
2017-11-17 23:04:54 +01:00
static void changeKnownToPossible ( std : : list < ValueFlow : : Value > & values )
{
std : : list < ValueFlow : : Value > : : iterator it ;
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it )
it - > changeKnownToPossible ( ) ;
}
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 ( ) = = " && " ) {
2017-10-08 07:54:39 +02:00
return conditionIsFalse ( condition - > astOperand1 ( ) , programMemory ) | |
2017-10-08 14:16:45 +02:00
conditionIsFalse ( condition - > astOperand2 ( ) , programMemory ) ;
2014-05-01 15:15:26 +02:00
}
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 ( ) = = " || " ) {
2017-10-08 07:54:39 +02:00
return conditionIsTrue ( condition - > astOperand1 ( ) , programMemory ) | |
2017-10-08 14:16:45 +02:00
conditionIsTrue ( condition - > astOperand2 ( ) , programMemory ) ;
2014-06-25 16:00:56 +02:00
}
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 ) ;
2017-08-27 19:50:44 +02:00
} else if ( Token : : Match ( tok2 , " [;{}] %var% = " ) | |
Token : : Match ( tok2 , " [;{}] const| %type% %var% ( " ) ) {
2015-05-24 17:02:00 +02:00
const Token * vartok = tok2 - > next ( ) ;
2017-08-27 19:50:44 +02:00
while ( vartok - > next ( ) - > isName ( ) )
vartok = vartok - > 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 ;
2017-08-27 19:50:44 +02:00
execute ( vartok - > next ( ) - > astOperand2 ( ) , & programMemory , & result , & error ) ;
2015-05-24 17:02:00 +02:00
if ( ! error )
2015-09-12 19:49:02 +02:00
programMemory . setIntValue ( vartok - > varId ( ) , result ) ;
2015-05-24 17:02:00 +02:00
}
}
2017-08-27 19:50:44 +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 ;
}
2016-12-20 19:32:21 +01:00
static ValueFlow : : Value castValue ( ValueFlow : : Value value , const ValueType : : Sign sign , unsigned int bit )
{
if ( value . isFloatValue ( ) ) {
value . valueType = ValueFlow : : Value : : INT ;
2017-11-03 14:05:23 +01:00
if ( value . floatValue > = std : : numeric_limits < int > : : min ( ) & & value . floatValue < = std : : numeric_limits < int > : : max ( ) ) {
value . intvalue = value . floatValue ;
} else { // don't perform UB
value . intvalue = 0 ;
}
2016-12-20 19:32:21 +01:00
}
2017-10-20 17:31:58 +02:00
if ( bit < MathLib : : bigint_bits ) {
2017-10-21 22:08:34 +02:00
const MathLib : : biguint one = 1 ;
value . intvalue & = ( one < < bit ) - 1 ;
if ( sign = = ValueType : : Sign : : SIGNED & & value . intvalue & ( one < < ( bit - 1 ) ) ) {
value . intvalue | = ~ ( ( one < < bit ) - 1ULL ) ;
2016-12-20 19:32:21 +01:00
}
}
return value ;
}
2015-07-17 09:46:31 +02:00
/** set ValueFlow value and perform calculations if possible */
2016-11-05 09:59:48 +01:00
static void setTokenValue ( Token * tok , const ValueFlow : : Value & value , const Settings * settings )
2015-07-17 09:46:31 +02:00
{
2017-03-27 18:48:34 +02:00
if ( ! tok - > addValue ( value ) )
2015-07-17 09:46:31 +02:00
return ;
2017-04-23 21:58:01 +02:00
// Don't set parent for uninitialized values
2017-04-23 21:59:58 +02:00
if ( value . isUninitValue ( ) )
2017-04-23 21:58:01 +02:00
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
2016-12-20 23:09:50 +01:00
if ( parent - > str ( ) = = " ( " & & ! parent - > astOperand2 ( ) & & Token : : Match ( parent , " ( %name% " ) ) {
2016-12-20 19:32:21 +01:00
const ValueType & valueType = ValueType : : parseDecl ( parent - > next ( ) , settings ) ;
if ( valueType . pointer )
setTokenValue ( parent , value , settings ) ;
else if ( valueType . type = = ValueType : : Type : : CHAR )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > char_bit ) , settings ) ;
else if ( valueType . type = = ValueType : : Type : : SHORT )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > short_bit ) , settings ) ;
else if ( valueType . type = = ValueType : : Type : : INT )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > int_bit ) , settings ) ;
else if ( valueType . type = = ValueType : : Type : : LONG )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > long_bit ) , settings ) ;
else if ( valueType . type = = ValueType : : Type : : LONGLONG )
setTokenValue ( parent , castValue ( value , valueType . sign , settings - > long_long_bit ) , settings ) ;
2017-08-14 17:07:36 +02:00
else if ( value . isIntValue ( ) ) {
2017-10-18 17:30:47 +02:00
const long long charMax = settings - > signedCharMax ( ) ;
const long long charMin = settings - > signedCharMin ( ) ;
2017-08-14 17:07:36 +02:00
if ( charMin < = value . intvalue & & value . intvalue < = charMax ) {
// unknown type, but value is small so there should be no truncation etc
setTokenValue ( parent , value , settings ) ;
}
}
2016-12-20 19:32:21 +01:00
}
2014-01-20 22:26:55 +01:00
2016-12-20 19:32:21 +01:00
else if ( parent - > str ( ) = = " : " ) {
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , value , settings ) ;
2015-07-02 20:11:27 +02:00
}
2016-08-21 11:06:48 +02:00
else if ( parent - > str ( ) = = " ? " & & tok - > str ( ) = = " : " & & tok = = parent - > astOperand2 ( ) & & parent - > astOperand1 ( ) ) {
2016-08-15 14:19:35 +02:00
// is condition always true/false?
2018-07-14 13:09:53 +02:00
if ( parent - > astOperand1 ( ) - > hasKnownValue ( ) ) {
2017-03-27 18:48:34 +02:00
const ValueFlow : : Value & condvalue = parent - > astOperand1 ( ) - > values ( ) . front ( ) ;
2016-11-13 22:33:39 +01:00
const bool cond ( condvalue . isTokValue ( ) | | ( condvalue . isIntValue ( ) & & condvalue . intvalue ! = 0 ) ) ;
2016-08-17 18:44:41 +02:00
if ( cond & & ! tok - > astOperand1 ( ) ) { // true condition, no second operator
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , condvalue , settings ) ;
2016-08-17 18:44:41 +02:00
} else {
const Token * op = cond ? tok - > astOperand1 ( ) : tok - > astOperand2 ( ) ;
2016-11-06 14:20:58 +01:00
if ( ! op ) // #7769 segmentation fault at setTokenValue()
return ;
2017-03-27 18:48:34 +02:00
const std : : list < ValueFlow : : Value > & values = op - > values ( ) ;
2016-08-17 18:44:41 +02:00
if ( std : : find ( values . begin ( ) , values . end ( ) , value ) ! = values . end ( ) )
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , value , settings ) ;
2016-08-17 18:44:41 +02:00
}
2016-08-15 14:19:35 +02:00
} else {
// is condition only depending on 1 variable?
std : : stack < const Token * > tokens ;
tokens . push ( parent - > astOperand1 ( ) ) ;
unsigned int varId = 0 ;
while ( ! tokens . empty ( ) ) {
const Token * t = tokens . top ( ) ;
tokens . pop ( ) ;
if ( ! t )
continue ;
tokens . push ( t - > astOperand1 ( ) ) ;
tokens . push ( t - > astOperand2 ( ) ) ;
if ( t - > varId ( ) ) {
if ( varId > 0 | | value . varId ! = 0U )
return ;
varId = t - > varId ( ) ;
} else if ( t - > str ( ) = = " ( " & & Token : : Match ( t - > previous ( ) , " %name% " ) )
return ; // function call
}
2015-07-02 20:11:27 +02:00
2016-08-15 14:19:35 +02:00
ValueFlow : : Value v ( value ) ;
v . conditional = true ;
v . changeKnownToPossible ( ) ;
2015-07-17 15:22:24 +02:00
2016-08-15 14:19:35 +02:00
if ( varId )
v . varId = varId ;
2015-07-02 20:11:27 +02:00
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , v , settings ) ;
2016-08-15 14:19:35 +02:00
}
2015-07-02 20:11:27 +02:00
}
2014-01-20 22:26:55 +01:00
// Calculations..
2016-10-17 13:05:19 +02:00
else if ( ( parent - > isArithmeticalOp ( ) | | parent - > isComparisonOp ( ) | | ( parent - > tokType ( ) = = Token : : eBitOp ) | | ( parent - > tokType ( ) = = Token : : eLogicalOp ) ) & &
2015-07-26 11:27:52 +02:00
parent - > astOperand1 ( ) & &
parent - > astOperand2 ( ) ) {
2018-07-14 13:09:53 +02:00
const bool known = ( parent - > astOperand1 ( ) - > hasKnownValue ( ) | |
parent - > astOperand2 ( ) - > hasKnownValue ( ) ) ;
2016-01-26 10:07:42 +01:00
// known result when a operand is 0.
2016-11-13 22:33:39 +01:00
if ( Token : : Match ( parent , " [&*] " ) & & value . isKnown ( ) & & value . isIntValue ( ) & & value . intvalue = = 0 ) {
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , value , settings ) ;
2016-01-26 10:07:42 +01:00
return ;
}
2018-07-23 08:51:59 +02:00
// known result when a operand is true.
if ( Token : : simpleMatch ( parent , " && " ) & & value . isKnown ( ) & & value . isIntValue ( ) & & value . intvalue = = 0 ) {
setTokenValue ( parent , value , settings ) ;
return ;
}
// known result when a operand is false.
if ( Token : : simpleMatch ( parent , " || " ) & & value . isKnown ( ) & & value . isIntValue ( ) & & value . intvalue ! = 0 ) {
setTokenValue ( parent , value , settings ) ;
return ;
}
2016-01-26 10:07:42 +01:00
2018-04-24 18:02:36 +02:00
for ( const ValueFlow : : Value & value1 : parent - > astOperand1 ( ) - > values ( ) ) {
if ( ! value1 . isIntValue ( ) & & ! value1 . isFloatValue ( ) & & ! value1 . isTokValue ( ) )
2016-11-14 06:42:52 +01:00
continue ;
2018-04-24 18:02:36 +02:00
if ( value1 . isTokValue ( ) & & ( ! parent - > isComparisonOp ( ) | | value1 . tokvalue - > tokType ( ) ! = Token : : eString ) )
2015-08-02 18:12:03 +02:00
continue ;
2018-04-24 18:02:36 +02:00
for ( const ValueFlow : : Value & value2 : parent - > astOperand2 ( ) - > values ( ) ) {
if ( ! value2 . isIntValue ( ) & & ! value2 . isFloatValue ( ) & & ! value2 . isTokValue ( ) )
2016-11-14 06:42:52 +01:00
continue ;
2018-04-24 18:02:36 +02:00
if ( value2 . isTokValue ( ) & & ( ! parent - > isComparisonOp ( ) | | value2 . tokvalue - > tokType ( ) ! = Token : : eString | | value1 . isTokValue ( ) ) )
2015-08-02 18:12:03 +02:00
continue ;
2018-04-24 18:02:36 +02:00
if ( known | | value1 . varId = = 0U | | value2 . varId = = 0U | |
( value1 . varId = = value2 . varId & & value1 . varvalue = = value2 . varvalue & & value1 . isIntValue ( ) & & value2 . isIntValue ( ) ) ) {
2014-01-18 19:30:44 +01:00
ValueFlow : : Value result ( 0 ) ;
2018-04-24 18:02:36 +02:00
result . condition = value1 . condition ? value1 . condition : value2 . condition ;
result . setInconclusive ( value1 . isInconclusive ( ) | value2 . isInconclusive ( ) ) ;
result . varId = ( value1 . varId ! = 0U ) ? value1 . varId : value2 . varId ;
result . varvalue = ( result . varId = = value1 . varId ) ? value1 . varvalue : value2 . varvalue ;
result . errorPath = ( value1 . errorPath . empty ( ) ? value2 : value1 ) . errorPath ;
if ( value1 . valueKind = = value2 . valueKind )
result . valueKind = value1 . valueKind ;
const float floatValue1 = value1 . isIntValue ( ) ? value1 . intvalue : value1 . floatValue ;
const float floatValue2 = value2 . isIntValue ( ) ? value2 . intvalue : value2 . floatValue ;
2014-01-18 19:30:44 +01:00
switch ( parent - > str ( ) [ 0 ] ) {
case ' + ' :
2018-04-24 18:02:36 +02:00
if ( value1 . isTokValue ( ) | | value2 . isTokValue ( ) )
2016-11-14 06:42:52 +01:00
break ;
2018-04-24 18:02:36 +02:00
if ( value1 . isFloatValue ( ) | | value2 . isFloatValue ( ) ) {
2016-11-14 06:42:52 +01:00
result . valueType = ValueFlow : : Value : : FLOAT ;
result . floatValue = floatValue1 + floatValue2 ;
} else {
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue + value2 . intvalue ;
2016-11-14 06:42:52 +01:00
}
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2014-01-18 19:30:44 +01:00
break ;
case ' - ' :
2018-04-24 18:02:36 +02:00
if ( value1 . isTokValue ( ) | | value2 . isTokValue ( ) )
2016-11-14 06:42:52 +01:00
break ;
2018-04-24 18:02:36 +02:00
if ( value1 . isFloatValue ( ) | | value2 . isFloatValue ( ) ) {
2016-11-14 06:42:52 +01:00
result . valueType = ValueFlow : : Value : : FLOAT ;
result . floatValue = floatValue1 - floatValue2 ;
} else {
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue - value2 . intvalue ;
2016-11-14 06:42:52 +01:00
}
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2014-01-18 19:30:44 +01:00
break ;
case ' * ' :
2018-04-24 18:02:36 +02:00
if ( value1 . isTokValue ( ) | | value2 . isTokValue ( ) )
2016-11-14 06:42:52 +01:00
break ;
2018-04-24 18:02:36 +02:00
if ( value1 . isFloatValue ( ) | | value2 . isFloatValue ( ) ) {
2016-11-14 06:42:52 +01:00
result . valueType = ValueFlow : : Value : : FLOAT ;
result . floatValue = floatValue1 * floatValue2 ;
} else {
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue * value2 . intvalue ;
2016-11-14 06:42:52 +01:00
}
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2014-01-18 19:30:44 +01:00
break ;
case ' / ' :
2018-04-24 18:02:36 +02:00
if ( value1 . isTokValue ( ) | | value2 . isTokValue ( ) | | value2 . intvalue = = 0 )
2014-01-18 19:30:44 +01:00
break ;
2018-04-24 18:02:36 +02:00
if ( value1 . isFloatValue ( ) | | value2 . isFloatValue ( ) ) {
2016-11-14 06:42:52 +01:00
result . valueType = ValueFlow : : Value : : FLOAT ;
result . floatValue = floatValue1 / floatValue2 ;
} else {
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue / value2 . intvalue ;
2016-11-14 06:42:52 +01:00
}
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2014-01-18 19:30:44 +01:00
break ;
case ' % ' :
2018-04-24 18:02:36 +02:00
if ( ! value1 . isIntValue ( ) | | ! value2 . isIntValue ( ) )
2016-11-14 06:42:52 +01:00
break ;
2018-04-24 18:02:36 +02:00
if ( value2 . intvalue = = 0 )
2014-01-18 19:30:44 +01:00
break ;
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue % value2 . intvalue ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2014-01-18 19:30:44 +01:00
break ;
2015-07-26 11:27:52 +02:00
case ' = ' :
if ( parent - > str ( ) = = " == " ) {
2018-04-24 18:02:36 +02:00
if ( ( value1 . isIntValue ( ) & & value2 . isTokValue ( ) ) | |
( value1 . isTokValue ( ) & & value2 . isIntValue ( ) ) ) {
2015-08-02 18:12:03 +02:00
result . intvalue = 0 ;
2016-11-13 22:33:39 +01:00
setTokenValue ( parent , result , settings ) ;
2018-04-24 18:02:36 +02:00
} else if ( value1 . isIntValue ( ) & & value2 . isIntValue ( ) ) {
result . intvalue = value1 . intvalue = = value2 . intvalue ;
2016-11-13 22:33:39 +01:00
setTokenValue ( parent , result , settings ) ;
}
2015-07-26 11:27:52 +02:00
}
break ;
case ' ! ' :
if ( parent - > str ( ) = = " != " ) {
2018-04-24 18:02:36 +02:00
if ( ( value1 . isIntValue ( ) & & value2 . isTokValue ( ) ) | |
( value1 . isTokValue ( ) & & value2 . isIntValue ( ) ) ) {
2015-08-02 18:12:03 +02:00
result . intvalue = 1 ;
2016-11-13 22:33:39 +01:00
setTokenValue ( parent , result , settings ) ;
2018-04-24 18:02:36 +02:00
} else if ( value1 . isIntValue ( ) & & value2 . isIntValue ( ) ) {
result . intvalue = value1 . intvalue ! = value2 . intvalue ;
2016-11-13 22:33:39 +01:00
setTokenValue ( parent , result , settings ) ;
}
2015-07-26 11:27:52 +02:00
}
break ;
2016-11-14 06:42:52 +01:00
case ' > ' : {
2018-04-24 18:02:36 +02:00
const bool f = value1 . isFloatValue ( ) | | value2 . isFloatValue ( ) ;
if ( ! f & & ! value1 . isIntValue ( ) & & ! value2 . isIntValue ( ) )
2015-08-02 18:12:03 +02:00
break ;
2015-07-26 11:27:52 +02:00
if ( parent - > str ( ) = = " > " )
2018-04-24 18:02:36 +02:00
result . intvalue = f ? ( floatValue1 > floatValue2 ) : ( value1 . intvalue > value2 . intvalue ) ;
2015-07-26 11:27:52 +02:00
else if ( parent - > str ( ) = = " >= " )
2018-04-24 18:02:36 +02:00
result . intvalue = f ? ( floatValue1 > = floatValue2 ) : ( value1 . intvalue > = value2 . intvalue ) ;
else if ( ! f & & parent - > str ( ) = = " >> " & & value1 . intvalue > = 0 & & value2 . intvalue > = 0 & & value2 . intvalue < MathLib : : bigint_bits )
result . intvalue = value1 . intvalue > > value2 . intvalue ;
2015-07-26 11:27:52 +02:00
else
break ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2015-07-26 11:27:52 +02:00
break ;
2016-11-14 06:42:52 +01:00
}
case ' < ' : {
2018-04-24 18:02:36 +02:00
const bool f = value1 . isFloatValue ( ) | | value2 . isFloatValue ( ) ;
if ( ! f & & ! value1 . isIntValue ( ) & & ! value2 . isIntValue ( ) )
2015-08-02 18:12:03 +02:00
break ;
2015-07-26 11:27:52 +02:00
if ( parent - > str ( ) = = " < " )
2018-04-24 18:02:36 +02:00
result . intvalue = f ? ( floatValue1 < floatValue2 ) : ( value1 . intvalue < value2 . intvalue ) ;
2015-07-26 11:27:52 +02:00
else if ( parent - > str ( ) = = " <= " )
2018-04-24 18:02:36 +02:00
result . intvalue = f ? ( floatValue1 < = floatValue2 ) : ( value1 . intvalue < = value2 . intvalue ) ;
else if ( ! f & & parent - > str ( ) = = " << " & & value1 . intvalue > = 0 & & value2 . intvalue > = 0 & & value2 . intvalue < MathLib : : bigint_bits )
result . intvalue = value1 . intvalue < < value2 . intvalue ;
2015-07-26 11:27:52 +02:00
else
break ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2015-07-26 11:27:52 +02:00
break ;
2016-11-14 06:42:52 +01:00
}
2015-10-14 10:44:04 +02:00
case ' & ' :
2018-04-24 18:02:36 +02:00
if ( ! value1 . isIntValue ( ) | | ! value2 . isIntValue ( ) )
2016-11-13 22:33:39 +01:00
break ;
2016-10-17 13:05:19 +02:00
if ( parent - > str ( ) = = " & " )
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue & value2 . intvalue ;
2016-10-17 13:05:19 +02:00
else
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue & & value2 . intvalue ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2015-10-14 10:44:04 +02:00
break ;
case ' | ' :
2018-04-24 18:02:36 +02:00
if ( ! value1 . isIntValue ( ) | | ! value2 . isIntValue ( ) )
2016-11-13 22:33:39 +01:00
break ;
2016-10-17 13:05:19 +02:00
if ( parent - > str ( ) = = " | " )
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue | value2 . intvalue ;
2016-10-17 13:05:19 +02:00
else
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue | | value2 . intvalue ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2015-10-14 10:44:04 +02:00
break ;
case ' ^ ' :
2018-04-24 18:02:36 +02:00
if ( ! value1 . isIntValue ( ) | | ! value2 . isIntValue ( ) )
2016-11-13 22:33:39 +01:00
break ;
2018-04-24 18:02:36 +02:00
result . intvalue = value1 . intvalue ^ value2 . intvalue ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2015-10-14 10:44:04 +02:00
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 ( ) = = " ! " ) {
2018-07-14 13:19:41 +02:00
for ( const ValueFlow : : Value & val : tok - > values ( ) ) {
if ( ! val . isIntValue ( ) )
2015-07-16 21:17:44 +02:00
continue ;
2018-07-14 13:19:41 +02:00
ValueFlow : : Value v ( val ) ;
2015-07-16 21:17:44 +02:00
v . intvalue = ! v . intvalue ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , v , settings ) ;
}
}
// ~
else if ( parent - > str ( ) = = " ~ " ) {
2018-07-14 13:19:41 +02:00
for ( const ValueFlow : : Value & val : tok - > values ( ) ) {
if ( ! val . isIntValue ( ) )
2016-11-05 09:29:22 +01:00
continue ;
2018-07-14 13:19:41 +02:00
ValueFlow : : Value v ( val ) ;
2016-11-05 09:29:22 +01:00
v . intvalue = ~ v . intvalue ;
unsigned int bits = 0 ;
2016-11-05 09:59:48 +01:00
if ( settings & &
tok - > valueType ( ) & &
2016-11-05 09:29:22 +01:00
tok - > valueType ( ) - > sign = = ValueType : : Sign : : UNSIGNED & &
tok - > valueType ( ) - > pointer = = 0 ) {
if ( tok - > valueType ( ) - > type = = ValueType : : Type : : INT )
bits = settings - > int_bit ;
else if ( tok - > valueType ( ) - > type = = ValueType : : Type : : LONG )
bits = settings - > long_bit ;
}
2017-10-20 17:31:58 +02:00
if ( bits > 0 & & bits < MathLib : : bigint_bits )
v . intvalue & = ( ( ( MathLib : : biguint ) 1 ) < < bits ) - 1 ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , v , settings ) ;
2015-07-16 21:17:44 +02:00
}
}
2016-08-14 22:19:06 +02:00
// unary minus
2018-07-14 13:19:41 +02:00
else if ( parent - > isUnaryOp ( " - " ) ) {
for ( const ValueFlow : : Value & val : tok - > values ( ) ) {
if ( ! val . isIntValue ( ) & & ! val . isFloatValue ( ) )
2016-08-14 22:19:06 +02:00
continue ;
2018-07-14 13:19:41 +02:00
ValueFlow : : Value v ( val ) ;
2016-11-14 06:42:52 +01:00
if ( v . isIntValue ( ) )
v . intvalue = - v . intvalue ;
else
v . floatValue = - v . floatValue ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , v , settings ) ;
2016-08-14 22:19:06 +02:00
}
}
2015-02-24 15:57:39 +01:00
// Array element
2018-07-14 13:19:41 +02:00
else if ( parent - > str ( ) = = " [ " & & parent - > isBinaryOp ( ) ) {
for ( const ValueFlow : : Value & value1 : parent - > astOperand1 ( ) - > values ( ) ) {
if ( ! value1 . isTokValue ( ) )
2015-02-24 15:57:39 +01:00
continue ;
2018-07-14 13:19:41 +02:00
for ( const ValueFlow : : Value & value2 : parent - > astOperand2 ( ) - > values ( ) ) {
if ( ! value2 . isIntValue ( ) )
2015-02-24 15:57:39 +01:00
continue ;
2018-07-14 13:19:41 +02:00
if ( value1 . varId = = 0U | | value2 . varId = = 0U | |
( value1 . varId = = value2 . varId & & value1 . varvalue = = value2 . varvalue ) ) {
2015-02-24 15:57:39 +01:00
ValueFlow : : Value result ( 0 ) ;
2018-07-14 13:19:41 +02:00
result . condition = value1 . condition ? value1 . condition : value2 . condition ;
result . setInconclusive ( value1 . isInconclusive ( ) | value2 . isInconclusive ( ) ) ;
result . varId = ( value1 . varId ! = 0U ) ? value1 . varId : value2 . varId ;
result . varvalue = ( result . varId = = value1 . varId ) ? value1 . intvalue : value2 . intvalue ;
if ( value1 . valueKind = = value2 . valueKind )
result . valueKind = value1 . valueKind ;
if ( value1 . tokvalue - > tokType ( ) = = Token : : eString ) {
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 ;
2016-11-05 09:59:48 +01:00
setTokenValue ( parent , result , settings ) ;
2015-11-12 16:14:27 +01:00
} else if ( index > = 0 & & index < s . size ( ) ) {
2015-02-24 15:57:39 +01:00
result . intvalue = s [ index ] ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2015-02-24 15:57:39 +01:00
}
2018-07-14 13:19:41 +02:00
} else if ( value1 . tokvalue - > str ( ) = = " { " ) {
MathLib : : bigint index = value2 . intvalue ;
const Token * element = value1 . tokvalue - > next ( ) ;
2015-02-24 15:57:39 +01:00
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 ( ) ) ;
2016-11-05 09:29:22 +01:00
setTokenValue ( parent , result , settings ) ;
2015-02-24 15:57:39 +01:00
}
}
}
}
}
}
2014-01-18 19:30:44 +01:00
}
2017-10-19 18:01:41 +02:00
static unsigned int getSizeOfType ( const Token * typeTok , const Settings * settings )
2017-10-18 23:20:04 +02:00
{
const std : : string & typeStr = typeTok - > str ( ) ;
if ( typeStr = = " char " )
return 1 ;
2017-10-18 23:25:21 +02:00
else if ( typeStr = = " short " )
2017-10-18 23:20:04 +02:00
return settings - > sizeof_short ;
else if ( typeStr = = " int " )
return settings - > sizeof_int ;
else if ( typeStr = = " long " )
return typeTok - > isLong ( ) ? settings - > sizeof_long_long : settings - > sizeof_long ;
2018-03-23 08:28:12 +01:00
else if ( typeStr = = " wchar_t " )
return settings - > sizeof_wchar_t ;
2017-10-18 23:20:04 +02:00
else
return 0 ;
}
2016-05-07 20:18:07 +02:00
// Handle various constants..
2016-10-18 21:44:02 +02:00
static Token * valueFlowSetConstantValue ( const Token * tok , const Settings * settings , bool cpp )
2016-05-07 20:18:07 +02:00
{
if ( ( tok - > isNumber ( ) & & MathLib : : isInt ( tok - > str ( ) ) ) | | ( tok - > tokType ( ) = = Token : : eChar ) ) {
ValueFlow : : Value value ( MathLib : : toLongNumber ( tok - > str ( ) ) ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2016-11-05 09:29:22 +01:00
setTokenValue ( const_cast < Token * > ( tok ) , value , settings ) ;
2016-11-13 22:59:56 +01:00
} else if ( tok - > isNumber ( ) & & MathLib : : isFloat ( tok - > str ( ) ) ) {
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : FLOAT ;
value . floatValue = MathLib : : toDoubleNumber ( tok - > str ( ) ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2016-11-13 22:59:56 +01:00
setTokenValue ( const_cast < Token * > ( tok ) , value , settings ) ;
2016-05-26 11:25:50 +02:00
} else if ( tok - > enumerator ( ) & & tok - > enumerator ( ) - > value_known ) {
2016-05-07 20:18:07 +02:00
ValueFlow : : Value value ( tok - > enumerator ( ) - > value ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2016-11-05 09:29:22 +01:00
setTokenValue ( const_cast < Token * > ( tok ) , value , settings ) ;
2016-10-18 21:44:02 +02:00
} else if ( tok - > str ( ) = = " NULL " | | ( cpp & & tok - > str ( ) = = " nullptr " ) ) {
ValueFlow : : Value value ( 0 ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2016-11-05 09:29:22 +01:00
setTokenValue ( const_cast < Token * > ( tok ) , value , settings ) ;
2016-12-18 14:03:48 +01:00
} else if ( Token : : simpleMatch ( tok , " sizeof ( " ) ) {
2016-07-08 20:39:34 +02:00
const Token * tok2 = tok - > tokAt ( 2 ) ;
2018-03-23 08:28:12 +01:00
// skip over tokens to find variable or type
while ( Token : : Match ( tok2 , " %name% ::|.|[ " ) ) {
if ( tok2 - > next ( ) - > str ( ) = = " [ " )
tok2 = tok2 - > linkAt ( 1 ) - > next ( ) ;
else
tok2 = tok2 - > tokAt ( 2 ) ;
}
2016-07-08 20:39:34 +02:00
if ( tok2 - > enumerator ( ) & & tok2 - > enumerator ( ) - > scope ) {
long long size = settings - > sizeof_int ;
const Token * type = tok2 - > enumerator ( ) - > scope - > enumType ;
if ( type ) {
2017-10-18 23:20:04 +02:00
size = getSizeOfType ( type , settings ) ;
2016-07-08 20:39:34 +02:00
}
ValueFlow : : Value value ( size ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-11-05 09:29:22 +01:00
setTokenValue ( const_cast < Token * > ( tok ) , value , settings ) ;
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
2016-07-08 20:39:34 +02:00
} else if ( tok2 - > type ( ) & & tok2 - > type ( ) - > isEnumType ( ) ) {
long long size = settings - > sizeof_int ;
2016-10-26 10:36:02 +02:00
if ( tok2 - > type ( ) - > classScope ) {
const Token * type = tok2 - > type ( ) - > classScope - > enumType ;
if ( type ) {
2017-10-18 23:20:04 +02:00
size = getSizeOfType ( type , settings ) ;
2016-10-26 10:36:02 +02:00
}
2016-07-08 20:39:34 +02:00
}
ValueFlow : : Value value ( size ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-11-05 09:29:22 +01:00
setTokenValue ( const_cast < Token * > ( tok ) , value , settings ) ;
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
2016-12-18 14:03:48 +01:00
} else if ( Token : : Match ( tok , " sizeof ( %var% ) / sizeof ( " ) && tok->next()->astParent() == tok->tokAt(4)) {
// Get number of elements in array
const Token * sz1 = tok - > tokAt ( 2 ) ;
const Token * sz2 = tok - > tokAt ( 7 ) ;
const unsigned int varid1 = sz1 - > varId ( ) ;
if ( varid1 & &
sz1 - > variable ( ) & &
sz1 - > variable ( ) - > isArray ( ) & &
! sz1 - > variable ( ) - > dimensions ( ) . empty ( ) & &
sz1 - > variable ( ) - > dimensionKnown ( 0 ) & &
( Token : : Match ( sz2 , " * %varid% ) " , varid1 ) | | Token : : Match ( sz2 , " %varid% [ 0 ] ) " , varid1 ) ) ) {
ValueFlow : : Value value ( sz1 - > variable ( ) - > dimension ( 0 ) ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-12-18 14:03:48 +01:00
setTokenValue ( const_cast < Token * > ( tok - > tokAt ( 4 ) ) , value , settings ) ;
}
2018-03-23 08:28:12 +01:00
} else if ( Token : : Match ( tok2 , " %var% ) " )) {
const Variable * var = tok2 - > variable ( ) ;
// only look for single token types (no pointers or references yet)
if ( var & & var - > typeStartToken ( ) = = var - > typeEndToken ( ) ) {
// find the size of the type
size_t size = 0 ;
if ( var - > isEnumType ( ) ) {
size = settings - > sizeof_int ;
if ( var - > type ( ) - > classScope & & var - > type ( ) - > classScope - > enumType )
size = getSizeOfType ( var - > type ( ) - > classScope - > enumType , settings ) ;
} else if ( ! var - > type ( ) ) {
size = getSizeOfType ( var - > typeStartToken ( ) , settings ) ;
}
// find the number of elements
size_t count = 1 ;
for ( size_t i = 0 ; i < var - > dimensions ( ) . size ( ) ; + + i ) {
if ( var - > dimensionKnown ( i ) )
count * = var - > dimension ( i ) ;
else
count = 0 ;
}
if ( size & & count > 0 ) {
ValueFlow : : Value value ( count * size ) ;
if ( settings - > platformType ! = cppcheck : : Platform : : Unspecified )
value . setKnown ( ) ;
setTokenValue ( const_cast < Token * > ( tok ) , value , settings ) ;
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
}
}
2016-12-18 14:03:48 +01:00
} else if ( ! tok2 - > type ( ) ) {
const ValueType & vt = ValueType : : parseDecl ( tok2 , settings ) ;
if ( vt . pointer ) {
ValueFlow : : Value value ( settings - > sizeof_pointer ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-12-18 14:03:48 +01:00
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
} else if ( vt . type = = ValueType : : Type : : CHAR ) {
ValueFlow : : Value value ( 1 ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-12-18 14:03:48 +01:00
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
} else if ( vt . type = = ValueType : : Type : : SHORT ) {
ValueFlow : : Value value ( settings - > sizeof_short ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-12-18 14:03:48 +01:00
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
} else if ( vt . type = = ValueType : : Type : : INT ) {
ValueFlow : : Value value ( settings - > sizeof_int ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-12-18 14:03:48 +01:00
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
} else if ( vt . type = = ValueType : : Type : : LONG ) {
ValueFlow : : Value value ( settings - > sizeof_long ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-12-18 14:03:48 +01:00
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
} else if ( vt . type = = ValueType : : Type : : LONGLONG ) {
ValueFlow : : Value value ( settings - > sizeof_long_long ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-12-18 14:03:48 +01:00
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
} else if ( vt . type = = ValueType : : Type : : FLOAT ) {
ValueFlow : : Value value ( settings - > sizeof_float ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-12-18 14:03:48 +01:00
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
} else if ( vt . type = = ValueType : : Type : : DOUBLE ) {
ValueFlow : : Value value ( settings - > sizeof_double ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
2017-05-01 12:23:28 +02:00
value . setKnown ( ) ;
2016-12-18 14:03:48 +01:00
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
}
2016-07-08 20:39:34 +02:00
}
// skip over enum
tok = tok - > linkAt ( 1 ) ;
2016-05-07 20:18:07 +02:00
}
2016-07-08 20:39:34 +02:00
return tok - > next ( ) ;
2016-05-07 20:18:07 +02:00
}
2014-01-18 19:30:44 +01:00
static void valueFlowNumber ( TokenList * tokenlist )
{
2016-07-08 20:39:34 +02:00
for ( Token * tok = tokenlist - > front ( ) ; tok ; ) {
2016-10-18 21:44:02 +02:00
tok = valueFlowSetConstantValue ( tok , tokenlist - > getSettings ( ) , tokenlist - > isCPP ( ) ) ;
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 " ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2016-01-26 16:53:01 +01:00
} else if ( Token : : Match ( tok , " [(,] NULL [,) ] " )) {
// NULL function parameters are not simplified in the
// normal tokenlist
ValueFlow : : Value value ( 0 ) ;
2018-01-11 14:22:27 +01:00
if ( ! tok - > isTemplateArg ( ) )
value . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok - > next ( ) , value , tokenlist - > getSettings ( ) ) ;
2015-07-16 17:33:16 +02:00
}
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 ;
2016-11-13 22:33:39 +01:00
strvalue . valueType = ValueFlow : : Value : : TOK ;
2014-08-03 20:11:22 +02:00
strvalue . tokvalue = tok ;
2015-07-25 19:36:29 +02:00
strvalue . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , strvalue , tokenlist - > getSettings ( ) ) ;
2014-08-03 20:11:22 +02:00
}
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 ;
2016-11-13 22:33:39 +01:00
value . valueType = ValueFlow : : Value : : TOK ;
2015-08-28 14:27:56 +02:00
value . tokvalue = it - > second ;
2015-11-07 15:07:26 +01:00
value . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2015-11-07 15:07:26 +01:00
}
// 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 ;
2016-11-13 22:33:39 +01:00
value . valueType = ValueFlow : : Value : : TOK ;
2015-11-07 15:07:26 +01:00
value . tokvalue = tok ;
value . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2015-08-28 14:27:56 +02:00
}
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
2018-07-14 22:36:08 +02:00
if ( ! tok - > isUnaryOp ( " & " ) )
2014-08-03 20:11:22 +02:00
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 ;
2016-11-13 22:33:39 +01:00
value . valueType = ValueFlow : : Value : : TOK ;
2014-08-03 20:11:22 +02:00
value . tokvalue = tok ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2014-08-03 20:11:22 +02:00
}
}
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 ;
2018-07-14 13:09:53 +02:00
if ( tok - > hasKnownValue ( ) )
2016-08-28 19:11:05 +02:00
continue ;
2014-04-14 06:45:39 +02:00
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 ;
2017-10-21 22:08:34 +02:00
while ( bit < = ( MathLib : : bigint_bits - 2 ) & & ( ( ( ( MathLib : : bigint ) 1 ) < < bit ) < number ) )
2014-04-14 06:45:39 +02:00
+ + bit ;
2017-10-21 22:17:07 +02:00
if ( ( ( ( MathLib : : bigint ) 1 ) < < bit ) = = number ) {
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , ValueFlow : : Value ( 0 ) , tokenlist - > getSettings ( ) ) ;
setTokenValue ( tok , ValueFlow : : Value ( number ) , tokenlist - > getSettings ( ) ) ;
2014-04-14 06:45:39 +02:00
}
}
}
2016-01-30 20:03:55 +01:00
static void valueFlowOppositeCondition ( SymbolDatabase * symboldatabase , const Settings * settings )
{
2018-07-14 22:26:22 +02:00
for ( const Scope & scope : symboldatabase - > scopeList ) {
if ( scope . type ! = Scope : : eIf )
2016-01-30 20:03:55 +01:00
continue ;
2018-07-14 22:26:22 +02:00
Token * tok = const_cast < Token * > ( scope . classDef ) ;
2016-01-30 20:34:33 +01:00
if ( ! Token : : simpleMatch ( tok , " if ( " ) )
2016-01-30 20:03:55 +01:00
continue ;
const Token * cond1 = tok - > next ( ) - > astOperand2 ( ) ;
if ( ! cond1 | | ! cond1 - > isComparisonOp ( ) )
continue ;
const bool cpp = symboldatabase - > isCPP ( ) ;
Token * tok2 = tok - > linkAt ( 1 ) ;
while ( Token : : simpleMatch ( tok2 , " ) { " ) ) {
tok2 = tok2 - > linkAt ( 1 ) ;
if ( ! Token : : simpleMatch ( tok2 , " } else { if ( " ) )
break ;
2017-09-11 23:21:36 +02:00
const Token * ifOpenBraceTok = tok2 - > tokAt ( 4 ) ;
const Token * cond2 = ifOpenBraceTok - > astOperand2 ( ) ;
2016-01-30 20:03:55 +01:00
if ( ! cond2 | | ! cond2 - > isComparisonOp ( ) )
continue ;
2016-12-06 12:31:16 +01:00
if ( isOppositeCond ( true , cpp , cond1 , cond2 , settings - > library , true ) ) {
2016-01-30 20:03:55 +01:00
ValueFlow : : Value value ( 1 ) ;
value . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( const_cast < Token * > ( cond2 ) , value , settings ) ;
2016-01-30 20:03:55 +01:00
}
2017-09-11 23:21:36 +02:00
tok2 = ifOpenBraceTok - > link ( ) ;
2016-01-30 20:03:55 +01:00
}
}
}
2017-09-15 15:58:19 +02:00
static void valueFlowGlobalStaticVar ( TokenList * tokenList , const Settings * settings )
{
// Get variable values...
std : : map < const Variable * , ValueFlow : : Value > vars ;
for ( const Token * tok = tokenList - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > variable ( ) )
continue ;
// Initialization...
if ( tok = = tok - > variable ( ) - > nameToken ( ) & &
tok - > variable ( ) - > isStatic ( ) & &
! tok - > variable ( ) - > isConst ( ) & &
tok - > valueType ( ) & &
tok - > valueType ( ) - > isIntegral ( ) & &
tok - > valueType ( ) - > pointer = = 0 & &
tok - > valueType ( ) - > constness = = 0 & &
Token : : Match ( tok , " %name% = " ) & &
tok - > next ( ) - > astOperand2 ( ) & &
tok - > next ( ) - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
vars [ tok - > variable ( ) ] = tok - > next ( ) - > astOperand2 ( ) - > values ( ) . front ( ) ;
} else {
// If variable is written anywhere in TU then remove it from vars
if ( ! tok - > astParent ( ) )
continue ;
if ( Token : : Match ( tok - > astParent ( ) , " ++|--|& " ) & & ! tok - > astParent ( ) - > astOperand2 ( ) )
vars . erase ( tok - > variable ( ) ) ;
2018-03-18 19:53:33 +01:00
else if ( tok - > astParent ( ) - > isAssignmentOp ( ) ) {
2017-09-15 15:58:19 +02:00
if ( tok = = tok - > astParent ( ) - > astOperand1 ( ) )
vars . erase ( tok - > variable ( ) ) ;
else if ( tokenList - > isCPP ( ) & & Token : : Match ( tok - > astParent ( ) - > tokAt ( - 2 ) , " & %name% = " ) )
vars . erase ( tok - > variable ( ) ) ;
2018-04-17 19:51:27 +02:00
} else if ( isLikelyStreamRead ( tokenList - > isCPP ( ) , tok - > astParent ( ) ) ) {
vars . erase ( tok - > variable ( ) ) ;
2017-09-15 15:58:19 +02:00
} else if ( Token : : Match ( tok - > astParent ( ) , " [(,] " ) )
vars . erase ( tok - > variable ( ) ) ;
}
}
// Set values..
for ( Token * tok = tokenList - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > variable ( ) )
continue ;
std : : map < const Variable * , ValueFlow : : Value > : : const_iterator var = vars . find ( tok - > variable ( ) ) ;
if ( var = = vars . end ( ) )
continue ;
setTokenValue ( tok , var - > second , settings ) ;
}
}
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
2017-08-25 23:07:26 +02:00
int inc = 0 ;
2015-07-20 19:45:38 +02:00
if ( Token : : Match ( tok2 - > previous ( ) , " [;{}] %name% ++|-- ; " ) )
2017-08-25 23:07:26 +02:00
inc = ( tok2 - > strAt ( 1 ) = = " ++ " ) ? - 1 : 1 ;
2015-07-20 19:45:38 +02:00
else if ( Token : : Match ( tok2 - > tokAt ( - 2 ) , " [ ; { } ] + + | - - % name % ; " ))
2017-08-25 23:07:26 +02:00
inc = ( tok2 - > strAt ( - 1 ) = = " ++ " ) ? - 1 : 1 ;
2015-07-20 19:45:38 +02:00
else if ( Token : : Match ( tok2 - > previous ( ) , " ++|-- %name% " ) | | Token : : Match ( tok2 , " %name% ++|-- " ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " increment/decrement of " + tok2 - > str ( ) ) ;
break ;
}
2017-08-25 23:07:26 +02:00
if ( inc ! = 0 ) {
val . intvalue + = inc ;
const std : : string info ( tok2 - > str ( ) + " is " + std : : string ( inc = = 1 ? " decremented " : " incremented " ) + " , before this " + ( inc = = 1 ? " decrement " : " increment " ) + " the value is " + val . infoString ( ) ) ;
2018-04-11 09:44:35 +02:00
val . errorPath . emplace_back ( tok2 , info ) ;
2017-08-25 23:07:26 +02:00
}
2015-07-20 19:45:38 +02:00
2017-04-25 20:45:02 +02:00
// compound assignment
if ( Token : : Match ( tok2 - > previous ( ) , " [;{}] %var% %assign% " ) & & tok2 - > next ( ) - > str ( ) ! = " = " ) {
const Token * const assignToken = tok2 - > next ( ) ;
const Token * const rhsToken = assignToken - > astOperand2 ( ) ;
if ( ! rhsToken | | ! rhsToken - > hasKnownIntValue ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " compound assignment, rhs value is not known " ) ;
break ;
}
const MathLib : : bigint rhsValue = rhsToken - > values ( ) . front ( ) . intvalue ;
if ( assignToken - > str ( ) = = " += " )
val . intvalue - = rhsValue ;
else if ( assignToken - > str ( ) = = " -= " )
val . intvalue + = rhsValue ;
2018-01-07 13:11:56 +01:00
else if ( assignToken - > str ( ) = = " *= " & & rhsValue ! = 0 )
2017-04-25 20:45:02 +02:00
val . intvalue / = rhsValue ;
else {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " compound assignment " + tok2 - > str ( ) ) ;
break ;
}
2017-08-24 22:02:49 +02:00
const std : : string info ( " Compound assignment ' " + assignToken - > str ( ) + " ', before assignment value is " + val . infoString ( ) ) ;
2018-04-11 09:44:35 +02:00
val . errorPath . emplace_back ( tok2 , info ) ;
2017-04-25 20:45:02 +02:00
}
2015-07-20 19:45:38 +02:00
// 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 ;
2016-10-23 13:54:44 +02:00
if ( isVariableChangedByFunctionCall ( tok2 , settings , & inconclusive ) ) {
2015-07-20 19:45:38 +02:00
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " possible assignment of " + tok2 - > str ( ) + " by subfunction " ) ;
break ;
}
2017-09-20 22:41:36 +02:00
val . setInconclusive ( inconclusive ) ;
val2 . setInconclusive ( inconclusive ) ;
2015-07-20 19:45:38 +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 " ) ;
continue ;
}
2018-02-24 22:07:34 +01:00
// do-while condition, break in the loop body
{
const Token * parent = tok2 - > astParent ( ) ;
while ( parent & & ! Token : : simpleMatch ( parent - > previous ( ) , " while ( " ) )
parent = parent - > astParent ( ) ;
if ( parent & & Token : : simpleMatch ( parent - > tokAt ( - 2 ) , " } while ( " ) & & Token : : simpleMatch ( parent - > linkAt ( - 2 ) - > previous ( ) , " do { " ) ) {
bool breakBailout = false ;
for ( const Token * iftok = parent - > linkAt ( - 2 ) ; iftok ! = parent ; iftok = iftok - > next ( ) ) {
if ( ! Token : : simpleMatch ( iftok , " if ( " ) )
continue ;
2018-02-24 22:35:37 +01:00
if ( ! Token : : simpleMatch ( iftok - > linkAt ( 1 ) , " ) { break " ) )
2018-02-24 22:07:34 +01:00
continue ;
ProgramMemory programMemory ;
programMemory . setIntValue ( varid , num ) ;
if ( conditionIsTrue ( iftok - > next ( ) - > astOperand2 ( ) , programMemory ) ) {
breakBailout = true ;
break ;
}
}
if ( breakBailout ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
tok2 ,
" no simplification of " + tok2 - > str ( ) + " in do-while condition since there is a break in the loop body " ) ;
break ;
}
}
}
2016-11-05 09:59:48 +01:00
setTokenValue ( tok2 , val , settings ) ;
2015-07-20 19:45:38 +02:00
if ( val2 . condition )
2016-11-05 09:59:48 +01:00
setTokenValue ( tok2 , val2 , settings ) ;
2015-11-05 19:27:10 +01:00
if ( tok2 = = var - > nameToken ( ) )
2015-07-20 19:45:38 +02:00
break ;
}
2017-04-29 08:25:55 +02:00
// skip sizeof etc..
if ( tok2 - > str ( ) = = " ) " & & Token : : Match ( tok2 - > link ( ) - > previous ( ) , " sizeof|typeof|typeid ( " ) )
2015-07-20 19:45:38 +02:00
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 ( ) ;
2018-04-18 17:45:43 +02:00
if ( isVariableChanged ( start , end , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2015-07-20 19:45:38 +02:00
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 ;
}
}
2017-10-18 22:24:31 +02:00
2017-10-18 22:31:44 +02:00
if ( Token : : Match ( tok2 , " %name% ( " ) & & ! Token : : simpleMatch ( tok2 - > linkAt ( 1 ) , " ) { " ) ) {
// bailout: global non-const variables
if ( ! ( var - > isLocal ( ) | | var - > isArgument ( ) ) & & ! var - > isConst ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok , " global variable " + var - > name ( ) ) ;
return ;
}
}
}
2015-07-20 19:45:38 +02:00
}
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
{
2018-07-14 22:26:22 +02:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2018-04-27 22:36:30 +02:00
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; 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 ( ) ) {
2016-10-18 21:44:02 +02:00
if ( tok - > astOperand1 ( ) - > isName ( ) & & tok - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
2015-07-20 19:45:38 +02:00
vartok = tok - > astOperand1 ( ) ;
2017-03-27 18:48:34 +02:00
num = tok - > astOperand2 ( ) - > values ( ) . front ( ) . intvalue ;
2016-10-18 21:44:02 +02:00
} else if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) & & tok - > astOperand2 ( ) - > isName ( ) ) {
2015-07-20 19:45:38 +02:00
vartok = tok - > astOperand2 ( ) ;
2017-03-27 18:48:34 +02:00
num = tok - > astOperand1 ( ) - > values ( ) . front ( ) . intvalue ;
2015-02-01 12:10:20 +01:00
} 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 ;
2016-10-16 07:15:28 +02:00
} else if ( Token : : Match ( tok , " [!?] " ) & & Token : : Match ( tok - > astOperand1 ( ) , " %name% " ) ) {
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: 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 ( " ) ) {
2018-04-18 17:45:43 +02:00
if ( tok2 - > astOperand2 ( ) & & tok2 - > astOperand2 ( ) - > astOperand2 ( ) & & isVariableChanged ( tok2 - > astOperand2 ( ) - > astOperand2 ( ) , tok2 - > link ( ) , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
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
2018-04-18 17:45:43 +02:00
if ( isVariableChanged ( start , end , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2015-02-01 12:10:20 +01:00
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
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
}
2016-03-02 17:07:20 +01:00
ValueFlow : : Value val ( tok , num ) ;
val . varId = varid ;
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 ;
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v2 : valuesToRemove ) {
if ( it - > intvalue = = v2 . intvalue ) {
2014-09-04 17:52:14 +02:00
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
}
}
2016-11-05 09:59:48 +01:00
static void valueFlowAST ( Token * tok , unsigned int varid , const ValueFlow : : Value & value , const Settings * settings )
2015-05-24 17:02:00 +02:00
{
if ( ! tok )
return ;
if ( tok - > varId ( ) = = varid )
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , settings ) ;
valueFlowAST ( const_cast < Token * > ( tok - > astOperand1 ( ) ) , varid , value , settings ) ;
2016-07-17 21:51:20 +02:00
if ( tok - > str ( ) = = " && " & & tok - > astOperand1 ( ) & & tok - > astOperand1 ( ) - > getValue ( 0 ) ) {
ProgramMemory pm ;
pm . setValue ( varid , value ) ;
if ( conditionIsFalse ( tok - > astOperand1 ( ) , pm ) )
return ;
} else if ( tok - > str ( ) = = " || " & & tok - > astOperand1 ( ) ) {
2017-09-12 06:46:38 +02:00
const std : : list < ValueFlow : : Value > & values = tok - > astOperand1 ( ) - > values ( ) ;
2016-07-17 21:51:20 +02:00
bool nonzero = false ;
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v : values ) {
if ( v . intvalue ! = 0 ) {
2017-09-12 06:48:06 +02:00
nonzero = true ;
break ;
}
2016-07-17 21:51:20 +02:00
}
if ( ! nonzero )
return ;
ProgramMemory pm ;
pm . setValue ( varid , value ) ;
if ( conditionIsTrue ( tok - > astOperand1 ( ) , pm ) )
return ;
}
2016-11-05 09:59:48 +01:00
valueFlowAST ( const_cast < Token * > ( tok - > astOperand2 ( ) ) , varid , value , settings ) ;
2015-05-24 17:02:00 +02:00
}
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 ,
2016-10-23 13:54:44 +02:00
unsigned int varid ,
2017-07-09 12:50:17 +02:00
bool globalvar ,
2016-10-23 13:54:44 +02:00
const Settings * settings )
2015-07-17 20:30:34 +02:00
{
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 ) {
2018-04-18 17:45:43 +02:00
if ( ! isVariableChanged ( startToken , endToken , varid , globalvar , settings , true ) )
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
}
}
}
2017-09-20 11:45:28 +02:00
static bool evalAssignment ( ValueFlow : : Value & lhsValue , const std : : string & assign , const ValueFlow : : Value & rhsValue )
{
if ( lhsValue . isIntValue ( ) ) {
if ( assign = = " += " )
lhsValue . intvalue + = rhsValue . intvalue ;
else if ( assign = = " -= " )
lhsValue . intvalue - = rhsValue . intvalue ;
else if ( assign = = " *= " )
lhsValue . intvalue * = rhsValue . intvalue ;
else if ( assign = = " /= " ) {
if ( rhsValue . intvalue = = 0 )
return false ;
else
lhsValue . intvalue / = rhsValue . intvalue ;
} else if ( assign = = " %= " ) {
if ( rhsValue . intvalue = = 0 )
return false ;
else
lhsValue . intvalue % = rhsValue . intvalue ;
} else if ( assign = = " &= " )
lhsValue . intvalue & = rhsValue . intvalue ;
else if ( assign = = " |= " )
lhsValue . intvalue | = rhsValue . intvalue ;
else if ( assign = = " ^= " )
lhsValue . intvalue ^ = rhsValue . intvalue ;
else
return false ;
} else if ( lhsValue . isFloatValue ( ) ) {
if ( assign = = " += " )
lhsValue . floatValue + = rhsValue . intvalue ;
else if ( assign = = " -= " )
lhsValue . floatValue - = rhsValue . intvalue ;
else if ( assign = = " *= " )
lhsValue . floatValue * = rhsValue . intvalue ;
else if ( assign = = " /= " )
lhsValue . floatValue / = rhsValue . intvalue ;
else
return false ;
} else {
return false ;
}
return true ;
}
2018-07-25 22:59:54 +02:00
static bool isEscapeScope ( const Token * tok , TokenList * tokenlist )
{
2018-07-25 23:02:16 +02:00
if ( ! Token : : simpleMatch ( tok , " { " ) )
2018-07-25 22:59:54 +02:00
return false ;
return Token : : findmatch ( tok , " return|continue|break|throw|goto " , tok - > link ( ) ) | |
2018-07-25 23:02:16 +02:00
( tokenlist & & tokenlist - > getSettings ( ) - > library . isScopeNoReturn ( tok - > link ( ) , nullptr ) ) ;
2018-07-25 22:59:54 +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 ,
2016-12-20 11:06:20 +01:00
const bool subFunction ,
2014-06-15 16:47:01 +02:00
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
2017-08-27 19:50:44 +02:00
if ( values . empty ( ) )
return true ;
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 ;
2016-01-16 18:52:34 +01:00
if ( indentlevel < = 0 & & isReturnScope ( 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 ;
2018-02-11 22:14:44 +01:00
const Token * const start1 = iselse ? tok2 - > link ( ) - > linkAt ( - 2 ) : nullptr ;
2018-02-11 22:54:14 +01:00
for ( std : : list < ValueFlow : : Value > : : iterator it = values . begin ( ) ; it ! = values . end ( ) ; ) {
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 ;
}
2018-04-18 17:45:43 +02:00
if ( iselse & & it - > isPossible ( ) & & isVariableChanged ( start1 , start1 - > link ( ) , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) )
2018-02-11 22:14:44 +01:00
values . erase ( it + + ) ;
else
+ + it ;
2014-08-27 16:59:18 +02:00
}
if ( bailoutflag ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " valueFlowForward, conditional return is assumed to be executed " ) ;
return false ;
}
2018-02-11 22:14:44 +01:00
if ( values . empty ( ) )
return true ;
2017-06-01 15:16:07 +02:00
} else if ( indentlevel < = 0 & &
Token : : simpleMatch ( tok2 - > link ( ) - > previous ( ) , " else { " ) & &
! isReturnScope ( tok2 - > link ( ) - > tokAt ( - 2 ) ) & &
2018-04-18 17:45:43 +02:00
isVariableChanged ( tok2 - > link ( ) , tok2 , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2017-11-17 23:04:54 +01:00
changeKnownToPossible ( values ) ;
2014-08-27 16:59:18 +02:00
}
}
2014-06-15 16:47:01 +02:00
2017-08-29 22:35:55 +02:00
// skip lambda functions
// TODO: handle lambda functions
if ( Token : : simpleMatch ( tok2 , " = [ " ) ) {
Token * lambdaEndToken = const_cast < Token * > ( findLambdaEndToken ( tok2 - > next ( ) ) ) ;
if ( lambdaEndToken ) {
tok2 = lambdaEndToken ;
continue ;
}
2017-08-28 22:39:12 +02:00
}
2015-11-08 14:08:47 +01:00
if ( Token : : Match ( tok2 , " [;{}] %name% : " ) | | tok2 - > str ( ) = = " case " ) {
2017-11-17 23:04:54 +01:00
changeKnownToPossible ( values ) ;
2015-07-26 17:05:21 +02:00
tok2 = tok2 - > tokAt ( 2 ) ;
continue ;
}
2017-03-23 20:01:16 +01:00
else if ( ( var - > isGlobal ( ) | | tok2 - > str ( ) = = " asm " ) & & Token : : Match ( tok2 , " %name% ( " ) & & Token : : Match ( tok2 - > linkAt ( 1 ) , " ) !! { " )) {
2017-01-09 15:53:08 +01:00
return false ;
}
2017-04-29 08:25:55 +02:00
// Skip sizeof etc
else if ( Token : : Match ( tok2 , " sizeof|typeof|typeid ( " ) )
2014-06-15 16:47:01 +02:00
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 ;
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v : values ) {
if ( conditionIsTrue ( condition , getProgramMemory ( tok2 , varid , v ) ) ) {
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 ) ;
2016-01-24 08:57:57 +01:00
2018-04-18 17:45:43 +02:00
if ( isVariableChanged ( start , end , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2016-01-24 08:57:57 +01:00
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " valueFlowForward, assignment in do-while " ) ;
return false ;
}
2017-07-09 12:50:17 +02:00
handleKnownValuesInLoop ( start , end , & values , varid , var - > isGlobal ( ) , settings ) ;
2015-07-17 20:30:34 +02:00
}
2014-06-15 16:47:01 +02:00
// conditional block of code that assigns variable..
2016-11-20 15:14:49 +01:00
else if ( ! tok2 - > varId ( ) & & Token : : Match ( tok2 , " %name% ( " ) & & Token : : simpleMatch ( tok2 - > linkAt ( 1 ) , " ) { " )) {
2014-10-11 17:48:51 +02:00
// is variable changed in condition?
2018-04-18 17:45:43 +02:00
if ( isVariableChanged ( tok2 - > next ( ) , tok2 - > next ( ) - > link ( ) , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2014-10-11 17:48:51 +02:00
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..
2016-01-26 16:10:15 +01:00
if ( Token : : Match ( tok2 , " for|while " ) )
2017-07-09 12:50:17 +02:00
handleKnownValuesInLoop ( tok2 , tok2 - > linkAt ( 1 ) - > linkAt ( 1 ) , & values , varid , var - > isGlobal ( ) , settings ) ;
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 ) {
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v : values )
setTokenValue ( tok3 , v , settings ) ;
2016-02-08 10:43:41 +01: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 ( ) ;
2017-08-27 19:50:44 +02:00
const bool condAlwaysTrue = ( condTok & & condTok - > hasKnownIntValue ( ) & & condTok - > values ( ) . front ( ) . intvalue ! = 0 ) ;
const bool condAlwaysFalse = ( condTok & & condTok - > hasKnownIntValue ( ) & & condTok - > values ( ) . front ( ) . intvalue = = 0 ) ;
2015-10-27 12:33:46 +01:00
2014-06-15 16:47:01 +02:00
// Should scope be skipped because variable value is checked?
2015-12-12 16:50:02 +01:00
std : : list < ValueFlow : : Value > truevalues ;
2017-08-27 19:50:44 +02:00
std : : list < ValueFlow : : Value > falsevalues ;
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v : values ) {
2017-08-27 19:50:44 +02:00
if ( condAlwaysTrue ) {
2018-07-14 22:26:22 +02:00
truevalues . push_back ( v ) ;
2017-08-27 19:50:44 +02:00
continue ;
}
if ( condAlwaysFalse ) {
2018-07-14 22:26:22 +02:00
falsevalues . push_back ( v ) ;
2017-08-27 19:50:44 +02:00
continue ;
}
2018-07-14 22:26:22 +02:00
const ProgramMemory & programMemory = getProgramMemory ( tok2 , varid , v ) ;
2017-08-27 19:50:44 +02:00
if ( subFunction & & conditionIsTrue ( condTok , programMemory ) )
2018-07-14 22:26:22 +02:00
truevalues . push_back ( v ) ;
2017-08-27 19:50:44 +02:00
else if ( ! subFunction & & ! conditionIsFalse ( condTok , programMemory ) )
2018-07-14 22:26:22 +02:00
truevalues . push_back ( v ) ;
2017-08-27 19:50:44 +02:00
if ( condAlwaysFalse )
2018-07-14 22:26:22 +02:00
falsevalues . push_back ( v ) ;
2017-08-27 19:50:44 +02:00
else if ( conditionIsFalse ( condTok , programMemory ) )
2018-07-14 22:26:22 +02:00
falsevalues . push_back ( v ) ;
2017-08-27 19:50:44 +02:00
else if ( ! subFunction & & ! conditionIsTrue ( condTok , programMemory ) )
2018-07-14 22:26:22 +02:00
falsevalues . push_back ( v ) ;
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 ( ) ;
2018-05-06 08:35:29 +02:00
bool vfresult = valueFlowForward ( startToken1 - > next ( ) ,
2018-05-06 09:50:53 +02:00
startToken1 - > link ( ) ,
var ,
varid ,
truevalues ,
constValue ,
subFunction ,
tokenlist ,
errorLogger ,
settings ) ;
2014-09-04 17:52:14 +02:00
2018-04-18 17:45:43 +02:00
if ( ! condAlwaysFalse & & isVariableChanged ( startToken1 , startToken1 - > link ( ) , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2014-09-04 17:52:14 +02:00
removeValues ( values , truevalues ) ;
2017-11-17 23:04:54 +01:00
changeKnownToPossible ( values ) ;
2016-01-27 19:20:00 +01:00
}
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
2018-05-06 08:35:29 +02:00
if ( isReturnScope ( tok2 ) | | ! vfresult ) {
2016-11-13 18:19:51 +01:00
if ( condAlwaysTrue )
return false ;
removeValues ( values , truevalues ) ;
}
2015-10-27 12:33:46 +01:00
2017-08-27 19:50:44 +02:00
if ( Token : : simpleMatch ( tok2 , " } else { " ) ) {
Token * const startTokenElse = tok2 - > tokAt ( 2 ) ;
2018-05-06 08:35:29 +02:00
vfresult = valueFlowForward ( startTokenElse - > next ( ) ,
2018-05-06 09:50:53 +02:00
startTokenElse - > link ( ) ,
var ,
varid ,
falsevalues ,
constValue ,
subFunction ,
tokenlist ,
errorLogger ,
settings ) ;
2017-08-27 19:50:44 +02:00
2018-04-18 17:45:43 +02:00
if ( ! condAlwaysTrue & & isVariableChanged ( startTokenElse , startTokenElse - > link ( ) , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2017-11-06 10:28:07 +01:00
removeValues ( values , falsevalues ) ;
2017-11-17 23:04:54 +01:00
changeKnownToPossible ( values ) ;
2017-11-06 10:28:07 +01:00
}
2017-08-27 19:50:44 +02:00
// goto '}'
tok2 = startTokenElse - > link ( ) ;
2018-05-06 08:35:29 +02:00
if ( isReturnScope ( tok2 ) | | ! vfresult ) {
2017-08-27 19:50:44 +02:00
if ( condAlwaysFalse )
return false ;
removeValues ( values , falsevalues ) ;
}
}
2014-06-15 16:47:01 +02:00
continue ;
}
Token * const start = tok2 - > linkAt ( 1 ) - > next ( ) ;
Token * const end = start - > link ( ) ;
2018-04-04 21:51:31 +02:00
const bool varusage = ( indentlevel > = 0 & & constValue & & number_of_if = = 0U ) ?
2018-04-18 17:45:43 +02:00
isVariableChanged ( start , end , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) :
2018-04-04 21:51:31 +02:00
( 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 ) {
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v : values )
setTokenValue ( condtok , v , settings ) ;
2014-08-16 18:32:25 +02:00
}
2016-02-08 10:43:41 +01:00
if ( Token : : Match ( condtok , " %oror%|&&|?|; " ) )
2014-08-16 18:32:25 +02:00
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
}
2018-08-10 18:05:23 +02:00
// Forward known values in the else branch
2018-08-10 22:06:23 +02:00
if ( Token : : simpleMatch ( end , " } else { " ) ) {
2018-08-10 18:05:23 +02:00
std : : list < ValueFlow : : Value > knownValues ;
std : : copy_if ( values . begin ( ) , values . end ( ) , std : : back_inserter ( knownValues ) , std : : mem_fn ( & ValueFlow : : Value : : isKnown ) ) ;
valueFlowForward ( end - > tokAt ( 2 ) ,
end - > linkAt ( 2 ) ,
var ,
varid ,
knownValues ,
constValue ,
subFunction ,
tokenlist ,
errorLogger ,
settings ) ;
}
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
}
}
2016-01-16 18:52:34 +01:00
// stop after conditional return scopes that are executed
if ( isReturnScope ( end ) ) {
2015-01-06 14:12:35 +01:00
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 ) ) & &
2018-07-25 22:59:54 +02:00
( isEscapeScope ( start , tokenlist ) | |
( Token : : simpleMatch ( end , " } else { " ) & & isEscapeScope ( end - > tokAt ( 2 ) , tokenlist ) ) ) ) {
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
}
2018-04-18 17:45:43 +02:00
if ( isVariableChanged ( start , end , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
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 { " ) & &
2018-07-25 23:02:16 +02:00
isEscapeScope ( end - > tokAt ( 2 ) , tokenlist ) ) ) {
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 ) & &
2018-04-27 22:36:30 +02:00
tok3 - > scope ( ) - > bodyEnd & &
Token : : Match ( tok3 - > scope ( ) - > bodyEnd - > tokAt ( - 3 ) , " [;}] break ; " ) & &
2014-09-23 16:06:02 +02:00
! 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
}
}
}
2016-12-17 21:23:14 +01:00
else if ( Token : : Match ( tok2 , " assert|ASSERT ( " ) & & Token : : simpleMatch ( tok2 - > linkAt ( 1 ) , " ) ; " )) {
const Token * const arg = tok2 - > next ( ) - > astOperand2 ( ) ;
if ( arg ! = nullptr & & arg - > str ( ) ! = " , " ) {
// Should scope be skipped because variable value is checked?
2016-12-17 22:20:50 +01:00
for ( std : : list < ValueFlow : : Value > : : iterator it = values . begin ( ) ; it ! = values . end ( ) ; ) {
2016-12-17 21:23:14 +01:00
if ( conditionIsFalse ( arg , getProgramMemory ( tok2 , varid , * it ) ) )
values . erase ( it + + ) ;
else
+ + it ;
}
}
}
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 ) {
2018-04-27 22:36:30 +02:00
tok2 = const_cast < Token * > ( scope - > bodyEnd ) ;
2015-07-27 06:43:43 +02:00
if ( tok2 = = endToken )
break ;
2014-07-16 09:12:56 +02:00
- - indentlevel ;
2017-11-17 23:04:54 +01:00
changeKnownToPossible ( values ) ;
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
2016-11-13 22:33:39 +01:00
if ( condition - > hasKnownIntValue ( ) ) {
2017-03-27 18:48:34 +02:00
const ValueFlow : : Value & condValue = condition - > values ( ) . front ( ) ;
2015-11-30 16:15:58 +01:00
const Token * expr = ( condValue . intvalue ! = 0 ) ? op2 - > astOperand1 ( ) : op2 - > astOperand2 ( ) ;
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v : values )
valueFlowAST ( const_cast < Token * > ( expr ) , varid , v , settings ) ;
2017-11-18 11:02:52 +01:00
if ( isVariableChangedByFunctionCall ( expr , varid , settings , nullptr ) )
2017-11-17 23:04:54 +01:00
changeKnownToPossible ( values ) ;
2015-11-30 16:15:58 +01:00
} else {
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v : values ) {
const ProgramMemory programMemory ( getProgramMemory ( tok2 , varid , v ) ) ;
2015-11-30 16:15:58 +01:00
if ( conditionIsTrue ( condition , programMemory ) )
2018-07-14 22:26:22 +02:00
valueFlowAST ( const_cast < Token * > ( op2 - > astOperand1 ( ) ) , varid , v , settings ) ;
2015-11-30 16:15:58 +01:00
else if ( conditionIsFalse ( condition , programMemory ) )
2018-07-14 22:26:22 +02:00
valueFlowAST ( const_cast < Token * > ( op2 - > astOperand2 ( ) ) , varid , v , settings ) ;
2015-11-30 16:15:58 +01:00
else
2018-07-14 22:26:22 +02:00
valueFlowAST ( const_cast < Token * > ( op2 ) , varid , v , settings ) ;
2015-11-30 16:15:58 +01:00
}
2017-11-18 11:02:52 +01:00
if ( isVariableChangedByFunctionCall ( op2 , varid , settings , nullptr ) )
2017-11-17 23:04:54 +01:00
changeKnownToPossible ( values ) ;
2015-05-24 17:02:00 +02:00
}
2016-07-17 21:51:20 +02:00
2015-05-24 17:02:00 +02:00
// Skip conditional expressions..
2018-03-16 19:13:48 +01:00
const Token * const questionToken = tok2 ;
2015-05-24 17:02:00 +02:00
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 ( ) ;
2018-03-16 19:13:48 +01:00
2018-04-18 17:45:43 +02:00
if ( isVariableChanged ( questionToken , questionToken - > astOperand2 ( ) , varid , false , settings , tokenlist - > isCPP ( ) ) & &
isVariableChanged ( questionToken - > astOperand2 ( ) , tok2 , varid , false , settings , tokenlist - > isCPP ( ) ) ) {
2018-03-16 19:13:48 +01:00
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " variable " + var - > name ( ) + " valueFlowForward, assignment in condition " ) ;
return false ;
}
2015-05-24 17:02:00 +02:00
}
2017-01-09 15:53:08 +01:00
else if ( tok2 - > varId ( ) = = varid ) {
2017-08-23 11:13:47 +02:00
// compound assignment, known value in rhs
if ( Token : : Match ( tok2 - > previous ( ) , " !!* %name% %assign% " ) & &
tok2 - > next ( ) - > str ( ) ! = " = " & &
tok2 - > next ( ) - > astOperand2 ( ) & &
tok2 - > next ( ) - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
const ValueFlow : : Value & rhsValue = tok2 - > next ( ) - > astOperand2 ( ) - > values ( ) . front ( ) ;
const std : : string & assign = tok2 - > next ( ) - > str ( ) ;
std : : list < ValueFlow : : Value > : : iterator it ;
// Erase values that are not int values..
for ( it = values . begin ( ) ; it ! = values . end ( ) ; ) {
2017-09-20 11:45:28 +02:00
if ( ! evalAssignment ( * it , assign , rhsValue ) ) {
it = values . erase ( it ) ;
} else {
2017-08-24 22:02:49 +02:00
const std : : string info ( " Compound assignment ' " + assign + " ', assigned value is " + it - > infoString ( ) ) ;
2018-04-11 09:44:35 +02:00
it - > errorPath . emplace_back ( tok2 , info ) ;
2017-09-20 11:45:28 +02:00
2017-08-23 11:13:47 +02:00
+ + it ;
}
2017-09-20 11:45:28 +02:00
2017-08-23 11:13:47 +02:00
}
if ( values . empty ( ) ) {
if ( settings - > debugwarnings )
2018-02-04 20:53:43 +01:00
bailout ( tokenlist , errorLogger , tok2 , " compound assignment of " + tok2 - > str ( ) ) ;
2017-08-23 11:13:47 +02:00
return false ;
}
}
2014-06-15 16:47:01 +02:00
// bailout: assignment
2017-08-23 11:13:47 +02:00
else if ( Token : : Match ( tok2 - > previous ( ) , " !!* %name% %assign% " ) ) {
2014-06-18 21:07:01 +02:00
// simplify rhs
2017-09-14 22:49:47 +02:00
std : : stack < Token * > rhs ;
rhs . push ( const_cast < Token * > ( tok2 - > next ( ) - > astOperand2 ( ) ) ) ;
while ( ! rhs . empty ( ) ) {
Token * rtok = rhs . top ( ) ;
rhs . pop ( ) ;
if ( ! rtok )
continue ;
if ( rtok - > str ( ) = = " ( " & & Token : : Match ( rtok - > astOperand1 ( ) , " sizeof|typeof|typeid " ) )
continue ;
if ( Token : : Match ( rtok , " ++|--|?|:|;|, " ) )
continue ;
if ( rtok - > varId ( ) = = varid ) {
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v : values )
setTokenValue ( rtok , v , settings ) ;
2017-09-14 22:49:47 +02:00
}
rhs . push ( const_cast < Token * > ( rtok - > astOperand1 ( ) ) ) ;
rhs . push ( const_cast < Token * > ( rtok - > astOperand2 ( ) ) ) ;
2014-06-18 21:07:01 +02:00
}
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 >>
2018-04-17 14:23:04 +02:00
if ( isLikelyStreamRead ( tokenlist - > isCPP ( ) , tok2 - > previous ( ) ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " Possible assignment of " + tok2 - > str ( ) + " using " + tok2 - > strAt ( - 1 ) ) ;
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 ) ;
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & v : values ) {
if ( ! conditional | | ! v . conditional )
setTokenValue ( tok2 , v , settings ) ;
2015-07-12 19:35:47 +02:00
}
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 ( ) ; ) {
2016-11-13 22:33:39 +01:00
if ( ! it - > isIntValue ( ) )
2015-05-02 17:30:09 +02:00
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 )
2016-11-05 09:59:48 +01:00
setTokenValue ( op , * it , settings ) ;
2015-05-02 17:30:09 +02:00
it - > intvalue + = ( inc ? 1 : - 1 ) ;
if ( pre )
2016-11-05 09:59:48 +01:00
setTokenValue ( op , * it , settings ) ;
2017-08-25 22:15:52 +02:00
const std : : string info ( tok2 - > str ( ) + " is " + std : : string ( inc ? " incremented " : " decremented " ) + " ', new value is " + it - > infoString ( ) ) ;
2018-04-11 09:44:35 +02:00
it - > errorPath . emplace_back ( tok2 , info ) ;
2015-05-02 17:30:09 +02:00
}
}
2014-10-20 15:54:02 +02:00
// bailout if address of var is taken..
2018-07-14 22:36:08 +02:00
if ( tok2 - > astParent ( ) & & tok2 - > astParent ( ) - > isUnaryOp ( " & " ) ) {
2014-10-20 15:54:02 +02:00
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 ;
2016-10-23 13:54:44 +02:00
if ( isVariableChangedByFunctionCall ( tok2 , settings , & inconclusive ) ) {
2014-06-15 16:47:01 +02:00
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 ) {
2018-07-14 22:26:22 +02:00
for ( ValueFlow : : Value & v : values )
v . setInconclusive ( ) ;
2014-06-15 16:47:01 +02:00
}
2016-11-20 15:14:49 +01:00
if ( tok2 - > strAt ( 1 ) = = " . " & & tok2 - > next ( ) - > originalName ( ) ! = " -> " ) {
if ( settings - > inconclusive ) {
2018-07-14 22:26:22 +02:00
for ( ValueFlow : : Value & v : values )
v . setInconclusive ( ) ;
2016-11-20 15:14:49 +01:00
} else {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " possible assignment of " + tok2 - > str ( ) + " by member function " ) ;
return false ;
}
}
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 ( ) ;
2018-04-18 17:45:43 +02:00
if ( isVariableChanged ( bodyStart , bodyStart - > link ( ) , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2015-07-26 19:28:42 +02:00
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
}
2016-11-20 15:14:49 +01:00
static bool isStdMoveOrStdForwarded ( Token * tok , ValueFlow : : Value : : MoveKind * moveKind , Token * * varTok = nullptr )
{
if ( tok - > str ( ) ! = " std " )
return false ;
2017-09-01 17:17:40 +02:00
ValueFlow : : Value : : MoveKind kind = ValueFlow : : Value : : NonMovedVariable ;
2016-11-20 15:14:49 +01:00
Token * variableToken = nullptr ;
if ( Token : : Match ( tok , " std :: move ( %var% ) " ) ) {
variableToken = tok - > tokAt ( 4 ) ;
kind = ValueFlow : : Value : : MovedVariable ;
} else if ( Token : : simpleMatch ( tok , " std :: forward < " ) ) {
Token * leftAngle = tok - > tokAt ( 3 ) ;
Token * rightAngle = leftAngle - > link ( ) ;
if ( Token : : Match ( rightAngle , " > ( %var% ) " ) ) {
variableToken = rightAngle - > tokAt ( 2 ) ;
kind = ValueFlow : : Value : : ForwardedVariable ;
}
}
2017-09-01 17:19:25 +02:00
if ( ! variableToken )
2016-11-20 15:14:49 +01:00
return false ;
if ( variableToken - > strAt ( 2 ) = = " . " ) // Only partially moved
return false ;
if ( moveKind ! = nullptr )
* moveKind = kind ;
if ( varTok ! = nullptr )
* varTok = variableToken ;
return true ;
}
2016-12-11 22:32:57 +01:00
static bool isOpenParenthesisMemberFunctionCallOfVarId ( const Token * openParenthesisToken , unsigned int varId )
{
2017-09-01 17:16:08 +02:00
const Token * varTok = openParenthesisToken - > tokAt ( - 3 ) ;
return Token : : Match ( varTok , " %varid% . %name% ( " , varId ) & &
varTok - > next ( ) - > originalName ( ) = = emptyString ;
2016-12-11 22:32:57 +01:00
}
2016-12-12 21:46:05 +01:00
static const Token * nextAfterAstRightmostLeaf ( Token const * tok )
{
const Token * rightmostLeaf = tok ;
if ( ! rightmostLeaf | | ! rightmostLeaf - > astOperand1 ( ) )
return nullptr ;
do {
if ( rightmostLeaf - > astOperand2 ( ) )
rightmostLeaf = rightmostLeaf - > astOperand2 ( ) ;
else
rightmostLeaf = rightmostLeaf - > astOperand1 ( ) ;
} while ( rightmostLeaf - > astOperand1 ( ) ) ;
return rightmostLeaf - > next ( ) ;
}
static const Token * findOpenParentesisOfMove ( const Token * moveVarTok )
{
const Token * tok = moveVarTok ;
while ( tok & & tok - > str ( ) ! = " ( " )
tok = tok - > previous ( ) ;
return tok ;
}
static const Token * findEndOfFunctionCallForParameter ( const Token * parameterToken )
{
if ( ! parameterToken )
return nullptr ;
const Token * parent = parameterToken - > astParent ( ) ;
2016-12-17 18:51:16 +01:00
while ( parent & & ! parent - > isOp ( ) & & parent - > str ( ) ! = " ( " )
2016-12-12 21:46:05 +01:00
parent = parent - > astParent ( ) ;
if ( ! parent )
return nullptr ;
return nextAfterAstRightmostLeaf ( parent ) ;
}
2016-11-20 15:14:49 +01:00
static void valueFlowAfterMove ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
{
if ( ! tokenlist - > isCPP ( ) | | settings - > standards . cpp < Standards : : CPP11 )
return ;
2018-07-14 22:26:22 +02:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2016-11-20 15:14:49 +01:00
if ( ! scope )
continue ;
2018-04-27 22:36:30 +02:00
const Token * start = scope - > bodyStart ;
2016-11-20 15:14:49 +01:00
if ( scope - > function ) {
const Token * memberInitializationTok = scope - > function - > constructorMemberInitialization ( ) ;
if ( memberInitializationTok )
start = memberInitializationTok ;
}
2018-04-27 22:36:30 +02:00
for ( Token * tok = const_cast < Token * > ( start ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2016-11-20 15:14:49 +01:00
Token * varTok ;
2016-12-12 14:29:27 +01:00
if ( Token : : Match ( tok , " %var% . reset|clear ( " ) & & tok - > next ( ) - > originalName ( ) = = emptyString ) {
varTok = tok ;
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : MOVED ;
value . moveKind = ValueFlow : : Value : : NonMovedVariable ;
2018-04-11 09:44:35 +02:00
value . errorPath . emplace_back ( tok , " Calling " + tok - > next ( ) - > expressionString ( ) + " makes " + tok - > str ( ) + " 'non-moved' " ) ;
2016-12-12 14:29:27 +01:00
value . setKnown ( ) ;
std : : list < ValueFlow : : Value > values ;
values . push_back ( value ) ;
const Variable * var = varTok - > variable ( ) ;
if ( ! var | | ( ! var - > isLocal ( ) & & ! var - > isArgument ( ) ) )
continue ;
const unsigned int varId = varTok - > varId ( ) ;
2018-04-27 22:36:30 +02:00
const Token * const endOfVarScope = var - > typeStartToken ( ) - > scope ( ) - > bodyEnd ;
2016-12-12 14:29:27 +01:00
setTokenValue ( varTok , value , settings ) ;
2016-12-20 11:06:20 +01:00
valueFlowForward ( varTok - > next ( ) , endOfVarScope , var , varId , values , false , false , tokenlist , errorLogger , settings ) ;
2016-12-12 14:29:27 +01:00
continue ;
}
2016-11-20 15:14:49 +01:00
ValueFlow : : Value : : MoveKind moveKind ;
if ( ! isStdMoveOrStdForwarded ( tok , & moveKind , & varTok ) )
continue ;
2016-12-11 22:32:57 +01:00
const unsigned int varId = varTok - > varId ( ) ;
2016-11-20 15:14:49 +01:00
// x is not MOVED after assignment if code is: x = ... std::move(x) .. ;
const Token * parent = tok - > astParent ( ) ;
2016-12-11 22:32:57 +01:00
while ( parent & & parent - > str ( ) ! = " = " & & parent - > str ( ) ! = " return " & &
! ( parent - > str ( ) = = " ( " & & isOpenParenthesisMemberFunctionCallOfVarId ( parent , varId ) ) )
2016-11-20 15:14:49 +01:00
parent = parent - > astParent ( ) ;
2016-12-11 22:32:57 +01:00
if ( parent & &
( parent - > str ( ) = = " return " | | // MOVED in return statement
parent - > str ( ) = = " ( " ) ) // MOVED in self assignment, isOpenParenthesisMemberFunctionCallOfVarId == true
2016-11-20 15:14:49 +01:00
continue ;
2016-12-11 22:32:57 +01:00
if ( parent & & parent - > astOperand1 ( ) - > varId ( ) = = varId )
2016-11-20 15:14:49 +01:00
continue ;
const Variable * var = varTok - > variable ( ) ;
if ( ! var )
continue ;
2018-04-27 22:36:30 +02:00
const Token * const endOfVarScope = var - > typeStartToken ( ) - > scope ( ) - > bodyEnd ;
2016-11-20 15:14:49 +01:00
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : MOVED ;
value . moveKind = moveKind ;
2017-10-10 15:49:15 +02:00
if ( moveKind = = ValueFlow : : Value : : MovedVariable )
2018-04-11 09:44:35 +02:00
value . errorPath . emplace_back ( tok , " Calling std::move( " + varTok - > str ( ) + " ) " ) ;
2017-10-10 15:49:15 +02:00
else // if (moveKind == ValueFlow::Value::ForwardedVariable)
2018-04-11 09:44:35 +02:00
value . errorPath . emplace_back ( tok , " Calling std::forward( " + varTok - > str ( ) + " ) " ) ;
2016-11-20 15:14:49 +01:00
value . setKnown ( ) ;
std : : list < ValueFlow : : Value > values ;
values . push_back ( value ) ;
2016-12-12 21:46:05 +01:00
const Token * openParentesisOfMove = findOpenParentesisOfMove ( varTok ) ;
const Token * endOfFunctionCall = findEndOfFunctionCallForParameter ( openParentesisOfMove ) ;
if ( endOfFunctionCall )
2016-12-20 11:06:20 +01:00
valueFlowForward ( const_cast < Token * > ( endOfFunctionCall ) , endOfVarScope , var , varId , values , false , false , tokenlist , errorLogger , settings ) ;
2016-11-20 15:14:49 +01: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
{
2018-07-14 22:26:22 +02:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2016-11-21 17:26:36 +01:00
std : : set < unsigned int > aliased ;
2018-04-27 22:36:30 +02:00
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2016-11-21 17:26:36 +01:00
// Alias
2018-07-14 22:36:08 +02:00
if ( tok - > isUnaryOp ( " & " ) ) {
2016-11-21 17:26:36 +01:00
aliased . insert ( tok - > astOperand1 ( ) - > varId ( ) ) ;
continue ;
}
2015-02-01 12:10:20 +01:00
// 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 ( ) ;
2016-11-21 17:26:36 +01:00
if ( aliased . find ( varid ) ! = aliased . end ( ) )
continue ;
2015-02-01 12:10:20 +01:00
const Variable * var = tok - > astOperand1 ( ) - > variable ( ) ;
2017-01-09 15:53:08 +01:00
if ( ! var | | ( ! var - > isLocal ( ) & & ! var - > isGlobal ( ) & & ! var - > isArgument ( ) ) )
2015-02-01 12:10:20 +01:00
continue ;
2014-07-15 10:36:13 +02:00
2018-04-27 22:36:30 +02:00
const Token * const endOfVarScope = var - > typeStartToken ( ) - > scope ( ) - > bodyEnd ;
2014-01-21 21:13:49 +01:00
2015-02-01 12:10:20 +01:00
// Rhs values..
2017-03-27 18:48:34 +02:00
if ( ! tok - > astOperand2 ( ) | | tok - > astOperand2 ( ) - > values ( ) . empty ( ) )
2015-02-01 12:10:20 +01:00
continue ;
2014-01-21 21:13:49 +01:00
2017-03-27 18:48:34 +02:00
std : : list < ValueFlow : : Value > values = tok - > astOperand2 ( ) - > values ( ) ;
2017-05-16 23:12:35 +02:00
for ( std : : list < ValueFlow : : Value > : : iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
2017-05-20 08:47:35 +02:00
const std : : string info = " Assignment ' " + tok - > expressionString ( ) + " ', assigned value is " + it - > infoString ( ) ;
2018-04-11 09:44:35 +02:00
it - > errorPath . emplace_back ( tok - > astOperand2 ( ) , info ) ;
2017-05-16 23:12:35 +02:00
}
2015-02-01 12:10:20 +01:00
const bool constValue = tok - > astOperand2 ( ) - > isNumber ( ) ;
2015-07-17 20:48:37 +02:00
2017-03-04 11:13:28 +01:00
if ( tokenlist - > isCPP ( ) & & Token : : Match ( var - > typeStartToken ( ) , " bool|_Bool " ) ) {
2016-12-19 21:21:18 +01:00
std : : list < ValueFlow : : Value > : : iterator it ;
for ( it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
if ( it - > isIntValue ( ) )
it - > intvalue = ( it - > intvalue ! = 0 ) ;
if ( it - > isTokValue ( ) )
it - > intvalue = ( it - > tokvalue ! = 0 ) ;
}
}
2015-07-17 20:48:37 +02:00
// Static variable initialisation?
2017-11-17 23:04:54 +01:00
if ( var - > isStatic ( ) & & var - > nameToken ( ) = = tok - > astOperand1 ( ) )
changeKnownToPossible ( values ) ;
2015-07-17 20:48:37 +02:00
2016-10-15 19:09:50 +02:00
// Skip RHS
2016-11-20 15:14:49 +01:00
const Token * nextExpression = nextAfterAstRightmostLeaf ( tok ) ;
2016-10-15 19:09:50 +02:00
2016-12-20 11:06:20 +01:00
valueFlowForward ( const_cast < Token * > ( nextExpression ) , endOfVarScope , var , varid , values , constValue , false , tokenlist , errorLogger , settings ) ;
2015-02-01 12:10:20 +01:00
}
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
{
2018-07-14 22:26:22 +02:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2017-09-03 23:24:55 +02:00
std : : set < unsigned > aliased ;
2018-04-27 22:36:30 +02:00
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2018-04-05 06:51:31 +02:00
const Token * vartok = nullptr ;
const Token * numtok = nullptr ;
const Token * lowertok = nullptr ;
const Token * uppertok = nullptr ;
2015-02-01 12:10:20 +01:00
2017-09-03 23:24:55 +02:00
if ( Token : : Match ( tok , " = & %var% ; " ) )
aliased . insert ( tok - > tokAt ( 2 ) - > varId ( ) ) ;
2015-02-01 12:10:20 +01:00
// Comparison
if ( Token : : Match ( tok , " ==|!=|>=|<= " ) ) {
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
2016-10-18 21:44:02 +02:00
if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) ) {
2015-02-01 12:10:20 +01:00
numtok = tok - > astOperand1 ( ) ;
vartok = tok - > astOperand2 ( ) ;
} else {
numtok = tok - > astOperand2 ( ) ;
vartok = tok - > astOperand1 ( ) ;
}
if ( vartok - > str ( ) = = " = " & & vartok - > astOperand1 ( ) & & vartok - > astOperand2 ( ) )
vartok = vartok - > astOperand1 ( ) ;
2018-04-05 06:51:31 +02:00
if ( ! vartok - > isName ( ) )
continue ;
} else if ( Token : : simpleMatch ( tok , " > " ) ) {
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) ) {
uppertok = tok - > astOperand1 ( ) ;
vartok = tok - > astOperand2 ( ) ;
} else {
lowertok = tok - > astOperand2 ( ) ;
vartok = tok - > astOperand1 ( ) ;
}
if ( vartok - > str ( ) = = " = " & & vartok - > astOperand1 ( ) & & vartok - > astOperand2 ( ) )
vartok = vartok - > astOperand1 ( ) ;
if ( ! vartok - > isName ( ) )
continue ;
} else if ( Token : : simpleMatch ( tok , " < " ) ) {
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) ) {
lowertok = tok - > astOperand1 ( ) ;
vartok = tok - > astOperand2 ( ) ;
} else {
uppertok = tok - > astOperand2 ( ) ;
vartok = tok - > astOperand1 ( ) ;
}
if ( vartok - > str ( ) = = " = " & & vartok - > astOperand1 ( ) & & vartok - > astOperand2 ( ) )
vartok = vartok - > astOperand1 ( ) ;
if ( ! vartok - > isName ( ) )
2015-02-01 12:10:20 +01:00
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
2018-04-05 08:21:43 +02:00
if ( numtok & & ! numtok - > hasKnownIntValue ( ) )
2018-04-05 06:51:31 +02:00
continue ;
2018-04-05 08:21:43 +02:00
if ( lowertok & & ! lowertok - > hasKnownIntValue ( ) )
2018-04-05 06:51:31 +02:00
continue ;
2018-04-05 08:21:43 +02:00
if ( uppertok & & ! uppertok - > hasKnownIntValue ( ) )
2018-04-05 06:51:31 +02:00
continue ;
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 ( ) ;
2017-01-09 15:53:08 +01:00
if ( ! var | | ! ( var - > isLocal ( ) | | var - > isGlobal ( ) | | var - > isArgument ( ) ) )
2014-06-16 16:39:41 +02:00
continue ;
2017-09-03 23:24:55 +02:00
if ( aliased . find ( varid ) ! = aliased . end ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , vartok , " variable is aliased so we just skip all valueflow after condition " ) ;
continue ;
}
2018-04-08 09:24:01 +02:00
std : : list < ValueFlow : : Value > true_values ;
std : : list < ValueFlow : : Value > false_values ;
2018-04-05 06:51:31 +02:00
// TODO: We should add all known values
2018-04-05 08:21:43 +02:00
if ( numtok ) {
2018-04-11 09:44:35 +02:00
false_values . emplace_back ( tok , numtok - > values ( ) . front ( ) . intvalue ) ;
true_values . emplace_back ( tok , numtok - > values ( ) . front ( ) . intvalue ) ;
2018-04-05 08:21:43 +02:00
} else if ( lowertok ) {
2018-04-05 06:51:31 +02:00
long long v = lowertok - > values ( ) . front ( ) . intvalue ;
2018-04-11 09:44:35 +02:00
true_values . emplace_back ( tok , v + 1 ) ;
false_values . emplace_back ( tok , v ) ;
2018-04-05 06:51:31 +02:00
2018-04-05 08:21:43 +02:00
} else if ( uppertok ) {
2018-04-05 06:51:31 +02:00
long long v = uppertok - > values ( ) . front ( ) . intvalue ;
2018-04-11 09:44:35 +02:00
true_values . emplace_back ( tok , v - 1 ) ;
false_values . emplace_back ( tok , v ) ;
2018-04-05 06:51:31 +02:00
} else {
2018-04-11 09:44:35 +02:00
true_values . emplace_back ( tok , 0LL ) ;
false_values . emplace_back ( tok , 0LL ) ;
2018-04-05 06:51:31 +02:00
}
2015-02-01 12:10:20 +01:00
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 )
2018-04-08 09:24:01 +02:00
setTokenValue ( rhstok , true_values . front ( ) , settings ) ;
2016-03-03 16:01:15 +01:00
else if ( Token : : Match ( rhstok , " ++|--|= " ) & & Token : : Match ( rhstok - > astOperand1 ( ) , " %varid% " , varid ) ) {
2015-02-01 12:10:20 +01:00
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%|&& " ) & &
2018-04-18 17:45:43 +02:00
isVariableChanged ( top , top - > link ( ) , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2015-02-01 12:10:20 +01:00
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
2018-04-08 09:24:01 +02:00
Token * startTokens [ ] = { nullptr , nullptr } ;
2015-02-01 12:10:20 +01:00
// based on the comparison, should we check the if or while?
2018-04-08 09:24:01 +02:00
bool check_if = false ;
bool check_else = false ;
2018-04-05 06:51:31 +02:00
if ( Token : : Match ( tok , " ==|>=|<=|!|>|< " ) )
2018-04-08 09:24:01 +02:00
check_if = true ;
if ( Token : : Match ( tok , " %name%|!=|>|< " ) )
check_else = true ;
2015-02-01 12:10:20 +01:00
2018-04-08 09:30:13 +02:00
if ( ! check_if & & ! check_else )
continue ;
// if astParent is "!" we need to invert codeblock
{
2015-02-01 12:10:20 +01:00
const Token * parent = tok - > astParent ( ) ;
while ( parent & & parent - > str ( ) = = " && " )
parent = parent - > astParent ( ) ;
2018-04-08 09:24:01 +02:00
if ( parent & & parent - > str ( ) = = " ! " ) {
check_if = ! check_if ;
check_else = ! check_else ;
}
2015-02-01 12:10:20 +01:00
}
2014-08-26 18:48:11 +02:00
2018-04-08 09:30:13 +02:00
// determine startToken(s)
if ( check_if & & Token : : simpleMatch ( top - > link ( ) , " ) { " ) )
startTokens [ 0 ] = top - > link ( ) - > next ( ) ;
if ( check_else & & Token : : simpleMatch ( top - > link ( ) - > linkAt ( 1 ) , " } else { " ) )
startTokens [ 1 ] = top - > link ( ) - > linkAt ( 1 ) - > tokAt ( 2 ) ;
2018-04-08 09:24:01 +02:00
bool bail = false ;
2018-04-08 09:25:59 +02:00
for ( int i = 0 ; i < 2 ; i + + ) {
2018-04-08 09:24:01 +02:00
Token * startToken = startTokens [ i ] ;
2018-04-08 09:30:13 +02:00
if ( ! startToken )
continue ;
std : : list < ValueFlow : : Value > & values = ( i = = 0 ? true_values : false_values ) ;
if ( values . size ( ) = = 1U & & Token : : Match ( tok , " ==|! " ) ) {
const Token * parent = tok - > astParent ( ) ;
while ( parent & & parent - > str ( ) = = " && " )
parent = parent - > astParent ( ) ;
if ( parent & & parent - > str ( ) = = " ( " )
values . front ( ) . setKnown ( ) ;
}
2018-04-08 09:24:01 +02:00
2018-04-08 09:30:13 +02:00
valueFlowForward ( startTokens [ i ] - > next ( ) , startTokens [ i ] - > link ( ) , var , varid , values , true , false , tokenlist , errorLogger , settings ) ;
values . front ( ) . setPossible ( ) ;
2018-04-18 17:45:43 +02:00
if ( isVariableChanged ( startTokens [ i ] , startTokens [ i ] - > link ( ) , varid , var - > isGlobal ( ) , settings , tokenlist - > isCPP ( ) ) ) {
2018-04-08 09:30:13 +02:00
// TODO: The endToken should not be startTokens[i]->link() in the valueFlowForward call
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , startTokens [ i ] - > link ( ) , " valueFlowAfterCondition: " + var - > name ( ) + " is changed in conditional block " ) ;
bail = true ;
break ;
2015-07-28 12:47:08 +02:00
}
2015-07-28 08:58:05 +02:00
}
2018-04-08 09:25:59 +02:00
if ( bail )
2018-04-08 09:24:01 +02:00
continue ;
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
2018-05-29 17:33:43 +02:00
const bool dead_if = isReturnScope ( after ) ;
2018-04-09 16:13:17 +02:00
bool dead_else = false ;
2015-02-01 12:10:20 +01:00
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 ;
}
2018-04-09 16:13:17 +02:00
dead_else = isReturnScope ( after ) ;
2015-02-01 12:10:20 +01:00
}
2018-04-09 16:13:17 +02:00
std : : list < ValueFlow : : Value > * values = nullptr ;
2018-04-09 22:21:17 +02:00
if ( ! dead_if & & check_if )
2018-04-09 16:13:17 +02:00
values = & true_values ;
2018-04-09 22:21:17 +02:00
else if ( ! dead_else & & check_else )
2018-04-09 16:13:17 +02:00
values = & false_values ;
2018-04-08 09:24:01 +02:00
if ( values ) {
2015-02-01 12:10:20 +01:00
// 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 ;
2018-04-27 22:36:30 +02:00
valueFlowForward ( after - > next ( ) , top - > scope ( ) - > bodyEnd , var , varid , * values , constValue , false , tokenlist , errorLogger , settings ) ;
2015-02-01 12:10:20 +01:00
}
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 ;
2016-11-13 22:33:39 +01:00
else if ( expr - > hasKnownIntValue ( ) ) {
2017-03-27 18:48:34 +02:00
* result = expr - > values ( ) . front ( ) . intvalue ;
2015-12-01 07:49:19 +01:00
}
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 {
2017-09-04 16:53:56 +02:00
long long intValue ;
if ( ! programMemory - > getIntValue ( expr - > astOperand1 ( ) - > varId ( ) , & intValue ) )
2014-03-17 18:43:47 +01:00
* error = true ;
else {
2017-09-04 16:53:56 +02:00
if ( intValue = = 0 & &
2014-03-25 18:22:22 +01:00
expr - > str ( ) = = " -- " & &
expr - > astOperand1 ( ) - > variable ( ) & &
expr - > astOperand1 ( ) - > variable ( ) - > typeStartToken ( ) - > isUnsigned ( ) )
* error = true ; // overflow
2017-09-04 16:53:56 +02:00
* result = intValue + ( expr - > str ( ) = = " ++ " ? 1 : - 1 ) ;
2016-11-14 06:42:52 +01:00
programMemory - > setIntValue ( expr - > astOperand1 ( ) - > varId ( ) , * result ) ;
2014-03-17 18:43:47 +01:00
}
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 ;
2016-11-23 23:26:18 +01:00
else if ( expr - > str ( ) = = " << " ) {
2017-10-21 21:04:14 +02:00
if ( result2 < 0 | | result1 < 0 | | result2 > = MathLib : : bigint_bits ) { // don't perform UB
2016-11-23 23:26:18 +01:00
* error = true ;
} else {
* result = result1 < < result2 ;
}
} else if ( expr - > str ( ) = = " >> " ) {
if ( result2 < 0 ) { // don't perform UB
* error = true ;
} else {
* result = result1 > > result2 ;
}
}
2014-03-22 19:02:33 +01:00
}
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 ( ) ) {
2017-05-22 15:22:38 +02:00
const Token * tokvalue = nullptr ;
2016-11-14 06:42:52 +01:00
if ( ! programMemory - > getTokValue ( expr - > astOperand1 ( ) - > varId ( ) , & tokvalue ) ) {
2017-03-27 18:48:34 +02:00
if ( expr - > astOperand1 ( ) - > values ( ) . size ( ) ! = 1U | | ! expr - > astOperand1 ( ) - > values ( ) . front ( ) . isTokValue ( ) ) {
2015-12-01 07:49:19 +01:00
* error = true ;
return ;
}
2017-03-27 18:48:34 +02:00
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
2017-08-09 20:00:26 +02:00
num2tok = nullptr ;
2014-03-17 18:43:47 +01:00
}
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 ) ;
2016-12-20 07:54:38 +01:00
if ( result = = 0 ) // 2nd expression is false => no looping
return false ;
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
}
2017-07-09 12:50:17 +02:00
static void valueFlowForLoopSimplify ( Token * const bodyStart , const unsigned int varid , bool globalvar , const MathLib : : bigint value , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
2014-03-17 18:43:47 +01:00
{
const Token * const bodyEnd = bodyStart - > link ( ) ;
// Is variable modified inside for loop
2018-04-18 17:45:43 +02:00
if ( isVariableChanged ( bodyStart , bodyEnd , varid , globalvar , settings , tokenlist - > isCPP ( ) ) )
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 ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok2 , value1 , settings ) ;
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 ) ) ) ;
2016-12-11 21:19:24 +01:00
if ( ( tok2 - > str ( ) = = " && " & & ! conditionIsTrue ( tok2 - > astOperand1 ( ) , programMemory ) ) | |
( tok2 - > str ( ) = = " || " & & ! conditionIsFalse ( tok2 - > astOperand1 ( ) , programMemory ) ) ) {
2014-08-01 16:12:57 +02:00
// 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
}
2016-01-24 13:11:51 +01:00
else if ( Token : : simpleMatch ( tok2 , " ) { " )) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok2 , " For loop skipping {} code " ) ;
tok2 = tok2 - > linkAt ( 1 ) ;
if ( Token : : simpleMatch ( tok2 , " } else { " ) )
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 ( ) )
2018-04-27 22:36:30 +02:00
endToken = var - > typeStartToken ( ) - > scope ( ) - > bodyEnd ;
2014-08-17 10:40:22 +02:00
else
2018-04-27 22:36:30 +02:00
endToken = fortok - > scope ( ) - > bodyEnd ;
2014-08-17 10:40:22 +02:00
std : : list < ValueFlow : : Value > values ;
2018-04-11 09:44:35 +02:00
values . emplace_back ( num ) ;
values . back ( ) . errorPath . emplace_back ( fortok , " After for loop, " + var - > name ( ) + " has value " + values . back ( ) . infoString ( ) ) ;
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 ,
2016-12-20 11:06:20 +01:00
false ,
2014-08-17 10:40:22 +02:00
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
{
2018-07-14 22:26:22 +02:00
for ( const Scope & scope : symboldatabase - > scopeList ) {
if ( scope . type ! = Scope : : eFor )
2014-03-17 18:43:47 +01:00
continue ;
2014-03-16 08:38:52 +01:00
2018-07-14 22:26:22 +02:00
Token * tok = const_cast < Token * > ( scope . classDef ) ;
Token * const bodyStart = const_cast < Token * > ( scope . bodyStart ) ;
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 ) {
2017-07-09 12:50:17 +02:00
valueFlowForLoopSimplify ( bodyStart , varid , false , num1 , tokenlist , errorLogger , settings ) ;
valueFlowForLoopSimplify ( bodyStart , varid , false , num2 , tokenlist , errorLogger , settings ) ;
2014-12-30 18:50:22 +01:00
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 ) ) {
2016-11-14 06:42:52 +01:00
std : : map < unsigned int , ValueFlow : : Value > : : const_iterator it ;
for ( it = mem1 . values . begin ( ) ; it ! = mem1 . values . end ( ) ; + + it ) {
if ( ! it - > second . isIntValue ( ) )
continue ;
2017-07-09 12:50:17 +02:00
valueFlowForLoopSimplify ( bodyStart , it - > first , false , it - > second . intvalue , tokenlist , errorLogger , settings ) ;
2016-11-14 06:42:52 +01:00
}
for ( it = mem2 . values . begin ( ) ; it ! = mem2 . values . end ( ) ; + + it ) {
if ( ! it - > second . isIntValue ( ) )
continue ;
2017-07-09 12:50:17 +02:00
valueFlowForLoopSimplify ( bodyStart , it - > first , false , it - > second . intvalue , tokenlist , errorLogger , settings ) ;
2016-11-14 06:42:52 +01:00
}
for ( it = memAfter . values . begin ( ) ; it ! = memAfter . values . end ( ) ; + + it ) {
if ( ! it - > second . isIntValue ( ) )
continue ;
valueFlowForLoopSimplifyAfter ( tok , it - > first , it - > second . intvalue , 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 ;
2018-04-27 22:36:30 +02:00
valueFlowForward ( const_cast < Token * > ( functionScope - > bodyStart - > next ( ) ) , functionScope - > bodyEnd , arg , varid2 , argvalues , false , true , tokenlist , errorLogger , settings ) ;
2015-02-01 15:05:00 +01:00
}
2015-07-20 19:45:38 +02:00
static void valueFlowSwitchVariable ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
{
2018-07-14 22:26:22 +02:00
for ( const Scope & scope : symboldatabase - > scopeList ) {
if ( scope . type ! = Scope : : ScopeType : : eSwitch )
2015-07-20 19:45:38 +02:00
continue ;
2018-07-14 22:26:22 +02:00
if ( ! Token : : Match ( scope . classDef , " switch ( %var% ) { " ) )
2015-07-20 19:45:38 +02:00
continue ;
2018-07-14 22:26:22 +02:00
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 ;
}
2018-07-14 22:26:22 +02:00
for ( Token * tok = scope . bodyStart - > next ( ) ; tok ! = scope . bodyEnd ; tok = tok - > next ( ) ) {
2015-07-20 19:45:38 +02:00
if ( tok - > str ( ) = = " { " ) {
tok = tok - > link ( ) ;
continue ;
}
if ( Token : : Match ( tok , " case %num% : " ) ) {
std : : list < ValueFlow : : Value > values ;
2018-04-11 09:44:35 +02:00
values . emplace_back ( MathLib : : toLongNumber ( tok - > next ( ) - > str ( ) ) ) ;
2015-07-29 19:54:57 +02:00
values . back ( ) . condition = tok ;
2017-10-18 22:31:44 +02:00
const std : : string info ( " case " + tok - > next ( ) - > str ( ) + " : " + vartok - > str ( ) + " is " + tok - > next ( ) - > str ( ) + " here. " ) ;
2018-04-11 09:44:35 +02:00
values . back ( ) . errorPath . emplace_back ( tok , info ) ;
2017-10-20 17:31:58 +02:00
bool known = false ;
2017-10-18 22:31:44 +02:00
if ( ( Token : : simpleMatch ( tok - > previous ( ) , " { " ) | | Token : : simpleMatch ( tok - > tokAt ( - 2 ) , " break ; " ) ) & & ! Token : : Match ( tok - > tokAt ( 3 ) , " ;| case " ) )
2017-10-20 17:31:58 +02:00
known = true ;
2015-07-27 14:57:02 +02:00
while ( Token : : Match ( tok - > tokAt ( 3 ) , " ;| case %num% : " ) ) {
2017-10-20 22:11:12 +02:00
known = false ;
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 ( ) ;
2018-04-11 09:44:35 +02:00
values . emplace_back ( MathLib : : toLongNumber ( tok - > next ( ) - > str ( ) ) ) ;
2015-07-29 19:54:57 +02:00
values . back ( ) . condition = tok ;
2017-10-18 22:31:44 +02:00
const std : : string info2 ( " case " + tok - > next ( ) - > str ( ) + " : " + vartok - > str ( ) + " is " + tok - > next ( ) - > str ( ) + " here. " ) ;
2018-04-11 09:44:35 +02:00
values . back ( ) . errorPath . emplace_back ( tok , info2 ) ;
2015-07-20 19:45:38 +02:00
}
for ( std : : list < ValueFlow : : Value > : : const_iterator val = values . begin ( ) ; val ! = values . end ( ) ; + + val ) {
valueFlowReverse ( tokenlist ,
2018-07-14 22:26:22 +02:00
const_cast < Token * > ( scope . classDef ) ,
2015-07-20 19:45:38 +02:00
vartok ,
* val ,
ValueFlow : : Value ( ) ,
errorLogger ,
settings ) ;
}
2017-10-20 17:31:58 +02:00
if ( vartok - > variable ( ) - > scope ( ) ) {
2017-10-20 22:11:12 +02:00
if ( known )
values . back ( ) . setKnown ( ) ;
2018-04-27 22:36:30 +02:00
valueFlowForward ( tok - > tokAt ( 3 ) , vartok - > variable ( ) - > scope ( ) - > bodyEnd , vartok - > variable ( ) , vartok - > varId ( ) , values , values . back ( ) . isKnown ( ) , false , tokenlist , errorLogger , settings ) ;
2017-10-20 22:11:12 +02:00
}
2015-07-20 19:45:38 +02:00
}
}
}
}
2016-11-05 09:59:48 +01:00
static void setTokenValues ( Token * tok , const std : : list < ValueFlow : : Value > & values , const Settings * settings )
2016-10-17 13:05:19 +02:00
{
for ( std : : list < ValueFlow : : Value > : : const_iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
const ValueFlow : : Value & value = * it ;
2016-11-13 22:33:39 +01:00
if ( value . isIntValue ( ) )
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , value , settings ) ;
2016-10-17 13:05:19 +02:00
}
}
static void valueFlowLibraryFunction ( Token * tok , const std : : string & returnValue , const Settings * settings )
{
std : : istringstream istr ( returnValue ) ;
TokenList tokenList ( settings ) ;
if ( ! tokenList . createTokens ( istr ) )
return ;
const Token * arg1 = tok - > astOperand2 ( ) ;
while ( arg1 & & arg1 - > str ( ) = = " , " )
arg1 = arg1 - > astOperand1 ( ) ;
if ( Token : : findsimplematch ( tokenList . front ( ) , " arg1 " ) & & ! arg1 )
return ;
if ( Token : : simpleMatch ( tokenList . front ( ) , " strlen ( arg1 ) " ) & & arg1 ) {
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & value : arg1 - > values ( ) ) {
2016-11-13 22:33:39 +01:00
if ( value . isTokValue ( ) & & value . tokvalue - > tokType ( ) = = Token : : eString ) {
2016-10-17 13:05:19 +02:00
ValueFlow : : Value retval ( value ) ; // copy all "inconclusive", "condition", etc attributes
// set return value..
2016-11-13 22:33:39 +01:00
retval . valueType = ValueFlow : : Value : : INT ;
2016-10-17 13:05:19 +02:00
retval . tokvalue = nullptr ;
retval . intvalue = Token : : getStrLength ( value . tokvalue ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , retval , settings ) ;
2016-10-17 13:05:19 +02:00
}
}
return ;
}
// combine operators, set links, etc..
for ( Token * tok2 = tokenList . front ( ) ; tok2 ; tok2 = tok2 - > next ( ) ) {
if ( Token : : Match ( tok2 , " [!<>=] = " ) ) {
tok2 - > str ( tok2 - > str ( ) + " = " ) ;
tok2 - > deleteNext ( ) ;
}
}
// Evaluate expression
tokenList . createAst ( ) ;
valueFlowNumber ( & tokenList ) ;
for ( Token * tok2 = tokenList . front ( ) ; tok2 ; tok2 = tok2 - > next ( ) ) {
if ( tok2 - > str ( ) = = " arg1 " & & arg1 ) {
2017-03-27 18:48:34 +02:00
setTokenValues ( tok2 , arg1 - > values ( ) , settings ) ;
2016-10-17 13:05:19 +02:00
}
}
// Find result..
for ( const Token * tok2 = tokenList . front ( ) ; tok2 ; tok2 = tok2 - > next ( ) ) {
2017-03-27 18:48:34 +02:00
if ( ! tok2 - > astParent ( ) & & ! tok2 - > values ( ) . empty ( ) ) {
setTokenValues ( tok , tok2 - > values ( ) , settings ) ;
2016-10-17 13:05:19 +02:00
return ;
}
}
}
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 ;
2017-05-20 08:47:35 +02:00
const Function * const calledFunction = tok - > function ( ) ;
if ( ! calledFunction ) {
2016-10-17 13:05:19 +02:00
// library function?
2016-12-05 17:30:06 +01:00
const std : : string & returnValue ( settings - > library . returnValue ( tok ) ) ;
2016-10-17 13:05:19 +02:00
if ( ! returnValue . empty ( ) )
valueFlowLibraryFunction ( tok - > next ( ) , returnValue , settings ) ;
2014-01-06 16:37:52 +01:00
continue ;
2016-10-17 13:05:19 +02:00
}
2014-01-06 16:37:52 +01:00
2017-05-20 08:47:35 +02:00
const Scope * const calledFunctionScope = calledFunction - > functionScope ;
if ( ! calledFunctionScope )
2014-01-06 16:37:52 +01:00
continue ;
2017-05-19 14:34:59 +02:00
const std : : vector < const Token * > & callArguments = getArguments ( tok ) ;
for ( unsigned int argnr = 0U ; argnr < callArguments . size ( ) ; + + argnr ) {
const Token * argtok = callArguments [ argnr ] ;
2014-08-06 06:33:06 +02:00
// Get function argument
2017-05-20 08:47:35 +02:00
const Variable * const argvar = calledFunction - > getArgumentVar ( argnr ) ;
2017-05-19 14:34:59 +02:00
if ( ! argvar )
2014-01-08 06:04:51 +01:00
break ;
2014-08-06 06:33:06 +02:00
// passing value(s) to function
2017-05-19 14:34:59 +02:00
std : : list < ValueFlow : : Value > argvalues ;
if ( Token : : Match ( argtok , " %comp%|%oror%|&&|! " ) & & ! argtok - > hasKnownIntValue ( ) ) {
2018-04-11 09:44:35 +02:00
argvalues . emplace_back ( 0 ) ;
argvalues . emplace_back ( 1 ) ;
2017-05-19 14:34:59 +02:00
} else {
2017-03-27 18:48:34 +02:00
argvalues = argtok - > values ( ) ;
2014-08-06 06:33:06 +02:00
}
2015-07-16 20:49:14 +02:00
2017-05-19 14:34:59 +02:00
if ( argvalues . empty ( ) )
continue ;
2017-05-16 22:38:13 +02:00
// Error path..
2017-05-20 08:47:35 +02:00
for ( std : : list < ValueFlow : : Value > : : iterator it = argvalues . begin ( ) ; it ! = argvalues . end ( ) ; + + it ) {
2018-04-04 21:51:31 +02:00
const std : : string nr = MathLib : : toString ( argnr + 1 ) + getOrdinalText ( argnr + 1 ) ;
2017-05-20 08:47:35 +02:00
2018-04-11 09:44:35 +02:00
it - > errorPath . emplace_back ( argtok ,
" Calling function ' " +
calledFunction - > name ( ) +
" ', " +
nr +
" argument ' " +
argvar - > name ( ) +
" ' value is " +
it - > infoString ( ) ) ;
2017-05-20 18:52:48 +02:00
}
2017-05-15 20:05:11 +02:00
2015-07-16 20:49:14 +02:00
// passed values are not "known"..
2017-11-17 23:04:54 +01:00
changeKnownToPossible ( argvalues ) ;
2015-07-16 20:49:14 +02:00
2017-05-20 08:47:35 +02:00
valueFlowInjectParameter ( tokenlist , errorLogger , settings , argvar , calledFunctionScope , argvalues ) ;
2015-02-01 15:05:00 +01:00
}
}
}
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
2018-07-14 22:26:22 +02:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2015-02-01 15:05:00 +01:00
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% [,)] " ) ) {
2017-03-27 18:48:34 +02:00
const std : : list < ValueFlow : : Value > & values = var - > nameToken ( ) - > tokAt ( 2 ) - > values ( ) ;
2015-07-17 16:05:07 +02:00
std : : list < ValueFlow : : Value > argvalues ;
2018-07-14 22:26:22 +02:00
for ( const ValueFlow : : Value & value : values ) {
ValueFlow : : Value v ( value ) ;
2015-07-17 16:05:07 +02:00
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
}
}
}
}
2016-10-22 12:24:15 +02:00
static bool isKnown ( const Token * tok )
2014-06-29 18:04:38 +02:00
{
2016-10-22 12:24:15 +02:00
return tok & & tok - > hasKnownIntValue ( ) ;
2014-06-29 18:04:38 +02:00
}
2016-11-05 09:59:48 +01:00
static void valueFlowFunctionReturn ( TokenList * tokenlist , ErrorLogger * errorLogger )
2014-06-29 18:04:38 +02:00
{
2016-10-22 17:22:57 +02:00
for ( Token * tok = tokenlist - > back ( ) ; tok ; tok = tok - > previous ( ) ) {
2014-06-29 18:04:38 +02:00
if ( tok - > str ( ) ! = " ( " | | ! tok - > astOperand1 ( ) | | ! tok - > astOperand1 ( ) - > function ( ) )
continue ;
// Arguments..
std : : vector < MathLib : : bigint > parvalues ;
2016-10-20 16:10:55 +02:00
if ( tok - > astOperand2 ( ) ) {
2014-06-29 18:04:38 +02:00
const Token * partok = tok - > astOperand2 ( ) ;
2016-10-22 12:24:15 +02:00
while ( partok & & partok - > str ( ) = = " , " & & isKnown ( partok - > astOperand2 ( ) ) )
2014-06-29 18:04:38 +02:00
partok = partok - > astOperand1 ( ) ;
2016-10-22 12:24:15 +02:00
if ( ! isKnown ( partok ) )
2014-06-29 18:04:38 +02:00
continue ;
2017-03-27 18:48:34 +02:00
parvalues . push_back ( partok - > values ( ) . front ( ) . intvalue ) ;
2014-06-29 18:04:38 +02:00
partok = partok - > astParent ( ) ;
while ( partok & & partok - > str ( ) = = " , " ) {
2017-03-27 18:48:34 +02:00
parvalues . push_back ( partok - > astOperand2 ( ) - > values ( ) . front ( ) . intvalue ) ;
2014-06-29 18:04:38 +02:00
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 ;
2018-04-27 22:36:30 +02:00
if ( ! functionScope | | ! Token : : simpleMatch ( functionScope - > bodyStart , " { return " ) ) {
if ( functionScope & & tokenlist - > getSettings ( ) - > debugwarnings & & Token : : findsimplematch ( functionScope - > bodyStart , " return " , functionScope - > bodyEnd ) )
2015-02-01 12:33:53 +01:00
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% ,|) " ) ) {
2016-11-05 09:59:48 +01:00
if ( tokenlist - > getSettings ( ) - > debugwarnings )
2014-06-29 18:04:38 +02:00
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
}
2016-10-20 16:10:55 +02:00
if ( programMemory . empty ( ) & & ! parvalues . empty ( ) )
2014-06-29 18:04:38 +02:00
continue ;
// Determine return value of subfunction..
MathLib : : bigint result = 0 ;
bool error = false ;
2018-04-27 22:36:30 +02:00
execute ( functionScope - > bodyStart - > next ( ) - > astOperand1 ( ) ,
2014-06-29 18:04:38 +02:00
& programMemory ,
& result ,
& error ) ;
2016-10-20 16:10:55 +02:00
if ( ! error ) {
ValueFlow : : Value v ( result ) ;
2016-12-11 10:42:39 +01:00
if ( function - > isVirtual ( ) )
v . setPossible ( ) ;
else
v . setKnown ( ) ;
2016-11-05 09:59:48 +01:00
setTokenValue ( tok , v , tokenlist - > getSettings ( ) ) ;
2016-10-20 16:10:55 +02:00
}
2014-06-29 18:04:38 +02:00
}
}
2017-04-23 18:05:14 +02:00
static void valueFlowUninit ( TokenList * tokenlist , SymbolDatabase * /*symbolDatabase*/ , ErrorLogger * errorLogger , const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! Token : : Match ( tok , " [;{}] %type% " ) )
continue ;
if ( ! tok - > scope ( ) - > isExecutable ( ) )
continue ;
const Token * vardecl = tok - > next ( ) ;
bool stdtype = false ;
2017-04-24 18:27:16 +02:00
bool pointer = false ;
2017-04-23 18:05:14 +02:00
while ( Token : : Match ( vardecl , " %name%|::|* " ) & & vardecl - > varId ( ) = = 0 ) {
stdtype | = vardecl - > isStandardType ( ) ;
2017-04-24 18:27:16 +02:00
pointer | = vardecl - > str ( ) = = " * " ;
2017-04-23 18:05:14 +02:00
vardecl = vardecl - > next ( ) ;
}
2017-04-28 13:29:09 +02:00
if ( ! stdtype & & ! pointer )
2017-04-24 18:27:16 +02:00
continue ;
2017-04-23 18:05:14 +02:00
if ( ! Token : : Match ( vardecl , " %var% ; " ) )
continue ;
if ( Token : : Match ( vardecl , " %varid% ; %varid% = " , vardecl - > varId ( ) ) )
continue ;
const Variable * var = vardecl - > variable ( ) ;
if ( ! var | | var - > nameToken ( ) ! = vardecl )
continue ;
if ( ( ! var - > isPointer ( ) & & var - > type ( ) & & var - > type ( ) - > needInitialization ! = Type : : True ) | |
2017-04-28 13:29:09 +02:00
! var - > isLocal ( ) | | var - > isStatic ( ) | | var - > isExtern ( ) | | var - > isReference ( ) | | var - > isThrow ( ) )
2017-04-23 18:05:14 +02:00
continue ;
ValueFlow : : Value uninitValue ;
uninitValue . setKnown ( ) ;
uninitValue . valueType = ValueFlow : : Value : : UNINIT ;
std : : list < ValueFlow : : Value > values ;
values . push_back ( uninitValue ) ;
const bool constValue = true ;
const bool subFunction = false ;
2018-04-27 22:36:30 +02:00
valueFlowForward ( vardecl - > next ( ) , vardecl - > scope ( ) - > bodyEnd , var , vardecl - > varId ( ) , values , constValue , subFunction , tokenlist , errorLogger , settings ) ;
2017-04-23 18:05:14 +02:00
}
}
2018-08-11 15:08:54 +02:00
static bool hasContainerSizeGuard ( const Token * tok , unsigned int containerId )
{
for ( ; tok & & tok - > astParent ( ) ; tok = tok - > astParent ( ) ) {
const Token * parent = tok - > astParent ( ) ;
if ( tok ! = parent - > astOperand2 ( ) )
continue ;
if ( ! Token : : Match ( parent , " %oror%|&&|? " ) )
continue ;
// is container found in lhs?
std : : stack < const Token * > tokens ;
tokens . push ( parent - > astOperand1 ( ) ) ;
while ( ! tokens . empty ( ) ) {
const Token * t = tokens . top ( ) ;
tokens . pop ( ) ;
if ( ! t )
continue ;
if ( t - > varId ( ) = = containerId )
return true ;
tokens . push ( t - > astOperand1 ( ) ) ;
tokens . push ( t - > astOperand2 ( ) ) ;
}
}
return false ;
}
2018-08-13 06:54:18 +02:00
static bool isContainerSizeChangedByFunction ( const Token * tok )
{
const Token * parent = tok - > astParent ( ) ;
if ( parent & & parent - > str ( ) = = " & " )
parent = parent - > astParent ( ) ;
while ( parent & & parent - > str ( ) = = " , " )
parent = parent - > astParent ( ) ;
return parent & & Token : : Match ( parent - > previous ( ) , " %name% ( " ) ;
}
2018-08-10 11:29:16 +02:00
static void valueFlowContainerReverse ( const Token * tok , unsigned int containerId , const ValueFlow : : Value & value , const Settings * settings )
{
while ( nullptr ! = ( tok = tok - > previous ( ) ) ) {
if ( Token : : Match ( tok , " [{}] " ) )
break ;
if ( tok - > varId ( ) ! = containerId )
continue ;
2018-08-10 12:26:53 +02:00
if ( Token : : Match ( tok , " %name% = " ) )
break ;
2018-08-13 06:54:18 +02:00
if ( isContainerSizeChangedByFunction ( tok ) )
break ;
2018-08-10 11:29:16 +02:00
if ( ! tok - > valueType ( ) | | ! tok - > valueType ( ) - > container )
2018-08-10 22:06:23 +02:00
break ;
if ( Token : : Match ( tok , " %name% . %name% ( " ) & & tok - > valueType ( ) - > container - > getAction ( tok - > strAt ( 2 ) ) ! = Library : : Container : : Action : : NO_ACTION )
break ;
2018-08-11 15:08:54 +02:00
if ( ! hasContainerSizeGuard ( tok , containerId ) )
setTokenValue ( const_cast < Token * > ( tok ) , value , settings ) ;
2018-08-10 22:06:23 +02:00
}
}
static void valueFlowContainerForward ( const Token * tok , unsigned int containerId , const ValueFlow : : Value & value , const Settings * settings )
{
while ( nullptr ! = ( tok = tok - > next ( ) ) ) {
if ( Token : : Match ( tok , " [{}] " ) )
break ;
if ( tok - > varId ( ) ! = containerId )
2018-08-10 11:29:16 +02:00
continue ;
2018-08-10 22:06:23 +02:00
if ( Token : : Match ( tok , " %name% = " ) )
break ;
2018-08-13 06:54:18 +02:00
if ( isContainerSizeChangedByFunction ( tok ) )
break ;
2018-08-10 22:06:23 +02:00
if ( ! tok - > valueType ( ) | | ! tok - > valueType ( ) - > container )
break ;
2018-08-10 11:53:56 +02:00
if ( Token : : Match ( tok , " %name% . %name% ( " ) & & tok - > valueType ( ) - > container - > getAction ( tok - > strAt ( 2 ) ) ! = Library : : Container : : Action : : NO_ACTION )
break ;
2018-08-11 15:08:54 +02:00
if ( ! hasContainerSizeGuard ( tok , containerId ) )
setTokenValue ( const_cast < Token * > ( tok ) , value , settings ) ;
2018-08-10 11:29:16 +02:00
}
}
2018-08-12 22:23:19 +02:00
static bool isContainerSizeChanged ( unsigned int varId , const Token * start , const Token * end )
{
for ( const Token * tok = start ; tok ! = end ; tok = tok - > next ( ) ) {
if ( tok - > varId ( ) ! = varId )
continue ;
if ( ! tok - > valueType ( ) | | ! tok - > valueType ( ) - > container )
return true ;
if ( Token : : Match ( tok , " %name% = " ) )
return true ;
if ( Token : : Match ( tok , " %name% . %name% ( " ) ) {
Library : : Container : : Action action = tok - > valueType ( ) - > container - > getAction ( tok - > strAt ( 2 ) ) ;
switch ( action ) {
case Library : : Container : : Action : : RESIZE :
case Library : : Container : : Action : : CLEAR :
case Library : : Container : : Action : : PUSH :
case Library : : Container : : Action : : POP :
case Library : : Container : : Action : : CHANGE :
case Library : : Container : : Action : : INSERT :
case Library : : Container : : Action : : ERASE :
case Library : : Container : : Action : : CHANGE_INTERNAL :
return true ;
case Library : : Container : : Action : : NO_ACTION : // might be unknown action
return true ;
case Library : : Container : : Action : : FIND :
case Library : : Container : : Action : : CHANGE_CONTENT :
break ;
} ;
}
}
return false ;
}
2018-08-11 13:49:42 +02:00
static void valueFlowContainerSize ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * /*errorLogger*/ , const Settings * settings )
2018-08-10 11:29:16 +02:00
{
2018-08-12 14:39:29 +02:00
// declaration
for ( const Variable * var : symboldatabase - > variableList ( ) ) {
if ( ! var | | ! var - > isLocal ( ) | | var - > isPointer ( ) | | var - > isReference ( ) )
continue ;
if ( ! var - > valueType ( ) | | ! var - > valueType ( ) - > container )
continue ;
if ( ! Token : : Match ( var - > nameToken ( ) , " %name% ; " ) )
continue ;
ValueFlow : : Value value ( 0 ) ;
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
value . setKnown ( ) ;
valueFlowContainerForward ( var - > nameToken ( ) - > next ( ) , var - > declarationId ( ) , value , settings ) ;
}
2018-08-12 22:01:58 +02:00
// after assignment
for ( const Scope * functionScope : symboldatabase - > functionScopes ) {
for ( const Token * tok = functionScope - > bodyStart ; tok ! = functionScope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( Token : : Match ( tok , " [;{}] %var% = %str% ; " ) ) {
const Token * containerTok = tok - > next ( ) ;
if ( containerTok & & containerTok - > valueType ( ) & & containerTok - > valueType ( ) - > container & & containerTok - > valueType ( ) - > container - > stdStringLike ) {
ValueFlow : : Value value ( Token : : getStrLength ( containerTok - > tokAt ( 2 ) ) ) ;
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
value . setKnown ( ) ;
valueFlowContainerForward ( containerTok - > next ( ) , containerTok - > varId ( ) , value , settings ) ;
}
}
}
}
2018-08-12 14:39:29 +02:00
// conditional conditionSize
2018-08-10 11:29:16 +02:00
for ( const Scope & scope : symboldatabase - > scopeList ) {
if ( scope . type ! = Scope : : ScopeType : : eIf ) // TODO: while
continue ;
for ( const Token * tok = scope . classDef ; tok & & tok - > str ( ) ! = " { " ; tok = tok - > next ( ) ) {
2018-08-10 12:53:16 +02:00
if ( ! tok - > isName ( ) | | ! tok - > valueType ( ) | | tok - > valueType ( ) - > type ! = ValueType : : CONTAINER | | ! tok - > valueType ( ) - > container )
2018-08-10 11:29:16 +02:00
continue ;
if ( ! Token : : Match ( tok , " %name% . %name% ( " ) )
continue ;
2018-08-10 11:53:56 +02:00
const Token * conditionToken ;
2018-08-10 21:42:13 +02:00
MathLib : : bigint intval ;
2018-08-10 11:53:56 +02:00
if ( tok - > valueType ( ) - > container - > getYield ( tok - > strAt ( 2 ) ) = = Library : : Container : : Yield : : SIZE ) {
const Token * parent = tok - > tokAt ( 3 ) - > astParent ( ) ;
if ( ! parent | | ! parent - > isComparisonOp ( ) | | ! parent - > astOperand2 ( ) )
continue ;
2018-08-10 21:42:13 +02:00
if ( parent - > astOperand1 ( ) - > hasKnownIntValue ( ) )
intval = parent - > astOperand1 ( ) - > values ( ) . front ( ) . intvalue ;
else if ( parent - > astOperand2 ( ) - > hasKnownIntValue ( ) )
intval = parent - > astOperand2 ( ) - > values ( ) . front ( ) . intvalue ;
else
2018-08-10 11:53:56 +02:00
continue ;
conditionToken = parent ;
} else if ( tok - > valueType ( ) - > container - > getYield ( tok - > strAt ( 2 ) ) = = Library : : Container : : Yield : : EMPTY ) {
conditionToken = tok - > tokAt ( 3 ) ;
2018-08-10 21:42:13 +02:00
intval = 0 ;
2018-08-10 11:53:56 +02:00
} else {
2018-08-10 11:29:16 +02:00
continue ;
2018-08-10 11:53:56 +02:00
}
2018-08-10 21:42:13 +02:00
ValueFlow : : Value value ( conditionToken , intval ) ;
2018-08-10 11:29:16 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
2018-08-10 22:18:38 +02:00
// possible value before condition
2018-08-10 11:29:16 +02:00
valueFlowContainerReverse ( scope . classDef , tok - > varId ( ) , value , settings ) ;
2018-08-10 22:18:38 +02:00
// possible value after condition
2018-08-11 13:49:42 +02:00
if ( ! isEscapeScope ( scope . bodyStart , tokenlist ) ) {
const Token * after = scope . bodyEnd ;
if ( Token : : simpleMatch ( after , " } else { " ) )
after = isEscapeScope ( after - > tokAt ( 2 ) , tokenlist ) ? nullptr : after - > linkAt ( 2 ) ;
2018-08-12 22:23:19 +02:00
if ( after & & ! isContainerSizeChanged ( tok - > varId ( ) , scope . bodyStart , after ) )
2018-08-11 13:49:42 +02:00
valueFlowContainerForward ( after , tok - > varId ( ) , value , settings ) ;
}
2018-08-10 22:18:38 +02:00
// known value in conditional code
if ( conditionToken - > str ( ) = = " == " | | conditionToken - > str ( ) = = " ( " ) {
2018-08-13 21:27:29 +02:00
const Token * parent = conditionToken - > astParent ( ) ;
while ( parent & & ! Token : : Match ( parent , " !|==|!= " ) )
2018-08-10 22:18:38 +02:00
parent = parent - > astParent ( ) ;
if ( ! parent ) {
value . setKnown ( ) ;
valueFlowContainerForward ( scope . bodyStart , tok - > varId ( ) , value , settings ) ;
}
}
2018-08-10 11:29:16 +02:00
}
}
}
2017-05-16 23:12:35 +02:00
ValueFlow : : Value : : Value ( const Token * c , long long val )
: valueType ( INT ) ,
intvalue ( val ) ,
tokvalue ( nullptr ) ,
floatValue ( 0.0 ) ,
moveKind ( NonMovedVariable ) ,
varvalue ( val ) ,
condition ( c ) ,
varId ( 0U ) ,
conditional ( false ) ,
defaultArg ( false ) ,
valueKind ( ValueKind : : Possible )
{
2018-04-11 09:44:35 +02:00
errorPath . emplace_back ( c , " Assuming that condition ' " + c - > expressionString ( ) + " ' is not redundant " ) ;
2017-05-16 23:12:35 +02:00
}
2017-05-19 16:32:58 +02:00
std : : string ValueFlow : : Value : : infoString ( ) const
{
switch ( valueType ) {
case INT :
2017-05-20 08:47:35 +02:00
return MathLib : : toString ( intvalue ) ;
2017-05-19 16:32:58 +02:00
case TOK :
2017-05-20 08:47:35 +02:00
return tokvalue - > str ( ) ;
2017-05-19 16:32:58 +02:00
case FLOAT :
2017-05-20 08:47:35 +02:00
return MathLib : : toString ( floatValue ) ;
2017-05-19 16:32:58 +02:00
case MOVED :
2017-05-20 08:47:35 +02:00
return " <Moved> " ;
2017-05-19 16:32:58 +02:00
case UNINIT :
2017-05-20 08:47:35 +02:00
return " <Uninit> " ;
2018-08-10 11:29:16 +02:00
case CONTAINER_SIZE :
return " size= " + MathLib : : toString ( intvalue ) ;
2017-05-19 16:32:58 +02:00
} ;
throw InternalError ( nullptr , " Invalid ValueFlow Value type " ) ;
}
2016-07-08 20:39:34 +02:00
const ValueFlow : : Value * ValueFlow : : valueFlowConstantFoldAST ( const Token * expr , const Settings * settings )
2016-05-07 20:18:07 +02:00
{
2017-03-27 18:48:34 +02:00
if ( expr & & expr - > values ( ) . empty ( ) ) {
2016-07-08 20:39:34 +02:00
valueFlowConstantFoldAST ( expr - > astOperand1 ( ) , settings ) ;
valueFlowConstantFoldAST ( expr - > astOperand2 ( ) , settings ) ;
2016-10-18 21:44:02 +02:00
valueFlowSetConstantValue ( expr , settings , true /* TODO: this is a guess */ ) ;
2016-05-08 11:17:10 +02:00
}
2018-07-14 13:09:53 +02:00
return expr & & expr - > hasKnownValue ( ) ? & expr - > values ( ) . front ( ) : nullptr ;
2016-05-07 20:18:07 +02:00
}
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 ( ) )
2017-03-27 18:48:34 +02:00
tok - > clearValueFlow ( ) ;
2014-01-04 20:57:02 +01:00
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 ) ;
2017-09-15 15:58:19 +02:00
valueFlowGlobalStaticVar ( tokenlist , settings ) ;
2014-08-03 20:11:22 +02:00
valueFlowPointerAlias ( tokenlist ) ;
2016-11-05 09:59:48 +01:00
valueFlowFunctionReturn ( tokenlist , errorLogger ) ;
2014-04-14 06:45:39 +02:00
valueFlowBitAnd ( tokenlist ) ;
2016-01-30 20:03:55 +01:00
valueFlowOppositeCondition ( symboldatabase , settings ) ;
2015-02-01 12:10:20 +01:00
valueFlowBeforeCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
2016-11-20 15:14:49 +01:00
valueFlowAfterMove ( tokenlist , symboldatabase , errorLogger , settings ) ;
2015-02-01 12:10:20 +01:00
valueFlowAfterAssign ( tokenlist , symboldatabase , errorLogger , settings ) ;
valueFlowAfterCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
2015-07-20 19:45:38 +02:00
valueFlowSwitchVariable ( tokenlist , symboldatabase , errorLogger , settings ) ;
2017-03-30 22:02:36 +02:00
valueFlowForLoop ( 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 ) ;
2017-04-23 18:05:14 +02:00
valueFlowUninit ( tokenlist , symboldatabase , errorLogger , settings ) ;
2018-08-10 11:29:16 +02:00
if ( tokenlist - > isCPP ( ) )
valueFlowContainerSize ( 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 " ;
}