Cppcheck
Classes | Enumerations | Functions
astutils.h File Reference
#include <functional>
#include <list>
#include <stack>
#include <string>
#include <type_traits>
#include <vector>
#include "config.h"
#include "errortypes.h"
#include "library.h"
#include "mathlib.h"
#include "smallvector.h"
#include "symboldatabase.h"
#include "token.h"

Go to the source code of this file.

Classes

struct  ReferenceToken
 

Enumerations

enum class  ChildrenToVisit {
  none , op1 , op2 , op1_and_op2 ,
  done
}
 
enum class  ExprUsage {
  None , NotUsed , PassedByReference , Used ,
  Inconclusive
}
 

Functions

template<class T , class TFunc , REQUIRES("T must be a Token class", std::is_convertible< T *, const Token * >) >
void visitAstNodes (T *ast, const TFunc &visitor)
 Visit AST nodes recursively. More...
 
template<class TFunc >
const Token * findAstNode (const Token *ast, const TFunc &pred)
 
template<class TFunc >
const Token * findParent (const Token *tok, const TFunc &pred)
 
const Token * findExpression (const nonneg int exprid, const Token *start, const Token *end, const std::function< bool(const Token *)> &pred)
 
const Token * findExpression (const Token *start, const nonneg int exprid)
 
template<class T , class OuputIterator , REQUIRES("T must be a Token class", std::is_convertible< T *, const Token * >) >
void astFlattenCopy (T *tok, const char *op, OuputIterator out, nonneg int depth=100)
 
std::vector< const Token * > astFlatten (const Token *tok, const char *op)
 
std::vector< Token * > astFlatten (Token *tok, const char *op)
 
nonneg int astCount (const Token *tok, const char *op, int depth=100)
 
bool astHasToken (const Token *root, const Token *tok)
 
bool astHasExpr (const Token *tok, nonneg int exprid)
 
bool astHasVar (const Token *tok, nonneg int varid)
 
bool astIsPrimitive (const Token *tok)
 
bool astIsSignedChar (const Token *tok)
 Is expression a 'signed char' if no promotion is used. More...
 
bool astIsUnknownSignChar (const Token *tok)
 Is expression a 'char' if no promotion is used? More...
 
bool astIsGenericChar (const Token *tok)
 Is expression a char according to valueType? More...
 
bool astIsIntegral (const Token *tok, bool unknown)
 Is expression of integral type? More...
 
bool astIsUnsigned (const Token *tok)
 
bool astIsFloat (const Token *tok, bool unknown)
 Is expression of floating point type? More...
 
bool astIsBool (const Token *tok)
 Is expression of boolean type? More...
 
bool astIsPointer (const Token *tok)
 
bool astIsSmartPointer (const Token *tok)
 
bool astIsUniqueSmartPointer (const Token *tok)
 
bool astIsIterator (const Token *tok)
 
bool astIsContainer (const Token *tok)
 
bool astIsNonStringContainer (const Token *tok)
 
bool astIsContainerView (const Token *tok)
 
bool astIsContainerOwned (const Token *tok)
 
bool astIsContainerString (const Token *tok)
 
Library::Container::Action astContainerAction (const Token *tok, const Token **ftok=nullptr)
 
Library::Container::Yield astContainerYield (const Token *tok, const Token **ftok=nullptr)
 
Library::Container::Yield astFunctionYield (const Token *tok, const Settings &settings, const Token **ftok=nullptr)
 
bool astIsRangeBasedForDecl (const Token *tok)
 Is given token a range-declaration in a range-based for loop. More...
 
std::string astCanonicalType (const Token *expr, bool pointedToType)
 Get canonical type of expression. More...
 
const Token * astIsVariableComparison (const Token *tok, const std::string &comp, const std::string &rhs, const Token **vartok=nullptr)
 Is given syntax tree a variable comparison against value. More...
 
bool isVariableDecl (const Token *tok)
 
bool isStlStringType (const Token *tok)
 
bool isTemporary (const Token *tok, const Library *library, bool unknown=false)
 
const Token * previousBeforeAstLeftmostLeaf (const Token *tok)
 
Token * previousBeforeAstLeftmostLeaf (Token *tok)
 
CPPCHECKLIB const Token * nextAfterAstRightmostLeaf (const Token *tok)
 
Token * nextAfterAstRightmostLeaf (Token *tok)
 
Token * astParentSkipParens (Token *tok)
 
const Token * astParentSkipParens (const Token *tok)
 
const Token * getParentMember (const Token *tok)
 
const Token * getParentLifetime (const Token *tok)
 
const Token * getParentLifetime (const Token *tok, const Library &library)
 
std::vector< ValueType > getParentValueTypes (const Token *tok, const Settings &settings, const Token **parent=nullptr)
 
bool astIsLHS (const Token *tok)
 
bool astIsRHS (const Token *tok)
 
Token * getCondTok (Token *tok)
 
const Token * getCondTok (const Token *tok)
 
Token * getInitTok (Token *tok)
 
const Token * getInitTok (const Token *tok)
 
Token * getStepTok (Token *tok)
 
const Token * getStepTok (const Token *tok)
 
Token * getCondTokFromEnd (Token *endBlock)
 
const Token * getCondTokFromEnd (const Token *endBlock)
 
const Token * findNextTokenFromBreak (const Token *breakToken)
 For a "break" token, locate the next token to execute. More...
 
bool extractForLoopValues (const Token *forToken, nonneg int &varid, bool &knownInitValue, long long &initValue, bool &partialCond, long long &stepValue, long long &lastValue)
 Extract for loop values: loopvar varid, init value, step value, last value (inclusive) More...
 
bool precedes (const Token *tok1, const Token *tok2)
 If tok2 comes after tok1. More...
 
bool succeeds (const Token *tok1, const Token *tok2)
 If tok1 comes after tok2. More...
 
bool exprDependsOnThis (const Token *expr, bool onVar=true, nonneg int depth=0)
 
SmallVector< ReferenceToken > followAllReferences (const Token *tok, bool temporary=true, bool inconclusive=true, ErrorPath errors=ErrorPath{}, int depth=20)
 
const Token * followReferences (const Token *tok, ErrorPath *errors=nullptr)
 
CPPCHECKLIB bool isSameExpression (bool macro, const Token *tok1, const Token *tok2, const Settings &settings, bool pure, bool followVar, ErrorPath *errors=nullptr)
 
bool isEqualKnownValue (const Token *const tok1, const Token *const tok2)
 
bool isStructuredBindingVariable (const Variable *var)
 
const Token * isInLoopCondition (const Token *tok)
 
CPPCHECKLIB bool isUsedAsBool (const Token *const tok, const Settings &settings)
 Is token used as boolean, that is to say cast to a bool, or used as a condition in a if/while/for. More...
 
bool compareTokenFlags (const Token *tok1, const Token *tok2, bool macro)
 Are the tokens' flags equal? More...
 
bool isOppositeCond (bool isNot, const Token *const cond1, const Token *const cond2, const Settings &settings, bool pure, bool followVar, ErrorPath *errors=nullptr)
 Are two conditions opposite. More...
 
bool isOppositeExpression (const Token *const tok1, const Token *const tok2, const Settings &settings, bool pure, bool followVar, ErrorPath *errors=nullptr)
 
bool isConstFunctionCall (const Token *ftok, const Library &library)
 
bool isConstExpression (const Token *tok, const Library &library)
 
bool isWithoutSideEffects (const Token *tok, bool checkArrayAccess=false, bool checkReference=true)
 
bool isUniqueExpression (const Token *tok)
 
bool isEscapeFunction (const Token *ftok, const Library *library)
 
CPPCHECKLIB bool isReturnScope (const Token *const endToken, const Library &library, const Token **unknownFunc=nullptr, bool functionScope=false)
 Is scope a return scope (scope will unconditionally return) More...
 
bool isWithinScope (const Token *tok, const Variable *var, Scope::ScopeType type)
 Is tok within a scope of the given type, nested within var's scope? More...
 
const Token * getTokenArgumentFunction (const Token *tok, int &argn)
 Return the token to the function and the argument number. More...
 
Token * getTokenArgumentFunction (Token *tok, int &argn)
 
std::vector< const Variable * > getArgumentVars (const Token *tok, int argnr)
 
bool isVariableChangedByFunctionCall (const Token *tok, int indirect, nonneg int varid, const Settings &settings, bool *inconclusive)
 Is variable changed by function call? In case the answer of the question is inconclusive, e.g. More...
 
CPPCHECKLIB bool isVariableChangedByFunctionCall (const Token *tok, int indirect, const Settings &settings, bool *inconclusive)
 Is variable changed by function call? In case the answer of the question is inconclusive, e.g. More...
 
CPPCHECKLIB bool isVariableChanged (const Token *start, const Token *end, const nonneg int exprid, bool globalvar, const Settings &settings, int depth=20)
 Is variable changed in block of code? More...
 
bool isVariableChanged (const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings &settings, int depth=20)
 
bool isVariableChanged (const Token *tok, int indirect, const Settings &settings, int depth=20)
 
bool isVariableChanged (const Variable *var, const Settings &settings, int depth=20)
 
bool isVariablesChanged (const Token *start, const Token *end, int indirect, const std::vector< const Variable * > &vars, const Settings &settings)
 
bool isThisChanged (const Token *tok, int indirect, const Settings &settings)
 
const Token * findVariableChanged (const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings &settings, int depth=20)
 
Token * findVariableChanged (Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings &settings, int depth=20)
 
CPPCHECKLIB const Token * findExpressionChanged (const Token *expr, const Token *start, const Token *end, const Settings &settings, int depth=20)
 
const Token * findExpressionChangedSkipDeadCode (const Token *expr, const Token *start, const Token *end, const Settings &settings, const std::function< std::vector< MathLib::bigint >(const Token *tok)> &evaluate, int depth=20)
 
bool isExpressionChangedAt (const Token *expr, const Token *tok, int indirect, bool globalvar, const Settings &settings, int depth=20)
 
bool isAliasOf (const Token *tok, nonneg int varid, bool *inconclusive=nullptr)
 If token is an alias if another variable. More...
 
bool isAliasOf (const Token *tok, const Token *expr, int *indirect=nullptr, bool *inconclusive=nullptr)
 
bool isAliased (const Variable *var)
 
const Token * getArgumentStart (const Token *ftok)
 
int numberOfArguments (const Token *ftok)
 Determines the number of arguments - if token is a function call or macro. More...
 
int numberOfArgumentsWithoutAst (const Token *start)
 Get number of arguments without using AST. More...
 
std::vector< const Token * > getArguments (const Token *ftok)
 Get arguments (AST) More...
 
int getArgumentPos (const Variable *var, const Function *f)
 
const Token * getIteratorExpression (const Token *tok)
 
bool isIteratorPair (const std::vector< const Token * > &args)
 Are the arguments a pair of iterators/pointers? More...
 
CPPCHECKLIB const Token * findLambdaStartToken (const Token *last)
 
CPPCHECKLIB const Token * findLambdaEndToken (const Token *first)
 find lambda function end token More...
 
CPPCHECKLIB Token * findLambdaEndToken (Token *first)
 
bool isLikelyStream (const Token *stream)
 
bool isLikelyStreamRead (const Token *op)
 do we see a likely write of rhs through overloaded operator s >> x; a & x; More...
 
bool isCPPCast (const Token *tok)
 
bool isConstVarExpression (const Token *tok, const std::function< bool(const Token *)> &skipPredicate=nullptr)
 
bool isLeafDot (const Token *tok)
 
ExprUsage getExprUsage (const Token *tok, int indirect, const Settings &settings)
 
const Variable * getLHSVariable (const Token *tok)
 
const Token * getLHSVariableToken (const Token *tok)
 
std::vector< const Variable * > getLHSVariables (const Token *tok)
 
const Token * findAllocFuncCallToken (const Token *expr, const Library &library)
 Find a allocation function call in expression, so result of expression is allocated memory/resource. More...
 
bool isScopeBracket (const Token *tok)
 
CPPCHECKLIB bool isNullOperand (const Token *expr)
 
bool isGlobalData (const Token *expr)
 
bool isUnevaluated (const Token *tok)
 

Enumeration Type Documentation

◆ ChildrenToVisit

enum ChildrenToVisit
strong
Enumerator
none 
op1 
op2 
op1_and_op2 
done 

Definition at line 42 of file astutils.h.

◆ ExprUsage

enum ExprUsage
strong
Enumerator
None 
NotUsed 
PassedByReference 
Used 
Inconclusive 

Definition at line 439 of file astutils.h.

Function Documentation

◆ astCanonicalType()

std::string astCanonicalType ( const Token *  expr,
bool  pointedToType 
)

Get canonical type of expression.

const/static/etc are not included and neither *&. For example: Expression type Return std::string std::string int * int static const int int std::vector<T> std::vector

Definition at line 326 of file astutils.cpp.

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

Referenced by Library::getFunctionName().

◆ astContainerAction()

Library::Container::Action astContainerAction ( const Token *  tok,
const Token **  ftok = nullptr 
)

◆ astContainerYield()

Library::Container::Yield astContainerYield ( const Token *  tok,
const Token **  ftok = nullptr 
)

◆ astCount()

nonneg int astCount ( const Token *  tok,
const char *  op,
int  depth = 100 
)

Definition at line 124 of file astutils.cpp.

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

Referenced by numberOfArguments().

◆ astFlatten() [1/2]

std::vector<const Token*> astFlatten ( const Token *  tok,
const char *  op 
)

◆ astFlatten() [2/2]

std::vector<Token*> astFlatten ( Token *  tok,
const char *  op 
)

Definition at line 117 of file astutils.cpp.

References astFlattenCopy().

◆ astFlattenCopy()

template<class T , class OuputIterator , REQUIRES("T must be a Token class", std::is_convertible< T *, const Token * >) >
void astFlattenCopy ( T *  tok,
const char *  op,
OuputIterator  out,
nonneg int  depth = 100 
)

Definition at line 120 of file astutils.h.

Referenced by astFlatten().

◆ astFunctionYield()

Library::Container::Yield astFunctionYield ( const Token *  tok,
const Settings &  settings,
const Token **  ftok = nullptr 
)

◆ astHasExpr()

bool astHasExpr ( const Token *  tok,
nonneg int  exprid 
)

Definition at line 152 of file astutils.cpp.

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

◆ astHasToken()

bool astHasToken ( const Token *  root,
const Token *  tok 
)

Definition at line 134 of file astutils.cpp.

References Token::astParent().

Referenced by followAllReferences().

◆ astHasVar()

bool astHasVar ( const Token *  tok,
nonneg int  varid 
)

Definition at line 143 of file astutils.cpp.

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

◆ astIsBool()

bool astIsBool ( const Token *  tok)

◆ astIsContainer()

bool astIsContainer ( const Token *  tok)

◆ astIsContainerOwned()

bool astIsContainerOwned ( const Token *  tok)

◆ astIsContainerString()

bool astIsContainerString ( const Token *  tok)

◆ astIsContainerView()

bool astIsContainerView ( const Token *  tok)

◆ astIsFloat()

bool astIsFloat ( const Token *  tok,
bool  unknown 
)

◆ astIsGenericChar()

bool astIsGenericChar ( const Token *  tok)

Is expression a char according to valueType?

Definition at line 181 of file astutils.cpp.

References astIsPointer(), ValueType::type, and Token::valueType().

Referenced by getInitListSize(), and valueFlowGetStrLength().

◆ astIsIntegral()

bool astIsIntegral ( const Token *  tok,
bool  unknown 
)

◆ astIsIterator()

bool astIsIterator ( const Token *  tok)

◆ astIsLHS()

bool astIsLHS ( const Token *  tok)

◆ astIsNonStringContainer()

bool astIsNonStringContainer ( const Token *  tok)

◆ astIsPointer()

bool astIsPointer ( const Token *  tok)

◆ astIsPrimitive()

bool astIsPrimitive ( const Token *  tok)

◆ astIsRangeBasedForDecl()

bool astIsRangeBasedForDecl ( const Token *  tok)

Is given token a range-declaration in a range-based for loop.

Definition at line 321 of file astutils.cpp.

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

Referenced by CheckOther::checkConstPointer(), CheckOther::checkConstVariable(), CheckOther::checkPassByReference(), CheckClass::initializeVarList(), and Token::typeDecl().

◆ astIsRHS()

bool astIsRHS ( const Token *  tok)

◆ astIsSignedChar()

bool astIsSignedChar ( const Token *  tok)

Is expression a 'signed char' if no promotion is used.

Definition at line 171 of file astutils.cpp.

References astIsCharWithSign().

Referenced by CheckOther::checkCharVariable().

◆ astIsSmartPointer()

bool astIsSmartPointer ( const Token *  tok)

◆ astIsUniqueSmartPointer()

bool astIsUniqueSmartPointer ( const Token *  tok)

◆ astIsUnknownSignChar()

bool astIsUnknownSignChar ( const Token *  tok)

Is expression a 'char' if no promotion is used?

Definition at line 176 of file astutils.cpp.

References astIsCharWithSign().

Referenced by CheckOther::checkCharVariable().

◆ astIsUnsigned()

bool astIsUnsigned ( const Token *  tok)

◆ astIsVariableComparison()

const Token* astIsVariableComparison ( const Token *  tok,
const std::string &  comp,
const std::string &  rhs,
const Token **  vartok = nullptr 
)

Is given syntax tree a variable comparison against value.

Definition at line 351 of file astutils.cpp.

References Token::astOperand1(), Token::astOperand2(), Token::isComparisonOp(), match(), Token::str(), and Token::varId().

Referenced by CheckLeakAutoVar::checkScope(), CheckUninitVar::checkScopeForVariable(), ifvar(), and notvar().

◆ astParentSkipParens() [1/2]

const Token* astParentSkipParens ( const Token *  tok)

◆ astParentSkipParens() [2/2]

Token* astParentSkipParens ( Token *  tok)

◆ compareTokenFlags()

bool compareTokenFlags ( const Token *  tok1,
const Token *  tok2,
bool  macro 
)

◆ exprDependsOnThis()

bool exprDependsOnThis ( const Token *  expr,
bool  onVar = true,
nonneg int  depth = 0 
)

◆ extractForLoopValues()

bool extractForLoopValues ( const Token *  forToken,
nonneg int &  varid,
bool &  knownInitValue,
long long &  initValue,
bool &  partialCond,
long long &  stepValue,
long long &  lastValue 
)

◆ findAllocFuncCallToken()

const Token* findAllocFuncCallToken ( const Token *  expr,
const Library &  library 
)

Find a allocation function call in expression, so result of expression is allocated memory/resource.

Definition at line 3501 of file astutils.cpp.

References Token::astOperand1(), Token::astOperand2(), Library::getAllocFuncInfo(), Token::isCast(), Token::Match(), Token::previous(), and Token::simpleMatch().

Referenced by CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(), and CheckUninitVar::checkScope().

◆ findAstNode()

template<class TFunc >
const Token* findAstNode ( const Token *  ast,
const TFunc &  pred 
)

◆ findExpression() [1/2]

const Token* findExpression ( const nonneg int  exprid,
const Token *  start,
const Token *  end,
const std::function< bool(const Token *)> &  pred 
)

Definition at line 50 of file astutils.cpp.

References Token::next(), and precedes().

Referenced by CheckOther::checkInnerScope(), and findVariableChanged().

◆ findExpression() [2/2]

const Token* findExpression ( const Token *  start,
const nonneg int  exprid 
)

◆ findExpressionChanged()

CPPCHECKLIB const Token* findExpressionChanged ( const Token *  expr,
const Token *  start,
const Token *  end,
const Settings &  settings,
int  depth = 20 
)

◆ findExpressionChangedSkipDeadCode()

const Token* findExpressionChangedSkipDeadCode ( const Token *  expr,
const Token *  start,
const Token *  end,
const Settings &  settings,
const std::function< std::vector< MathLib::bigint >(const Token *tok)> &  evaluate,
int  depth = 20 
)

◆ findLambdaEndToken() [1/2]

CPPCHECKLIB const Token* findLambdaEndToken ( const Token *  first)

◆ findLambdaEndToken() [2/2]

CPPCHECKLIB Token* findLambdaEndToken ( Token *  first)

Definition at line 3199 of file astutils.cpp.

References findLambdaEndTokenGeneric().

◆ findLambdaStartToken()

CPPCHECKLIB const Token* findLambdaStartToken ( const Token *  last)

◆ findNextTokenFromBreak()

const Token* findNextTokenFromBreak ( const Token *  breakToken)

For a "break" token, locate the next token to execute.

The token will be either a "}" or a ";".

Definition at line 912 of file astutils.cpp.

References Scope::bodyEnd, Scope::isLoopScope(), Token::linkAt(), Scope::nestedIn, Token::next(), Token::scope(), Token::simpleMatch(), and Scope::type.

Referenced by FwdAnalysis::check(), FwdAnalysis::checkRecursive(), and CheckVaarg::va_list_usage().

◆ findParent()

template<class TFunc >
const Token* findParent ( const Token *  tok,
const TFunc &  pred 
)

Definition at line 102 of file astutils.h.

References Token::astParent().

Referenced by CheckOther::checkKnownPointerToBool(), and setTokenValue().

◆ findVariableChanged() [1/2]

const Token* findVariableChanged ( const Token *  start,
const Token *  end,
int  indirect,
const nonneg int  exprid,
bool  globalvar,
const Settings &  settings,
int  depth = 20 
)

Definition at line 2872 of file astutils.cpp.

References findVariableChanged().

◆ findVariableChanged() [2/2]

Token* findVariableChanged ( Token *  start,
const Token *  end,
int  indirect,
const nonneg int  exprid,
bool  globalvar,
const Settings &  settings,
int  depth = 20 
)

◆ followAllReferences()

SmallVector<ReferenceToken> followAllReferences ( const Token *  tok,
bool  temporary = true,
bool  inconclusive = true,
ErrorPath  errors = ErrorPath{},
int  depth = 20 
)

◆ followReferences()

const Token* followReferences ( const Token *  tok,
ErrorPath *  errors = nullptr 
)

Definition at line 1369 of file astutils.cpp.

References followAllReferences().

Referenced by isSameExpression().

◆ getArgumentPos()

int getArgumentPos ( const Variable *  var,
const Function *  f 
)

Definition at line 3087 of file astutils.cpp.

References Function::argumentList.

◆ getArguments()

std::vector<const Token *> getArguments ( const Token *  ftok)

Get arguments (AST)

Definition at line 3083 of file astutils.cpp.

References astFlatten(), and getArgumentStart().

Referenced by ValueFlowAnalyzer::analyzeCondition(), CheckBufferOverrun::argumentSize(), CheckBufferOverrun::bufferOverflow(), CheckLeakAutoVar::changeAllocStatusIfRealloc(), CheckClass::checkConstFunc(), CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(), CheckOther::checkOverlappingWrite(), CheckMemoryLeakInFunction::checkReallocUsage(), FwdAnalysis::checkRecursive(), CheckClass::checkUselessOverride(), CheckStl::eraseIteratorOutOfBounds(), Scope::findFunction(), followAllReferences(), LifetimeStore::fromFunctionArg(), Lambda::getCaptures(), getContainerSizeFromConstructor(), CTU::getFileInfo(), getInitListSize(), getLifetimeTokens(), CheckMemoryLeak::getReallocationType(), CheckFunctions::invalidFunctionUsage(), isConstFunctionCall(), isConstVarExpression(), CheckMemoryLeak::isOpenDevNull(), CheckMemoryLeak::isReopenStandardStream(), ContainerExpressionAnalyzer::isWritable(), CheckStl::knownEmptyContainer(), CheckFunctions::memsetInvalid2ndParam(), CheckFunctions::memsetZeroBytes(), CheckStl::mismatchingContainerIterator(), CheckStl::mismatchingContainers(), CheckNullPointer::nullConstantDereference(), CheckString::overlappingStrcmp(), CheckNullPointer::parseFunctionCall(), FwdAnalysis::possiblyAliased(), SymbolDatabase::setValueTypeInTokenList(), CheckString::sprintfOverlappingData(), CheckBufferOverrun::stringNotZeroTerminated(), CheckStl::uselessCalls(), valueFlowArrayElement(), valueFlowContainerSize(), valueFlowDynamicBufferSize(), valueFlowFunctionReturn(), valueFlowLibraryFunction(), valueFlowLifetimeClassConstructor(), valueFlowLifetimeConstructor(), valueFlowLifetimeFunction(), valueFlowSmartPointer(), and valueFlowSubFunction().

◆ getArgumentStart()

const Token* getArgumentStart ( const Token *  ftok)

◆ getArgumentVars()

std::vector<const Variable*> getArgumentVars ( const Token *  tok,
int  argnr 
)

◆ getCondTok() [1/2]

const Token* getCondTok ( const Token *  tok)

Definition at line 884 of file astutils.cpp.

References getCondTokImpl().

◆ getCondTok() [2/2]

Token* getCondTok ( Token *  tok)

◆ getCondTokFromEnd() [1/2]

const Token* getCondTokFromEnd ( const Token *  endBlock)

Definition at line 893 of file astutils.cpp.

References getCondTokFromEndImpl().

◆ getCondTokFromEnd() [2/2]

Token* getCondTokFromEnd ( Token *  endBlock)

◆ getExprUsage()

ExprUsage getExprUsage ( const Token *  tok,
int  indirect,
const Settings &  settings 
)

◆ getInitTok() [1/2]

const Token* getInitTok ( const Token *  tok)

Definition at line 901 of file astutils.cpp.

References getInitTokImpl().

◆ getInitTok() [2/2]

Token* getInitTok ( Token *  tok)

◆ getIteratorExpression()

const Token* getIteratorExpression ( const Token *  tok)

◆ getLHSVariable()

const Variable* getLHSVariable ( const Token *  tok)

◆ getLHSVariables()

std::vector<const Variable*> getLHSVariables ( const Token *  tok)

◆ getLHSVariableToken()

const Token* getLHSVariableToken ( const Token *  tok)

◆ getParentLifetime() [1/2]

const Token* getParentLifetime ( const Token *  tok)

◆ getParentLifetime() [2/2]

const Token* getParentLifetime ( const Token *  tok,
const Library &  library 
)

Definition at line 642 of file astutils.cpp.

References getParentMembers().

◆ getParentMember()

const Token* getParentMember ( const Token *  tok)

◆ getParentValueTypes()

std::vector<ValueType> getParentValueTypes ( const Token *  tok,
const Settings &  settings,
const Token **  parent = nullptr 
)

◆ getStepTok() [1/2]

const Token* getStepTok ( const Token *  tok)

Definition at line 908 of file astutils.cpp.

References getStepTokImpl().

◆ getStepTok() [2/2]

Token* getStepTok ( Token *  tok)

◆ getTokenArgumentFunction() [1/2]

const Token* getTokenArgumentFunction ( const Token *  tok,
int &  argn 
)

◆ getTokenArgumentFunction() [2/2]

Token* getTokenArgumentFunction ( Token *  tok,
int &  argn 
)

Definition at line 2364 of file astutils.cpp.

References getTokenArgumentFunctionImpl().

◆ isAliased()

bool isAliased ( const Variable *  var)

◆ isAliasOf() [1/2]

bool isAliasOf ( const Token *  tok,
const Token *  expr,
int *  indirect = nullptr,
bool *  inconclusive = nullptr 
)

Definition at line 1038 of file astutils.cpp.

References astIsPointer(), Token::exprId(), findAstNode(), and followAllReferences().

◆ isAliasOf() [2/2]

bool isAliasOf ( const Token *  tok,
nonneg int  varid,
bool *  inconclusive = nullptr 
)

If token is an alias if another variable.

Definition at line 1017 of file astutils.cpp.

References inconclusive, Token::values(), and Token::varId().

Referenced by isAliased(), and isExpressionChangedAt().

◆ isConstExpression()

bool isConstExpression ( const Token *  tok,
const Library &  library 
)

◆ isConstFunctionCall()

bool isConstFunctionCall ( const Token *  ftok,
const Library &  library 
)

◆ isConstVarExpression()

bool isConstVarExpression ( const Token *  tok,
const std::function< bool(const Token *)> &  skipPredicate = nullptr 
)

◆ isCPPCast()

bool isCPPCast ( const Token *  tok)

◆ isEqualKnownValue()

bool isEqualKnownValue ( const Token *const  tok1,
const Token *const  tok2 
)

◆ isEscapeFunction()

bool isEscapeFunction ( const Token *  ftok,
const Library *  library 
)

Definition at line 2155 of file astutils.cpp.

References Token::function(), Library::isnoreturn(), and Token::Match().

◆ isExpressionChangedAt()

bool isExpressionChangedAt ( const Token *  expr,
const Token *  tok,
int  indirect,
bool  globalvar,
const Settings &  settings,
int  depth = 20 
)

Definition at line 2844 of file astutils.cpp.

References Token::exprId(), and isExpressionChangedAt().

◆ isGlobalData()

bool isGlobalData ( const Token *  expr)

◆ isInLoopCondition()

const Token* isInLoopCondition ( const Token *  tok)

◆ isIteratorPair()

bool isIteratorPair ( const std::vector< const Token * > &  args)

◆ isLeafDot()

bool isLeafDot ( const Token *  tok)

◆ isLikelyStream()

bool isLikelyStream ( const Token *  stream)

◆ isLikelyStreamRead()

bool isLikelyStreamRead ( const Token *  op)

◆ isNullOperand()

CPPCHECKLIB bool isNullOperand ( const Token *  expr)

◆ isOppositeCond()

bool isOppositeCond ( bool  isNot,
const Token *const  cond1,
const Token *const  cond2,
const Settings &  settings,
bool  pure,
bool  followVar,
ErrorPath *  errors = nullptr 
)

Are two conditions opposite.

Parameters
isNotdo you want to know if cond1 is !cond2 or if cond1 and cond2 are non-overlapping. true: cond1==!cond2 false: cond1==true => cond2==false
cond1condition1
cond2condition2
settingssettings
pureboolean

Definition at line 1785 of file astutils.cpp.

References Token::astOperand1(), Token::astOperand2(), Token::astSibling(), Library::Container::EMPTY, ValueFlow::Value::intvalue, Token::isComparisonOp(), Library::isContainerYield(), isDifferentKnownValues(), isSameExpression(), isUsedAsBool(), isZeroBoundCond(), op1, op2, Library::Container::SIZE, and Token::str().

Referenced by CheckCondition::checkIncorrectLogicOperator(), isOppositeExpression(), OppositeExpressionAnalyzer::match(), and CheckCondition::multiCondition().

◆ isOppositeExpression()

bool isOppositeExpression ( const Token *const  tok1,
const Token *const  tok2,
const Settings &  settings,
bool  pure,
bool  followVar,
ErrorPath *  errors = nullptr 
)

◆ isReturnScope()

CPPCHECKLIB bool isReturnScope ( const Token *const  endToken,
const Library &  library,
const Token **  unknownFunc = nullptr,
bool  functionScope = false 
)

◆ isSameExpression()

CPPCHECKLIB bool isSameExpression ( bool  macro,
const Token *  tok1,
const Token *  tok2,
const Settings &  settings,
bool  pure,
bool  followVar,
ErrorPath *  errors = nullptr 
)

Definition at line 1556 of file astutils.cpp.

References astIsBoolLike(), Token::astOperand1(), Token::astOperand2(), Token::astParent(), compareTokenFlags(), ValueType::constness, Token::eIncDecOp, Token::enumerator(), findExpressionChanged(), followReferences(), followVariableExpression(), followVariableExpressionError(), Token::function(), ValueFlow::Value::intvalue, Token::isAssignmentOp(), Token::isAttributeConst(), Token::isAttributePure(), Token::isBinaryOp(), Token::isCpp(), isDifferentKnownValues(), Library::isFunctionConst(), Token::isName(), isSameConstantValue(), Settings::library, Token::link(), Token::Match(), Token::next(), Token::originalName(), ValueType::pointer, precedes(), Token::previous(), Token::simpleMatch(), Token::str(), Token::tokAt(), Token::tokType(), ValueType::type, Token::valueType(), Token::variable(), Token::varId(), and ValueType::VOID.

Referenced by CheckCondition::alwaysTrueFalse(), CheckOther::checkDuplicateBranch(), CheckCondition::checkDuplicateConditionalAssign(), CheckOther::checkDuplicateExpression(), CheckOther::checkEvaluationOrder(), CheckStl::checkFindInsert(), CheckCondition::checkIncorrectLogicOperator(), CheckCondition::checkInvalidTestForOverflow(), CheckOther::checkKnownArgument(), CheckOther::checkOverlappingWrite(), FwdAnalysis::checkRecursive(), CheckCondition::duplicateCondition(), findInsertValue(), FwdAnalysis::hasOperand(), CheckStl::isContainerSize(), isOppositeCond(), isOppositeExpression(), CheckCondition::isOverlappingCond(), isSameIteratorContainerExpression(), SameExpressionAnalyzer::match(), CheckStl::mismatchingContainers(), CheckCondition::multiCondition2(), CheckString::overlappingStrcmp(), FwdAnalysis::possiblyAliased(), CheckString::sprintfOverlappingData(), CheckBufferOverrun::stringNotZeroTerminated(), and valueFlowSameExpressions().

◆ isScopeBracket()

bool isScopeBracket ( const Token *  tok)

Definition at line 2273 of file astutils.cpp.

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

Referenced by valueFlowForwardLifetime().

◆ isStlStringType()

bool isStlStringType ( const Token *  tok)

Definition at line 409 of file astutils.cpp.

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

Referenced by CheckStl::string_c_str().

◆ isStructuredBindingVariable()

bool isStructuredBindingVariable ( const Variable *  var)

◆ isTemporary()

bool isTemporary ( const Token *  tok,
const Library *  library,
bool  unknown = false 
)

◆ isThisChanged()

bool isThisChanged ( const Token *  tok,
int  indirect,
const Settings &  settings 
)

◆ isUnevaluated()

bool isUnevaluated ( const Token *  tok)

◆ isUniqueExpression()

bool isUniqueExpression ( const Token *  tok)

◆ isUsedAsBool()

CPPCHECKLIB bool isUsedAsBool ( const Token *const  tok,
const Settings &  settings 
)

◆ isVariableChanged() [1/4]

CPPCHECKLIB bool isVariableChanged ( const Token *  start,
const Token *  end,
const nonneg int  exprid,
bool  globalvar,
const Settings &  settings,
int  depth = 20 
)

Is variable changed in block of code?

Definition at line 2764 of file astutils.cpp.

References findVariableChanged().

◆ isVariableChanged() [2/4]

bool isVariableChanged ( const Token *  start,
const Token *  end,
int  indirect,
const nonneg int  exprid,
bool  globalvar,
const Settings &  settings,
int  depth = 20 
)

Definition at line 2769 of file astutils.cpp.

References findVariableChanged().

◆ isVariableChanged() [3/4]

bool isVariableChanged ( const Token *  tok,
int  indirect,
const Settings &  settings,
int  depth = 20 
)

Definition at line 2546 of file astutils.cpp.

References astIsContainer(), astIsLHS(), astIsPointer(), astIsRHS(), astIsSmartPointer(), Token::astOperand1(), Token::astParent(), Library::Container::AT_INDEX, Library::Container::BUFFER, Library::Container::BUFFER_NT, Library::Container::CHANGE, Library::Container::CHANGE_CONTENT, Library::Container::CHANGE_INTERNAL, Library::Container::CLEAR, ValueType::container, contains(), Token::eIncDecOp, Library::Container::EMPTY, Library::Container::END_ITERATOR, Library::Container::ERASE, Library::Container::FIND, Token::function(), Library::Container::getAction(), getLHSVariable(), Library::Container::getYield(), inconclusive, Library::Container::INSERT, Token::isArithmeticalOp(), Token::isBinaryOp(), Token::isCast(), Variable::isConst(), Function::isConst(), ValueType::isConst(), isConstVarExpression(), Token::isCpp(), isFunctionCall(), Library::isFunctionConst(), Token::isIncDecOp(), isLikelyStream(), isLikelyStreamRead(), Variable::isLocal(), Variable::isPointer(), Variable::isReference(), Token::isUnaryOp(), isVariableChangedByFunctionCall(), Library::Container::ITEM, Library::Container::ITERATOR, Settings::library, Token::link(), Token::Match(), Variable::nameToken(), Token::next(), Library::Container::POP, Token::previous(), Library::Container::PUSH, Library::Container::RESIZE, Token::simpleMatch(), Library::Container::SIZE, Library::Container::START_ITERATOR, Library::Container::stdAssociativeLike, Token::str(), Token::tokAt(), Token::tokType(), Token::valueType(), and Token::variable().

Referenced by ConditionHandler::afterCondition(), CheckCondition::assignIfParseScope(), CheckOther::checkConstPointer(), CheckOther::checkConstVariable(), CheckOther::checkDuplicateExpression(), CheckOther::checkPassByReference(), CheckOther::checkRedundantCopy(), CheckAutoVariables::checkVarLifetimeScope(), ExpressionAnalyzer::ExpressionAnalyzer(), fillProgramMemoryFromAssignments(), followVariableExpression(), getUnsafeFunction(), ValueFlowAnalyzer::isAliasModified(), isExpressionChangedAt(), CheckUnusedVar::isFunctionWithoutSideEffects(), ValueFlowAnalyzer::isModified(), isThisChanged(), isVariablesChanged(), removeModifiedVars(), singleAssignInScope(), singleConditionalInScope(), singleMemberCallInScope(), valueFlowContainerSize(), valueFlowForLoopSimplify(), valueFlowLifetimeFunction(), and CheckUninitVar::valueFlowUninit().

◆ isVariableChanged() [4/4]

bool isVariableChanged ( const Variable *  var,
const Settings &  settings,
int  depth = 20 
)

◆ isVariableChangedByFunctionCall() [1/2]

CPPCHECKLIB bool isVariableChangedByFunctionCall ( const Token *  tok,
int  indirect,
const Settings &  settings,
bool *  inconclusive 
)

Is variable changed by function call? In case the answer of the question is inconclusive, e.g.

because the function declaration is not known the return value is false and the output parameter inconclusive is set to true

Parameters
toktoken of variable in function call
settingsprogram settings
inconclusivepointer to output variable which indicates that the answer of the question is inconclusive

Definition at line 2438 of file astutils.cpp.

References astIsPrimitive(), Token::astParent(), Library::ArgumentChecks::DIR_IN, Library::ArgumentChecks::DIR_INOUT, Library::ArgumentChecks::DIR_OUT, Token::eType, Token::function(), Library::getArgDirection(), getArgumentVars(), getTokenArgumentFunction(), inconclusive, isArray(), Token::isCpp(), isCPPCastKeyword(), ValueType::isIntegral(), Token::isKeyword(), Token::isName(), Library::isnullargbad(), isTrivialConstructor(), Library::isuninitargbad(), Settings::library, Token::link(), Token::Match(), Token::next(), ValueType::pointer, Token::previous(), Token::simpleMatch(), startsWith(), Token::str(), Token::tokAt(), Token::tokType(), ValueType::UNKNOWN_TYPE, Token::valueType(), and Token::variable().

◆ isVariableChangedByFunctionCall() [2/2]

bool isVariableChangedByFunctionCall ( const Token *  tok,
int  indirect,
nonneg int  varid,
const Settings &  settings,
bool *  inconclusive 
)

Is variable changed by function call? In case the answer of the question is inconclusive, e.g.

because the function declaration is not known the return value is false and the output parameter inconclusive is set to true

Parameters
tokast tree
varidVariable Id
settingsprogram settings
inconclusivepointer to output variable which indicates that the answer of the question is inconclusive

Definition at line 2263 of file astutils.cpp.

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

Referenced by CheckOther::checkAccessOfMovedVariable(), CheckOther::checkConstPointer(), CheckOther::checkConstVariable(), CheckClass::initializeVarList(), isContainerSizeChangedByFunction(), ValueFlowAnalyzer::isModified(), isVariableChanged(), and CheckUninitVar::valueFlowUninit().

◆ isVariableDecl()

bool isVariableDecl ( const Token *  tok)

◆ isVariablesChanged()

bool isVariablesChanged ( const Token *  start,
const Token *  end,
int  indirect,
const std::vector< const Variable * > &  vars,
const Settings &  settings 
)

◆ isWithinScope()

bool isWithinScope ( const Token *  tok,
const Variable *  var,
Scope::ScopeType  type 
)

Is tok within a scope of the given type, nested within var's scope?

Definition at line 2250 of file astutils.cpp.

References Scope::nestedIn, Variable::scope(), Token::scope(), and Scope::type.

Referenced by CheckOther::checkVariableScope().

◆ isWithoutSideEffects()

bool isWithoutSideEffects ( const Token *  tok,
bool  checkArrayAccess = false,
bool  checkReference = true 
)

◆ nextAfterAstRightmostLeaf() [1/2]

CPPCHECKLIB const Token* nextAfterAstRightmostLeaf ( const Token *  tok)

◆ nextAfterAstRightmostLeaf() [2/2]

Token* nextAfterAstRightmostLeaf ( Token *  tok)

Definition at line 552 of file astutils.cpp.

References nextAfterAstRightmostLeafGeneric().

◆ numberOfArguments()

int numberOfArguments ( const Token *  ftok)

Determines the number of arguments - if token is a function call or macro.

Parameters
ftokstart token which is supposed to be the function/macro name.
Returns
Number of arguments

Definition at line 3063 of file astutils.cpp.

References astCount(), and getArgumentStart().

Referenced by CheckLeakAutoVar::changeAllocStatusIfRealloc(), CheckMemoryLeak::getAllocationType(), getArgumentVars(), getParentValueTypes(), CheckMemoryLeak::getReallocationType(), TemplateSimplifier::instantiateMatch(), CheckMemoryLeak::isOpenDevNull(), CheckMemoryLeak::isReopenStandardStream(), and CheckFunctions::memsetZeroBytes().

◆ numberOfArgumentsWithoutAst()

int numberOfArgumentsWithoutAst ( const Token *  start)

Get number of arguments without using AST.

Definition at line 3067 of file astutils.cpp.

References Token::next(), Token::nextArgument(), Token::simpleMatch(), and Token::str().

Referenced by Library::matchArguments().

◆ precedes()

bool precedes ( const Token *  tok1,
const Token *  tok2 
)

◆ previousBeforeAstLeftmostLeaf() [1/2]

const Token* previousBeforeAstLeftmostLeaf ( const Token *  tok)

◆ previousBeforeAstLeftmostLeaf() [2/2]

Token* previousBeforeAstLeftmostLeaf ( Token *  tok)

Definition at line 518 of file astutils.cpp.

References previousBeforeAstLeftmostLeafGeneric().

◆ succeeds()

bool succeeds ( const Token *  tok1,
const Token *  tok2 
)

◆ visitAstNodes()

template<class T , class TFunc , REQUIRES("T must be a Token class", std::is_convertible< T *, const Token * >) >
void visitAstNodes ( T *  ast,
const TFunc &  visitor 
)