Cppcheck
Functions | Variables
checkstl.cpp File Reference
#include "checkstl.h"
#include "astutils.h"
#include "errortypes.h"
#include "library.h"
#include "mathlib.h"
#include "pathanalysis.h"
#include "settings.h"
#include "standards.h"
#include "symboldatabase.h"
#include "token.h"
#include "tokenize.h"
#include "utils.h"
#include "valueflow.h"
#include "checknullpointer.h"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

Go to the source code of this file.

Functions

static bool isElementAccessYield (Library::Container::Yield yield)
 
static bool containerAppendsElement (const Library::Container *container, const Token *parent)
 
static bool containerYieldsElement (const Library::Container *container, const Token *parent)
 
static bool containerPopsElement (const Library::Container *container, const Token *parent)
 
static const Token * getContainerIndex (const Library::Container *container, const Token *parent)
 
static const Token * getContainerFromSize (const Library::Container *container, const Token *tok)
 
static std::string indexValueString (const ValueFlow::Value &indexValue, const std::string &containerName=emptyString)
 
static const Token * skipMembers (const Token *tok)
 
static bool isIterator (const Variable *var, bool &inconclusiveType)
 
static std::string getContainerName (const Token *containerToken)
 
static bool isVector (const Token *tok)
 
static std::vector< const Token * > getAddressContainer (const Token *tok)
 
static bool isSameIteratorContainerExpression (const Token *tok1, const Token *tok2, const Settings &settings, ValueFlow::Value::LifetimeKind kind=ValueFlow::Value::LifetimeKind::Iterator)
 
static ValueFlow::Value getLifetimeIteratorValue (const Token *tok, MathLib::bigint path=0)
 
static const Token * getInvalidMethod (const Token *tok)
 
static const Token * getLoopContainer (const Token *tok)
 
static const ValueFlow::Value * getInnerLifetime (const Token *tok, nonneg int id, ErrorPath *errorPath=nullptr, int depth=4)
 
static const Token * endOfExpression (const Token *tok)
 
static bool if_findCompare (const Token *const tokBack, bool stdStringLike)
 
static std::pair< const Token *, const Token * > isMapFind (const Token *tok)
 
static const Token * skipLocalVars (const Token *const tok)
 
static const Token * findInsertValue (const Token *tok, const Token *containerTok, const Token *keyTok, const Settings &settings)
 
static bool isCpp03ContainerSizeSlow (const Token *tok)
 Is container.size() slow? More...
 
static bool isLocal (const Token *tok)
 
static bool isEarlyExit (const Token *start)
 
static const Token * singleStatement (const Token *start)
 
static const Token * singleAssignInScope (const Token *start, nonneg int varid, bool &input, bool &hasBreak, const Settings &settings)
 
static const Token * singleMemberCallInScope (const Token *start, nonneg int varid, bool &input, const Settings &settings)
 
static const Token * singleIncrementInScope (const Token *start, nonneg int varid, bool &input)
 
static const Token * singleConditionalInScope (const Token *start, nonneg int varid, const Settings &settings)
 
static bool addByOne (const Token *tok, nonneg int varid)
 
static bool accumulateBoolLiteral (const Token *tok, nonneg int varid)
 
static bool accumulateBool (const Token *tok, nonneg int varid)
 
static bool hasVarIds (const Token *tok, nonneg int var1, nonneg int var2)
 
static std::string flipMinMax (const std::string &algo)
 
static std::string minmaxCompare (const Token *condTok, nonneg int loopVar, nonneg int assignVar, bool invert=false)
 
static bool isKnownEmptyContainer (const Token *tok)
 
static const ValueFlow::Value * getOOBIterValue (const Token *tok, const ValueFlow::Value *sizeVal)
 
static bool isMutex (const Variable *var)
 
static bool isLockGuard (const Variable *var)
 
static bool isLocalMutex (const Variable *var, const Scope *scope)
 

Variables

static const CWE CWE398 (398U)
 
static const CWE CWE597 (597U)
 
static const CWE CWE628 (628U)
 
static const CWE CWE664 (664U)
 
static const CWE CWE667 (667U)
 
static const CWE CWE704 (704U)
 
static const CWE CWE762 (762U)
 
static const CWE CWE786 (786U)
 
static const CWE CWE788 (788U)
 
static const CWE CWE825 (825U)
 
static const CWE CWE833 (833U)
 
static const CWE CWE834 (834U)
 

Function Documentation

◆ accumulateBool()

static bool accumulateBool ( const Token *  tok,
nonneg int  varid 
)
static

Definition at line 2689 of file checkstl.cpp.

References Token::Match(), and Token::simpleMatch().

Referenced by CheckStl::useStlAlgorithm().

◆ accumulateBoolLiteral()

static bool accumulateBoolLiteral ( const Token *  tok,
nonneg int  varid 
)
static

Definition at line 2676 of file checkstl.cpp.

References Token::hasKnownIntValue(), Token::Match(), and Token::tokAt().

Referenced by CheckStl::useStlAlgorithm().

◆ addByOne()

static bool addByOne ( const Token *  tok,
nonneg int  varid 
)
static

◆ containerAppendsElement()

static bool containerAppendsElement ( const Library::Container *  container,
const Token *  parent 
)
static

◆ containerPopsElement()

static bool containerPopsElement ( const Library::Container *  container,
const Token *  parent 
)
static

◆ containerYieldsElement()

static bool containerYieldsElement ( const Library::Container *  container,
const Token *  parent 
)
static

◆ endOfExpression()

static const Token* endOfExpression ( const Token *  tok)
static

◆ findInsertValue()

static const Token* findInsertValue ( const Token *  tok,
const Token *  containerTok,
const Token *  keyTok,
const Settings &  settings 
)
static

◆ flipMinMax()

static std::string flipMinMax ( const std::string &  algo)
static

Definition at line 2713 of file checkstl.cpp.

Referenced by minmaxCompare().

◆ getAddressContainer()

static std::vector<const Token*> getAddressContainer ( const Token *  tok)
static

◆ getContainerFromSize()

static const Token* getContainerFromSize ( const Library::Container *  container,
const Token *  tok 
)
static

◆ getContainerIndex()

static const Token* getContainerIndex ( const Library::Container *  container,
const Token *  parent 
)
static

◆ getContainerName()

static std::string getContainerName ( const Token *  containerToken)
static

Definition at line 442 of file checkstl.cpp.

References Token::Match(), Token::str(), and Token::tokAt().

Referenced by CheckStl::iterators().

◆ getInnerLifetime()

static const ValueFlow::Value* getInnerLifetime ( const Token *  tok,
nonneg int  id,
ErrorPath *  errorPath = nullptr,
int  depth = 4 
)
static

◆ getInvalidMethod()

static const Token* getInvalidMethod ( const Token *  tok)
static

◆ getLifetimeIteratorValue()

static ValueFlow::Value getLifetimeIteratorValue ( const Token *  tok,
MathLib::bigint  path = 0 
)
static

◆ getLoopContainer()

static const Token* getLoopContainer ( const Token *  tok)
static

Definition at line 1046 of file checkstl.cpp.

References Token::astOperand2(), Token::next(), and Token::simpleMatch().

Referenced by CheckStl::invalidContainer().

◆ getOOBIterValue()

static const ValueFlow::Value* getOOBIterValue ( const Token *  tok,
const ValueFlow::Value *  sizeVal 
)
static

Definition at line 3175 of file checkstl.cpp.

References Token::values().

Referenced by CheckStl::eraseIteratorOutOfBounds().

◆ hasVarIds()

static bool hasVarIds ( const Token *  tok,
nonneg int  var1,
nonneg int  var2 
)
static

Definition at line 2701 of file checkstl.cpp.

References Token::astOperand1(), and Token::astOperand2().

Referenced by minmaxCompare(), and CheckStl::useStlAlgorithm().

◆ if_findCompare()

static bool if_findCompare ( const Token *const  tokBack,
bool  stdStringLike 
)
static

◆ indexValueString()

static std::string indexValueString ( const ValueFlow::Value &  indexValue,
const std::string &  containerName = emptyString 
)
static

◆ isCpp03ContainerSizeSlow()

static bool isCpp03ContainerSizeSlow ( const Token *  tok)
static

Is container.size() slow?

Definition at line 1750 of file checkstl.cpp.

References Variable::isStlType(), and Token::variable().

Referenced by CheckStl::size().

◆ isEarlyExit()

static bool isEarlyExit ( const Token *  start)
static

◆ isElementAccessYield()

static bool isElementAccessYield ( Library::Container::Yield  yield)
static

Definition at line 67 of file checkstl.cpp.

References Library::Container::AT_INDEX, contains(), and Library::Container::ITEM.

Referenced by containerYieldsElement().

◆ isIterator()

static bool isIterator ( const Variable *  var,
bool &  inconclusiveType 
)
static

◆ isKnownEmptyContainer()

static bool isKnownEmptyContainer ( const Token *  tok)
static

Definition at line 3090 of file checkstl.cpp.

References Token::values().

Referenced by CheckStl::knownEmptyContainer().

◆ isLocal()

static bool isLocal ( const Token *  tok)
static

Definition at line 1944 of file checkstl.cpp.

References Variable::isLocal(), Variable::isStatic(), and Token::variable().

Referenced by CheckStl::string_c_str().

◆ isLocalMutex()

static bool isLocalMutex ( const Variable *  var,
const Scope *  scope 
)
static

◆ isLockGuard()

static bool isLockGuard ( const Variable *  var)
static

Definition at line 3225 of file checkstl.cpp.

References Token::Match(), Variable::nameToken(), and Token::typeDecl().

Referenced by CheckStl::checkMutexes(), and isLocalMutex().

◆ isMapFind()

static std::pair<const Token *, const Token *> isMapFind ( const Token *  tok)
static

◆ isMutex()

static bool isMutex ( const Variable *  var)
static

Definition at line 3219 of file checkstl.cpp.

References Token::Match(), Variable::nameToken(), and Token::typeDecl().

Referenced by CheckStl::checkMutexes().

◆ isSameIteratorContainerExpression()

static bool isSameIteratorContainerExpression ( const Token *  tok1,
const Token *  tok2,
const Settings &  settings,
ValueFlow::Value::LifetimeKind  kind = ValueFlow::Value::LifetimeKind::Iterator 
)
static

◆ isVector()

static bool isVector ( const Token *  tok)
static

◆ minmaxCompare()

static std::string minmaxCompare ( const Token *  condTok,
nonneg int  loopVar,
nonneg int  assignVar,
bool  invert = false 
)
static

Definition at line 2722 of file checkstl.cpp.

References Token::astOperand1(), flipMinMax(), hasVarIds(), and Token::Match().

Referenced by CheckStl::useStlAlgorithm().

◆ singleAssignInScope()

static const Token* singleAssignInScope ( const Token *  start,
nonneg int  varid,
bool &  input,
bool &  hasBreak,
const Settings &  settings 
)
static

◆ singleConditionalInScope()

static const Token* singleConditionalInScope ( const Token *  start,
nonneg int  varid,
const Settings &  settings 
)
static

◆ singleIncrementInScope()

static const Token* singleIncrementInScope ( const Token *  start,
nonneg int  varid,
bool &  input 
)
static

◆ singleMemberCallInScope()

static const Token* singleMemberCallInScope ( const Token *  start,
nonneg int  varid,
bool &  input,
const Settings &  settings 
)
static

◆ singleStatement()

static const Token* singleStatement ( const Token *  start)
static

◆ skipLocalVars()

static const Token* skipLocalVars ( const Token *const  tok)
static

◆ skipMembers()

static const Token* skipMembers ( const Token *  tok)
static

Definition at line 412 of file checkstl.cpp.

References Token::Match(), and Token::tokAt().

Referenced by CheckStl::iterators().

Variable Documentation

◆ CWE398

const CWE CWE398(398U) ( 398U  )
static

◆ CWE597

const CWE CWE597(597U) ( 597U  )
static

Referenced by CheckStl::if_findError().

◆ CWE628

const CWE CWE628(628U) ( 628U  )
static

◆ CWE664

const CWE CWE664(664U) ( 664U  )
static

◆ CWE667

const CWE CWE667(667U) ( 667U  )
static

◆ CWE704

const CWE CWE704(704U) ( 704U  )
static

◆ CWE762

const CWE CWE762(762U) ( 762U  )
static

◆ CWE786

const CWE CWE786(786U) ( 786U  )
static

◆ CWE788

const CWE CWE788(788U) ( 788U  )
static

◆ CWE825

const CWE CWE825(825U) ( 825U  )
static

◆ CWE833

const CWE CWE833(833U) ( 833U  )
static

◆ CWE834

const CWE CWE834(834U) ( 834U  )
static