cppcheck  1.89
About: Cppcheck is a static analysis tool for C/C++ code. It checks for memory leaks, mismatching allocation-deallocation, buffer overrun, and many more.
  Fossies Dox: cppcheck-1.89.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

astutils.h File Reference
#include <functional>
#include <string>
#include <vector>
#include "errorlogger.h"
#include "utils.h"
Include dependency graph for astutils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  PathAnalysis
 
struct  PathAnalysis::Info
 
class  FwdAnalysis
 
struct  FwdAnalysis::KnownAndToken
 
struct  FwdAnalysis::Result
 

Enumerations

enum  ChildrenToVisit {
  ChildrenToVisit::none, ChildrenToVisit::op1, ChildrenToVisit::op2, ChildrenToVisit::op1_and_op2,
  ChildrenToVisit::done
}
 

Functions

void visitAstNodes (const Token *ast, std::function< ChildrenToVisit(const Token *)> visitor)
 
std::vector< const Token * > astFlatten (const Token *tok, const char *op)
 
bool astHasToken (const Token *root, const Token *tok)
 
bool astHasVar (const Token *tok, int varid)
 
bool astIsSignedChar (const Token *tok)
 
bool astIsUnknownSignChar (const Token *tok)
 
bool astIsIntegral (const Token *tok, bool unknown)
 
bool astIsFloat (const Token *tok, bool unknown)
 
bool astIsBool (const Token *tok)
 
bool astIsPointer (const Token *tok)
 
bool astIsIterator (const Token *tok)
 
bool astIsContainer (const Token *tok)
 
std::string astCanonicalType (const Token *expr)
 
const TokenastIsVariableComparison (const Token *tok, const std::string &comp, const std::string &rhs, const Token **vartok=nullptr)
 
const TokennextAfterAstRightmostLeaf (const Token *tok)
 
TokenastParentSkipParens (Token *tok)
 
const TokenastParentSkipParens (const Token *tok)
 
bool precedes (const Token *tok1, const Token *tok2)
 If tok2 comes after tok1. More...
 
bool isSameExpression (bool cpp, bool macro, const Token *tok1, const Token *tok2, const Library &library, bool pure, bool followVar, ErrorPath *errors=nullptr)
 
bool isEqualKnownValue (const Token *const tok1, const Token *const tok2)
 
bool isDifferentKnownValues (const Token *const tok1, const Token *const tok2)
 
bool isOppositeCond (bool isNot, bool cpp, const Token *const cond1, const Token *const cond2, const Library &library, bool pure, bool followVar, ErrorPath *errors=nullptr)
 
bool isOppositeExpression (bool cpp, const Token *const tok1, const Token *const tok2, const Library &library, bool pure, bool followVar, ErrorPath *errors=nullptr)
 
bool isConstExpression (const Token *tok, const Library &library, bool pure, bool cpp)
 
bool isWithoutSideEffects (bool cpp, const Token *tok)
 
bool isUniqueExpression (const Token *tok)
 
bool isReturnScope (const Token *endToken, const Settings *settings=nullptr, bool functionScope=false)
 
bool isVariableChangedByFunctionCall (const Token *tok, int indirect, int varid, const Settings *settings, bool *inconclusive)
 
bool isVariableChangedByFunctionCall (const Token *tok, int indirect, const Settings *settings, bool *inconclusive)
 
bool isVariableChanged (const Token *start, const Token *end, const int varid, bool globalvar, const Settings *settings, bool cpp, int depth=20)
 
bool isVariableChanged (const Token *tok, int indirect, const Settings *settings, bool cpp, int depth=20)
 
bool isVariableChanged (const Variable *var, const Settings *settings, bool cpp, int depth=20)
 
const TokenfindVariableChanged (const Token *start, const Token *end, int indirect, const int varid, bool globalvar, const Settings *settings, bool cpp, int depth=20)
 
TokenfindVariableChanged (Token *start, const Token *end, int indirect, const int varid, bool globalvar, const Settings *settings, bool cpp, int depth=20)
 
bool isAliased (const Variable *var)
 
int numberOfArguments (const Token *start)
 
std::vector< const Token * > getArguments (const Token *ftok)
 
const TokenfindLambdaStartToken (const Token *last)
 
const TokenfindLambdaEndToken (const Token *first)
 
bool isLikelyStream (bool cpp, const Token *stream)
 
bool isLikelyStreamRead (bool cpp, const Token *op)
 
bool isCPPCast (const Token *tok)
 
bool isConstVarExpression (const Token *tok)
 
const VariablegetLHSVariable (const Token *tok)
 
bool reaches (const Token *start, const Token *dest, const Library &library, ErrorPath *errorPath)
 Returns true if there is a path between the two tokens. More...
 

Enumeration Type Documentation

◆ ChildrenToVisit

enum ChildrenToVisit
strong
Enumerator
none 
op1 
op2 
op1_and_op2 
done 

Definition at line 38 of file astutils.h.

Function Documentation

◆ astCanonicalType()

std::string astCanonicalType ( const Token expr)

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 150 of file astutils.cpp.

References Token::Match(), Variable::nameToken(), addons.namingng::type, Variable::typeStartToken(), and Token::variable().

Referenced by Library::getFunctionName().

◆ astFlatten()

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

Definition at line 68 of file astutils.cpp.

References astFlattenRecursive().

◆ astHasToken()

bool astHasToken ( const Token root,
const Token tok 
)

Definition at line 76 of file astutils.cpp.

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

Referenced by astHasToken(), and valueFlowForward().

◆ astHasVar()

bool astHasVar ( const Token tok,
int  varid 
)

Definition at line 85 of file astutils.cpp.

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

Referenced by astHasVar(), and isAliasOf().

◆ astIsBool()

◆ astIsContainer()

◆ astIsFloat()

bool astIsFloat ( const Token tok,
bool  unknown 
)

Is expression of floating point type?

Definition at line 122 of file astutils.cpp.

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

◆ astIsIntegral()

bool astIsIntegral ( const Token tok,
bool  unknown 
)

◆ astIsIterator()

bool astIsIterator ( const Token tok)

◆ astIsPointer()

◆ astIsSignedChar()

bool astIsSignedChar ( const Token tok)

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

Definition at line 104 of file astutils.cpp.

References astIsCharWithSign().

Referenced by CheckOther::checkCharVariable().

◆ astIsUnknownSignChar()

bool astIsUnknownSignChar ( const Token tok)

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

Definition at line 109 of file astutils.cpp.

References astIsCharWithSign().

Referenced by CheckOther::checkCharVariable().

◆ astIsVariableComparison()

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

◆ astParentSkipParens() [1/2]

const Token* astParentSkipParens ( const Token tok)

Definition at line 255 of file astutils.cpp.

References astParentSkipParens().

Referenced by astParentSkipParens(), CheckStl::outOfBounds(), and valueFlowLifetime().

◆ astParentSkipParens() [2/2]

Token* astParentSkipParens ( Token tok)

◆ findLambdaEndToken()

◆ findLambdaStartToken()

const Token* findLambdaStartToken ( const Token last)

Definition at line 1216 of file astutils.cpp.

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

Referenced by isExecutableScope().

◆ findVariableChanged() [1/2]

const Token* findVariableChanged ( const Token start,
const Token end,
int  indirect,
const int  varid,
bool  globalvar,
const Settings settings,
bool  cpp,
int  depth = 20 
)

Definition at line 1157 of file astutils.cpp.

References findVariableChanged().

◆ findVariableChanged() [2/2]

Token* findVariableChanged ( Token start,
const Token end,
int  indirect,
const int  varid,
bool  globalvar,
const Settings settings,
bool  cpp,
int  depth = 20 
)

◆ getArguments()

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

◆ getLHSVariable()

◆ isAliased()

bool isAliased ( const Variable var)

◆ isConstExpression()

◆ isConstVarExpression()

◆ isCPPCast()

◆ isDifferentKnownValues()

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

Definition at line 604 of file astutils.cpp.

References Token::hasKnownValue(), and Token::values().

Referenced by isOppositeCond().

◆ isEqualKnownValue()

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

Definition at line 599 of file astutils.cpp.

References Token::hasKnownValue(), and Token::values().

Referenced by CheckOther::checkDuplicateExpression().

◆ isLikelyStream()

bool isLikelyStream ( bool  cpp,
const Token stream 
)

◆ isLikelyStreamRead()

◆ isOppositeCond()

bool isOppositeCond ( bool  isNot,
bool  cpp,
const Token *const  cond1,
const Token *const  cond2,
const Library library,
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
cppc++ file
cond1condition1
cond2condition2
libraryfiles data
pureboolean

Definition at line 627 of file astutils.cpp.

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

Referenced by CheckCondition::checkIncorrectLogicOperator(), isOppositeCond(), isOppositeExpression(), CheckCondition::multiCondition(), CheckCondition::multiCondition2(), valueFlowOppositeCondition(), and valueFlowTerminatingCondition().

◆ isOppositeExpression()

bool isOppositeExpression ( bool  cpp,
const Token *const  tok1,
const Token *const  tok2,
const Library library,
bool  pure,
bool  followVar,
ErrorPath errors = nullptr 
)

◆ isReturnScope()

◆ isSameExpression()

bool isSameExpression ( bool  cpp,
bool  macro,
const Token tok1,
const Token tok2,
const Library library,
bool  pure,
bool  followVar,
ErrorPath errors = nullptr 
)

Definition at line 448 of file astutils.cpp.

References Token::astOperand1(), Token::astOperand2(), Token::astParent(), ValueType::constness, Token::eIncDecOp, addons.namingng::errors, followVariableExpression(), followVariableExpressionError(), Token::function(), Token::isAssignmentOp(), Token::isAttributeConst(), Token::isAttributePure(), Token::isBinaryOp(), Token::isComplex(), Token::isExpandedMacro(), Library::isFunctionConst(), Token::isLong(), Token::isName(), isSameExpression(), Token::isSigned(), Token::isTemplateArg(), Token::isUnsigned(), Token::link(), Token::Match(), Token::next(), Token::originalName(), ValueType::pointer, 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(), FwdAnalysis::checkRecursive(), CheckCondition::duplicateCondition(), findInsertValue(), FwdAnalysis::hasOperand(), CheckStl::isContainerSize(), isOppositeCond(), isOppositeExpression(), CheckCondition::isOverlappingCond(), isSameExpression(), CheckStl::mismatchingContainers(), CheckCondition::multiCondition2(), CheckString::overlappingStrcmp(), FwdAnalysis::possiblyAliased(), CheckString::sprintfOverlappingData(), CheckBufferOverrun::stringNotZeroTerminated(), valueFlowSameExpressions(), and valueFlowTerminatingCondition().

◆ isUniqueExpression()

◆ isVariableChanged() [1/3]

bool isVariableChanged ( const Token start,
const Token end,
const int  varid,
bool  globalvar,
const Settings settings,
bool  cpp,
int  depth = 20 
)

Is variable changed in block of code?

Definition at line 1133 of file astutils.cpp.

References findVariableChanged().

◆ isVariableChanged() [2/3]

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

◆ isVariableChanged() [3/3]

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

◆ isVariableChangedByFunctionCall() [1/2]

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 936 of file astutils.cpp.

References Token::astParent(), Library::ArgumentChecks::DIR_IN, Library::ArgumentChecks::DIR_INOUT, Library::ArgumentChecks::DIR_OUT, Token::function(), Scope::functionList, Library::getArgDirection(), Token::isCast(), Variable::isConst(), Library::isnullargbad(), Variable::isPointer(), Variable::isReference(), isScopeBracket(), Token::isUnaryOp(), Settings::library, Token::link(), Token::Match(), Token::next(), numberOfArguments(), ValueType::pointer, Token::previous(), Token::simpleMatch(), Token::str(), Variable::typeEndToken(), Token::valueType(), and Token::variable().

◆ isVariableChangedByFunctionCall() [2/2]

bool isVariableChangedByFunctionCall ( const Token tok,
int  indirect,
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 913 of file astutils.cpp.

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

Referenced by CheckOther::checkAccessOfMovedVariable(), isVariableChanged(), isVariableChangedByFunctionCall(), valueFlowForward(), and valueFlowReverse().

◆ isWithoutSideEffects()

bool isWithoutSideEffects ( bool  cpp,
const Token tok 
)

◆ nextAfterAstRightmostLeaf()

◆ numberOfArguments()

int numberOfArguments ( const Token start)

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

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

Definition at line 1176 of file astutils.cpp.

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

Referenced by CheckLeakAutoVar::changeAllocStatusIfRealloc(), CheckMemoryLeak::getAllocationType(), CheckMemoryLeak::getReallocationType(), TemplateSimplifier::instantiateMatch(), CheckMemoryLeak::isReopenStandardStream(), isVariableChangedByFunctionCall(), Library::matchArguments(), and CheckFunctions::memsetZeroBytes().

◆ precedes()

bool precedes ( const Token tok1,
const Token tok2 
)

If tok2 comes after tok1.

Definition at line 286 of file astutils.cpp.

References Token::index().

Referenced by CheckClass::constructors(), findVariableChanged(), followVariableExpression(), isAliased(), and isDeadScope().

◆ reaches()

bool reaches ( const Token start,
const Token dest,
const Library library,
ErrorPath errorPath 
)

Returns true if there is a path between the two tokens.

Parameters
startStarting point of the path
destThe path destination
errorPathAdds the path traversal to the errorPath

Definition at line 1518 of file astutils.cpp.

References reduce::i, and donate-cpu::info.

Referenced by CheckStl::invalidContainer().

◆ visitAstNodes()