2014-01-04 20:57:02 +01:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2020-05-10 11:16:32 +02:00
* Copyright ( C ) 2007 - 2020 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
*
2019-09-26 10:32:25 +02:00
* 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 .
2018-05-05 23:03:04 +02:00
*
* 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 ( ) .
2019-09-26 10:32:25 +02:00
* 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 .
2018-05-05 23:03:04 +02:00
*
* Forward / Reverse flow analysis
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*
2019-09-26 10:32:25 +02:00
* In forward value flow analysis we know a value and see what happens when we are stepping the program forward . Like
* normal execution . The valueFlowForwardVariable is used in this analysis .
2018-05-05 23:03:04 +02:00
*
2019-09-26 10:32:25 +02:00
* 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 .
2018-05-05 23:03:04 +02:00
*
*
*/
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"
2020-02-13 16:27:06 +01:00
# include "forwardanalyzer.h"
2017-05-27 04:33:47 +02:00
# include "library.h"
2014-01-04 20:57:02 +01:00
# include "mathlib.h"
2020-02-13 16:27:06 +01:00
# include "path.h"
2017-05-27 04:33:47 +02:00
# include "platform.h"
2019-10-30 17:57:46 +01:00
# include "programmemory.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"
2020-05-10 16:45:45 +02:00
# include "valueptr.h"
2017-05-27 04:33:47 +02:00
# include <algorithm>
2019-09-03 17:15:58 +02:00
# include <cassert>
2017-05-27 04:33:47 +02:00
# 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>
2020-05-21 08:47:48 +02:00
# include <tuple>
2017-05-27 04:33:47 +02:00
# include <vector>
2014-01-06 16:37:52 +01:00
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
{
2020-05-23 07:16:49 +02:00
std : : list < ErrorMessage : : FileLocation > callstack ( 1 , ErrorMessage : : FileLocation ( tok , tokenlist ) ) ;
ErrorMessage errmsg ( callstack , tokenlist - > getSourceFilePath ( ) , Severity : : debug ,
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
2019-08-30 18:32:45 +02:00
static void changeKnownToPossible ( std : : list < ValueFlow : : Value > & values , int indirect = - 1 )
2017-11-17 23:04:54 +01:00
{
2019-08-30 18:32:45 +02:00
for ( ValueFlow : : Value & v : values ) {
if ( indirect > = 0 & & v . indirect ! = indirect )
continue ;
v . changeKnownToPossible ( ) ;
}
2017-11-17 23:04:54 +01:00
}
2019-09-20 15:06:37 +02:00
static void removeImpossible ( std : : list < ValueFlow : : Value > & values , int indirect = - 1 )
{
values . remove_if ( [ & ] ( const ValueFlow : : Value & v ) {
if ( indirect > = 0 & & v . indirect ! = indirect )
return false ;
return v . isImpossible ( ) ;
} ) ;
}
static void lowerToPossible ( std : : list < ValueFlow : : Value > & values , int indirect = - 1 )
{
changeKnownToPossible ( values , indirect ) ;
removeImpossible ( values , indirect ) ;
}
static void changePossibleToKnown ( std : : list < ValueFlow : : Value > & values , int indirect = - 1 )
{
for ( ValueFlow : : Value & v : values ) {
if ( indirect > = 0 & & v . indirect ! = indirect )
continue ;
if ( ! v . isPossible ( ) )
continue ;
if ( v . bound ! = ValueFlow : : Value : : Bound : : Point )
continue ;
v . setKnown ( ) ;
}
}
2019-09-28 20:50:56 +02:00
static void setValueUpperBound ( ValueFlow : : Value & value , bool upper )
2019-09-20 15:06:37 +02:00
{
if ( upper )
value . bound = ValueFlow : : Value : : Bound : : Upper ;
else
value . bound = ValueFlow : : Value : : Bound : : Lower ;
}
2019-09-28 20:50:56 +02:00
static void setValueBound ( ValueFlow : : Value & value , const Token * tok , bool invert )
2019-09-20 15:06:37 +02:00
{
if ( Token : : Match ( tok , " <|<= " ) ) {
setValueUpperBound ( value , ! invert ) ;
} else if ( Token : : Match ( tok , " >|>= " ) ) {
setValueUpperBound ( value , invert ) ;
}
}
2019-09-03 17:15:58 +02:00
static void setConditionalValues ( const Token * tok ,
bool invert ,
MathLib : : bigint value ,
ValueFlow : : Value & true_value ,
ValueFlow : : Value & false_value )
{
if ( Token : : Match ( tok , " ==|!=|>=|<= " ) ) {
true_value = ValueFlow : : Value { tok , value } ;
2019-09-20 15:06:37 +02:00
const char * greaterThan = " >= " ;
const char * lessThan = " <= " ;
if ( invert )
std : : swap ( greaterThan , lessThan ) ;
if ( Token : : simpleMatch ( tok , greaterThan ) ) {
false_value = ValueFlow : : Value { tok , value - 1 } ;
} else if ( Token : : simpleMatch ( tok , lessThan ) ) {
false_value = ValueFlow : : Value { tok , value + 1 } ;
} else {
false_value = ValueFlow : : Value { tok , value } ;
}
} else {
const char * greaterThan = " > " ;
const char * lessThan = " < " ;
if ( invert )
std : : swap ( greaterThan , lessThan ) ;
if ( Token : : simpleMatch ( tok , greaterThan ) ) {
true_value = ValueFlow : : Value { tok , value + 1 } ;
false_value = ValueFlow : : Value { tok , value } ;
} else if ( Token : : simpleMatch ( tok , lessThan ) ) {
true_value = ValueFlow : : Value { tok , value - 1 } ;
false_value = ValueFlow : : Value { tok , value } ;
}
}
setValueBound ( true_value , tok , invert ) ;
setValueBound ( false_value , tok , ! invert ) ;
}
static bool isSaturated ( MathLib : : bigint value )
{
return value = = std : : numeric_limits < MathLib : : bigint > : : max ( ) | | value = = std : : numeric_limits < MathLib : : bigint > : : min ( ) ;
2019-09-03 17:15:58 +02:00
}
2019-10-30 17:57:46 +01:00
const Token * parseCompareInt ( const Token * tok , ValueFlow : : Value & true_value , ValueFlow : : Value & false_value )
2019-09-03 17:15:58 +02:00
{
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
return nullptr ;
2020-05-19 21:03:28 +02:00
if ( tok - > isComparisonOp ( ) ) {
2019-09-03 17:15:58 +02:00
if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) ) {
2019-09-20 15:06:37 +02:00
MathLib : : bigint value = tok - > astOperand1 ( ) - > values ( ) . front ( ) . intvalue ;
if ( isSaturated ( value ) )
return nullptr ;
setConditionalValues ( tok , true , value , true_value , false_value ) ;
2019-09-03 17:15:58 +02:00
return tok - > astOperand2 ( ) ;
} else if ( tok - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
2019-09-20 15:06:37 +02:00
MathLib : : bigint value = tok - > astOperand2 ( ) - > values ( ) . front ( ) . intvalue ;
if ( isSaturated ( value ) )
return nullptr ;
setConditionalValues ( tok , false , value , true_value , false_value ) ;
2019-09-03 17:15:58 +02:00
return tok - > astOperand1 ( ) ;
}
}
return nullptr ;
}
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 ;
2019-01-03 07:05:31 +01:00
if ( tok - > hasKnownIntValue ( ) )
return tok ;
2014-01-19 09:05:48 +01:00
// Is variable protected in LHS..
2018-11-23 17:18:07 +01:00
bool bailout = false ;
visitAstNodes ( tok - > astOperand1 ( ) , [ & ] ( const Token * tok2 ) {
if ( tok2 - > str ( ) = = " . " )
return ChildrenToVisit : : none ;
2015-09-27 13:29:28 +02:00
// A variable is seen..
2019-01-03 07:05:31 +01:00
if ( tok2 ! = valuetok & & tok2 - > variable ( ) & &
( tok2 - > varId ( ) = = valuetok - > varId ( ) | | ( ! tok2 - > variable ( ) - > isArgument ( ) & & ! tok2 - > hasKnownIntValue ( ) ) ) ) {
2015-09-27 13:29:28 +02:00
// TODO: limit this bailout
2018-11-23 17:18:07 +01:00
bailout = true ;
return ChildrenToVisit : : done ;
2015-09-27 13:29:28 +02:00
}
2018-11-23 17:18:07 +01:00
return ChildrenToVisit : : op1_and_op2 ;
} ) ;
if ( bailout )
return tok ;
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
}
2018-11-11 08:04:01 +01:00
static bool isEscapeScope ( const Token * tok , TokenList * tokenlist , bool unknown = false )
2018-11-07 06:49:07 +01:00
{
if ( ! Token : : simpleMatch ( tok , " { " ) )
return false ;
2019-08-26 06:56:29 +02:00
// TODO this search for termTok in all subscopes. It should check the end of the scope.
2018-11-07 06:49:07 +01:00
const Token * termTok = Token : : findmatch ( tok , " return|continue|break|throw|goto " , tok - > link ( ) ) ;
2018-11-12 06:32:02 +01:00
if ( termTok & & termTok - > scope ( ) = = tok - > scope ( ) )
2018-11-11 08:04:01 +01:00
return true ;
std : : string unknownFunction ;
2018-11-12 06:32:02 +01:00
if ( tokenlist & & tokenlist - > getSettings ( ) - > library . isScopeNoReturn ( tok - > link ( ) , & unknownFunction ) )
2018-11-11 08:04:01 +01:00
return unknownFunction . empty ( ) | | unknown ;
return false ;
2018-11-07 06:49:07 +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 ;
}
2019-07-15 13:47:17 +02:00
static ValueFlow : : Value castValue ( ValueFlow : : Value value , const ValueType : : Sign sign , nonneg int bit )
2016-12-20 19:32:21 +01:00
{
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 ;
}
2018-11-02 18:28:32 +01:00
static void combineValueProperties ( const ValueFlow : : Value & value1 , const ValueFlow : : Value & value2 , ValueFlow : : Value * result )
{
if ( value1 . isKnown ( ) & & value2 . isKnown ( ) )
result - > setKnown ( ) ;
2019-09-20 15:06:37 +02:00
else if ( value1 . isImpossible ( ) | | value2 . isImpossible ( ) )
result - > setImpossible ( ) ;
2018-11-02 18:28:32 +01:00
else if ( value1 . isInconclusive ( ) | | value2 . isInconclusive ( ) )
result - > setInconclusive ( ) ;
else
result - > setPossible ( ) ;
result - > condition = value1 . condition ? value1 . condition : value2 . condition ;
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 ;
2019-07-25 17:19:51 +02:00
result - > safe = value1 . safe | | value2 . safe ;
2019-12-26 15:47:53 +01:00
if ( value1 . bound = = ValueFlow : : Value : : Bound : : Point | | value2 . bound = = ValueFlow : : Value : : Bound : : Point ) {
if ( value1 . bound = = ValueFlow : : Value : : Bound : : Upper | | value2 . bound = = ValueFlow : : Value : : Bound : : Upper )
result - > bound = ValueFlow : : Value : : Bound : : Upper ;
if ( value1 . bound = = ValueFlow : : Value : : Bound : : Lower | | value2 . bound = = ValueFlow : : Value : : Bound : : Lower )
result - > bound = ValueFlow : : Value : : Bound : : Lower ;
}
2020-02-17 10:31:08 +01:00
if ( value1 . path ! = value2 . path )
result - > path = - 1 ;
else
result - > path = value1 . path ;
2018-11-02 18:28:32 +01:00
}
2019-01-06 12:21:47 +01:00
static const Token * getCastTypeStartToken ( const Token * parent )
{
// TODO: This might be a generic utility function?
if ( ! parent | | parent - > str ( ) ! = " ( " )
return nullptr ;
if ( ! parent - > astOperand2 ( ) & & Token : : Match ( parent , " ( %name% " ) )
return parent - > next ( ) ;
if ( parent - > astOperand2 ( ) & & Token : : Match ( parent - > astOperand1 ( ) , " const_cast|dynamic_cast|reinterpret_cast|static_cast < " ) )
return parent - > astOperand1 ( ) - > tokAt ( 2 ) ;
return nullptr ;
}
2018-12-27 21:33:01 +01:00
/** Set token value for cast */
static void setTokenValueCast ( Token * parent , const ValueType & valueType , const ValueFlow : : Value & value , const Settings * settings ) ;
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 ;
2020-02-17 10:31:08 +01:00
if ( value . path < 0 )
return ;
2019-06-16 10:09:38 +02:00
Token * parent = tok - > astParent ( ) ;
2015-02-24 15:57:39 +01:00
if ( ! parent )
return ;
2014-01-20 22:26:55 +01:00
2018-11-02 18:28:32 +01:00
if ( value . isContainerSizeValue ( ) ) {
// .empty, .size, +"abc", +'a'
2018-11-02 20:14:34 +01:00
if ( parent - > str ( ) = = " + " ) {
2018-11-02 18:28:32 +01:00
for ( const ValueFlow : : Value & value1 : parent - > astOperand1 ( ) - > values ( ) ) {
for ( const ValueFlow : : Value & value2 : parent - > astOperand2 ( ) - > values ( ) ) {
2020-02-17 10:31:08 +01:00
if ( value1 . path ! = value2 . path )
continue ;
2018-11-02 18:28:32 +01:00
ValueFlow : : Value result ;
result . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
if ( value1 . isContainerSizeValue ( ) & & value2 . isContainerSizeValue ( ) )
result . intvalue = value1 . intvalue + value2 . intvalue ;
else if ( value1 . isContainerSizeValue ( ) & & value2 . isTokValue ( ) & & value2 . tokvalue - > tokType ( ) = = Token : : eString )
result . intvalue = value1 . intvalue + Token : : getStrLength ( value2 . tokvalue ) ;
else if ( value2 . isContainerSizeValue ( ) & & value1 . isTokValue ( ) & & value1 . tokvalue - > tokType ( ) = = Token : : eString )
result . intvalue = Token : : getStrLength ( value1 . tokvalue ) + value2 . intvalue ;
else
continue ;
combineValueProperties ( value1 , value2 , & result ) ;
setTokenValue ( parent , result , settings ) ;
}
}
}
else if ( Token : : Match ( parent , " . %name% ( " ) & & parent - > astParent ( ) = = parent - > tokAt ( 2 ) & & parent - > astOperand1 ( ) & & parent - > astOperand1 ( ) - > valueType ( ) ) {
const Library : : Container * c = parent - > astOperand1 ( ) - > valueType ( ) - > container ;
const Library : : Container : : Yield yields = c ? c - > getYield ( parent - > strAt ( 1 ) ) : Library : : Container : : Yield : : NO_YIELD ;
if ( yields = = Library : : Container : : Yield : : SIZE ) {
ValueFlow : : Value v ( value ) ;
v . valueType = ValueFlow : : Value : : ValueType : : INT ;
2019-06-16 10:09:38 +02:00
setTokenValue ( parent - > astParent ( ) , v , settings ) ;
2018-11-02 18:28:32 +01:00
} else if ( yields = = Library : : Container : : Yield : : EMPTY ) {
ValueFlow : : Value v ( value ) ;
v . intvalue = ! v . intvalue ;
v . valueType = ValueFlow : : Value : : ValueType : : INT ;
2019-06-16 10:09:38 +02:00
setTokenValue ( parent - > astParent ( ) , v , settings ) ;
2018-11-02 18:28:32 +01:00
}
}
return ;
}
2018-11-12 10:08:57 +01:00
if ( value . isLifetimeValue ( ) ) {
2019-10-05 16:32:20 +02:00
if ( ! isLifetimeBorrowed ( parent , settings ) )
return ;
2019-07-16 10:51:07 +02:00
if ( value . lifetimeKind = = ValueFlow : : Value : : LifetimeKind : : Iterator & & astIsIterator ( parent ) ) {
2018-11-12 10:08:17 +01:00
setTokenValue ( parent , value , settings ) ;
2018-11-17 09:41:59 +01:00
} else if ( astIsPointer ( tok ) & & astIsPointer ( parent ) & &
( parent - > isArithmeticalOp ( ) | | Token : : Match ( parent , " ( %type% " ) ) ) {
2018-11-12 10:08:17 +01:00
setTokenValue ( parent , value , settings ) ;
}
return ;
}
2019-08-15 10:44:55 +02:00
if ( value . isUninitValue ( ) ) {
2019-08-17 07:36:41 +02:00
ValueFlow : : Value pvalue = value ;
if ( parent - > isUnaryOp ( " & " ) ) {
pvalue . indirect + + ;
setTokenValue ( parent , pvalue , settings ) ;
} else if ( Token : : Match ( parent , " . %var% " ) & & parent - > astOperand1 ( ) = = tok ) {
2019-08-23 06:23:21 +02:00
if ( parent - > originalName ( ) = = " -> " & & pvalue . indirect > 0 )
2019-08-17 07:36:41 +02:00
pvalue . indirect - - ;
setTokenValue ( parent - > astOperand2 ( ) , pvalue , settings ) ;
2019-08-23 06:23:21 +02:00
} else if ( Token : : Match ( parent - > astParent ( ) , " . %var% " ) & & parent - > astParent ( ) - > astOperand1 ( ) = = parent ) {
if ( parent - > astParent ( ) - > originalName ( ) = = " -> " & & pvalue . indirect > 0 )
pvalue . indirect - - ;
setTokenValue ( parent - > astParent ( ) - > astOperand2 ( ) , pvalue , settings ) ;
2019-08-17 07:36:41 +02:00
} else if ( parent - > isUnaryOp ( " * " ) & & pvalue . indirect > 0 ) {
pvalue . indirect - - ;
setTokenValue ( parent , pvalue , settings ) ;
}
2019-08-15 10:44:55 +02:00
return ;
}
2018-12-27 21:33:01 +01:00
// cast..
2019-01-06 12:21:47 +01:00
if ( const Token * castType = getCastTypeStartToken ( parent ) ) {
2019-09-20 15:06:37 +02:00
if ( astIsPointer ( tok ) & & value . valueType = = ValueFlow : : Value : : INT & &
Token : : simpleMatch ( parent - > astOperand1 ( ) , " dynamic_cast " ) )
return ;
2019-01-06 12:21:47 +01:00
const ValueType & valueType = ValueType : : parseDecl ( castType , settings ) ;
2018-12-27 21:33:01 +01:00
setTokenValueCast ( parent , valueType , 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?
2019-07-15 13:47:17 +02:00
int varId = 0 ;
2018-11-23 19:16:19 +01:00
bool ret = false ;
visitAstNodes ( parent - > astOperand1 ( ) ,
[ & ] ( const Token * t ) {
2016-08-15 14:19:35 +02:00
if ( t - > varId ( ) ) {
if ( varId > 0 | | value . varId ! = 0U )
2018-11-23 19:16:19 +01:00
ret = true ;
2016-08-15 14:19:35 +02:00
varId = t - > varId ( ) ;
} else if ( t - > str ( ) = = " ( " & & Token : : Match ( t - > previous ( ) , " %name% " ) )
2018-11-23 19:16:19 +01:00
ret = true ; // function call
return ret ? ChildrenToVisit : : done : ChildrenToVisit : : op1_and_op2 ;
} ) ;
if ( ret )
return ;
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 ( ) ) {
2016-01-26 10:07:42 +01:00
2019-09-24 08:15:03 +02:00
const bool noninvertible = parent - > isComparisonOp ( ) | | Token : : Match ( parent , " %|/|&|%or% " ) ;
2019-09-20 15:06:37 +02:00
2019-09-21 19:53:54 +02:00
// Skip operators with impossible values that are not invertible
2019-09-24 08:15:03 +02:00
if ( noninvertible & & value . isImpossible ( ) )
2019-09-21 19:53:54 +02:00
return ;
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 ( ) ) {
2019-09-24 08:15:03 +02:00
if ( noninvertible & & value1 . isImpossible ( ) )
continue ;
2018-04-24 18:02:36 +02:00
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 ( ) ) {
2020-02-17 10:31:08 +01:00
if ( value1 . path ! = value2 . path )
continue ;
2019-09-24 08:15:03 +02:00
if ( noninvertible & & value2 . isImpossible ( ) )
continue ;
2018-04-24 18:02:36 +02:00
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-12-29 09:31:21 +01:00
if ( value1 . isKnown ( ) | | value2 . isKnown ( ) | | 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-11-02 18:28:32 +01:00
combineValueProperties ( value1 , value2 , & result ) ;
2018-04-24 18:02:36 +02:00
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
}
2019-12-26 15:47:53 +01:00
// If the bound comes from the second value then invert the bound
if ( value2 . bound = = result . bound & & value2 . bound ! = ValueFlow : : Value : : Bound : : Point )
result . invertBound ( ) ;
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 ;
2019-07-15 13:47:17 +02:00
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 ;
2019-12-26 15:47:53 +01:00
v . invertBound ( ) ;
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
}
}
}
}
}
}
2019-10-19 21:08:59 +02:00
else if ( Token : : Match ( parent , " :: %name% " ) & & parent - > astOperand2 ( ) = = tok ) {
setTokenValue ( parent , value , settings ) ;
}
2014-01-18 19:30:44 +01:00
}
2018-12-27 21:33:01 +01:00
static void setTokenValueCast ( Token * parent , const ValueType & valueType , const ValueFlow : : Value & value , const Settings * 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 ) ;
else if ( value . isIntValue ( ) ) {
const long long charMax = settings - > signedCharMax ( ) ;
const long long charMin = settings - > signedCharMin ( ) ;
if ( charMin < = value . intvalue & & value . intvalue < = charMax ) {
// unknown type, but value is small so there should be no truncation etc
setTokenValue ( parent , value , settings ) ;
}
}
}
2019-07-15 13:47:17 +02:00
static nonneg 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
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
size_t ValueFlow : : getSizeOf ( const ValueType & vt , const Settings * settings )
2019-03-11 20:32:24 +01:00
{
if ( vt . pointer )
return settings - > sizeof_pointer ;
else if ( vt . type = = ValueType : : Type : : CHAR )
return 1 ;
else if ( vt . type = = ValueType : : Type : : SHORT )
return settings - > sizeof_short ;
2019-05-01 16:34:28 +02:00
else if ( vt . type = = ValueType : : Type : : WCHAR_T )
return settings - > sizeof_wchar_t ;
2019-03-11 20:32:24 +01:00
else if ( vt . type = = ValueType : : Type : : INT )
return settings - > sizeof_int ;
else if ( vt . type = = ValueType : : Type : : LONG )
return settings - > sizeof_long ;
else if ( vt . type = = ValueType : : Type : : LONGLONG )
return settings - > sizeof_long_long ;
else if ( vt . type = = ValueType : : Type : : FLOAT )
return settings - > sizeof_float ;
else if ( vt . type = = ValueType : : Type : : DOUBLE )
return settings - > sizeof_double ;
return 0 ;
}
2016-05-07 20:18:07 +02:00
// Handle various constants..
2019-06-16 10:09:38 +02:00
static Token * valueFlowSetConstantValue ( 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 ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( 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 ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( 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 ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( 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 ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( 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 ) ;
}
2019-04-06 06:55:46 +02:00
if ( Token : : simpleMatch ( tok , " sizeof ( * " ) ) {
2019-03-11 20:32:24 +01:00
const ValueType * vt = tok - > tokAt ( 2 ) - > valueType ( ) ;
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
const size_t sz = vt ? ValueFlow : : getSizeOf ( * vt , settings ) : 0 ;
2019-03-11 20:32:24 +01:00
if ( sz > 0 ) {
ValueFlow : : Value value ( sz ) ;
if ( ! tok2 - > isTemplateArg ( ) & & settings - > platformType ! = cppcheck : : Platform : : Unspecified )
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok - > next ( ) , value , settings ) ;
2019-03-11 20:32:24 +01:00
}
} else if ( tok2 - > enumerator ( ) & & tok2 - > enumerator ( ) - > scope ) {
2016-07-08 20:39:34 +02:00
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 ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
setTokenValue ( 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 ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
setTokenValue ( 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 ) ;
2019-07-15 13:47:17 +02:00
const int varid1 = sz1 - > varId ( ) ;
2016-12-18 14:03:48 +01:00
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 ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok - > tokAt ( 4 ) , value , settings ) ;
2016-12-18 14:03:48 +01:00
}
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 ) ;
2019-03-13 18:31:41 +01:00
} else if ( var - > valueType ( ) ) {
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
size = ValueFlow : : getSizeOf ( * var - > valueType ( ) , settings ) ;
2018-03-23 08:28:12 +01:00
} 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 ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
setTokenValue ( tok - > next ( ) , value , settings ) ;
2018-03-23 08:28:12 +01:00
}
}
2019-10-20 21:02:28 +02:00
} else if ( tok2 - > tokType ( ) = = Token : : eString ) {
size_t sz = Token : : getStrSize ( tok2 , settings ) ;
if ( sz > 0 ) {
ValueFlow : : Value value ( sz ) ;
value . setKnown ( ) ;
setTokenValue ( const_cast < Token * > ( tok - > next ( ) ) , value , settings ) ;
}
} else if ( tok2 - > tokType ( ) = = Token : : eChar ) {
2019-10-31 14:06:34 +01:00
nonneg int sz = 0 ;
if ( cpp & & settings - > standards . cpp > = Standards : : CPP20 & & tok2 - > isUtf8 ( ) )
sz = 1 ;
else if ( tok2 - > isUtf16 ( ) )
sz = 2 ;
else if ( tok2 - > isUtf32 ( ) )
sz = 4 ;
else if ( tok2 - > isLong ( ) )
sz = settings - > sizeof_wchar_t ;
else if ( ( tok2 - > isCChar ( ) & & ! cpp ) | | ( tok2 - > isCMultiChar ( ) ) )
sz = settings - > sizeof_int ;
else
sz = 1 ;
2019-10-20 21:02:28 +02:00
if ( sz > 0 ) {
ValueFlow : : Value value ( sz ) ;
value . setKnown ( ) ;
setTokenValue ( tok - > next ( ) , value , settings ) ;
}
2016-12-18 14:03:48 +01:00
} else if ( ! tok2 - > type ( ) ) {
const ValueType & vt = ValueType : : parseDecl ( tok2 , settings ) ;
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
const size_t sz = ValueFlow : : getSizeOf ( vt , settings ) ;
2019-03-11 20:32:24 +01:00
if ( sz > 0 ) {
ValueFlow : : Value value ( sz ) ;
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 ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( tok - > next ( ) , value , settings ) ;
2016-12-18 14:03:48 +01:00
}
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 )
{
2019-07-15 13:47:17 +02:00
std : : map < int , const Token * > constantArrays ;
2015-02-24 15:57:39 +01:00
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2015-08-28 14:27:56 +02:00
if ( tok - > varId ( ) > 0U ) {
2018-12-17 06:07:34 +01:00
// array
2019-07-15 13:47:17 +02:00
const std : : map < int , const Token * > : : const_iterator it = constantArrays . find ( tok - > varId ( ) ) ;
2015-08-28 14:27:56 +02:00
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
}
}
2019-01-21 20:05:35 +01:00
static bool isNonZero ( const Token * tok )
{
return tok & & ( ! tok - > hasKnownIntValue ( ) | | tok - > values ( ) . front ( ) . intvalue ! = 0 ) ;
}
static const Token * getOtherOperand ( const Token * tok )
{
if ( ! tok )
return nullptr ;
if ( ! tok - > astParent ( ) )
return nullptr ;
if ( tok - > astParent ( ) - > astOperand1 ( ) ! = tok )
return tok - > astParent ( ) - > astOperand1 ( ) ;
if ( tok - > astParent ( ) - > astOperand2 ( ) ! = tok )
return tok - > astParent ( ) - > astOperand2 ( ) ;
return nullptr ;
}
static void valueFlowArrayBool ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > hasKnownIntValue ( ) )
continue ;
const Variable * var = nullptr ;
bool known = false ;
std : : list < ValueFlow : : Value > : : const_iterator val =
std : : find_if ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , std : : mem_fn ( & ValueFlow : : Value : : isTokValue ) ) ;
if ( val = = tok - > values ( ) . end ( ) ) {
var = tok - > variable ( ) ;
known = true ;
} else {
var = val - > tokvalue - > variable ( ) ;
known = val - > isKnown ( ) ;
}
if ( ! var )
continue ;
if ( ! var - > isArray ( ) | | var - > isArgument ( ) | | var - > isStlType ( ) )
continue ;
if ( isNonZero ( getOtherOperand ( tok ) ) & & Token : : Match ( tok - > astParent ( ) , " %comp% " ) )
continue ;
// TODO: Check for function argument
if ( ( astIsBool ( tok - > astParent ( ) ) & & ! Token : : Match ( tok - > astParent ( ) , " (|%name% " ) ) | |
( tok - > astParent ( ) & & Token : : Match ( tok - > astParent ( ) - > previous ( ) , " if|while|for ( " ) ) ) {
ValueFlow : : Value value { 1 } ;
if ( known )
value . setKnown ( ) ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
}
}
}
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
}
}
2019-08-12 12:58:53 +02:00
static void valueFlowPointerAliasDeref ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > isUnaryOp ( " * " ) )
continue ;
if ( ! astIsPointer ( tok - > astOperand1 ( ) ) )
continue ;
const Token * lifeTok = nullptr ;
ErrorPath errorPath ;
for ( const ValueFlow : : Value & v : tok - > astOperand1 ( ) - > values ( ) ) {
if ( ! v . isLocalLifetimeValue ( ) )
continue ;
lifeTok = v . tokvalue ;
errorPath = v . errorPath ;
}
if ( ! lifeTok )
continue ;
if ( lifeTok - > varId ( ) = = 0 )
continue ;
const Variable * var = lifeTok - > variable ( ) ;
if ( ! var )
continue ;
if ( ! var - > isConst ( ) & & isVariableChanged ( lifeTok - > next ( ) , tok , lifeTok - > varId ( ) , ! var - > isLocal ( ) , tokenlist - > getSettings ( ) , tokenlist - > isCPP ( ) ) )
continue ;
for ( const ValueFlow : : Value & v : lifeTok - > values ( ) ) {
if ( v . isLifetimeValue ( ) )
continue ;
ValueFlow : : Value value = v ;
value . errorPath . insert ( value . errorPath . begin ( ) , errorPath . begin ( ) , errorPath . end ( ) ) ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
}
}
}
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
}
}
}
2018-12-16 07:35:27 +01:00
static void valueFlowSameExpressions ( TokenList * tokenlist )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > hasKnownValue ( ) )
continue ;
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
if ( tok - > astOperand1 ( ) - > isLiteral ( ) | | tok - > astOperand2 ( ) - > isLiteral ( ) )
continue ;
2019-01-11 08:39:23 +01:00
if ( ! astIsIntegral ( tok - > astOperand1 ( ) , false ) & & ! astIsIntegral ( tok - > astOperand2 ( ) , false ) )
2018-12-16 07:35:27 +01:00
continue ;
ValueFlow : : Value val ;
if ( Token : : Match ( tok , " ==|>=|<=|/ " ) ) {
val = ValueFlow : : Value ( 1 ) ;
val . setKnown ( ) ;
}
if ( Token : : Match ( tok , " !=|>|<|%|- " ) ) {
val = ValueFlow : : Value ( 0 ) ;
val . setKnown ( ) ;
}
if ( ! val . isKnown ( ) )
continue ;
2018-12-28 12:59:05 +01:00
if ( isSameExpression ( tokenlist - > isCPP ( ) , false , tok - > astOperand1 ( ) , tok - > astOperand2 ( ) , tokenlist - > getSettings ( ) - > library , true , true , & val . errorPath ) ) {
2018-12-16 07:35:27 +01:00
setTokenValue ( tok , val , tokenlist - > getSettings ( ) ) ;
}
}
}
2019-08-05 07:18:06 +02:00
static void valueFlowTerminatingCondition ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2018-11-07 06:49:07 +01:00
{
const bool cpp = symboldatabase - > isCPP ( ) ;
typedef std : : pair < const Token * , const Scope * > Condition ;
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2019-08-05 07:18:06 +02:00
bool skipFunction = false ;
2018-11-07 06:49:07 +01:00
std : : vector < Condition > conds ;
for ( const Token * tok = scope - > bodyStart ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2019-08-05 07:18:06 +02:00
if ( tok - > isIncompleteVar ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok , " Skipping function due to incomplete variable " + tok - > str ( ) ) ;
skipFunction = true ;
break ;
}
2018-11-07 06:49:07 +01:00
if ( ! Token : : simpleMatch ( tok , " if ( " ) )
continue ;
// Skip known values
if ( tok - > next ( ) - > hasKnownValue ( ) )
continue ;
const Token * condTok = tok - > next ( ) ;
if ( ! Token : : simpleMatch ( condTok - > link ( ) , " ) { " ) )
continue ;
const Token * blockTok = condTok - > link ( ) - > tokAt ( 1 ) ;
// Check if the block terminates early
2018-11-10 21:30:01 +01:00
if ( ! isEscapeScope ( blockTok , tokenlist ) )
2018-11-07 06:49:07 +01:00
continue ;
// Check if any variables are modified in scope
bool bail = false ;
2018-11-10 21:30:01 +01:00
for ( const Token * tok2 = condTok - > next ( ) ; tok2 ! = condTok - > link ( ) ; tok2 = tok2 - > next ( ) ) {
2018-11-07 06:49:07 +01:00
const Variable * var = tok2 - > variable ( ) ;
2018-11-10 21:30:01 +01:00
if ( ! var )
2018-11-07 06:49:07 +01:00
continue ;
2018-11-14 19:10:52 +01:00
if ( ! var - > scope ( ) )
2018-11-14 06:23:45 +01:00
continue ;
2018-11-07 06:49:07 +01:00
const Token * endToken = var - > scope ( ) - > bodyEnd ;
2018-11-10 21:30:01 +01:00
if ( ! var - > isLocal ( ) & & ! var - > isConst ( ) & & ! var - > isArgument ( ) ) {
2018-11-07 06:49:07 +01:00
bail = true ;
break ;
}
2018-11-10 21:30:01 +01:00
if ( var - > isStatic ( ) & & ! var - > isConst ( ) ) {
2018-11-07 06:49:07 +01:00
bail = true ;
break ;
}
2018-11-10 21:30:01 +01:00
if ( ! var - > isConst ( ) & & var - > declEndToken ( ) & & isVariableChanged ( var - > declEndToken ( ) - > next ( ) , endToken , tok2 - > varId ( ) , false , settings , cpp ) ) {
2018-11-07 06:49:07 +01:00
bail = true ;
break ;
}
}
2018-11-10 21:30:01 +01:00
if ( bail )
2018-11-07 06:49:07 +01:00
continue ;
// TODO: Handle multiple conditions
2018-11-10 21:30:01 +01:00
if ( Token : : Match ( condTok - > astOperand2 ( ) , " %oror%|%or%|&|&& " ) )
2018-11-07 06:49:07 +01:00
continue ;
const Scope * condScope = nullptr ;
2018-11-10 21:30:01 +01:00
for ( const Scope * parent = condTok - > scope ( ) ; parent ; parent = parent - > nestedIn ) {
if ( parent - > type = = Scope : : eIf | |
2018-11-09 06:09:51 +01:00
parent - > type = = Scope : : eWhile | |
2018-11-07 06:49:07 +01:00
parent - > type = = Scope : : eSwitch ) {
condScope = parent ;
break ;
}
}
conds . emplace_back ( condTok - > astOperand2 ( ) , condScope ) ;
}
2019-08-05 07:18:06 +02:00
if ( skipFunction )
break ;
2018-11-10 21:30:01 +01:00
for ( Condition cond : conds ) {
if ( ! cond . first )
2018-11-07 06:49:07 +01:00
continue ;
2018-11-27 06:43:59 +01:00
Token * const startToken = cond . first - > findExpressionStartEndTokens ( ) . second - > next ( ) ;
for ( Token * tok = startToken ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2018-11-07 06:49:07 +01:00
if ( ! Token : : Match ( tok , " %comp% " ) )
continue ;
// Skip known values
if ( tok - > hasKnownValue ( ) )
continue ;
2018-11-10 21:30:01 +01:00
if ( cond . second ) {
2018-11-07 06:49:07 +01:00
bool bail = true ;
2018-11-10 21:30:01 +01:00
for ( const Scope * parent = tok - > scope ( ) - > nestedIn ; parent ; parent = parent - > nestedIn ) {
if ( parent = = cond . second ) {
2018-11-07 06:49:07 +01:00
bail = false ;
break ;
}
}
2018-11-10 21:30:01 +01:00
if ( bail )
2018-11-07 06:49:07 +01:00
continue ;
}
ErrorPath errorPath ;
2018-11-12 06:08:57 +01:00
if ( isOppositeCond ( true , cpp , tok , cond . first , settings - > library , true , true , & errorPath ) ) {
2018-11-07 06:49:07 +01:00
ValueFlow : : Value val ( 1 ) ;
val . setKnown ( ) ;
val . condition = cond . first ;
val . errorPath = errorPath ;
val . errorPath . emplace_back ( cond . first , " Assuming condition ' " + cond . first - > expressionString ( ) + " ' is false " ) ;
setTokenValue ( tok , val , tokenlist - > getSettings ( ) ) ;
2018-11-12 06:08:57 +01:00
} else if ( isSameExpression ( cpp , true , tok , cond . first , settings - > library , true , true , & errorPath ) ) {
2018-11-07 06:49:07 +01:00
ValueFlow : : Value val ( 0 ) ;
val . setKnown ( ) ;
val . condition = cond . first ;
val . errorPath = errorPath ;
val . errorPath . emplace_back ( cond . first , " Assuming condition ' " + cond . first - > expressionString ( ) + " ' is false " ) ;
setTokenValue ( tok , val , tokenlist - > getSettings ( ) ) ;
}
}
}
}
}
2018-11-03 23:25:46 +01:00
static bool getExpressionRange ( const Token * expr , MathLib : : bigint * minvalue , MathLib : : bigint * maxvalue )
{
if ( expr - > hasKnownIntValue ( ) ) {
if ( minvalue )
* minvalue = expr - > values ( ) . front ( ) . intvalue ;
if ( maxvalue )
* maxvalue = expr - > values ( ) . front ( ) . intvalue ;
return true ;
}
if ( expr - > str ( ) = = " & " & & expr - > astOperand1 ( ) & & expr - > astOperand2 ( ) ) {
MathLib : : bigint vals [ 4 ] ;
bool lhsHasKnownRange = getExpressionRange ( expr - > astOperand1 ( ) , & vals [ 0 ] , & vals [ 1 ] ) ;
bool rhsHasKnownRange = getExpressionRange ( expr - > astOperand2 ( ) , & vals [ 2 ] , & vals [ 3 ] ) ;
if ( ! lhsHasKnownRange & & ! rhsHasKnownRange )
return false ;
if ( ! lhsHasKnownRange | | ! rhsHasKnownRange ) {
if ( minvalue )
* minvalue = lhsHasKnownRange ? vals [ 0 ] : vals [ 2 ] ;
if ( maxvalue )
* maxvalue = lhsHasKnownRange ? vals [ 1 ] : vals [ 3 ] ;
} else {
if ( minvalue )
* minvalue = vals [ 0 ] & vals [ 2 ] ;
if ( maxvalue )
* maxvalue = vals [ 1 ] & vals [ 3 ] ;
}
return true ;
}
if ( expr - > str ( ) = = " % " & & expr - > astOperand1 ( ) & & expr - > astOperand2 ( ) ) {
MathLib : : bigint vals [ 4 ] ;
if ( ! getExpressionRange ( expr - > astOperand2 ( ) , & vals [ 2 ] , & vals [ 3 ] ) )
return false ;
if ( vals [ 2 ] < = 0 )
return false ;
bool lhsHasKnownRange = getExpressionRange ( expr - > astOperand1 ( ) , & vals [ 0 ] , & vals [ 1 ] ) ;
if ( lhsHasKnownRange & & vals [ 0 ] < 0 )
return false ;
// If lhs has unknown value, it must be unsigned
if ( ! lhsHasKnownRange & & ( ! expr - > astOperand1 ( ) - > valueType ( ) | | expr - > astOperand1 ( ) - > valueType ( ) - > sign ! = ValueType : : Sign : : UNSIGNED ) )
return false ;
if ( minvalue )
* minvalue = 0 ;
if ( maxvalue )
* maxvalue = vals [ 3 ] - 1 ;
return true ;
}
return false ;
}
2019-01-01 14:15:50 +01:00
static void valueFlowRightShift ( TokenList * tokenList , const Settings * settings )
2018-11-03 23:25:46 +01:00
{
for ( Token * tok = tokenList - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) ! = " >> " )
continue ;
if ( tok - > hasKnownValue ( ) )
continue ;
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
continue ;
if ( ! tok - > astOperand2 ( ) - > hasKnownValue ( ) )
continue ;
2018-11-04 17:13:23 +01:00
const MathLib : : bigint rhsvalue = tok - > astOperand2 ( ) - > values ( ) . front ( ) . intvalue ;
if ( rhsvalue < 0 )
continue ;
2018-11-03 23:25:46 +01:00
if ( ! tok - > astOperand1 ( ) - > valueType ( ) | | ! tok - > astOperand1 ( ) - > valueType ( ) - > isIntegral ( ) )
continue ;
if ( ! tok - > astOperand2 ( ) - > valueType ( ) | | ! tok - > astOperand2 ( ) - > valueType ( ) - > isIntegral ( ) )
continue ;
MathLib : : bigint lhsmax = 0 ;
if ( ! getExpressionRange ( tok - > astOperand1 ( ) , nullptr , & lhsmax ) )
continue ;
if ( lhsmax < 0 )
continue ;
2019-01-01 14:15:50 +01:00
int lhsbits ;
if ( ( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : CHAR ) | |
( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : SHORT ) | |
2019-05-01 16:34:28 +02:00
( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : WCHAR_T ) | |
2019-01-01 14:15:50 +01:00
( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : BOOL ) | |
( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : INT ) )
lhsbits = settings - > int_bit ;
else if ( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : LONG )
lhsbits = settings - > long_bit ;
else if ( tok - > astOperand1 ( ) - > valueType ( ) - > type = = ValueType : : Type : : LONGLONG )
lhsbits = settings - > long_long_bit ;
else
continue ;
if ( rhsvalue > = lhsbits | | rhsvalue > = MathLib : : bigint_bits | | ( 1ULL < < rhsvalue ) < = lhsmax )
2018-11-03 23:25:46 +01:00
continue ;
ValueFlow : : Value val ( 0 ) ;
val . setKnown ( ) ;
setTokenValue ( tok , val , tokenList - > getSettings ( ) ) ;
}
}
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 ;
2019-06-16 10:09:38 +02:00
Token * ifOpenBraceTok = tok2 - > tokAt ( 4 ) ;
Token * cond2 = ifOpenBraceTok - > astOperand2 ( ) ;
2016-01-30 20:03:55 +01:00
if ( ! cond2 | | ! cond2 - > isComparisonOp ( ) )
continue ;
2018-09-28 08:38:24 +02:00
if ( isOppositeCond ( true , cpp , cond1 , cond2 , settings - > library , true , true ) ) {
2016-01-30 20:03:55 +01:00
ValueFlow : : Value value ( 1 ) ;
value . setKnown ( ) ;
2019-06-16 10:09:38 +02:00
setTokenValue ( 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
}
}
}
2019-04-21 06:54:32 +02:00
static void valueFlowGlobalConstVar ( 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 ( ) & &
2019-05-14 08:58:27 +02:00
! tok - > variable ( ) - > isVolatile ( ) & &
! tok - > variable ( ) - > isArgument ( ) & &
2019-04-21 06:54:32 +02:00
tok - > variable ( ) - > isConst ( ) & &
tok - > valueType ( ) & &
tok - > valueType ( ) - > isIntegral ( ) & &
tok - > valueType ( ) - > pointer = = 0 & &
tok - > valueType ( ) - > constness = = 1 & &
Token : : Match ( tok , " %name% = " ) & &
tok - > next ( ) - > astOperand2 ( ) & &
tok - > next ( ) - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
vars [ tok - > variable ( ) ] = tok - > next ( ) - > astOperand2 ( ) - > values ( ) . front ( ) ;
}
}
// 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 ) ;
}
}
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 ) ;
}
}
2019-10-03 09:58:57 +02:00
static void valueFlowForward ( Token * startToken ,
const Token * endToken ,
const Token * exprTok ,
std : : list < ValueFlow : : Value > values ,
const bool constValue ,
const bool subFunction ,
TokenList * const tokenlist ,
ErrorLogger * const errorLogger ,
const Settings * 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 ;
2019-07-15 13:47:17 +02:00
const int varid = varToken - > varId ( ) ;
2015-07-20 19:45:38 +02:00
const Token * const startToken = var - > nameToken ( ) ;
for ( Token * tok2 = tok - > previous ( ) ; ; tok2 = tok2 - > previous ( ) ) {
2019-11-29 09:45:02 +01:00
if ( ! tok2 | | tok2 = = startToken | |
( tok2 - > str ( ) = = " { " & &
( tok2 - > scope ( ) - > type = = Scope : : ScopeType : : eFunction | | tok2 - > scope ( ) - > type = = Scope : : ScopeType : : eLambda ) ) ) {
2015-07-20 19:45:38 +02:00
break ;
}
if ( tok2 - > varId ( ) = = varid ) {
2019-10-03 09:58:57 +02:00
if ( tok2 - > hasKnownValue ( ) )
break ;
2015-07-20 19:45:38 +02:00
// bailout: assignment
if ( Token : : Match ( tok2 - > previous ( ) , " !!* %name% = " ) ) {
2019-10-03 09:58:57 +02:00
Token * assignTok = const_cast < Token * > ( tok2 - > next ( ) - > astOperand2 ( ) ) ;
if ( ! assignTok - > hasKnownValue ( ) ) {
setTokenValue ( assignTok , val , settings ) ;
2019-11-08 08:11:41 +01:00
const std : : string info = " Assignment from ' " + assignTok - > expressionString ( ) + " ' " ;
val . errorPath . emplace_back ( assignTok , info ) ;
std : : list < ValueFlow : : Value > values = { val } ;
2019-10-03 09:58:57 +02:00
if ( val2 . condition ) {
2019-11-08 08:11:41 +01:00
val2 . errorPath . emplace_back ( assignTok , info ) ;
2019-10-03 09:58:57 +02:00
setTokenValue ( assignTok , val2 , settings ) ;
values . push_back ( val2 ) ;
}
const Token * startForwardToken = nextAfterAstRightmostLeaf ( tok2 - > next ( ) ) ;
const Token * endForwardToken = tok - > scope ( ) ? tok - > scope ( ) - > bodyEnd : tok ;
valueFlowForward ( const_cast < Token * > ( startForwardToken ) ,
endForwardToken ,
assignTok ,
values ,
false ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2019-11-30 09:22:03 +01:00
// Only reverse analysis supported with variables
if ( assignTok - > varId ( ) > 0 )
valueFlowReverse ( tokenlist , tok2 - > previous ( ) , assignTok , val , val2 , errorLogger , settings ) ;
2019-10-03 09:58:57 +02:00
}
2015-07-20 19:45:38 +02:00
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 ;
2019-08-30 18:32:45 +02:00
if ( isVariableChangedByFunctionCall ( tok2 , std : : max ( val . indirect , val2 . indirect ) , 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 ;
}
2019-09-29 21:23:19 +02:00
// Impossible values can't be inconclusive
2019-09-20 15:06:37 +02:00
if ( val . isImpossible ( ) | | val2 . isImpossible ( ) )
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 ( ) = = " } " ) {
2020-05-21 08:47:48 +02:00
const Token * condTok = getCondTokFromEnd ( tok2 ) ;
// Evaluate condition of for and while loops first
if ( condTok & & condTok - > astTop ( ) & & Token : : Match ( condTok - > astTop ( ) - > previous ( ) , " for|while ( " ) ) {
const Token * startTok = nullptr ;
const Token * endTok = nullptr ;
std : : tie ( startTok , endTok ) = condTok - > findExpressionStartEndTokens ( ) ;
if ( ! isVariableChanged ( startTok , endTok , varid , false , settings , true ) ) {
std : : list < ValueFlow : : Value > values = { val } ;
if ( val2 . condition ) {
values . push_back ( val2 ) ;
}
const Token * expr = Token : : findmatch ( tok2 , " %varid% " , varid ) ;
valueFlowForward ( const_cast < Token * > ( startTok ) ,
2020-05-22 08:48:28 +02:00
endTok ,
expr ,
values ,
false ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2020-05-21 08:47:48 +02:00
}
}
2015-07-20 19:45:38 +02:00
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
}
2019-10-03 09:58:57 +02:00
static bool isConditionKnown ( const Token * tok , bool then )
{
const char * op = " || " ;
if ( then )
op = " && " ;
const Token * parent = tok - > astParent ( ) ;
while ( parent & & parent - > str ( ) = = op )
parent = parent - > astParent ( ) ;
return ( parent & & parent - > str ( ) = = " ( " ) ;
}
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 ;
}
2019-07-15 13:47:17 +02:00
int varid = vartok - > varId ( ) ;
2015-07-20 19:45:38 +02:00
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
2019-07-27 20:02:49 +02:00
if ( tok - > str ( ) = = " ? " & & tok - > isExpandedMacro ( ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok , " variable " + var - > name ( ) + " , condition is defined in macro " ) ;
continue ;
}
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 ;
2020-01-11 13:11:19 +01:00
if ( var - > valueType ( ) & & var - > valueType ( ) - > sign ! = ValueType : : Sign : : UNSIGNED )
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 , " <=|>= " ) ) {
2020-01-11 14:00:41 +01:00
if ( var - > isUnsigned ( ) ) {
2015-02-01 12:10:20 +01:00
val2 = ValueFlow : : Value ( tok , 0 ) ;
val2 . varId = varid ;
}
2014-04-28 06:21:48 +02:00
}
2019-09-20 15:06:37 +02:00
Token * startTok = tok - > astParent ( ) ? tok - > astParent ( ) : tok - > previous ( ) ;
valueFlowReverse ( tokenlist , startTok , vartok , val , val2 , errorLogger , settings ) ;
2014-01-04 20:57:02 +01:00
}
}
}
2019-07-15 13:47:17 +02:00
static void valueFlowAST ( Token * tok , nonneg 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 ) ;
2019-06-16 10:09:38 +02:00
valueFlowAST ( 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 ( ) ;
2019-05-04 20:41:43 +02:00
const bool nonzero = std : : any_of ( values . cbegin ( ) , values . cend ( ) ,
[ = ] ( const ValueFlow : : Value & v ) {
return v . intvalue ! = 0 ;
} ) ;
2016-07-17 21:51:20 +02:00
if ( ! nonzero )
return ;
ProgramMemory pm ;
pm . setValue ( varid , value ) ;
if ( conditionIsTrue ( tok - > astOperand1 ( ) , pm ) )
return ;
}
2019-06-16 10:09:38 +02:00
valueFlowAST ( tok - > astOperand2 ( ) , varid , value , settings ) ;
2015-05-24 17:02:00 +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 ( ) ) {
2020-02-13 16:27:06 +01:00
if ( assign = = " = " )
lhsValue . intvalue = rhsValue . intvalue ;
else if ( assign = = " += " )
2017-09-20 11:45:28 +02:00
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 ( ) ) {
2020-02-13 16:27:06 +01:00
if ( assign = = " = " )
lhsValue . intvalue = rhsValue . intvalue ;
else if ( assign = = " += " )
2017-09-20 11:45:28 +02:00
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 ;
}
2020-05-19 21:03:28 +02:00
// Check if its an alias of the variable or is being aliased to this variable
static bool isAliasOf ( const Variable * var , const Token * tok , nonneg int varid , const ValueFlow : : Value & val )
{
if ( tok - > varId ( ) = = varid )
return false ;
if ( tok - > varId ( ) = = 0 )
return false ;
if ( isAliasOf ( tok , varid ) )
return true ;
if ( var & & ! var - > isPointer ( ) )
return false ;
// Search through non value aliases
if ( ! val . isNonValue ( ) )
return false ;
if ( val . isInconclusive ( ) )
return false ;
if ( val . isLifetimeValue ( ) & & ! val . isLocalLifetimeValue ( ) )
return false ;
if ( val . isLifetimeValue ( ) & & val . lifetimeKind ! = ValueFlow : : Value : : LifetimeKind : : Address )
return false ;
if ( ! Token : : Match ( val . tokvalue , " .|&|*|%var% " ) )
return false ;
if ( astHasVar ( val . tokvalue , tok - > varId ( ) ) )
return true ;
return false ;
}
2019-08-29 08:38:50 +02:00
// Check if its an alias of the variable or is being aliased to this variable
static bool isAliasOf ( const Variable * var , const Token * tok , nonneg int varid , const std : : list < ValueFlow : : Value > & values )
{
if ( tok - > varId ( ) = = varid )
return false ;
if ( tok - > varId ( ) = = 0 )
return false ;
if ( isAliasOf ( tok , varid ) )
return true ;
2020-02-16 16:02:22 +01:00
if ( var & & ! var - > isPointer ( ) )
2019-08-29 08:38:50 +02:00
return false ;
// Search through non value aliases
for ( const ValueFlow : : Value & val : values ) {
if ( ! val . isNonValue ( ) )
continue ;
2019-09-11 19:25:09 +02:00
if ( val . isInconclusive ( ) )
continue ;
2019-08-29 08:38:50 +02:00
if ( val . isLifetimeValue ( ) & & ! val . isLocalLifetimeValue ( ) )
continue ;
if ( val . isLifetimeValue ( ) & & val . lifetimeKind ! = ValueFlow : : Value : : LifetimeKind : : Address )
continue ;
if ( ! Token : : Match ( val . tokvalue , " .|&|*|%var% " ) )
continue ;
if ( astHasVar ( val . tokvalue , tok - > varId ( ) ) )
return true ;
}
return false ;
}
2020-02-13 16:27:06 +01:00
static const ValueFlow : : Value * getKnownValue ( const Token * tok , ValueFlow : : Value : : ValueType type )
{
if ( ! tok )
return nullptr ;
auto it = std : : find_if ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , [ & ] ( const ValueFlow : : Value & v ) {
return v . isKnown ( ) & & v . valueType = = type ;
} ) ;
if ( it ! = tok - > values ( ) . end ( ) )
return & * it ;
return nullptr ;
}
2020-01-05 16:25:33 +01:00
static bool bifurcate ( const Token * tok , const std : : set < nonneg int > & varids , const Settings * settings , int depth = 20 )
{
if ( depth < 0 )
return false ;
if ( ! tok )
return true ;
if ( tok - > hasKnownIntValue ( ) )
return true ;
if ( Token : : Match ( tok , " %cop% " ) )
2020-01-17 03:17:26 +01:00
return bifurcate ( tok - > astOperand1 ( ) , varids , settings , depth ) & & bifurcate ( tok - > astOperand2 ( ) , varids , settings , depth ) ;
2020-01-05 16:25:33 +01:00
if ( Token : : Match ( tok , " %var% " ) ) {
if ( varids . count ( tok - > varId ( ) ) > 0 )
return true ;
const Variable * var = tok - > variable ( ) ;
if ( ! var )
return false ;
const Token * start = var - > declEndToken ( ) ;
if ( ! start )
return false ;
2020-01-17 03:17:26 +01:00
if ( start - > strAt ( - 1 ) = = " ) " | | start - > strAt ( - 1 ) = = " } " )
return false ;
2020-01-05 16:25:33 +01:00
if ( Token : : Match ( start , " ; %varid% = " , var - > declarationId ( ) ) )
start = start - > tokAt ( 2 ) ;
if ( var - > isConst ( ) | |
! isVariableChanged ( start - > next ( ) , tok , var - > declarationId ( ) , var - > isGlobal ( ) , settings , true ) )
return var - > isArgument ( ) | | bifurcate ( start - > astOperand2 ( ) , varids , settings , depth - 1 ) ;
return false ;
}
return false ;
}
2020-02-16 19:58:09 +01:00
struct SelectMapKeys {
2020-02-16 16:02:22 +01:00
template < class Pair >
2020-02-16 19:58:09 +01:00
typename Pair : : first_type operator ( ) ( const Pair & p ) const {
2020-02-16 16:02:22 +01:00
return p . first ;
}
} ;
2020-02-13 16:27:06 +01:00
2020-03-01 16:46:20 +01:00
struct SelectMapValues {
template < class Pair >
typename Pair : : second_type operator ( ) ( const Pair & p ) const {
return p . second ;
}
} ;
2020-02-16 16:02:22 +01:00
struct ValueFlowForwardAnalyzer : ForwardAnalyzer {
const TokenList * tokenlist ;
2020-02-19 07:55:04 +01:00
ProgramMemoryState pms ;
2020-02-13 16:27:06 +01:00
2020-02-16 16:02:22 +01:00
ValueFlowForwardAnalyzer ( )
2020-02-19 07:55:04 +01:00
: tokenlist ( nullptr ) , pms ( )
2020-02-16 16:02:22 +01:00
{ }
ValueFlowForwardAnalyzer ( const TokenList * t )
2020-02-19 07:55:04 +01:00
: tokenlist ( t ) , pms ( )
2020-02-16 16:02:22 +01:00
{ }
2020-03-01 16:46:20 +01:00
virtual const ValueFlow : : Value * getValue ( const Token * tok ) const = 0 ;
virtual ValueFlow : : Value * getValue ( const Token * tok ) = 0 ;
virtual void makeConditional ( ) = 0 ;
virtual void addErrorPath ( const Token * tok , const std : : string & s ) = 0 ;
2020-02-16 16:02:22 +01:00
virtual bool match ( const Token * tok ) const = 0 ;
virtual bool isAlias ( const Token * tok ) const = 0 ;
using ProgramState = std : : unordered_map < nonneg int , ValueFlow : : Value > ;
virtual ProgramState getProgramState ( ) const = 0 ;
2020-03-01 16:46:20 +01:00
virtual int getIndirect ( const Token * tok ) const {
const ValueFlow : : Value * value = getValue ( tok ) ;
if ( value )
return value - > indirect ;
return 0 ;
}
virtual bool isWritableValue ( const Token * tok ) const {
const ValueFlow : : Value * value = getValue ( tok ) ;
if ( value )
return value - > isIntValue ( ) | | value - > isFloatValue ( ) ;
2020-02-16 16:02:22 +01:00
return false ;
}
virtual bool isGlobal ( ) const {
return false ;
}
virtual bool invalid ( ) const {
return false ;
}
bool isCPP ( ) const {
return tokenlist - > isCPP ( ) ;
}
const Settings * getSettings ( ) const {
return tokenlist - > getSettings ( ) ;
}
virtual Action isModified ( const Token * tok ) const {
Action read = Action : : Read ;
bool inconclusive = false ;
2020-03-01 16:46:20 +01:00
if ( isVariableChangedByFunctionCall ( tok , getIndirect ( tok ) , getSettings ( ) , & inconclusive ) )
2020-02-16 16:02:22 +01:00
return read | Action : : Invalid ;
if ( inconclusive )
return read | Action : : Inconclusive ;
2020-03-01 16:46:20 +01:00
if ( isVariableChanged ( tok , getIndirect ( tok ) , getSettings ( ) , isCPP ( ) ) ) {
2020-02-16 16:02:22 +01:00
if ( Token : : Match ( tok - > astParent ( ) , " *|[|.|++|-- " ) )
return read | Action : : Invalid ;
return Action : : Invalid ;
}
return read ;
}
virtual Action isAliasModified ( const Token * tok ) const {
int indirect = 0 ;
if ( tok - > valueType ( ) )
indirect = tok - > valueType ( ) - > pointer ;
if ( isVariableChanged ( tok , indirect , getSettings ( ) , isCPP ( ) ) )
return Action : : Invalid ;
return Action : : None ;
2020-02-13 17:04:05 +01:00
}
2020-02-13 16:27:06 +01:00
2020-02-13 17:04:05 +01:00
virtual Action analyze ( const Token * tok ) const OVERRIDE {
2020-02-16 16:02:22 +01:00
if ( invalid ( ) )
return Action : : Invalid ;
if ( match ( tok ) ) {
2020-02-13 16:27:06 +01:00
const Token * parent = tok - > astParent ( ) ;
2020-03-01 16:46:20 +01:00
if ( ( Token : : Match ( parent , " *|[ " ) | | ( parent & & parent - > originalName ( ) = = " -> " ) ) & & getIndirect ( tok ) < = 0 )
2020-02-13 16:27:06 +01:00
return Action : : Read ;
Action read = Action : : Read ;
2020-05-19 21:03:28 +02:00
if ( parent & & isWritableValue ( tok ) & & parent - > isAssignmentOp ( ) & & astIsLHS ( tok ) & &
2020-02-13 16:27:06 +01:00
parent - > astOperand2 ( ) - > hasKnownValue ( ) ) {
const Token * rhs = parent - > astOperand2 ( ) ;
const ValueFlow : : Value * rhsValue = getKnownValue ( rhs , ValueFlow : : Value : : ValueType : : INT ) ;
Action a ;
if ( ! rhsValue )
a = Action : : Invalid ;
2015-01-05 16:39:47 +01:00
else
2020-02-13 16:27:06 +01:00
a = Action : : Write ;
2020-05-19 21:03:28 +02:00
if ( parent - > str ( ) ! = " = " )
2020-02-13 16:27:06 +01:00
a | = Action : : Read ;
return a ;
2014-08-27 16:59:18 +02:00
}
2014-06-15 16:47:01 +02:00
2020-02-13 16:27:06 +01:00
// increment/decrement
2020-03-01 16:46:20 +01:00
if ( isWritableValue ( tok ) & & ( Token : : Match ( tok - > previous ( ) , " ++|-- %name% " ) | | Token : : Match ( tok , " %name% ++|-- " ) ) ) {
2020-02-13 16:27:06 +01:00
return read | Action : : Write ;
2017-08-29 22:35:55 +02:00
}
2020-02-13 16:27:06 +01:00
// Check for modifications by function calls
2020-02-16 16:02:22 +01:00
return isModified ( tok ) ;
} else if ( isAlias ( tok ) ) {
return isAliasModified ( tok ) ;
2020-02-13 16:27:06 +01:00
} else if ( Token : : Match ( tok , " %name% ( " ) & & ! Token : : simpleMatch ( tok - > linkAt ( 1 ) , " ) { " )) {
// bailout: global non-const variables
2020-02-16 16:02:22 +01:00
if ( isGlobal ( ) ) {
2020-02-13 16:27:06 +01:00
return Action : : Invalid ;
2014-06-25 16:00:56 +02:00
}
}
2020-02-13 16:27:06 +01:00
return Action : : None ;
}
2020-02-16 16:02:22 +01:00
2020-02-13 17:04:05 +01:00
virtual std : : vector < int > evaluate ( const Token * tok ) const OVERRIDE {
2020-02-13 16:27:06 +01:00
if ( tok - > hasKnownIntValue ( ) )
return { static_cast < int > ( tok - > values ( ) . front ( ) . intvalue ) } ;
std : : vector < int > result ;
2020-02-19 07:55:04 +01:00
ProgramMemory pm = pms . get ( tok , getProgramState ( ) ) ;
if ( conditionIsTrue ( tok , pm ) )
2020-02-13 16:27:06 +01:00
result . push_back ( 1 ) ;
2020-02-19 07:55:04 +01:00
if ( conditionIsFalse ( tok , pm ) )
2020-02-13 16:27:06 +01:00
result . push_back ( 0 ) ;
return result ;
}
2020-03-01 16:46:20 +01:00
virtual void assume ( const Token * tok , bool state , const Token * at ) OVERRIDE {
// Update program state
pms . removeModifiedVars ( tok ) ;
pms . addState ( tok , getProgramState ( ) ) ;
pms . assume ( tok , state ) ;
const bool isAssert = Token : : Match ( at , " assert|ASSERT " ) ;
2020-05-19 21:03:28 +02:00
if ( ! isAssert & & ! Token : : simpleMatch ( at , " } " ) ) {
2020-03-01 16:46:20 +01:00
std : : string s = state ? " true " : " false " ;
addErrorPath ( tok , " Assuming condition is " + s ) ;
}
if ( ! isAssert )
makeConditional ( ) ;
}
virtual void update ( Token * tok , Action a ) OVERRIDE {
ValueFlow : : Value * value = getValue ( tok ) ;
if ( ! value )
return ;
if ( a . isRead ( ) )
setTokenValue ( tok , * value , getSettings ( ) ) ;
if ( a . isInconclusive ( ) )
lowerToInconclusive ( ) ;
2020-05-19 21:03:28 +02:00
if ( a . isWrite ( ) & & tok - > astParent ( ) ) {
if ( tok - > astParent ( ) - > isAssignmentOp ( ) ) {
2020-03-01 16:46:20 +01:00
// TODO: Check result
if ( evalAssignment ( * value ,
tok - > astParent ( ) - > str ( ) ,
* getKnownValue ( tok - > astParent ( ) - > astOperand2 ( ) , ValueFlow : : Value : : ValueType : : INT ) ) ) {
const std : : string info ( " Compound assignment ' " + tok - > astParent ( ) - > str ( ) + " ', assigned value is " +
value - > infoString ( ) ) ;
2020-05-19 21:03:28 +02:00
if ( tok - > astParent ( ) - > str ( ) = = " = " )
value - > errorPath . clear ( ) ;
2020-03-01 16:46:20 +01:00
value - > errorPath . emplace_back ( tok , info ) ;
} else {
// TODO: Don't set to zero
value - > intvalue = 0 ;
}
2020-05-19 21:03:28 +02:00
} else if ( tok - > astParent ( ) - > tokType ( ) = = Token : : eIncDecOp ) {
const bool inc = tok - > astParent ( ) - > str ( ) = = " ++ " ;
2020-03-01 16:46:20 +01:00
value - > intvalue + = ( inc ? 1 : - 1 ) ;
const std : : string info ( tok - > str ( ) + " is " + std : : string ( inc ? " incremented " : " decremented " ) +
" ', new value is " + value - > infoString ( ) ) ;
value - > errorPath . emplace_back ( tok , info ) ;
}
}
}
2020-02-16 16:02:22 +01:00
} ;
struct SingleValueFlowForwardAnalyzer : ValueFlowForwardAnalyzer {
2020-05-19 21:03:28 +02:00
std : : unordered_map < nonneg int , const Variable * > varids ;
2020-02-16 16:02:22 +01:00
ValueFlow : : Value value ;
SingleValueFlowForwardAnalyzer ( )
2020-02-16 19:58:09 +01:00
: ValueFlowForwardAnalyzer ( )
2020-02-16 16:02:22 +01:00
{ }
SingleValueFlowForwardAnalyzer ( const ValueFlow : : Value & v , const TokenList * t )
2020-02-16 19:58:09 +01:00
: ValueFlowForwardAnalyzer ( t ) , value ( v )
2020-02-16 16:02:22 +01:00
{ }
2020-05-19 21:03:28 +02:00
const std : : unordered_map < nonneg int , const Variable * > & getVars ( ) const {
return varids ;
}
2020-02-16 16:02:22 +01:00
2020-03-01 16:46:20 +01:00
virtual const ValueFlow : : Value * getValue ( const Token * ) const OVERRIDE {
return & value ;
}
virtual ValueFlow : : Value * getValue ( const Token * ) OVERRIDE {
return & value ;
}
virtual void makeConditional ( ) OVERRIDE {
value . conditional = true ;
}
virtual void addErrorPath ( const Token * tok , const std : : string & s ) OVERRIDE {
value . errorPath . emplace_back ( tok , s ) ;
2020-02-16 16:02:22 +01:00
}
virtual bool isAlias ( const Token * tok ) const OVERRIDE {
if ( value . isLifetimeValue ( ) )
return false ;
2020-02-16 19:58:09 +01:00
for ( const auto & p : getVars ( ) ) {
2020-02-16 16:02:22 +01:00
nonneg int varid = p . first ;
const Variable * var = p . second ;
if ( tok - > varId ( ) = = varid )
return true ;
2020-05-19 21:03:28 +02:00
if ( isAliasOf ( var , tok , varid , value ) )
2020-02-16 16:02:22 +01:00
return true ;
}
return false ;
}
virtual bool isGlobal ( ) const OVERRIDE {
2020-02-16 19:58:09 +01:00
for ( const auto & p : getVars ( ) ) {
2020-02-16 16:02:22 +01:00
const Variable * var = p . second ;
if ( var - > isGlobal ( ) & & ! var - > isConst ( ) )
return true ;
}
return false ;
}
2020-02-13 17:04:05 +01:00
virtual bool lowerToPossible ( ) OVERRIDE {
2020-02-13 16:27:06 +01:00
if ( value . isImpossible ( ) )
2014-06-18 05:51:23 +02:00
return false ;
2020-02-13 16:27:06 +01:00
value . changeKnownToPossible ( ) ;
return true ;
}
2020-02-13 17:04:05 +01:00
virtual bool lowerToInconclusive ( ) OVERRIDE {
2020-02-13 16:27:06 +01:00
if ( value . isImpossible ( ) )
2014-06-18 05:51:23 +02:00
return false ;
2020-02-13 16:27:06 +01:00
value . setInconclusive ( ) ;
return true ;
}
2014-06-15 16:47:01 +02:00
2020-02-13 17:04:05 +01:00
virtual bool isConditional ( ) const OVERRIDE {
2020-02-13 16:27:06 +01:00
if ( value . conditional )
return true ;
if ( value . condition )
return ! value . isImpossible ( ) ;
return false ;
}
2019-09-20 15:06:37 +02:00
2020-02-13 17:04:05 +01:00
virtual bool updateScope ( const Token * endBlock , bool ) const OVERRIDE {
2020-02-13 16:27:06 +01:00
const Scope * scope = endBlock - > scope ( ) ;
if ( ! scope )
return false ;
if ( scope - > type = = Scope : : eLambda ) {
return value . isLifetimeValue ( ) ;
} else if ( scope - > type = = Scope : : eIf | | scope - > type = = Scope : : eElse | | scope - > type = = Scope : : eWhile | |
scope - > type = = Scope : : eFor ) {
if ( value . isKnown ( ) | | value . isImpossible ( ) )
return true ;
if ( value . isLifetimeValue ( ) )
return true ;
if ( isConditional ( ) )
2019-08-29 08:38:50 +02:00
return false ;
2020-02-13 16:27:06 +01:00
const Token * condTok = getCondTokFromEnd ( endBlock ) ;
2020-05-19 21:55:28 +02:00
std : : set < nonneg int > varids2 ;
std : : transform ( getVars ( ) . begin ( ) , getVars ( ) . end ( ) , std : : inserter ( varids2 , varids2 . begin ( ) ) , SelectMapKeys { } ) ;
return bifurcate ( condTok , varids2 , getSettings ( ) ) ;
2014-06-15 16:47:01 +02:00
}
2015-07-26 19:28:42 +02:00
2020-02-13 16:27:06 +01:00
return false ;
}
} ;
2019-08-24 11:27:47 +02:00
2020-02-16 16:02:22 +01:00
struct VariableForwardAnalyzer : SingleValueFlowForwardAnalyzer {
const Variable * var ;
VariableForwardAnalyzer ( )
2020-05-19 21:03:28 +02:00
: SingleValueFlowForwardAnalyzer ( ) , var ( nullptr )
2020-02-16 16:02:22 +01:00
{ }
VariableForwardAnalyzer ( const Variable * v , const ValueFlow : : Value & val , const TokenList * t )
2020-05-19 21:03:28 +02:00
: SingleValueFlowForwardAnalyzer ( val , t ) , var ( v ) {
2020-02-16 16:02:22 +01:00
varids [ var - > declarationId ( ) ] = var ;
}
virtual bool match ( const Token * tok ) const OVERRIDE {
return tok - > varId ( ) = = var - > declarationId ( ) ;
}
virtual ProgramState getProgramState ( ) const OVERRIDE {
ProgramState ps ;
ps [ var - > declarationId ( ) ] = value ;
return ps ;
}
} ;
2020-02-13 16:27:06 +01:00
static bool valueFlowForwardVariable ( Token * const startToken ,
const Token * const endToken ,
const Variable * const var ,
2020-02-16 16:02:22 +01:00
const nonneg int ,
2020-02-13 16:27:06 +01:00
std : : list < ValueFlow : : Value > values ,
const bool ,
const bool ,
2020-02-16 16:02:22 +01:00
TokenList * const tokenlist ,
2020-02-13 16:27:06 +01:00
ErrorLogger * const ,
const Settings * const settings )
{
for ( ValueFlow : : Value & v : values ) {
2020-02-16 16:02:22 +01:00
VariableForwardAnalyzer a ( var , v , tokenlist ) ;
2020-02-13 16:27:06 +01:00
valueFlowGenericForward ( startToken , endToken , a , settings ) ;
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
}
2020-02-16 16:02:22 +01:00
struct ExpressionForwardAnalyzer : SingleValueFlowForwardAnalyzer {
const Token * expr ;
bool local ;
bool unknown ;
ExpressionForwardAnalyzer ( )
2020-05-19 21:03:28 +02:00
: SingleValueFlowForwardAnalyzer ( ) , expr ( nullptr ) , local ( true ) , unknown ( false )
2020-02-16 16:02:22 +01:00
{ }
ExpressionForwardAnalyzer ( const Token * e , const ValueFlow : : Value & val , const TokenList * t )
2020-05-19 21:03:28 +02:00
: SingleValueFlowForwardAnalyzer ( val , t ) , expr ( e ) , local ( true ) , unknown ( false ) {
2020-02-16 16:02:22 +01:00
setupExprVarIds ( ) ;
}
2020-02-16 19:58:09 +01:00
static bool nonLocal ( const Variable * var , bool deref ) {
2020-02-16 16:02:22 +01:00
return ! var | | ( ! var - > isLocal ( ) & & ! var - > isArgument ( ) ) | | ( deref & & var - > isArgument ( ) & & var - > isPointer ( ) ) | | var - > isStatic ( ) | | var - > isReference ( ) | | var - > isExtern ( ) ;
}
2020-02-16 19:58:09 +01:00
void setupExprVarIds ( ) {
2020-02-16 16:02:22 +01:00
visitAstNodes ( expr ,
[ & ] ( const Token * tok ) {
if ( tok - > varId ( ) = = 0 & & tok - > isName ( ) & & tok - > previous ( ) - > str ( ) ! = " . " ) {
// unknown variable
unknown = true ;
return ChildrenToVisit : : none ;
}
if ( tok - > varId ( ) > 0 ) {
varids [ tok - > varId ( ) ] = tok - > variable ( ) ;
if ( ! Token : : simpleMatch ( tok - > previous ( ) , " . " ) ) {
const Variable * var = tok - > variable ( ) ;
if ( var & & var - > isReference ( ) & & var - > isLocal ( ) & & Token : : Match ( var - > nameToken ( ) , " %var% [=(] " ) & & ! isGlobalData ( var - > nameToken ( ) - > next ( ) - > astOperand2 ( ) , isCPP ( ) ) )
return ChildrenToVisit : : none ;
const bool deref = tok - > astParent ( ) & & ( tok - > astParent ( ) - > isUnaryOp ( " * " ) | | ( tok - > astParent ( ) - > str ( ) = = " [ " & & tok = = tok - > astParent ( ) - > astOperand1 ( ) ) ) ;
local & = ! nonLocal ( tok - > variable ( ) , deref ) ;
}
}
return ChildrenToVisit : : op1_and_op2 ;
} ) ;
}
virtual bool invalid ( ) const OVERRIDE {
return unknown ;
}
virtual std : : vector < int > evaluate ( const Token * tok ) const OVERRIDE {
if ( tok - > hasKnownIntValue ( ) )
return { static_cast < int > ( tok - > values ( ) . front ( ) . intvalue ) } ;
2020-02-16 19:58:09 +01:00
return std : : vector < int > { } ;
2020-02-16 16:02:22 +01:00
}
virtual bool match ( const Token * tok ) const OVERRIDE {
return isSameExpression ( isCPP ( ) , true , expr , tok , getSettings ( ) - > library , true , true ) ;
}
virtual ProgramState getProgramState ( ) const OVERRIDE {
return ProgramState { } ;
}
virtual bool isGlobal ( ) const OVERRIDE {
return ! local ;
}
} ;
static void valueFlowForwardExpression ( Token * startToken ,
const Token * endToken ,
const Token * exprTok ,
const std : : list < ValueFlow : : Value > & values ,
const TokenList * const tokenlist ,
const Settings * settings )
{
for ( const ValueFlow : : Value & v : values ) {
ExpressionForwardAnalyzer a ( exprTok , v , tokenlist ) ;
valueFlowGenericForward ( startToken , endToken , a , settings ) ;
}
}
2019-09-28 20:50:56 +02:00
static const Token * parseBinaryIntOp ( const Token * expr , MathLib : : bigint & known )
2019-09-26 10:32:25 +02:00
{
if ( ! expr )
return nullptr ;
if ( ! expr - > astOperand1 ( ) | | ! expr - > astOperand2 ( ) )
return nullptr ;
const Token * knownTok = nullptr ;
const Token * varTok = nullptr ;
if ( expr - > astOperand1 ( ) - > hasKnownIntValue ( ) & & ! expr - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
varTok = expr - > astOperand2 ( ) ;
knownTok = expr - > astOperand1 ( ) ;
} else if ( expr - > astOperand2 ( ) - > hasKnownIntValue ( ) & & ! expr - > astOperand1 ( ) - > hasKnownIntValue ( ) ) {
varTok = expr - > astOperand1 ( ) ;
knownTok = expr - > astOperand2 ( ) ;
}
if ( knownTok )
known = knownTok - > values ( ) . front ( ) . intvalue ;
return varTok ;
}
template < class F >
void transformIntValues ( std : : list < ValueFlow : : Value > & values , F f )
{
std : : transform ( values . begin ( ) , values . end ( ) , values . begin ( ) , [ & ] ( ValueFlow : : Value x ) {
if ( x . isIntValue ( ) )
x . intvalue = f ( x . intvalue ) ;
return x ;
} ) ;
}
2019-09-28 20:50:56 +02:00
static const Token * solveExprValues ( const Token * expr , std : : list < ValueFlow : : Value > & values )
2019-09-26 10:32:25 +02:00
{
MathLib : : bigint intval ;
const Token * binaryTok = parseBinaryIntOp ( expr , intval ) ;
if ( binaryTok & & expr - > str ( ) . size ( ) = = 1 ) {
switch ( expr - > str ( ) [ 0 ] ) {
case ' + ' : {
2019-09-26 10:32:49 +02:00
transformIntValues ( values , [ & ] ( MathLib : : bigint x ) {
return x - intval ;
} ) ;
2019-09-26 10:32:25 +02:00
return solveExprValues ( binaryTok , values ) ;
}
case ' * ' : {
2019-12-06 08:08:40 +01:00
if ( intval = = 0 )
break ;
2019-09-26 10:32:49 +02:00
transformIntValues ( values , [ & ] ( MathLib : : bigint x ) {
return x / intval ;
} ) ;
2019-09-26 10:32:25 +02:00
return solveExprValues ( binaryTok , values ) ;
}
case ' ^ ' : {
2019-09-26 10:32:49 +02:00
transformIntValues ( values , [ & ] ( MathLib : : bigint x ) {
return x ^ intval ;
} ) ;
2019-09-26 10:32:25 +02:00
return solveExprValues ( binaryTok , values ) ;
}
}
}
return expr ;
}
static void valueFlowForward ( Token * startToken ,
const Token * endToken ,
const Token * exprTok ,
std : : list < ValueFlow : : Value > values ,
const bool constValue ,
const bool subFunction ,
TokenList * const tokenlist ,
ErrorLogger * const errorLogger ,
const Settings * settings )
{
const Token * expr = solveExprValues ( exprTok , values ) ;
2020-05-19 21:03:28 +02:00
if ( expr - > variable ( ) ) {
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( startToken ,
endToken ,
expr - > variable ( ) ,
expr - > varId ( ) ,
values ,
constValue ,
subFunction ,
tokenlist ,
errorLogger ,
settings ) ;
} else {
valueFlowForwardExpression ( startToken , endToken , expr , values , tokenlist , settings ) ;
}
}
2019-09-11 19:25:09 +02:00
static std : : vector < const Token * > findReturns ( const Function * f )
{
std : : vector < const Token * > result ;
const Scope * scope = f - > functionScope ;
if ( ! scope )
return result ;
for ( const Token * tok = scope - > bodyStart - > next ( ) ; tok & & tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) = = " { " & & tok - > scope ( ) & &
( tok - > scope ( ) - > type = = Scope : : eLambda | | tok - > scope ( ) - > type = = Scope : : eClass ) ) {
tok = tok - > link ( ) ;
continue ;
}
if ( Token : : simpleMatch ( tok - > astParent ( ) , " return " ) ) {
result . push_back ( tok ) ;
}
// Skip lambda functions since the scope may not be set correctly
const Token * lambdaEndToken = findLambdaEndToken ( tok ) ;
if ( lambdaEndToken ) {
tok = lambdaEndToken ;
}
}
return result ;
}
2019-01-29 09:47:52 +01:00
static int getArgumentPos ( const Variable * var , const Function * f )
{
auto arg_it = std : : find_if ( f - > argumentList . begin ( ) , f - > argumentList . end ( ) , [ & ] ( const Variable & v ) {
return v . nameToken ( ) = = var - > nameToken ( ) ;
} ) ;
if ( arg_it = = f - > argumentList . end ( ) )
return - 1 ;
return std : : distance ( f - > argumentList . begin ( ) , arg_it ) ;
}
std : : string lifetimeType ( const Token * tok , const ValueFlow : : Value * val )
{
std : : string result ;
if ( ! val )
return " object " ;
switch ( val - > lifetimeKind ) {
2019-07-16 10:51:07 +02:00
case ValueFlow : : Value : : LifetimeKind : : Lambda :
2019-01-29 09:47:52 +01:00
result = " lambda " ;
break ;
2019-07-16 10:51:07 +02:00
case ValueFlow : : Value : : LifetimeKind : : Iterator :
2019-01-29 09:47:52 +01:00
result = " iterator " ;
break ;
2019-07-16 10:51:07 +02:00
case ValueFlow : : Value : : LifetimeKind : : Object :
case ValueFlow : : Value : : LifetimeKind : : Address :
2019-01-29 09:47:52 +01:00
if ( astIsPointer ( tok ) )
result = " pointer " ;
else
result = " object " ;
break ;
}
return result ;
}
2019-07-18 10:56:44 +02:00
std : : string lifetimeMessage ( const Token * tok , const ValueFlow : : Value * val , ErrorPath & errorPath )
{
const Token * tokvalue = val ? val - > tokvalue : nullptr ;
const Variable * tokvar = tokvalue ? tokvalue - > variable ( ) : nullptr ;
const Token * vartok = tokvar ? tokvar - > nameToken ( ) : nullptr ;
std : : string type = lifetimeType ( tok , val ) ;
std : : string msg = type ;
if ( vartok ) {
errorPath . emplace_back ( vartok , " Variable created here. " ) ;
const Variable * var = vartok - > variable ( ) ;
if ( var ) {
switch ( val - > lifetimeKind ) {
case ValueFlow : : Value : : LifetimeKind : : Object :
case ValueFlow : : Value : : LifetimeKind : : Address :
if ( type = = " pointer " )
msg + = " to local variable " ;
else
msg + = " that points to local variable " ;
break ;
case ValueFlow : : Value : : LifetimeKind : : Lambda :
msg + = " that captures local variable " ;
break ;
case ValueFlow : : Value : : LifetimeKind : : Iterator :
msg + = " to local container " ;
break ;
}
msg + = " ' " + var - > name ( ) + " ' " ;
}
}
return msg ;
}
2019-05-31 11:16:04 +02:00
ValueFlow : : Value getLifetimeObjValue ( const Token * tok )
{
ValueFlow : : Value result ;
auto pred = [ ] ( const ValueFlow : : Value & v ) {
if ( ! v . isLocalLifetimeValue ( ) )
return false ;
2019-09-11 19:25:09 +02:00
if ( v . isInconclusive ( ) )
return false ;
2019-05-31 11:16:04 +02:00
if ( ! v . tokvalue - > variable ( ) )
return false ;
return true ;
} ;
auto it = std : : find_if ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , pred ) ;
if ( it = = tok - > values ( ) . end ( ) )
return result ;
result = * it ;
// There should only be one lifetime
if ( std : : find_if ( std : : next ( it ) , tok - > values ( ) . end ( ) , pred ) ! = tok - > values ( ) . end ( ) )
2019-12-31 08:09:04 +01:00
return ValueFlow : : Value { } ;
2019-05-31 11:16:04 +02:00
return result ;
}
2019-09-11 19:25:09 +02:00
std : : vector < LifetimeToken > getLifetimeTokens ( const Token * tok , ValueFlow : : Value : : ErrorPath errorPath , int depth )
2018-11-16 06:12:28 +01:00
{
2019-01-23 07:29:16 +01:00
if ( ! tok )
2019-09-11 19:25:09 +02:00
return std : : vector < LifetimeToken > { } ;
2018-11-16 06:12:28 +01:00
const Variable * var = tok - > variable ( ) ;
2019-01-31 10:34:41 +01:00
if ( depth < 0 )
2019-09-11 19:25:09 +02:00
return { { tok , std : : move ( errorPath ) } } ;
2019-01-26 11:03:57 +01:00
if ( var & & var - > declarationId ( ) = = tok - > varId ( ) ) {
if ( var - > isReference ( ) | | var - > isRValueReference ( ) ) {
if ( ! var - > declEndToken ( ) )
2019-09-11 19:25:09 +02:00
return { { tok , true , std : : move ( errorPath ) } } ;
2019-01-26 11:03:57 +01:00
if ( var - > isArgument ( ) ) {
errorPath . emplace_back ( var - > declEndToken ( ) , " Passed to reference. " ) ;
2019-09-11 19:25:09 +02:00
return { { tok , true , std : : move ( errorPath ) } } ;
2019-01-26 11:03:57 +01:00
} else if ( Token : : simpleMatch ( var - > declEndToken ( ) , " = " ) ) {
errorPath . emplace_back ( var - > declEndToken ( ) , " Assigned to reference. " ) ;
const Token * vartok = var - > declEndToken ( ) - > astOperand2 ( ) ;
2020-02-10 18:01:11 +01:00
if ( vartok = = tok | | ( var - > isConst ( ) & & isTemporary ( true , vartok , nullptr , true ) ) )
2019-09-11 19:25:09 +02:00
return { { tok , true , std : : move ( errorPath ) } } ;
2019-01-26 11:03:57 +01:00
if ( vartok )
2019-09-11 19:25:09 +02:00
return getLifetimeTokens ( vartok , std : : move ( errorPath ) , depth - 1 ) ;
2019-01-26 11:03:57 +01:00
} else {
2019-09-11 19:25:09 +02:00
return std : : vector < LifetimeToken > { } ;
2019-01-26 11:03:57 +01:00
}
}
} else if ( Token : : Match ( tok - > previous ( ) , " %name% ( " ) ) {
const Function * f = tok - > previous ( ) - > function ( ) ;
2019-09-02 06:58:09 +02:00
if ( f ) {
if ( ! Function : : returnsReference ( f ) )
2019-09-11 19:25:09 +02:00
return { { tok , std : : move ( errorPath ) } } ;
std : : vector < LifetimeToken > result ;
std : : vector < const Token * > returns = findReturns ( f ) ;
for ( const Token * returnTok : returns ) {
if ( returnTok = = tok )
continue ;
for ( LifetimeToken & lt : getLifetimeTokens ( returnTok , std : : move ( errorPath ) , depth - 1 ) ) {
const Token * argvarTok = lt . token ;
const Variable * argvar = argvarTok - > variable ( ) ;
if ( ! argvar )
continue ;
if ( argvar - > isArgument ( ) & & ( argvar - > isReference ( ) | | argvar - > isRValueReference ( ) ) ) {
int n = getArgumentPos ( argvar , f ) ;
if ( n < 0 )
return std : : vector < LifetimeToken > { } ;
2019-12-29 08:23:58 +01:00
std : : vector < const Token * > args = getArguments ( tok - > previous ( ) ) ;
// TODO: Track lifetimes of default parameters
if ( n > = args . size ( ) )
return std : : vector < LifetimeToken > { } ;
const Token * argTok = args [ n ] ;
2019-09-11 19:25:09 +02:00
lt . errorPath . emplace_back ( returnTok , " Return reference. " ) ;
lt . errorPath . emplace_back ( tok - > previous ( ) , " Called function passing ' " + argTok - > str ( ) + " '. " ) ;
std : : vector < LifetimeToken > arglts = LifetimeToken : : setInconclusive (
getLifetimeTokens ( argTok , std : : move ( lt . errorPath ) , depth - 1 ) , returns . size ( ) > 1 ) ;
result . insert ( result . end ( ) , arglts . begin ( ) , arglts . end ( ) ) ;
}
}
2019-09-02 06:58:09 +02:00
}
2019-09-11 19:25:09 +02:00
return result ;
2019-09-02 06:58:09 +02:00
} else if ( Token : : Match ( tok - > tokAt ( - 2 ) , " . %name% ( " ) & & astIsContainer ( tok - > tokAt ( - 2 ) - > astOperand1 ( ) ) ) {
const Library : : Container * library = getLibraryContainer ( tok - > tokAt ( - 2 ) - > astOperand1 ( ) ) ;
Library : : Container : : Yield y = library - > getYield ( tok - > previous ( ) - > str ( ) ) ;
if ( y = = Library : : Container : : Yield : : AT_INDEX | | y = = Library : : Container : : Yield : : ITEM ) {
errorPath . emplace_back ( tok - > previous ( ) , " Accessing container. " ) ;
2019-09-11 19:25:09 +02:00
return LifetimeToken : : setAddressOf (
getLifetimeTokens ( tok - > tokAt ( - 2 ) - > astOperand1 ( ) , std : : move ( errorPath ) , depth - 1 ) , false ) ;
2019-09-02 06:58:09 +02:00
}
2019-02-22 06:38:56 +01:00
}
} else if ( Token : : Match ( tok , " .|::|[ " ) ) {
const Token * vartok = tok ;
while ( vartok ) {
if ( vartok - > str ( ) = = " [ " | | vartok - > originalName ( ) = = " -> " )
vartok = vartok - > astOperand1 ( ) ;
else if ( vartok - > str ( ) = = " . " | | vartok - > str ( ) = = " :: " )
vartok = vartok - > astOperand2 ( ) ;
else
break ;
}
if ( ! vartok )
2019-09-11 19:25:09 +02:00
return { { tok , std : : move ( errorPath ) } } ;
2019-02-22 06:38:56 +01:00
const Variable * tokvar = vartok - > variable ( ) ;
2019-11-07 09:33:17 +01:00
if ( ! astIsContainer ( vartok ) & & ! ( tokvar & & tokvar - > isArray ( ) & & ! tokvar - > isArgument ( ) ) & &
2019-02-23 08:32:08 +01:00
( Token : : Match ( vartok - > astParent ( ) , " [|* " ) | | vartok - > astParent ( ) - > originalName ( ) = = " -> " ) ) {
2019-02-22 06:38:56 +01:00
for ( const ValueFlow : : Value & v : vartok - > values ( ) ) {
if ( ! v . isLocalLifetimeValue ( ) )
continue ;
errorPath . insert ( errorPath . end ( ) , v . errorPath . begin ( ) , v . errorPath . end ( ) ) ;
2019-09-11 19:25:09 +02:00
return getLifetimeTokens ( v . tokvalue , std : : move ( errorPath ) ) ;
2019-02-22 06:38:56 +01:00
}
} else {
2019-09-11 19:25:09 +02:00
return LifetimeToken : : setAddressOf ( getLifetimeTokens ( vartok , std : : move ( errorPath ) ) ,
! ( astIsContainer ( vartok ) & & Token : : simpleMatch ( vartok - > astParent ( ) , " [ " ) ) ) ;
2019-01-26 11:03:57 +01:00
}
2018-11-16 06:12:28 +01:00
}
2019-09-11 19:25:09 +02:00
return { { tok , std : : move ( errorPath ) } } ;
}
static const Token * getLifetimeToken ( const Token * tok , ValueFlow : : Value : : ErrorPath & errorPath , bool * addressOf = nullptr )
{
std : : vector < LifetimeToken > lts = getLifetimeTokens ( tok ) ;
if ( lts . size ( ) ! = 1 )
return nullptr ;
if ( lts . front ( ) . inconclusive )
return nullptr ;
if ( addressOf )
* addressOf = lts . front ( ) . addressOf ;
errorPath . insert ( errorPath . end ( ) , lts . front ( ) . errorPath . begin ( ) , lts . front ( ) . errorPath . end ( ) ) ;
return lts . front ( ) . token ;
2019-02-22 06:38:56 +01:00
}
2019-09-02 06:58:09 +02:00
const Variable * getLifetimeVariable ( const Token * tok , ValueFlow : : Value : : ErrorPath & errorPath , bool * addressOf )
2019-02-22 06:38:56 +01:00
{
2019-09-02 06:58:09 +02:00
const Token * tok2 = getLifetimeToken ( tok , errorPath , addressOf ) ;
2019-02-22 06:38:56 +01:00
if ( tok2 & & tok2 - > variable ( ) )
return tok2 - > variable ( ) ;
return nullptr ;
2018-11-16 06:12:28 +01:00
}
2018-11-14 06:59:25 +01:00
static bool isNotLifetimeValue ( const ValueFlow : : Value & val )
{
return ! val . isLifetimeValue ( ) ;
}
2019-05-05 11:40:59 +02:00
static bool isLifetimeOwned ( const ValueType * vt , const ValueType * vtParent )
{
if ( ! vtParent )
return false ;
if ( ! vt ) {
if ( vtParent - > type = = ValueType : : CONTAINER )
return true ;
return false ;
}
if ( vt - > type ! = ValueType : : UNKNOWN_TYPE & & vtParent - > type ! = ValueType : : UNKNOWN_TYPE ) {
if ( vt - > pointer ! = vtParent - > pointer )
return true ;
if ( vt - > type ! = vtParent - > type ) {
if ( vtParent - > type = = ValueType : : RECORD )
return true ;
if ( vtParent - > type = = ValueType : : CONTAINER )
return true ;
}
}
return false ;
}
static bool isLifetimeBorrowed ( const ValueType * vt , const ValueType * vtParent )
{
if ( ! vtParent )
return false ;
if ( ! vt )
return false ;
if ( vt - > type ! = ValueType : : UNKNOWN_TYPE & & vtParent - > type ! = ValueType : : UNKNOWN_TYPE ) {
if ( vtParent - > pointer > vt - > pointer )
return true ;
if ( vtParent - > pointer < vt - > pointer & & vtParent - > isIntegral ( ) )
return true ;
2020-05-23 23:12:00 +02:00
if ( vtParent - > str ( ) = = vt - > str ( ) )
return true ;
2019-05-05 11:40:59 +02:00
}
return false ;
}
2020-05-23 23:12:00 +02:00
static const Token * skipCVRefs ( const Token * tok , const Token * endTok )
{
2020-05-24 10:52:58 +02:00
while ( tok ! = endTok & & Token : : Match ( tok , " const|volatile|auto|&|&& " ) )
2020-05-23 23:12:00 +02:00
tok = tok - > next ( ) ;
return tok ;
}
static bool isNotEqual ( std : : pair < const Token * , const Token * > x , std : : pair < const Token * , const Token * > y )
{
const Token * start1 = x . first ;
const Token * start2 = y . first ;
if ( start1 = = nullptr | | start2 = = nullptr )
return false ;
2020-05-24 10:52:58 +02:00
while ( start1 ! = x . second & & start2 ! = y . second ) {
2020-05-23 23:12:00 +02:00
const Token * tok1 = skipCVRefs ( start1 , x . second ) ;
if ( tok1 ! = start1 ) {
start1 = tok1 ;
continue ;
}
const Token * tok2 = skipCVRefs ( start2 , y . second ) ;
if ( tok2 ! = start2 ) {
start2 = tok2 ;
continue ;
}
if ( start1 - > str ( ) ! = start2 - > str ( ) )
return true ;
start1 = start1 - > next ( ) ;
start2 = start2 - > next ( ) ;
}
start1 = skipCVRefs ( start1 , x . second ) ;
start2 = skipCVRefs ( start2 , y . second ) ;
return ! ( start1 = = x . second & & start2 = = y . second ) ;
}
2019-05-05 11:40:59 +02:00
bool isLifetimeBorrowed ( const Token * tok , const Settings * settings )
{
if ( ! tok )
return true ;
2020-05-19 21:03:28 +02:00
if ( tok - > str ( ) = = " , " )
2019-05-05 11:40:59 +02:00
return true ;
if ( ! tok - > astParent ( ) )
return true ;
if ( ! Token : : Match ( tok - > astParent ( ) - > previous ( ) , " %name% ( " ) & & ! Token : : simpleMatch ( tok - > astParent ( ) , " , " ) ) {
if ( ! Token : : simpleMatch ( tok , " { " ) ) {
const ValueType * vt = tok - > valueType ( ) ;
const ValueType * vtParent = tok - > astParent ( ) - > valueType ( ) ;
if ( isLifetimeBorrowed ( vt , vtParent ) )
return true ;
if ( isLifetimeOwned ( vt , vtParent ) )
return false ;
}
2020-05-23 23:12:00 +02:00
if ( Token : : Match ( tok - > astParent ( ) , " return|(|{|%assign% " ) ) {
const Type * t = Token : : typeOf ( tok ) ;
const Type * parentT = Token : : typeOf ( tok - > astParent ( ) ) ;
if ( t & & parentT ) {
if ( t - > classDef & & parentT - > classDef & & t - > classDef ! = parentT - > classDef )
return false ;
} else {
std : : pair < const Token * , const Token * > decl = Token : : typeDecl ( tok ) ;
std : : pair < const Token * , const Token * > parentdecl = Token : : typeDecl ( tok - > astParent ( ) ) ;
if ( isNotEqual ( decl , parentdecl ) )
return false ;
}
2019-05-05 11:40:59 +02:00
}
} else if ( Token : : Match ( tok - > astParent ( ) - > tokAt ( - 3 ) , " %var% . push_back|push_front|insert|push ( " ) & &
astIsContainer ( tok - > astParent ( ) - > tokAt ( - 3 ) ) ) {
const ValueType * vt = tok - > valueType ( ) ;
const ValueType * vtCont = tok - > astParent ( ) - > tokAt ( - 3 ) - > valueType ( ) ;
if ( ! vtCont - > containerTypeToken )
return true ;
ValueType vtParent = ValueType : : parseDecl ( vtCont - > containerTypeToken , settings ) ;
if ( isLifetimeBorrowed ( vt , & vtParent ) )
return true ;
if ( isLifetimeOwned ( vt , & vtParent ) )
return false ;
}
return true ;
}
2018-11-16 06:12:28 +01:00
static void valueFlowLifetimeFunction ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings ) ;
2019-03-19 06:25:10 +01:00
static void valueFlowLifetimeConstructor ( Token * tok ,
TokenList * tokenlist ,
ErrorLogger * errorLogger ,
const Settings * settings ) ;
2018-11-14 06:59:25 +01:00
static void valueFlowForwardLifetime ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
2019-06-16 10:09:38 +02:00
Token * parent = tok - > astParent ( ) ;
2018-11-21 08:43:57 +01:00
while ( parent & & ( parent - > isArithmeticalOp ( ) | | parent - > str ( ) = = " , " ) )
2018-11-16 06:12:28 +01:00
parent = parent - > astParent ( ) ;
if ( ! parent )
2018-11-14 06:59:25 +01:00
return ;
2018-11-16 06:12:28 +01:00
// Assignment
2019-02-22 06:37:02 +01:00
if ( parent - > str ( ) = = " = " & & ( ! parent - > astParent ( ) | | Token : : simpleMatch ( parent - > astParent ( ) , " ; " ) ) ) {
2018-11-16 06:12:28 +01:00
// Rhs values..
if ( ! parent - > astOperand2 ( ) | | parent - > astOperand2 ( ) - > values ( ) . empty ( ) )
return ;
2018-11-14 06:59:25 +01:00
2019-05-05 11:40:59 +02:00
if ( ! isLifetimeBorrowed ( parent - > astOperand2 ( ) , settings ) )
2018-11-16 06:12:28 +01:00
return ;
2018-11-14 06:59:25 +01:00
2020-02-13 16:27:06 +01:00
std : : vector < const Variable * > vars = getLHSVariables ( parent ) ;
2018-11-14 06:59:25 +01:00
2020-01-04 11:39:52 +01:00
const Token * endOfVarScope = nullptr ;
2020-02-13 16:27:06 +01:00
for ( const Variable * var : vars ) {
if ( var & & var - > isLocal ( ) )
endOfVarScope = var - > typeStartToken ( ) - > scope ( ) - > bodyEnd ;
else if ( ! endOfVarScope )
endOfVarScope = tok - > scope ( ) - > bodyEnd ;
}
2020-01-04 11:39:52 +01:00
// Only forward lifetime values
std : : list < ValueFlow : : Value > values = parent - > astOperand2 ( ) - > values ( ) ;
values . remove_if ( & isNotLifetimeValue ) ;
2018-11-14 06:59:25 +01:00
2018-11-16 06:12:28 +01:00
// Skip RHS
const Token * nextExpression = nextAfterAstRightmostLeaf ( parent ) ;
2020-01-04 11:39:52 +01:00
if ( Token : : Match ( parent - > astOperand1 ( ) , " .|[|( " ) ) {
valueFlowForwardExpression (
const_cast < Token * > ( nextExpression ) , endOfVarScope , parent - > astOperand1 ( ) , values , tokenlist , settings ) ;
for ( ValueFlow : : Value & val : values ) {
if ( val . lifetimeKind = = ValueFlow : : Value : : LifetimeKind : : Address )
val . lifetimeKind = ValueFlow : : Value : : LifetimeKind : : Object ;
}
}
2020-02-13 16:27:06 +01:00
for ( const Variable * var : vars ) {
2020-01-04 11:39:52 +01:00
valueFlowForwardVariable ( const_cast < Token * > ( nextExpression ) ,
endOfVarScope ,
var ,
var - > declarationId ( ) ,
values ,
false ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2019-02-22 06:37:02 +01:00
2020-01-04 11:39:52 +01:00
if ( tok - > astTop ( ) & & Token : : simpleMatch ( tok - > astTop ( ) - > previous ( ) , " for ( " ) & &
Token : : simpleMatch ( tok - > astTop ( ) - > link ( ) , " ) { " ) ) {
Token * start = tok - > astTop ( ) - > link ( ) - > next ( ) ;
valueFlowForwardVariable (
start , start - > link ( ) , var , var - > declarationId ( ) , values , false , false , tokenlist , errorLogger , settings ) ;
}
2019-02-22 06:37:02 +01:00
}
2019-03-19 06:25:10 +01:00
// Constructor
2019-09-10 19:41:35 +02:00
} else if ( Token : : simpleMatch ( parent , " { " ) & & ! isScopeBracket ( parent ) ) {
2019-06-16 10:09:38 +02:00
valueFlowLifetimeConstructor ( parent , tokenlist , errorLogger , settings ) ;
2019-09-10 19:41:35 +02:00
valueFlowForwardLifetime ( parent , tokenlist , errorLogger , settings ) ;
2018-11-16 06:12:28 +01:00
// Function call
} else if ( Token : : Match ( parent - > previous ( ) , " %name% ( " ) ) {
2019-06-16 10:09:38 +02:00
valueFlowLifetimeFunction ( parent - > previous ( ) , tokenlist , errorLogger , settings ) ;
2019-09-10 19:41:35 +02:00
valueFlowForwardLifetime ( parent , tokenlist , errorLogger , settings ) ;
2018-11-21 08:43:57 +01:00
// Variable
} else if ( tok - > variable ( ) ) {
const Variable * var = tok - > variable ( ) ;
2020-01-11 09:17:32 +01:00
const Token * endOfVarScope = var - > scope ( ) - > bodyEnd ;
2018-11-21 08:43:57 +01:00
std : : list < ValueFlow : : Value > values = tok - > values ( ) ;
const Token * nextExpression = nextAfterAstRightmostLeaf ( parent ) ;
// Only forward lifetime values
values . remove_if ( & isNotLifetimeValue ) ;
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( const_cast < Token * > ( nextExpression ) ,
endOfVarScope ,
var ,
var - > declarationId ( ) ,
values ,
false ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2018-11-16 06:12:28 +01:00
}
}
2018-11-14 06:59:25 +01:00
2018-11-16 06:12:28 +01:00
struct LifetimeStore {
const Token * argtok ;
std : : string message ;
ValueFlow : : Value : : LifetimeKind type ;
2019-01-29 09:47:52 +01:00
ErrorPath errorPath ;
2019-09-11 19:25:09 +02:00
bool inconclusive ;
2019-01-29 09:47:52 +01:00
2019-07-26 07:02:07 +02:00
LifetimeStore ( )
2019-09-11 19:25:09 +02:00
: argtok ( nullptr ) , message ( ) , type ( ) , errorPath ( ) , inconclusive ( false )
2019-07-26 07:02:07 +02:00
{ }
2019-01-29 09:47:52 +01:00
LifetimeStore ( const Token * argtok ,
const std : : string & message ,
2019-09-11 19:25:09 +02:00
ValueFlow : : Value : : LifetimeKind type = ValueFlow : : Value : : LifetimeKind : : Object ,
bool inconclusive = false )
: argtok ( argtok ) , message ( message ) , type ( type ) , errorPath ( ) , inconclusive ( inconclusive )
2019-01-29 09:47:52 +01:00
{ }
2018-11-14 06:59:25 +01:00
2019-07-26 07:02:07 +02:00
static LifetimeStore fromFunctionArg ( const Function * f , Token * tok , const Variable * var , TokenList * tokenlist , ErrorLogger * errorLogger ) {
if ( ! var )
return LifetimeStore { } ;
if ( ! var - > isArgument ( ) )
return LifetimeStore { } ;
int n = getArgumentPos ( var , f ) ;
if ( n < 0 )
return LifetimeStore { } ;
std : : vector < const Token * > args = getArguments ( tok ) ;
if ( n > = args . size ( ) ) {
if ( tokenlist - > getSettings ( ) - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
tok ,
" Argument mismatch: Function ' " + tok - > str ( ) + " ' returning lifetime from argument index " +
std : : to_string ( n ) + " but only " + std : : to_string ( args . size ( ) ) +
" arguments are available. " ) ;
return LifetimeStore { } ;
}
const Token * argtok2 = args [ n ] ;
return LifetimeStore { argtok2 , " Passed to ' " + tok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object } ;
}
2018-11-16 06:12:28 +01:00
template < class Predicate >
void byRef ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings , Predicate pred ) const {
2019-09-11 19:25:09 +02:00
if ( ! settings - > inconclusive & & inconclusive )
2019-07-26 07:02:07 +02:00
return ;
2019-09-11 19:25:09 +02:00
if ( ! argtok )
2018-11-16 06:12:28 +01:00
return ;
2019-09-11 19:25:09 +02:00
for ( const LifetimeToken & lt : getLifetimeTokens ( argtok ) ) {
if ( ! settings - > inconclusive & & lt . inconclusive )
continue ;
ErrorPath er = errorPath ;
er . insert ( er . end ( ) , lt . errorPath . begin ( ) , lt . errorPath . end ( ) ) ;
if ( ! lt . token )
return ;
if ( ! pred ( lt . token ) )
return ;
er . emplace_back ( argtok , message ) ;
2018-11-14 06:59:25 +01:00
2019-09-11 19:25:09 +02:00
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : LIFETIME ;
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Local ;
value . tokvalue = lt . token ;
value . errorPath = std : : move ( er ) ;
value . lifetimeKind = type ;
value . setInconclusive ( lt . inconclusive | | inconclusive ) ;
// Don't add the value a second time
if ( std : : find ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , value ) ! = tok - > values ( ) . end ( ) )
return ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
2018-11-16 06:12:28 +01:00
}
void byRef ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings ) const {
2019-02-22 06:38:56 +01:00
byRef ( tok , tokenlist , errorLogger , settings , [ ] ( const Token * ) {
2018-11-16 06:12:28 +01:00
return true ;
} ) ;
}
template < class Predicate >
void byVal ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings , Predicate pred ) const {
2019-09-11 19:25:09 +02:00
if ( ! settings - > inconclusive & & inconclusive )
return ;
2019-07-26 07:02:07 +02:00
if ( ! argtok )
return ;
2019-01-29 09:47:52 +01:00
if ( argtok - > values ( ) . empty ( ) ) {
ErrorPath er ;
er . emplace_back ( argtok , message ) ;
const Variable * var = getLifetimeVariable ( argtok , er ) ;
if ( var & & var - > isArgument ( ) ) {
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : LIFETIME ;
2019-07-16 11:12:35 +02:00
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Argument ;
2019-01-29 09:47:52 +01:00
value . tokvalue = var - > nameToken ( ) ;
value . errorPath = er ;
value . lifetimeKind = type ;
2019-09-11 19:25:09 +02:00
value . setInconclusive ( inconclusive ) ;
2019-01-29 09:47:52 +01:00
// Don't add the value a second time
if ( std : : find ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , value ) ! = tok - > values ( ) . end ( ) )
return ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
}
2018-11-16 06:12:28 +01:00
for ( const ValueFlow : : Value & v : argtok - > values ( ) ) {
2018-11-14 06:59:25 +01:00
if ( ! v . isLifetimeValue ( ) )
continue ;
2018-11-16 06:12:28 +01:00
const Token * tok3 = v . tokvalue ;
2019-09-11 19:25:09 +02:00
for ( const LifetimeToken & lt : getLifetimeTokens ( tok3 ) ) {
if ( ! settings - > inconclusive & & lt . inconclusive )
continue ;
ErrorPath er = v . errorPath ;
er . insert ( er . end ( ) , lt . errorPath . begin ( ) , lt . errorPath . end ( ) ) ;
if ( ! lt . token )
return ;
if ( ! pred ( lt . token ) )
return ;
er . emplace_back ( argtok , message ) ;
er . insert ( er . end ( ) , errorPath . begin ( ) , errorPath . end ( ) ) ;
2018-11-16 06:12:28 +01:00
2019-09-11 19:25:09 +02:00
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : LIFETIME ;
value . lifetimeScope = v . lifetimeScope ;
value . tokvalue = lt . token ;
value . errorPath = std : : move ( er ) ;
value . lifetimeKind = type ;
value . setInconclusive ( lt . inconclusive | | v . isInconclusive ( ) | | inconclusive ) ;
// Don't add the value a second time
if ( std : : find ( tok - > values ( ) . begin ( ) , tok - > values ( ) . end ( ) , value ) ! = tok - > values ( ) . end ( ) )
continue ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
2018-11-16 06:12:28 +01:00
}
}
void byVal ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings ) const {
2019-02-22 06:38:56 +01:00
byVal ( tok , tokenlist , errorLogger , settings , [ ] ( const Token * ) {
2018-11-16 06:12:28 +01:00
return true ;
} ) ;
}
2018-11-21 08:43:57 +01:00
template < class Predicate >
void byDerefCopy ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings , Predicate pred ) const {
2019-09-11 19:25:09 +02:00
if ( ! settings - > inconclusive & & inconclusive )
return ;
2019-07-26 07:02:07 +02:00
if ( ! argtok )
return ;
2018-11-21 08:43:57 +01:00
for ( const ValueFlow : : Value & v : argtok - > values ( ) ) {
if ( ! v . isLifetimeValue ( ) )
continue ;
const Token * tok2 = v . tokvalue ;
2019-01-29 09:47:52 +01:00
ErrorPath er = v . errorPath ;
const Variable * var = getLifetimeVariable ( tok2 , er ) ;
er . insert ( er . end ( ) , errorPath . begin ( ) , errorPath . end ( ) ) ;
2018-11-21 08:43:57 +01:00
if ( ! var )
continue ;
2018-12-02 14:31:31 +01:00
for ( const Token * tok3 = tok ; tok3 & & tok3 ! = var - > declEndToken ( ) ; tok3 = tok3 - > previous ( ) ) {
2018-11-21 08:43:57 +01:00
if ( tok3 - > varId ( ) = = var - > declarationId ( ) ) {
2019-09-11 19:25:09 +02:00
LifetimeStore { tok3 , message , type , inconclusive } . byVal ( tok , tokenlist , errorLogger , settings , pred ) ;
2018-11-21 08:43:57 +01:00
break ;
}
}
}
}
void byDerefCopy ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings ) const {
2019-02-22 06:38:56 +01:00
byDerefCopy ( tok , tokenlist , errorLogger , settings , [ ] ( const Token * ) {
2018-11-21 08:43:57 +01:00
return true ;
} ) ;
}
2018-11-16 06:12:28 +01:00
} ;
static void valueFlowLifetimeFunction ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
if ( ! Token : : Match ( tok , " %name% ( " ) )
return ;
2019-12-25 09:32:50 +01:00
int returnContainer = settings - > library . returnValueContainer ( tok ) ;
if ( returnContainer > = 0 ) {
std : : vector < const Token * > args = getArguments ( tok ) ;
for ( int argnr = 1 ; argnr < = args . size ( ) ; + + argnr ) {
const Library : : ArgumentChecks : : IteratorInfo * i = settings - > library . getArgIteratorInfo ( tok , argnr ) ;
if ( ! i )
continue ;
if ( i - > container ! = returnContainer )
continue ;
const Token * const argTok = args [ argnr - 1 ] ;
2019-12-31 08:09:04 +01:00
// Check if lifetime is available to avoid adding the lifetime twice
ValueFlow : : Value val = getLifetimeObjValue ( argTok ) ;
if ( val . tokvalue ) {
LifetimeStore { argTok , " Passed to ' " + tok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Iterator } . byVal (
tok - > next ( ) , tokenlist , errorLogger , settings ) ;
break ;
}
2019-12-25 09:32:50 +01:00
}
} else if ( Token : : Match ( tok - > tokAt ( - 2 ) , " std :: ref|cref|tie|front_inserter|back_inserter " ) ) {
2018-11-16 06:12:28 +01:00
for ( const Token * argtok : getArguments ( tok ) ) {
2019-07-16 10:51:07 +02:00
LifetimeStore { argtok , " Passed to ' " + tok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object } . byRef (
2018-11-16 06:12:28 +01:00
tok - > next ( ) , tokenlist , errorLogger , settings ) ;
}
} else if ( Token : : Match ( tok - > tokAt ( - 2 ) , " std :: make_tuple|tuple_cat|make_pair|make_reverse_iterator|next|prev|move " ) ) {
for ( const Token * argtok : getArguments ( tok ) ) {
2019-07-16 10:51:07 +02:00
LifetimeStore { argtok , " Passed to ' " + tok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object } . byVal (
2018-11-16 06:12:28 +01:00
tok - > next ( ) , tokenlist , errorLogger , settings ) ;
2018-11-14 06:59:25 +01:00
}
2018-11-21 08:43:57 +01:00
} else if ( Token : : Match ( tok - > tokAt ( - 2 ) , " %var% . push_back|push_front|insert|push|assign " ) & &
astIsContainer ( tok - > tokAt ( - 2 ) ) ) {
Token * vartok = tok - > tokAt ( - 2 ) ;
std : : vector < const Token * > args = getArguments ( tok ) ;
2018-12-02 14:31:31 +01:00
std : : size_t n = args . size ( ) ;
2019-06-02 10:14:48 +02:00
if ( n > 1 & & Token : : typeStr ( args [ n - 2 ] ) = = Token : : typeStr ( args [ n - 1 ] ) & &
2018-12-02 14:31:31 +01:00
( ( ( astIsIterator ( args [ n - 2 ] ) & & astIsIterator ( args [ n - 1 ] ) ) | |
( astIsPointer ( args [ n - 2 ] ) & & astIsPointer ( args [ n - 1 ] ) ) ) ) ) {
2019-07-16 10:51:07 +02:00
LifetimeStore { args . back ( ) , " Added to container ' " + vartok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object } . byDerefCopy (
2018-11-21 08:43:57 +01:00
vartok , tokenlist , errorLogger , settings ) ;
2019-05-05 11:40:59 +02:00
} else if ( ! args . empty ( ) & & isLifetimeBorrowed ( args . back ( ) , settings ) ) {
2019-07-16 10:51:07 +02:00
LifetimeStore { args . back ( ) , " Added to container ' " + vartok - > str ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object } . byVal (
2018-11-21 08:43:57 +01:00
vartok , tokenlist , errorLogger , settings ) ;
}
2019-01-29 09:47:52 +01:00
} else if ( tok - > function ( ) ) {
const Function * f = tok - > function ( ) ;
if ( Function : : returnsReference ( f ) )
return ;
2019-09-11 19:25:09 +02:00
std : : vector < const Token * > returns = findReturns ( f ) ;
const bool inconclusive = returns . size ( ) > 1 ;
for ( const Token * returnTok : returns ) {
if ( returnTok = = tok )
2019-01-29 09:47:52 +01:00
continue ;
2019-09-11 19:25:09 +02:00
const Variable * returnVar = returnTok - > variable ( ) ;
if ( returnVar & & returnVar - > isArgument ( ) & & ( returnVar - > isConst ( ) | | ! isVariableChanged ( returnVar , settings , tokenlist - > isCPP ( ) ) ) ) {
LifetimeStore ls = LifetimeStore : : fromFunctionArg ( f , tok , returnVar , tokenlist , errorLogger ) ;
ls . inconclusive = inconclusive ;
2019-01-29 09:47:52 +01:00
ls . byVal ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
}
2019-09-11 19:25:09 +02:00
for ( const ValueFlow : : Value & v : returnTok - > values ( ) ) {
if ( ! v . isLifetimeValue ( ) )
continue ;
if ( ! v . tokvalue )
continue ;
const Variable * var = v . tokvalue - > variable ( ) ;
LifetimeStore ls = LifetimeStore : : fromFunctionArg ( f , tok , var , tokenlist , errorLogger ) ;
if ( ! ls . argtok )
continue ;
ls . inconclusive = inconclusive ;
ls . errorPath = v . errorPath ;
ls . errorPath . emplace_front ( returnTok , " Return " + lifetimeType ( returnTok , & v ) + " . " ) ;
if ( var - > isReference ( ) | | var - > isRValueReference ( ) ) {
ls . byRef ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
} else if ( v . isArgumentLifetimeValue ( ) ) {
ls . byVal ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
}
}
2019-01-29 09:47:52 +01:00
}
2018-11-14 06:59:25 +01:00
}
}
2019-09-10 19:41:35 +02:00
static void valueFlowLifetimeConstructor ( Token * tok ,
2019-09-10 19:42:17 +02:00
const Type * t ,
TokenList * tokenlist ,
ErrorLogger * errorLogger ,
const Settings * settings )
2019-03-19 06:25:10 +01:00
{
2019-09-10 19:41:35 +02:00
if ( ! t )
return ;
2019-03-19 06:25:10 +01:00
if ( ! Token : : Match ( tok , " (|{ " ) )
return ;
2019-09-10 19:41:35 +02:00
const Scope * scope = t - > classScope ;
if ( ! scope )
return ;
// Only support aggregate constructors for now
if ( scope - > numConstructors = = 0 & & t - > derivedFrom . empty ( ) & & ( t - > isClassType ( ) | | t - > isStructType ( ) ) ) {
std : : vector < const Token * > args = getArguments ( tok ) ;
std : : size_t i = 0 ;
for ( const Variable & var : scope - > varlist ) {
if ( i > = args . size ( ) )
break ;
const Token * argtok = args [ i ] ;
LifetimeStore ls {
argtok , " Passed to constructor of ' " + t - > name ( ) + " '. " , ValueFlow : : Value : : LifetimeKind : : Object } ;
if ( var . isReference ( ) | | var . isRValueReference ( ) ) {
ls . byRef ( tok , tokenlist , errorLogger , settings ) ;
} else {
ls . byVal ( tok , tokenlist , errorLogger , settings ) ;
2019-03-19 06:25:10 +01:00
}
2019-09-10 19:41:35 +02:00
i + + ;
2019-03-19 06:25:10 +01:00
}
2019-09-10 19:41:35 +02:00
}
}
2019-11-17 03:22:04 +01:00
static bool hasInitList ( const Token * tok )
{
if ( astIsPointer ( tok ) )
return true ;
if ( astIsContainer ( tok ) ) {
const Library : : Container * library = getLibraryContainer ( tok ) ;
if ( ! library )
return false ;
return library - > hasInitializerListConstructor ;
}
return false ;
}
2019-09-10 19:41:35 +02:00
static void valueFlowLifetimeConstructor ( Token * tok , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
if ( ! Token : : Match ( tok , " (|{ " ) )
return ;
Token * parent = tok - > astParent ( ) ;
while ( Token : : simpleMatch ( parent , " , " ) )
parent = parent - > astParent ( ) ;
2019-11-17 03:22:04 +01:00
if ( Token : : simpleMatch ( parent , " { " ) & & hasInitList ( parent - > astParent ( ) ) ) {
2019-09-10 19:41:35 +02:00
valueFlowLifetimeConstructor ( tok , Token : : typeOf ( parent - > previous ( ) ) , tokenlist , errorLogger , settings ) ;
2019-11-17 03:22:04 +01:00
} else if ( Token : : simpleMatch ( tok , " { " ) & & hasInitList ( parent ) ) {
2019-03-19 06:25:10 +01:00
std : : vector < const Token * > args = getArguments ( tok ) ;
2020-04-02 10:17:58 +02:00
// Assume range constructor if passed a pair of iterators
if ( astIsContainer ( parent ) & & args . size ( ) = = 2 & & astIsIterator ( args [ 0 ] ) & & astIsIterator ( args [ 1 ] ) ) {
for ( const Token * argtok : args ) {
LifetimeStore ls { argtok , " Passed to initializer list. " , ValueFlow : : Value : : LifetimeKind : : Object } ;
ls . byDerefCopy ( tok , tokenlist , errorLogger , settings ) ;
}
} else {
for ( const Token * argtok : args ) {
LifetimeStore ls { argtok , " Passed to initializer list. " , ValueFlow : : Value : : LifetimeKind : : Object } ;
ls . byVal ( tok , tokenlist , errorLogger , settings ) ;
}
2019-03-19 06:25:10 +01:00
}
2019-09-10 19:41:35 +02:00
} else if ( const Type * t = Token : : typeOf ( tok - > previous ( ) ) ) {
valueFlowLifetimeConstructor ( tok , t , tokenlist , errorLogger , settings ) ;
2019-03-19 06:25:10 +01:00
}
}
2018-11-14 06:59:25 +01:00
struct Lambda {
explicit Lambda ( const Token * tok )
: capture ( nullptr ) , arguments ( nullptr ) , returnTok ( nullptr ) , bodyTok ( nullptr ) {
if ( ! Token : : simpleMatch ( tok , " [ " ) | | ! tok - > link ( ) )
return ;
capture = tok ;
if ( Token : : simpleMatch ( capture - > link ( ) , " ] ( " ) ) {
arguments = capture - > link ( ) - > next ( ) ;
}
const Token * afterArguments = arguments ? arguments - > link ( ) - > next ( ) : capture - > link ( ) - > next ( ) ;
if ( afterArguments & & afterArguments - > originalName ( ) = = " -> " ) {
returnTok = afterArguments - > next ( ) ;
bodyTok = Token : : findsimplematch ( returnTok , " { " ) ;
} else if ( Token : : simpleMatch ( afterArguments , " { " ) ) {
bodyTok = afterArguments ;
}
}
const Token * capture ;
const Token * arguments ;
const Token * returnTok ;
const Token * bodyTok ;
bool isLambda ( ) const {
return capture & & bodyTok ;
}
} ;
2019-12-10 17:48:27 +01:00
static bool isDecayedPointer ( const Token * tok )
2018-12-15 17:58:45 +01:00
{
if ( ! tok )
return false ;
2020-05-20 16:02:13 +02:00
if ( ! tok - > astParent ( ) )
return false ;
2018-12-15 17:58:45 +01:00
if ( astIsPointer ( tok - > astParent ( ) ) & & ! Token : : simpleMatch ( tok - > astParent ( ) , " return " ) )
return true ;
2020-05-19 21:03:28 +02:00
if ( tok - > astParent ( ) - > isConstOp ( ) )
2019-02-23 08:32:08 +01:00
return true ;
2018-12-15 17:58:45 +01:00
if ( ! Token : : simpleMatch ( tok - > astParent ( ) , " return " ) )
return false ;
2019-10-12 11:40:02 +02:00
return astIsPointer ( tok - > astParent ( ) ) ;
2018-12-15 17:58:45 +01:00
}
2018-11-14 06:59:25 +01:00
static void valueFlowLifetime ( TokenList * tokenlist , SymbolDatabase * , ErrorLogger * errorLogger , const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2019-01-29 09:47:52 +01:00
if ( ! tok - > scope ( ) )
continue ;
if ( tok - > scope ( ) - > type = = Scope : : eGlobal )
continue ;
2018-11-14 06:59:25 +01:00
Lambda lam ( tok ) ;
// Lamdas
if ( lam . isLambda ( ) ) {
const Scope * bodyScope = lam . bodyTok - > scope ( ) ;
std : : set < const Scope * > scopes ;
2019-02-22 06:38:56 +01:00
auto isCapturingVariable = [ & ] ( const Token * varTok ) {
const Variable * var = varTok - > variable ( ) ;
if ( ! var )
return false ;
2019-11-05 07:10:32 +01:00
if ( ! var - > isLocal ( ) & & ! var - > isArgument ( ) )
return false ;
2018-11-16 06:12:28 +01:00
const Scope * scope = var - > scope ( ) ;
2018-12-01 19:11:26 +01:00
if ( ! scope )
return false ;
2018-11-16 06:12:28 +01:00
if ( scopes . count ( scope ) > 0 )
return false ;
if ( scope - > isNestedIn ( bodyScope ) )
return false ;
scopes . insert ( scope ) ;
return true ;
} ;
2018-11-14 06:59:25 +01:00
// TODO: Handle explicit capture
bool captureByRef = Token : : Match ( lam . capture , " [ & ] " ) ;
bool captureByValue = Token : : Match ( lam . capture , " [ = ] " ) ;
for ( const Token * tok2 = lam . bodyTok ; tok2 ! = lam . bodyTok - > link ( ) ; tok2 = tok2 - > next ( ) ) {
ErrorPath errorPath ;
if ( captureByRef ) {
2019-07-16 10:51:07 +02:00
LifetimeStore { tok2 , " Lambda captures variable by reference here. " , ValueFlow : : Value : : LifetimeKind : : Lambda } . byRef (
2018-11-16 06:12:28 +01:00
tok , tokenlist , errorLogger , settings , isCapturingVariable ) ;
2018-11-14 06:59:25 +01:00
} else if ( captureByValue ) {
2019-07-16 10:51:07 +02:00
LifetimeStore { tok2 , " Lambda captures variable by value here. " , ValueFlow : : Value : : LifetimeKind : : Lambda } . byVal (
2018-11-16 06:12:28 +01:00
tok , tokenlist , errorLogger , settings , isCapturingVariable ) ;
2018-11-14 06:59:25 +01:00
}
}
}
// address of
else if ( tok - > isUnaryOp ( " & " ) ) {
2019-09-11 19:25:09 +02:00
for ( const LifetimeToken & lt : getLifetimeTokens ( tok - > astOperand1 ( ) ) ) {
if ( ! settings - > inconclusive & & lt . inconclusive )
continue ;
ErrorPath errorPath = lt . errorPath ;
errorPath . emplace_back ( tok , " Address of variable taken here. " ) ;
2018-11-14 06:59:25 +01:00
2019-09-11 19:25:09 +02:00
ValueFlow : : Value value ;
value . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Local ;
value . tokvalue = lt . token ;
value . errorPath = std : : move ( errorPath ) ;
if ( astIsPointer ( lt . token ) | | ! Token : : Match ( lt . token - > astParent ( ) , " .|[ " ) )
value . lifetimeKind = ValueFlow : : Value : : LifetimeKind : : Address ;
value . setInconclusive ( lt . inconclusive ) ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
2018-11-14 06:59:25 +01:00
2019-09-11 19:25:09 +02:00
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
2018-11-14 06:59:25 +01:00
}
// container lifetimes
2019-08-15 21:14:27 +02:00
else if ( astIsContainer ( tok ) ) {
Token * parent = astParentSkipParens ( tok ) ;
if ( ! Token : : Match ( parent , " . %name% ( " ) )
2018-11-14 06:59:25 +01:00
continue ;
2019-08-15 21:14:27 +02:00
LifetimeStore ls ;
2018-11-14 06:59:25 +01:00
2019-08-15 21:14:27 +02:00
if ( astIsIterator ( parent - > tokAt ( 2 ) ) )
ls = LifetimeStore { tok , " Iterator to container is created here. " , ValueFlow : : Value : : LifetimeKind : : Iterator } ;
else if ( astIsPointer ( parent - > tokAt ( 2 ) ) | | Token : : Match ( parent - > next ( ) , " data|c_str " ) )
ls = LifetimeStore { tok , " Pointer to container is created here. " , ValueFlow : : Value : : LifetimeKind : : Object } ;
else
continue ;
2018-11-14 06:59:25 +01:00
2019-08-15 21:14:27 +02:00
// Dereferencing
if ( tok - > isUnaryOp ( " * " ) | | parent - > originalName ( ) = = " -> " )
ls . byDerefCopy ( parent - > tokAt ( 2 ) , tokenlist , errorLogger , settings ) ;
else
ls . byRef ( parent - > tokAt ( 2 ) , tokenlist , errorLogger , settings ) ;
2018-11-14 06:59:25 +01:00
}
2019-03-19 06:25:10 +01:00
// Check constructors
else if ( Token : : Match ( tok , " =|return|%type%|%var% { " ) ) {
valueFlowLifetimeConstructor ( tok - > next ( ) , tokenlist , errorLogger , settings ) ;
}
2018-11-16 06:12:28 +01:00
// Check function calls
else if ( Token : : Match ( tok , " %name% ( " ) ) {
valueFlowLifetimeFunction ( tok , tokenlist , errorLogger , settings ) ;
}
2018-11-14 06:59:25 +01:00
// Check variables
else if ( tok - > variable ( ) ) {
ErrorPath errorPath ;
const Variable * var = getLifetimeVariable ( tok , errorPath ) ;
if ( ! var )
continue ;
2018-12-15 17:58:45 +01:00
if ( var - > nameToken ( ) = = tok )
continue ;
2019-12-10 17:48:27 +01:00
if ( var - > isArray ( ) & & ! var - > isStlType ( ) & & ! var - > isArgument ( ) & & isDecayedPointer ( tok ) ) {
2018-11-14 06:59:25 +01:00
errorPath . emplace_back ( tok , " Array decayed to pointer here. " ) ;
ValueFlow : : Value value ;
2019-07-16 11:12:35 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : LIFETIME ;
value . lifetimeScope = ValueFlow : : Value : : LifetimeScope : : Local ;
2018-11-14 06:59:25 +01:00
value . tokvalue = var - > nameToken ( ) ;
value . errorPath = errorPath ;
setTokenValue ( tok , value , tokenlist - > getSettings ( ) ) ;
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
}
}
}
}
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 ;
2019-07-10 15:27:07 +02:00
ValueFlow : : Value : : MoveKind kind = ValueFlow : : Value : : MoveKind : : NonMovedVariable ;
2016-11-20 15:14:49 +01:00
Token * variableToken = nullptr ;
if ( Token : : Match ( tok , " std :: move ( %var% ) " ) ) {
variableToken = tok - > tokAt ( 4 ) ;
2019-07-10 15:27:07 +02:00
kind = ValueFlow : : Value : : MoveKind : : MovedVariable ;
2016-11-20 15:14:49 +01:00
} else if ( Token : : simpleMatch ( tok , " std :: forward < " ) ) {
2018-09-23 20:24:51 +02:00
const Token * const leftAngle = tok - > tokAt ( 3 ) ;
2016-11-20 15:14:49 +01:00
Token * rightAngle = leftAngle - > link ( ) ;
if ( Token : : Match ( rightAngle , " > ( %var% ) " ) ) {
variableToken = rightAngle - > tokAt ( 2 ) ;
2019-07-10 15:27:07 +02:00
kind = ValueFlow : : Value : : MoveKind : : ForwardedVariable ;
2016-11-20 15:14:49 +01:00
}
}
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 ;
2019-12-21 07:39:14 +01:00
if ( variableToken - > valueType ( ) & & variableToken - > valueType ( ) - > type > = ValueType : : Type : : VOID )
return false ;
2016-11-20 15:14:49 +01:00
if ( moveKind ! = nullptr )
* moveKind = kind ;
if ( varTok ! = nullptr )
* varTok = variableToken ;
return true ;
}
2019-07-15 13:47:17 +02:00
static bool isOpenParenthesisMemberFunctionCallOfVarId ( const Token * openParenthesisToken , nonneg int varId )
2016-12-11 22:32:57 +01:00
{
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 * 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 ;
2019-07-10 15:27:07 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : MOVED ;
value . moveKind = ValueFlow : : Value : : MoveKind : : 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 ;
2019-07-15 13:47:17 +02:00
const int varId = varTok - > varId ( ) ;
2020-01-11 09:17:32 +01:00
const Token * const endOfVarScope = var - > scope ( ) - > bodyEnd ;
2016-12-12 14:29:27 +01:00
setTokenValue ( varTok , value , settings ) ;
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable (
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 ;
2019-07-15 13:47:17 +02:00
const 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 ;
2018-12-02 14:36:01 +01:00
if ( parent & & parent - > astOperand1 ( ) & & parent - > astOperand1 ( ) - > varId ( ) = = varId )
2016-11-20 15:14:49 +01:00
continue ;
const Variable * var = varTok - > variable ( ) ;
if ( ! var )
continue ;
2020-01-11 09:17:32 +01:00
const Token * const endOfVarScope = var - > scope ( ) - > bodyEnd ;
2016-11-20 15:14:49 +01:00
ValueFlow : : Value value ;
2019-07-10 15:27:07 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : MOVED ;
2016-11-20 15:14:49 +01:00
value . moveKind = moveKind ;
2019-07-10 15:27:07 +02:00
if ( moveKind = = ValueFlow : : Value : : MoveKind : : 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 )
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( const_cast < Token * > ( endOfFunctionCall ) ,
endOfVarScope ,
var ,
varId ,
values ,
false ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2016-11-20 15:14:49 +01:00
}
}
}
2019-04-26 11:30:09 +02:00
static void valueFlowForwardAssign ( Token * const tok ,
2019-04-26 11:30:35 +02:00
const Variable * const var ,
std : : list < ValueFlow : : Value > values ,
const bool constValue ,
const bool init ,
TokenList * const tokenlist ,
ErrorLogger * const errorLogger ,
const Settings * const settings )
2019-04-26 11:30:09 +02:00
{
2020-04-19 08:28:07 +02:00
const Token * endOfVarScope = nullptr ;
if ( var - > isLocal ( ) )
endOfVarScope = var - > scope ( ) - > bodyEnd ;
if ( ! endOfVarScope )
endOfVarScope = tok - > scope ( ) - > bodyEnd ;
2019-04-26 11:30:09 +02:00
if ( std : : any_of ( values . begin ( ) , values . end ( ) , std : : mem_fn ( & ValueFlow : : Value : : isLifetimeValue ) ) ) {
valueFlowForwardLifetime ( tok , tokenlist , errorLogger , settings ) ;
values . remove_if ( std : : mem_fn ( & ValueFlow : : Value : : isLifetimeValue ) ) ;
}
if ( ! var - > isPointer ( ) & & ! var - > isSmartPointer ( ) )
values . remove_if ( std : : mem_fn ( & ValueFlow : : Value : : isTokValue ) ) ;
if ( tok - > astParent ( ) ) {
for ( std : : list < ValueFlow : : Value > : : iterator it = values . begin ( ) ; it ! = values . end ( ) ; + + it ) {
const std : : string info = " Assignment ' " + tok - > astParent ( ) - > expressionString ( ) + " ', assigned value is " + it - > infoString ( ) ;
it - > errorPath . emplace_back ( tok , info ) ;
2019-04-26 11:30:35 +02:00
}
2019-04-26 11:30:09 +02:00
}
if ( tokenlist - > isCPP ( ) & & Token : : Match ( var - > typeStartToken ( ) , " bool|_Bool " ) ) {
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 ( ) )
2019-06-30 21:39:22 +02:00
it - > intvalue = ( it - > tokvalue ! = nullptr ) ;
2019-04-26 11:30:09 +02:00
}
}
// Static variable initialisation?
if ( var - > isStatic ( ) & & init )
2019-09-20 15:06:37 +02:00
lowerToPossible ( values ) ;
2019-04-26 11:30:09 +02:00
// Skip RHS
const Token * nextExpression = tok - > astParent ( ) ? nextAfterAstRightmostLeaf ( tok - > astParent ( ) ) : tok - > next ( ) ;
if ( std : : any_of ( values . begin ( ) , values . end ( ) , std : : mem_fn ( & ValueFlow : : Value : : isTokValue ) ) ) {
std : : list < ValueFlow : : Value > tokvalues ;
std : : copy_if ( values . begin ( ) ,
values . end ( ) ,
std : : back_inserter ( tokvalues ) ,
std : : mem_fn ( & ValueFlow : : Value : : isTokValue ) ) ;
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( const_cast < Token * > ( nextExpression ) ,
endOfVarScope ,
var ,
var - > declarationId ( ) ,
tokvalues ,
constValue ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2019-04-26 11:30:09 +02:00
values . remove_if ( std : : mem_fn ( & ValueFlow : : Value : : isTokValue ) ) ;
}
2019-09-03 17:15:58 +02:00
for ( ValueFlow : : Value & value : values )
value . tokvalue = tok ;
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( const_cast < Token * > ( nextExpression ) ,
endOfVarScope ,
var ,
var - > declarationId ( ) ,
values ,
constValue ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2019-04-26 11:30:09 +02:00
}
2019-05-01 17:05:16 +02:00
static std : : list < ValueFlow : : Value > truncateValues ( std : : list < ValueFlow : : Value > values , const ValueType * valueType , const Settings * settings )
{
if ( ! valueType | | ! valueType - > isIntegral ( ) )
return values ;
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
const size_t sz = ValueFlow : : getSizeOf ( * valueType , settings ) ;
2019-05-01 17:05:16 +02:00
for ( ValueFlow : : Value & value : values ) {
if ( value . isFloatValue ( ) ) {
value . intvalue = value . floatValue ;
value . valueType = ValueFlow : : Value : : INT ;
}
if ( value . isIntValue ( ) & & sz > 0 & & sz < 8 ) {
const MathLib : : biguint unsignedMaxValue = ( 1ULL < < ( sz * 8 ) ) - 1ULL ;
const MathLib : : biguint signBit = 1ULL < < ( sz * 8 - 1 ) ;
value . intvalue & = unsignedMaxValue ;
if ( valueType - > sign = = ValueType : : Sign : : SIGNED & & ( value . intvalue & signBit ) )
value . intvalue | = ~ unsignedMaxValue ;
}
}
return values ;
}
2019-06-30 17:49:44 +02:00
static bool isLiteralNumber ( const Token * tok , bool cpp )
{
2020-05-19 21:03:28 +02:00
return tok - > isNumber ( ) | | tok - > isEnumerator ( ) | | tok - > str ( ) = = " NULL " | | ( cpp & & Token : : Match ( tok , " false|true|nullptr " ) ) ;
2019-06-30 17:49:44 +02:00
}
2015-02-01 12:10:20 +01:00
static void valueFlowAfterAssign ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-01-21 21:13:49 +01:00
{
2018-07-14 22:26:22 +02:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
2019-07-15 13:47:17 +02:00
std : : set < 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
2018-11-03 15:53:24 +01:00
if ( ! tok - > astOperand1 ( ) | | ! tok - > astOperand1 ( ) - > varId ( ) | | tok - > astOperand1 ( ) - > hasKnownValue ( ) )
2015-02-01 12:10:20 +01:00
continue ;
2019-07-15 13:47:17 +02:00
const 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
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
2019-05-01 17:05:16 +02:00
std : : list < ValueFlow : : Value > values = truncateValues ( tok - > astOperand2 ( ) - > values ( ) , tok - > astOperand1 ( ) - > valueType ( ) , settings ) ;
2019-06-30 17:49:44 +02:00
const bool constValue = isLiteralNumber ( tok - > astOperand2 ( ) , tokenlist - > isCPP ( ) ) ;
2019-04-26 11:30:09 +02:00
const bool init = var - > nameToken ( ) = = tok - > astOperand1 ( ) ;
2019-06-16 10:09:38 +02:00
valueFlowForwardAssign ( tok - > astOperand2 ( ) , var , values , constValue , init , 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
2019-09-20 15:06:37 +02:00
static void valueFlowSetConditionToKnown ( const Token * tok , std : : list < ValueFlow : : Value > & values , bool then )
{
if ( values . empty ( ) )
return ;
if ( then & & ! Token : : Match ( tok , " ==|!|( " ) )
return ;
if ( ! then & & ! Token : : Match ( tok , " !=|%var%|( " ) )
return ;
if ( isConditionKnown ( tok , then ) )
changePossibleToKnown ( values ) ;
2019-08-08 07:46:47 +02:00
}
static bool isBreakScope ( const Token * const endToken )
{
if ( ! Token : : simpleMatch ( endToken , " } " ) )
return false ;
if ( ! Token : : simpleMatch ( endToken - > link ( ) , " { " ) )
return false ;
return Token : : findmatch ( endToken - > link ( ) , " break|goto " , endToken ) ;
}
2019-09-20 15:06:37 +02:00
static ValueFlow : : Value asImpossible ( ValueFlow : : Value v )
{
v . invertRange ( ) ;
v . setImpossible ( ) ;
return v ;
}
2019-09-28 20:50:56 +02:00
static void insertImpossible ( std : : list < ValueFlow : : Value > & values , const std : : list < ValueFlow : : Value > & input )
2019-09-20 15:06:37 +02:00
{
std : : transform ( input . begin ( ) , input . end ( ) , std : : back_inserter ( values ) , & asImpossible ) ;
}
2019-09-26 10:32:25 +02:00
static std : : vector < const Variable * > getExprVariables ( const Token * expr ,
2019-09-26 10:32:49 +02:00
const TokenList * tokenlist ,
const SymbolDatabase * symboldatabase ,
const Settings * settings )
2019-09-26 10:32:25 +02:00
{
std : : vector < const Variable * > result ;
FwdAnalysis fwdAnalysis ( tokenlist - > isCPP ( ) , settings - > library ) ;
std : : set < int > varids = fwdAnalysis . getExprVarIds ( expr ) ;
std : : transform ( varids . begin ( ) , varids . end ( ) , std : : back_inserter ( result ) , [ & ] ( int id ) {
return symboldatabase - > getVariableFromVarId ( id ) ;
} ) ;
return result ;
}
2018-11-24 10:07:12 +01:00
struct ValueFlowConditionHandler {
struct Condition {
const Token * vartok ;
std : : list < ValueFlow : : Value > true_values ;
std : : list < ValueFlow : : Value > false_values ;
2015-02-01 12:10:20 +01:00
2018-11-24 10:07:12 +01:00
Condition ( ) : vartok ( nullptr ) , true_values ( ) , false_values ( ) { }
} ;
2019-09-26 10:32:25 +02:00
std : : function < bool ( Token * start , const Token * stop , const Token * exprTok , const std : : list < ValueFlow : : Value > & values , bool constValue ) >
2019-09-26 10:32:49 +02:00
forward ;
2018-11-24 10:07:12 +01:00
std : : function < Condition ( Token * tok ) > parse ;
void afterCondition ( TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
ErrorLogger * errorLogger ,
const Settings * settings ) const {
for ( const Scope * scope : symboldatabase - > functionScopes ) {
std : : set < unsigned > aliased ;
for ( Token * tok = const_cast < Token * > ( scope - > bodyStart ) ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
2019-09-30 21:04:43 +02:00
if ( Token : : Match ( tok , " if|while|for ( " ) )
continue ;
2018-11-24 10:07:12 +01:00
if ( Token : : Match ( tok , " = & %var% ; " ) )
aliased . insert ( tok - > tokAt ( 2 ) - > varId ( ) ) ;
2019-09-26 10:32:25 +02:00
const Token * top = tok - > astTop ( ) ;
if ( ! top )
continue ;
if ( ! Token : : Match ( top - > previous ( ) , " if|while|for ( " ) & & ! Token : : Match ( tok - > astParent ( ) , " &&|%oror% " ) )
continue ;
2018-11-24 10:07:12 +01:00
Condition cond = parse ( tok ) ;
if ( ! cond . vartok )
2015-02-01 12:10:20 +01:00
continue ;
2020-04-19 17:29:40 +02:00
if ( cond . vartok - > variable ( ) & & cond . vartok - > variable ( ) - > isVolatile ( ) )
continue ;
2018-11-24 10:07:12 +01:00
if ( cond . true_values . empty ( ) | | cond . false_values . empty ( ) )
2018-04-05 06:51:31 +02:00
continue ;
2019-09-26 10:32:25 +02:00
2019-09-30 21:04:43 +02:00
if ( exprDependsOnThis ( cond . vartok ) )
continue ;
2019-09-26 10:32:25 +02:00
std : : vector < const Variable * > vars = getExprVariables ( cond . vartok , tokenlist , symboldatabase , settings ) ;
2019-10-01 08:39:08 +02:00
if ( std : : any_of ( vars . begin ( ) , vars . end ( ) , [ ] ( const Variable * var ) {
return ! var ;
} ) )
continue ;
2019-09-30 21:04:43 +02:00
if ( ! vars . empty ( ) & & ( vars . front ( ) ) )
if ( std : : any_of ( vars . begin ( ) , vars . end ( ) , [ & ] ( const Variable * var ) {
2019-10-01 08:39:08 +02:00
return var & & aliased . find ( var - > declarationId ( ) ) ! = aliased . end ( ) ;
} ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
cond . vartok ,
" variable is aliased so we just skip all valueflow after condition " ) ;
continue ;
}
2018-04-05 06:51:31 +02:00
2018-11-24 10:07:12 +01:00
if ( Token : : Match ( tok - > astParent ( ) , " %oror%|&& " ) ) {
2019-06-16 10:09:38 +02:00
Token * parent = tok - > astParent ( ) ;
2018-11-24 10:07:12 +01:00
const std : : string & op ( parent - > str ( ) ) ;
if ( parent - > astOperand1 ( ) = = tok & & ( ( op = = " && " & & Token : : Match ( tok , " ==|>=|<=|! " ) ) | |
( op = = " || " & & Token : : Match ( tok , " %name%|!= " ) ) ) ) {
2019-06-16 10:09:38 +02:00
for ( ; parent & & parent - > str ( ) = = op ; parent = parent - > astParent ( ) ) {
2018-11-24 10:07:12 +01:00
std : : stack < Token * > tokens ;
2019-06-16 10:09:38 +02:00
tokens . push ( parent - > astOperand2 ( ) ) ;
2018-11-24 10:07:12 +01:00
bool assign = false ;
while ( ! tokens . empty ( ) ) {
Token * rhstok = tokens . top ( ) ;
tokens . pop ( ) ;
if ( ! rhstok )
continue ;
2019-06-16 10:09:38 +02:00
tokens . push ( rhstok - > astOperand1 ( ) ) ;
tokens . push ( rhstok - > astOperand2 ( ) ) ;
2019-09-26 10:32:25 +02:00
if ( isSameExpression (
tokenlist - > isCPP ( ) , false , cond . vartok , rhstok , settings - > library , true , false ) )
2018-11-24 10:07:12 +01:00
setTokenValue ( rhstok , cond . true_values . front ( ) , settings ) ;
2019-09-26 10:32:25 +02:00
else if ( Token : : Match ( rhstok , " ++|--|= " ) & & isSameExpression ( tokenlist - > isCPP ( ) ,
2019-09-26 10:32:49 +02:00
false ,
cond . vartok ,
rhstok - > astOperand1 ( ) ,
settings - > library ,
true ,
false ) ) {
2018-11-24 10:07:12 +01:00
assign = true ;
break ;
}
2015-02-01 12:10:20 +01:00
}
2018-11-24 10:07:12 +01:00
if ( assign )
break ;
while ( parent - > astParent ( ) & & parent = = parent - > astParent ( ) - > astOperand2 ( ) )
2019-06-16 10:09:38 +02:00
parent = parent - > astParent ( ) ;
2018-11-24 10:07:12 +01:00
}
2014-06-19 17:29:41 +02:00
}
}
2014-07-07 17:48:58 +02:00
2018-11-24 10:07:12 +01:00
if ( top & & Token : : Match ( top - > previous ( ) , " if|while ( " ) & & ! top - > previous ( ) - > isExpandedMacro ( ) ) {
// does condition reassign variable?
if ( tok ! = top - > astOperand2 ( ) & & Token : : Match ( top - > astOperand2 ( ) , " %oror%|&& " ) & &
2019-09-26 10:32:25 +02:00
isVariablesChanged ( top , top - > link ( ) , 0 , vars , settings , tokenlist - > isCPP ( ) ) ) {
2018-11-24 10:07:12 +01:00
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , tok , " assignment in condition " ) ;
continue ;
2018-04-08 09:24:01 +02:00
}
2014-08-26 18:48:11 +02:00
2019-09-20 15:06:37 +02:00
std : : list < ValueFlow : : Value > thenValues ;
std : : list < ValueFlow : : Value > elseValues ;
2018-04-08 09:30:13 +02:00
2019-09-26 10:32:25 +02:00
if ( ! Token : : Match ( tok , " !=|= " ) & & tok ! = cond . vartok ) {
2019-09-20 15:06:37 +02:00
thenValues . insert ( thenValues . end ( ) , cond . true_values . begin ( ) , cond . true_values . end ( ) ) ;
if ( isConditionKnown ( tok , false ) )
insertImpossible ( elseValues , cond . false_values ) ;
}
2019-09-26 10:32:25 +02:00
if ( ! Token : : Match ( tok , " ==|! " ) ) {
2019-09-20 15:06:37 +02:00
elseValues . insert ( elseValues . end ( ) , cond . false_values . begin ( ) , cond . false_values . end ( ) ) ;
if ( isConditionKnown ( tok , true ) )
insertImpossible ( thenValues , cond . true_values ) ;
}
2018-04-08 09:24:01 +02:00
2019-09-20 15:06:37 +02:00
// start token of conditional code
Token * startTokens [ ] = { nullptr , nullptr } ;
2018-11-24 10:07:12 +01:00
// if astParent is "!" we need to invert codeblock
{
2019-05-29 09:45:15 +02:00
const Token * tok2 = tok ;
2019-05-30 14:41:14 +02:00
while ( tok2 - > astParent ( ) ) {
2019-05-29 09:45:15 +02:00
const Token * parent = tok2 - > astParent ( ) ;
while ( parent & & parent - > str ( ) = = " && " )
parent = parent - > astParent ( ) ;
if ( parent & & ( parent - > str ( ) = = " ! " | | Token : : simpleMatch ( parent , " == false " ) ) ) {
2019-09-20 15:06:37 +02:00
std : : swap ( thenValues , elseValues ) ;
2019-05-29 09:45:15 +02:00
}
tok2 = parent ;
2018-11-24 10:07:12 +01:00
}
2018-04-08 09:30:13 +02:00
}
2018-04-08 09:24:01 +02:00
2018-11-24 10:07:12 +01:00
// determine startToken(s)
2019-09-20 15:06:37 +02:00
if ( Token : : simpleMatch ( top - > link ( ) , " ) { " ) )
2018-11-24 10:07:12 +01:00
startTokens [ 0 ] = top - > link ( ) - > next ( ) ;
2019-09-20 15:06:37 +02:00
if ( Token : : simpleMatch ( top - > link ( ) - > linkAt ( 1 ) , " } else { " ) )
2018-11-24 10:07:12 +01:00
startTokens [ 1 ] = top - > link ( ) - > linkAt ( 1 ) - > tokAt ( 2 ) ;
2015-02-01 12:10:20 +01:00
2019-09-20 15:06:37 +02:00
int changeBlock = - 1 ;
2014-08-26 18:48:11 +02:00
2018-11-24 10:07:12 +01:00
for ( int i = 0 ; i < 2 ; i + + ) {
const Token * const startToken = startTokens [ i ] ;
if ( ! startToken )
continue ;
2019-09-20 15:06:37 +02:00
std : : list < ValueFlow : : Value > & values = ( i = = 0 ? thenValues : elseValues ) ;
2019-08-08 07:46:47 +02:00
valueFlowSetConditionToKnown ( tok , values , i = = 0 ) ;
2019-09-20 15:06:37 +02:00
2019-09-26 10:32:25 +02:00
// TODO: The endToken should not be startTokens[i]->link() in the valueFlowForwardVariable call
if ( forward ( startTokens [ i ] , startTokens [ i ] - > link ( ) , cond . vartok , values , true ) )
2019-09-20 15:06:37 +02:00
changeBlock = i ;
changeKnownToPossible ( values ) ;
2018-11-24 10:07:12 +01:00
}
2019-09-20 15:06:37 +02:00
// TODO: Values changed in noreturn blocks should not bail
if ( changeBlock > = 0 & & ! Token : : simpleMatch ( top - > previous ( ) , " while ( " ) ) {
if ( settings - > debugwarnings )
bailout ( tokenlist ,
errorLogger ,
startTokens [ changeBlock ] - > link ( ) ,
2019-09-26 10:32:25 +02:00
" valueFlowAfterCondition: " + cond . vartok - > expressionString ( ) +
2019-09-26 10:32:49 +02:00
" is changed in conditional block " ) ;
2018-11-24 10:07:12 +01:00
continue ;
2019-09-20 15:06:37 +02:00
}
2018-11-24 10:07:12 +01:00
// After conditional code..
if ( Token : : simpleMatch ( top - > link ( ) , " ) { " ) ) {
Token * after = top - > link ( ) - > linkAt ( 1 ) ;
2020-05-21 08:47:48 +02:00
const Token * unknownFunction = nullptr ;
bool dead_if = isReturnScope ( after , & settings - > library , & unknownFunction ) | |
2019-08-08 07:46:47 +02:00
( tok - > astParent ( ) & & Token : : simpleMatch ( tok - > astParent ( ) - > previous ( ) , " while ( " ) & &
! isBreakScope ( after ) ) ;
2018-11-24 10:07:12 +01:00
bool dead_else = false ;
2020-05-21 08:47:48 +02:00
if ( ! dead_if & & unknownFunction ) {
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , unknownFunction , " possible noreturn scope " ) ;
continue ;
}
2018-11-24 10:07:12 +01:00
if ( Token : : simpleMatch ( after , " } else { " ) ) {
after = after - > linkAt ( 2 ) ;
2020-05-21 08:47:48 +02:00
unknownFunction = nullptr ;
dead_else = isReturnScope ( after , & settings - > library , & unknownFunction ) ;
if ( ! dead_else & & unknownFunction ) {
2018-11-24 10:07:12 +01:00
if ( settings - > debugwarnings )
2020-05-21 08:47:48 +02:00
bailout ( tokenlist , errorLogger , unknownFunction , " possible noreturn scope " ) ;
2018-11-24 10:07:12 +01:00
continue ;
}
}
2019-09-20 15:06:37 +02:00
if ( dead_if & & dead_else )
continue ;
2018-11-24 10:07:12 +01:00
2019-09-20 15:06:37 +02:00
std : : list < ValueFlow : : Value > values ;
if ( dead_if ) {
values = elseValues ;
} else if ( dead_else ) {
values = thenValues ;
} else {
std : : copy_if ( thenValues . begin ( ) ,
thenValues . end ( ) ,
std : : back_inserter ( values ) ,
std : : mem_fn ( & ValueFlow : : Value : : isPossible ) ) ;
std : : copy_if ( elseValues . begin ( ) ,
elseValues . end ( ) ,
std : : back_inserter ( values ) ,
std : : mem_fn ( & ValueFlow : : Value : : isPossible ) ) ;
}
if ( ! values . empty ( ) ) {
2019-08-08 07:46:47 +02:00
if ( ( dead_if | | dead_else ) & & ! Token : : Match ( tok - > astParent ( ) , " &&|& " ) ) {
2019-09-20 15:06:37 +02:00
valueFlowSetConditionToKnown ( tok , values , true ) ;
valueFlowSetConditionToKnown ( tok , values , false ) ;
2019-08-08 07:46:47 +02:00
}
2018-11-24 10:07:12 +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 ;
2019-09-26 10:32:25 +02:00
forward ( after , top - > scope ( ) - > bodyEnd , cond . vartok , values , constValue ) ;
2018-11-24 10:07:12 +01:00
}
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
}
}
2018-11-24 10:07:12 +01:00
} ;
static void valueFlowAfterCondition ( TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
ErrorLogger * errorLogger ,
const Settings * settings )
{
ValueFlowConditionHandler handler ;
2019-09-26 10:32:25 +02:00
handler . forward = [ & ] ( Token * start ,
const Token * stop ,
const Token * vartok ,
const std : : list < ValueFlow : : Value > & values ,
2019-09-26 10:32:49 +02:00
bool constValue ) {
2019-09-26 10:32:25 +02:00
valueFlowForward ( start - > next ( ) , stop , vartok , values , constValue , false , tokenlist , errorLogger , settings ) ;
std : : vector < const Variable * > vars = getExprVariables ( vartok , tokenlist , symboldatabase , settings ) ;
return isVariablesChanged ( start , stop , 0 , vars , settings , tokenlist - > isCPP ( ) ) ;
2018-11-24 10:07:12 +01:00
} ;
handler . parse = [ & ] ( const Token * tok ) {
ValueFlowConditionHandler : : Condition cond ;
ValueFlow : : Value true_value ;
ValueFlow : : Value false_value ;
const Token * vartok = parseCompareInt ( tok , true_value , false_value ) ;
if ( vartok ) {
if ( vartok - > str ( ) = = " = " & & vartok - > astOperand1 ( ) & & vartok - > astOperand2 ( ) )
vartok = vartok - > astOperand1 ( ) ;
cond . true_values . push_back ( true_value ) ;
cond . false_values . push_back ( false_value ) ;
cond . vartok = vartok ;
return cond ;
}
if ( tok - > str ( ) = = " ! " ) {
vartok = tok - > astOperand1 ( ) ;
2019-09-26 10:32:25 +02:00
} else if ( tok - > astParent ( ) & & ( Token : : Match ( tok - > astParent ( ) , " %oror%|&& " ) | |
Token : : Match ( tok - > astParent ( ) - > previous ( ) , " if|while ( " ) ) ) {
if ( Token : : simpleMatch ( tok , " = " ) )
vartok = tok - > astOperand1 ( ) ;
else if ( ! Token : : Match ( tok , " %comp%|%assign% " ) )
vartok = tok ;
2018-11-24 10:07:12 +01:00
}
2019-09-26 10:32:25 +02:00
if ( ! vartok )
2018-11-24 10:07:12 +01:00
return cond ;
cond . true_values . emplace_back ( tok , 0LL ) ;
2019-09-20 15:06:37 +02:00
cond . false_values . emplace_back ( tok , 0LL ) ;
2018-11-24 10:07:12 +01:00
cond . vartok = vartok ;
return cond ;
} ;
handler . afterCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
2014-01-21 21:13:49 +01:00
}
2019-09-20 15:06:37 +02:00
static bool isInBounds ( const ValueFlow : : Value & value , MathLib : : bigint x )
{
if ( value . intvalue = = x )
return true ;
if ( value . bound = = ValueFlow : : Value : : Bound : : Lower & & value . intvalue > x )
return false ;
if ( value . bound = = ValueFlow : : Value : : Bound : : Upper & & value . intvalue < x )
return false ;
// Checking for equality is not necessary since we already know the value is not equal
if ( value . bound = = ValueFlow : : Value : : Bound : : Point )
return false ;
return true ;
}
2020-02-01 08:24:31 +01:00
static const ValueFlow : : Value * getCompareIntValue ( const std : : list < ValueFlow : : Value > & values , std : : function < bool ( MathLib : : bigint , MathLib : : bigint ) > compare )
2019-12-26 15:47:53 +01:00
{
const ValueFlow : : Value * result = nullptr ;
for ( const ValueFlow : : Value & value : values ) {
if ( ! value . isIntValue ( ) )
continue ;
if ( result )
2020-02-01 08:28:18 +01:00
result = & std : : min ( value , * result , [ compare ] ( const ValueFlow : : Value & x , const ValueFlow : : Value & y ) {
2019-12-26 15:48:17 +01:00
return compare ( x . intvalue , y . intvalue ) ;
} ) ;
2019-12-26 15:47:53 +01:00
else
result = & value ;
}
return result ;
}
static const ValueFlow : : Value * proveLessThan ( const std : : list < ValueFlow : : Value > & values , MathLib : : bigint x )
{
const ValueFlow : : Value * result = nullptr ;
2019-12-26 15:48:17 +01:00
const ValueFlow : : Value * maxValue = getCompareIntValue ( values , std : : greater < MathLib : : bigint > { } ) ;
2019-12-26 15:47:53 +01:00
if ( maxValue & & maxValue - > isImpossible ( ) & & maxValue - > bound = = ValueFlow : : Value : : Bound : : Lower ) {
if ( maxValue - > intvalue < = x )
result = maxValue ;
}
return result ;
}
static const ValueFlow : : Value * proveGreaterThan ( const std : : list < ValueFlow : : Value > & values , MathLib : : bigint x )
{
const ValueFlow : : Value * result = nullptr ;
2019-12-26 15:48:17 +01:00
const ValueFlow : : Value * minValue = getCompareIntValue ( values , std : : less < MathLib : : bigint > { } ) ;
2019-12-26 15:47:53 +01:00
if ( minValue & & minValue - > isImpossible ( ) & & minValue - > bound = = ValueFlow : : Value : : Bound : : Upper ) {
if ( minValue - > intvalue > = x )
result = minValue ;
}
return result ;
}
2019-09-20 15:06:37 +02:00
static const ValueFlow : : Value * proveNotEqual ( const std : : list < ValueFlow : : Value > & values , MathLib : : bigint x )
{
const ValueFlow : : Value * result = nullptr ;
for ( const ValueFlow : : Value & value : values ) {
if ( value . valueType ! = ValueFlow : : Value : : INT )
continue ;
if ( result & & ! isInBounds ( value , result - > intvalue ) )
continue ;
if ( value . isImpossible ( ) ) {
if ( value . intvalue = = x )
return & value ;
if ( ! isInBounds ( value , x ) )
continue ;
result = & value ;
} else {
if ( value . intvalue = = x )
return nullptr ;
if ( ! isInBounds ( value , x ) )
continue ;
result = nullptr ;
}
}
return result ;
}
static void valueFlowInferCondition ( TokenList * tokenlist ,
const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > astParent ( ) )
continue ;
if ( tok - > hasKnownValue ( ) )
continue ;
2020-05-19 21:03:28 +02:00
if ( tok - > variable ( ) & & ( Token : : Match ( tok - > astParent ( ) , " ?|&&|!|%oror% " ) | |
Token : : Match ( tok - > astParent ( ) - > previous ( ) , " if|while ( " ) ) ) {
2019-09-20 15:06:37 +02:00
const ValueFlow : : Value * result = proveNotEqual ( tok - > values ( ) , 0 ) ;
if ( ! result )
continue ;
ValueFlow : : Value value = * result ;
value . intvalue = 1 ;
2019-12-26 15:47:53 +01:00
value . bound = ValueFlow : : Value : : Bound : : Point ;
2019-09-20 15:06:37 +02:00
value . setKnown ( ) ;
setTokenValue ( tok , value , settings ) ;
2020-05-19 21:03:28 +02:00
} else if ( tok - > isComparisonOp ( ) ) {
2019-09-20 15:06:37 +02:00
MathLib : : bigint val = 0 ;
const Token * varTok = nullptr ;
2020-05-23 07:15:13 +02:00
std : : string op = tok - > str ( ) ;
2019-09-20 15:06:37 +02:00
if ( tok - > astOperand1 ( ) - > hasKnownIntValue ( ) ) {
val = tok - > astOperand1 ( ) - > values ( ) . front ( ) . intvalue ;
varTok = tok - > astOperand2 ( ) ;
2020-05-23 07:15:13 +02:00
// Flip the operator
if ( op = = " > " )
op = " < " ;
else if ( op = = " < " )
op = " > " ;
else if ( op = = " >= " )
op = " <= " ;
else if ( op = = " <= " )
op = " >= " ;
2019-09-20 15:06:37 +02:00
} else if ( tok - > astOperand2 ( ) - > hasKnownIntValue ( ) ) {
val = tok - > astOperand2 ( ) - > values ( ) . front ( ) . intvalue ;
varTok = tok - > astOperand1 ( ) ;
}
if ( ! varTok )
continue ;
if ( varTok - > hasKnownIntValue ( ) )
continue ;
2019-12-26 15:47:53 +01:00
if ( varTok - > values ( ) . empty ( ) )
continue ;
const ValueFlow : : Value * result = nullptr ;
bool known = false ;
2020-05-23 07:15:13 +02:00
if ( op = = " == " | | op = = " != " ) {
2019-12-26 15:47:53 +01:00
result = proveNotEqual ( varTok - > values ( ) , val ) ;
2020-05-23 07:15:13 +02:00
known = op = = " != " ;
} else if ( op = = " < " | | op = = " >= " ) {
2019-12-26 15:47:53 +01:00
result = proveLessThan ( varTok - > values ( ) , val ) ;
2020-05-23 07:15:13 +02:00
known = op = = " < " ;
2019-12-26 15:47:53 +01:00
if ( ! result & & ! isSaturated ( val ) ) {
result = proveGreaterThan ( varTok - > values ( ) , val - 1 ) ;
2020-05-23 07:15:13 +02:00
known = op = = " >= " ;
2019-12-26 15:47:53 +01:00
}
2020-05-23 07:15:13 +02:00
} else if ( op = = " > " | | op = = " <= " ) {
2019-12-26 15:47:53 +01:00
result = proveGreaterThan ( varTok - > values ( ) , val ) ;
2020-05-23 07:15:13 +02:00
known = op = = " > " ;
2019-12-26 15:47:53 +01:00
if ( ! result & & ! isSaturated ( val ) ) {
result = proveLessThan ( varTok - > values ( ) , val + 1 ) ;
2020-05-23 07:15:13 +02:00
known = op = = " <= " ;
2019-12-26 15:47:53 +01:00
}
}
2019-09-20 15:06:37 +02:00
if ( ! result )
continue ;
ValueFlow : : Value value = * result ;
2019-12-26 15:47:53 +01:00
value . intvalue = known ;
value . bound = ValueFlow : : Value : : Bound : : Point ;
2019-09-20 15:06:37 +02:00
value . setKnown ( ) ;
setTokenValue ( tok , value , settings ) ;
}
}
}
2019-07-15 13:47:17 +02:00
static bool valueFlowForLoop1 ( const Token * tok , 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
2018-11-23 19:16:19 +01:00
bool reassign = false ;
visitAstNodes ( secondExpression ,
[ & ] ( const Token * t ) {
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.
2018-11-23 19:16:19 +01:00
reassign = true ;
return reassign ? ChildrenToVisit : : done : ChildrenToVisit : : op1_and_op2 ;
} ) ;
if ( reassign )
return false ;
2014-06-30 07:26:48 +02:00
}
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
2019-07-15 13:47:17 +02:00
int maxcount = 10000 ;
while ( result ! = 0 & & ! error & & - - maxcount > 0 ) {
2014-03-17 18:43:47 +01:00
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
}
2019-07-15 13:47:17 +02:00
static void valueFlowForLoopSimplify ( Token * const bodyStart , const nonneg 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
}
}
2019-07-15 13:47:17 +02:00
static void valueFlowForLoopSimplifyAfter ( Token * fortok , nonneg int varid , const MathLib : : bigint num , TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
2014-08-17 10:40:22 +02:00
{
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 ( ) )
2020-01-11 09:17:32 +01:00
endToken = var - > 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
2020-03-20 07:16:05 +01:00
Token * blockTok = fortok - > linkAt ( 1 ) - > linkAt ( 1 ) ;
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
2020-03-20 07:16:05 +01:00
if ( blockTok ! = endToken ) {
valueFlowForwardVariable (
blockTok - > next ( ) , endToken , var , varid , values , false , false , tokenlist , errorLogger , settings ) ;
}
2014-08-17 10:40:22 +02:00
}
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 ;
2019-07-15 13:47:17 +02:00
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 ) ) {
2020-02-19 07:55:04 +01:00
ProgramMemory : : Map : : const_iterator it ;
2016-11-14 06:42:52 +01:00
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
}
}
}
}
2020-03-01 16:46:20 +01:00
struct MultiValueFlowForwardAnalyzer : ValueFlowForwardAnalyzer {
std : : unordered_map < nonneg int , ValueFlow : : Value > values ;
std : : unordered_map < nonneg int , const Variable * > vars ;
MultiValueFlowForwardAnalyzer ( )
: ValueFlowForwardAnalyzer ( ) , values ( ) , vars ( )
{ }
MultiValueFlowForwardAnalyzer ( const std : : unordered_map < const Variable * , ValueFlow : : Value > & args , const TokenList * t )
2020-03-01 20:39:00 +01:00
: ValueFlowForwardAnalyzer ( t ) , values ( ) , vars ( ) {
for ( const auto & p : args ) {
2020-03-01 16:46:20 +01:00
values [ p . first - > declarationId ( ) ] = p . second ;
vars [ p . first - > declarationId ( ) ] = p . first ;
}
}
virtual const std : : unordered_map < nonneg int , const Variable * > & getVars ( ) const {
return vars ;
}
virtual const ValueFlow : : Value * getValue ( const Token * tok ) const OVERRIDE {
if ( tok - > varId ( ) = = 0 )
return nullptr ;
auto it = values . find ( tok - > varId ( ) ) ;
if ( it = = values . end ( ) )
return nullptr ;
return & it - > second ;
}
virtual ValueFlow : : Value * getValue ( const Token * tok ) OVERRIDE {
if ( tok - > varId ( ) = = 0 )
return nullptr ;
auto it = values . find ( tok - > varId ( ) ) ;
if ( it = = values . end ( ) )
return nullptr ;
return & it - > second ;
}
virtual void makeConditional ( ) OVERRIDE {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
p . second . conditional = true ;
}
}
virtual void addErrorPath ( const Token * tok , const std : : string & s ) OVERRIDE {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
p . second . errorPath . emplace_back ( tok , " Assuming condition is " + s ) ;
}
}
virtual bool isAlias ( const Token * tok ) const OVERRIDE {
2020-04-03 09:25:21 +02:00
std : : list < ValueFlow : : Value > vals ;
std : : transform ( values . begin ( ) , values . end ( ) , std : : back_inserter ( vals ) , SelectMapValues { } ) ;
2020-03-01 16:46:20 +01:00
for ( const auto & p : getVars ( ) ) {
nonneg int varid = p . first ;
const Variable * var = p . second ;
if ( tok - > varId ( ) = = varid )
return true ;
if ( isAliasOf ( var , tok , varid , vals ) )
return true ;
}
return false ;
}
virtual bool isGlobal ( ) const OVERRIDE {
return false ;
}
virtual bool lowerToPossible ( ) OVERRIDE {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
if ( p . second . isImpossible ( ) )
return false ;
p . second . changeKnownToPossible ( ) ;
}
return true ;
}
virtual bool lowerToInconclusive ( ) OVERRIDE {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
if ( p . second . isImpossible ( ) )
return false ;
p . second . setInconclusive ( ) ;
}
return true ;
}
virtual bool isConditional ( ) const OVERRIDE {
2020-03-01 20:39:00 +01:00
for ( auto & & p : values ) {
2020-03-01 16:46:20 +01:00
if ( p . second . conditional )
return true ;
if ( p . second . condition )
return ! p . second . isImpossible ( ) ;
}
return false ;
}
virtual bool updateScope ( const Token * endBlock , bool ) const OVERRIDE {
const Scope * scope = endBlock - > scope ( ) ;
if ( ! scope )
return false ;
if ( scope - > type = = Scope : : eLambda ) {
2020-03-01 20:39:00 +01:00
for ( const auto & p : values ) {
2020-03-01 16:46:20 +01:00
if ( ! p . second . isLifetimeValue ( ) )
return false ;
}
return true ;
} else if ( scope - > type = = Scope : : eIf | | scope - > type = = Scope : : eElse | | scope - > type = = Scope : : eWhile | |
scope - > type = = Scope : : eFor ) {
auto pred = [ ] ( const ValueFlow : : Value & value ) {
if ( value . isKnown ( ) )
return true ;
if ( value . isImpossible ( ) )
return true ;
if ( value . isLifetimeValue ( ) )
return true ;
return false ;
} ;
if ( std : : all_of ( values . begin ( ) , values . end ( ) , std : : bind ( pred , std : : bind ( SelectMapValues { } , std : : placeholders : : _1 ) ) ) )
return true ;
if ( isConditional ( ) )
return false ;
const Token * condTok = getCondTokFromEnd ( endBlock ) ;
std : : set < nonneg int > varids ;
std : : transform ( getVars ( ) . begin ( ) , getVars ( ) . end ( ) , std : : inserter ( varids , varids . begin ( ) ) , SelectMapKeys { } ) ;
return bifurcate ( condTok , varids , getSettings ( ) ) ;
}
return false ;
}
virtual bool match ( const Token * tok ) const OVERRIDE {
return values . count ( tok - > varId ( ) ) > 0 ;
}
virtual ProgramState getProgramState ( ) const OVERRIDE {
ProgramState ps ;
2020-03-01 20:39:00 +01:00
for ( const auto & p : values )
2020-03-01 16:46:20 +01:00
ps [ p . first ] = p . second ;
return ps ;
}
} ;
static void valueFlowInjectParameter ( TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings , const Scope * functionScope , const std : : unordered_map < const Variable * , std : : list < ValueFlow : : Value > > & vars )
{
using Args = std : : vector < std : : unordered_map < const Variable * , ValueFlow : : Value > > ;
Args args ( 1 ) ;
// Compute cartesian product of all arguments
2020-03-01 20:39:00 +01:00
for ( const auto & p : vars ) {
2020-03-01 16:46:20 +01:00
if ( p . second . empty ( ) )
continue ;
args . back ( ) [ p . first ] = p . second . front ( ) ;
}
2020-03-01 20:39:00 +01:00
for ( const auto & p : vars ) {
2020-04-01 22:33:09 +02:00
if ( args . size ( ) > 256 ) {
std : : string fname = " <unknown> " ;
Function * f = functionScope - > function ;
if ( f )
fname = f - > name ( ) ;
if ( settings - > debugwarnings )
bailout ( tokenlist , errorLogger , functionScope - > bodyStart , " Too many argument passed to " + fname ) ;
break ;
}
2020-03-01 16:46:20 +01:00
std : : for_each ( std : : next ( p . second . begin ( ) ) , p . second . end ( ) , [ & ] ( const ValueFlow : : Value & value ) {
Args new_args ;
2020-03-01 20:39:00 +01:00
for ( auto arg : args ) {
2020-04-01 22:33:09 +02:00
if ( value . path ! = 0 ) {
2020-04-04 10:31:38 +02:00
for ( const auto & q : arg ) {
2020-04-01 22:33:09 +02:00
if ( q . second . path = = 0 )
continue ;
if ( q . second . path ! = value . path )
return ;
}
}
2020-03-01 16:46:20 +01:00
arg [ p . first ] = value ;
new_args . push_back ( arg ) ;
}
std : : copy ( new_args . begin ( ) , new_args . end ( ) , std : : back_inserter ( args ) ) ;
} ) ;
}
2020-03-01 20:39:00 +01:00
for ( const auto & arg : args ) {
2020-03-01 16:46:20 +01:00
if ( arg . empty ( ) )
continue ;
bool skip = false ;
// Make sure all arguments are the same path
MathLib : : bigint path = arg . begin ( ) - > second . path ;
2020-03-01 20:39:00 +01:00
for ( const auto & p : arg ) {
2020-03-01 16:46:20 +01:00
if ( p . second . path ! = path ) {
skip = true ;
break ;
}
}
if ( skip )
continue ;
MultiValueFlowForwardAnalyzer a ( arg , tokenlist ) ;
valueFlowGenericForward ( const_cast < Token * > ( functionScope - > bodyStart ) , functionScope - > bodyEnd , a , settings ) ;
}
}
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..
2019-07-15 13:47:17 +02:00
const int varid2 = arg - > declarationId ( ) ;
2015-02-01 15:05:00 +01:00
if ( ! varid2 )
return ;
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( 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 ( ) ;
2019-10-18 16:16:56 +02:00
// FIXME We must check if there is a return. See #9276
/*
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( tok - > tokAt ( 3 ) ,
vartok - > variable ( ) - > scope ( ) - > bodyEnd ,
vartok - > variable ( ) ,
vartok - > varId ( ) ,
values ,
values . back ( ) . isKnown ( ) ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2019-10-18 16:16:56 +02:00
*/
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
{
2018-11-04 20:52:12 +01:00
for ( const ValueFlow : : Value & value : values ) {
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
}
}
2018-11-04 20:52:12 +01:00
static bool evaluate ( const Token * expr , const std : : vector < std : : list < ValueFlow : : Value > > & values , std : : list < ValueFlow : : Value > * result )
2016-10-17 13:05:19 +02:00
{
2018-11-04 20:52:12 +01:00
if ( ! expr )
return false ;
2018-11-05 16:52:12 +01:00
// strlen(arg)..
if ( expr - > str ( ) = = " ( " & & Token : : Match ( expr - > previous ( ) , " strlen ( %name% ) " ) ) {
const Token * arg = expr - > next ( ) ;
if ( arg - > str ( ) . compare ( 0 , 3 , " arg " ) ! = 0 | | arg - > str ( ) . size ( ) ! = 4 )
return false ;
const char n = arg - > str ( ) [ 3 ] ;
if ( n < ' 1 ' | | n - ' 1 ' > = values . size ( ) )
return false ;
for ( const ValueFlow : : Value & argvalue : values [ n - ' 1 ' ] ) {
if ( argvalue . isTokValue ( ) & & argvalue . tokvalue - > tokType ( ) = = Token : : eString ) {
ValueFlow : : Value res ( argvalue ) ; // copy all "inconclusive", "condition", etc attributes
// set return value..
res . valueType = ValueFlow : : Value : : INT ;
res . tokvalue = nullptr ;
res . intvalue = Token : : getStrLength ( argvalue . tokvalue ) ;
2019-09-28 20:22:46 +02:00
result - > emplace_back ( std : : move ( res ) ) ;
2018-11-05 16:52:12 +01:00
}
}
return ! result - > empty ( ) ;
}
2018-11-05 06:53:48 +01:00
// unary operands
if ( expr - > astOperand1 ( ) & & ! expr - > astOperand2 ( ) ) {
std : : list < ValueFlow : : Value > opvalues ;
if ( ! evaluate ( expr - > astOperand1 ( ) , values , & opvalues ) )
return false ;
if ( expr - > str ( ) = = " + " ) {
result - > swap ( opvalues ) ;
return true ;
}
if ( expr - > str ( ) = = " - " ) {
for ( ValueFlow : : Value v : opvalues ) {
if ( v . isIntValue ( ) ) {
v . intvalue = - v . intvalue ;
2019-09-28 20:22:46 +02:00
result - > emplace_back ( std : : move ( v ) ) ;
2018-11-05 06:53:48 +01:00
}
}
return true ;
}
return false ;
}
// binary/ternary operands
2018-11-04 20:52:12 +01:00
if ( expr - > astOperand1 ( ) & & expr - > astOperand2 ( ) ) {
std : : list < ValueFlow : : Value > lhsValues , rhsValues ;
if ( ! evaluate ( expr - > astOperand1 ( ) , values , & lhsValues ) )
return false ;
if ( expr - > str ( ) ! = " ? " & & ! evaluate ( expr - > astOperand2 ( ) , values , & rhsValues ) )
return false ;
2016-10-17 13:05:19 +02:00
2018-11-04 20:52:12 +01:00
for ( const ValueFlow : : Value & val1 : lhsValues ) {
if ( ! val1 . isIntValue ( ) )
continue ;
if ( expr - > str ( ) = = " ? " ) {
rhsValues . clear ( ) ;
const Token * expr2 = val1 . intvalue ? expr - > astOperand2 ( ) - > astOperand1 ( ) : expr - > astOperand2 ( ) - > astOperand2 ( ) ;
if ( ! evaluate ( expr2 , values , & rhsValues ) )
continue ;
result - > insert ( result - > end ( ) , rhsValues . begin ( ) , rhsValues . end ( ) ) ;
continue ;
}
2016-10-17 13:05:19 +02:00
2018-11-04 20:52:12 +01:00
for ( const ValueFlow : : Value & val2 : rhsValues ) {
if ( ! val2 . isIntValue ( ) )
continue ;
2018-11-05 18:07:35 +01:00
if ( val1 . varId ! = 0 & & val2 . varId ! = 0 ) {
if ( val1 . varId ! = val2 . varId | | val1 . varvalue ! = val2 . varvalue )
continue ;
}
2018-11-04 20:52:12 +01:00
if ( expr - > str ( ) = = " + " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue + val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " - " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue - val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " * " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue * val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " / " & & val2 . intvalue ! = 0 )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue / val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " % " & & val2 . intvalue ! = 0 )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue % val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " & " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue & val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " | " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue | val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " ^ " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue ^ val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " == " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue = = val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " != " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue ! = val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " < " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue < val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " > " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue > val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " >= " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue > = val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " <= " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue < = val2 . intvalue ) ) ;
2018-11-05 06:53:48 +01:00
else if ( expr - > str ( ) = = " && " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue & & val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " || " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue | | val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " << " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue < < val2 . intvalue ) ) ;
else if ( expr - > str ( ) = = " >> " )
result - > emplace_back ( ValueFlow : : Value ( val1 . intvalue > > val2 . intvalue ) ) ;
2018-11-04 20:52:12 +01:00
else
return false ;
combineValueProperties ( val1 , val2 , & result - > back ( ) ) ;
}
}
return ! result - > empty ( ) ;
}
if ( expr - > str ( ) . compare ( 0 , 3 , " arg " ) = = 0 ) {
* result = values [ expr - > str ( ) [ 3 ] - ' 1 ' ] ;
return true ;
}
if ( expr - > isNumber ( ) ) {
result - > emplace_back ( ValueFlow : : Value ( MathLib : : toLongNumber ( expr - > str ( ) ) ) ) ;
2018-11-05 06:53:48 +01:00
result - > back ( ) . setKnown ( ) ;
2018-11-04 20:52:12 +01:00
return true ;
2018-11-05 16:52:12 +01:00
} else if ( expr - > tokType ( ) = = Token : : eChar ) {
result - > emplace_back ( ValueFlow : : Value ( MathLib : : toLongNumber ( expr - > str ( ) ) ) ) ;
result - > back ( ) . setKnown ( ) ;
return true ;
2018-11-04 20:52:12 +01:00
}
return false ;
}
2018-11-06 06:38:26 +01:00
static std : : list < ValueFlow : : Value > getFunctionArgumentValues ( const Token * argtok )
{
std : : list < ValueFlow : : Value > argvalues ( argtok - > values ( ) ) ;
2019-09-21 08:19:54 +02:00
removeImpossible ( argvalues ) ;
2018-11-06 07:02:20 +01:00
if ( argvalues . empty ( ) & & Token : : Match ( argtok , " %comp%|%oror%|&&|! " ) ) {
argvalues . emplace_back ( 0 ) ;
argvalues . emplace_back ( 1 ) ;
2018-11-06 06:38:26 +01:00
}
return argvalues ;
}
2018-11-04 20:52:12 +01:00
static void valueFlowLibraryFunction ( Token * tok , const std : : string & returnValue , const Settings * settings )
{
std : : vector < std : : list < ValueFlow : : Value > > argValues ;
for ( const Token * argtok : getArguments ( tok - > previous ( ) ) ) {
2018-11-06 06:38:26 +01:00
argValues . emplace_back ( getFunctionArgumentValues ( argtok ) ) ;
2018-11-04 20:52:12 +01:00
if ( argValues . back ( ) . empty ( ) )
return ;
}
2018-11-11 16:41:01 +01:00
if ( returnValue . find ( " arg " ) ! = std : : string : : npos & & argValues . empty ( ) )
return ;
2018-11-04 20:52:12 +01:00
TokenList tokenList ( settings ) ;
{
const std : : string code = " return " + returnValue + " ; " ;
std : : istringstream istr ( code ) ;
if ( ! tokenList . createTokens ( istr ) )
return ;
2016-10-17 13:05:19 +02:00
}
// combine operators, set links, etc..
2018-11-04 20:52:12 +01:00
std : : stack < Token * > lpar ;
2016-10-17 13:05:19 +02:00
for ( Token * tok2 = tokenList . front ( ) ; tok2 ; tok2 = tok2 - > next ( ) ) {
if ( Token : : Match ( tok2 , " [!<>=] = " ) ) {
tok2 - > str ( tok2 - > str ( ) + " = " ) ;
tok2 - > deleteNext ( ) ;
2018-11-04 20:52:12 +01:00
} else if ( tok2 - > str ( ) = = " ( " )
lpar . push ( tok2 ) ;
else if ( tok2 - > str ( ) = = " ) " ) {
if ( lpar . empty ( ) )
return ;
Token : : createMutualLinks ( lpar . top ( ) , tok2 ) ;
lpar . pop ( ) ;
2016-10-17 13:05:19 +02:00
}
}
2018-11-04 20:52:12 +01:00
if ( ! lpar . empty ( ) )
return ;
2016-10-17 13:05:19 +02:00
// Evaluate expression
tokenList . createAst ( ) ;
2018-11-04 20:52:12 +01:00
std : : list < ValueFlow : : Value > results ;
if ( evaluate ( tokenList . front ( ) - > astOperand1 ( ) , argValues , & results ) )
setTokenValues ( tok , results , settings ) ;
2016-10-17 13:05:19 +02:00
}
2020-02-17 10:31:08 +01:00
static void valueFlowSubFunction ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
2014-01-06 16:37:52 +01:00
{
2020-02-17 10:31:08 +01:00
for ( const Scope * scope : symboldatabase - > functionScopes ) {
const Function * function = scope - > function ;
if ( ! function )
2014-01-08 16:17:47 +01:00
continue ;
2020-02-17 10:31:08 +01:00
int id = 0 ;
for ( const Token * tok = scope - > bodyStart ; tok ! = scope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( ! Token : : Match ( tok , " %name% ( " ) )
continue ;
2014-01-08 16:17:47 +01:00
2020-02-17 10:31:08 +01:00
const Function * const calledFunction = tok - > function ( ) ;
if ( ! calledFunction ) {
// library function?
const std : : string & returnValue ( settings - > library . returnValue ( tok ) ) ;
if ( ! returnValue . empty ( ) )
valueFlowLibraryFunction ( tok - > next ( ) , returnValue , settings ) ;
continue ;
}
2014-01-06 16:37:52 +01:00
2020-02-17 10:31:08 +01:00
const Scope * const calledFunctionScope = calledFunction - > functionScope ;
if ( ! calledFunctionScope )
continue ;
2014-01-06 16:37:52 +01:00
2020-02-17 10:31:08 +01:00
id + + ;
2020-03-01 16:46:20 +01:00
std : : unordered_map < const Variable * , std : : list < ValueFlow : : Value > > argvars ;
2020-02-17 10:31:08 +01:00
// TODO: Rewrite this. It does not work well to inject 1 argument at a time.
const std : : vector < const Token * > & callArguments = getArguments ( tok ) ;
for ( int argnr = 0U ; argnr < callArguments . size ( ) ; + + argnr ) {
const Token * argtok = callArguments [ argnr ] ;
// Get function argument
const Variable * const argvar = calledFunction - > getArgumentVar ( argnr ) ;
if ( ! argvar )
break ;
2014-08-06 06:33:06 +02:00
2020-02-17 10:31:08 +01:00
// passing value(s) to function
std : : list < ValueFlow : : Value > argvalues ( getFunctionArgumentValues ( argtok ) ) ;
2015-07-16 20:49:14 +02:00
2020-02-17 10:31:08 +01:00
// Don't forward lifetime values
argvalues . remove_if ( std : : mem_fn ( & ValueFlow : : Value : : isLifetimeValue ) ) ;
2020-03-01 16:46:20 +01:00
// Don't forward container sizes for now since programmemory can't evaluate conditions
argvalues . remove_if ( std : : mem_fn ( & ValueFlow : : Value : : isContainerSizeValue ) ) ;
2018-11-11 16:43:54 +01:00
2020-02-17 10:31:08 +01:00
if ( argvalues . empty ( ) )
continue ;
2017-05-19 14:34:59 +02:00
2020-02-17 10:31:08 +01:00
// Error path..
for ( ValueFlow : : Value & v : argvalues ) {
const std : : string nr = MathLib : : toString ( argnr + 1 ) + getOrdinalText ( argnr + 1 ) ;
v . errorPath . emplace_back ( argtok ,
" Calling function ' " +
calledFunction - > name ( ) +
" ', " +
nr +
" argument ' " +
argtok - > expressionString ( ) +
" ' value is " +
v . infoString ( ) ) ;
v . path = 256 * v . path + id ;
}
2017-05-15 20:05:11 +02:00
2020-02-17 10:31:08 +01:00
// passed values are not "known"..
lowerToPossible ( argvalues ) ;
2015-07-16 20:49:14 +02:00
2020-03-01 16:46:20 +01:00
argvars [ argvar ] = argvalues ;
2020-02-17 10:31:08 +01:00
}
2020-03-01 16:46:20 +01:00
valueFlowInjectParameter ( tokenlist , errorLogger , settings , calledFunctionScope , argvars ) ;
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 ;
2018-11-03 15:53:24 +01:00
if ( tok - > hasKnownValue ( ) )
continue ;
2014-06-29 18:04:38 +02:00
// 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 ) ;
2019-07-04 12:32:32 +02:00
if ( function - > hasVirtualSpecifier ( ) )
2016-12-11 10:42:39 +01:00
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 ( ) ;
}
2019-08-15 10:44:55 +02:00
// if (!stdtype && !pointer)
2019-08-15 10:46: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 ;
2019-08-02 21:14:29 +02:00
if ( ( ! var - > isPointer ( ) & & var - > type ( ) & & var - > type ( ) - > needInitialization ! = Type : : NeedInitialization : : 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 ;
2019-08-15 10:44:55 +02:00
if ( ! var - > type ( ) & & ! stdtype & & ! pointer )
continue ;
2017-04-23 18:05:14 +02:00
ValueFlow : : Value uninitValue ;
uninitValue . setKnown ( ) ;
uninitValue . valueType = ValueFlow : : Value : : UNINIT ;
2019-08-29 08:38:50 +02:00
uninitValue . tokvalue = vardecl ;
2017-04-23 18:05:14 +02:00
std : : list < ValueFlow : : Value > values ;
values . push_back ( uninitValue ) ;
const bool constValue = true ;
const bool subFunction = false ;
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( vardecl - > next ( ) ,
vardecl - > scope ( ) - > bodyEnd ,
var ,
vardecl - > varId ( ) ,
values ,
constValue ,
subFunction ,
tokenlist ,
errorLogger ,
settings ) ;
2017-04-23 18:05:14 +02:00
}
}
2019-07-15 13:47:17 +02:00
static bool hasContainerSizeGuard ( const Token * tok , nonneg int containerId )
2018-08-11 15:08:54 +02:00
{
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?
2018-11-23 19:16:19 +01:00
bool found = false ;
visitAstNodes ( parent - > astOperand1 ( ) ,
[ & ] ( const Token * t ) {
2018-08-11 15:08:54 +02:00
if ( t - > varId ( ) = = containerId )
2018-11-23 19:16:19 +01:00
found = true ;
return found ? ChildrenToVisit : : done : ChildrenToVisit : : op1_and_op2 ;
} ) ;
if ( found )
return true ;
2018-08-11 15:08:54 +02:00
}
return false ;
}
2018-11-24 10:07:12 +01:00
static bool isContainerSize ( const Token * tok )
{
if ( ! Token : : Match ( tok , " %var% . %name% ( " ) )
return false ;
if ( ! astIsContainer ( tok ) )
return false ;
if ( tok - > valueType ( ) - > container & & tok - > valueType ( ) - > container - > getYield ( tok - > strAt ( 2 ) ) = = Library : : Container : : Yield : : SIZE )
return true ;
if ( Token : : Match ( tok - > tokAt ( 2 ) , " size|length ( ) " ) )
return true ;
return false ;
}
static bool isContainerEmpty ( const Token * tok )
{
if ( ! Token : : Match ( tok , " %var% . %name% ( " ) )
return false ;
if ( ! astIsContainer ( tok ) )
return false ;
if ( tok - > valueType ( ) - > container & & tok - > valueType ( ) - > container - > getYield ( tok - > strAt ( 2 ) ) = = Library : : Container : : Yield : : EMPTY )
return true ;
if ( Token : : simpleMatch ( tok - > tokAt ( 2 ) , " empty ( ) " ) )
return true ;
return false ;
}
2019-09-06 21:18:45 +02:00
static bool isContainerSizeChanged ( nonneg int varId , const Token * start , const Token * end , int depth = 20 ) ;
2018-11-02 20:10:40 +01:00
2019-09-06 21:18:45 +02:00
static bool isContainerSizeChangedByFunction ( const Token * tok , int depth = 20 )
2018-08-13 06:54:18 +02:00
{
2019-09-06 21:18:45 +02:00
if ( ! tok - > valueType ( ) | | ! tok - > valueType ( ) - > container )
2018-09-02 14:08:34 +02:00
return false ;
2019-09-06 21:18:45 +02:00
// If we are accessing an element then we are not changing the container size
if ( Token : : Match ( tok , " %name% . %name% ( " ) ) {
Library : : Container : : Yield yield = tok - > valueType ( ) - > container - > getYield ( tok - > strAt ( 2 ) ) ;
if ( yield ! = Library : : Container : : Yield : : NO_YIELD )
return false ;
}
if ( Token : : simpleMatch ( tok - > astParent ( ) , " [ " ) )
return false ;
// address of variable
2019-10-18 08:21:07 +02:00
const bool addressOf = tok - > valueType ( ) - > pointer | | ( tok - > astParent ( ) & & tok - > astParent ( ) - > isUnaryOp ( " & " ) ) ;
2019-09-06 21:18:45 +02:00
int narg ;
const Token * ftok = getTokenArgumentFunction ( tok , narg ) ;
if ( ! ftok )
return false ; // not a function => variable not changed
const Function * fun = ftok - > function ( ) ;
if ( fun ) {
const Variable * arg = fun - > getArgumentVar ( narg ) ;
2019-09-10 19:39:44 +02:00
if ( arg ) {
if ( ! arg - > isReference ( ) & & ! addressOf )
return false ;
2019-12-15 20:10:12 +01:00
if ( ! addressOf & & arg - > isConst ( ) )
return false ;
if ( arg - > valueType ( ) & & arg - > valueType ( ) - > constness = = 1 )
2019-09-06 21:18:45 +02:00
return false ;
2019-09-10 19:39:44 +02:00
const Scope * scope = fun - > functionScope ;
if ( scope ) {
// Argument not used
if ( ! arg - > nameToken ( ) )
return false ;
if ( depth > 0 )
return isContainerSizeChanged ( arg - > declarationId ( ) , scope - > bodyStart , scope - > bodyEnd , depth - 1 ) ;
}
2019-10-18 08:21:07 +02:00
// Don't know => Safe guess
return true ;
2019-09-06 21:18:45 +02:00
}
}
bool inconclusive = false ;
const bool isChanged = isVariableChangedByFunctionCall ( tok , 0 , nullptr , & inconclusive ) ;
return ( isChanged | | inconclusive ) ;
2018-08-13 06:54:18 +02:00
}
2019-07-15 13:47:17 +02:00
static void valueFlowContainerReverse ( Token * tok , nonneg int containerId , const ValueFlow : : Value & value , const Settings * settings )
2018-08-10 11:29:16 +02:00
{
while ( nullptr ! = ( tok = tok - > previous ( ) ) ) {
if ( Token : : Match ( tok , " [{}] " ) )
break ;
2018-09-04 20:28:48 +02:00
if ( Token : : Match ( tok , " return|break|continue " ) )
break ;
2018-08-10 11:29:16 +02:00
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 ) )
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
2018-08-10 22:06:23 +02:00
}
}
2019-07-15 13:47:17 +02:00
static void valueFlowContainerForward ( Token * tok , nonneg int containerId , ValueFlow : : Value value , const Settings * settings , bool cpp )
2018-08-10 22:06:23 +02:00
{
while ( nullptr ! = ( tok = tok - > next ( ) ) ) {
if ( Token : : Match ( tok , " [{}] " ) )
break ;
2018-11-02 20:10:40 +01:00
if ( Token : : Match ( tok , " while|for ( " ) ) {
const Token * start = tok - > linkAt ( 1 ) - > next ( ) ;
if ( ! Token : : simpleMatch ( start - > link ( ) , " { " ) )
break ;
if ( isContainerSizeChanged ( containerId , start , start - > link ( ) ) )
break ;
}
2019-09-03 06:43:54 +02:00
if ( Token : : simpleMatch ( tok , " ) { " ) & & Token : : Match ( tok - > link ( ) - > previous ( ) , " while|for|if ( " ) ) {
const Token * start = tok - > next ( ) ;
2019-12-14 19:04:19 +01:00
if ( isContainerSizeChanged ( containerId , start , start - > link ( ) ) | | isEscapeScope ( start , nullptr ) )
2019-09-03 06:43:54 +02:00
break ;
tok = start - > link ( ) ;
if ( Token : : simpleMatch ( tok , " } else { " ) ) {
start = tok - > tokAt ( 2 ) ;
if ( isContainerSizeChanged ( containerId , start , start - > link ( ) ) )
break ;
tok = start - > link ( ) ;
}
}
2018-08-10 22:06:23 +02:00
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-10-27 18:38:04 +02:00
if ( Token : : Match ( tok , " %name% += " ) ) {
if ( ! tok - > valueType ( ) | | ! tok - > valueType ( ) - > container | | ! tok - > valueType ( ) - > container - > stdStringLike )
break ;
const Token * rhs = tok - > next ( ) - > astOperand2 ( ) ;
if ( rhs - > tokType ( ) = = Token : : eString )
value . intvalue + = Token : : getStrLength ( rhs ) ;
else if ( rhs - > valueType ( ) & & rhs - > valueType ( ) - > container & & rhs - > valueType ( ) - > container - > stdStringLike ) {
bool found = false ;
for ( const ValueFlow : : Value & rhsval : rhs - > values ( ) ) {
if ( rhsval . isKnown ( ) & & rhsval . isContainerSizeValue ( ) ) {
value . intvalue + = rhsval . intvalue ;
found = true ;
}
}
if ( ! found )
break ;
} else
break ;
}
2018-09-08 10:43:08 +02:00
if ( isLikelyStreamRead ( cpp , tok - > astParent ( ) ) )
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 ) )
2019-06-16 10:09:38 +02:00
setTokenValue ( tok , value , settings ) ;
2018-08-10 11:29:16 +02:00
}
}
2019-09-06 21:18:45 +02:00
static bool isContainerSizeChanged ( nonneg int varId , const Token * start , const Token * end , int depth )
2018-08-12 22:23:19 +02:00
{
for ( const Token * tok = start ; tok ! = end ; tok = tok - > next ( ) ) {
if ( tok - > varId ( ) ! = varId )
continue ;
if ( ! tok - > valueType ( ) | | ! tok - > valueType ( ) - > container )
return true ;
2019-09-03 06:43:54 +02:00
if ( Token : : Match ( tok , " %name% %assign%|<< " ) )
2018-08-12 22:23:19 +02:00
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 ;
2020-04-21 17:27:51 +02:00
}
2018-08-12 22:23:19 +02:00
}
2019-09-06 21:18:45 +02:00
if ( isContainerSizeChangedByFunction ( tok , depth ) )
2019-09-05 16:42:26 +02:00
return true ;
2018-08-12 22:23:19 +02:00
}
return false ;
}
2019-04-26 11:30:09 +02:00
static void valueFlowSmartPointer ( TokenList * tokenlist , ErrorLogger * errorLogger , const Settings * settings )
{
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
if ( ! tok - > scope ( ) )
continue ;
if ( ! tok - > scope ( ) - > isExecutable ( ) )
continue ;
if ( ! tok - > variable ( ) )
continue ;
const Variable * var = tok - > variable ( ) ;
if ( ! var - > isSmartPointer ( ) )
continue ;
if ( var - > nameToken ( ) = = tok ) {
if ( Token : : Match ( tok , " %var% (|{ " ) & & tok - > next ( ) - > astOperand2 ( ) & & tok - > next ( ) - > astOperand2 ( ) - > str ( ) ! = " , " ) {
2019-06-16 10:09:38 +02:00
Token * inTok = tok - > next ( ) - > astOperand2 ( ) ;
2019-04-26 11:30:09 +02:00
std : : list < ValueFlow : : Value > values = inTok - > values ( ) ;
const bool constValue = inTok - > isNumber ( ) ;
2019-06-16 10:09:38 +02:00
valueFlowForwardAssign ( inTok , var , values , constValue , true , tokenlist , errorLogger , settings ) ;
2019-04-26 11:30:09 +02:00
} else if ( Token : : Match ( tok , " %var% ; " ) ) {
std : : list < ValueFlow : : Value > values ;
ValueFlow : : Value v ( 0 ) ;
v . setKnown ( ) ;
values . push_back ( v ) ;
valueFlowForwardAssign ( tok , var , values , false , true , tokenlist , errorLogger , settings ) ;
}
2019-07-23 22:45:58 +02:00
} else if ( Token : : Match ( tok , " %var% . reset ( " ) & & tok - > next ( ) - > originalName ( ) ! = " -> " ) {
2019-04-26 11:30:09 +02:00
if ( Token : : simpleMatch ( tok - > tokAt ( 3 ) , " ( ) " ) ) {
std : : list < ValueFlow : : Value > values ;
ValueFlow : : Value v ( 0 ) ;
v . setKnown ( ) ;
values . push_back ( v ) ;
valueFlowForwardAssign ( tok - > tokAt ( 4 ) , var , values , false , false , tokenlist , errorLogger , settings ) ;
} else {
2019-05-05 09:51:36 +02:00
tok - > removeValues ( std : : mem_fn ( & ValueFlow : : Value : : isIntValue ) ) ;
2019-06-16 10:09:38 +02:00
Token * inTok = tok - > tokAt ( 3 ) - > astOperand2 ( ) ;
2019-05-05 09:51:36 +02:00
if ( ! inTok )
continue ;
2019-04-26 11:30:09 +02:00
std : : list < ValueFlow : : Value > values = inTok - > values ( ) ;
const bool constValue = inTok - > isNumber ( ) ;
2019-06-16 10:09:38 +02:00
valueFlowForwardAssign ( inTok , var , values , constValue , false , tokenlist , errorLogger , settings ) ;
2019-04-26 11:30:09 +02:00
}
2019-07-23 22:45:58 +02:00
} else if ( Token : : Match ( tok , " %var% . release ( ) " ) && tok->next()->originalName() != " - > " ) {
2019-04-26 11:30:09 +02:00
std : : list < ValueFlow : : Value > values ;
ValueFlow : : Value v ( 0 ) ;
v . setKnown ( ) ;
values . push_back ( v ) ;
valueFlowForwardAssign ( tok - > tokAt ( 4 ) , var , values , false , false , tokenlist , errorLogger , settings ) ;
}
2019-04-26 11:30:35 +02:00
}
2019-04-26 11:30:09 +02:00
}
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 ( ) ) {
2020-02-12 18:53:36 +01:00
if ( ! var | | ! var - > isLocal ( ) | | var - > isPointer ( ) | | var - > isReference ( ) | | var - > isStatic ( ) )
2018-08-12 14:39:29 +02:00
continue ;
if ( ! var - > valueType ( ) | | ! var - > valueType ( ) - > container )
continue ;
if ( ! Token : : Match ( var - > nameToken ( ) , " %name% ; " ) )
continue ;
2018-11-03 15:53:24 +01:00
if ( var - > nameToken ( ) - > hasKnownValue ( ) )
continue ;
2018-08-12 14:39:29 +02:00
ValueFlow : : Value value ( 0 ) ;
2018-08-27 13:49:47 +02:00
if ( var - > valueType ( ) - > container - > size_templateArgNo > = 0 ) {
2020-02-19 07:36:02 +01:00
if ( var - > dimensions ( ) . size ( ) = = 1 & & var - > dimensions ( ) . front ( ) . known )
value . intvalue = var - > dimensions ( ) . front ( ) . num ;
2018-08-27 13:49:47 +02:00
else
continue ;
}
2018-08-12 14:39:29 +02:00
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
value . setKnown ( ) ;
2018-09-08 10:43:08 +02:00
valueFlowContainerForward ( var - > nameToken ( ) - > next ( ) , var - > declarationId ( ) , value , settings , tokenlist - > isCPP ( ) ) ;
2018-08-12 14:39:29 +02:00
}
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 ( ) ) {
2019-08-12 20:24:16 +02:00
if ( Token : : Match ( tok , " %name%|;|{|} %var% = %str% ; " ) ) {
2018-08-12 22:01:58 +02:00
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 ( ) ;
2018-09-08 10:43:08 +02:00
valueFlowContainerForward ( containerTok - > next ( ) , containerTok - > varId ( ) , value , settings , tokenlist - > isCPP ( ) ) ;
2018-08-12 22:01:58 +02:00
}
}
}
}
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 ;
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
2018-10-28 19:14:00 +01:00
if ( Token : : Match ( tok , " %name% . %name% ( " ) ) {
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 ;
if ( parent - > astOperand1 ( ) - > hasKnownIntValue ( ) )
intval = parent - > astOperand1 ( ) - > values ( ) . front ( ) . intvalue ;
else if ( parent - > astOperand2 ( ) - > hasKnownIntValue ( ) )
intval = parent - > astOperand2 ( ) - > values ( ) . front ( ) . intvalue ;
else
continue ;
conditionToken = parent ;
} else if ( tok - > valueType ( ) - > container - > getYield ( tok - > strAt ( 2 ) ) = = Library : : Container : : Yield : : EMPTY ) {
conditionToken = tok - > tokAt ( 3 ) ;
intval = 0 ;
} else {
2018-08-10 11:53:56 +02:00
continue ;
2018-10-28 19:14:00 +01:00
}
} else if ( tok - > valueType ( ) - > container - > stdStringLike & & Token : : Match ( tok , " %name% ==|!= %str% " ) & & tok - > next ( ) - > astOperand2 ( ) = = tok - > tokAt ( 2 ) ) {
intval = Token : : getStrLength ( tok - > tokAt ( 2 ) ) ;
conditionToken = tok - > next ( ) ;
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
2019-06-16 10:09:38 +02:00
valueFlowContainerReverse ( const_cast < Token * > ( scope . classDef ) , tok - > varId ( ) , value , settings ) ;
2018-11-24 10:07:12 +01:00
}
}
}
2018-08-10 22:18:38 +02:00
2018-11-24 10:07:12 +01:00
static void valueFlowContainerAfterCondition ( TokenList * tokenlist ,
SymbolDatabase * symboldatabase ,
ErrorLogger * errorLogger ,
const Settings * settings )
{
ValueFlowConditionHandler handler ;
handler . forward =
2019-09-26 10:32:49 +02:00
[ & ] ( Token * start , const Token * stop , const Token * vartok , const std : : list < ValueFlow : : Value > & values , bool ) {
// TODO: Forward multiple values
if ( values . empty ( ) )
return false ;
const Variable * var = vartok - > variable ( ) ;
if ( ! var )
return false ;
valueFlowContainerForward ( start , var - > declarationId ( ) , values . front ( ) , settings , tokenlist - > isCPP ( ) ) ;
return isContainerSizeChanged ( var - > declarationId ( ) , start , stop ) ;
} ;
2018-11-24 10:07:12 +01:00
handler . parse = [ & ] ( const Token * tok ) {
ValueFlowConditionHandler : : Condition cond ;
ValueFlow : : Value true_value ;
ValueFlow : : Value false_value ;
const Token * vartok = parseCompareInt ( tok , true_value , false_value ) ;
if ( vartok ) {
vartok = vartok - > tokAt ( - 3 ) ;
if ( ! isContainerSize ( vartok ) )
return cond ;
true_value . valueType = ValueFlow : : Value : : CONTAINER_SIZE ;
false_value . valueType = ValueFlow : : Value : : CONTAINER_SIZE ;
cond . true_values . push_back ( true_value ) ;
cond . false_values . push_back ( false_value ) ;
cond . vartok = vartok ;
return cond ;
}
2018-08-10 22:18:38 +02:00
2018-11-24 10:07:12 +01:00
// Empty check
if ( tok - > str ( ) = = " ( " ) {
vartok = tok - > tokAt ( - 3 ) ;
// TODO: Handle .size()
if ( ! isContainerEmpty ( vartok ) )
return cond ;
2019-01-06 12:21:47 +01:00
const Token * parent = tok - > astParent ( ) ;
while ( parent ) {
if ( Token : : Match ( parent , " %comp%|! " ) )
return cond ;
parent = parent - > astParent ( ) ;
}
2018-11-24 10:07:12 +01:00
ValueFlow : : Value value ( tok , 0LL ) ;
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
cond . true_values . emplace_back ( value ) ;
2019-09-28 20:22:46 +02:00
cond . false_values . emplace_back ( std : : move ( value ) ) ;
2018-11-24 10:07:12 +01:00
cond . vartok = vartok ;
return cond ;
}
// String compare
if ( Token : : Match ( tok , " ==|!= " ) ) {
const Token * strtok = nullptr ;
if ( Token : : Match ( tok - > astOperand1 ( ) , " %str% " ) ) {
strtok = tok - > astOperand1 ( ) ;
vartok = tok - > astOperand2 ( ) ;
} else if ( Token : : Match ( tok - > astOperand2 ( ) , " %str% " ) ) {
strtok = tok - > astOperand2 ( ) ;
vartok = tok - > astOperand1 ( ) ;
2018-08-10 22:18:38 +02:00
}
2018-11-24 10:07:12 +01:00
if ( ! strtok )
return cond ;
if ( ! astIsContainer ( vartok ) )
return cond ;
ValueFlow : : Value value ( tok , Token : : getStrLength ( strtok ) ) ;
value . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
cond . false_values . emplace_back ( value ) ;
2019-09-28 20:22:46 +02:00
cond . true_values . emplace_back ( std : : move ( value ) ) ;
2018-11-24 10:07:12 +01:00
cond . vartok = vartok ;
return cond ;
2018-08-10 11:29:16 +02:00
}
2018-11-24 10:07:12 +01:00
return cond ;
} ;
handler . afterCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
2018-08-10 11:29:16 +02:00
}
2018-12-31 17:05:46 +01:00
static void valueFlowFwdAnalysis ( const TokenList * tokenlist , const Settings * settings )
{
for ( const Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2019-12-20 19:06:35 +01:00
if ( Token : : simpleMatch ( tok , " for ( " ) )
tok = tok - > linkAt ( 1 ) ;
2019-01-01 17:23:46 +01:00
if ( tok - > str ( ) ! = " = " | | ! tok - > astOperand1 ( ) | | ! tok - > astOperand2 ( ) )
2018-12-31 17:05:46 +01:00
continue ;
2020-02-16 16:02:22 +01:00
// Skip variables
if ( tok - > astOperand1 ( ) - > variable ( ) )
continue ;
2018-12-31 17:05:46 +01:00
if ( ! tok - > scope ( ) - > isExecutable ( ) )
continue ;
if ( ! tok - > astOperand2 ( ) - > hasKnownIntValue ( ) )
continue ;
ValueFlow : : Value v ( tok - > astOperand2 ( ) - > values ( ) . front ( ) ) ;
2019-01-01 17:23:46 +01:00
v . errorPath . emplace_back ( tok , tok - > astOperand1 ( ) - > expressionString ( ) + " is assigned value " + MathLib : : toString ( v . intvalue ) ) ;
2018-12-31 17:05:46 +01:00
const Token * startToken = tok - > findExpressionStartEndTokens ( ) . second - > next ( ) ;
const Scope * functionScope = tok - > scope ( ) ;
while ( functionScope - > nestedIn & & functionScope - > nestedIn - > isExecutable ( ) )
functionScope = functionScope - > nestedIn ;
const Token * endToken = functionScope - > bodyEnd ;
2019-09-26 10:32:25 +02:00
valueFlowForwardExpression ( const_cast < Token * > ( startToken ) , endToken , tok - > astOperand1 ( ) , { v } , tokenlist , settings ) ;
2018-12-31 17:05:46 +01:00
}
}
2019-03-17 13:09:15 +01:00
static void valueFlowDynamicBufferSize ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
{
for ( const Scope * functionScope : symboldatabase - > functionScopes ) {
for ( const Token * tok = functionScope - > bodyStart ; tok ! = functionScope - > bodyEnd ; tok = tok - > next ( ) ) {
if ( ! Token : : Match ( tok , " [;{}] %var% = " ) )
continue ;
if ( ! tok - > next ( ) - > variable ( ) )
continue ;
const Token * rhs = tok - > tokAt ( 2 ) - > astOperand2 ( ) ;
while ( rhs & & rhs - > isCast ( ) )
rhs = rhs - > astOperand2 ( ) ? rhs - > astOperand2 ( ) : rhs - > astOperand1 ( ) ;
if ( ! rhs )
continue ;
if ( ! Token : : Match ( rhs - > previous ( ) , " %name% ( " ) )
continue ;
2019-07-05 12:44:52 +02:00
const Library : : AllocFunc * allocFunc = settings - > library . getAllocFuncInfo ( rhs - > previous ( ) ) ;
if ( ! allocFunc )
allocFunc = settings - > library . getReallocFuncInfo ( rhs - > previous ( ) ) ;
2019-03-20 06:46:55 +01:00
if ( ! allocFunc | | allocFunc - > bufferSize = = Library : : AllocFunc : : BufferSize : : none )
2019-03-17 13:09:15 +01:00
continue ;
const std : : vector < const Token * > args = getArguments ( rhs - > previous ( ) ) ;
2019-03-20 19:26:57 +01:00
const Token * const arg1 = ( args . size ( ) > = allocFunc - > bufferSizeArg1 ) ? args [ allocFunc - > bufferSizeArg1 - 1 ] : nullptr ;
const Token * const arg2 = ( args . size ( ) > = allocFunc - > bufferSizeArg2 ) ? args [ allocFunc - > bufferSizeArg2 - 1 ] : nullptr ;
2019-03-20 06:46:55 +01:00
MathLib : : bigint sizeValue = - 1 ;
switch ( allocFunc - > bufferSize ) {
case Library : : AllocFunc : : BufferSize : : none :
break ;
case Library : : AllocFunc : : BufferSize : : malloc :
2019-03-20 19:26:57 +01:00
if ( arg1 & & arg1 - > hasKnownIntValue ( ) )
sizeValue = arg1 - > getKnownIntValue ( ) ;
2019-03-20 06:46:55 +01:00
break ;
case Library : : AllocFunc : : BufferSize : : calloc :
2019-03-20 19:26:57 +01:00
if ( arg1 & & arg2 & & arg1 - > hasKnownIntValue ( ) & & arg2 - > hasKnownIntValue ( ) )
sizeValue = arg1 - > getKnownIntValue ( ) * arg2 - > getKnownIntValue ( ) ;
2019-03-20 06:46:55 +01:00
break ;
case Library : : AllocFunc : : BufferSize : : strdup :
2019-03-20 19:26:57 +01:00
if ( arg1 & & arg1 - > hasKnownValue ( ) ) {
const ValueFlow : : Value & value = arg1 - > values ( ) . back ( ) ;
2019-03-20 06:46:55 +01:00
if ( value . isTokValue ( ) & & value . tokvalue - > tokType ( ) = = Token : : eString )
2019-06-16 16:02:27 +02:00
sizeValue = Token : : getStrLength ( value . tokvalue ) + 1 ; // Add one for the null terminator
2019-03-20 06:46:55 +01:00
}
break ;
2020-04-21 17:27:51 +02:00
}
2019-03-20 06:46:55 +01:00
if ( sizeValue < 0 )
2019-03-17 13:09:15 +01:00
continue ;
2019-03-20 06:46:55 +01:00
ValueFlow : : Value value ( sizeValue ) ;
value . errorPath . emplace_back ( tok - > tokAt ( 2 ) , " Assign " + tok - > strAt ( 1 ) + " , buffer with size " + MathLib : : toString ( sizeValue ) ) ;
2019-03-17 13:09:15 +01:00
value . valueType = ValueFlow : : Value : : ValueType : : BUFFER_SIZE ;
value . setKnown ( ) ;
const std : : list < ValueFlow : : Value > values { value } ;
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( const_cast < Token * > ( rhs ) ,
functionScope - > bodyEnd ,
tok - > next ( ) - > variable ( ) ,
tok - > next ( ) - > varId ( ) ,
values ,
true ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2019-03-17 13:09:15 +01:00
}
}
}
2019-07-10 21:26:20 +02:00
static bool getMinMaxValues ( const ValueType * vt , const cppcheck : : Platform & platform , MathLib : : bigint * minValue , MathLib : : bigint * maxValue )
2019-07-10 16:59:05 +02:00
{
if ( ! vt | | ! vt - > isIntegral ( ) | | vt - > pointer )
return false ;
int bits ;
switch ( vt - > type ) {
case ValueType : : Type : : BOOL :
bits = 1 ;
break ;
case ValueType : : Type : : CHAR :
bits = platform . char_bit ;
break ;
case ValueType : : Type : : SHORT :
bits = platform . short_bit ;
break ;
case ValueType : : Type : : INT :
bits = platform . int_bit ;
break ;
case ValueType : : Type : : LONG :
bits = platform . long_bit ;
break ;
case ValueType : : Type : : LONGLONG :
bits = platform . long_long_bit ;
break ;
default :
return false ;
2020-04-21 17:27:51 +02:00
}
2019-07-10 16:59:05 +02:00
if ( bits = = 1 ) {
* minValue = 0 ;
* maxValue = 1 ;
} else if ( bits < 62 ) {
if ( vt - > sign = = ValueType : : Sign : : UNSIGNED ) {
* minValue = 0 ;
* maxValue = ( 1LL < < bits ) - 1 ;
} else {
* minValue = - ( 1LL < < ( bits - 1 ) ) ;
* maxValue = ( 1LL < < ( bits - 1 ) ) - 1 ;
}
} else if ( bits = = 64 ) {
if ( vt - > sign = = ValueType : : Sign : : UNSIGNED ) {
* minValue = 0 ;
* maxValue = LLONG_MAX ; // todo max unsigned value
} else {
* minValue = LLONG_MIN ;
* maxValue = LLONG_MAX ;
}
} else {
return false ;
}
return true ;
}
2019-07-10 21:26:20 +02:00
static bool getMinMaxValues ( const std : : string & typestr , const Settings * settings , MathLib : : bigint * minvalue , MathLib : : bigint * maxvalue )
2019-07-10 20:00:21 +02:00
{
TokenList typeTokens ( settings ) ;
std : : istringstream istr ( typestr + " ; " ) ;
if ( ! typeTokens . createTokens ( istr ) )
return false ;
typeTokens . simplifyPlatformTypes ( ) ;
typeTokens . simplifyStdType ( ) ;
const ValueType & vt = ValueType : : parseDecl ( typeTokens . front ( ) , settings ) ;
return getMinMaxValues ( & vt , * settings , minvalue , maxvalue ) ;
}
2019-07-10 16:59:05 +02:00
static void valueFlowSafeFunctions ( TokenList * tokenlist , SymbolDatabase * symboldatabase , ErrorLogger * errorLogger , const Settings * settings )
{
for ( const Scope * functionScope : symboldatabase - > functionScopes ) {
if ( ! functionScope - > bodyStart )
continue ;
const Function * function = functionScope - > function ;
if ( ! function )
continue ;
2019-07-24 11:39:18 +02:00
const bool safe = function - > isSafe ( settings ) ;
const bool all = safe & & settings - > platformType ! = cppcheck : : Platform : : PlatformType : : Unspecified ;
2019-07-10 16:59:05 +02:00
for ( const Variable & arg : function - > argumentList ) {
2019-07-24 12:08:56 +02:00
if ( ! arg . nameToken ( ) | | ! arg . valueType ( ) )
2019-07-10 16:59:05 +02:00
continue ;
2019-07-24 12:08:56 +02:00
if ( arg . valueType ( ) - > type = = ValueType : : Type : : CONTAINER ) {
2019-07-24 11:39:18 +02:00
if ( ! safe )
continue ;
std : : list < ValueFlow : : Value > argValues ;
argValues . emplace_back ( 0 ) ;
argValues . back ( ) . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , " Assuming " + arg . name ( ) + " is empty " ) ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . safe = true ;
2019-07-24 11:39:18 +02:00
argValues . emplace_back ( 1000000 ) ;
argValues . back ( ) . valueType = ValueFlow : : Value : : ValueType : : CONTAINER_SIZE ;
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , " Assuming " + arg . name ( ) + " size is 1000000 " ) ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . safe = true ;
2019-07-24 11:39:18 +02:00
for ( const ValueFlow : : Value & value : argValues )
valueFlowContainerForward ( const_cast < Token * > ( functionScope - > bodyStart ) , arg . declarationId ( ) , value , settings , tokenlist - > isCPP ( ) ) ;
continue ;
}
2019-07-12 11:09:24 +02:00
MathLib : : bigint low , high ;
bool isLow = arg . nameToken ( ) - > getCppcheckAttribute ( TokenImpl : : CppcheckAttributes : : Type : : LOW , & low ) ;
bool isHigh = arg . nameToken ( ) - > getCppcheckAttribute ( TokenImpl : : CppcheckAttributes : : Type : : HIGH , & high ) ;
2019-07-10 16:59:05 +02:00
2019-07-12 11:09:24 +02:00
if ( ! isLow & & ! isHigh & & ! all )
continue ;
2019-07-25 17:19:51 +02:00
const bool safeLow = ! isLow ;
const bool safeHigh = ! isHigh ;
2019-07-12 11:09:24 +02:00
if ( ( ! isLow | | ! isHigh ) & & all ) {
MathLib : : bigint minValue , maxValue ;
if ( getMinMaxValues ( arg . valueType ( ) , * settings , & minValue , & maxValue ) ) {
if ( ! isLow )
low = minValue ;
if ( ! isHigh )
high = maxValue ;
isLow = isHigh = true ;
2019-07-24 12:08:56 +02:00
} else if ( arg . valueType ( ) - > type = = ValueType : : Type : : FLOAT | | arg . valueType ( ) - > type = = ValueType : : Type : : DOUBLE | | arg . valueType ( ) - > type = = ValueType : : Type : : LONGDOUBLE ) {
std : : list < ValueFlow : : Value > argValues ;
argValues . emplace_back ( 0 ) ;
argValues . back ( ) . valueType = ValueFlow : : Value : : ValueType : : FLOAT ;
argValues . back ( ) . floatValue = isLow ? low : - 1E25 f ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , " Safe checks: Assuming argument has value " + MathLib : : toString ( argValues . back ( ) . floatValue ) ) ;
argValues . back ( ) . safe = true ;
2019-07-24 12:30:33 +02:00
argValues . emplace_back ( 0 ) ;
2019-07-24 12:08:56 +02:00
argValues . back ( ) . valueType = ValueFlow : : Value : : ValueType : : FLOAT ;
argValues . back ( ) . floatValue = isHigh ? high : 1E25 f ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , " Safe checks: Assuming argument has value " + MathLib : : toString ( argValues . back ( ) . floatValue ) ) ;
argValues . back ( ) . safe = true ;
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( const_cast < Token * > ( functionScope - > bodyStart - > next ( ) ) ,
functionScope - > bodyEnd ,
& arg ,
arg . declarationId ( ) ,
argValues ,
false ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2019-07-24 12:08:56 +02:00
continue ;
2019-07-12 11:09:24 +02:00
}
}
std : : list < ValueFlow : : Value > argValues ;
2019-07-17 22:17:34 +02:00
if ( isLow ) {
2019-07-12 11:09:24 +02:00
argValues . emplace_back ( low ) ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , std : : string ( safeLow ? " Safe checks: " : " " ) + " Assuming argument has value " + MathLib : : toString ( low ) ) ;
argValues . back ( ) . safe = safeLow ;
2019-07-17 22:17:34 +02:00
}
if ( isHigh ) {
2019-07-12 11:09:24 +02:00
argValues . emplace_back ( high ) ;
2019-07-25 17:19:51 +02:00
argValues . back ( ) . errorPath . emplace_back ( arg . nameToken ( ) , std : : string ( safeHigh ? " Safe checks: " : " " ) + " Assuming argument has value " + MathLib : : toString ( high ) ) ;
argValues . back ( ) . safe = safeHigh ;
2019-07-17 22:17:34 +02:00
}
2019-07-12 11:09:24 +02:00
if ( ! argValues . empty ( ) )
2019-09-26 10:32:25 +02:00
valueFlowForwardVariable ( const_cast < Token * > ( functionScope - > bodyStart - > next ( ) ) ,
functionScope - > bodyEnd ,
& arg ,
arg . declarationId ( ) ,
argValues ,
false ,
false ,
tokenlist ,
errorLogger ,
settings ) ;
2019-07-10 16:59:05 +02:00
}
}
}
2019-07-11 16:05:34 +02:00
static void valueFlowUnknownFunctionReturn ( TokenList * tokenlist , const Settings * settings )
2019-07-10 20:00:21 +02:00
{
2019-07-11 16:05:34 +02:00
if ( settings - > checkUnknownFunctionReturn . empty ( ) )
2019-07-10 20:00:21 +02:00
return ;
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) ) {
2020-05-19 21:03:28 +02:00
if ( ! tok - > astParent ( ) | | tok - > str ( ) ! = " ( " | | ! tok - > previous ( ) - > isName ( ) )
2019-07-10 20:00:21 +02:00
continue ;
2019-07-11 16:05:34 +02:00
if ( settings - > checkUnknownFunctionReturn . find ( tok - > previous ( ) - > str ( ) ) = = settings - > checkUnknownFunctionReturn . end ( ) )
continue ;
std : : vector < MathLib : : bigint > unknownValues = settings - > library . unknownReturnValues ( tok - > astOperand1 ( ) ) ;
if ( unknownValues . empty ( ) )
2019-07-10 20:00:21 +02:00
continue ;
// Get min/max values for return type
2019-07-10 22:08:27 +02:00
const std : : string & typestr = settings - > library . returnValueType ( tok - > previous ( ) ) ;
2019-07-10 21:26:20 +02:00
MathLib : : bigint minvalue , maxvalue ;
2019-07-10 20:00:21 +02:00
if ( ! getMinMaxValues ( typestr , settings , & minvalue , & maxvalue ) )
continue ;
2019-07-11 16:05:34 +02:00
for ( MathLib : : bigint value : unknownValues ) {
if ( value < minvalue )
value = minvalue ;
else if ( value > maxvalue )
value = maxvalue ;
setTokenValue ( const_cast < Token * > ( tok ) , ValueFlow : : Value ( value ) , settings ) ;
}
2019-07-10 20:00:21 +02:00
}
}
2019-09-20 15:06:37 +02:00
ValueFlow : : Value : : Value ( const Token * c , long long val )
2017-05-16 23:12:35 +02:00
: valueType ( INT ) ,
2019-09-20 15:06:37 +02:00
bound ( Bound : : Point ) ,
2017-05-16 23:12:35 +02:00
intvalue ( val ) ,
tokvalue ( nullptr ) ,
floatValue ( 0.0 ) ,
2019-07-10 15:27:07 +02:00
moveKind ( MoveKind : : NonMovedVariable ) ,
2017-05-16 23:12:35 +02:00
varvalue ( val ) ,
condition ( c ) ,
varId ( 0U ) ,
2019-07-25 17:19:51 +02:00
safe ( false ) ,
2017-05-16 23:12:35 +02:00
conditional ( false ) ,
defaultArg ( false ) ,
2019-08-17 07:36:41 +02:00
indirect ( 0 ) ,
2020-02-17 10:31:08 +01:00
path ( 0 ) ,
2019-07-16 10:51:07 +02:00
lifetimeKind ( LifetimeKind : : Object ) ,
2019-07-16 11:12:35 +02:00
lifetimeScope ( LifetimeScope : : Local ) ,
2017-05-16 23:12:35 +02:00
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> " ;
2019-03-17 13:09:15 +01:00
case BUFFER_SIZE :
2018-08-10 11:29:16 +02:00
case CONTAINER_SIZE :
return " size= " + MathLib : : toString ( intvalue ) ;
2018-11-10 16:40:40 +01:00
case LIFETIME :
return " lifetime= " + tokvalue - > str ( ) ;
2020-04-21 17:27:51 +02:00
}
2017-05-19 16:32:58 +02:00
throw InternalError ( nullptr , " Invalid ValueFlow Value type " ) ;
}
2020-05-10 20:32:59 +02:00
const char * ValueFlow : : Value : : toString ( MoveKind moveKind )
{
switch ( moveKind ) {
case MoveKind : : NonMovedVariable :
return " NonMovedVariable " ;
case MoveKind : : MovedVariable :
return " MovedVariable " ;
case MoveKind : : ForwardedVariable :
return " ForwardedVariable " ;
}
return " " ;
}
2019-06-16 10:09:38 +02:00
const ValueFlow : : Value * ValueFlow : : valueFlowConstantFoldAST ( 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
}
2018-11-03 15:53:24 +01:00
static std : : size_t getTotalValues ( TokenList * tokenlist )
{
std : : size_t n = 1 ;
for ( Token * tok = tokenlist - > front ( ) ; tok ; tok = tok - > next ( ) )
n + = tok - > values ( ) . size ( ) ;
return n ;
}
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 ) ;
2019-07-11 16:05:34 +02:00
valueFlowUnknownFunctionReturn ( tokenlist , settings ) ;
2019-04-21 06:54:32 +02:00
valueFlowGlobalConstVar ( tokenlist , settings ) ;
2017-09-15 15:58:19 +02:00
valueFlowGlobalStaticVar ( tokenlist , settings ) ;
2014-08-03 20:11:22 +02:00
valueFlowPointerAlias ( tokenlist ) ;
2018-11-10 16:40:40 +01:00
valueFlowLifetime ( tokenlist , symboldatabase , errorLogger , settings ) ;
2016-11-05 09:59:48 +01:00
valueFlowFunctionReturn ( tokenlist , errorLogger ) ;
2014-04-14 06:45:39 +02:00
valueFlowBitAnd ( tokenlist ) ;
2018-12-16 07:35:27 +01:00
valueFlowSameExpressions ( tokenlist ) ;
2018-12-31 17:05:46 +01:00
valueFlowFwdAnalysis ( tokenlist , settings ) ;
2018-11-03 15:53:24 +01:00
std : : size_t values = 0 ;
2019-09-23 19:35:39 +02:00
std : : size_t n = 4 ;
while ( n > 0 & & values < getTotalValues ( tokenlist ) ) {
2018-11-03 15:53:24 +01:00
values = getTotalValues ( tokenlist ) ;
2019-08-12 12:58:53 +02:00
valueFlowPointerAliasDeref ( tokenlist ) ;
2019-01-21 20:05:35 +01:00
valueFlowArrayBool ( tokenlist ) ;
2019-01-01 14:15:50 +01:00
valueFlowRightShift ( tokenlist , settings ) ;
2018-11-03 15:53:24 +01:00
valueFlowOppositeCondition ( symboldatabase , settings ) ;
2019-08-05 07:18:06 +02:00
valueFlowTerminatingCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
2018-11-03 15:53:24 +01:00
valueFlowBeforeCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
valueFlowAfterMove ( tokenlist , symboldatabase , errorLogger , settings ) ;
valueFlowAfterCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
2019-09-28 20:50:56 +02:00
valueFlowInferCondition ( tokenlist , settings ) ;
2020-01-05 16:25:33 +01:00
valueFlowAfterAssign ( tokenlist , symboldatabase , errorLogger , settings ) ;
2018-11-03 15:53:24 +01:00
valueFlowSwitchVariable ( tokenlist , symboldatabase , errorLogger , settings ) ;
valueFlowForLoop ( tokenlist , symboldatabase , errorLogger , settings ) ;
2020-02-17 10:31:08 +01:00
valueFlowSubFunction ( tokenlist , symboldatabase , errorLogger , settings ) ;
2018-11-03 15:53:24 +01:00
valueFlowFunctionDefaultParameter ( tokenlist , symboldatabase , errorLogger , settings ) ;
valueFlowUninit ( tokenlist , symboldatabase , errorLogger , settings ) ;
2018-11-24 10:07:12 +01:00
if ( tokenlist - > isCPP ( ) ) {
2019-04-26 11:30:09 +02:00
valueFlowSmartPointer ( tokenlist , errorLogger , settings ) ;
2018-11-03 15:53:24 +01:00
valueFlowContainerSize ( tokenlist , symboldatabase , errorLogger , settings ) ;
2018-11-24 10:07:12 +01:00
valueFlowContainerAfterCondition ( tokenlist , symboldatabase , errorLogger , settings ) ;
}
2019-07-10 16:59:05 +02:00
valueFlowSafeFunctions ( tokenlist , symboldatabase , errorLogger , settings ) ;
2019-09-23 19:35:39 +02:00
n - - ;
2018-11-03 15:53:24 +01:00
}
2019-03-17 13:09:15 +01:00
valueFlowDynamicBufferSize ( 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 " ;
2020-05-19 21:55:28 +02:00
}