2010-01-16 08:47:46 +01:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2010-04-13 21:23:17 +02:00
* Copyright ( C ) 2007 - 2010 Daniel Marjamäki and Cppcheck team .
2010-01-16 08:47:46 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
# include "checkclass.h"
# include "tokenize.h"
# include "token.h"
# include "errorlogger.h"
2010-11-13 07:31:56 +01:00
# include "symboldatabase.h"
2010-01-16 08:47:46 +01:00
# include <locale>
# include <cstring>
# include <string>
# include <sstream>
# include <algorithm>
//---------------------------------------------------------------------------
// Register CheckClass..
namespace
{
CheckClass instance ;
}
2010-07-26 16:46:37 +02:00
//---------------------------------------------------------------------------
CheckClass : : CheckClass ( const Tokenizer * tokenizer , const Settings * settings , ErrorLogger * errorLogger )
2010-08-07 12:41:11 +02:00
: Check ( tokenizer , settings , errorLogger ) ,
2010-12-07 07:08:49 +01:00
symbolDatabase ( NULL )
2010-07-26 16:46:37 +02:00
{
2010-08-07 12:41:11 +02:00
}
2010-11-13 07:31:56 +01:00
void CheckClass : : createSymbolDatabase ( )
2010-07-14 19:00:52 +02:00
{
2010-11-13 07:31:56 +01:00
// Multiple calls => bail out
if ( symbolDatabase )
return ;
2010-07-14 19:00:52 +02:00
2010-12-07 07:08:49 +01:00
symbolDatabase = _tokenizer - > getSymbolDatabase ( ) ;
2010-07-14 19:00:52 +02:00
}
2010-01-16 08:47:46 +01:00
2010-07-26 16:46:37 +02:00
//---------------------------------------------------------------------------
// ClassCheck: Check that all class constructors are ok.
//---------------------------------------------------------------------------
2010-07-15 10:16:16 +02:00
2010-01-16 08:47:46 +01:00
void CheckClass : : constructors ( )
{
2010-04-21 08:38:25 +02:00
if ( ! _settings - > _checkCodingStyle )
return ;
2010-08-07 12:41:11 +02:00
createSymbolDatabase ( ) ;
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : SpaceInfo * > : : iterator i ;
2010-07-26 16:46:37 +02:00
2010-11-13 07:31:56 +01:00
for ( i = symbolDatabase - > spaceInfoList . begin ( ) ; i ! = symbolDatabase - > spaceInfoList . end ( ) ; + + i )
2010-01-16 08:47:46 +01:00
{
2010-11-13 07:31:56 +01:00
SymbolDatabase : : SpaceInfo * info = * i ;
2010-01-16 08:47:46 +01:00
2010-11-20 07:26:50 +01:00
// only check classes and structures
if ( ! info - > isClassOrStruct ( ) )
2010-08-09 17:50:26 +02:00
continue ;
2010-07-26 16:46:37 +02:00
// There are no constructors.
if ( info - > numConstructors = = 0 )
2010-07-15 10:16:16 +02:00
{
2010-07-26 16:46:37 +02:00
// If there is a private variable, there should be a constructor..
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : Var > : : const_iterator var ;
2010-08-11 22:36:04 +02:00
for ( var = info - > varlist . begin ( ) ; var ! = info - > varlist . end ( ) ; + + var )
2010-07-14 19:00:52 +02:00
{
2010-11-13 07:31:56 +01:00
if ( var - > access = = SymbolDatabase : : Private & & ! var - > isClass & & ! var - > isStatic )
2010-07-15 10:16:16 +02:00
{
2010-07-26 16:46:37 +02:00
noConstructorError ( info - > classDef , info - > className , info - > classDef - > str ( ) = = " struct " ) ;
break ;
2010-01-16 08:47:46 +01:00
}
}
2010-07-26 16:46:37 +02:00
}
2010-01-16 08:47:46 +01:00
2010-11-20 07:26:50 +01:00
std : : list < SymbolDatabase : : Func > : : const_iterator func ;
2010-01-16 08:47:46 +01:00
2010-11-20 07:26:50 +01:00
for ( func = info - > functionList . begin ( ) ; func ! = info - > functionList . end ( ) ; + + func )
2010-07-26 16:46:37 +02:00
{
2010-12-04 17:47:00 +01:00
if ( ! func - > hasBody | | ! ( func - > type = = SymbolDatabase : : Func : : Constructor | |
func - > type = = SymbolDatabase : : Func : : CopyConstructor | |
func - > type = = SymbolDatabase : : Func : : OperatorEqual ) )
2010-07-26 16:46:37 +02:00
continue ;
// Mark all variables not used
2010-09-09 07:26:40 +02:00
info - > clearAllVar ( ) ;
2010-07-14 19:00:52 +02:00
2010-07-26 16:46:37 +02:00
std : : list < std : : string > callstack ;
2010-11-20 07:26:50 +01:00
info - > initializeVarList ( * func , callstack ) ;
2010-07-14 19:00:52 +02:00
2010-07-26 16:46:37 +02:00
// Check if any variables are uninitialized
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : Var > : : const_iterator var ;
2010-08-11 22:36:04 +02:00
for ( var = info - > varlist . begin ( ) ; var ! = info - > varlist . end ( ) ; + + var )
2010-07-14 19:00:52 +02:00
{
2010-09-09 07:26:40 +02:00
if ( var - > assign | | var - > init | | var - > isStatic )
2010-07-26 16:46:37 +02:00
continue ;
2010-07-15 10:16:16 +02:00
2010-11-16 07:30:55 +01:00
if ( var - > isConst & & var - > token - > previous ( ) - > str ( ) ! = " * " )
continue ;
2010-12-02 07:35:01 +01:00
// Check if this is a class constructor
if ( var - > isClass & & func - > type = = SymbolDatabase : : Func : : Constructor )
{
// Unknown type so assume it is initialized
if ( ! var - > type )
continue ;
// Known type that doesn't need initialization or
// known type that has member variables of an unknown type
else if ( var - > type - > needInitialization ! = SymbolDatabase : : SpaceInfo : : True )
continue ;
}
2010-07-26 16:46:37 +02:00
// It's non-static and it's not initialized => error
2010-11-20 07:26:50 +01:00
if ( func - > type = = SymbolDatabase : : Func : : OperatorEqual )
2010-01-16 08:47:46 +01:00
{
2010-07-26 16:46:37 +02:00
const Token * operStart = 0 ;
2010-11-20 07:26:50 +01:00
if ( func - > token - > str ( ) = = " = " )
operStart = func - > token - > tokAt ( 1 ) ;
2010-07-26 16:46:37 +02:00
else
2010-11-20 07:26:50 +01:00
operStart = func - > token - > tokAt ( 3 ) ;
2010-07-26 16:46:37 +02:00
bool classNameUsed = false ;
for ( const Token * operTok = operStart ; operTok ! = operStart - > link ( ) ; operTok = operTok - > next ( ) )
2010-01-16 08:47:46 +01:00
{
2010-07-26 16:46:37 +02:00
if ( operTok - > str ( ) = = info - > className )
2010-07-14 19:00:52 +02:00
{
2010-07-26 16:46:37 +02:00
classNameUsed = true ;
break ;
2010-07-14 19:00:52 +02:00
}
2010-07-15 10:16:16 +02:00
}
2010-01-16 08:47:46 +01:00
2010-07-26 16:46:37 +02:00
if ( classNameUsed )
2010-11-20 07:26:50 +01:00
operatorEqVarError ( func - > token , info - > className , var - > token - > str ( ) ) ;
2010-07-26 16:46:37 +02:00
}
2010-11-20 07:26:50 +01:00
else if ( func - > access ! = SymbolDatabase : : Private )
uninitVarError ( func - > token , info - > className , var - > token - > str ( ) ) ;
2010-07-15 10:16:16 +02:00
}
2010-07-14 19:00:52 +02:00
}
2010-01-16 08:47:46 +01:00
}
}
2010-11-14 06:50:33 +01:00
void CheckClass : : noConstructorError ( const Token * tok , const std : : string & classname , bool isStruct )
{
// For performance reasons the constructor might be intentionally missing. Therefore this is not a "warning"
2010-11-27 09:17:03 +01:00
reportError ( tok , Severity : : style , " noConstructor " ,
" The " + std : : string ( isStruct ? " struct " : " class " ) + " ' " + classname +
" ' does not have a constructor. \n "
2010-12-01 16:55:22 +01:00
" The " + std : : string ( isStruct ? " struct " : " class " ) + " ' " + classname +
" 'does not have a constructor but it has attributes. "
2010-11-27 09:17:03 +01:00
" The attributes are not initialized which may cause bugs or undefined behavior. " ) ;
2010-11-14 06:50:33 +01:00
}
void CheckClass : : uninitVarError ( const Token * tok , const std : : string & classname , const std : : string & varname )
{
2011-01-03 19:03:42 +01:00
reportError ( tok , Severity : : warning , " uninitVar " , " Member variable ' " + classname + " :: " + varname + " ' is not initialised in the constructor. " ) ;
2010-11-14 06:50:33 +01:00
}
void CheckClass : : operatorEqVarError ( const Token * tok , const std : : string & classname , const std : : string & varname )
{
reportError ( tok , Severity : : warning , " operatorEqVarError " , " Member variable ' " + classname + " :: " + varname + " ' is not assigned a value in ' " + classname + " ::operator= " + " ' " ) ;
}
2010-01-16 08:47:46 +01:00
//---------------------------------------------------------------------------
// ClassCheck: Unused private functions
//---------------------------------------------------------------------------
void CheckClass : : privateFunctions ( )
{
2010-04-21 08:38:25 +02:00
if ( ! _settings - > _checkCodingStyle )
return ;
2010-09-30 21:22:49 +02:00
// don't check code that contains templates. Templates that are
// "unused" are removed from the code. #2067
if ( _tokenizer - > codeWithTemplates ( ) )
return ;
2010-09-07 18:37:43 +02:00
// dont check borland classes with properties..
if ( Token : : findmatch ( _tokenizer - > tokens ( ) , " ; __property ; " ) )
return ;
2010-09-02 07:40:20 +02:00
createSymbolDatabase ( ) ;
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : SpaceInfo * > : : iterator i ;
2010-06-13 07:17:50 +02:00
2010-11-13 07:31:56 +01:00
for ( i = symbolDatabase - > spaceInfoList . begin ( ) ; i ! = symbolDatabase - > spaceInfoList . end ( ) ; + + i )
2010-01-16 08:47:46 +01:00
{
2010-11-13 07:31:56 +01:00
SymbolDatabase : : SpaceInfo * info = * i ;
2010-09-02 07:40:20 +02:00
2010-11-20 07:26:50 +01:00
// only check classes and structures
if ( ! info - > isClassOrStruct ( ) )
2010-09-02 07:40:20 +02:00
continue ;
2010-12-15 18:45:53 +01:00
// don’ t check derived classes
2010-09-02 07:40:20 +02:00
if ( ! info - > derivedFrom . empty ( ) )
continue ;
// Locate some class
const Token * tok1 = info - > classDef ;
2010-09-02 19:22:54 +02:00
// check that the whole class implementation is seen
bool whole = true ;
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : Func > : : const_iterator func ;
2010-09-02 19:22:54 +02:00
for ( func = info - > functionList . begin ( ) ; func ! = info - > functionList . end ( ) ; + + func )
{
if ( ! func - > hasBody )
{
// empty private copy constructors and assignment operators are OK
2010-11-13 07:31:56 +01:00
if ( ( func - > type = = SymbolDatabase : : Func : : CopyConstructor | | func - > type = = SymbolDatabase : : Func : : OperatorEqual ) & & func - > access = = SymbolDatabase : : Private )
2010-09-02 19:22:54 +02:00
continue ;
whole = false ;
break ;
}
}
if ( ! whole )
2010-01-16 08:47:46 +01:00
continue ;
const std : : string & classname = tok1 - > next ( ) - > str ( ) ;
std : : list < const Token * > FuncList ;
2010-09-02 07:40:20 +02:00
/** @todo embedded class have access to private functions */
2010-11-10 16:52:52 +01:00
if ( ! info - > getNestedNonFunctions ( ) )
2010-01-16 08:47:46 +01:00
{
2010-09-02 19:22:54 +02:00
for ( func = info - > functionList . begin ( ) ; func ! = info - > functionList . end ( ) ; + + func )
2010-01-16 08:47:46 +01:00
{
2010-09-02 07:40:20 +02:00
// Get private functions..
2010-11-13 07:31:56 +01:00
if ( func - > type = = SymbolDatabase : : Func : : Function & &
func - > access = = SymbolDatabase : : Private & & func - > hasBody )
2010-09-02 19:22:54 +02:00
FuncList . push_back ( func - > tokenDef ) ;
2010-01-16 08:47:46 +01:00
}
}
// Check that all private functions are used..
bool HasFuncImpl = false ;
bool inclass = false ;
2010-09-02 07:40:20 +02:00
int indent_level = 0 ;
2010-04-02 07:30:58 +02:00
for ( const Token * ftok = _tokenizer - > tokens ( ) ; ftok ; ftok = ftok - > next ( ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( ftok - > str ( ) = = " { " )
2010-01-16 08:47:46 +01:00
+ + indent_level ;
2010-04-02 07:30:58 +02:00
else if ( ftok - > str ( ) = = " } " )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( indent_level > 0 )
2010-01-16 08:47:46 +01:00
- - indent_level ;
2010-04-02 07:30:58 +02:00
if ( indent_level = = 0 )
2010-01-16 08:47:46 +01:00
inclass = false ;
}
2010-09-20 19:43:03 +02:00
else if ( ftok - > str ( ) = = " class " & &
ftok - > next ( ) - > str ( ) = = classname & &
Token : : Match ( ftok - > tokAt ( 2 ) , " :|{ " ) )
2010-01-16 08:47:46 +01:00
{
indent_level = 0 ;
inclass = true ;
}
// Check member class functions to see what functions are used..
2010-09-20 19:43:03 +02:00
else if ( ( inclass & & indent_level = = 1 & & Token : : Match ( ftok , " %var% ( " ) ) | |
( ftok - > str ( ) = = classname & & Token : : Match ( ftok - > next ( ) , " :: ~| %var% ( " ) ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
while ( ftok & & ftok - > str ( ) ! = " ) " )
2010-01-16 08:47:46 +01:00
ftok = ftok - > next ( ) ;
2010-04-02 07:30:58 +02:00
if ( ! ftok )
2010-01-16 08:47:46 +01:00
break ;
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( ftok , " ) : %var% ( " ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
while ( ! Token : : Match ( ftok - > next ( ) , " [{};] " ) )
2010-04-08 19:06:54 +02:00
{
if ( Token : : Match ( ftok , " ::|,|( %var% ,|) " ) )
{
// Remove function from FuncList
std : : list < const Token * > : : iterator it = FuncList . begin ( ) ;
while ( it ! = FuncList . end ( ) )
{
if ( ftok - > next ( ) - > str ( ) = = ( * it ) - > str ( ) )
FuncList . erase ( it + + ) ;
else
2010-10-17 14:15:29 +02:00
+ + it ;
2010-04-08 19:06:54 +02:00
}
}
2010-01-16 08:47:46 +01:00
ftok = ftok - > next ( ) ;
2010-04-08 19:06:54 +02:00
}
2010-01-16 08:47:46 +01:00
}
2010-04-02 07:30:58 +02:00
if ( ! Token : : Match ( ftok , " ) const| { " ) )
2010-01-16 08:47:46 +01:00
continue ;
2010-04-02 07:30:58 +02:00
if ( ftok - > fileIndex ( ) = = 0 )
2010-01-16 08:47:46 +01:00
HasFuncImpl = true ;
// Parse function..
int indentlevel2 = 0 ;
2010-04-02 07:30:58 +02:00
for ( const Token * tok2 = ftok ; tok2 ; tok2 = tok2 - > next ( ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( tok2 - > str ( ) = = " { " )
2010-01-16 08:47:46 +01:00
+ + indentlevel2 ;
2010-04-02 07:30:58 +02:00
else if ( tok2 - > str ( ) = = " } " )
2010-01-16 08:47:46 +01:00
{
- - indentlevel2 ;
2010-04-02 07:30:58 +02:00
if ( indentlevel2 < 1 )
2010-01-16 08:47:46 +01:00
break ;
}
2010-04-02 07:30:58 +02:00
else if ( Token : : Match ( tok2 , " %var% ( " ) )
2010-01-16 08:47:46 +01:00
{
// Remove function from FuncList
std : : list < const Token * > : : iterator it = FuncList . begin ( ) ;
2010-04-02 07:30:58 +02:00
while ( it ! = FuncList . end ( ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( tok2 - > str ( ) = = ( * it ) - > str ( ) )
2010-01-16 08:47:46 +01:00
FuncList . erase ( it + + ) ;
else
2010-10-17 14:15:29 +02:00
+ + it ;
2010-01-16 08:47:46 +01:00
}
}
}
}
}
2010-04-02 07:30:58 +02:00
while ( HasFuncImpl & & ! FuncList . empty ( ) )
2010-01-16 08:47:46 +01:00
{
// Final check; check if the function pointer is used somewhere..
const std : : string _pattern ( " return|(|)|,|= " + FuncList . front ( ) - > str ( ) ) ;
2010-08-15 07:44:08 +02:00
// or if the function address is used somewhere...
// eg. sigc::mem_fun(this, &className::classFunction)
const std : : string _pattern2 ( " & " + classname + " :: " + FuncList . front ( ) - > str ( ) ) ;
if ( ! Token : : findmatch ( _tokenizer - > tokens ( ) , _pattern . c_str ( ) ) & &
! Token : : findmatch ( _tokenizer - > tokens ( ) , _pattern2 . c_str ( ) ) )
2010-01-16 08:47:46 +01:00
{
unusedPrivateFunctionError ( FuncList . front ( ) , classname , FuncList . front ( ) - > str ( ) ) ;
}
FuncList . pop_front ( ) ;
}
}
}
2010-11-14 06:50:33 +01:00
void CheckClass : : unusedPrivateFunctionError ( const Token * tok , const std : : string & classname , const std : : string & funcname )
{
reportError ( tok , Severity : : style , " unusedPrivateFunction " , " Unused private function ' " + classname + " :: " + funcname + " ' " ) ;
}
2010-01-16 08:47:46 +01:00
//---------------------------------------------------------------------------
// ClassCheck: Check that memset is not used on classes
//---------------------------------------------------------------------------
void CheckClass : : noMemset ( )
{
// Locate all 'memset' tokens..
2010-04-02 07:30:58 +02:00
for ( const Token * tok = _tokenizer - > tokens ( ) ; tok ; tok = tok - > next ( ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( ! Token : : Match ( tok , " memset|memcpy|memmove " ) )
2010-01-16 08:47:46 +01:00
continue ;
2010-02-14 19:58:17 +01:00
std : : string type ;
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tok , " memset ( %var% , %num% , sizeof ( %type% ) ) " ) )
2010-01-16 08:47:46 +01:00
type = tok - > strAt ( 8 ) ;
2010-04-02 07:30:58 +02:00
else if ( Token : : Match ( tok , " memset ( & %var% , %num% , sizeof ( %type% ) ) " ) )
2010-01-16 08:47:46 +01:00
type = tok - > strAt ( 9 ) ;
2010-04-02 07:30:58 +02:00
else if ( Token : : Match ( tok , " memset ( %var% , %num% , sizeof ( struct %type% ) ) " ) )
2010-01-16 08:47:46 +01:00
type = tok - > strAt ( 9 ) ;
2010-04-02 07:30:58 +02:00
else if ( Token : : Match ( tok , " memset ( & %var% , %num% , sizeof ( struct %type% ) ) " ) )
2010-01-16 08:47:46 +01:00
type = tok - > strAt ( 10 ) ;
2010-04-02 07:30:58 +02:00
else if ( Token : : Match ( tok , " %type% ( %var% , %var% , sizeof ( %type% ) ) " ) )
2010-01-16 08:47:46 +01:00
type = tok - > strAt ( 8 ) ;
// No type defined => The tokens didn't match
2010-04-02 07:30:58 +02:00
if ( type . empty ( ) )
2010-01-16 08:47:46 +01:00
continue ;
2010-02-04 19:40:35 +01:00
// Warn if type is a class or struct that contains any std::* variables
const std : : string pattern2 ( std : : string ( " struct|class " ) + type + " { " ) ;
2010-04-02 07:30:58 +02:00
for ( const Token * tstruct = Token : : findmatch ( _tokenizer - > tokens ( ) , pattern2 . c_str ( ) ) ; tstruct ; tstruct = tstruct - > next ( ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( tstruct - > str ( ) = = " } " )
2010-01-16 08:47:46 +01:00
break ;
2010-02-04 21:49:58 +01:00
// struct with function? skip function body..
2010-04-02 07:30:58 +02:00
if ( Token : : simpleMatch ( tstruct , " ) { " ) )
2010-01-16 08:47:46 +01:00
{
2010-02-04 21:49:58 +01:00
tstruct = tstruct - > next ( ) - > link ( ) ;
2010-04-02 07:30:58 +02:00
if ( ! tstruct )
2010-02-04 21:49:58 +01:00
break ;
2010-01-16 08:47:46 +01:00
}
2010-02-04 19:40:35 +01:00
2010-02-04 21:49:58 +01:00
// before a statement there must be either:
// * private:|protected:|public:
// * { } ;
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tstruct , " [;{}] " ) | |
tstruct - > str ( ) . find ( " : " ) ! = std : : string : : npos )
2010-02-04 19:40:35 +01:00
{
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tstruct - > next ( ) , " std :: %type% %var% ; " ) )
2010-02-04 21:49:58 +01:00
memsetStructError ( tok , tok - > str ( ) , tstruct - > strAt ( 3 ) ) ;
2010-04-02 07:30:58 +02:00
else if ( Token : : Match ( tstruct - > next ( ) , " std :: %type% < " ) )
2010-02-04 21:49:58 +01:00
{
// backup the type
const std : : string typestr ( tstruct - > strAt ( 3 ) ) ;
// check if it's a pointer variable..
unsigned int level = 0 ;
2010-04-02 07:30:58 +02:00
while ( 0 ! = ( tstruct = tstruct - > next ( ) ) )
2010-02-04 21:49:58 +01:00
{
2010-04-02 07:30:58 +02:00
if ( tstruct - > str ( ) = = " < " )
2010-02-04 21:49:58 +01:00
+ + level ;
2010-04-02 07:30:58 +02:00
else if ( tstruct - > str ( ) = = " > " )
2010-02-04 21:49:58 +01:00
{
2010-04-02 07:30:58 +02:00
if ( level < = 1 )
2010-02-04 21:49:58 +01:00
break ;
- - level ;
}
2010-04-02 07:30:58 +02:00
else if ( tstruct - > str ( ) = = " ( " )
2010-02-04 21:49:58 +01:00
tstruct = tstruct - > link ( ) ;
}
2010-04-02 07:30:58 +02:00
if ( ! tstruct )
2010-02-04 21:49:58 +01:00
break ;
// found error => report
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tstruct , " > %var% ; " ) )
2010-02-04 21:49:58 +01:00
memsetStructError ( tok , tok - > str ( ) , typestr ) ;
}
2010-02-04 19:40:35 +01:00
}
2010-01-16 08:47:46 +01:00
}
}
}
2010-11-14 06:50:33 +01:00
void CheckClass : : memsetClassError ( const Token * tok , const std : : string & memfunc )
{
reportError ( tok , Severity : : error , " memsetClass " , " Using ' " + memfunc + " ' on class " ) ;
}
2010-01-16 08:47:46 +01:00
2010-11-14 06:50:33 +01:00
void CheckClass : : memsetStructError ( const Token * tok , const std : : string & memfunc , const std : : string & classname )
{
reportError ( tok , Severity : : error , " memsetStruct " , " Using ' " + memfunc + " ' on struct that contains a 'std:: " + classname + " ' " ) ;
}
2010-01-16 08:47:46 +01:00
//---------------------------------------------------------------------------
2010-09-11 08:23:30 +02:00
// ClassCheck: "void operator=(" and "const type & operator=("
2010-01-16 08:47:46 +01:00
//---------------------------------------------------------------------------
void CheckClass : : operatorEq ( )
{
2010-04-21 08:38:25 +02:00
if ( ! _settings - > _checkCodingStyle )
return ;
2010-08-07 12:41:11 +02:00
createSymbolDatabase ( ) ;
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : SpaceInfo * > : : const_iterator i ;
2010-01-16 08:47:46 +01:00
2010-11-13 07:31:56 +01:00
for ( i = symbolDatabase - > spaceInfoList . begin ( ) ; i ! = symbolDatabase - > spaceInfoList . end ( ) ; + + i )
2010-01-16 08:47:46 +01:00
{
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : Func > : : const_iterator it ;
2010-07-26 16:46:37 +02:00
2010-11-02 18:28:55 +01:00
for ( it = ( * i ) - > functionList . begin ( ) ; it ! = ( * i ) - > functionList . end ( ) ; + + it )
2010-01-16 08:47:46 +01:00
{
2010-11-13 07:31:56 +01:00
if ( it - > type = = SymbolDatabase : : Func : : OperatorEqual & & it - > access ! = SymbolDatabase : : Private )
2010-01-16 08:47:46 +01:00
{
2010-07-26 16:46:37 +02:00
if ( it - > token - > strAt ( - 2 ) = = " void " )
operatorEqReturnError ( it - > token - > tokAt ( - 2 ) ) ;
2010-01-16 08:47:46 +01:00
}
}
}
}
2010-11-14 06:50:33 +01:00
void CheckClass : : operatorEqReturnError ( const Token * tok )
{
reportError ( tok , Severity : : style , " operatorEq " , " 'operator=' should return something " ) ;
}
2010-01-16 08:47:46 +01:00
//---------------------------------------------------------------------------
// ClassCheck: "C& operator=(const C&) { ... return *this; }"
// operator= should return a reference to *this
//---------------------------------------------------------------------------
2010-11-13 07:31:56 +01:00
void CheckClass : : checkReturnPtrThis ( const SymbolDatabase : : SpaceInfo * info , const SymbolDatabase : : Func * func , const Token * tok , const Token * last )
2010-09-11 08:23:30 +02:00
{
bool foundReturn = false ;
for ( ; tok & & tok ! = last ; tok = tok - > next ( ) )
{
// check for return of reference to this
if ( tok - > str ( ) = = " return " )
{
foundReturn = true ;
std : : string cast ( " ( " + info - > className + " & ) " ) ;
if ( Token : : Match ( tok - > next ( ) , cast . c_str ( ) ) )
tok = tok - > tokAt ( 4 ) ;
// check if a function is called
if ( Token : : Match ( tok - > tokAt ( 1 ) , " %any% ( " ) & &
tok - > tokAt ( 2 ) - > link ( ) - > next ( ) - > str ( ) = = " ; " )
{
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : Func > : : const_iterator it ;
2010-09-11 08:23:30 +02:00
// check if it is a member function
for ( it = info - > functionList . begin ( ) ; it ! = info - > functionList . end ( ) ; + + it )
{
// check for a regular function with the same name and a bofy
2010-11-13 07:31:56 +01:00
if ( it - > type = = SymbolDatabase : : Func : : Function & & it - > hasBody & &
2010-09-11 08:23:30 +02:00
it - > token - > str ( ) = = tok - > next ( ) - > str ( ) )
{
// check for the proper return type
if ( it - > tokenDef - > previous ( ) - > str ( ) = = " & " & &
it - > tokenDef - > strAt ( - 2 ) = = info - > className )
{
// make sure it's not a const function
if ( it - > arg - > link ( ) - > next ( ) - > str ( ) ! = " const " )
checkReturnPtrThis ( info , & * it , it - > arg - > link ( ) - > next ( ) , it - > arg - > link ( ) - > next ( ) - > link ( ) ) ;
}
}
}
}
// check of *this is returned
else if ( ! ( Token : : Match ( tok - > tokAt ( 1 ) , " (| * this ; | = " ) ||
Token : : Match ( tok - > tokAt ( 1 ) , " (| * this += " ) | |
Token : : Match ( tok - > tokAt ( 1 ) , " operator = ( " ) ) )
operatorEqRetRefThisError ( func - > token ) ;
}
}
if ( ! foundReturn )
operatorEqRetRefThisError ( func - > token ) ;
}
2010-01-16 08:47:46 +01:00
void CheckClass : : operatorEqRetRefThis ( )
{
2010-04-21 08:38:25 +02:00
if ( ! _settings - > _checkCodingStyle )
return ;
2010-08-07 12:41:11 +02:00
createSymbolDatabase ( ) ;
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : SpaceInfo * > : : const_iterator i ;
2010-01-16 08:47:46 +01:00
2010-11-13 07:31:56 +01:00
for ( i = symbolDatabase - > spaceInfoList . begin ( ) ; i ! = symbolDatabase - > spaceInfoList . end ( ) ; + + i )
2010-01-16 08:47:46 +01:00
{
2010-11-13 07:31:56 +01:00
const SymbolDatabase : : SpaceInfo * info = * i ;
2010-01-16 08:47:46 +01:00
2010-11-20 07:26:50 +01:00
// only check classes and structures
if ( info - > isClassOrStruct ( ) )
2010-01-16 08:47:46 +01:00
{
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : Func > : : const_iterator func ;
2010-11-02 18:28:55 +01:00
for ( func = info - > functionList . begin ( ) ; func ! = info - > functionList . end ( ) ; + + func )
2010-01-16 08:47:46 +01:00
{
2010-11-13 07:31:56 +01:00
if ( func - > type = = SymbolDatabase : : Func : : OperatorEqual & & func - > hasBody )
2010-01-16 08:47:46 +01:00
{
2010-11-02 18:28:55 +01:00
// make sure return signature is correct
if ( Token : : Match ( func - > tokenDef - > tokAt ( - 4 ) , " ;|}|{|public:|protected:|private: %type% & " ) & &
func - > tokenDef - > strAt ( - 3 ) = = info - > className )
{
// find the ')'
const Token * tok = func - > token - > next ( ) - > link ( ) ;
2010-01-16 08:47:46 +01:00
2010-11-02 18:28:55 +01:00
checkReturnPtrThis ( info , & ( * func ) , tok - > tokAt ( 2 ) , tok - > next ( ) - > link ( ) ) ;
}
2010-01-16 08:47:46 +01:00
}
}
}
}
}
2010-07-26 16:46:37 +02:00
2010-11-14 06:50:33 +01:00
void CheckClass : : operatorEqRetRefThisError ( const Token * tok )
{
reportError ( tok , Severity : : style , " operatorEqRetRefThis " , " 'operator=' should return reference to self " ) ;
}
2010-01-16 08:47:46 +01:00
//---------------------------------------------------------------------------
// ClassCheck: "C& operator=(const C& rhs) { if (this == &rhs) ... }"
// operator= should check for assignment to self
//
// For simple classes, an assignment to self check is only a potential optimization.
//
// For classes that allocate dynamic memory, assignment to self can be a real error
// if it is deallocated and allocated again without being checked for.
//
// This check is not valid for classes with multiple inheritance because a
// class can have multiple addresses so there is no trivial way to check for
// assignment to self.
//---------------------------------------------------------------------------
2010-11-14 06:50:33 +01:00
void CheckClass : : operatorEqToSelf ( )
{
if ( ! _settings - > _checkCodingStyle )
return ;
createSymbolDatabase ( ) ;
std : : list < SymbolDatabase : : SpaceInfo * > : : const_iterator i ;
for ( i = symbolDatabase - > spaceInfoList . begin ( ) ; i ! = symbolDatabase - > spaceInfoList . end ( ) ; + + i )
{
const SymbolDatabase : : SpaceInfo * info = * i ;
std : : list < SymbolDatabase : : Func > : : const_iterator it ;
// skip classes with multiple inheritance
if ( info - > derivedFrom . size ( ) > 1 )
continue ;
for ( it = info - > functionList . begin ( ) ; it ! = info - > functionList . end ( ) ; + + it )
{
if ( it - > type = = SymbolDatabase : : Func : : OperatorEqual & & it - > hasBody )
{
// make sure return signature is correct
if ( Token : : Match ( it - > tokenDef - > tokAt ( - 4 ) , " ;|}|{|public:|protected:|private: %type% & " ) & &
it - > tokenDef - > strAt ( - 3 ) = = info - > className )
{
// check for proper function parameter signature
if ( ( Token : : Match ( it - > tokenDef - > next ( ) , " ( const %var% & ) " ) | |
Token : : Match ( it - > tokenDef - > next ( ) , " ( const %var% & %var% ) " ) ) & &
it - > tokenDef - > strAt ( 3 ) = = info - > className )
{
// find the parameter name
const Token * rhs = it - > token ;
while ( rhs - > str ( ) ! = " & " )
rhs = rhs - > next ( ) ;
rhs = rhs - > next ( ) ;
// find the ')'
const Token * tok = it - > token - > next ( ) - > link ( ) ;
const Token * tok1 = tok ;
if ( tok1 & & tok1 - > tokAt ( 1 ) & & tok1 - > tokAt ( 1 ) - > str ( ) = = " { " & & tok1 - > tokAt ( 1 ) - > link ( ) )
{
const Token * first = tok1 - > tokAt ( 1 ) ;
const Token * last = first - > link ( ) ;
if ( ! hasAssignSelf ( first , last , rhs ) )
{
if ( hasDeallocation ( first , last ) )
operatorEqToSelfError ( tok ) ;
}
}
}
}
}
}
}
}
bool CheckClass : : hasDeallocation ( const Token * first , const Token * last )
2010-01-16 08:47:46 +01:00
{
// This function is called when no simple check was found for assignment
// to self. We are currently looking for a specific sequence of:
// deallocate member ; ... member = allocate
// This check is far from ideal because it can cause false negatives.
// Unfortunately, this is necessary to prevent false positives.
// This check needs to do careful analysis someday to get this
// correct with a high degree of certainty.
2010-08-28 11:23:23 +02:00
for ( const Token * tok = first ; tok & & ( tok ! = last ) ; tok = tok - > next ( ) )
2010-01-16 08:47:46 +01:00
{
// check for deallocating memory
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tok , " {|;|, free ( %var% " ) )
2010-01-16 08:47:46 +01:00
{
2010-08-28 11:23:23 +02:00
const Token * var = tok - > tokAt ( 3 ) ;
2010-01-16 08:47:46 +01:00
// we should probably check that var is a pointer in this class
2010-08-28 11:23:23 +02:00
const Token * tok1 = tok - > tokAt ( 4 ) ;
2010-01-16 08:47:46 +01:00
2010-04-02 07:30:58 +02:00
while ( tok1 & & ( tok1 ! = last ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tok1 , " %var% = " ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( tok1 - > str ( ) = = var - > str ( ) )
2010-01-16 08:47:46 +01:00
return true ;
}
tok1 = tok1 - > next ( ) ;
}
}
2010-04-02 07:30:58 +02:00
else if ( Token : : Match ( tok , " {|;|, delete [ ] %var% " ) )
2010-01-16 08:47:46 +01:00
{
2010-08-28 11:23:23 +02:00
const Token * var = tok - > tokAt ( 4 ) ;
2010-01-16 08:47:46 +01:00
// we should probably check that var is a pointer in this class
2010-08-28 11:23:23 +02:00
const Token * tok1 = tok - > tokAt ( 5 ) ;
2010-01-16 08:47:46 +01:00
2010-04-02 07:30:58 +02:00
while ( tok1 & & ( tok1 ! = last ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tok1 , " %var% = new %type% [ " ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( tok1 - > str ( ) = = var - > str ( ) )
2010-01-16 08:47:46 +01:00
return true ;
}
tok1 = tok1 - > next ( ) ;
}
}
2010-04-02 07:30:58 +02:00
else if ( Token : : Match ( tok , " {|;|, delete %var% " ) )
2010-01-16 08:47:46 +01:00
{
2010-08-28 11:23:23 +02:00
const Token * var = tok - > tokAt ( 2 ) ;
2010-01-16 08:47:46 +01:00
// we should probably check that var is a pointer in this class
2010-08-28 11:23:23 +02:00
const Token * tok1 = tok - > tokAt ( 3 ) ;
2010-01-16 08:47:46 +01:00
2010-04-02 07:30:58 +02:00
while ( tok1 & & ( tok1 ! = last ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tok1 , " %var% = new " ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( tok1 - > str ( ) = = var - > str ( ) )
2010-01-16 08:47:46 +01:00
return true ;
}
tok1 = tok1 - > next ( ) ;
}
}
}
return false ;
}
2010-11-14 06:50:33 +01:00
bool CheckClass : : hasAssignSelf ( const Token * first , const Token * last , const Token * rhs )
2010-01-16 08:47:46 +01:00
{
2010-08-28 11:23:23 +02:00
for ( const Token * tok = first ; tok & & tok ! = last ; tok = tok - > next ( ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tok , " if ( " ) )
2010-01-16 08:47:46 +01:00
{
2010-08-28 11:23:23 +02:00
const Token * tok1 = tok - > tokAt ( 2 ) ;
const Token * tok2 = tok - > tokAt ( 1 ) - > link ( ) ;
2010-01-16 08:47:46 +01:00
2010-04-02 07:30:58 +02:00
if ( tok1 & & tok2 )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
for ( ; tok1 & & tok1 ! = tok2 ; tok1 = tok1 - > next ( ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( Token : : Match ( tok1 , " this ==|!= & %var% " ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( tok1 - > tokAt ( 3 ) - > str ( ) = = rhs - > str ( ) )
2010-01-16 08:47:46 +01:00
return true ;
}
2010-04-02 07:30:58 +02:00
else if ( Token : : Match ( tok1 , " & %var% ==|!= this " ) )
2010-01-16 08:47:46 +01:00
{
2010-04-02 07:30:58 +02:00
if ( tok1 - > tokAt ( 1 ) - > str ( ) = = rhs - > str ( ) )
2010-01-16 08:47:46 +01:00
return true ;
}
}
}
}
}
return false ;
}
2010-11-14 06:50:33 +01:00
void CheckClass : : operatorEqToSelfError ( const Token * tok )
2010-01-16 08:47:46 +01:00
{
2010-11-14 06:50:33 +01:00
reportError ( tok , Severity : : warning , " operatorEqToSelf " , " 'operator=' should check for assignment to self " ) ;
2010-01-16 08:47:46 +01:00
}
//---------------------------------------------------------------------------
// A destructor in a base class should be virtual
//---------------------------------------------------------------------------
void CheckClass : : virtualDestructor ( )
{
2010-05-29 11:19:28 +02:00
// This error should only be given if:
// * base class doesn't have virtual destructor
// * derived class has non-empty destructor
// * base class is deleted
if ( ! _settings - > inconclusive )
return ;
2010-08-13 18:34:02 +02:00
createSymbolDatabase ( ) ;
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : SpaceInfo * > : : const_iterator i ;
2010-01-16 08:47:46 +01:00
2010-11-13 07:31:56 +01:00
for ( i = symbolDatabase - > spaceInfoList . begin ( ) ; i ! = symbolDatabase - > spaceInfoList . end ( ) ; + + i )
2010-01-16 08:47:46 +01:00
{
2010-11-13 07:31:56 +01:00
const SymbolDatabase : : SpaceInfo * info = * i ;
2010-01-16 08:47:46 +01:00
2010-08-13 18:34:02 +02:00
// Skip base classes and namespaces
if ( info - > derivedFrom . empty ( ) )
continue ;
2010-01-16 08:47:46 +01:00
2010-08-13 18:34:02 +02:00
// Find the destructor
2010-11-13 07:31:56 +01:00
const SymbolDatabase : : Func * destructor = info - > getDestructor ( ) ;
2010-08-13 18:34:02 +02:00
// Check for destructor with implementation
if ( ! destructor | | ! destructor - > hasBody )
continue ;
// Empty destructor
2010-09-10 07:02:49 +02:00
if ( destructor - > token - > tokAt ( 3 ) - > link ( ) = = destructor - > token - > tokAt ( 4 ) )
2010-08-13 18:34:02 +02:00
continue ;
2010-01-16 08:47:46 +01:00
2010-08-13 18:34:02 +02:00
const Token * derived = info - > classDef ;
2010-01-16 08:47:46 +01:00
const Token * derivedClass = derived - > tokAt ( 1 ) ;
// Iterate through each base class...
2010-08-13 18:34:02 +02:00
for ( unsigned int j = 0 ; j < info - > derivedFrom . size ( ) ; + + j )
2010-01-16 08:47:46 +01:00
{
2010-08-13 18:34:02 +02:00
// Check if base class is public and exists in database
2010-11-13 07:31:56 +01:00
if ( info - > derivedFrom [ j ] . access = = SymbolDatabase : : Public & & info - > derivedFrom [ j ] . spaceInfo )
2010-01-16 08:47:46 +01:00
{
2010-11-13 07:31:56 +01:00
const SymbolDatabase : : SpaceInfo * spaceInfo = info - > derivedFrom [ j ] . spaceInfo ;
2010-01-16 08:47:46 +01:00
2010-08-13 23:57:53 +02:00
// Name of base class..
const std : : string baseName = spaceInfo - > className ;
2010-01-16 08:47:46 +01:00
2010-08-13 23:57:53 +02:00
// Find the destructor declaration for the base class.
2010-11-13 07:31:56 +01:00
const SymbolDatabase : : Func * base_destructor = spaceInfo - > getDestructor ( ) ;
2010-08-13 23:57:53 +02:00
const Token * base = 0 ;
if ( base_destructor )
base = base_destructor - > token ;
2010-01-16 08:47:46 +01:00
2010-08-13 23:57:53 +02:00
// Check that there is a destructor..
if ( ! base_destructor )
{
if ( spaceInfo - > derivedFrom . empty ( ) )
virtualDestructorError ( spaceInfo - > classDef , baseName , derivedClass - > str ( ) ) ;
}
else if ( ! base_destructor - > isVirtual )
{
// TODO: This is just a temporary fix, better solution is needed.
// Skip situations where base class has base classes of its own, because
// some of the base classes might have virtual destructor.
// Proper solution is to check all of the base classes. If base class is not
// found or if one of the base classes has virtual destructor, error should not
// be printed. See TODO test case "virtualDestructorInherited"
if ( spaceInfo - > derivedFrom . empty ( ) )
{
// Make sure that the destructor is public (protected or private
// would not compile if inheritance is used in a way that would
// cause the bug we are trying to find here.)
2010-11-13 07:31:56 +01:00
if ( base_destructor - > access = = SymbolDatabase : : Public )
2010-08-13 23:57:53 +02:00
virtualDestructorError ( base , baseName , derivedClass - > str ( ) ) ;
}
}
}
2010-01-16 08:47:46 +01:00
}
}
}
2010-11-14 06:50:33 +01:00
void CheckClass : : virtualDestructorError ( const Token * tok , const std : : string & Base , const std : : string & Derived )
2010-01-16 08:47:46 +01:00
{
2010-11-14 06:50:33 +01:00
reportError ( tok , Severity : : error , " virtualDestructor " , " Class " + Base + " which is inherited by class " + Derived + " does not have a virtual destructor " ) ;
2010-01-16 08:47:46 +01:00
}
2010-11-14 06:50:33 +01:00
//---------------------------------------------------------------------------
// warn for "this-x". The indented code may be "this->x"
//---------------------------------------------------------------------------
2010-01-16 08:47:46 +01:00
void CheckClass : : thisSubtraction ( )
{
2010-05-01 21:43:47 +02:00
if ( ! _settings - > _checkCodingStyle )
2010-04-21 08:38:25 +02:00
return ;
2010-01-16 08:47:46 +01:00
const Token * tok = _tokenizer - > tokens ( ) ;
2010-04-02 07:30:58 +02:00
for ( ; ; )
2010-01-16 08:47:46 +01:00
{
tok = Token : : findmatch ( tok , " this - %var% " ) ;
2010-04-02 07:30:58 +02:00
if ( ! tok )
2010-01-16 08:47:46 +01:00
break ;
2010-04-02 07:30:58 +02:00
if ( ! Token : : simpleMatch ( tok - > previous ( ) , " * " ) )
2010-01-16 08:47:46 +01:00
thisSubtractionError ( tok ) ;
tok = tok - > next ( ) ;
}
}
2010-11-14 06:50:33 +01:00
void CheckClass : : thisSubtractionError ( const Token * tok )
{
reportError ( tok , Severity : : warning , " thisSubtraction " , " Suspicious pointer subtraction " ) ;
}
//---------------------------------------------------------------------------
// can member function be const?
2010-03-05 17:06:25 +01:00
//---------------------------------------------------------------------------
2010-01-16 08:47:46 +01:00
2010-01-23 09:19:22 +01:00
void CheckClass : : checkConst ( )
{
2010-08-07 13:08:36 +02:00
if ( ! _settings - > _checkCodingStyle | | _settings - > ifcfg )
2010-01-23 09:38:35 +01:00
return ;
2010-10-20 22:15:35 +02:00
// Don't check C# and JAVA classes
2010-10-28 18:51:55 +02:00
if ( _tokenizer - > isJavaOrCSharp ( ) )
2010-10-20 22:15:35 +02:00
{
return ;
}
2010-08-07 12:41:11 +02:00
createSymbolDatabase ( ) ;
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : SpaceInfo * > : : iterator it ;
2010-01-23 09:19:22 +01:00
2010-11-13 07:31:56 +01:00
for ( it = symbolDatabase - > spaceInfoList . begin ( ) ; it ! = symbolDatabase - > spaceInfoList . end ( ) ; + + it )
2010-07-26 16:46:37 +02:00
{
2010-11-13 07:31:56 +01:00
SymbolDatabase : : SpaceInfo * info = * it ;
2010-01-23 09:19:22 +01:00
2010-11-20 07:26:50 +01:00
// only check classes and structures
if ( ! info - > isClassOrStruct ( ) )
continue ;
2010-11-13 07:31:56 +01:00
std : : list < SymbolDatabase : : Func > : : const_iterator func ;
2010-03-05 17:06:25 +01:00
2010-11-02 18:28:55 +01:00
for ( func = info - > functionList . begin ( ) ; func ! = info - > functionList . end ( ) ; + + func )
2010-07-26 16:46:37 +02:00
{
// does the function have a body?
2010-11-13 07:31:56 +01:00
if ( func - > type = = SymbolDatabase : : Func : : Function & & func - > hasBody & & ! func - > isFriend & & ! func - > isStatic & & ! func - > isConst & & ! func - > isVirtual )
2010-03-12 18:30:20 +01:00
{
2010-07-26 16:46:37 +02:00
// get last token of return type
2010-11-02 18:28:55 +01:00
const Token * previous = func - > tokenDef - > isName ( ) ? func - > token - > previous ( ) : func - > token - > tokAt ( - 2 ) ;
2010-11-20 07:26:50 +01:00
while ( previous & & previous - > str ( ) = = " :: " )
2010-07-26 16:46:37 +02:00
previous = previous - > tokAt ( - 2 ) ;
2010-03-12 18:30:20 +01:00
2010-07-26 16:46:37 +02:00
// does the function return a pointer or reference?
if ( Token : : Match ( previous , " *|& " ) )
{
2010-11-02 18:28:55 +01:00
const Token * temp = func - > token - > previous ( ) ;
2010-01-23 09:19:22 +01:00
2010-07-26 16:46:37 +02:00
while ( ! Token : : Match ( temp - > previous ( ) , " ;|}|{|public:|protected:|private: " ) )
temp = temp - > previous ( ) ;
2010-01-24 13:45:56 +01:00
2010-07-26 16:46:37 +02:00
if ( temp - > str ( ) ! = " const " )
2010-03-19 17:40:23 +01:00
continue ;
}
2010-07-26 16:46:37 +02:00
else if ( Token : : Match ( previous - > previous ( ) , " *|& > " ) )
2010-04-09 19:15:39 +02:00
{
2010-11-02 18:28:55 +01:00
const Token * temp = func - > token - > previous ( ) ;
2010-07-26 16:46:37 +02:00
while ( ! Token : : Match ( temp - > previous ( ) , " ;|}|{|public:|protected:|private: " ) )
2010-04-09 19:15:39 +02:00
{
2010-07-26 16:46:37 +02:00
temp = temp - > previous ( ) ;
if ( temp - > str ( ) = = " const " )
2010-04-09 19:15:39 +02:00
break ;
}
2010-07-26 16:46:37 +02:00
if ( temp - > str ( ) ! = " const " )
2010-04-09 19:15:39 +02:00
continue ;
}
2010-07-26 16:46:37 +02:00
else
2010-01-23 09:19:22 +01:00
{
2010-07-26 16:46:37 +02:00
// don't warn for unknown types..
// LPVOID, HDC, etc
if ( previous - > isName ( ) )
2010-03-05 17:06:25 +01:00
{
2010-07-26 16:46:37 +02:00
bool allupper = true ;
const std : : string s ( previous - > str ( ) ) ;
for ( std : : string : : size_type pos = 0 ; pos < s . size ( ) ; + + pos )
2010-07-18 10:18:41 +02:00
{
2010-08-06 21:02:43 +02:00
const char ch = s [ pos ] ;
2010-07-26 16:46:37 +02:00
if ( ! ( ch = = ' _ ' | | ( ch > = ' A ' & & ch < = ' Z ' ) ) )
2010-01-23 09:19:22 +01:00
{
2010-07-26 16:46:37 +02:00
allupper = false ;
break ;
2010-01-23 20:59:20 +01:00
}
2010-03-05 17:06:25 +01:00
}
2010-07-26 16:46:37 +02:00
2010-09-06 19:04:14 +02:00
if ( allupper & & previous - > str ( ) . size ( ) > 2 )
2010-07-26 16:46:37 +02:00
continue ;
2010-03-05 17:06:25 +01:00
}
}
2010-01-23 20:59:20 +01:00
2011-01-01 17:54:37 +01:00
const Token * paramEnd = func - > arg - > link ( ) ;
2010-03-05 17:06:25 +01:00
2010-07-26 16:46:37 +02:00
// check if base class function is virtual
if ( ! info - > derivedFrom . empty ( ) )
2010-03-05 17:06:25 +01:00
{
2010-11-13 07:31:56 +01:00
if ( symbolDatabase - > isVirtualFunc ( info , func - > tokenDef ) )
2010-07-26 16:46:37 +02:00
continue ;
2010-01-23 09:19:22 +01:00
}
2010-03-05 17:06:25 +01:00
2010-07-26 16:46:37 +02:00
// if nothing non-const was found. write error..
2010-11-13 07:31:56 +01:00
if ( symbolDatabase - > checkConstFunc ( info , paramEnd ) )
2010-03-28 11:46:42 +02:00
{
2010-07-26 16:46:37 +02:00
std : : string classname = info - > className ;
2010-11-13 07:31:56 +01:00
SymbolDatabase : : SpaceInfo * nest = info - > nestedIn ;
2010-11-20 07:26:50 +01:00
while ( nest & & nest - > type ! = SymbolDatabase : : SpaceInfo : : Global )
2010-03-28 11:46:42 +02:00
{
2010-07-26 16:46:37 +02:00
classname = std : : string ( nest - > className + " :: " + classname ) ;
2010-08-28 11:23:23 +02:00
nest = nest - > nestedIn ;
2010-03-28 11:46:42 +02:00
}
2010-04-18 15:40:31 +02:00
2010-09-01 06:32:46 +02:00
// get function name
2010-11-02 18:28:55 +01:00
std : : string functionName ( ( func - > tokenDef - > isName ( ) ? " " : " operator " ) + func - > tokenDef - > str ( ) ) ;
2010-09-01 06:32:46 +02:00
2010-11-02 18:28:55 +01:00
if ( func - > tokenDef - > str ( ) = = " ( " )
2010-09-01 06:32:46 +02:00
functionName + = " ) " ;
2010-11-02 18:28:55 +01:00
else if ( func - > tokenDef - > str ( ) = = " [ " )
2010-09-01 06:32:46 +02:00
functionName + = " ] " ;
2010-11-02 18:28:55 +01:00
if ( func - > isInline )
checkConstError ( func - > token , classname , functionName ) ;
2010-07-26 16:46:37 +02:00
else // not inline
2010-11-02 18:28:55 +01:00
checkConstError2 ( func - > token , func - > tokenDef , classname , functionName ) ;
2010-04-18 15:40:31 +02:00
}
2010-03-28 11:46:42 +02:00
}
}
}
}
2010-01-23 09:19:22 +01:00
void CheckClass : : checkConstError ( const Token * tok , const std : : string & classname , const std : : string & funcname )
{
2010-12-26 22:34:00 +01:00
reportError ( tok , Severity : : information , " functionConst " ,
" Technically the member function ' " + classname + " :: " + funcname + " ' can be const. \n "
" The member function ' " + classname + " :: " + funcname + " ' can be made a const "
" function. Making this function const function should not cause compiler errors. "
" Even though the function can be made const function technically it may not make "
" sense conceptually. Think about your design and task of the function first - is "
" it a function that must not change object internal state? " ) ;
2010-01-23 09:19:22 +01:00
}
2010-03-10 07:47:01 +01:00
void CheckClass : : checkConstError2 ( const Token * tok1 , const Token * tok2 , const std : : string & classname , const std : : string & funcname )
{
std : : list < const Token * > toks ;
toks . push_back ( tok1 ) ;
toks . push_back ( tok2 ) ;
2010-12-26 22:34:00 +01:00
reportError ( toks , Severity : : information , " functionConst " ,
" Technically the member function ' " + classname + " :: " + funcname + " ' can be const. \n "
" The member function ' " + classname + " :: " + funcname + " ' can be made a const "
" function. Making this function const function should not cause compiler errors. "
" Even though the function can be made const function technically it may not make "
" sense conceptually. Think about your design and task of the function first - is "
" it a function that must not change object internal state? " ) ;
2010-03-10 07:47:01 +01:00
}