2012-05-05 18:33:26 +02:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2014-02-15 07:45:39 +01:00
* Copyright ( C ) 2007 - 2014 Daniel Marjamäki and Cppcheck team .
2012-05-05 18:33:26 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
# include "tokenlist.h"
# include "token.h"
# include "mathlib.h"
# include "path.h"
# include "preprocessor.h"
# include "settings.h"
2012-05-06 10:17:15 +02:00
# include "errorlogger.h"
2012-05-05 18:33:26 +02:00
# include <cstring>
# include <sstream>
# include <cctype>
# include <stack>
2014-05-31 18:02:19 +02:00
# include <cassert>
2012-05-05 18:33:26 +02:00
2014-04-15 06:31:09 +02:00
// How many compileExpression recursions are allowed?
// For practical code this could be endless. But in some special torture test
// there needs to be a limit.
static const unsigned int AST_MAX_DEPTH = 50U ;
2012-05-05 18:33:26 +02:00
TokenList : : TokenList ( const Settings * settings ) :
_front ( 0 ) ,
_back ( 0 ) ,
2014-06-24 18:45:06 +02:00
_settings ( settings ) ,
_isC ( false ) ,
_isCPP ( false )
2012-05-05 18:33:26 +02:00
{
}
TokenList : : ~ TokenList ( )
{
deallocateTokens ( ) ;
}
//---------------------------------------------------------------------------
2014-06-04 18:00:22 +02:00
const std : : string & TokenList : : getSourceFilePath ( ) const
{
if ( getFiles ( ) . empty ( ) ) {
2014-06-26 11:44:19 +02:00
return emptyString ;
2014-06-04 18:00:22 +02:00
}
return getFiles ( ) [ 0 ] ;
}
//---------------------------------------------------------------------------
2012-05-05 18:33:26 +02:00
// Deallocate lists..
void TokenList : : deallocateTokens ( )
{
deleteTokens ( _front ) ;
_front = 0 ;
_back = 0 ;
_files . clear ( ) ;
}
2014-01-28 17:15:07 +01:00
unsigned int TokenList : : appendFileIfNew ( const std : : string & fileName )
{
// Has this file been tokenized already?
2014-07-07 21:25:30 +02:00
for ( std : : size_t i = 0 ; i < _files . size ( ) ; + + i )
2014-01-28 17:15:07 +01:00
if ( Path : : sameFileName ( _files [ i ] , fileName ) )
2014-07-08 21:47:22 +02:00
return ( unsigned int ) i ;
2014-01-28 17:15:07 +01:00
// The "_files" vector remembers what files have been tokenized..
2014-04-02 13:56:34 +02:00
_files . push_back ( Path : : simplifyPath ( fileName ) ) ;
2014-06-24 17:34:20 +02:00
// Update _isC and _isCPP properties
if ( _files . size ( ) = = 1 ) { // Update only useful if first file added to _files
if ( ! _settings ) {
_isC = Path : : isC ( getSourceFilePath ( ) ) ;
_isCPP = Path : : isCPP ( getSourceFilePath ( ) ) ;
} else {
_isC = _settings - > enforcedLang = = Settings : : C | | ( _settings - > enforcedLang = = Settings : : None & & Path : : isC ( getSourceFilePath ( ) ) ) ;
_isCPP = _settings - > enforcedLang = = Settings : : CPP | | ( _settings - > enforcedLang = = Settings : : None & & Path : : isCPP ( getSourceFilePath ( ) ) ) ;
}
}
2014-01-28 17:15:07 +01:00
return static_cast < unsigned int > ( _files . size ( ) - 1 ) ;
}
2012-05-05 18:33:26 +02:00
void TokenList : : deleteTokens ( Token * tok )
{
while ( tok ) {
Token * next = tok - > next ( ) ;
delete tok ;
tok = next ;
}
}
//---------------------------------------------------------------------------
// add a token.
//---------------------------------------------------------------------------
2014-03-09 17:54:49 +01:00
void TokenList : : addtoken ( const std : : string & str , const unsigned int lineno , const unsigned int fileno , bool split )
2012-05-05 18:33:26 +02:00
{
2014-03-09 17:54:49 +01:00
if ( str . empty ( ) )
2012-05-05 18:33:26 +02:00
return ;
// If token contains # characters, split it up
2014-03-09 17:54:49 +01:00
if ( split ) {
size_t begin = 0 ;
size_t end = 0 ;
while ( ( end = str . find ( " ## " , begin ) ) ! = std : : string : : npos ) {
addtoken ( str . substr ( begin , end - begin ) , lineno , fileno , false ) ;
addtoken ( " ## " , lineno , fileno , false ) ;
begin = end + 2 ;
}
if ( begin ! = 0 ) {
addtoken ( str . substr ( begin ) , lineno , fileno , false ) ;
return ;
2012-05-05 18:33:26 +02:00
}
}
// Replace hexadecimal value with decimal
2014-03-09 17:54:49 +01:00
std : : string str2 ;
2012-09-10 21:13:32 +02:00
if ( MathLib : : isHex ( str ) | | MathLib : : isOct ( str ) | | MathLib : : isBin ( str ) ) {
2014-03-09 17:54:49 +01:00
std : : ostringstream str2stream ;
str2stream < < MathLib : : toLongNumber ( str ) ;
str2 = str2stream . str ( ) ;
} else if ( str . compare ( 0 , 5 , " _Bool " ) = = 0 ) {
str2 = " bool " ;
2012-05-05 18:33:26 +02:00
} else {
2014-03-09 17:54:49 +01:00
str2 = str ;
2012-05-05 18:33:26 +02:00
}
if ( _back ) {
2014-03-09 17:54:49 +01:00
_back - > insertToken ( str2 ) ;
2012-05-05 18:33:26 +02:00
} else {
_front = new Token ( & _back ) ;
_back = _front ;
2014-03-09 17:54:49 +01:00
_back - > str ( str2 ) ;
2012-05-05 18:33:26 +02:00
}
2014-06-04 22:33:08 +02:00
if ( isCPP ( ) & & str = = " delete " )
_back - > isKeyword ( true ) ;
2012-05-05 18:33:26 +02:00
_back - > linenr ( lineno ) ;
_back - > fileIndex ( fileno ) ;
}
void TokenList : : addtoken ( const Token * tok , const unsigned int lineno , const unsigned int fileno )
{
if ( tok = = 0 )
return ;
if ( _back ) {
2013-09-24 06:43:03 +02:00
_back - > insertToken ( tok - > str ( ) , tok - > originalName ( ) ) ;
2012-05-05 18:33:26 +02:00
} else {
_front = new Token ( & _back ) ;
_back = _front ;
_back - > str ( tok - > str ( ) ) ;
2014-06-26 10:57:39 +02:00
if ( ! tok - > originalName ( ) . empty ( ) )
_back - > originalName ( tok - > originalName ( ) ) ;
2012-05-05 18:33:26 +02:00
}
_back - > linenr ( lineno ) ;
_back - > fileIndex ( fileno ) ;
2014-05-06 06:35:48 +02:00
_back - > flags ( tok - > flags ( ) ) ;
2012-05-05 18:33:26 +02:00
}
//---------------------------------------------------------------------------
// InsertTokens - Copy and insert tokens
//---------------------------------------------------------------------------
void TokenList : : insertTokens ( Token * dest , const Token * src , unsigned int n )
{
std : : stack < Token * > link ;
while ( n > 0 ) {
2013-09-24 06:43:03 +02:00
dest - > insertToken ( src - > str ( ) , src - > originalName ( ) ) ;
2012-05-05 18:33:26 +02:00
dest = dest - > next ( ) ;
// Set links
if ( Token : : Match ( dest , " (|[|{ " ) )
link . push ( dest ) ;
else if ( ! link . empty ( ) & & Token : : Match ( dest , " )|]|} " ) ) {
Token : : createMutualLinks ( dest , link . top ( ) ) ;
link . pop ( ) ;
}
dest - > fileIndex ( src - > fileIndex ( ) ) ;
dest - > linenr ( src - > linenr ( ) ) ;
dest - > varId ( src - > varId ( ) ) ;
dest - > type ( src - > type ( ) ) ;
2014-05-06 06:35:48 +02:00
dest - > flags ( src - > flags ( ) ) ;
2012-05-05 18:33:26 +02:00
src = src - > next ( ) ;
- - n ;
}
}
//---------------------------------------------------------------------------
// Tokenize - tokenizes a given file.
//---------------------------------------------------------------------------
bool TokenList : : createTokens ( std : : istream & code , const std : : string & file0 )
{
2014-06-24 17:34:20 +02:00
appendFileIfNew ( file0 ) ;
2012-05-05 18:33:26 +02:00
// line number in parsed code
unsigned int lineno = 1 ;
// The current token being parsed
std : : string CurrentToken ;
// lineNumbers holds line numbers for files in fileIndexes
// every time an include file is completely parsed, last item in the vector
// is removed and lineno is set to point to that value.
std : : stack < unsigned int > lineNumbers ;
// fileIndexes holds index for _files vector about currently parsed files
// every time an include file is completely parsed, last item in the vector
// is removed and FileIndex is set to point to that value.
std : : stack < unsigned int > fileIndexes ;
// FileIndex. What file in the _files vector is read now?
unsigned int FileIndex = 0 ;
bool expandedMacro = false ;
// Read one byte at a time from code and create tokens
2014-03-09 17:54:49 +01:00
for ( char ch = ( char ) code . get ( ) ; code . good ( ) & & ch ; ch = ( char ) code . get ( ) ) {
2012-05-05 18:33:26 +02:00
if ( ch = = Preprocessor : : macroChar ) {
while ( code . peek ( ) = = Preprocessor : : macroChar )
code . get ( ) ;
2014-01-03 18:53:37 +01:00
if ( ! CurrentToken . empty ( ) ) {
2014-03-09 17:54:49 +01:00
addtoken ( CurrentToken , lineno , FileIndex , true ) ;
2014-01-03 18:53:37 +01:00
_back - > isExpandedMacro ( expandedMacro ) ;
}
CurrentToken . clear ( ) ;
2012-05-05 18:33:26 +02:00
expandedMacro = true ;
2014-01-03 18:53:37 +01:00
continue ;
2012-05-05 18:33:26 +02:00
}
// char/string..
// multiline strings are not handled. The preprocessor should handle that for us.
else if ( ch = = ' \' ' | | ch = = ' \" ' ) {
std : : string line ;
// read char
bool special = false ;
char c = ch ;
do {
// Append token..
line + = c ;
// Special sequence '\.'
if ( special )
special = false ;
else
special = ( c = = ' \\ ' ) ;
// Get next character
c = ( char ) code . get ( ) ;
} while ( code . good ( ) & & ( special | | c ! = ch ) ) ;
line + = ch ;
// Handle #file "file.h"
if ( CurrentToken = = " #file " ) {
// Extract the filename
line = line . substr ( 1 , line . length ( ) - 2 ) ;
+ + lineno ;
fileIndexes . push ( FileIndex ) ;
2014-01-28 17:15:07 +01:00
FileIndex = appendFileIfNew ( line ) ;
2012-05-05 18:33:26 +02:00
lineNumbers . push ( lineno ) ;
lineno = 0 ;
} else {
// Add previous token
2014-03-09 17:54:49 +01:00
addtoken ( CurrentToken , lineno , FileIndex ) ;
2012-05-05 18:33:26 +02:00
if ( ! CurrentToken . empty ( ) )
2013-08-30 06:27:46 +02:00
_back - > isExpandedMacro ( expandedMacro ) ;
2012-05-05 18:33:26 +02:00
// Add content of the string
2014-03-09 17:54:49 +01:00
addtoken ( line , lineno , FileIndex ) ;
2012-05-05 18:33:26 +02:00
if ( ! line . empty ( ) )
2013-08-30 06:27:46 +02:00
_back - > isExpandedMacro ( expandedMacro ) ;
2012-05-05 18:33:26 +02:00
}
CurrentToken . clear ( ) ;
continue ;
}
if ( ch = = ' . ' & &
2014-05-31 18:02:19 +02:00
! CurrentToken . empty ( ) & &
2012-05-05 18:33:26 +02:00
std : : isdigit ( CurrentToken [ 0 ] ) ) {
// Don't separate doubles "5.4"
2012-12-27 11:51:12 +01:00
} else if ( std : : strchr ( " +- " , ch ) & &
2012-05-05 18:33:26 +02:00
CurrentToken . length ( ) > 0 & &
std : : isdigit ( CurrentToken [ 0 ] ) & &
( CurrentToken [ CurrentToken . length ( ) - 1 ] = = ' e ' | |
CurrentToken [ CurrentToken . length ( ) - 1 ] = = ' E ' ) & &
! MathLib : : isHex ( CurrentToken ) ) {
// Don't separate doubles "4.2e+10"
} else if ( CurrentToken . empty ( ) & & ch = = ' . ' & & std : : isdigit ( code . peek ( ) ) ) {
// tokenize .125 into 0.125
CurrentToken = " 0 " ;
2012-12-27 11:51:12 +01:00
} else if ( std : : strchr ( " +-*/%&|^?!=<>[]() { } ; : , . ~ \ n " , ch)) {
2012-05-05 18:33:26 +02:00
if ( CurrentToken = = " #file " ) {
// Handle this where strings are handled
continue ;
2012-12-27 16:52:31 +01:00
} else if ( CurrentToken = = " #line " ) {
// Read to end of line
std : : string line ;
std : : getline ( code , line ) ;
2013-01-23 18:27:28 +01:00
unsigned int row ;
2014-01-28 17:15:07 +01:00
std : : istringstream fiss ( line ) ;
if ( fiss > > row ) {
// Update the current line number
2013-01-23 18:27:28 +01:00
lineno = row ;
2014-01-28 17:15:07 +01:00
2014-01-29 03:16:29 +01:00
std : : string line2 ;
if ( std : : getline ( fiss , line2 ) & & line2 . length ( ) > 4U ) {
2014-01-28 17:15:07 +01:00
// _"file_name" -> file_name
2014-01-29 03:16:29 +01:00
line2 = line2 . substr ( 2 , line2 . length ( ) - 3 ) ;
2014-01-28 17:15:07 +01:00
// Update the current file
2014-01-29 03:16:29 +01:00
FileIndex = appendFileIfNew ( line2 ) ;
2014-01-28 17:15:07 +01:00
}
} else
+ + lineno ;
2012-12-27 16:52:31 +01:00
CurrentToken . clear ( ) ;
continue ;
2012-05-05 18:33:26 +02:00
} else if ( CurrentToken = = " #endfile " ) {
if ( lineNumbers . empty ( ) | | fileIndexes . empty ( ) ) { // error
deallocateTokens ( ) ;
return false ;
}
lineno = lineNumbers . top ( ) ;
lineNumbers . pop ( ) ;
FileIndex = fileIndexes . top ( ) ;
fileIndexes . pop ( ) ;
CurrentToken . clear ( ) ;
continue ;
}
2014-03-09 17:54:49 +01:00
addtoken ( CurrentToken , lineno , FileIndex , true ) ;
2014-01-03 18:53:37 +01:00
if ( ! CurrentToken . empty ( ) ) {
2013-08-30 06:27:46 +02:00
_back - > isExpandedMacro ( expandedMacro ) ;
2014-01-03 18:53:37 +01:00
expandedMacro = false ;
}
2012-05-05 18:33:26 +02:00
CurrentToken . clear ( ) ;
if ( ch = = ' \n ' ) {
2013-07-24 13:06:59 +02:00
if ( _settings - > terminated ( ) )
return false ;
2012-05-05 18:33:26 +02:00
+ + lineno ;
continue ;
} else if ( ch = = ' ' ) {
continue ;
}
CurrentToken + = ch ;
// Add "++", "--", ">>" or ... token
2012-12-27 11:51:12 +01:00
if ( std : : strchr ( " +-<>=:&| " , ch ) & & ( code . peek ( ) = = ch ) )
2012-05-05 18:33:26 +02:00
CurrentToken + = ( char ) code . get ( ) ;
2014-03-09 17:54:49 +01:00
addtoken ( CurrentToken , lineno , FileIndex ) ;
2013-08-30 06:27:46 +02:00
_back - > isExpandedMacro ( expandedMacro ) ;
2012-05-05 18:33:26 +02:00
CurrentToken . clear ( ) ;
2014-01-03 18:53:37 +01:00
expandedMacro = false ;
2012-05-05 18:33:26 +02:00
continue ;
}
CurrentToken + = ch ;
}
2014-03-09 17:54:49 +01:00
addtoken ( CurrentToken , lineno , FileIndex , true ) ;
2012-05-05 18:33:26 +02:00
if ( ! CurrentToken . empty ( ) )
2013-08-30 06:27:46 +02:00
_back - > isExpandedMacro ( expandedMacro ) ;
2014-05-20 21:55:08 +02:00
Token : : assignProgressValues ( _front ) ;
2012-05-05 18:33:26 +02:00
2014-07-07 21:25:30 +02:00
for ( std : : size_t i = 1 ; i < _files . size ( ) ; i + + )
2012-05-05 18:33:26 +02:00
_files [ i ] = Path : : getRelativePath ( _files [ i ] , _settings - > _basePaths ) ;
return true ;
}
2014-09-02 18:05:02 +02:00
//---------------------------------------------------------------------------
unsigned long long TokenList : : calculateChecksum ( ) const
{
unsigned long long checksum = 0 ;
for ( const Token * tok = front ( ) ; tok ; tok = tok - > next ( ) ) {
unsigned int subchecksum1 = tok - > flags ( ) + tok - > varId ( ) + static_cast < unsigned int > ( tok - > type ( ) ) ;
unsigned int subchecksum2 = 0 ;
for ( std : : size_t i = 0 ; i < tok - > str ( ) . size ( ) ; i + + )
subchecksum2 + = ( unsigned int ) tok - > str ( ) [ i ] ;
if ( ! tok - > originalName ( ) . empty ( ) ) {
for ( std : : size_t i = 0 ; i < tok - > originalName ( ) . size ( ) ; i + + )
subchecksum2 + = ( unsigned int ) tok - > originalName ( ) [ i ] ;
}
checksum ^ = ( ( static_cast < unsigned long long > ( subchecksum1 ) < < 32 ) | subchecksum2 ) ;
2014-09-03 23:09:40 +02:00
bool bit1 = ( checksum & 1 ) ! = 0 ;
checksum > > = 1 ;
if ( bit1 )
checksum | = ( 1ULL < < 63 ) ;
2014-09-02 18:05:02 +02:00
}
return checksum ;
}
2012-05-05 18:33:26 +02:00
//---------------------------------------------------------------------------
2014-06-04 22:33:08 +02:00
struct AST_state {
std : : stack < Token * > op ;
unsigned int depth ;
2014-07-31 23:14:44 +02:00
unsigned int inArrayAssignment ;
2014-06-04 22:33:08 +02:00
bool cpp ;
2014-07-31 23:14:44 +02:00
AST_state ( bool cpp_ ) : depth ( 0 ) , inArrayAssignment ( 0 ) , cpp ( cpp_ ) { }
2014-06-04 22:33:08 +02:00
} ;
2013-11-11 16:39:34 +01:00
static bool iscast ( const Token * tok )
{
if ( ! Token : : Match ( tok , " ( %var% " ) )
return false ;
2014-09-12 06:43:52 +02:00
if ( tok - > previous ( ) & & tok - > previous ( ) - > isName ( ) & & tok - > previous ( ) - > str ( ) ! = " return " )
2014-05-17 12:09:32 +02:00
return false ;
2014-05-04 18:36:04 +02:00
if ( Token : : Match ( tok , " ( (| typeof ( " ) & & Token : : Match ( tok - > link ( ) , " ) %num% " ) )
return true ;
2014-05-24 19:04:47 +02:00
bool type = false ;
2013-11-11 16:39:34 +01:00
for ( const Token * tok2 = tok - > next ( ) ; tok2 ; tok2 = tok2 - > next ( ) ) {
2014-05-24 20:21:08 +02:00
while ( tok2 - > link ( ) & & Token : : Match ( tok2 , " (|[|< " ) )
2014-05-24 11:28:43 +02:00
tok2 = tok2 - > link ( ) - > next ( ) ;
2014-04-15 15:46:26 +02:00
if ( tok2 - > str ( ) = = " ) " )
2014-06-04 18:08:51 +02:00
return type | | tok2 - > strAt ( - 1 ) = = " * " | |
2014-04-15 15:46:26 +02:00
( Token : : Match ( tok2 , " ) %any% " ) & &
2014-05-24 11:28:43 +02:00
( tok2 - > strAt ( 1 ) = = " & " | | ( ! tok2 - > next ( ) - > isOp ( ) & & ! Token : : Match ( tok2 - > next ( ) , " [[]);,?:.] " ) ) ) ) ;
2013-12-25 21:19:28 +01:00
if ( ! Token : : Match ( tok2 , " %var%|*|&|:: " ) )
2014-04-15 15:46:26 +02:00
return false ;
2014-05-24 11:28:43 +02:00
if ( tok2 - > isStandardType ( ) )
2014-05-24 19:04:47 +02:00
type = true ;
2013-11-11 16:39:34 +01:00
}
return false ;
}
2014-06-04 22:33:08 +02:00
static void compileUnaryOp ( Token * & tok , AST_state & state , void ( * f ) ( Token * & tok , AST_state & state ) )
2012-12-15 20:21:09 +01:00
{
2013-11-04 11:26:16 +01:00
Token * unaryop = tok ;
2014-05-17 12:09:32 +02:00
if ( f ) {
tok = tok - > next ( ) ;
2014-06-04 22:33:08 +02:00
state . depth + + ;
if ( tok & & state . depth < = AST_MAX_DEPTH )
f ( tok , state ) ;
state . depth - - ;
2014-05-17 12:09:32 +02:00
}
2013-11-04 11:26:16 +01:00
2014-06-04 22:33:08 +02:00
if ( ! state . op . empty ( ) ) {
unaryop - > astOperand1 ( state . op . top ( ) ) ;
state . op . pop ( ) ;
2013-11-04 11:26:16 +01:00
}
2014-06-04 22:33:08 +02:00
state . op . push ( unaryop ) ;
2013-11-04 11:26:16 +01:00
}
2014-06-04 22:33:08 +02:00
static void compileBinOp ( Token * & tok , AST_state & state , void ( * f ) ( Token * & tok , AST_state & state ) )
2013-11-04 11:26:16 +01:00
{
Token * binop = tok ;
2014-05-17 12:09:32 +02:00
if ( f ) {
tok = tok - > next ( ) ;
2014-06-04 22:33:08 +02:00
state . depth + + ;
if ( tok & & state . depth < = AST_MAX_DEPTH )
f ( tok , state ) ;
state . depth - - ;
2014-05-17 12:09:32 +02:00
}
2014-05-03 12:08:42 +02:00
2013-11-04 11:26:16 +01:00
// TODO: Should we check if op is empty.
// * Is it better to add assertion that it isn't?
// * Write debug warning if it's empty?
2014-06-04 22:33:08 +02:00
if ( ! state . op . empty ( ) ) {
binop - > astOperand2 ( state . op . top ( ) ) ;
state . op . pop ( ) ;
2013-11-04 11:26:16 +01:00
}
2014-06-04 22:33:08 +02:00
if ( ! state . op . empty ( ) ) {
binop - > astOperand1 ( state . op . top ( ) ) ;
state . op . pop ( ) ;
2013-11-04 11:26:16 +01:00
}
2014-06-04 22:33:08 +02:00
state . op . push ( binop ) ;
2013-11-04 11:26:16 +01:00
}
2013-03-02 15:49:48 +01:00
2014-06-04 22:33:08 +02:00
static void compileExpression ( Token * & tok , AST_state & state ) ;
2013-11-04 11:26:16 +01:00
2014-06-04 22:33:08 +02:00
static void compileTerm ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2013-11-11 16:39:34 +01:00
if ( ! tok )
return ;
2013-11-20 05:57:56 +01:00
if ( Token : : Match ( tok , " L %str%|%char% " ) )
tok = tok - > next ( ) ;
2014-07-31 23:14:44 +02:00
if ( state . inArrayAssignment & & tok - > str ( ) = = " . " & & ( tok - > strAt ( - 1 ) = = " , " | | tok - > strAt ( - 1 ) = = " { " ) ) // Jump over . in C style struct initialization
tok = tok - > next ( ) ;
2013-11-04 11:26:16 +01:00
if ( tok - > isLiteral ( ) ) {
2014-06-04 22:33:08 +02:00
state . op . push ( tok ) ;
2013-11-04 11:26:16 +01:00
tok = tok - > next ( ) ;
2014-06-26 09:03:02 +02:00
} else if ( tok - > isName ( ) & & tok - > str ( ) ! = " case " ) {
if ( tok - > str ( ) = = " return " ) {
compileUnaryOp ( tok , state , compileExpression ) ;
state . op . pop ( ) ;
} else if ( ! state . cpp | | ! Token : : Match ( tok , " new|delete %var%|*|&|::|(|[ " ) ) {
while ( tok - > next ( ) & & tok - > next ( ) - > isName ( ) )
tok = tok - > next ( ) ;
state . op . push ( tok ) ;
if ( tok - > next ( ) & & tok - > linkAt ( 1 ) & & Token : : Match ( tok , " %var% < " ) )
tok = tok - > linkAt ( 1 ) ;
2013-11-04 11:26:16 +01:00
tok = tok - > next ( ) ;
2014-06-26 09:03:02 +02:00
}
2014-02-05 06:05:48 +01:00
} else if ( tok - > str ( ) = = " { " ) {
2014-07-31 23:14:44 +02:00
if ( ! state . inArrayAssignment & & tok - > strAt ( - 1 ) ! = " = " ) {
state . op . push ( tok ) ;
tok = tok - > link ( ) - > next ( ) ;
} else {
if ( tok - > link ( ) ! = tok - > next ( ) ) {
state . inArrayAssignment + + ;
compileUnaryOp ( tok , state , compileExpression ) ;
state . inArrayAssignment - - ;
} else {
state . op . push ( tok ) ;
}
}
2012-12-15 20:21:09 +01:00
}
2013-11-04 11:26:16 +01:00
}
2012-12-15 20:21:09 +01:00
2014-06-04 22:33:08 +02:00
static void compileScope ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileTerm ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
if ( tok - > str ( ) = = " :: " ) {
2014-06-04 18:08:51 +02:00
Token * binop = tok ;
tok = tok - > next ( ) ;
if ( tok & & tok - > str ( ) = = " ~ " ) // Jump over ~ of destructor definition
tok = tok - > next ( ) ;
if ( tok )
2014-06-04 22:33:08 +02:00
compileTerm ( tok , state ) ;
2014-06-04 18:08:51 +02:00
if ( binop - > previous ( ) & & binop - > previous ( ) - > isName ( ) )
2014-06-04 22:33:08 +02:00
compileBinOp ( binop , state , nullptr ) ;
2014-02-25 06:36:10 +01:00
else
2014-06-04 22:33:08 +02:00
compileUnaryOp ( binop , state , nullptr ) ;
2013-11-04 11:26:16 +01:00
} else break ;
2012-12-16 10:06:55 +01:00
}
2013-11-04 11:26:16 +01:00
}
2014-06-04 22:33:08 +02:00
static bool isPrefixUnary ( const Token * tok , bool cpp )
2013-11-25 20:58:40 +01:00
{
2014-05-17 12:09:32 +02:00
if ( ! tok - > previous ( )
2014-06-04 22:33:08 +02:00
| | ( ( Token : : Match ( tok - > previous ( ) , " (|[|{|%op%|;|}|?|:|,|.|return|:: " ) | | ( cpp & & tok - > strAt ( - 1 ) = = " throw " ) )
2014-05-17 12:09:32 +02:00
& & ( tok - > previous ( ) - > type ( ) ! = Token : : eIncDecOp | | tok - > type ( ) = = Token : : eIncDecOp ) ) )
return true ;
return tok - > strAt ( - 1 ) = = " ) " & & iscast ( tok - > linkAt ( - 1 ) ) ;
2014-05-08 06:48:53 +02:00
}
2014-06-04 22:33:08 +02:00
static void compilePrecedence2 ( Token * & tok , AST_state & state )
2014-05-08 06:48:53 +02:00
{
2014-06-04 22:33:08 +02:00
compileScope ( tok , state ) ;
2014-05-08 06:48:53 +02:00
while ( tok ) {
2014-06-04 22:33:08 +02:00
if ( tok - > type ( ) = = Token : : eIncDecOp & & ! isPrefixUnary ( tok , state . cpp ) ) {
compileUnaryOp ( tok , state , compileScope ) ;
2014-05-19 21:54:59 +02:00
} else if ( tok - > str ( ) = = " . " & & tok - > strAt ( 1 ) ! = " * " ) {
2014-06-04 18:08:51 +02:00
if ( tok - > strAt ( 1 ) = = " . " ) {
2014-06-04 22:33:08 +02:00
state . op . push ( tok ) ;
2014-06-04 18:08:51 +02:00
tok = tok - > tokAt ( 3 ) ;
break ;
} else
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileScope ) ;
2014-05-08 06:48:53 +02:00
} else if ( tok - > str ( ) = = " [ " ) {
2014-09-01 17:01:05 +02:00
if ( state . cpp & & isPrefixUnary ( tok , state . cpp ) & & tok - > link ( ) - > strAt ( 1 ) = = " ( " ) { // Lambda
2014-05-25 19:48:31 +02:00
// What we do here:
// - Nest the round bracket under the square bracket.
// - Nest what follows the lambda (if anything) with the lambda opening [
// - Compile the content of the lambda function as separate tree
Token * squareBracket = tok ;
Token * roundBracket = squareBracket - > link ( ) - > next ( ) ;
Token * curlyBracket = Token : : findsimplematch ( roundBracket - > link ( ) - > next ( ) , " { " ) ;
2014-07-31 23:14:44 +02:00
if ( ! curlyBracket )
break ;
2014-05-25 19:48:31 +02:00
tok = curlyBracket - > next ( ) ;
2014-06-04 22:33:08 +02:00
compileExpression ( tok , state ) ;
state . op . push ( roundBracket ) ;
compileUnaryOp ( squareBracket , state , nullptr ) ;
2014-05-25 19:48:31 +02:00
tok = curlyBracket - > link ( ) - > next ( ) ;
} else {
Token * tok2 = tok ;
2014-06-04 18:08:51 +02:00
if ( tok - > strAt ( 1 ) ! = " ] " )
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileExpression ) ;
2014-06-04 18:08:51 +02:00
else
2014-06-04 22:33:08 +02:00
compileUnaryOp ( tok , state , compileExpression ) ;
2014-05-25 19:48:31 +02:00
tok = tok2 - > link ( ) - > next ( ) ;
}
2014-05-17 12:09:32 +02:00
} else if ( tok - > str ( ) = = " ( " & & ( ! iscast ( tok ) | | Token : : Match ( tok - > previous ( ) , " if|while|for|switch|catch " ) ) ) {
Token * tok2 = tok ;
2014-04-29 06:09:26 +02:00
tok = tok - > next ( ) ;
2014-06-04 22:33:08 +02:00
bool opPrevTopSquare = ! state . op . empty ( ) & & state . op . top ( ) & & state . op . top ( ) - > str ( ) = = " [ " ;
std : : size_t oldOpSize = state . op . size ( ) ;
compileExpression ( tok , state ) ;
2014-05-17 12:09:32 +02:00
tok = tok2 ;
2014-06-04 22:33:08 +02:00
if ( ( tok - > previous ( ) & & tok - > previous ( ) - > isName ( ) & & ( tok - > strAt ( - 1 ) ! = " return " & & ( ! state . cpp | | ! Token : : Match ( tok - > previous ( ) , " throw|delete " ) ) ) )
| | ( tok - > strAt ( - 1 ) = = " ] " & & ( ! state . cpp | | ! Token : : Match ( tok - > linkAt ( - 1 ) - > previous ( ) , " new|delete " ) ) )
2014-05-17 12:09:32 +02:00
| | ( tok - > strAt ( - 1 ) = = " > " & & tok - > linkAt ( - 1 ) )
2014-05-25 19:48:31 +02:00
| | ( tok - > strAt ( - 1 ) = = " ) " & & ! iscast ( tok - > linkAt ( - 1 ) ) ) // Don't treat brackets to clarify precedence as function calls
| | ( tok - > strAt ( - 1 ) = = " } " & & opPrevTopSquare ) ) {
2014-07-10 09:23:14 +02:00
const bool operandInside = oldOpSize < state . op . size ( ) ;
2014-06-04 18:08:51 +02:00
if ( operandInside )
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , nullptr ) ;
2014-05-17 12:09:32 +02:00
else
2014-06-04 22:33:08 +02:00
compileUnaryOp ( tok , state , nullptr ) ;
2014-05-17 12:09:32 +02:00
}
tok = tok - > link ( ) - > next ( ) ;
2013-11-25 20:58:40 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compilePrecedence3 ( Token * & tok , AST_state & state )
2014-05-17 12:09:32 +02:00
{
2014-06-04 22:33:08 +02:00
compilePrecedence2 ( tok , state ) ;
2014-05-17 12:09:32 +02:00
while ( tok ) {
if ( ( Token : : Match ( tok , " [+-!~*&] " ) | | tok - > type ( ) = = Token : : eIncDecOp ) & &
2014-06-04 22:33:08 +02:00
isPrefixUnary ( tok , state . cpp ) ) {
2014-06-04 18:08:51 +02:00
if ( Token : : Match ( tok , " * [*,)] " ) ) {
Token * tok2 = tok ;
while ( tok2 - > next ( ) & & tok2 - > str ( ) = = " * " )
tok2 = tok2 - > next ( ) ;
if ( Token : : Match ( tok2 , " [>),] " ) ) {
tok = tok2 ;
continue ;
}
}
2014-06-04 22:33:08 +02:00
compileUnaryOp ( tok , state , compilePrecedence3 ) ;
2014-05-17 12:09:32 +02:00
} else if ( tok - > str ( ) = = " ( " & & iscast ( tok ) ) {
Token * tok2 = tok ;
tok = tok - > link ( ) - > next ( ) ;
2014-06-04 22:33:08 +02:00
compilePrecedence3 ( tok , state ) ;
compileUnaryOp ( tok2 , state , nullptr ) ;
2014-06-26 09:03:02 +02:00
} else if ( state . cpp & & Token : : Match ( tok , " new %var%|::|( " ) ) {
2014-06-04 18:08:51 +02:00
Token * tok2 = tok ;
tok = tok - > next ( ) ;
2014-08-23 13:21:36 +02:00
if ( tok - > str ( ) = = " ( " & & Token : : Match ( tok - > link ( ) , " ) %type% " ) )
tok = tok - > link ( ) - > next ( ) ;
2014-06-04 22:33:08 +02:00
state . op . push ( tok ) ;
2014-06-04 18:08:51 +02:00
while ( Token : : Match ( tok , " %var%|*|&|<|[ " ) ) {
if ( tok - > link ( ) )
tok = tok - > link ( ) ;
tok = tok - > next ( ) ;
}
2014-06-04 22:33:08 +02:00
compileUnaryOp ( tok2 , state , nullptr ) ;
2014-06-26 09:03:02 +02:00
} else if ( state . cpp & & Token : : Match ( tok , " delete %var%|*|&|::|(|[ " ) ) {
2014-06-04 18:08:51 +02:00
Token * tok2 = tok ;
tok = tok - > next ( ) ;
if ( tok - > str ( ) = = " [ " )
tok = tok - > link ( ) - > next ( ) ;
2014-06-04 22:33:08 +02:00
compilePrecedence3 ( tok , state ) ;
compileUnaryOp ( tok2 , state , nullptr ) ;
2014-05-17 12:09:32 +02:00
}
2014-06-04 18:08:51 +02:00
// TODO: Handle sizeof
2014-05-17 12:09:32 +02:00
else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compilePointerToElem ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compilePrecedence3 ( tok , state ) ;
2014-05-19 21:54:59 +02:00
while ( tok ) {
if ( Token : : simpleMatch ( tok , " . * " ) ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compilePrecedence3 ) ;
2014-05-19 21:54:59 +02:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileMulDiv ( Token * & tok , AST_state & state )
2014-05-19 21:54:59 +02:00
{
2014-06-04 22:33:08 +02:00
compilePointerToElem ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
2014-05-24 19:04:47 +02:00
if ( Token : : Match ( tok , " [/%] " ) | | ( tok - > str ( ) = = " * " & & ! tok - > astOperand1 ( ) ) ) {
2014-04-26 13:32:08 +02:00
if ( Token : : Match ( tok , " * [*,)] " ) ) {
2014-05-17 12:09:32 +02:00
Token * tok2 = tok ;
while ( tok2 - > next ( ) & & tok2 - > str ( ) = = " * " )
tok2 = tok2 - > next ( ) ;
2014-06-04 18:08:51 +02:00
if ( Token : : Match ( tok2 , " [>),] " ) ) {
tok = tok2 ;
2014-05-17 12:09:32 +02:00
break ;
}
2014-04-26 13:32:08 +02:00
}
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compilePointerToElem ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileAddSub ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileMulDiv ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
2014-05-24 19:04:47 +02:00
if ( Token : : Match ( tok , " +|- " ) & & ! tok - > astOperand1 ( ) ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileMulDiv ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileShift ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileAddSub ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
if ( Token : : Match ( tok , " <<|>> " ) ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileAddSub ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileRelComp ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileShift ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
2014-06-04 18:08:51 +02:00
if ( Token : : Match ( tok , " <|<=|>=|> " ) & & ! tok - > link ( ) ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileShift ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2012-12-16 10:06:55 +01:00
2014-06-04 22:33:08 +02:00
static void compileEqComp ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileRelComp ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
if ( Token : : Match ( tok , " ==|!= " ) ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileRelComp ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileAnd ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileEqComp ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
2014-05-24 19:04:47 +02:00
if ( tok - > str ( ) = = " & " & & ! tok - > astOperand1 ( ) ) {
2014-06-04 18:08:51 +02:00
Token * tok2 = tok - > next ( ) ;
if ( tok2 - > str ( ) = = " & " )
tok2 = tok2 - > next ( ) ;
2014-06-04 22:33:08 +02:00
if ( state . cpp & & ( tok2 - > str ( ) = = " , " | | tok2 - > str ( ) = = " ) " ) ) {
2014-06-04 18:08:51 +02:00
tok = tok2 ;
break ; // rValue reference
}
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileEqComp ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileXor ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileAnd ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
if ( tok - > str ( ) = = " ^ " ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileAnd ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileOr ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileXor ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
if ( tok - > str ( ) = = " | " ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileXor ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileLogicAnd ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileOr ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
if ( tok - > str ( ) = = " && " ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileOr ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileLogicOr ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileLogicAnd ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
if ( tok - > str ( ) = = " || " ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileLogicAnd ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileAssignTernary ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileLogicOr ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
2014-05-11 13:39:28 +02:00
// TODO: http://en.cppreference.com/w/cpp/language/operator_precedence says:
// "The expression in the middle of the conditional operator (between ? and :) is parsed as if parenthesized: its precedence relative to ?: is ignored."
if ( tok - > isAssignmentOp ( ) | | Token : : Match ( tok , " [?:] " ) ) {
2014-05-19 21:54:59 +02:00
if ( tok - > str ( ) = = " ? " & & tok - > strAt ( 1 ) = = " : " ) {
2014-06-04 22:33:08 +02:00
state . op . push ( 0 ) ;
2014-05-19 21:54:59 +02:00
}
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileAssignTernary ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileComma ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
compileAssignTernary ( tok , state ) ;
2013-11-04 11:26:16 +01:00
while ( tok ) {
if ( tok - > str ( ) = = " , " ) {
2014-06-04 22:33:08 +02:00
compileBinOp ( tok , state , compileAssignTernary ) ;
2013-11-04 11:26:16 +01:00
} else break ;
}
}
2014-06-04 22:33:08 +02:00
static void compileExpression ( Token * & tok , AST_state & state )
2013-11-04 11:26:16 +01:00
{
2014-06-04 22:33:08 +02:00
if ( state . depth > AST_MAX_DEPTH )
2014-04-13 12:47:54 +02:00
return ; // ticket #5592
2013-11-10 15:51:33 +01:00
if ( tok )
2014-06-04 22:33:08 +02:00
compileComma ( tok , state ) ;
2013-11-04 11:26:16 +01:00
}
2014-06-04 22:33:08 +02:00
static Token * createAstAtToken ( Token * tok , bool cpp )
2014-01-27 06:18:42 +01:00
{
if ( Token : : simpleMatch ( tok , " for ( " ) ) {
Token * tok2 = tok - > tokAt ( 2 ) ;
2014-02-16 11:47:52 +01:00
Token * init1 = nullptr ;
2014-01-27 06:18:42 +01:00
const Token * const endPar = tok - > next ( ) - > link ( ) ;
while ( tok2 & & tok2 ! = endPar & & tok2 - > str ( ) ! = " ; " ) {
if ( tok2 - > str ( ) = = " < " & & tok2 - > link ( ) ) {
tok2 = tok2 - > link ( ) ;
if ( ! tok2 )
break ;
2014-05-11 13:37:53 +02:00
} else if ( Token : : Match ( tok2 , " %var% %op%|(|[|.|:|:: " ) | | Token : : Match ( tok2 - > previous ( ) , " [( ; { } ] % cop % | ( " )) {
2014-01-27 06:18:42 +01:00
init1 = tok2 ;
2014-06-04 22:33:08 +02:00
AST_state state1 ( cpp ) ;
compileExpression ( tok2 , state1 ) ;
2014-01-27 06:18:42 +01:00
if ( tok2 - > str ( ) = = " ; " | | tok2 - > str ( ) = = " ) " )
break ;
init1 = 0 ;
2014-01-15 17:32:14 +01:00
}
2014-01-27 06:18:42 +01:00
tok2 = tok2 - > next ( ) ;
}
if ( ! tok2 | | tok2 - > str ( ) ! = " ; " ) {
if ( tok2 = = endPar & & init1 ) {
tok - > next ( ) - > astOperand2 ( init1 ) ;
tok - > next ( ) - > astOperand1 ( tok ) ;
2014-01-15 17:32:14 +01:00
}
2014-01-27 06:18:42 +01:00
return tok2 ;
}
2014-01-15 17:32:14 +01:00
2014-01-27 06:18:42 +01:00
Token * const init = init1 ? init1 : tok2 ;
2014-01-15 17:32:14 +01:00
2014-01-27 06:18:42 +01:00
Token * const semicolon1 = tok2 ;
tok2 = tok2 - > next ( ) ;
2014-06-04 22:33:08 +02:00
AST_state state2 ( cpp ) ;
compileExpression ( tok2 , state2 ) ;
2014-01-27 06:18:42 +01:00
Token * const semicolon2 = tok2 ;
tok2 = tok2 - > next ( ) ;
2014-06-04 22:33:08 +02:00
AST_state state3 ( cpp ) ;
compileExpression ( tok2 , state3 ) ;
2014-01-15 17:32:14 +01:00
2014-01-27 06:18:42 +01:00
if ( init ! = semicolon1 )
semicolon1 - > astOperand1 ( const_cast < Token * > ( init - > astTop ( ) ) ) ;
tok2 = semicolon1 - > next ( ) ;
while ( tok2 ! = semicolon2 & & ! tok2 - > isName ( ) & & ! tok2 - > isNumber ( ) )
2014-01-15 17:32:14 +01:00
tok2 = tok2 - > next ( ) ;
2014-01-27 06:18:42 +01:00
if ( tok2 ! = semicolon2 )
semicolon2 - > astOperand1 ( const_cast < Token * > ( tok2 - > astTop ( ) ) ) ;
tok2 = tok - > linkAt ( 1 ) ;
while ( tok2 ! = semicolon2 & & ! tok2 - > isName ( ) & & ! tok2 - > isNumber ( ) )
tok2 = tok2 - > previous ( ) ;
if ( tok2 ! = semicolon2 )
semicolon2 - > astOperand2 ( const_cast < Token * > ( tok2 - > astTop ( ) ) ) ;
semicolon1 - > astOperand2 ( semicolon2 ) ;
tok - > next ( ) - > astOperand1 ( tok ) ;
tok - > next ( ) - > astOperand2 ( semicolon1 ) ;
return tok - > linkAt ( 1 ) ;
}
2014-01-28 06:11:53 +01:00
if ( Token : : simpleMatch ( tok , " ( { " ) )
return tok ;
2014-02-17 17:37:39 +01:00
if ( Token : : Match ( tok , " %type% < " ) & & Token : : Match ( tok - > linkAt ( 1 ) , " > !!( " ) )
return tok - > linkAt ( 1 ) ;
2014-06-26 17:31:57 +02:00
if ( tok - > str ( ) = = " return " | | ! tok - > previous ( ) | | Token : : Match ( tok , " %var% %op%|(|[|.|:: " ) | | Token : : Match ( tok - > previous ( ) , " [;{}] %cop%|++|--|( !!{ " ) ) {
2014-01-27 06:18:42 +01:00
Token * const tok1 = tok ;
2014-06-04 22:33:08 +02:00
AST_state state ( cpp ) ;
compileExpression ( tok , state ) ;
2014-01-27 06:18:42 +01:00
Token * const endToken = tok ;
2014-01-28 06:11:53 +01:00
if ( endToken = = tok1 )
return tok1 ;
2014-01-27 06:18:42 +01:00
// Compile inner expressions inside inner ({..})
2014-01-28 06:11:53 +01:00
for ( tok = tok1 - > next ( ) ; tok & & tok ! = endToken ; tok = tok ? tok - > next ( ) : NULL ) {
2014-01-27 06:18:42 +01:00
if ( ! Token : : simpleMatch ( tok , " ( { " ) )
continue ;
2014-01-28 06:11:53 +01:00
if ( tok - > next ( ) = = endToken )
break ;
2014-08-25 19:10:16 +02:00
if ( Token : : simpleMatch ( tok , " ( { . " ) )
break ;
2014-01-27 06:18:42 +01:00
const Token * const endToken2 = tok - > linkAt ( 1 ) ;
for ( ; tok & & tok ! = endToken & & tok ! = endToken2 ; tok = tok ? tok - > next ( ) : NULL )
2014-06-04 22:33:08 +02:00
tok = createAstAtToken ( tok , cpp ) ;
2012-12-15 20:21:09 +01:00
}
2014-01-27 06:18:42 +01:00
return endToken ? endToken - > previous ( ) : NULL ;
}
return tok ;
}
void TokenList : : createAst ( )
{
for ( Token * tok = _front ; tok ; tok = tok ? tok - > next ( ) : NULL ) {
2014-06-04 22:33:08 +02:00
tok = createAstAtToken ( tok , isCPP ( ) ) ;
2012-12-15 20:21:09 +01:00
}
}
2012-05-05 18:33:26 +02:00
const std : : string & TokenList : : file ( const Token * tok ) const
{
return _files . at ( tok - > fileIndex ( ) ) ;
}
std : : string TokenList : : fileLine ( const Token * tok ) const
{
2012-05-06 10:17:15 +02:00
return ErrorLogger : : ErrorMessage : : FileLocation ( tok , this ) . stringify ( ) ;
2012-05-05 18:33:26 +02:00
}