2008-12-18 22:28:57 +01:00
/*
2009-01-21 21:04:20 +01:00
* Cppcheck - A tool for static C / C + + code analysis
2023-01-28 10:16:34 +01:00
* Copyright ( C ) 2007 - 2023 Cppcheck team .
2008-12-18 22:28:57 +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
2009-09-27 17:08:31 +02:00
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
2008-12-18 22:28:57 +01:00
*/
# include "token.h"
2017-05-27 04:33:47 +02:00
2019-01-20 13:20:23 +01:00
# include "astutils.h"
2022-01-27 19:03:20 +01:00
# include "errortypes.h"
2017-05-27 04:33:47 +02:00
# include "library.h"
2014-04-02 06:49:28 +02:00
# include "settings.h"
2023-09-26 15:58:16 +02:00
# include "simplecpp.h"
2014-08-05 06:24:23 +02:00
# include "symboldatabase.h"
2020-05-19 14:32:50 +02:00
# include "tokenlist.h"
2015-11-29 10:49:10 +01:00
# include "utils.h"
2021-06-14 07:39:01 +02:00
# include "tokenrange.h"
2021-03-30 14:02:28 +02:00
# include "valueflow.h"
2017-05-27 04:33:47 +02:00
2020-05-10 20:32:59 +02:00
# include <algorithm>
2009-08-22 16:22:50 +02:00
# include <cassert>
2017-05-27 04:33:47 +02:00
# include <cctype>
2022-01-27 19:03:20 +01:00
# include <climits>
# include <cstdio>
2008-12-18 22:28:57 +01:00
# include <cstring>
2021-03-30 14:02:28 +02:00
# include <functional>
2008-12-18 22:28:57 +01:00
# include <iostream>
2022-01-27 19:03:20 +01:00
# include <iterator>
2009-03-03 21:17:23 +01:00
# include <map>
2017-05-27 04:33:47 +02:00
# include <set>
2022-09-16 07:15:49 +02:00
# include <sstream> // IWYU pragma: keep
2013-12-28 11:02:39 +01:00
# include <stack>
2022-01-27 19:03:20 +01:00
# include <unordered_set>
2017-05-27 04:33:47 +02:00
# include <utility>
2014-03-08 20:56:39 +01:00
2023-01-26 22:23:22 +01:00
namespace {
struct less {
template < class T , class U >
bool operator ( ) ( const T & x , const U & y ) const {
return x < y ;
}
} ;
}
2018-12-21 13:51:45 +01:00
const std : : list < ValueFlow : : Value > TokenImpl : : mEmptyValueList ;
2017-08-13 14:15:24 +02:00
2018-05-25 07:15:05 +02:00
Token : : Token ( TokensFrontBack * tokensFrontBack ) :
2023-08-08 11:05:02 +02:00
mTokensFrontBack ( tokensFrontBack )
2008-12-18 22:28:57 +01:00
{
2018-12-21 13:51:45 +01:00
mImpl = new TokenImpl ( ) ;
2008-12-18 22:28:57 +01:00
}
2009-01-03 21:29:20 +01:00
Token : : ~ Token ( )
2008-12-18 22:28:57 +01:00
{
2018-12-21 13:51:45 +01:00
delete mImpl ;
2008-12-18 22:28:57 +01:00
}
2021-06-14 07:39:01 +02:00
/*
2021-08-07 20:51:18 +02:00
* Get a TokenRange which starts at this token and contains every token following it in order up to but not including ' t '
* e . g . for the sequence of tokens A B C D E , C . until ( E ) would yield the Range C D
* note t can be nullptr to iterate all the way to the end .
*/
2023-01-14 20:16:55 +01:00
// cppcheck-suppress unusedFunction // only used in testtokenrange.cpp
2021-06-14 07:39:01 +02:00
ConstTokenRange Token : : until ( const Token * t ) const
{
return ConstTokenRange ( this , t ) ;
}
2021-01-16 13:52:09 +01:00
static const std : : unordered_set < std : : string > controlFlowKeywords = {
2018-04-08 22:54:10 +02:00
" goto " ,
" do " ,
" if " ,
" else " ,
" for " ,
" while " ,
" switch " ,
" case " ,
" break " ,
" continue " ,
" return "
} ;
2017-11-17 22:10:39 +01:00
2023-03-02 21:20:56 +01:00
// TODO: replace with Keywords::getX()?
2022-06-18 21:30:42 +02:00
// Another list of keywords
static const std : : unordered_set < std : : string > baseKeywords = {
" asm " ,
" auto " ,
" break " ,
" case " ,
" const " ,
" continue " ,
" default " ,
" do " ,
" else " ,
" enum " ,
" extern " ,
" for " ,
" goto " ,
" if " ,
" inline " ,
" register " ,
" restrict " ,
" return " ,
" sizeof " ,
" static " ,
" struct " ,
" switch " ,
" typedef " ,
" union " ,
" volatile " ,
" while " ,
" void "
} ;
2011-10-23 20:38:03 +02:00
void Token : : update_property_info ( )
2008-12-18 22:28:57 +01:00
{
2018-06-16 23:03:15 +02:00
setFlag ( fIsControlFlowKeyword , controlFlowKeywords . find ( mStr ) ! = controlFlowKeywords . end ( ) ) ;
2017-11-17 22:10:39 +01:00
2018-06-16 23:03:15 +02:00
if ( ! mStr . empty ( ) ) {
if ( mStr = = " true " | | mStr = = " false " )
2017-10-15 01:27:47 +02:00
tokType ( eBoolean ) ;
2019-10-16 11:41:33 +02:00
else if ( isStringLiteral ( mStr ) )
2019-03-10 10:38:50 +01:00
tokType ( eString ) ;
2019-10-16 11:41:33 +02:00
else if ( isCharLiteral ( mStr ) )
2019-03-10 10:38:50 +01:00
tokType ( eChar ) ;
2018-06-16 23:03:15 +02:00
else if ( std : : isalpha ( ( unsigned char ) mStr [ 0 ] ) | | mStr [ 0 ] = = ' _ ' | | mStr [ 0 ] = = ' $ ' ) { // Name
2018-12-21 13:51:45 +01:00
if ( mImpl - > mVarId )
2017-10-15 01:27:47 +02:00
tokType ( eVariable ) ;
2020-05-17 17:25:33 +02:00
else if ( mTokensFrontBack & & mTokensFrontBack - > list & & mTokensFrontBack - > list - > isKeyword ( mStr ) )
tokType ( eKeyword ) ;
2022-06-18 21:30:42 +02:00
else if ( baseKeywords . count ( mStr ) > 0 )
tokType ( eKeyword ) ;
2018-06-16 16:40:02 +02:00
else if ( mTokType ! = eVariable & & mTokType ! = eFunction & & mTokType ! = eType & & mTokType ! = eKeyword )
2017-10-15 01:27:47 +02:00
tokType ( eName ) ;
2023-09-26 15:58:16 +02:00
} else if ( simplecpp : : Token : : isNumberLike ( mStr ) ) {
2023-09-15 10:00:00 +02:00
if ( MathLib : : isInt ( mStr ) | | MathLib : : isFloat ( mStr ) )
tokType ( eNumber ) ;
else
tokType ( eName ) ; // assume it is a user defined literal
} else if ( mStr = = " = " | | mStr = = " <<= " | | mStr = = " >>= " | |
( mStr . size ( ) = = 2U & & mStr [ 1 ] = = ' = ' & & std : : strchr ( " +-*/%&^| " , mStr [ 0 ] ) ) )
2017-10-15 01:27:47 +02:00
tokType ( eAssignmentOp ) ;
2018-06-16 23:03:15 +02:00
else if ( mStr . size ( ) = = 1 & & mStr . find_first_of ( " ,[]()?: " ) ! = std : : string : : npos )
2017-10-15 01:27:47 +02:00
tokType ( eExtendedOp ) ;
2018-06-16 23:03:15 +02:00
else if ( mStr = = " << " | | mStr = = " >> " | | ( mStr . size ( ) = = 1 & & mStr . find_first_of ( " +-*/% " ) ! = std : : string : : npos ) )
2017-10-15 01:27:47 +02:00
tokType ( eArithmeticalOp ) ;
2018-06-16 23:03:15 +02:00
else if ( mStr . size ( ) = = 1 & & mStr . find_first_of ( " &|^~ " ) ! = std : : string : : npos )
2017-10-15 01:27:47 +02:00
tokType ( eBitOp ) ;
2018-06-16 23:03:15 +02:00
else if ( mStr . size ( ) < = 2 & &
( mStr = = " && " | |
mStr = = " || " | |
mStr = = " ! " ) )
2017-10-15 01:27:47 +02:00
tokType ( eLogicalOp ) ;
2018-06-16 23:03:15 +02:00
else if ( mStr . size ( ) < = 2 & & ! mLink & &
( mStr = = " == " | |
mStr = = " != " | |
2021-08-07 20:51:18 +02:00
mStr = = " < " | |
2018-06-16 23:03:15 +02:00
mStr = = " <= " | |
2021-08-07 20:51:18 +02:00
mStr = = " > " | |
2018-06-16 23:03:15 +02:00
mStr = = " >= " ) )
2017-10-15 01:27:47 +02:00
tokType ( eComparisonOp ) ;
2021-04-22 19:15:22 +02:00
else if ( mStr = = " <=> " )
tokType ( eComparisonOp ) ;
2018-06-16 23:03:15 +02:00
else if ( mStr . size ( ) = = 2 & &
( mStr = = " ++ " | |
mStr = = " -- " ) )
2017-10-15 01:27:47 +02:00
tokType ( eIncDecOp ) ;
2018-06-16 23:03:15 +02:00
else if ( mStr . size ( ) = = 1 & & ( mStr . find_first_of ( " { } " ) != std::string::npos || (mLink && mStr.find_first_of( " < > " ) != std::string::npos)))
2017-10-15 01:27:47 +02:00
tokType ( eBracket ) ;
2019-09-04 08:07:30 +02:00
else if ( mStr = = " ... " )
tokType ( eEllipsis ) ;
2011-04-18 06:56:39 +02:00
else
2017-10-15 01:27:47 +02:00
tokType ( eOther ) ;
2011-10-23 20:38:03 +02:00
} else {
2017-10-15 01:27:47 +02:00
tokType ( eNone ) ;
2011-04-18 06:56:39 +02:00
}
2011-11-09 21:45:59 +01:00
2019-03-18 06:18:25 +01:00
update_property_char_string_literal ( ) ;
2011-11-09 21:45:59 +01:00
update_property_isStandardType ( ) ;
}
2021-01-16 13:52:09 +01:00
static const std : : unordered_set < std : : string > stdTypes = { " bool "
2021-01-22 21:47:24 +01:00
, " _Bool "
, " char "
, " double "
, " float "
, " int "
, " long "
, " short "
, " size_t "
, " void "
, " wchar_t "
2021-08-07 20:51:18 +02:00
} ;
2015-06-10 21:14:17 +02:00
2011-11-09 21:45:59 +01:00
void Token : : update_property_isStandardType ( )
{
2014-05-06 06:35:48 +02:00
isStandardType ( false ) ;
2011-11-09 21:45:59 +01:00
2018-06-16 23:03:15 +02:00
if ( mStr . size ( ) < 3 )
2011-11-09 21:45:59 +01:00
return ;
2018-06-16 23:03:15 +02:00
if ( stdTypes . find ( mStr ) ! = stdTypes . end ( ) ) {
2014-05-06 06:35:48 +02:00
isStandardType ( true ) ;
2017-10-15 01:27:47 +02:00
tokType ( eType ) ;
2011-11-09 21:45:59 +01:00
}
2011-10-23 20:38:03 +02:00
}
2008-12-21 14:58:56 +01:00
2019-03-18 06:18:25 +01:00
void Token : : update_property_char_string_literal ( )
{
2019-10-16 11:41:33 +02:00
if ( mTokType ! = Token : : eString & & mTokType ! = Token : : eChar )
2019-03-18 06:18:25 +01:00
return ;
2019-10-16 11:41:33 +02:00
isLong ( ( ( mTokType = = Token : : eString ) & & isPrefixStringCharLiteral ( mStr , ' " ' , " L " ) ) | |
( ( mTokType = = Token : : eChar ) & & isPrefixStringCharLiteral ( mStr , ' \' ' , " L " ) ) ) ;
2019-03-18 06:18:25 +01:00
}
2011-11-09 21:45:59 +01:00
2012-06-21 19:00:53 +02:00
bool Token : : isUpperCaseName ( ) const
{
if ( ! isName ( ) )
return false ;
2023-03-04 11:58:12 +01:00
return std : : none_of ( mStr . begin ( ) , mStr . end ( ) , [ ] ( char c ) {
return std : : islower ( c ) ;
} ) ;
2012-06-21 19:00:53 +02:00
}
2009-03-28 20:33:55 +01:00
void Token : : concatStr ( std : : string const & b )
{
2021-02-17 22:39:29 +01:00
mStr . pop_back ( ) ;
2019-10-16 11:41:33 +02:00
mStr . append ( getStringLiteral ( b ) + " \" " ) ;
2011-10-23 20:38:03 +02:00
2019-11-08 08:03:45 +01:00
if ( isCChar ( ) & & isStringLiteral ( b ) & & b [ 0 ] ! = ' " ' ) {
mStr . insert ( 0 , b . substr ( 0 , b . find ( ' " ' ) ) ) ;
}
2011-10-23 20:38:03 +02:00
update_property_info ( ) ;
2009-03-28 20:33:55 +01:00
}
2009-09-13 10:02:23 +02:00
std : : string Token : : strValue ( ) const
2009-09-12 22:54:47 +02:00
{
2018-06-16 16:40:02 +02:00
assert ( mTokType = = eString ) ;
2019-10-16 11:41:33 +02:00
std : : string ret ( getStringLiteral ( mStr ) ) ;
2015-06-07 11:25:33 +02:00
std : : string : : size_type pos = 0U ;
2015-08-14 20:46:13 +02:00
while ( ( pos = ret . find ( ' \\ ' , pos ) ) ! = std : : string : : npos ) {
2015-06-07 11:25:33 +02:00
ret . erase ( pos , 1U ) ;
if ( ret [ pos ] > = ' a ' ) {
if ( ret [ pos ] = = ' n ' )
ret [ pos ] = ' \n ' ;
else if ( ret [ pos ] = = ' r ' )
ret [ pos ] = ' \r ' ;
else if ( ret [ pos ] = = ' t ' )
ret [ pos ] = ' \t ' ;
}
if ( ret [ pos ] = = ' 0 ' )
return ret . substr ( 0 , pos ) ;
pos + + ;
}
return ret ;
2009-09-12 22:54:47 +02:00
}
2019-07-14 12:22:33 +02:00
void Token : : deleteNext ( nonneg int count )
2008-12-18 22:28:57 +01:00
{
2019-07-13 15:47:53 +02:00
while ( mNext & & count > 0 ) {
2018-06-16 16:16:55 +02:00
Token * n = mNext ;
2017-08-25 17:17:19 +02:00
// #8154 we are about to be unknown -> destroy the link to us
2018-06-16 20:30:09 +02:00
if ( n - > mLink & & n - > mLink - > mLink = = n )
n - > mLink - > link ( nullptr ) ;
2017-08-25 17:17:19 +02:00
2018-06-16 16:16:55 +02:00
mNext = n - > next ( ) ;
2011-12-07 23:36:11 +01:00
delete n ;
2019-07-13 15:47:53 +02:00
- - count ;
2011-12-07 23:36:11 +01:00
}
2018-06-16 16:16:55 +02:00
if ( mNext )
mNext - > previous ( this ) ;
2018-06-16 16:22:35 +02:00
else if ( mTokensFrontBack )
mTokensFrontBack - > back = this ;
2018-05-25 07:15:05 +02:00
}
2019-07-14 12:22:33 +02:00
void Token : : deletePrevious ( nonneg int count )
2018-05-25 07:15:05 +02:00
{
2019-07-13 15:47:53 +02:00
while ( mPrevious & & count > 0 ) {
2018-06-16 16:16:55 +02:00
Token * p = mPrevious ;
2018-05-25 07:15:05 +02:00
// #8154 we are about to be unknown -> destroy the link to us
2018-06-16 20:30:09 +02:00
if ( p - > mLink & & p - > mLink - > mLink = = p )
p - > mLink - > link ( nullptr ) ;
2018-05-25 07:15:05 +02:00
2018-06-16 16:16:55 +02:00
mPrevious = p - > previous ( ) ;
2018-05-25 07:15:05 +02:00
delete p ;
2019-07-13 15:47:53 +02:00
- - count ;
2018-05-25 07:15:05 +02:00
}
2018-06-16 16:16:55 +02:00
if ( mPrevious )
mPrevious - > next ( this ) ;
2018-06-16 16:22:35 +02:00
else if ( mTokensFrontBack )
mTokensFrontBack - > front = this ;
2008-12-18 22:28:57 +01:00
}
2014-12-27 10:53:26 +01:00
void Token : : swapWithNext ( )
{
2018-06-16 16:16:55 +02:00
if ( mNext ) {
2018-06-16 23:03:15 +02:00
std : : swap ( mStr , mNext - > mStr ) ;
2018-06-16 16:40:02 +02:00
std : : swap ( mTokType , mNext - > mTokType ) ;
2018-06-16 16:16:55 +02:00
std : : swap ( mFlags , mNext - > mFlags ) ;
2018-12-21 13:51:45 +01:00
std : : swap ( mImpl , mNext - > mImpl ) ;
2020-05-15 09:02:59 +02:00
if ( mImpl - > mTemplateSimplifierPointers )
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2020-05-15 09:02:59 +02:00
for ( auto * templateSimplifierPointer : * mImpl - > mTemplateSimplifierPointers ) {
templateSimplifierPointer - > token ( this ) ;
}
2018-12-21 13:51:45 +01:00
2020-05-15 09:02:59 +02:00
if ( mNext - > mImpl - > mTemplateSimplifierPointers )
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2020-05-15 09:02:59 +02:00
for ( auto * templateSimplifierPointer : * mNext - > mImpl - > mTemplateSimplifierPointers ) {
templateSimplifierPointer - > token ( mNext ) ;
}
2018-06-16 20:30:09 +02:00
if ( mNext - > mLink )
mNext - > mLink - > mLink = this ;
if ( this - > mLink )
this - > mLink - > mLink = mNext ;
std : : swap ( mLink , mNext - > mLink ) ;
2014-12-27 10:53:26 +01:00
}
}
2017-08-25 23:30:04 +02:00
void Token : : takeData ( Token * fromToken )
{
2018-06-16 23:03:15 +02:00
mStr = fromToken - > mStr ;
2018-06-16 16:40:02 +02:00
tokType ( fromToken - > mTokType ) ;
2018-06-16 16:14:34 +02:00
mFlags = fromToken - > mFlags ;
2018-12-21 13:51:45 +01:00
delete mImpl ;
mImpl = fromToken - > mImpl ;
fromToken - > mImpl = nullptr ;
2020-05-15 09:02:59 +02:00
if ( mImpl - > mTemplateSimplifierPointers )
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2020-05-15 09:02:59 +02:00
for ( auto * templateSimplifierPointer : * mImpl - > mTemplateSimplifierPointers ) {
templateSimplifierPointer - > token ( this ) ;
}
2018-12-21 13:51:45 +01:00
mLink = fromToken - > mLink ;
2018-06-16 20:30:09 +02:00
if ( mLink )
mLink - > link ( this ) ;
2017-08-25 23:30:04 +02:00
}
2009-03-13 00:07:05 +01:00
void Token : : deleteThis ( )
{
2018-06-16 16:16:55 +02:00
if ( mNext ) { // Copy next to this and delete next
takeData ( mNext ) ;
mNext - > link ( nullptr ) ; // mark as unlinked
2009-03-13 00:07:05 +01:00
deleteNext ( ) ;
2020-11-22 16:43:36 +01:00
} else if ( mPrevious ) { // Copy previous to this and delete previous
2018-06-16 16:16:55 +02:00
takeData ( mPrevious ) ;
2020-11-22 16:43:36 +01:00
mPrevious - > link ( nullptr ) ;
deletePrevious ( ) ;
2011-10-13 20:53:06 +02:00
} else {
2009-03-13 00:07:05 +01:00
// We are the last token in the list, we can't delete
2012-02-13 17:44:08 +01:00
// ourselves, so just make us empty
2020-11-22 16:43:36 +01:00
str ( " ; " ) ;
2009-03-13 00:07:05 +01:00
}
}
2009-01-26 23:26:50 +01:00
void Token : : replace ( Token * replaceThis , Token * start , Token * end )
{
// Fix the whole in the old location of start and end
2010-04-02 07:30:58 +02:00
if ( start - > previous ( ) )
2009-05-26 22:22:00 +02:00
start - > previous ( ) - > next ( end - > next ( ) ) ;
2010-04-02 07:30:58 +02:00
if ( end - > next ( ) )
2009-05-26 22:22:00 +02:00
end - > next ( ) - > previous ( start - > previous ( ) ) ;
2009-01-26 23:26:50 +01:00
// Move start and end to their new location
2010-04-02 07:30:58 +02:00
if ( replaceThis - > previous ( ) )
2009-05-26 22:22:00 +02:00
replaceThis - > previous ( ) - > next ( start ) ;
2010-04-02 07:30:58 +02:00
if ( replaceThis - > next ( ) )
2009-05-26 22:22:00 +02:00
replaceThis - > next ( ) - > previous ( end ) ;
2009-01-26 23:26:50 +01:00
start - > previous ( replaceThis - > previous ( ) ) ;
end - > next ( replaceThis - > next ( ) ) ;
2018-06-16 16:22:35 +02:00
if ( end - > mTokensFrontBack & & end - > mTokensFrontBack - > back = = end ) {
2010-04-02 07:30:58 +02:00
while ( end - > next ( ) )
2010-01-06 20:19:27 +01:00
end = end - > next ( ) ;
2018-06-16 16:22:35 +02:00
end - > mTokensFrontBack - > back = end ;
2010-01-06 20:19:27 +01:00
}
2018-06-16 16:41:25 +02:00
// Update mProgressValue, fileIndex and linenr
2012-01-22 00:02:55 +01:00
for ( Token * tok = start ; tok ! = end - > next ( ) ; tok = tok - > next ( ) )
2018-12-21 13:51:45 +01:00
tok - > mImpl - > mProgressValue = replaceThis - > mImpl - > mProgressValue ;
2012-01-21 21:05:41 +01:00
2009-01-26 23:26:50 +01:00
// Delete old token, which is replaced
delete replaceThis ;
}
2023-12-06 14:15:35 +01:00
template < class T , REQUIRES ( " T must be a Token class " , std : : is_convertible < T * , const Token * > ) >
static T * tokAtImpl ( T * tok , int index )
2008-12-18 22:28:57 +01:00
{
2015-12-14 22:04:26 +01:00
while ( index > 0 & & tok ) {
tok = tok - > next ( ) ;
- - index ;
}
while ( index < 0 & & tok ) {
tok = tok - > previous ( ) ;
+ + index ;
2008-12-18 22:28:57 +01:00
}
return tok ;
}
2023-12-06 14:15:35 +01:00
const Token * Token : : tokAt ( int index ) const
2011-11-11 21:55:37 +01:00
{
2023-12-06 14:15:35 +01:00
return tokAtImpl ( this , index ) ;
}
Token * Token : : tokAt ( int index )
{
return tokAtImpl ( this , index ) ;
}
template < class T , REQUIRES ( " T must be a Token class " , std : : is_convertible < T * , const Token * > ) >
static T * linkAtImpl ( T * thisTok , int index )
{
T * tok = thisTok - > tokAt ( index ) ;
2011-11-19 13:34:36 +01:00
if ( ! tok ) {
2023-12-06 14:15:35 +01:00
throw InternalError ( thisTok , " Internal error. Token::linkAt called with index outside the tokens range. " ) ;
2011-11-19 13:34:36 +01:00
}
2012-09-09 14:34:07 +02:00
return tok - > link ( ) ;
2011-11-11 21:55:37 +01:00
}
2023-12-06 14:15:35 +01:00
const Token * Token : : linkAt ( int index ) const
{
return linkAtImpl ( this , index ) ;
}
Token * Token : : linkAt ( int index )
{
return linkAtImpl ( this , index ) ;
}
2011-11-14 09:16:47 +01:00
const std : : string & Token : : strAt ( int index ) const
2008-12-18 22:28:57 +01:00
{
2009-01-03 21:29:20 +01:00
const Token * tok = this - > tokAt ( index ) ;
2018-06-16 23:03:15 +02:00
return tok ? tok - > mStr : emptyString ;
2008-12-18 22:28:57 +01:00
}
optimized `Token::Match()` a bit by always inlining `Token::multiCompare()` (#5332)
Scanning `mame_regtest` with `DISABLE_VALUEFLOW=1` and `--enable=all
--inconclusive`:
Clang 15 `1,170,770,173` -> `1,167,227,434`
GGC 12 `1,370,070,422` -> `1,366,775,852`
2023-08-18 20:41:50 +02:00
static
# if defined(__GNUC__)
// GCC does not inline this by itself
// need to use the old syntax since the C++11 [[xxx:always_inline]] cannot be used here
inline __attribute__ ( ( always_inline ) )
# endif
int multiComparePercent ( const Token * tok , const char * & haystack , nonneg int varid )
2011-11-05 19:24:21 +01:00
{
2014-06-26 18:17:05 +02:00
+ + haystack ;
// Compare only the first character of the string for optimization reasons
switch ( haystack [ 0 ] ) {
case ' v ' :
if ( haystack [ 3 ] = = ' % ' ) { // %var%
haystack + = 4 ;
2015-01-31 10:50:39 +01:00
if ( tok - > varId ( ) ! = 0 )
2014-06-26 18:17:05 +02:00
return 1 ;
} else { // %varid%
if ( varid = = 0 ) {
throw InternalError ( tok , " Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers " ) ;
}
2011-11-05 19:24:21 +01:00
2014-06-26 18:17:05 +02:00
haystack + = 6 ;
if ( tok - > varId ( ) = = varid )
return 1 ;
}
break ;
case ' t ' :
// Type (%type%)
{
haystack + = 5 ;
2020-10-27 09:08:13 +01:00
if ( tok - > isName ( ) & & tok - > varId ( ) = = 0 & & ( tok - > str ( ) ! = " delete " | | ! tok - > isKeyword ( ) ) ) // HACK: this is legacy behaviour, it should return false for all keywords, except types
2014-06-26 18:17:05 +02:00
return 1 ;
}
break ;
case ' a ' :
2015-12-31 01:15:49 +01:00
// Accept any token (%any%) or assign (%assign%)
2014-06-26 18:17:05 +02:00
{
2015-12-31 01:15:49 +01:00
if ( haystack [ 3 ] = = ' % ' ) { // %any%
haystack + = 4 ;
return 1 ;
}
2023-06-20 18:43:21 +02:00
// %assign%
haystack + = 7 ;
if ( tok - > isAssignmentOp ( ) )
return 1 ;
2014-06-26 18:17:05 +02:00
}
2015-12-31 01:15:49 +01:00
break ;
2014-06-26 18:17:05 +02:00
case ' n ' :
2015-01-31 10:50:39 +01:00
// Number (%num%) or name (%name%)
2014-06-26 18:17:05 +02:00
{
2015-01-31 10:50:39 +01:00
if ( haystack [ 4 ] = = ' % ' ) { // %name%
haystack + = 5 ;
if ( tok - > isName ( ) )
return 1 ;
} else {
haystack + = 4 ;
if ( tok - > isNumber ( ) )
return 1 ;
}
2014-06-26 18:17:05 +02:00
}
break ;
case ' c ' : {
haystack + = 1 ;
// Character (%char%)
if ( haystack [ 0 ] = = ' h ' ) {
haystack + = 4 ;
2015-08-14 20:46:13 +02:00
if ( tok - > tokType ( ) = = Token : : eChar )
2011-11-05 19:24:21 +01:00
return 1 ;
2014-06-26 18:17:05 +02:00
}
// Const operator (%cop%)
else if ( haystack [ 1 ] = = ' p ' ) {
haystack + = 3 ;
2013-03-01 11:43:59 +01:00
if ( tok - > isConstOp ( ) )
return 1 ;
2014-06-26 18:17:05 +02:00
}
// Comparison operator (%comp%)
else {
haystack + = 4 ;
if ( tok - > isComparisonOp ( ) )
2011-11-05 19:24:21 +01:00
return 1 ;
2014-06-26 18:17:05 +02:00
}
}
break ;
case ' s ' :
// String (%str%)
{
haystack + = 4 ;
2015-08-14 20:46:13 +02:00
if ( tok - > tokType ( ) = = Token : : eString )
2014-06-26 18:17:05 +02:00
return 1 ;
}
break ;
case ' b ' :
// Bool (%bool%)
{
haystack + = 5 ;
if ( tok - > isBoolean ( ) )
return 1 ;
}
break ;
case ' o ' : {
+ + haystack ;
if ( haystack [ 1 ] = = ' % ' ) {
// Op (%op%)
if ( haystack [ 0 ] = = ' p ' ) {
haystack + = 2 ;
if ( tok - > isOp ( ) )
return 1 ;
}
// Or (%or%)
else {
haystack + = 2 ;
2015-08-14 20:46:13 +02:00
if ( tok - > tokType ( ) = = Token : : eBitOp & & tok - > str ( ) = = " | " )
2014-06-26 18:17:05 +02:00
return 1 ;
}
}
// Oror (%oror%)
else {
haystack + = 4 ;
2015-08-14 20:46:13 +02:00
if ( tok - > tokType ( ) = = Token : : eLogicalOp & & tok - > str ( ) = = " || " )
2011-11-05 19:24:21 +01:00
return 1 ;
}
2014-06-26 18:17:05 +02:00
}
break ;
default :
//unknown %cmd%, abort
2015-03-29 21:05:18 +02:00
throw InternalError ( tok , " Unexpected command " ) ;
2011-11-05 19:24:21 +01:00
}
2014-06-26 18:17:05 +02:00
if ( * haystack = = ' | ' )
haystack + = 1 ;
else
return - 1 ;
2011-11-05 19:24:21 +01:00
return 0xFFFF ;
}
optimized `Token::Match()` a bit by always inlining `Token::multiCompare()` (#5332)
Scanning `mame_regtest` with `DISABLE_VALUEFLOW=1` and `--enable=all
--inconclusive`:
Clang 15 `1,170,770,173` -> `1,167,227,434`
GGC 12 `1,370,070,422` -> `1,366,775,852`
2023-08-18 20:41:50 +02:00
static
# if defined(__GNUC__)
// need to use the old syntax since the C++11 [[xxx:always_inline]] cannot be used here
inline __attribute__ ( ( always_inline ) )
# endif
int multiCompareImpl ( const Token * tok , const char * haystack , nonneg int varid )
2008-12-18 22:28:57 +01:00
{
2014-06-26 17:32:24 +02:00
const char * needle = tok - > str ( ) . c_str ( ) ;
2009-01-27 20:30:01 +01:00
const char * needlePointer = needle ;
2012-03-25 11:51:59 +02:00
for ( ; ; ) {
2014-06-26 18:17:05 +02:00
if ( needlePointer = = needle & & haystack [ 0 ] = = ' % ' & & haystack [ 1 ] ! = ' | ' & & haystack [ 1 ] ! = ' \0 ' & & haystack [ 1 ] ! = ' ' ) {
2018-04-04 21:51:31 +02:00
const int ret = multiComparePercent ( tok , haystack , varid ) ;
2014-06-26 18:17:05 +02:00
if ( ret < 2 )
return ret ;
2011-10-13 20:53:06 +02:00
} else if ( * haystack = = ' | ' ) {
if ( * needlePointer = = 0 ) {
2010-09-19 15:14:13 +02:00
// If needle is at the end, we have a match.
2008-12-18 22:28:57 +01:00
return 1 ;
2009-01-27 20:30:01 +01:00
}
2008-12-18 22:28:57 +01:00
2009-01-27 20:30:01 +01:00
needlePointer = needle ;
2010-09-19 15:14:13 +02:00
+ + haystack ;
2014-06-26 18:17:05 +02:00
} else if ( * needlePointer = = * haystack ) {
if ( * needlePointer = = ' \0 ' )
return 1 ;
+ + needlePointer ;
+ + haystack ;
2011-10-13 20:53:06 +02:00
} else if ( * haystack = = ' ' | | * haystack = = ' \0 ' ) {
2010-09-19 15:14:13 +02:00
if ( needlePointer = = needle )
return 0 ;
break ;
2008-12-18 22:28:57 +01:00
}
2009-01-27 20:30:01 +01:00
// If haystack and needle don't share the same character,
// find next '|' character.
2011-10-13 20:53:06 +02:00
else {
2010-09-19 15:14:13 +02:00
needlePointer = needle ;
2008-12-18 22:28:57 +01:00
2011-10-13 20:53:06 +02:00
do {
2010-09-19 15:14:13 +02:00
+ + haystack ;
2008-12-18 22:28:57 +01:00
2021-10-26 08:57:54 +02:00
if ( * haystack = = ' ' | | * haystack = = ' \0 ' ) {
return - 1 ;
}
if ( * haystack = = ' | ' ) {
break ;
}
} while ( true ) ;
2008-12-18 22:28:57 +01:00
2010-09-19 15:14:13 +02:00
+ + haystack ;
2009-01-27 20:30:01 +01:00
}
}
2010-09-19 15:14:13 +02:00
if ( * needlePointer = = ' \0 ' )
return 1 ;
2008-12-18 22:28:57 +01:00
return - 1 ;
}
optimized `Token::Match()` a bit by always inlining `Token::multiCompare()` (#5332)
Scanning `mame_regtest` with `DISABLE_VALUEFLOW=1` and `--enable=all
--inconclusive`:
Clang 15 `1,170,770,173` -> `1,167,227,434`
GGC 12 `1,370,070,422` -> `1,366,775,852`
2023-08-18 20:41:50 +02:00
// cppcheck-suppress unusedFunction - used in tests only
int Token : : multiCompare ( const Token * tok , const char * haystack , nonneg int varid )
{
return multiCompareImpl ( tok , haystack , varid ) ;
}
2020-05-26 20:13:56 +02:00
bool Token : : simpleMatch ( const Token * tok , const char pattern [ ] , size_t pattern_len )
2008-12-22 00:28:09 +01:00
{
2014-04-27 09:32:02 +02:00
if ( ! tok )
return false ; // shortcut
2020-05-26 20:13:56 +02:00
const char * current = pattern ;
const char * end = pattern + pattern_len ;
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2022-02-10 20:48:51 +01:00
const char * next = static_cast < const char * > ( std : : memchr ( pattern , ' ' , pattern_len ) ) ;
2010-04-02 07:30:58 +02:00
if ( ! next )
2020-05-26 20:13:56 +02:00
next = end ;
2008-12-23 22:45:47 +01:00
2011-10-13 20:53:06 +02:00
while ( * current ) {
2018-04-04 21:51:31 +02:00
const std : : size_t length = next - current ;
2008-12-23 22:45:47 +01:00
2022-09-16 18:58:59 +02:00
if ( ! tok | | length ! = tok - > mStr . length ( ) | | std : : strncmp ( current , tok - > mStr . c_str ( ) , length ) ! = 0 )
2008-12-22 00:28:09 +01:00
return false ;
2008-12-23 22:45:47 +01:00
current = next ;
2011-10-13 20:53:06 +02:00
if ( * next ) {
2012-12-27 11:51:12 +01:00
next = std : : strchr ( + + current , ' ' ) ;
2010-04-02 07:30:58 +02:00
if ( ! next )
2020-05-26 20:13:56 +02:00
next = end ;
2008-12-23 22:45:47 +01:00
}
tok = tok - > next ( ) ;
2008-12-22 00:28:09 +01:00
}
2008-12-23 22:45:47 +01:00
2008-12-22 00:28:09 +01:00
return true ;
}
2012-04-16 15:28:38 +02:00
bool Token : : firstWordEquals ( const char * str , const char * word )
2009-08-16 22:28:17 +02:00
{
2011-10-13 20:53:06 +02:00
for ( ; ; ) {
2023-06-20 18:43:21 +02:00
if ( * str ! = * word )
2012-04-26 16:39:16 +02:00
return ( * str = = ' ' & & * word = = 0 ) ;
2023-06-20 18:43:21 +02:00
if ( * str = = 0 )
2009-08-16 22:28:17 +02:00
break ;
+ + str ;
+ + word ;
}
2012-04-26 16:39:16 +02:00
return true ;
2009-08-16 22:28:17 +02:00
}
const char * Token : : chrInFirstWord ( const char * str , char c )
{
2011-10-13 20:53:06 +02:00
for ( ; ; ) {
2010-04-02 07:30:58 +02:00
if ( * str = = ' ' | | * str = = 0 )
2017-08-09 20:00:26 +02:00
return nullptr ;
2009-08-16 22:28:17 +02:00
2010-04-02 07:30:58 +02:00
if ( * str = = c )
2009-08-16 22:28:17 +02:00
return str ;
+ + str ;
}
}
2019-07-14 12:22:33 +02:00
bool Token : : Match ( const Token * tok , const char pattern [ ] , nonneg int varid )
2008-12-18 22:28:57 +01:00
{
2022-05-31 13:52:34 +02:00
if ( ! ( * pattern ) )
return true ;
2008-12-18 22:28:57 +01:00
const char * p = pattern ;
2022-05-31 13:52:34 +02:00
while ( true ) {
2008-12-18 22:28:57 +01:00
// Skip spaces in pattern..
2010-04-02 07:30:58 +02:00
while ( * p = = ' ' )
2009-01-01 23:22:28 +01:00
+ + p ;
2008-12-18 22:28:57 +01:00
// No token => Success!
2012-12-01 00:47:07 +01:00
if ( * p = = ' \0 ' )
break ;
2008-12-18 22:28:57 +01:00
2011-10-13 20:53:06 +02:00
if ( ! tok ) {
2009-01-10 01:33:48 +01:00
// If we have no tokens, pattern "!!else" should return true
2012-11-25 15:13:41 +01:00
if ( p [ 0 ] = = ' ! ' & & p [ 1 ] = = ' ! ' & & p [ 2 ] ! = ' \0 ' ) {
2010-09-20 20:15:07 +02:00
while ( * p & & * p ! = ' ' )
+ + p ;
2009-01-10 01:33:48 +01:00
continue ;
2022-05-31 13:52:34 +02:00
}
return false ;
2009-01-10 01:33:48 +01:00
}
2008-12-18 22:28:57 +01:00
// [.. => search for a one-character token..
2014-06-26 18:17:05 +02:00
if ( p [ 0 ] = = ' [ ' & & chrInFirstWord ( p , ' ] ' ) ) {
2012-11-20 00:16:53 +01:00
if ( tok - > str ( ) . length ( ) ! = 1 )
2011-07-15 19:01:36 +02:00
return false ;
2011-07-15 19:02:16 +02:00
2012-11-20 00:16:53 +01:00
const char * temp = p + 1 ;
2009-08-16 22:28:17 +02:00
bool chrFound = false ;
2019-07-13 16:06:24 +02:00
int count = 0 ;
2011-10-13 20:53:06 +02:00
while ( * temp & & * temp ! = ' ' ) {
if ( * temp = = ' ] ' ) {
2009-08-16 22:28:17 +02:00
+ + count ;
}
2012-11-20 00:16:53 +01:00
else if ( * temp = = tok - > str ( ) [ 0 ] ) {
2009-08-16 22:28:17 +02:00
chrFound = true ;
break ;
}
+ + temp ;
}
2012-11-20 00:16:53 +01:00
if ( count > 1 & & tok - > str ( ) [ 0 ] = = ' ] ' )
chrFound = true ;
2009-08-16 22:28:17 +02:00
2010-04-02 07:30:58 +02:00
if ( ! chrFound )
2008-12-18 22:28:57 +01:00
return false ;
2012-11-20 00:16:53 +01:00
p = temp ;
2008-12-18 22:28:57 +01:00
}
2014-06-26 18:17:05 +02:00
// Parse "not" options. Token can be anything except the given one
else if ( p [ 0 ] = = ' ! ' & & p [ 1 ] = = ' ! ' & & p [ 2 ] ! = ' \0 ' ) {
p + = 2 ;
if ( firstWordEquals ( p , tok - > str ( ) . c_str ( ) ) )
return false ;
}
2008-12-18 22:28:57 +01:00
// Parse multi options, such as void|int|char (accept token which is one of these 3)
2014-06-26 18:17:05 +02:00
else {
optimized `Token::Match()` a bit by always inlining `Token::multiCompare()` (#5332)
Scanning `mame_regtest` with `DISABLE_VALUEFLOW=1` and `--enable=all
--inconclusive`:
Clang 15 `1,170,770,173` -> `1,167,227,434`
GGC 12 `1,370,070,422` -> `1,366,775,852`
2023-08-18 20:41:50 +02:00
const int res = multiCompareImpl ( tok , p , varid ) ;
2011-10-13 20:53:06 +02:00
if ( res = = 0 ) {
2008-12-18 22:28:57 +01:00
// Empty alternative matches, use the same token on next round
2010-09-20 20:15:07 +02:00
while ( * p & & * p ! = ' ' )
+ + p ;
2008-12-18 22:28:57 +01:00
continue ;
2022-05-31 13:52:34 +02:00
}
if ( res = = - 1 ) {
2008-12-18 22:28:57 +01:00
// No match
return false ;
}
}
2022-05-31 13:52:34 +02:00
// using strchr() for the other instances leads to a performance decrease
if ( ! ( p = strchr ( p , ' ' ) ) )
break ;
2008-12-18 22:28:57 +01:00
tok = tok - > next ( ) ;
}
// The end of the pattern has been reached and nothing wrong has been found
return true ;
}
2019-07-14 12:22:33 +02:00
nonneg int Token : : getStrLength ( const Token * tok )
2009-08-30 13:07:10 +02:00
{
2014-02-15 08:05:54 +01:00
assert ( tok ! = nullptr ) ;
2018-06-16 16:40:02 +02:00
assert ( tok - > mTokType = = eString ) ;
2009-08-30 13:07:10 +02:00
2019-07-13 16:06:24 +02:00
int len = 0 ;
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2019-10-16 11:41:33 +02:00
const std : : string str ( getStringLiteral ( tok - > str ( ) ) ) ;
2022-12-20 20:32:16 +01:00
std : : string : : const_iterator it = str . cbegin ( ) ;
const std : : string : : const_iterator end = str . cend ( ) ;
2009-08-30 13:07:10 +02:00
2015-06-07 11:25:33 +02:00
while ( it ! = end ) {
if ( * it = = ' \\ ' ) {
+ + it ;
2009-09-26 17:58:14 +02:00
// string ends at '\0'
2015-06-07 11:25:33 +02:00
if ( * it = = ' 0 ' )
return len ;
2009-09-26 17:58:14 +02:00
}
2015-06-07 11:25:33 +02:00
if ( * it = = ' \0 ' )
return len ;
+ + it ;
2009-08-30 13:07:10 +02:00
+ + len ;
}
return len ;
}
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
nonneg int Token : : getStrArraySize ( const Token * tok )
2014-08-01 13:12:18 +02:00
{
2019-05-07 10:28:31 +02:00
assert ( tok ! = nullptr ) ;
assert ( tok - > tokType ( ) = = eString ) ;
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2019-10-16 11:41:33 +02:00
const std : : string str ( getStringLiteral ( tok - > str ( ) ) ) ;
2019-07-13 16:06:24 +02:00
int sizeofstring = 1 ;
2019-10-16 11:41:33 +02:00
for ( int i = 0 ; i < ( int ) str . size ( ) ; i + + ) {
2014-08-01 13:12:18 +02:00
if ( str [ i ] = = ' \\ ' )
+ + i ;
+ + sizeofstring ;
}
return sizeofstring ;
}
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
nonneg int Token : : getStrSize ( const Token * tok , const Settings * settings )
{
assert ( tok ! = nullptr & & tok - > tokType ( ) = = eString ) ;
nonneg int sizeofType = 1 ;
if ( tok - > valueType ( ) ) {
ValueType vt ( * tok - > valueType ( ) ) ;
vt . pointer = 0 ;
sizeofType = ValueFlow : : getSizeOf ( vt , settings ) ;
}
return getStrArraySize ( tok ) * sizeofType ;
}
2009-11-27 22:21:13 +01:00
void Token : : move ( Token * srcStart , Token * srcEnd , Token * newLocation )
{
/**[newLocation] -> b -> c -> [srcStart] -> [srcEnd] -> f */
// Fix the gap, which tokens to be moved will leave
srcStart - > previous ( ) - > next ( srcEnd - > next ( ) ) ;
srcEnd - > next ( ) - > previous ( srcStart - > previous ( ) ) ;
// Fix the tokens to be moved
srcEnd - > next ( newLocation - > next ( ) ) ;
srcStart - > previous ( newLocation ) ;
// Fix the tokens at newLocation
newLocation - > next ( ) - > previous ( srcEnd ) ;
newLocation - > next ( srcStart ) ;
2010-08-03 16:36:21 +02:00
// Update _progressValue
2012-01-21 21:05:41 +01:00
for ( Token * tok = srcStart ; tok ! = srcEnd - > next ( ) ; tok = tok - > next ( ) )
2018-12-21 13:51:45 +01:00
tok - > mImpl - > mProgressValue = newLocation - > mImpl - > mProgressValue ;
2009-11-27 22:21:13 +01:00
}
2023-12-06 14:15:35 +01:00
template < class T , REQUIRES ( " T must be a Token class " , std : : is_convertible < T * , const Token * > ) >
static T * nextArgumentImpl ( T * thisTok )
2011-10-23 11:23:48 +02:00
{
2023-12-06 14:15:35 +01:00
for ( T * tok = thisTok ; tok ; tok = tok - > next ( ) ) {
2011-10-23 11:23:48 +02:00
if ( tok - > str ( ) = = " , " )
2011-10-28 18:57:10 +02:00
return tok - > next ( ) ;
2023-06-20 18:43:21 +02:00
if ( tok - > link ( ) & & Token : : Match ( tok , " (|{|[|< " ) )
2012-04-16 15:28:38 +02:00
tok = tok - > link ( ) ;
2014-09-14 11:26:16 +02:00
else if ( Token : : Match ( tok , " ) | ; " ))
2015-12-06 12:50:05 +01:00
return nullptr ;
2011-10-23 11:23:48 +02:00
}
2015-12-06 12:50:05 +01:00
return nullptr ;
2011-10-23 11:23:48 +02:00
}
2023-12-06 14:15:35 +01:00
const Token * Token : : nextArgument ( ) const
{
return nextArgumentImpl ( this ) ;
}
Token * Token : : nextArgument ( )
{
return nextArgumentImpl ( this ) ;
}
2023-02-08 21:07:16 +01:00
const Token * Token : : nextArgumentBeforeCreateLinks2 ( ) const
2014-08-20 14:57:00 +02:00
{
for ( const Token * tok = this ; tok ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) = = " , " )
return tok - > next ( ) ;
2023-06-20 18:43:21 +02:00
if ( tok - > link ( ) & & Token : : Match ( tok , " (|{|[ " ) )
2014-08-20 14:57:00 +02:00
tok = tok - > link ( ) ;
else if ( tok - > str ( ) = = " < " ) {
const Token * temp = tok - > findClosingBracket ( ) ;
if ( temp )
tok = temp ;
2014-09-14 11:26:16 +02:00
} else if ( Token : : Match ( tok , " ) | ; " ))
2015-12-06 12:50:05 +01:00
return nullptr ;
2014-08-20 14:57:00 +02:00
}
2015-12-06 12:50:05 +01:00
return nullptr ;
2014-08-20 14:57:00 +02:00
}
2023-02-08 21:07:16 +01:00
const Token * Token : : nextTemplateArgument ( ) const
2015-01-03 20:35:33 +01:00
{
for ( const Token * tok = this ; tok ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) = = " , " )
return tok - > next ( ) ;
2023-06-20 18:43:21 +02:00
if ( tok - > link ( ) & & Token : : Match ( tok , " (|{|[|< " ) )
2015-01-03 20:35:33 +01:00
tok = tok - > link ( ) ;
else if ( Token : : Match ( tok , " >|; " ) )
2015-12-06 12:50:05 +01:00
return nullptr ;
2015-01-03 20:35:33 +01:00
}
2015-12-06 12:50:05 +01:00
return nullptr ;
2015-01-03 20:35:33 +01:00
}
2019-10-03 12:26:45 +02:00
static bool isOperator ( const Token * tok )
{
if ( tok - > link ( ) )
tok = tok - > link ( ) ;
// TODO handle multi token operators
return tok - > strAt ( - 1 ) = = " operator " ;
}
2013-07-31 10:30:20 +02:00
const Token * Token : : findClosingBracket ( ) const
2012-04-18 16:02:03 +02:00
{
2018-06-16 23:03:15 +02:00
if ( mStr ! = " < " )
2018-01-01 12:22:04 +01:00
return nullptr ;
2020-11-30 19:26:15 +01:00
if ( ! mPrevious )
return nullptr ;
2023-10-20 15:32:46 +02:00
if ( ! ( mPrevious - > isName ( ) | | Token : : simpleMatch ( mPrevious , " ] " ) | |
2020-11-30 19:26:15 +01:00
Token : : Match ( mPrevious - > previous ( ) , " operator %op% < " ) | |
Token : : Match ( mPrevious - > tokAt ( - 2 ) , " operator [([] [)]] < " ) ) )
return nullptr ;
2014-02-16 11:47:52 +01:00
const Token * closing = nullptr ;
2019-08-04 10:24:44 +02:00
const bool templateParameter ( strAt ( - 1 ) = = " template " ) ;
std : : set < std : : string > templateParameters ;
2013-07-31 10:30:20 +02:00
2020-11-29 12:56:13 +01:00
bool isDecl = true ;
for ( const Token * prev = previous ( ) ; prev ; prev = prev - > previous ( ) ) {
if ( prev - > str ( ) = = " = " )
isDecl = false ;
if ( Token : : simpleMatch ( prev , " template < " ) )
isDecl = true ;
if ( Token : : Match ( prev , " [;{}] " ) )
break ;
}
2018-01-01 12:22:04 +01:00
unsigned int depth = 0 ;
for ( closing = this ; closing ! = nullptr ; closing = closing - > next ( ) ) {
if ( Token : : Match ( closing , " {|[|( " ) ) {
closing = closing - > link ( ) ;
if ( ! closing )
return nullptr ; // #6803
} else if ( Token : : Match ( closing , " }|]|) | ; " ))
return nullptr ;
2019-08-04 10:24:44 +02:00
// we can make some guesses for template parameters
2019-10-03 12:26:45 +02:00
else if ( closing - > str ( ) = = " < " & & closing - > previous ( ) & &
2023-10-20 15:32:46 +02:00
( closing - > previous ( ) - > isName ( ) | | Token : : simpleMatch ( closing - > previous ( ) , " ] " ) | | isOperator ( closing - > previous ( ) ) ) & &
2019-09-20 12:35:01 +02:00
( templateParameter ? templateParameters . find ( closing - > strAt ( - 1 ) ) = = templateParameters . end ( ) : true ) )
2018-01-01 12:22:04 +01:00
+ + depth ;
else if ( closing - > str ( ) = = " > " ) {
if ( - - depth = = 0 )
return closing ;
2020-08-25 07:14:44 +02:00
} else if ( closing - > str ( ) = = " >> " | | closing - > str ( ) = = " >>= " ) {
2020-11-29 12:56:13 +01:00
if ( ! isDecl & & depth = = 1 )
continue ;
2018-01-01 12:22:04 +01:00
if ( depth < = 2 )
return closing ;
depth - = 2 ;
2012-04-18 16:02:03 +02:00
}
2019-08-04 10:24:44 +02:00
// save named template parameter
else if ( templateParameter & & depth = = 1 & & closing - > str ( ) = = " , " & &
closing - > previous ( ) - > isName ( ) & & ! Match ( closing - > previous ( ) , " class|typename|. " ) )
templateParameters . insert ( closing - > strAt ( - 1 ) ) ;
2012-04-18 16:02:03 +02:00
}
2013-07-31 10:30:20 +02:00
return closing ;
}
Token * Token : : findClosingBracket ( )
{
// return value of const function
return const_cast < Token * > ( const_cast < const Token * > ( this ) - > findClosingBracket ( ) ) ;
2012-04-18 16:02:03 +02:00
}
2018-10-14 16:57:07 +02:00
const Token * Token : : findOpeningBracket ( ) const
{
if ( mStr ! = " > " )
return nullptr ;
const Token * opening = nullptr ;
unsigned int depth = 0 ;
for ( opening = this ; opening ! = nullptr ; opening = opening - > previous ( ) ) {
if ( Token : : Match ( opening , " }|]|) " ) ) {
opening = opening - > link ( ) ;
if ( ! opening )
return nullptr ;
} else if ( Token : : Match ( opening , " {|{|(|; " ) )
return nullptr ;
else if ( opening - > str ( ) = = " > " )
+ + depth ;
else if ( opening - > str ( ) = = " < " ) {
if ( - - depth = = 0 )
return opening ;
}
}
return opening ;
}
Token * Token : : findOpeningBracket ( )
{
// return value of const function
return const_cast < Token * > ( const_cast < const Token * > ( this ) - > findOpeningBracket ( ) ) ;
}
2008-12-18 22:28:57 +01:00
//---------------------------------------------------------------------------
2023-12-06 14:15:35 +01:00
template < class T , REQUIRES ( " T must be a Token class " , std : : is_convertible < T * , const Token * > ) >
static T * findsimplematchImpl ( T * const startTok , const char pattern [ ] , size_t pattern_len )
2011-10-27 10:54:50 +02:00
{
2023-12-06 14:15:35 +01:00
for ( T * tok = startTok ; tok ; tok = tok - > next ( ) ) {
2020-05-26 20:13:56 +02:00
if ( Token : : simpleMatch ( tok , pattern , pattern_len ) )
2011-10-27 10:54:50 +02:00
return tok ;
}
2017-08-09 20:00:26 +02:00
return nullptr ;
2011-10-27 10:54:50 +02:00
}
2023-12-06 14:15:35 +01:00
const Token * Token : : findsimplematch ( const Token * const startTok , const char pattern [ ] , size_t pattern_len )
{
return findsimplematchImpl ( startTok , pattern , pattern_len ) ;
}
Token * Token : : findsimplematch ( Token * const startTok , const char pattern [ ] , size_t pattern_len )
{
return findsimplematchImpl ( startTok , pattern , pattern_len ) ;
}
template < class T , REQUIRES ( " T must be a Token class " , std : : is_convertible < T * , const Token * > ) >
static T * findsimplematchImpl ( T * const startTok , const char pattern [ ] , size_t pattern_len , const Token * const end )
2011-10-27 10:54:50 +02:00
{
2023-12-06 14:15:35 +01:00
for ( T * tok = startTok ; tok & & tok ! = end ; tok = tok - > next ( ) ) {
2020-05-26 20:13:56 +02:00
if ( Token : : simpleMatch ( tok , pattern , pattern_len ) )
2011-10-27 10:54:50 +02:00
return tok ;
}
2015-12-06 12:50:05 +01:00
return nullptr ;
2011-10-27 10:54:50 +02:00
}
2023-12-06 14:15:35 +01:00
const Token * Token : : findsimplematch ( const Token * const startTok , const char pattern [ ] , size_t pattern_len , const Token * const end )
{
return findsimplematchImpl ( startTok , pattern , pattern_len , end ) ;
}
Token * Token : : findsimplematch ( Token * const startTok , const char pattern [ ] , size_t pattern_len , const Token * const end ) {
return findsimplematchImpl ( startTok , pattern , pattern_len , end ) ;
}
template < class T , REQUIRES ( " T must be a Token class " , std : : is_convertible < T * , const Token * > ) >
static T * findmatchImpl ( T * const startTok , const char pattern [ ] , const nonneg int varId )
2009-01-04 20:55:12 +01:00
{
2023-12-06 14:15:35 +01:00
for ( T * tok = startTok ; tok ; tok = tok - > next ( ) ) {
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tok , pattern , varId ) )
2009-01-04 20:55:12 +01:00
return tok ;
}
2015-12-06 12:50:05 +01:00
return nullptr ;
2009-01-04 20:55:12 +01:00
}
2023-12-06 14:15:35 +01:00
const Token * Token : : findmatch ( const Token * const startTok , const char pattern [ ] , const nonneg int varId )
{
return findmatchImpl ( startTok , pattern , varId ) ;
}
Token * Token : : findmatch ( Token * const startTok , const char pattern [ ] , const nonneg int varId ) {
return findmatchImpl ( startTok , pattern , varId ) ;
}
template < class T , REQUIRES ( " T must be a Token class " , std : : is_convertible < T * , const Token * > ) >
static T * findmatchImpl ( T * const startTok , const char pattern [ ] , const Token * const end , const nonneg int varId )
2010-07-26 16:46:37 +02:00
{
2023-12-06 14:15:35 +01:00
for ( T * tok = startTok ; tok & & tok ! = end ; tok = tok - > next ( ) ) {
2010-07-26 16:46:37 +02:00
if ( Token : : Match ( tok , pattern , varId ) )
return tok ;
}
2015-12-06 12:50:05 +01:00
return nullptr ;
2010-07-26 16:46:37 +02:00
}
2023-12-06 14:15:35 +01:00
const Token * Token : : findmatch ( const Token * const startTok , const char pattern [ ] , const Token * const end , const nonneg int varId )
{
return findmatchImpl ( startTok , pattern , end , varId ) ;
}
Token * Token : : findmatch ( Token * const startTok , const char pattern [ ] , const Token * const end , const nonneg int varId ) {
return findmatchImpl ( startTok , pattern , end , varId ) ;
}
2019-07-05 14:00:59 +02:00
void Token : : function ( const Function * f )
{
2019-07-05 12:30:42 +02:00
mImpl - > mFunction = f ;
if ( f ) {
if ( f - > isLambda ( ) )
tokType ( eLambda ) ;
else
tokType ( eFunction ) ;
} else if ( mTokType = = eFunction )
tokType ( eName ) ;
}
2021-12-05 15:46:17 +01:00
Token * Token : : insertToken ( const std : : string & tokenStr , const std : : string & originalNameStr , bool prepend )
2013-09-24 06:43:03 +02:00
{
2014-07-05 12:10:23 +02:00
Token * newToken ;
2018-06-16 23:03:15 +02:00
if ( mStr . empty ( ) )
2013-09-24 06:43:03 +02:00
newToken = this ;
else
2018-06-16 16:22:35 +02:00
newToken = new Token ( mTokensFrontBack ) ;
2013-09-24 06:43:03 +02:00
newToken - > str ( tokenStr ) ;
2014-06-26 10:57:39 +02:00
if ( ! originalNameStr . empty ( ) )
newToken - > originalName ( originalNameStr ) ;
2013-09-24 06:43:03 +02:00
2012-02-13 17:44:08 +01:00
if ( newToken ! = this ) {
2018-12-21 13:51:45 +01:00
newToken - > mImpl - > mLineNumber = mImpl - > mLineNumber ;
newToken - > mImpl - > mFileIndex = mImpl - > mFileIndex ;
newToken - > mImpl - > mProgressValue = mImpl - > mProgressValue ;
2018-04-28 17:17:40 +02:00
2012-11-20 02:58:19 +01:00
if ( prepend ) {
2018-11-07 21:25:42 +01:00
if ( this - > previous ( ) ) {
2012-11-20 02:58:19 +01:00
newToken - > previous ( this - > previous ( ) ) ;
newToken - > previous ( ) - > next ( newToken ) ;
2018-11-07 21:25:42 +01:00
} else if ( mTokensFrontBack ) {
mTokensFrontBack - > front = newToken ;
}
2012-11-20 02:58:19 +01:00
this - > previous ( newToken ) ;
newToken - > next ( this ) ;
} else {
if ( this - > next ( ) ) {
newToken - > next ( this - > next ( ) ) ;
newToken - > next ( ) - > previous ( newToken ) ;
2018-06-16 16:22:35 +02:00
} else if ( mTokensFrontBack ) {
mTokensFrontBack - > back = newToken ;
2012-11-20 02:58:19 +01:00
}
this - > next ( newToken ) ;
newToken - > previous ( this ) ;
2012-02-13 17:44:08 +01:00
}
2019-07-31 09:19:27 +02:00
if ( mImpl - > mScopeInfo ) {
// If the brace is immediately closed there is no point opening a new scope for it
2020-05-17 17:25:33 +02:00
if ( newToken - > str ( ) = = " { " ) {
2019-09-25 15:25:19 +02:00
std : : string nextScopeNameAddition ;
2019-07-31 09:19:27 +02:00
// This might be the opening of a member function
Token * tok1 = newToken ;
while ( Token : : Match ( tok1 - > previous ( ) , " const|volatile|final|override|&|&&|noexcept " ) )
tok1 = tok1 - > previous ( ) ;
2020-05-20 18:03:03 +02:00
if ( tok1 - > previous ( ) & & tok1 - > strAt ( - 1 ) = = " ) " ) {
2019-07-31 09:19:27 +02:00
tok1 = tok1 - > linkAt ( - 1 ) ;
if ( Token : : Match ( tok1 - > previous ( ) , " throw|noexcept " ) ) {
tok1 = tok1 - > previous ( ) ;
while ( Token : : Match ( tok1 - > previous ( ) , " const|volatile|final|override|&|&&|noexcept " ) )
tok1 = tok1 - > previous ( ) ;
if ( tok1 - > strAt ( - 1 ) ! = " ) " )
2021-12-05 15:46:17 +01:00
return newToken ;
2019-07-31 09:19:27 +02:00
} else if ( Token : : Match ( newToken - > tokAt ( - 2 ) , " :|, %name% " ) ) {
tok1 = tok1 - > tokAt ( - 2 ) ;
if ( tok1 - > strAt ( - 1 ) ! = " ) " )
2021-12-05 15:46:17 +01:00
return newToken ;
2019-07-31 09:19:27 +02:00
}
if ( tok1 - > strAt ( - 1 ) = = " > " )
tok1 = tok1 - > previous ( ) - > findOpeningBracket ( ) ;
if ( tok1 & & Token : : Match ( tok1 - > tokAt ( - 3 ) , " %name% :: %name% " ) ) {
tok1 = tok1 - > tokAt ( - 2 ) ;
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2019-07-31 09:19:27 +02:00
std : : string scope = tok1 - > strAt ( - 1 ) ;
while ( Token : : Match ( tok1 - > tokAt ( - 2 ) , " :: %name% " ) ) {
scope = tok1 - > strAt ( - 3 ) + " :: " + scope ;
tok1 = tok1 - > tokAt ( - 2 ) ;
}
nextScopeNameAddition + = scope ;
}
}
// Or it might be a namespace/class/struct
if ( Token : : Match ( newToken - > previous ( ) , " %name%|> " ) ) {
Token * nameTok = newToken - > previous ( ) ;
while ( nameTok & & ! Token : : Match ( nameTok , " namespace|class|struct|union %name% {|::|:|< " ) ) {
nameTok = nameTok - > previous ( ) ;
}
if ( nameTok ) {
for ( nameTok = nameTok - > next ( ) ; nameTok & & ! Token : : Match ( nameTok , " {|:|< " ) ; nameTok = nameTok - > next ( ) ) {
nextScopeNameAddition . append ( nameTok - > str ( ) ) ;
nextScopeNameAddition . append ( " " ) ;
}
2022-07-11 23:07:37 +02:00
if ( ! nextScopeNameAddition . empty ( ) )
nextScopeNameAddition . pop_back ( ) ;
2019-07-31 09:19:27 +02:00
}
}
// New scope is opening, record it here
std : : shared_ptr < ScopeInfo2 > newScopeInfo = std : : make_shared < ScopeInfo2 > ( mImpl - > mScopeInfo - > name , nullptr , mImpl - > mScopeInfo - > usingNamespaces ) ;
if ( ! newScopeInfo - > name . empty ( ) & & ! nextScopeNameAddition . empty ( ) ) newScopeInfo - > name . append ( " :: " ) ;
newScopeInfo - > name . append ( nextScopeNameAddition ) ;
2019-08-02 19:55:08 +02:00
nextScopeNameAddition = " " ;
2019-07-31 09:19:27 +02:00
newToken - > scopeInfo ( newScopeInfo ) ;
2020-05-17 17:25:33 +02:00
} else if ( newToken - > str ( ) = = " } " ) {
2019-07-31 09:19:27 +02:00
Token * matchingTok = newToken - > previous ( ) ;
int depth = 0 ;
while ( matchingTok & & ( depth ! = 0 | | ! Token : : simpleMatch ( matchingTok , " { " ) ) ) {
if ( Token : : simpleMatch ( matchingTok , " } " ) ) depth + + ;
if ( Token : : simpleMatch ( matchingTok , " { " ) ) depth - - ;
matchingTok = matchingTok - > previous ( ) ;
}
if ( matchingTok & & matchingTok - > previous ( ) ) {
newToken - > mImpl - > mScopeInfo = matchingTok - > previous ( ) - > scopeInfo ( ) ;
}
} else {
if ( prepend & & newToken - > previous ( ) ) {
newToken - > mImpl - > mScopeInfo = newToken - > previous ( ) - > scopeInfo ( ) ;
2019-07-31 11:11:01 +02:00
} else {
2019-07-31 09:19:27 +02:00
newToken - > mImpl - > mScopeInfo = mImpl - > mScopeInfo ;
}
2020-05-17 17:25:33 +02:00
if ( newToken - > str ( ) = = " ; " ) {
2019-07-31 09:19:27 +02:00
const Token * statementStart ;
for ( statementStart = newToken ; statementStart - > previous ( ) & & ! Token : : Match ( statementStart - > previous ( ) , " ;|{ " ) ; statementStart = statementStart - > previous ( ) ) ;
if ( Token : : Match ( statementStart , " using namespace %name% ::|; " ) ) {
const Token * tok1 = statementStart - > tokAt ( 2 ) ;
std : : string nameSpace ;
while ( tok1 & & tok1 - > str ( ) ! = " ; " ) {
if ( ! nameSpace . empty ( ) )
nameSpace + = " " ;
nameSpace + = tok1 - > str ( ) ;
tok1 = tok1 - > next ( ) ;
}
mImpl - > mScopeInfo - > usingNamespaces . insert ( nameSpace ) ;
}
}
}
}
2012-02-13 17:44:08 +01:00
}
2021-12-05 15:46:17 +01:00
return newToken ;
2008-12-18 22:28:57 +01:00
}
2009-01-05 16:49:57 +01:00
void Token : : eraseTokens ( Token * begin , const Token * end )
2008-12-18 22:28:57 +01:00
{
2012-02-13 17:44:08 +01:00
if ( ! begin | | begin = = end )
2008-12-18 22:28:57 +01:00
return ;
2011-10-13 20:53:06 +02:00
while ( begin - > next ( ) & & begin - > next ( ) ! = end ) {
2008-12-18 22:28:57 +01:00
begin - > deleteNext ( ) ;
}
}
2009-08-22 16:22:50 +02:00
void Token : : createMutualLinks ( Token * begin , Token * end )
{
2014-02-15 08:05:54 +01:00
assert ( begin ! = nullptr ) ;
assert ( end ! = nullptr ) ;
2009-08-22 16:22:50 +02:00
assert ( begin ! = end ) ;
begin - > link ( end ) ;
end - > link ( begin ) ;
}
2009-01-05 16:49:57 +01:00
void Token : : printOut ( const char * title ) const
2008-12-18 22:28:57 +01:00
{
2013-08-18 09:01:03 +02:00
if ( title & & title [ 0 ] )
2012-04-16 19:51:07 +02:00
std : : cout < < " \n ### " < < title < < " ### \n " ;
2020-11-28 15:41:07 +01:00
std : : cout < < stringifyList ( stringifyOptions : : forPrintOut ( ) , nullptr , nullptr ) < < std : : endl ;
2009-11-27 23:04:04 +01:00
}
void Token : : printOut ( const char * title , const std : : vector < std : : string > & fileNames ) const
{
2013-08-18 09:01:03 +02:00
if ( title & & title [ 0 ] )
2012-04-16 19:51:07 +02:00
std : : cout < < " \n ### " < < title < < " ### \n " ;
2020-11-28 15:41:07 +01:00
std : : cout < < stringifyList ( stringifyOptions : : forPrintOut ( ) , & fileNames , nullptr ) < < std : : endl ;
2008-12-18 22:28:57 +01:00
}
2009-02-13 07:25:29 +01:00
2022-01-18 22:02:25 +01:00
// cppcheck-suppress unusedFunction - used for debugging
2020-05-23 11:30:54 +02:00
void Token : : printLines ( int lines ) const
{
const Token * end = this ;
while ( end & & end - > linenr ( ) < lines + linenr ( ) )
end = end - > next ( ) ;
2020-08-20 18:21:29 +02:00
std : : cout < < stringifyList ( stringifyOptions : : forDebugExprId ( ) , nullptr , end ) < < std : : endl ;
2020-05-23 11:30:54 +02:00
}
2021-02-21 17:21:33 +01:00
std : : string Token : : stringify ( const stringifyOptions & options ) const
2011-12-13 21:42:38 +01:00
{
2021-02-21 17:21:33 +01:00
std : : string ret ;
2020-08-20 18:21:29 +02:00
if ( options . attributes ) {
2012-04-16 19:51:07 +02:00
if ( isUnsigned ( ) )
2021-02-21 17:21:33 +01:00
ret + = " unsigned " ;
2012-04-16 19:51:07 +02:00
else if ( isSigned ( ) )
2021-02-21 17:21:33 +01:00
ret + = " signed " ;
2015-08-27 14:34:00 +02:00
if ( isComplex ( ) )
2021-02-21 17:21:33 +01:00
ret + = " _Complex " ;
2013-08-31 06:26:39 +02:00
if ( isLong ( ) ) {
2019-10-16 11:41:33 +02:00
if ( ! ( mTokType = = eString | | mTokType = = eChar ) )
2021-02-21 17:21:33 +01:00
ret + = " long " ;
2013-08-31 06:26:39 +02:00
}
2011-12-13 21:42:38 +01:00
}
2020-08-20 18:21:29 +02:00
if ( options . macro & & isExpandedMacro ( ) )
2021-02-21 17:21:33 +01:00
ret + = ' $ ' ;
2018-06-16 23:03:15 +02:00
if ( isName ( ) & & mStr . find ( ' ' ) ! = std : : string : : npos ) {
2022-10-02 07:12:40 +02:00
for ( const char i : mStr ) {
2019-09-19 20:29:33 +02:00
if ( i ! = ' ' )
2021-02-21 17:21:33 +01:00
ret + = i ;
2017-12-25 23:16:51 +01:00
}
2018-06-16 23:03:15 +02:00
} else if ( mStr [ 0 ] ! = ' \" ' | | mStr . find ( ' \0 ' ) = = std : : string : : npos )
2021-02-21 17:21:33 +01:00
ret + = mStr ;
2012-12-25 08:56:12 +01:00
else {
2022-10-02 07:12:40 +02:00
for ( const char i : mStr ) {
2019-09-19 20:29:33 +02:00
if ( i = = ' \0 ' )
2021-02-21 17:21:33 +01:00
ret + = " \\ 0 " ;
2012-12-25 08:56:12 +01:00
else
2021-02-21 17:21:33 +01:00
ret + = i ;
2012-12-25 08:56:12 +01:00
}
}
2021-01-16 19:03:28 +01:00
if ( options . varid & & mImpl - > mVarId ! = 0 ) {
2021-02-21 17:21:33 +01:00
ret + = ' @ ' ;
ret + = ( options . idtype ? " var " : " " ) ;
ret + = std : : to_string ( mImpl - > mVarId ) ;
2021-01-16 19:03:28 +01:00
} else if ( options . exprid & & mImpl - > mExprId ! = 0 ) {
2021-02-21 17:21:33 +01:00
ret + = ' @ ' ;
ret + = ( options . idtype ? " expr " : " " ) ;
2023-12-05 14:22:32 +01:00
if ( ( mImpl - > mExprId & ( 1U < < efIsUnique ) ) ! = 0 )
ret + = " UNIQUE " ;
else
ret + = std : : to_string ( mImpl - > mExprId ) ;
2021-01-16 19:03:28 +01:00
}
2021-02-21 17:21:33 +01:00
return ret ;
2011-12-13 21:42:38 +01:00
}
2021-02-21 17:21:33 +01:00
std : : string Token : : stringify ( bool varid , bool attributes , bool macro ) const
2020-08-20 18:21:29 +02:00
{
stringifyOptions options ;
options . varid = varid ;
options . attributes = attributes ;
options . macro = macro ;
2021-02-21 17:21:33 +01:00
return stringify ( options ) ;
2020-08-20 18:21:29 +02:00
}
std : : string Token : : stringifyList ( const stringifyOptions & options , const std : : vector < std : : string > * fileNames , const Token * end ) const
2009-11-27 23:04:04 +01:00
{
2012-04-16 19:51:07 +02:00
if ( this = = end )
return " " ;
2009-11-27 23:04:04 +01:00
2021-01-16 19:03:28 +01:00
std : : string ret ;
2009-03-03 20:45:58 +01:00
2020-08-20 18:21:29 +02:00
unsigned int lineNumber = mImpl - > mLineNumber - ( options . linenumbers ? 1U : 0U ) ;
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2020-08-20 18:21:29 +02:00
unsigned int fileIndex = options . files ? ~ 0U : mImpl - > mFileIndex ;
2010-08-06 21:02:43 +02:00
std : : map < int , unsigned int > lineNumbers ;
2012-04-16 19:51:07 +02:00
for ( const Token * tok = this ; tok ! = end ; tok = tok - > next ( ) ) {
2022-01-16 12:34:20 +01:00
assert ( tok & & " end precedes token " ) ;
if ( ! tok )
return ret ;
2009-03-03 21:17:23 +01:00
bool fileChange = false ;
2019-07-17 16:28:47 +02:00
if ( tok - > mImpl - > mFileIndex ! = fileIndex ) {
if ( fileIndex ! = ~ 0U ) {
lineNumbers [ fileIndex ] = tok - > mImpl - > mFileIndex ;
2009-03-03 21:17:23 +01:00
}
2019-07-17 16:28:47 +02:00
fileIndex = tok - > mImpl - > mFileIndex ;
2020-08-20 18:21:29 +02:00
if ( options . files ) {
2021-01-16 19:03:28 +01:00
ret + = " \n \n ##file " ;
2018-12-21 13:51:45 +01:00
if ( fileNames & & fileNames - > size ( ) > tok - > mImpl - > mFileIndex )
2021-01-16 19:03:28 +01:00
ret + = fileNames - > at ( tok - > mImpl - > mFileIndex ) ;
2012-04-16 19:51:07 +02:00
else
2021-01-16 19:03:28 +01:00
ret + = std : : to_string ( fileIndex ) ;
ret + = ' \n ' ;
2012-04-16 19:51:07 +02:00
}
2009-03-03 21:17:23 +01:00
2019-07-17 16:28:47 +02:00
lineNumber = lineNumbers [ fileIndex ] ;
2009-03-03 21:17:23 +01:00
fileChange = true ;
}
2020-08-20 18:21:29 +02:00
if ( options . linebreaks & & ( lineNumber ! = tok - > linenr ( ) | | fileChange ) ) {
2019-07-17 16:28:47 +02:00
if ( lineNumber + 4 < tok - > linenr ( ) & & fileIndex = = tok - > mImpl - > mFileIndex ) {
2021-01-16 19:03:28 +01:00
ret + = ' \n ' ;
ret + = std : : to_string ( lineNumber + 1 ) ;
ret + = " : \n | \n " ;
ret + = std : : to_string ( tok - > linenr ( ) - 1 ) ;
ret + = " : \n " ;
ret + = std : : to_string ( tok - > linenr ( ) ) ;
ret + = " : " ;
2020-08-20 18:21:29 +02:00
} else if ( this = = tok & & options . linenumbers ) {
2021-01-16 19:03:28 +01:00
ret + = std : : to_string ( tok - > linenr ( ) ) ;
ret + = " : " ;
2020-12-08 10:35:13 +01:00
} else if ( lineNumber > tok - > linenr ( ) ) {
lineNumber = tok - > linenr ( ) ;
2021-01-16 19:03:28 +01:00
ret + = ' \n ' ;
2020-12-08 10:35:13 +01:00
if ( options . linenumbers ) {
2021-01-16 19:03:28 +01:00
ret + = std : : to_string ( lineNumber ) ;
ret + = ' : ' ;
2021-07-30 21:29:35 +02:00
ret + = ' ' ;
2020-12-08 10:35:13 +01:00
}
2012-08-02 20:36:54 +02:00
} else {
while ( lineNumber < tok - > linenr ( ) ) {
+ + lineNumber ;
2021-01-16 19:03:28 +01:00
ret + = ' \n ' ;
2020-08-20 18:21:29 +02:00
if ( options . linenumbers ) {
2021-01-16 19:03:28 +01:00
ret + = std : : to_string ( lineNumber ) ;
ret + = ' : ' ;
2012-08-02 20:36:54 +02:00
if ( lineNumber = = tok - > linenr ( ) )
2021-01-16 19:03:28 +01:00
ret + = ' ' ;
2012-08-02 20:36:54 +02:00
}
2012-04-16 19:51:07 +02:00
}
2009-03-03 21:17:23 +01:00
}
2010-04-09 21:40:37 +02:00
lineNumber = tok - > linenr ( ) ;
2009-02-13 07:25:29 +01:00
}
2009-03-03 21:17:23 +01:00
2021-02-21 17:21:33 +01:00
ret + = tok - > stringify ( options ) ; // print token
2020-12-08 10:35:13 +01:00
if ( tok - > next ( ) ! = end & & ( ! options . linebreaks | | ( tok - > next ( ) - > linenr ( ) = = tok - > linenr ( ) & & tok - > next ( ) - > fileIndex ( ) = = tok - > fileIndex ( ) ) ) )
2021-01-16 19:03:28 +01:00
ret + = ' ' ;
2009-02-13 07:25:29 +01:00
}
2020-08-20 18:21:29 +02:00
if ( options . linebreaks & & ( options . files | | options . linenumbers ) )
2021-01-16 19:03:28 +01:00
ret + = ' \n ' ;
return ret ;
2009-02-13 07:25:29 +01:00
}
2020-08-20 18:21:29 +02:00
std : : string Token : : stringifyList ( bool varid , bool attributes , bool linenumbers , bool linebreaks , bool files , const std : : vector < std : : string > * fileNames , const Token * end ) const
{
stringifyOptions options ;
options . varid = varid ;
options . attributes = attributes ;
options . macro = attributes ;
options . linenumbers = linenumbers ;
options . linebreaks = linebreaks ;
options . files = files ;
return stringifyList ( options , fileNames , end ) ;
}
2009-02-13 07:25:29 +01:00
2012-04-16 19:51:07 +02:00
std : : string Token : : stringifyList ( const Token * end , bool attributes ) const
{
2017-08-09 20:00:26 +02:00
return stringifyList ( false , attributes , false , false , false , nullptr , end ) ;
2012-04-16 19:51:07 +02:00
}
std : : string Token : : stringifyList ( bool varid ) const
{
2017-08-09 20:00:26 +02:00
return stringifyList ( varid , false , true , true , true , nullptr , nullptr ) ;
2012-04-16 19:51:07 +02:00
}
2012-12-15 20:21:09 +01:00
2021-07-08 21:13:51 +02:00
void Token : : astParent ( Token * tok )
2021-07-07 08:21:35 +02:00
{
2021-07-08 21:13:51 +02:00
const Token * tok2 = tok ;
while ( tok2 ) {
if ( this = = tok2 )
throw InternalError ( this , " Internal error. AST cyclic dependency. " ) ;
tok2 = tok2 - > astParent ( ) ;
2021-07-07 08:21:35 +02:00
}
2021-07-08 21:13:51 +02:00
// Clear children to avoid nodes referenced twice
if ( this - > astParent ( ) ) {
Token * parent = this - > astParent ( ) ;
if ( parent - > astOperand1 ( ) = = this )
parent - > mImpl - > mAstOperand1 = nullptr ;
if ( parent - > astOperand2 ( ) = = this )
parent - > mImpl - > mAstOperand2 = nullptr ;
}
mImpl - > mAstParent = tok ;
2021-07-07 08:21:35 +02:00
}
2012-12-15 20:21:09 +01:00
void Token : : astOperand1 ( Token * tok )
{
2018-12-21 13:51:45 +01:00
if ( mImpl - > mAstOperand1 )
2021-07-08 21:13:51 +02:00
mImpl - > mAstOperand1 - > astParent ( nullptr ) ;
2012-12-16 11:48:19 +01:00
// goto parent operator
2014-05-19 21:54:59 +02:00
if ( tok ) {
2021-07-08 21:13:51 +02:00
tok = tok - > astTop ( ) ;
tok - > astParent ( this ) ;
2014-05-19 21:54:59 +02:00
}
2018-12-21 13:51:45 +01:00
mImpl - > mAstOperand1 = tok ;
2012-12-15 20:21:09 +01:00
}
void Token : : astOperand2 ( Token * tok )
{
2018-12-21 13:51:45 +01:00
if ( mImpl - > mAstOperand2 )
2021-07-08 21:13:51 +02:00
mImpl - > mAstOperand2 - > astParent ( nullptr ) ;
2012-12-16 11:48:19 +01:00
// goto parent operator
2014-05-19 21:54:59 +02:00
if ( tok ) {
2021-07-08 21:13:51 +02:00
tok = tok - > astTop ( ) ;
tok - > astParent ( this ) ;
2014-05-19 21:54:59 +02:00
}
2018-12-21 13:51:45 +01:00
mImpl - > mAstOperand2 = tok ;
2012-12-15 20:21:09 +01:00
}
2018-10-18 12:09:55 +02:00
static const Token * goToLeftParenthesis ( const Token * start , const Token * end )
{
// move start to lpar in such expression: '(*it).x'
int par = 0 ;
for ( const Token * tok = start ; tok & & tok ! = end ; tok = tok - > next ( ) ) {
if ( tok - > str ( ) = = " ( " )
+ + par ;
else if ( tok - > str ( ) = = " ) " ) {
if ( par = = 0 )
start = tok - > link ( ) ;
else
- - par ;
}
}
return start ;
}
static const Token * goToRightParenthesis ( const Token * start , const Token * end )
{
// move end to rpar in such expression: '2>(x+1)'
int par = 0 ;
for ( const Token * tok = end ; tok & & tok ! = start ; tok = tok - > previous ( ) ) {
if ( tok - > str ( ) = = " ) " )
+ + par ;
else if ( tok - > str ( ) = = " ( " ) {
if ( par = = 0 )
end = tok - > link ( ) ;
else
- - par ;
}
}
return end ;
}
std : : pair < const Token * , const Token * > Token : : findExpressionStartEndTokens ( ) const
{
const Token * const top = this ;
2019-01-20 13:20:23 +01:00
// find start node in AST tree
2018-10-18 12:09:55 +02:00
const Token * start = top ;
2020-10-25 20:32:45 +01:00
while ( start - > astOperand1 ( ) & & precedes ( start - > astOperand1 ( ) , start ) )
2018-10-18 12:09:55 +02:00
start = start - > astOperand1 ( ) ;
2019-01-20 13:20:23 +01:00
// find end node in AST tree
2018-10-18 12:09:55 +02:00
const Token * end = top ;
while ( end - > astOperand1 ( ) & & ( end - > astOperand2 ( ) | | end - > isUnaryPreOp ( ) ) ) {
2019-01-20 13:20:23 +01:00
// lambda..
if ( end - > str ( ) = = " [ " ) {
const Token * lambdaEnd = findLambdaEndToken ( end ) ;
if ( lambdaEnd ) {
end = lambdaEnd ;
break ;
}
2019-01-20 13:23:19 +01:00
}
2020-03-08 16:45:51 +01:00
if ( Token : : Match ( end , " (|[|{ " ) & &
2023-06-07 20:45:27 +02:00
! ( Token : : Match ( end , " ( ::| %type% " ) & & ! end - > astOperand2 ( ) ) ) {
2018-10-18 12:09:55 +02:00
end = end - > link ( ) ;
break ;
}
end = end - > astOperand2 ( ) ? end - > astOperand2 ( ) : end - > astOperand1 ( ) ;
}
2019-01-20 13:20:23 +01:00
// skip parentheses
2018-10-18 12:09:55 +02:00
start = goToLeftParenthesis ( start , end ) ;
end = goToRightParenthesis ( start , end ) ;
2018-12-14 18:56:09 +01:00
if ( Token : : simpleMatch ( end , " { " ) )
end = end - > link ( ) ;
2018-10-18 12:09:55 +02:00
return std : : pair < const Token * , const Token * > ( start , end ) ;
}
2015-09-09 14:46:47 +02:00
bool Token : : isCalculation ( ) const
2013-12-28 11:02:39 +01:00
{
if ( ! Token : : Match ( this , " %cop%|++|-- " ) )
return false ;
if ( Token : : Match ( this , " *|& " ) ) {
// dereference or address-of?
if ( ! this - > astOperand2 ( ) )
return false ;
if ( this - > astOperand2 ( ) - > str ( ) = = " [ " )
return false ;
// type specification?
std : : stack < const Token * > operands ;
operands . push ( this ) ;
while ( ! operands . empty ( ) ) {
const Token * op = operands . top ( ) ;
operands . pop ( ) ;
if ( op - > isNumber ( ) | | op - > varId ( ) > 0 )
return true ;
if ( op - > astOperand1 ( ) )
operands . push ( op - > astOperand1 ( ) ) ;
if ( op - > astOperand2 ( ) )
operands . push ( op - > astOperand2 ( ) ) ;
else if ( Token : : Match ( op , " *|& " ) )
return false ;
}
// type specification => return false
return false ;
}
return true ;
}
2015-05-24 17:02:00 +02:00
bool Token : : isUnaryPreOp ( ) const
2014-07-28 14:27:35 +02:00
{
2015-05-24 17:02:00 +02:00
if ( ! astOperand1 ( ) | | astOperand2 ( ) )
2014-07-28 14:27:35 +02:00
return false ;
2022-06-08 16:58:57 +02:00
if ( this - > tokType ( ) ! = Token : : eIncDecOp )
2014-07-28 14:27:35 +02:00
return true ;
2018-06-16 16:16:55 +02:00
const Token * tokbefore = mPrevious ;
const Token * tokafter = mNext ;
2015-10-26 13:29:47 +01:00
for ( int distance = 1 ; distance < 10 & & tokbefore ; distance + + ) {
2018-12-21 13:51:45 +01:00
if ( tokbefore = = mImpl - > mAstOperand1 )
2014-07-28 14:27:35 +02:00
return false ;
2018-12-21 13:51:45 +01:00
if ( tokafter = = mImpl - > mAstOperand1 )
2014-07-28 14:27:35 +02:00
return true ;
2018-06-16 16:16:55 +02:00
tokbefore = tokbefore - > mPrevious ;
tokafter = tokafter - > mPrevious ;
2014-07-28 14:27:35 +02:00
}
return false ; // <- guess
}
2016-09-19 06:13:09 +02:00
static std : : string stringFromTokenRange ( const Token * start , const Token * end )
{
2021-01-16 19:03:28 +01:00
std : : string ret ;
2017-07-21 09:16:42 +02:00
if ( end )
2017-07-21 09:17:25 +02:00
end = end - > next ( ) ;
2014-01-17 18:37:49 +01:00
for ( const Token * tok = start ; tok & & tok ! = end ; tok = tok - > next ( ) ) {
2017-07-21 09:17:25 +02:00
if ( tok - > isUnsigned ( ) )
2021-01-16 19:03:28 +01:00
ret + = " unsigned " ;
2019-10-16 11:41:33 +02:00
if ( tok - > isLong ( ) & & ! tok - > isLiteral ( ) )
2021-01-16 19:03:28 +01:00
ret + = " long " ;
2020-09-04 20:43:54 +02:00
if ( tok - > tokType ( ) = = Token : : eString ) {
2022-10-02 07:12:40 +02:00
for ( const unsigned char c : tok - > str ( ) ) {
2020-09-04 20:43:54 +02:00
if ( c = = ' \n ' )
2021-01-16 19:03:28 +01:00
ret + = " \\ n " ;
2020-09-04 20:43:54 +02:00
else if ( c = = ' \r ' )
2021-01-16 19:03:28 +01:00
ret + = " \\ r " ;
2020-09-04 20:43:54 +02:00
else if ( c = = ' \t ' )
2021-01-16 19:03:28 +01:00
ret + = " \\ t " ;
2020-09-04 20:43:54 +02:00
else if ( c > = ' ' & & c < = 126 )
2021-01-16 19:03:28 +01:00
ret + = c ;
2020-09-04 20:43:54 +02:00
else {
char str [ 10 ] ;
sprintf ( str , " \\ x%02x " , c ) ;
2021-01-16 19:03:28 +01:00
ret + = str ;
2020-09-04 20:43:54 +02:00
}
}
} else if ( tok - > originalName ( ) . empty ( ) | | tok - > isUnsigned ( ) | | tok - > isLong ( ) ) {
2021-01-16 19:03:28 +01:00
ret + = tok - > str ( ) ;
2017-07-21 09:16:42 +02:00
} else
2021-01-16 19:03:28 +01:00
ret + = tok - > originalName ( ) ;
2015-01-31 10:50:39 +01:00
if ( Token : : Match ( tok , " %name%|%num% %name%|%num% " ) )
2021-01-16 19:03:28 +01:00
ret + = ' ' ;
2014-01-17 18:37:49 +01:00
}
2021-01-16 19:03:28 +01:00
return ret ;
2014-01-17 18:37:49 +01:00
}
2016-09-19 06:13:09 +02:00
std : : string Token : : expressionString ( ) const
{
2018-10-18 12:09:55 +02:00
const auto tokens = findExpressionStartEndTokens ( ) ;
return stringFromTokenRange ( tokens . first , tokens . second ) ;
2016-09-19 06:13:09 +02:00
}
2019-07-14 12:22:33 +02:00
static void astStringXml ( const Token * tok , nonneg int indent , std : : ostream & out )
2014-07-13 17:21:45 +02:00
{
const std : : string strindent ( indent , ' ' ) ;
2014-07-14 15:51:45 +02:00
out < < strindent < < " <token str= \" " < < tok - > str ( ) < < ' \" ' ;
2021-01-27 19:49:13 +01:00
if ( tok - > varId ( ) )
2023-08-17 16:46:32 +02:00
out < < " varId= \" " < < tok - > varId ( ) < < ' \" ' ;
2014-07-14 15:51:45 +02:00
if ( tok - > variable ( ) )
out < < " variable= \" " < < tok - > variable ( ) < < ' \" ' ;
if ( tok - > function ( ) )
out < < " function= \" " < < tok - > function ( ) < < ' \" ' ;
2017-03-27 18:48:34 +02:00
if ( ! tok - > values ( ) . empty ( ) )
out < < " values= \" " < < & tok - > values ( ) < < ' \" ' ;
2014-07-14 15:51:45 +02:00
2014-07-13 17:21:45 +02:00
if ( ! tok - > astOperand1 ( ) & & ! tok - > astOperand2 ( ) ) {
2014-07-14 15:51:45 +02:00
out < < " /> " < < std : : endl ;
2014-07-13 17:21:45 +02:00
}
2014-07-14 15:51:45 +02:00
else {
out < < ' > ' < < std : : endl ;
if ( tok - > astOperand1 ( ) )
astStringXml ( tok - > astOperand1 ( ) , indent + 2U , out ) ;
if ( tok - > astOperand2 ( ) )
astStringXml ( tok - > astOperand2 ( ) , indent + 2U , out ) ;
out < < strindent < < " </token> " < < std : : endl ;
}
2014-07-13 17:21:45 +02:00
}
2020-10-31 18:57:48 +01:00
void Token : : printAst ( bool verbose , bool xml , const std : : vector < std : : string > & fileNames , std : : ostream & out ) const
2013-11-02 18:37:35 +01:00
{
2020-10-28 21:41:21 +01:00
if ( ! xml )
out < < " \n \n ##AST " < < std : : endl ;
2015-07-21 11:54:11 +02:00
std : : set < const Token * > printed ;
2013-11-02 18:37:35 +01:00
for ( const Token * tok = this ; tok ; tok = tok - > next ( ) ) {
2018-12-21 13:51:45 +01:00
if ( ! tok - > mImpl - > mAstParent & & tok - > mImpl - > mAstOperand1 ) {
2020-10-28 21:41:21 +01:00
if ( printed . find ( tok ) ! = printed . end ( ) )
2015-07-21 11:54:11 +02:00
continue ;
printed . insert ( tok ) ;
2014-07-13 17:21:45 +02:00
if ( xml ) {
2019-04-13 10:22:13 +02:00
out < < " <ast scope= \" " < < tok - > scope ( ) < < " \" fileIndex= \" " < < tok - > fileIndex ( ) < < " \" linenr= \" " < < tok - > linenr ( )
2019-08-18 12:19:05 +02:00
< < " \" column= \" " < < tok - > column ( ) < < " \" > " < < std : : endl ;
2015-07-21 11:54:11 +02:00
astStringXml ( tok , 2U , out ) ;
2014-07-14 15:51:45 +02:00
out < < " </ast> " < < std : : endl ;
2014-07-13 17:21:45 +02:00
} else if ( verbose )
2020-10-31 18:57:48 +01:00
out < < " [ " < < fileNames [ tok - > fileIndex ( ) ] < < " : " < < tok - > linenr ( ) < < " ] " < < std : : endl < < tok - > astStringVerbose ( ) < < std : : endl ;
2014-02-24 17:22:29 +01:00
else
2015-07-21 11:54:11 +02:00
out < < tok - > astString ( " " ) < < std : : endl ;
2014-01-18 09:58:32 +01:00
if ( tok - > str ( ) = = " ( " )
tok = tok - > link ( ) ;
2013-11-02 18:37:35 +01:00
}
}
}
2014-01-18 09:58:32 +01:00
2019-07-14 12:22:33 +02:00
static void indent ( std : : string & str , const nonneg int indent1 , const nonneg int indent2 )
2014-02-24 17:22:29 +01:00
{
2019-07-13 16:06:24 +02:00
for ( int i = 0 ; i < indent1 ; + + i )
Optimize astStringVerbose() for large arrays (#1815)
Change the astStringVerbose() recursion to extend a string instead of
returning one. This has the benefit that for tokens where the recursion
runs deep (typically large arrays), the time savings can be substantial
(see comments on benchmarks further down).
The reason is that previously, for each token, the astString of its
operands was constructed, and then appended to this tokens astString.
This led to a lot of unnecessary string copying (and with that
allocations). Instead, by passing the string by reference, the number
of temporary strings is greatly reduced.
Another way of seeing it is that previously, the string was constructed
from end to beginning, but now it is constructed from the beginning to
end. There was no notable speedup by preallocating the entire string
using string::reserve() (at least not on Linux).
To benchmark, the changes and master were tested on Linux using the
commands:
make
time cppcheck --debug --verbose $file >/dev/null
i.e., the cppcheck binary was compiled with the settings in the
Makefile. Printing the output to screen or file will of course take
longer time.
In Trac ticket #8355 which triggered this change, an example file from the
Wine repository was attached. Running the above cppcheck on master took
24 minutes and with the changes in this commmit, took 22 seconds.
Another test made was on lib/tokenlist.cpp in the cppcheck repo, which is
more "normal" file. On that file there was no measurable time difference.
A synthetic benchmark was generated to illustrate the effects on dumping
the ast for arrays of different sizes. The generate code looked as
follows:
const int array[] = {...};
with different number of elements. The results are as follows (times are
in seconds):
N master optimized
10 0.1 0.1
100 0.1 0.1
1000 2.8 0.7
2000 19 1.8
3000 53 3.8
5000 350 10
10000 3215 38
As we can see, for small arrays, there is no time difference, but for
large arrays the time savings are substantial.
2019-04-30 13:35:48 +02:00
str + = ' ' ;
2019-07-13 16:06:24 +02:00
for ( int i = indent1 ; i < indent2 ; i + = 2 )
Optimize astStringVerbose() for large arrays (#1815)
Change the astStringVerbose() recursion to extend a string instead of
returning one. This has the benefit that for tokens where the recursion
runs deep (typically large arrays), the time savings can be substantial
(see comments on benchmarks further down).
The reason is that previously, for each token, the astString of its
operands was constructed, and then appended to this tokens astString.
This led to a lot of unnecessary string copying (and with that
allocations). Instead, by passing the string by reference, the number
of temporary strings is greatly reduced.
Another way of seeing it is that previously, the string was constructed
from end to beginning, but now it is constructed from the beginning to
end. There was no notable speedup by preallocating the entire string
using string::reserve() (at least not on Linux).
To benchmark, the changes and master were tested on Linux using the
commands:
make
time cppcheck --debug --verbose $file >/dev/null
i.e., the cppcheck binary was compiled with the settings in the
Makefile. Printing the output to screen or file will of course take
longer time.
In Trac ticket #8355 which triggered this change, an example file from the
Wine repository was attached. Running the above cppcheck on master took
24 minutes and with the changes in this commmit, took 22 seconds.
Another test made was on lib/tokenlist.cpp in the cppcheck repo, which is
more "normal" file. On that file there was no measurable time difference.
A synthetic benchmark was generated to illustrate the effects on dumping
the ast for arrays of different sizes. The generate code looked as
follows:
const int array[] = {...};
with different number of elements. The results are as follows (times are
in seconds):
N master optimized
10 0.1 0.1
100 0.1 0.1
1000 2.8 0.7
2000 19 1.8
3000 53 3.8
5000 350 10
10000 3215 38
As we can see, for small arrays, there is no time difference, but for
large arrays the time savings are substantial.
2019-04-30 13:35:48 +02:00
str + = " | " ;
2014-02-24 17:22:29 +01:00
}
2019-07-14 12:22:33 +02:00
void Token : : astStringVerboseRecursive ( std : : string & ret , const nonneg int indent1 , const nonneg int indent2 ) const
2014-02-24 17:22:29 +01:00
{
2015-01-17 23:11:22 +01:00
if ( isExpandedMacro ( ) )
2016-01-01 13:54:07 +01:00
ret + = ' $ ' ;
2018-06-16 23:03:15 +02:00
ret + = mStr ;
2018-12-21 13:51:45 +01:00
if ( mImpl - > mValueType )
ret + = " \' " + mImpl - > mValueType - > str ( ) + ' \' ' ;
2020-01-12 15:04:25 +01:00
if ( function ( ) ) {
std : : ostringstream ostr ;
2020-10-09 07:54:16 +02:00
ostr < < std : : hex < < function ( ) ;
2020-01-12 15:04:25 +01:00
ret + = " f: " + ostr . str ( ) ;
}
2016-01-01 13:54:07 +01:00
ret + = ' \n ' ;
2015-01-17 23:11:22 +01:00
2018-12-21 13:51:45 +01:00
if ( mImpl - > mAstOperand1 ) {
2019-07-13 16:06:24 +02:00
int i1 = indent1 , i2 = indent2 + 2 ;
Optimize astStringVerbose() for large arrays (#1815)
Change the astStringVerbose() recursion to extend a string instead of
returning one. This has the benefit that for tokens where the recursion
runs deep (typically large arrays), the time savings can be substantial
(see comments on benchmarks further down).
The reason is that previously, for each token, the astString of its
operands was constructed, and then appended to this tokens astString.
This led to a lot of unnecessary string copying (and with that
allocations). Instead, by passing the string by reference, the number
of temporary strings is greatly reduced.
Another way of seeing it is that previously, the string was constructed
from end to beginning, but now it is constructed from the beginning to
end. There was no notable speedup by preallocating the entire string
using string::reserve() (at least not on Linux).
To benchmark, the changes and master were tested on Linux using the
commands:
make
time cppcheck --debug --verbose $file >/dev/null
i.e., the cppcheck binary was compiled with the settings in the
Makefile. Printing the output to screen or file will of course take
longer time.
In Trac ticket #8355 which triggered this change, an example file from the
Wine repository was attached. Running the above cppcheck on master took
24 minutes and with the changes in this commmit, took 22 seconds.
Another test made was on lib/tokenlist.cpp in the cppcheck repo, which is
more "normal" file. On that file there was no measurable time difference.
A synthetic benchmark was generated to illustrate the effects on dumping
the ast for arrays of different sizes. The generate code looked as
follows:
const int array[] = {...};
with different number of elements. The results are as follows (times are
in seconds):
N master optimized
10 0.1 0.1
100 0.1 0.1
1000 2.8 0.7
2000 19 1.8
3000 53 3.8
5000 350 10
10000 3215 38
As we can see, for small arrays, there is no time difference, but for
large arrays the time savings are substantial.
2019-04-30 13:35:48 +02:00
if ( indent1 = = indent2 & & ! mImpl - > mAstOperand2 )
2014-02-24 17:22:29 +01:00
i1 + = 2 ;
Optimize astStringVerbose() for large arrays (#1815)
Change the astStringVerbose() recursion to extend a string instead of
returning one. This has the benefit that for tokens where the recursion
runs deep (typically large arrays), the time savings can be substantial
(see comments on benchmarks further down).
The reason is that previously, for each token, the astString of its
operands was constructed, and then appended to this tokens astString.
This led to a lot of unnecessary string copying (and with that
allocations). Instead, by passing the string by reference, the number
of temporary strings is greatly reduced.
Another way of seeing it is that previously, the string was constructed
from end to beginning, but now it is constructed from the beginning to
end. There was no notable speedup by preallocating the entire string
using string::reserve() (at least not on Linux).
To benchmark, the changes and master were tested on Linux using the
commands:
make
time cppcheck --debug --verbose $file >/dev/null
i.e., the cppcheck binary was compiled with the settings in the
Makefile. Printing the output to screen or file will of course take
longer time.
In Trac ticket #8355 which triggered this change, an example file from the
Wine repository was attached. Running the above cppcheck on master took
24 minutes and with the changes in this commmit, took 22 seconds.
Another test made was on lib/tokenlist.cpp in the cppcheck repo, which is
more "normal" file. On that file there was no measurable time difference.
A synthetic benchmark was generated to illustrate the effects on dumping
the ast for arrays of different sizes. The generate code looked as
follows:
const int array[] = {...};
with different number of elements. The results are as follows (times are
in seconds):
N master optimized
10 0.1 0.1
100 0.1 0.1
1000 2.8 0.7
2000 19 1.8
3000 53 3.8
5000 350 10
10000 3215 38
As we can see, for small arrays, there is no time difference, but for
large arrays the time savings are substantial.
2019-04-30 13:35:48 +02:00
indent ( ret , indent1 , indent2 ) ;
ret + = mImpl - > mAstOperand2 ? " |- " : " `- " ;
mImpl - > mAstOperand1 - > astStringVerboseRecursive ( ret , i1 , i2 ) ;
2014-02-24 17:22:29 +01:00
}
2018-12-21 13:51:45 +01:00
if ( mImpl - > mAstOperand2 ) {
2019-07-13 16:06:24 +02:00
int i1 = indent1 , i2 = indent2 + 2 ;
Optimize astStringVerbose() for large arrays (#1815)
Change the astStringVerbose() recursion to extend a string instead of
returning one. This has the benefit that for tokens where the recursion
runs deep (typically large arrays), the time savings can be substantial
(see comments on benchmarks further down).
The reason is that previously, for each token, the astString of its
operands was constructed, and then appended to this tokens astString.
This led to a lot of unnecessary string copying (and with that
allocations). Instead, by passing the string by reference, the number
of temporary strings is greatly reduced.
Another way of seeing it is that previously, the string was constructed
from end to beginning, but now it is constructed from the beginning to
end. There was no notable speedup by preallocating the entire string
using string::reserve() (at least not on Linux).
To benchmark, the changes and master were tested on Linux using the
commands:
make
time cppcheck --debug --verbose $file >/dev/null
i.e., the cppcheck binary was compiled with the settings in the
Makefile. Printing the output to screen or file will of course take
longer time.
In Trac ticket #8355 which triggered this change, an example file from the
Wine repository was attached. Running the above cppcheck on master took
24 minutes and with the changes in this commmit, took 22 seconds.
Another test made was on lib/tokenlist.cpp in the cppcheck repo, which is
more "normal" file. On that file there was no measurable time difference.
A synthetic benchmark was generated to illustrate the effects on dumping
the ast for arrays of different sizes. The generate code looked as
follows:
const int array[] = {...};
with different number of elements. The results are as follows (times are
in seconds):
N master optimized
10 0.1 0.1
100 0.1 0.1
1000 2.8 0.7
2000 19 1.8
3000 53 3.8
5000 350 10
10000 3215 38
As we can see, for small arrays, there is no time difference, but for
large arrays the time savings are substantial.
2019-04-30 13:35:48 +02:00
if ( indent1 = = indent2 )
2014-02-24 17:22:29 +01:00
i1 + = 2 ;
Optimize astStringVerbose() for large arrays (#1815)
Change the astStringVerbose() recursion to extend a string instead of
returning one. This has the benefit that for tokens where the recursion
runs deep (typically large arrays), the time savings can be substantial
(see comments on benchmarks further down).
The reason is that previously, for each token, the astString of its
operands was constructed, and then appended to this tokens astString.
This led to a lot of unnecessary string copying (and with that
allocations). Instead, by passing the string by reference, the number
of temporary strings is greatly reduced.
Another way of seeing it is that previously, the string was constructed
from end to beginning, but now it is constructed from the beginning to
end. There was no notable speedup by preallocating the entire string
using string::reserve() (at least not on Linux).
To benchmark, the changes and master were tested on Linux using the
commands:
make
time cppcheck --debug --verbose $file >/dev/null
i.e., the cppcheck binary was compiled with the settings in the
Makefile. Printing the output to screen or file will of course take
longer time.
In Trac ticket #8355 which triggered this change, an example file from the
Wine repository was attached. Running the above cppcheck on master took
24 minutes and with the changes in this commmit, took 22 seconds.
Another test made was on lib/tokenlist.cpp in the cppcheck repo, which is
more "normal" file. On that file there was no measurable time difference.
A synthetic benchmark was generated to illustrate the effects on dumping
the ast for arrays of different sizes. The generate code looked as
follows:
const int array[] = {...};
with different number of elements. The results are as follows (times are
in seconds):
N master optimized
10 0.1 0.1
100 0.1 0.1
1000 2.8 0.7
2000 19 1.8
3000 53 3.8
5000 350 10
10000 3215 38
As we can see, for small arrays, there is no time difference, but for
large arrays the time savings are substantial.
2019-04-30 13:35:48 +02:00
indent ( ret , indent1 , indent2 ) ;
ret + = " `- " ;
mImpl - > mAstOperand2 - > astStringVerboseRecursive ( ret , i1 , i2 ) ;
2014-02-24 17:22:29 +01:00
}
Optimize astStringVerbose() for large arrays (#1815)
Change the astStringVerbose() recursion to extend a string instead of
returning one. This has the benefit that for tokens where the recursion
runs deep (typically large arrays), the time savings can be substantial
(see comments on benchmarks further down).
The reason is that previously, for each token, the astString of its
operands was constructed, and then appended to this tokens astString.
This led to a lot of unnecessary string copying (and with that
allocations). Instead, by passing the string by reference, the number
of temporary strings is greatly reduced.
Another way of seeing it is that previously, the string was constructed
from end to beginning, but now it is constructed from the beginning to
end. There was no notable speedup by preallocating the entire string
using string::reserve() (at least not on Linux).
To benchmark, the changes and master were tested on Linux using the
commands:
make
time cppcheck --debug --verbose $file >/dev/null
i.e., the cppcheck binary was compiled with the settings in the
Makefile. Printing the output to screen or file will of course take
longer time.
In Trac ticket #8355 which triggered this change, an example file from the
Wine repository was attached. Running the above cppcheck on master took
24 minutes and with the changes in this commmit, took 22 seconds.
Another test made was on lib/tokenlist.cpp in the cppcheck repo, which is
more "normal" file. On that file there was no measurable time difference.
A synthetic benchmark was generated to illustrate the effects on dumping
the ast for arrays of different sizes. The generate code looked as
follows:
const int array[] = {...};
with different number of elements. The results are as follows (times are
in seconds):
N master optimized
10 0.1 0.1
100 0.1 0.1
1000 2.8 0.7
2000 19 1.8
3000 53 3.8
5000 350 10
10000 3215 38
As we can see, for small arrays, there is no time difference, but for
large arrays the time savings are substantial.
2019-04-30 13:35:48 +02:00
}
std : : string Token : : astStringVerbose ( ) const
{
std : : string ret ;
astStringVerboseRecursive ( ret ) ;
2014-02-24 17:22:29 +01:00
return ret ;
}
2020-05-18 19:53:35 +02:00
std : : string Token : : astStringZ3 ( ) const
{
2020-05-18 19:31:13 +02:00
if ( ! astOperand1 ( ) )
return str ( ) ;
if ( ! astOperand2 ( ) )
return " ( " + str ( ) + " " + astOperand1 ( ) - > astStringZ3 ( ) + " ) " ;
return " ( " + str ( ) + " " + astOperand1 ( ) - > astStringZ3 ( ) + " " + astOperand2 ( ) - > astStringZ3 ( ) + " ) " ;
}
2014-02-24 17:22:29 +01:00
2014-07-14 15:51:45 +02:00
void Token : : printValueFlow ( bool xml , std : : ostream & out ) const
2014-01-18 09:58:32 +01:00
{
2023-08-31 11:54:46 +02:00
std : : string outs ;
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction
2023-11-16 13:44:50 +01:00
int fileIndex = - 1 ;
2019-07-13 16:06:24 +02:00
int line = 0 ;
2014-07-14 15:51:45 +02:00
if ( xml )
2023-08-31 11:54:46 +02:00
outs + = " <valueflow> \n " ;
2014-07-14 15:51:45 +02:00
else
2023-08-31 11:54:46 +02:00
outs + = " \n \n ##Value flow \n " ;
2014-01-18 09:58:32 +01:00
for ( const Token * tok = this ; tok ; tok = tok - > next ( ) ) {
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2022-10-16 13:46:26 +02:00
const auto * const values = tok - > mImpl - > mValues ;
if ( ! values )
2014-01-18 09:58:32 +01:00
continue ;
2022-10-16 13:46:26 +02:00
if ( values - > empty ( ) ) // Values might be removed by removeContradictions
2020-01-20 08:10:39 +01:00
continue ;
2023-08-31 11:54:46 +02:00
if ( xml ) {
outs + = " <values id= \" " ;
2023-08-31 23:14:28 +02:00
outs + = id_string ( values ) ;
2023-08-31 11:54:46 +02:00
outs + = " \" > " ;
outs + = ' \n ' ;
}
2023-11-16 13:44:50 +01:00
else {
if ( fileIndex ! = tok - > fileIndex ( ) ) {
outs + = " File " ;
outs + = tok - > mTokensFrontBack - > list - > getFiles ( ) [ tok - > fileIndex ( ) ] ;
outs + = ' \n ' ;
line = 0 ;
}
if ( line ! = tok - > linenr ( ) ) {
outs + = " Line " ;
outs + = std : : to_string ( tok - > linenr ( ) ) ;
outs + = ' \n ' ;
}
2023-08-31 11:54:46 +02:00
}
2023-11-16 13:44:50 +01:00
fileIndex = tok - > fileIndex ( ) ;
2014-01-18 09:58:32 +01:00
line = tok - > linenr ( ) ;
2015-07-26 15:36:09 +02:00
if ( ! xml ) {
2022-10-16 13:46:26 +02:00
ValueFlow : : Value : : ValueKind valueKind = values - > front ( ) . valueKind ;
const bool same = std : : all_of ( values - > begin ( ) , values - > end ( ) , [ & ] ( const ValueFlow : : Value & value ) {
return value . valueKind = = valueKind ;
} ) ;
2023-08-31 11:54:46 +02:00
outs + = " " ;
outs + = tok - > str ( ) ;
outs + = " " ;
2020-05-08 16:13:55 +02:00
if ( same ) {
switch ( valueKind ) {
case ValueFlow : : Value : : ValueKind : : Impossible :
case ValueFlow : : Value : : ValueKind : : Known :
2023-08-31 11:54:46 +02:00
outs + = " always " ;
2020-05-08 16:13:55 +02:00
break ;
case ValueFlow : : Value : : ValueKind : : Inconclusive :
2023-08-31 11:54:46 +02:00
outs + = " inconclusive " ;
2021-01-28 22:19:37 +01:00
break ;
2020-05-08 16:13:55 +02:00
case ValueFlow : : Value : : ValueKind : : Possible :
2023-08-31 11:54:46 +02:00
outs + = " possible " ;
2020-05-08 16:13:55 +02:00
break ;
2020-05-10 16:45:45 +02:00
}
2020-05-08 16:13:55 +02:00
}
2022-10-16 13:46:26 +02:00
if ( values - > size ( ) > 1U )
2023-08-31 11:54:46 +02:00
outs + = ' { ' ;
2015-07-26 15:36:09 +02:00
}
2022-10-16 13:46:26 +02:00
for ( const ValueFlow : : Value & value : * values ) {
2014-07-14 15:51:45 +02:00
if ( xml ) {
2023-08-31 11:54:46 +02:00
outs + = " <value " ;
2018-08-10 11:29:16 +02:00
switch ( value . valueType ) {
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : INT :
2023-08-31 11:54:46 +02:00
if ( tok - > valueType ( ) & & tok - > valueType ( ) - > sign = = ValueType : : UNSIGNED ) {
outs + = " intvalue= \" " ;
outs + = std : : to_string ( static_cast < MathLib : : biguint > ( value . intvalue ) ) ;
outs + = ' \" ' ;
}
else {
outs + = " intvalue= \" " ;
outs + = std : : to_string ( value . intvalue ) ;
outs + = ' \" ' ;
}
2016-11-13 22:59:56 +01:00
break ;
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : TOK :
2023-08-31 11:54:46 +02:00
outs + = " tokvalue= \" " ;
2023-08-31 23:14:28 +02:00
outs + = id_string ( value . tokvalue ) ;
2023-08-31 11:54:46 +02:00
outs + = ' \" ' ;
2016-11-13 22:59:56 +01:00
break ;
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : FLOAT :
2023-08-31 11:54:46 +02:00
outs + = " floatvalue= \" " ;
outs + = std : : to_string ( value . floatValue ) ; // TODO: should this be MathLib::toString()?
outs + = ' \" ' ;
2016-11-13 22:59:56 +01:00
break ;
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : MOVED :
2023-08-31 11:54:46 +02:00
outs + = " movedvalue= \" " ;
outs + = ValueFlow : : Value : : toString ( value . moveKind ) ;
outs + = ' \" ' ;
2016-11-20 15:14:49 +01:00
break ;
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : UNINIT :
2023-08-31 11:54:46 +02:00
outs + = " uninit= \" 1 \" " ;
2017-04-23 18:05:14 +02:00
break ;
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : BUFFER_SIZE :
2023-08-31 11:54:46 +02:00
outs + = " buffer-size= \" " ;
outs + = std : : to_string ( value . intvalue ) ;
outs + = " \" " ;
2019-03-17 13:09:15 +01:00
break ;
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : CONTAINER_SIZE :
2023-08-31 11:54:46 +02:00
outs + = " container-size= \" " ;
outs + = std : : to_string ( value . intvalue ) ;
outs + = ' \" ' ;
2018-08-10 11:29:16 +02:00
break ;
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : ITERATOR_START :
2023-08-31 11:54:46 +02:00
outs + = " iterator-start= \" " ;
outs + = std : : to_string ( value . intvalue ) ;
outs + = ' \" ' ;
2020-08-17 23:36:45 +02:00
break ;
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : ITERATOR_END :
2023-08-31 11:54:46 +02:00
outs + = " iterator-end= \" " ;
outs + = std : : to_string ( value . intvalue ) ;
outs + = ' \" ' ;
2020-08-17 23:36:45 +02:00
break ;
2021-01-02 09:30:00 +01:00
case ValueFlow : : Value : : ValueType : : LIFETIME :
2023-08-31 11:54:46 +02:00
outs + = " lifetime= \" " ;
2023-08-31 23:14:28 +02:00
outs + = id_string ( value . tokvalue ) ;
2023-08-31 11:54:46 +02:00
outs + = ' \" ' ;
outs + = " lifetime-scope= \" " ;
outs + = ValueFlow : : Value : : toString ( value . lifetimeScope ) ;
outs + = " \" " ;
outs + = " lifetime-kind= \" " ;
outs + = ValueFlow : : Value : : toString ( value . lifetimeKind ) ;
outs + = " \" " ;
2018-11-14 06:14:04 +01:00
break ;
2021-07-30 21:29:35 +02:00
case ValueFlow : : Value : : ValueType : : SYMBOLIC :
2023-08-31 11:54:46 +02:00
outs + = " symbolic= \" " ;
2023-08-31 23:14:28 +02:00
outs + = id_string ( value . tokvalue ) ;
2023-08-31 11:54:46 +02:00
outs + = ' \" ' ;
outs + = " symbolic-delta= \" " ;
outs + = std : : to_string ( value . intvalue ) ;
outs + = ' \" ' ;
2021-07-30 21:29:35 +02:00
break ;
2016-11-13 22:59:56 +01:00
}
2023-08-31 11:54:46 +02:00
outs + = " bound= \" " ;
outs + = ValueFlow : : Value : : toString ( value . bound ) ;
outs + = " \" " ;
if ( value . condition ) {
outs + = " condition-line= \" " ;
outs + = std : : to_string ( value . condition - > linenr ( ) ) ;
outs + = ' \" ' ;
}
2018-08-10 11:29:16 +02:00
if ( value . isKnown ( ) )
2023-08-31 11:54:46 +02:00
outs + = " known= \" true \" " ;
2018-08-10 11:29:16 +02:00
else if ( value . isPossible ( ) )
2023-08-31 11:54:46 +02:00
outs + = " possible= \" true \" " ;
2019-09-20 15:06:37 +02:00
else if ( value . isImpossible ( ) )
2023-08-31 11:54:46 +02:00
outs + = " impossible= \" true \" " ;
2018-08-10 11:29:16 +02:00
else if ( value . isInconclusive ( ) )
2023-08-31 11:54:46 +02:00
outs + = " inconclusive= \" true \" " ;
outs + = " path= \" " ;
outs + = std : : to_string ( value . path ) ;
outs + = " \" " ;
outs + = " /> \n " ;
2014-07-14 15:51:45 +02:00
}
else {
2022-10-16 13:46:26 +02:00
if ( & value ! = & values - > front ( ) )
2023-08-31 11:54:46 +02:00
outs + = " , " ;
outs + = value . toString ( ) ;
2014-07-14 15:51:45 +02:00
}
2014-01-18 09:58:32 +01:00
}
2014-07-14 15:51:45 +02:00
if ( xml )
2023-08-31 11:54:46 +02:00
outs + = " </values> \n " ;
2022-10-16 13:46:26 +02:00
else if ( values - > size ( ) > 1U )
2023-08-31 11:54:46 +02:00
outs + = " } \n " ;
2014-07-14 15:51:45 +02:00
else
2023-08-31 11:54:46 +02:00
outs + = ' \n ' ;
2014-01-18 09:58:32 +01:00
}
2014-07-14 15:51:45 +02:00
if ( xml )
2023-08-31 11:54:46 +02:00
outs + = " </valueflow> \n " ;
out < < outs ;
2014-01-18 09:58:32 +01:00
}
2014-04-02 06:49:28 +02:00
const ValueFlow : : Value * Token : : getValueLE ( const MathLib : : bigint val , const Settings * settings ) const
{
2018-12-21 13:51:45 +01:00
if ( ! mImpl - > mValues )
2017-03-27 18:48:34 +02:00
return nullptr ;
2021-06-04 17:17:41 +02:00
return ValueFlow : : findValue ( * mImpl - > mValues , settings , [ & ] ( const ValueFlow : : Value & v ) {
return ! v . isImpossible ( ) & & v . isIntValue ( ) & & v . intvalue < = val ;
} ) ;
2014-04-02 06:49:28 +02:00
}
const ValueFlow : : Value * Token : : getValueGE ( const MathLib : : bigint val , const Settings * settings ) const
{
2018-12-21 13:51:45 +01:00
if ( ! mImpl - > mValues )
2017-03-27 18:48:34 +02:00
return nullptr ;
2021-06-04 17:17:41 +02:00
return ValueFlow : : findValue ( * mImpl - > mValues , settings , [ & ] ( const ValueFlow : : Value & v ) {
return ! v . isImpossible ( ) & & v . isIntValue ( ) & & v . intvalue > = val ;
} ) ;
2017-04-20 22:14:54 +02:00
}
2019-07-14 12:22:33 +02:00
const ValueFlow : : Value * Token : : getInvalidValue ( const Token * ftok , nonneg int argnr , const Settings * settings ) const
2017-04-20 22:14:54 +02:00
{
2018-12-21 13:51:45 +01:00
if ( ! mImpl - > mValues | | ! settings )
2017-04-20 22:14:54 +02:00
return nullptr ;
const ValueFlow : : Value * ret = nullptr ;
2023-10-18 10:06:17 +02:00
for ( std : : list < ValueFlow : : Value > : : const_iterator it = mImpl - > mValues - > begin ( ) ; it ! = mImpl - > mValues - > end ( ) ; + + it ) {
2019-09-20 15:06:37 +02:00
if ( it - > isImpossible ( ) )
continue ;
2018-07-15 23:05:48 +02:00
if ( ( it - > isIntValue ( ) & & ! settings - > library . isIntArgValid ( ftok , argnr , it - > intvalue ) ) | |
( it - > isFloatValue ( ) & & ! settings - > library . isFloatArgValid ( ftok , argnr , it - > floatValue ) ) ) {
2017-09-20 22:41:36 +02:00
if ( ! ret | | ret - > isInconclusive ( ) | | ( ret - > condition & & ! it - > isInconclusive ( ) ) )
2017-04-20 22:14:54 +02:00
ret = & ( * it ) ;
2017-09-20 22:41:36 +02:00
if ( ! ret - > isInconclusive ( ) & & ! ret - > condition )
2017-04-20 22:14:54 +02:00
break ;
}
}
2018-01-28 14:27:01 +01:00
if ( ret ) {
2021-02-24 22:00:06 +01:00
if ( ret - > isInconclusive ( ) & & ! settings - > certainty . isEnabled ( Certainty : : inconclusive ) )
2014-04-02 06:49:28 +02:00
return nullptr ;
2021-02-24 22:00:06 +01:00
if ( ret - > condition & & ! settings - > severity . isEnabled ( Severity : : warning ) )
2014-04-02 06:49:28 +02:00
return nullptr ;
}
return ret ;
}
2022-08-17 09:45:07 +02:00
const Token * Token : : getValueTokenMinStrSize ( const Settings * settings , MathLib : : bigint * path ) const
2014-08-04 08:25:10 +02:00
{
2018-12-21 13:51:45 +01:00
if ( ! mImpl - > mValues )
2017-03-27 18:48:34 +02:00
return nullptr ;
2014-08-04 08:25:10 +02:00
const Token * ret = nullptr ;
2019-07-13 16:06:24 +02:00
int minsize = INT_MAX ;
2023-10-18 10:06:17 +02:00
for ( std : : list < ValueFlow : : Value > : : const_iterator it = mImpl - > mValues - > begin ( ) ; it ! = mImpl - > mValues - > end ( ) ; + + it ) {
2016-11-13 22:33:39 +01:00
if ( it - > isTokValue ( ) & & it - > tokvalue & & it - > tokvalue - > tokType ( ) = = Token : : eString ) {
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 int size = getStrSize ( it - > tokvalue , settings ) ;
2014-08-04 08:25:10 +02:00
if ( ! ret | | size < minsize ) {
minsize = size ;
ret = it - > tokvalue ;
2022-08-17 09:45:07 +02:00
if ( path )
* path = it - > path ;
2014-08-04 08:25:10 +02:00
}
}
}
return ret ;
}
const Token * Token : : getValueTokenMaxStrLength ( ) const
{
2018-12-21 13:51:45 +01:00
if ( ! mImpl - > mValues )
2017-03-27 18:48:34 +02:00
return nullptr ;
2014-08-04 08:25:10 +02:00
const Token * ret = nullptr ;
2019-07-13 16:06:24 +02:00
int maxlength = 0 ;
2023-10-18 10:06:17 +02:00
for ( std : : list < ValueFlow : : Value > : : const_iterator it = mImpl - > mValues - > begin ( ) ; it ! = mImpl - > mValues - > end ( ) ; + + it ) {
2016-11-13 22:33:39 +01:00
if ( it - > isTokValue ( ) & & it - > tokvalue & & it - > tokvalue - > tokType ( ) = = Token : : eString ) {
2019-07-13 16:06:24 +02:00
const int length = getStrLength ( it - > tokvalue ) ;
2014-08-04 08:25:10 +02:00
if ( ! ret | | length > maxlength ) {
maxlength = length ;
ret = it - > tokvalue ;
}
}
}
return ret ;
}
2021-03-30 14:02:28 +02:00
static bool isAdjacent ( const ValueFlow : : Value & x , const ValueFlow : : Value & y )
{
if ( x . bound ! = ValueFlow : : Value : : Bound : : Point & & x . bound = = y . bound )
return true ;
if ( x . valueType = = ValueFlow : : Value : : ValueType : : FLOAT )
return false ;
return std : : abs ( x . intvalue - y . intvalue ) = = 1 ;
}
static bool removePointValue ( std : : list < ValueFlow : : Value > & values , ValueFlow : : Value & x )
{
const bool isPoint = x . bound = = ValueFlow : : Value : : Bound : : Point ;
if ( ! isPoint )
x . decreaseRange ( ) ;
else
values . remove ( x ) ;
return isPoint ;
}
2019-09-20 15:06:37 +02:00
static bool removeContradiction ( std : : list < ValueFlow : : Value > & values )
{
bool result = false ;
for ( ValueFlow : : Value & x : values ) {
if ( x . isNonValue ( ) )
continue ;
for ( ValueFlow : : Value & y : values ) {
if ( y . isNonValue ( ) )
continue ;
if ( x = = y )
continue ;
if ( x . valueType ! = y . valueType )
continue ;
if ( x . isImpossible ( ) = = y . isImpossible ( ) )
continue ;
2021-08-19 22:01:55 +02:00
if ( x . isSymbolicValue ( ) & & ! ValueFlow : : Value : : sameToken ( x . tokvalue , y . tokvalue ) )
continue ;
2021-03-30 14:02:28 +02:00
if ( ! x . equalValue ( y ) ) {
auto compare = [ ] ( const ValueFlow : : Value & x , const ValueFlow : : Value & y ) {
2023-01-26 22:23:22 +01:00
return x . compareValue ( y , less { } ) ;
2021-03-30 14:02:28 +02:00
} ;
const ValueFlow : : Value & maxValue = std : : max ( x , y , compare ) ;
const ValueFlow : : Value & minValue = std : : min ( x , y , compare ) ;
// TODO: Adjust non-points instead of removing them
if ( maxValue . isImpossible ( ) & & maxValue . bound = = ValueFlow : : Value : : Bound : : Upper ) {
values . remove ( minValue ) ;
return true ;
}
if ( minValue . isImpossible ( ) & & minValue . bound = = ValueFlow : : Value : : Bound : : Lower ) {
values . remove ( maxValue ) ;
return true ;
}
2019-09-20 15:06:37 +02:00
continue ;
2021-03-30 14:02:28 +02:00
}
const bool removex = ! x . isImpossible ( ) | | y . isKnown ( ) ;
const bool removey = ! y . isImpossible ( ) | | x . isKnown ( ) ;
if ( x . bound = = y . bound ) {
2019-09-20 15:06:37 +02:00
if ( removex )
values . remove ( x ) ;
if ( removey )
values . remove ( y ) ;
return true ;
}
2023-06-20 18:43:21 +02:00
result = removex | | removey ;
bool bail = false ;
if ( removex & & removePointValue ( values , x ) )
bail = true ;
if ( removey & & removePointValue ( values , y ) )
bail = true ;
if ( bail )
return true ;
2019-09-20 15:06:37 +02:00
}
}
return result ;
}
2021-03-30 14:02:28 +02:00
using ValueIterator = std : : list < ValueFlow : : Value > : : iterator ;
2021-08-07 20:51:18 +02:00
template < class Iterator >
2021-03-30 14:02:28 +02:00
static ValueIterator removeAdjacentValues ( std : : list < ValueFlow : : Value > & values , ValueIterator x , Iterator start , Iterator last )
{
if ( ! isAdjacent ( * x , * * start ) )
return std : : next ( x ) ;
2021-04-04 18:20:32 +02:00
auto it = std : : adjacent_find ( start , last , [ ] ( ValueIterator x , ValueIterator y ) {
return ! isAdjacent ( * x , * y ) ;
} ) ;
2021-03-30 14:02:28 +02:00
if ( it = = last )
it - - ;
( * it ) - > bound = x - > bound ;
2021-04-04 18:20:32 +02:00
std : : for_each ( start , it , [ & ] ( ValueIterator y ) {
values . erase ( y ) ;
} ) ;
2021-03-30 14:02:28 +02:00
return values . erase ( x ) ;
}
static void mergeAdjacent ( std : : list < ValueFlow : : Value > & values )
{
for ( auto x = values . begin ( ) ; x ! = values . end ( ) ; ) {
if ( x - > isNonValue ( ) ) {
x + + ;
continue ;
}
if ( x - > bound = = ValueFlow : : Value : : Bound : : Point ) {
x + + ;
continue ;
}
std : : vector < ValueIterator > adjValues ;
for ( auto y = values . begin ( ) ; y ! = values . end ( ) ; y + + ) {
if ( x = = y )
continue ;
if ( y - > isNonValue ( ) )
continue ;
if ( x - > valueType ! = y - > valueType )
continue ;
if ( x - > valueKind ! = y - > valueKind )
continue ;
2021-08-19 22:01:55 +02:00
if ( x - > isSymbolicValue ( ) & & ! ValueFlow : : Value : : sameToken ( x - > tokvalue , y - > tokvalue ) )
continue ;
2021-03-30 14:02:28 +02:00
if ( x - > bound ! = y - > bound ) {
2021-05-16 10:34:22 +02:00
if ( y - > bound ! = ValueFlow : : Value : : Bound : : Point & & isAdjacent ( * x , * y ) ) {
adjValues . clear ( ) ;
break ;
}
2021-03-30 14:02:28 +02:00
// No adjacent points for floating points
if ( x - > valueType = = ValueFlow : : Value : : ValueType : : FLOAT )
continue ;
if ( y - > bound ! = ValueFlow : : Value : : Bound : : Point )
continue ;
}
2023-01-26 22:23:22 +01:00
if ( x - > bound = = ValueFlow : : Value : : Bound : : Lower & & ! y - > compareValue ( * x , less { } ) )
2021-03-30 14:02:28 +02:00
continue ;
2023-01-26 22:23:22 +01:00
if ( x - > bound = = ValueFlow : : Value : : Bound : : Upper & & ! x - > compareValue ( * y , less { } ) )
2021-03-30 14:02:28 +02:00
continue ;
adjValues . push_back ( y ) ;
}
if ( adjValues . empty ( ) ) {
x + + ;
continue ;
}
std : : sort ( adjValues . begin ( ) , adjValues . end ( ) , [ & values ] ( ValueIterator xx , ValueIterator yy ) {
2022-03-13 20:07:58 +01:00
( void ) values ;
2021-03-30 14:02:28 +02:00
assert ( xx ! = values . end ( ) & & yy ! = values . end ( ) ) ;
2023-01-26 22:23:22 +01:00
return xx - > compareValue ( * yy , less { } ) ;
2021-03-30 14:02:28 +02:00
} ) ;
if ( x - > bound = = ValueFlow : : Value : : Bound : : Lower )
x = removeAdjacentValues ( values , x , adjValues . rbegin ( ) , adjValues . rend ( ) ) ;
else if ( x - > bound = = ValueFlow : : Value : : Bound : : Upper )
x = removeAdjacentValues ( values , x , adjValues . begin ( ) , adjValues . end ( ) ) ;
}
}
2019-09-20 15:06:37 +02:00
static void removeOverlaps ( std : : list < ValueFlow : : Value > & values )
{
2023-11-22 14:05:53 +01:00
for ( const ValueFlow : : Value & x : values ) {
2019-09-20 15:06:37 +02:00
if ( x . isNonValue ( ) )
continue ;
2023-11-22 14:05:53 +01:00
values . remove_if ( [ & ] ( const ValueFlow : : Value & y ) {
2019-09-20 15:06:37 +02:00
if ( y . isNonValue ( ) )
return false ;
if ( & x = = & y )
return false ;
if ( x . valueType ! = y . valueType )
return false ;
if ( x . valueKind ! = y . valueKind )
return false ;
2022-09-27 06:48:06 +02:00
// TODO: Remove points covered in a lower or upper bound
2019-09-20 15:06:37 +02:00
// TODO: Remove lower or upper bound already covered by a lower and upper bound
if ( ! x . equalValue ( y ) )
return false ;
if ( x . bound ! = y . bound )
return false ;
return true ;
} ) ;
}
2021-03-30 14:02:28 +02:00
mergeAdjacent ( values ) ;
2019-09-20 15:06:37 +02:00
}
// Removing contradictions is an NP-hard problem. Instead we run multiple
// passes to try to catch most contradictions
static void removeContradictions ( std : : list < ValueFlow : : Value > & values )
{
2021-03-30 14:02:28 +02:00
removeOverlaps ( values ) ;
2019-09-20 15:06:37 +02:00
for ( int i = 0 ; i < 4 ; i + + ) {
if ( ! removeContradiction ( values ) )
return ;
removeOverlaps ( values ) ;
}
}
2021-08-22 18:06:54 +02:00
static bool sameValueType ( const ValueFlow : : Value & x , const ValueFlow : : Value & y )
2021-08-19 22:01:55 +02:00
{
if ( x . valueType ! = y . valueType )
return false ;
// Symbolic are the same type if they share the same tokvalue
if ( x . isSymbolicValue ( ) )
return x . tokvalue - > exprId ( ) = = 0 | | x . tokvalue - > exprId ( ) = = y . tokvalue - > exprId ( ) ;
return true ;
}
2017-03-27 18:48:34 +02:00
bool Token : : addValue ( const ValueFlow : : Value & value )
{
2018-12-21 13:51:45 +01:00
if ( value . isKnown ( ) & & mImpl - > mValues ) {
2018-11-14 06:59:25 +01:00
// Clear all other values of the same type since value is known
2021-07-30 21:29:35 +02:00
mImpl - > mValues - > remove_if ( [ & ] ( const ValueFlow : : Value & x ) {
2021-08-19 22:01:55 +02:00
return sameValueType ( x , value ) ;
2018-11-14 06:59:25 +01:00
} ) ;
2017-03-27 18:48:34 +02:00
}
2022-03-16 15:28:44 +01:00
// Don't add a value if its already known
2022-01-22 12:25:20 +01:00
if ( ! value . isKnown ( ) & & mImpl - > mValues & &
std : : any_of ( mImpl - > mValues - > begin ( ) , mImpl - > mValues - > end ( ) , [ & ] ( const ValueFlow : : Value & x ) {
return x . isKnown ( ) & & sameValueType ( x , value ) & & ! x . equalValue ( value ) ;
} ) )
return false ;
2022-01-21 09:56:24 +01:00
// assert(value.isKnown() || !mImpl->mValues || std::none_of(mImpl->mValues->begin(), mImpl->mValues->end(),
2021-08-19 22:01:55 +02:00
// [&](const ValueFlow::Value& x) {
// return x.isKnown() && sameValueType(x, value);
// }));
2018-12-21 13:51:45 +01:00
if ( mImpl - > mValues ) {
2017-03-27 18:48:34 +02:00
// Don't handle more than 10 values for performance reasons
// TODO: add setting?
2018-12-21 13:51:45 +01:00
if ( mImpl - > mValues - > size ( ) > = 10U )
2017-03-27 18:48:34 +02:00
return false ;
// if value already exists, don't add it again
std : : list < ValueFlow : : Value > : : iterator it ;
2018-12-21 13:51:45 +01:00
for ( it = mImpl - > mValues - > begin ( ) ; it ! = mImpl - > mValues - > end ( ) ; + + it ) {
2017-03-27 18:48:34 +02:00
// different types => continue
if ( it - > valueType ! = value . valueType )
continue ;
2019-07-24 12:30:33 +02:00
2019-09-20 15:06:37 +02:00
if ( it - > isImpossible ( ) ! = value . isImpossible ( ) )
continue ;
2019-07-24 12:30:33 +02:00
// different value => continue
2021-07-30 21:29:35 +02:00
if ( ! it - > equalValue ( value ) )
2019-07-24 12:30:33 +02:00
continue ;
2018-11-14 06:59:25 +01:00
if ( ( value . isTokValue ( ) | | value . isLifetimeValue ( ) ) & & ( it - > tokvalue ! = value . tokvalue ) & & ( it - > tokvalue - > str ( ) ! = value . tokvalue - > str ( ) ) )
2017-03-27 18:48:34 +02:00
continue ;
// same value, but old value is inconclusive so replace it
2019-09-20 15:06:37 +02:00
if ( it - > isInconclusive ( ) & & ! value . isInconclusive ( ) & & ! value . isImpossible ( ) ) {
2017-03-27 18:48:34 +02:00
* it = value ;
if ( it - > varId = = 0 )
2018-12-21 13:51:45 +01:00
it - > varId = mImpl - > mVarId ;
2017-03-27 18:48:34 +02:00
break ;
}
// Same value already exists, don't add new value
return false ;
}
// Add value
2018-12-21 13:51:45 +01:00
if ( it = = mImpl - > mValues - > end ( ) ) {
2017-03-27 18:48:34 +02:00
ValueFlow : : Value v ( value ) ;
if ( v . varId = = 0 )
2018-12-21 13:51:45 +01:00
v . varId = mImpl - > mVarId ;
2018-11-14 19:10:52 +01:00
if ( v . isKnown ( ) & & v . isIntValue ( ) )
2022-09-08 09:21:35 +02:00
mImpl - > mValues - > push_front ( std : : move ( v ) ) ;
2018-11-14 06:59:25 +01:00
else
2022-09-08 09:21:35 +02:00
mImpl - > mValues - > push_back ( std : : move ( v ) ) ;
2017-03-27 18:48:34 +02:00
}
} else {
ValueFlow : : Value v ( value ) ;
if ( v . varId = = 0 )
2018-12-21 13:51:45 +01:00
v . varId = mImpl - > mVarId ;
2023-03-02 21:46:23 +01:00
mImpl - > mValues = new std : : list < ValueFlow : : Value > ;
mImpl - > mValues - > push_back ( std : : move ( v ) ) ;
2017-03-27 18:48:34 +02:00
}
2019-09-20 15:06:37 +02:00
removeContradictions ( * mImpl - > mValues ) ;
2017-03-27 18:48:34 +02:00
return true ;
}
2014-05-20 21:55:08 +02:00
void Token : : assignProgressValues ( Token * tok )
{
2019-07-13 16:06:24 +02:00
int total_count = 0 ;
2014-05-20 21:55:08 +02:00
for ( Token * tok2 = tok ; tok2 ; tok2 = tok2 - > next ( ) )
+ + total_count ;
2019-07-13 16:06:24 +02:00
int count = 0 ;
2014-05-20 21:55:08 +02:00
for ( Token * tok2 = tok ; tok2 ; tok2 = tok2 - > next ( ) )
2021-08-07 20:51:18 +02:00
tok2 - > mImpl - > mProgressValue = count + + * 100 / total_count ;
2014-05-20 21:55:08 +02:00
}
2015-10-04 19:42:58 +02:00
2019-06-21 22:16:23 +02:00
void Token : : assignIndexes ( )
{
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2019-07-13 16:06:24 +02:00
int index = ( mPrevious ? mPrevious - > mImpl - > mIndex : 0 ) + 1 ;
2019-06-21 22:16:23 +02:00
for ( Token * tok = this ; tok ; tok = tok - > next ( ) )
tok - > mImpl - > mIndex = index + + ;
}
2015-10-04 19:42:58 +02:00
void Token : : setValueType ( ValueType * vt )
{
2018-12-21 13:51:45 +01:00
if ( vt ! = mImpl - > mValueType ) {
delete mImpl - > mValueType ;
mImpl - > mValueType = vt ;
2015-10-04 19:42:58 +02:00
}
}
2016-04-22 06:02:54 +02:00
void Token : : type ( const : : Type * t )
{
2018-12-21 13:51:45 +01:00
mImpl - > mType = t ;
2016-04-22 06:02:54 +02:00
if ( t ) {
2017-10-15 01:27:47 +02:00
tokType ( eType ) ;
2018-12-21 13:51:45 +01:00
isEnumType ( mImpl - > mType - > isEnumType ( ) ) ;
2018-06-16 16:40:02 +02:00
} else if ( mTokType = = eType )
2017-10-15 01:27:47 +02:00
tokType ( eName ) ;
2016-04-22 06:02:54 +02:00
}
2021-06-03 07:31:46 +02:00
const : : Type * Token : : typeOf ( const Token * tok , const Token * * typeTok )
2019-05-02 11:04:23 +02:00
{
2020-06-25 19:54:46 +02:00
if ( ! tok )
return nullptr ;
2021-06-03 07:31:46 +02:00
if ( typeTok ! = nullptr )
* typeTok = tok ;
2022-02-18 12:59:21 +01:00
const Token * lhsVarTok { } ;
2023-06-20 18:43:21 +02:00
if ( tok - > type ( ) )
2022-06-08 16:58:57 +02:00
return tok - > type ( ) ;
2023-06-20 18:43:21 +02:00
if ( tok - > variable ( ) )
2022-06-08 16:58:57 +02:00
return tok - > variable ( ) - > type ( ) ;
2023-06-20 18:43:21 +02:00
if ( tok - > function ( ) )
2022-06-08 16:58:57 +02:00
return tok - > function ( ) - > retType ;
2023-06-20 18:43:21 +02:00
if ( Token : : simpleMatch ( tok , " return " ) ) {
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2019-05-02 11:04:23 +02:00
const Scope * scope = tok - > scope ( ) ;
if ( ! scope )
return nullptr ;
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2019-05-02 11:04:23 +02:00
const Function * function = scope - > function ;
if ( ! function )
return nullptr ;
return function - > retType ;
2023-06-20 18:43:21 +02:00
}
if ( Token : : Match ( tok - > previous ( ) , " %type%|= (|{ " ) )
2021-06-03 07:31:46 +02:00
return typeOf ( tok - > previous ( ) , typeTok ) ;
2023-06-20 18:43:21 +02:00
if ( Token : : simpleMatch ( tok , " = " ) & & ( lhsVarTok = getLHSVariableToken ( tok ) ) ! = tok - > next ( ) )
2022-02-18 12:59:21 +01:00
return Token : : typeOf ( lhsVarTok , typeTok ) ;
2023-06-20 18:43:21 +02:00
if ( Token : : simpleMatch ( tok , " . " ) )
2021-06-03 07:31:46 +02:00
return Token : : typeOf ( tok - > astOperand2 ( ) , typeTok ) ;
2023-06-20 18:43:21 +02:00
if ( Token : : simpleMatch ( tok , " [ " ) )
2021-06-03 07:31:46 +02:00
return Token : : typeOf ( tok - > astOperand1 ( ) , typeTok ) ;
2023-06-20 18:43:21 +02:00
if ( Token : : simpleMatch ( tok , " { " ) ) {
2021-06-03 07:31:46 +02:00
int argnr ;
const Token * ftok = getTokenArgumentFunction ( tok , argnr ) ;
if ( argnr < 0 )
return nullptr ;
if ( ! ftok )
return nullptr ;
if ( ftok = = tok )
return nullptr ;
std : : vector < const Variable * > vars = getArgumentVars ( ftok , argnr ) ;
if ( vars . empty ( ) )
return nullptr ;
if ( std : : all_of (
2022-12-30 15:13:47 +01:00
vars . cbegin ( ) , vars . cend ( ) , [ & ] ( const Variable * var ) {
2021-08-07 20:51:18 +02:00
return var - > type ( ) = = vars . front ( ) - > type ( ) ;
2021-06-03 07:35:50 +02:00
} ) )
2021-08-07 20:51:18 +02:00
return vars . front ( ) - > type ( ) ;
2019-05-02 11:04:23 +02:00
}
2021-06-03 07:31:46 +02:00
2019-05-02 11:04:23 +02:00
return nullptr ;
}
2022-12-07 09:38:36 +01:00
std : : pair < const Token * , const Token * > Token : : typeDecl ( const Token * tok , bool pointedToType )
2019-05-02 11:04:23 +02:00
{
2020-06-18 00:06:06 +02:00
if ( ! tok )
return { } ;
2023-06-20 18:43:21 +02:00
if ( tok - > type ( ) )
2019-05-02 11:04:23 +02:00
return { tok , tok - > next ( ) } ;
2023-06-20 18:43:21 +02:00
if ( tok - > variable ( ) ) {
2019-05-02 11:04:23 +02:00
const Variable * var = tok - > variable ( ) ;
if ( ! var - > typeStartToken ( ) | | ! var - > typeEndToken ( ) )
return { } ;
2023-04-01 09:38:40 +02:00
if ( pointedToType & & astIsSmartPointer ( var - > nameToken ( ) ) ) {
const ValueType * vt = var - > valueType ( ) ;
if ( vt & & vt - > smartPointerTypeToken )
return { vt - > smartPointerTypeToken , vt - > smartPointerTypeToken - > linkAt ( - 1 ) } ;
}
if ( pointedToType & & astIsIterator ( var - > nameToken ( ) ) ) {
const ValueType * vt = var - > valueType ( ) ;
if ( vt & & vt - > containerTypeToken )
return { vt - > containerTypeToken , vt - > containerTypeToken - > linkAt ( - 1 ) } ;
}
2022-12-18 16:54:58 +01:00
std : : pair < const Token * , const Token * > result ;
2020-06-18 00:06:06 +02:00
if ( Token : : simpleMatch ( var - > typeStartToken ( ) , " auto " ) ) {
const Token * tok2 = var - > declEndToken ( ) ;
if ( Token : : Match ( tok2 , " ; %varid% = " , var - > declarationId ( ) ) )
tok2 = tok2 - > tokAt ( 2 ) ;
2021-08-26 19:03:35 +02:00
if ( Token : : simpleMatch ( tok2 , " = " ) & & Token : : Match ( tok2 - > astOperand2 ( ) , " !!= " ) & & tok ! = tok2 - > astOperand2 ( ) ) {
2022-12-02 20:28:14 +01:00
tok2 = tok2 - > astOperand2 ( ) ;
2023-02-24 21:05:26 +01:00
2023-02-28 15:05:45 +01:00
if ( Token : : simpleMatch ( tok2 , " [ " ) & & tok2 - > astOperand1 ( ) ) {
const ValueType * vt = tok2 - > astOperand1 ( ) - > valueType ( ) ;
if ( vt & & vt - > containerTypeToken )
return { vt - > containerTypeToken , vt - > containerTypeToken - > linkAt ( - 1 ) } ;
}
2023-02-24 21:05:26 +01:00
const Token * varTok = tok2 ; // try to find a variable
if ( Token : : Match ( varTok , " :: %name% " ) )
varTok = varTok - > next ( ) ;
while ( Token : : Match ( varTok , " %name% :: " ) )
varTok = varTok - > tokAt ( 2 ) ;
std : : pair < const Token * , const Token * > r = typeDecl ( varTok ) ;
2020-06-18 00:06:06 +02:00
if ( r . first )
return r ;
2023-02-24 21:05:26 +01:00
2022-12-07 09:38:36 +01:00
if ( pointedToType & & tok2 - > astOperand1 ( ) & & Token : : simpleMatch ( tok2 , " new " ) ) {
2022-12-02 20:28:14 +01:00
if ( Token : : simpleMatch ( tok2 - > astOperand1 ( ) , " ( " ) )
return { tok2 - > next ( ) , tok2 - > astOperand1 ( ) } ;
const Token * declEnd = nextAfterAstRightmostLeaf ( tok2 - > astOperand1 ( ) ) ;
if ( Token : : simpleMatch ( declEnd , " < " ) & & declEnd - > link ( ) )
declEnd = declEnd - > link ( ) - > next ( ) ;
2022-12-16 10:46:15 +01:00
return { tok2 - > next ( ) , declEnd } ;
2022-12-02 20:28:14 +01:00
}
2022-12-18 16:54:58 +01:00
const Token * typeBeg { } , * typeEnd { } ;
if ( tok2 - > str ( ) = = " :: " & & Token : : simpleMatch ( tok2 - > astOperand2 ( ) , " { " ) ) { // empty initlist
typeBeg = previousBeforeAstLeftmostLeaf ( tok2 ) ;
typeEnd = tok2 - > astOperand2 ( ) ;
}
else if ( tok2 - > str ( ) = = " { " ) {
typeBeg = previousBeforeAstLeftmostLeaf ( tok2 ) ;
typeEnd = tok2 ;
}
if ( typeBeg )
result = { typeBeg - > next ( ) , typeEnd } ; // handle smart pointers/iterators first
2020-06-18 00:06:06 +02:00
}
2022-12-02 07:21:33 +01:00
if ( astIsRangeBasedForDecl ( var - > nameToken ( ) ) & & astIsContainer ( var - > nameToken ( ) - > astParent ( ) - > astOperand2 ( ) ) ) { // range-based for
const ValueType * vt = var - > nameToken ( ) - > astParent ( ) - > astOperand2 ( ) - > valueType ( ) ;
if ( vt & & vt - > containerTypeToken )
return { vt - > containerTypeToken , vt - > containerTypeToken - > linkAt ( - 1 ) } ;
}
2020-06-18 00:06:06 +02:00
}
2023-02-01 14:53:57 +01:00
if ( result . first )
return result ;
2019-05-02 11:04:23 +02:00
return { var - > typeStartToken ( ) , var - > typeEndToken ( ) - > next ( ) } ;
2023-06-20 18:43:21 +02:00
}
if ( Token : : simpleMatch ( tok , " return " ) ) {
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2022-06-08 16:58:57 +02:00
const Scope * scope = tok - > scope ( ) ;
if ( ! scope )
return { } ;
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2022-06-08 16:58:57 +02:00
const Function * function = scope - > function ;
2019-05-02 11:04:23 +02:00
if ( ! function )
return { } ;
2022-06-08 16:58:57 +02:00
return { function - > retDef , function - > returnDefEnd ( ) } ;
2023-06-20 18:43:21 +02:00
}
if ( tok - > previous ( ) & & tok - > previous ( ) - > function ( ) ) {
2023-12-01 15:59:01 +01:00
// cppcheck-suppress shadowFunction - TODO: fix this
2022-06-08 16:58:57 +02:00
const Function * function = tok - > previous ( ) - > function ( ) ;
2019-05-02 11:04:23 +02:00
return { function - > retDef , function - > returnDefEnd ( ) } ;
2023-06-20 18:43:21 +02:00
}
if ( Token : : simpleMatch ( tok , " = " ) )
2019-05-02 11:04:23 +02:00
return Token : : typeDecl ( tok - > astOperand1 ( ) ) ;
2023-06-20 18:43:21 +02:00
if ( Token : : simpleMatch ( tok , " . " ) )
2019-05-02 11:04:23 +02:00
return Token : : typeDecl ( tok - > astOperand2 ( ) ) ;
2023-06-20 18:43:21 +02:00
const : : Type * t = typeOf ( tok ) ;
if ( ! t | | ! t - > classDef )
return { } ;
return { t - > classDef - > next ( ) , t - > classDef - > tokAt ( 2 ) } ;
2019-05-02 11:04:23 +02:00
}
std : : string Token : : typeStr ( const Token * tok )
{
2019-06-02 10:14:48 +02:00
if ( tok - > valueType ( ) ) {
const ValueType * vt = tok - > valueType ( ) ;
std : : string ret = vt - > str ( ) ;
if ( ! ret . empty ( ) )
2019-06-02 13:29:20 +02:00
return ret ;
2019-06-02 10:14:48 +02:00
}
2019-05-02 11:04:23 +02:00
std : : pair < const Token * , const Token * > r = Token : : typeDecl ( tok ) ;
if ( ! r . first | | ! r . second )
return " " ;
return r . first - > stringifyList ( r . second , false ) ;
}
2019-07-31 11:11:01 +02:00
void Token : : scopeInfo ( std : : shared_ptr < ScopeInfo2 > newScopeInfo )
{
2022-09-16 18:58:59 +02:00
mImpl - > mScopeInfo = std : : move ( newScopeInfo ) ;
2019-07-31 09:19:27 +02:00
}
2019-07-31 11:11:01 +02:00
std : : shared_ptr < ScopeInfo2 > Token : : scopeInfo ( ) const
{
2019-07-31 09:19:27 +02:00
return mImpl - > mScopeInfo ;
}
2020-05-10 20:32:59 +02:00
bool Token : : hasKnownIntValue ( ) const
{
if ( ! mImpl - > mValues )
return false ;
return std : : any_of ( mImpl - > mValues - > begin ( ) , mImpl - > mValues - > end ( ) , [ ] ( const ValueFlow : : Value & value ) {
return value . isKnown ( ) & & value . isIntValue ( ) ;
} ) ;
}
bool Token : : hasKnownValue ( ) const
{
return mImpl - > mValues & & std : : any_of ( mImpl - > mValues - > begin ( ) , mImpl - > mValues - > end ( ) , std : : mem_fn ( & ValueFlow : : Value : : isKnown ) ) ;
}
2021-07-24 22:44:18 +02:00
bool Token : : hasKnownValue ( ValueFlow : : Value : : ValueType t ) const
{
return mImpl - > mValues & &
2021-08-07 20:51:18 +02:00
std : : any_of ( mImpl - > mValues - > begin ( ) , mImpl - > mValues - > end ( ) , [ & ] ( const ValueFlow : : Value & value ) {
2021-07-26 16:32:00 +02:00
return value . isKnown ( ) & & value . valueType = = t ;
} ) ;
2021-07-24 22:44:18 +02:00
}
2021-08-19 22:01:55 +02:00
bool Token : : hasKnownSymbolicValue ( const Token * tok ) const
{
if ( tok - > exprId ( ) = = 0 )
return false ;
return mImpl - > mValues & &
std : : any_of ( mImpl - > mValues - > begin ( ) , mImpl - > mValues - > end ( ) , [ & ] ( const ValueFlow : : Value & value ) {
2021-08-26 07:46:40 +02:00
return value . isKnown ( ) & & value . isSymbolicValue ( ) & & value . tokvalue & &
value . tokvalue - > exprId ( ) = = tok - > exprId ( ) ;
2021-08-19 22:01:55 +02:00
} ) ;
}
2021-07-24 22:44:18 +02:00
const ValueFlow : : Value * Token : : getKnownValue ( ValueFlow : : Value : : ValueType t ) const
{
if ( ! mImpl - > mValues )
return nullptr ;
auto it = std : : find_if ( mImpl - > mValues - > begin ( ) , mImpl - > mValues - > end ( ) , [ & ] ( const ValueFlow : : Value & value ) {
return value . isKnown ( ) & & value . valueType = = t ;
} ) ;
return it = = mImpl - > mValues - > end ( ) ? nullptr : & * it ;
}
2020-05-10 20:32:59 +02:00
const ValueFlow : : Value * Token : : getValue ( const MathLib : : bigint val ) const
{
if ( ! mImpl - > mValues )
return nullptr ;
const auto it = std : : find_if ( mImpl - > mValues - > begin ( ) , mImpl - > mValues - > end ( ) , [ = ] ( const ValueFlow : : Value & value ) {
return value . isIntValue ( ) & & ! value . isImpossible ( ) & & value . intvalue = = val ;
} ) ;
return it = = mImpl - > mValues - > end ( ) ? nullptr : & * it ;
}
2023-08-14 15:17:05 +02:00
template < class Compare >
static const ValueFlow : : Value * getCompareValue ( const std : : list < ValueFlow : : Value > & values ,
bool condition ,
MathLib : : bigint path ,
Compare compare )
2020-05-10 20:32:59 +02:00
{
const ValueFlow : : Value * ret = nullptr ;
2023-08-14 15:17:05 +02:00
for ( const ValueFlow : : Value & value : values ) {
2020-05-10 20:32:59 +02:00
if ( ! value . isIntValue ( ) )
continue ;
if ( value . isImpossible ( ) )
continue ;
2022-01-18 14:48:02 +01:00
if ( path > - 0 & & value . path ! = 0 & & value . path ! = path )
2021-08-16 09:19:07 +02:00
continue ;
2023-08-14 15:17:05 +02:00
if ( ( ! ret | | compare ( value . intvalue , ret - > intvalue ) ) & & ( ( value . condition ! = nullptr ) = = condition ) )
2020-05-10 20:32:59 +02:00
ret = & value ;
}
return ret ;
}
2023-08-14 15:17:05 +02:00
const ValueFlow : : Value * Token : : getMaxValue ( bool condition , MathLib : : bigint path ) const
{
if ( ! mImpl - > mValues )
return nullptr ;
return getCompareValue ( * mImpl - > mValues , condition , path , std : : greater < MathLib : : bigint > { } ) ;
}
const ValueFlow : : Value * Token : : getMinValue ( bool condition , MathLib : : bigint path ) const
{
if ( ! mImpl - > mValues )
return nullptr ;
return getCompareValue ( * mImpl - > mValues , condition , path , std : : less < MathLib : : bigint > { } ) ;
}
2020-05-10 20:32:59 +02:00
const ValueFlow : : Value * Token : : getMovedValue ( ) const
{
if ( ! mImpl - > mValues )
return nullptr ;
const auto it = std : : find_if ( mImpl - > mValues - > begin ( ) , mImpl - > mValues - > end ( ) , [ ] ( const ValueFlow : : Value & value ) {
return value . isMovedValue ( ) & & ! value . isImpossible ( ) & &
2021-08-07 20:51:18 +02:00
value . moveKind ! = ValueFlow : : Value : : MoveKind : : NonMovedVariable ;
2020-05-10 20:32:59 +02:00
} ) ;
return it = = mImpl - > mValues - > end ( ) ? nullptr : & * it ;
}
2022-01-18 22:02:25 +01:00
// cppcheck-suppress unusedFunction
2020-05-10 20:32:59 +02:00
const ValueFlow : : Value * Token : : getContainerSizeValue ( const MathLib : : bigint val ) const
{
if ( ! mImpl - > mValues )
return nullptr ;
const auto it = std : : find_if ( mImpl - > mValues - > begin ( ) , mImpl - > mValues - > end ( ) , [ = ] ( const ValueFlow : : Value & value ) {
return value . isContainerSizeValue ( ) & & ! value . isImpossible ( ) & & value . intvalue = = val ;
} ) ;
return it = = mImpl - > mValues - > end ( ) ? nullptr : & * it ;
}
2018-12-21 13:51:45 +01:00
TokenImpl : : ~ TokenImpl ( )
{
delete mOriginalName ;
delete mValueType ;
delete mValues ;
2022-07-18 16:20:17 +02:00
if ( mTemplateSimplifierPointers ) {
2020-05-15 09:02:59 +02:00
for ( auto * templateSimplifierPointer : * mTemplateSimplifierPointers ) {
templateSimplifierPointer - > token ( nullptr ) ;
}
2022-07-18 16:20:17 +02:00
}
2020-05-15 09:02:59 +02:00
delete mTemplateSimplifierPointers ;
2019-07-12 11:09:24 +02:00
while ( mCppcheckAttributes ) {
2023-10-08 09:10:17 +02:00
CppcheckAttributes * c = mCppcheckAttributes ;
2019-07-12 11:09:24 +02:00
mCppcheckAttributes = mCppcheckAttributes - > next ;
delete c ;
}
}
void TokenImpl : : setCppcheckAttribute ( TokenImpl : : CppcheckAttributes : : Type type , MathLib : : bigint value )
{
2023-10-08 09:10:17 +02:00
CppcheckAttributes * attr = mCppcheckAttributes ;
2019-07-12 11:09:24 +02:00
while ( attr & & attr - > type ! = type )
attr = attr - > next ;
if ( attr )
attr - > value = value ;
else {
attr = new CppcheckAttributes ;
attr - > type = type ;
attr - > value = value ;
attr - > next = mCppcheckAttributes ;
mCppcheckAttributes = attr ;
}
}
2023-03-02 22:05:41 +01:00
bool TokenImpl : : getCppcheckAttribute ( TokenImpl : : CppcheckAttributes : : Type type , MathLib : : bigint & value ) const
2019-07-12 11:09:24 +02:00
{
2023-10-08 09:10:17 +02:00
CppcheckAttributes * attr = mCppcheckAttributes ;
2019-07-12 11:09:24 +02:00
while ( attr & & attr - > type ! = type )
attr = attr - > next ;
if ( attr )
2023-03-02 22:05:41 +01:00
value = attr - > value ;
2019-07-12 11:09:24 +02:00
return attr ! = nullptr ;
2018-12-21 13:51:45 +01:00
}
2021-08-29 15:40:10 +02:00
Token * findTypeEnd ( Token * tok )
{
while ( Token : : Match ( tok , " %name%|.|::|*|&|&&|<|(|template|decltype|sizeof " ) ) {
if ( Token : : Match ( tok , " (|< " ) )
tok = tok - > link ( ) ;
if ( ! tok )
return nullptr ;
tok = tok - > next ( ) ;
}
return tok ;
}
Token * findLambdaEndScope ( Token * tok )
{
if ( ! Token : : simpleMatch ( tok , " [ " ) )
return nullptr ;
tok = tok - > link ( ) ;
if ( ! Token : : Match ( tok , " ] (|{ " ) )
return nullptr ;
tok = tok - > linkAt ( 1 ) ;
if ( Token : : simpleMatch ( tok , " } " ) )
return tok ;
if ( Token : : simpleMatch ( tok , " ) { " ) )
return tok - > linkAt ( 1 ) ;
if ( ! Token : : simpleMatch ( tok , " ) " ) )
return nullptr ;
tok = tok - > next ( ) ;
#11134 Fix broken AST with (designated) initializers (#4550)
* Make control flow a bit easier, and more similar to previous code
Made similar to around line 790
* In a cpp11init, always parse only the corresponding } (#11134)
- _always_, because in some cases this was omitted (around line 790) or too strict (around line 860)
- _only_, and not following tokens which happen to be } as well (around line 1030)
* Fix unit tests: AST was incorrect, now is fixed
auto var{ {{},{}}, {} };
Old AST:
```
{
|-var
`-{
`-,
|-,
| |-{
| `-{
`-{
```
New AST:
```
{
|-var
`-,
|-{
| `-,
| | |-{
| | `-{
`-{
```
Compare the same example, but with `X{}` instead of just `{}`:
`auto var{ a{b{},c{}}, d{} };`
```
{
|-var
`-,
|-{
| |-a
| `-,
| | |-{
| | | `-b
| | `-{
| | | `-c
`-{
`-d
```
This structure is similar to that of the new AST, not the old AST
* Fix unit tests: another AST was incorrect, now is fixed
Code: `auto var{{1,a::b{2,3}}, {4,a::b{5,6}}};`
Old AST:
```
{
|-var
`-{
`-,
|-,
| |-1 'signed int'
| `-{
| | |-::
| | | |-a
| | | `-b
| | `-,
| | | |-2 'signed int'
| | | `-3 'signed int'
`-{
`-,
|-4 'signed int'
`-{
|-::
| |-a
| `-b
`-,
|-5 'signed int'
`-6 'signed int'
```
New AST:
```
{
|-var
`-,
|-{
| `-,
| | |-1 'signed int'
| | `-{
| | | |-::
| | | | |-a
| | | | `-b
| | | `-,
| | | | |-2 'signed int'
| | | | `-3 'signed int'
`-{
`-,
|-4 'signed int'
`-{
|-::
| |-a
| `-b
`-,
|-5 'signed int'
`-6 'signed int'
```
* Fix unit tests: missing ; after class, resulting in incorrectly being marked as cpp11init
Because of the missing `;` after the class declaration, it was marked as a cpp11init block.
Which it isn't, and which now throws an exception
* Fix cpp11init to let unit tests pass again
The following unit tests failed on the newly introduced throws, because the code for these tests incorrectly marked some tokens as cpp11init:
TestVarID::varid_cpp11initialization
TestTokenizer::checkRefQualifiers
* Fix typo
* Improve check for void trailing return type
Observation: the only function body _not_ containing a semicolon, is a void function: something like
auto make_zero(ini& i) -> void {
while(--i > 0) {}
}
Non-void function? Then it must return a value, and thus contain a semicolon, which is checked for a few lines later.
* Fix cpp11init with templated trailing return type
In the following example, vector was marked as cpp11init due to the mismatch of `%any% {`
auto f() -> std::vector<int> { return {}; }
I made the assumption that whenever "%any% {" matches, endtok must be set too.
If this assumtion doesn't hold (so "%any% {" matches, but endtok == nullptr), then the for-loop would search all the way to the end of stream. Which I guess was not the intention.
* Remove comments
Co-authored-by: Gerbo Engels <gerbo.engels@ortec-finance.com>
2022-10-19 07:25:15 +02:00
while ( Token : : Match ( tok , " mutable|constexpr|consteval|noexcept|. " ) ) {
2021-08-29 15:40:10 +02:00
if ( Token : : simpleMatch ( tok , " noexcept ( " ) )
tok = tok - > linkAt ( 1 ) ;
if ( Token : : simpleMatch ( tok , " . " ) ) {
tok = findTypeEnd ( tok ) ;
break ;
}
tok = tok - > next ( ) ;
}
if ( Token : : simpleMatch ( tok , " { " ) )
return tok - > link ( ) ;
return nullptr ;
}
const Token * findLambdaEndScope ( const Token * tok ) {
return findLambdaEndScope ( const_cast < Token * > ( tok ) ) ;
}
2023-08-18 22:48:24 +02:00
bool Token : : isCpp ( ) const
{
if ( mTokensFrontBack & & mTokensFrontBack - > list ) {
return mTokensFrontBack - > list - > isCPP ( ) ;
}
return true ; // assume C++ by default
}