Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduced Tokenizer::isCPP() usage / SymbolDatabase: removed mIsCpp and isCPP() #5725

Merged
merged 5 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 109 additions & 111 deletions lib/astutils.cpp

Large diffs are not rendered by default.

45 changes: 20 additions & 25 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
bool isVariableDecl(const Token* tok);
bool isStlStringType(const Token* tok);

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

const Token* previousBeforeAstLeftmostLeaf(const Token* tok);
Token* previousBeforeAstLeftmostLeaf(Token* tok);
Expand All @@ -208,7 +208,7 @@ const Token* astParentSkipParens(const Token* tok);
const Token* getParentMember(const Token * tok);

const Token* getParentLifetime(const Token* tok);
const Token* getParentLifetime(bool cpp, const Token* tok, const Library* library);
const Token* getParentLifetime(const Token* tok, const Library* library);

std::vector<ValueType> getParentValueTypes(const Token* tok,
const Settings* settings = nullptr,
Expand Down Expand Up @@ -261,7 +261,7 @@ SmallVector<ReferenceToken> followAllReferences(const Token* tok,
int depth = 20);
const Token* followReferences(const Token* tok, ErrorPath* errors = nullptr);

CPPCHECKLIB bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);
CPPCHECKLIB bool isSameExpression(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);

Expand All @@ -282,21 +282,20 @@ bool compareTokenFlags(const Token* tok1, const Token* tok2, bool macro);
/**
* Are two conditions opposite
* @param isNot do you want to know if cond1 is !cond2 or if cond1 and cond2 are non-overlapping. true: cond1==!cond2 false: cond1==true => cond2==false
* @param cpp c++ file
* @param cond1 condition1
* @param cond2 condition2
* @param library files data
* @param pure boolean
*/
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 isOppositeCond(bool isNot, 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 isOppositeExpression(const Token * const tok1, const Token * const tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors=nullptr);

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

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

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

bool isUniqueExpression(const Token* tok);

Expand Down Expand Up @@ -341,38 +340,35 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, nonneg int
CPPCHECKLIB bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Settings *settings, bool *inconclusive);

/** Is variable changed in block of code? */
CPPCHECKLIB bool isVariableChanged(const Token *start, const Token *end, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
bool isVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
CPPCHECKLIB bool isVariableChanged(const Token *start, const Token *end, const nonneg int exprid, bool globalvar, const Settings *settings, int depth = 20);
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, bool cpp, int depth = 20);
bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, int depth = 20);

bool isVariableChanged(const Variable * var, const Settings *settings, bool cpp, 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 cpp);
const Settings* settings);

bool isThisChanged(const Token* tok, int indirect, const Settings* settings, bool cpp);
const Token* findThisChanged(const Token* start, const Token* end, int indirect, const Settings* settings, bool cpp);
bool isThisChanged(const Token* tok, int indirect, const Settings* settings);
const Token* findThisChanged(const Token* start, const Token* end, 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, bool cpp, int depth = 20);
Token* findVariableChanged(Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth = 20);
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,
bool cpp,
int depth = 20);

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

Expand All @@ -381,7 +377,6 @@ bool isExpressionChangedAt(const Token* expr,
int indirect,
bool globalvar,
const Settings* settings,
bool cpp,
int depth = 20);

/// If token is an alias if another variable
Expand Down Expand Up @@ -426,14 +421,14 @@ CPPCHECKLIB const Token *findLambdaStartToken(const Token *last);
CPPCHECKLIB const Token *findLambdaEndToken(const Token *first);
CPPCHECKLIB Token* findLambdaEndToken(Token* first);

bool isLikelyStream(bool cpp, const Token *stream);
bool isLikelyStream(const Token *stream);

/**
* do we see a likely write of rhs through overloaded operator
* s >> x;
* a & x;
*/
bool isLikelyStreamRead(bool cpp, const Token *op);
bool isLikelyStreamRead(const Token *op);

bool isCPPCast(const Token* tok);

Expand All @@ -443,7 +438,7 @@ bool isLeafDot(const Token* tok);

enum class ExprUsage { None, NotUsed, PassedByReference, Used, Inconclusive };

ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings, bool cpp);
ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings);

const Variable *getLHSVariable(const Token *tok);

Expand All @@ -458,7 +453,7 @@ bool isScopeBracket(const Token* tok);

CPPCHECKLIB bool isNullOperand(const Token *expr);

bool isGlobalData(const Token *expr, bool cpp);
bool isGlobalData(const Token *expr);

bool isUnevaluated(const Token *tok);

Expand Down
23 changes: 11 additions & 12 deletions lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void CheckAutoVariables::autoVariables()
}
// Invalid pointer deallocation
else if ((Token::Match(tok, "%name% ( %var%|%str% ) ;") && mSettings->library.getDeallocFuncInfo(tok)) ||
(mTokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| %var%|%str% !!["))) {
(tok->isCpp() && Token::Match(tok, "delete [| ]| (| %var%|%str% !!["))) {
tok = Token::findmatch(tok->next(), "%var%|%str%");
if (Token::simpleMatch(tok->astParent(), "."))
continue;
Expand All @@ -314,7 +314,7 @@ void CheckAutoVariables::autoVariables()
}
}
} else if ((Token::Match(tok, "%name% ( & %var% ) ;") && mSettings->library.getDeallocFuncInfo(tok)) ||
(mTokenizer->isCPP() && Token::Match(tok, "delete [| ]| (| & %var% !!["))) {
(tok->isCpp() && Token::Match(tok, "delete [| ]| (| & %var% !!["))) {
tok = Token::findmatch(tok->next(), "%var%");
if (isAutoVar(tok))
errorInvalidDeallocation(tok, nullptr);
Expand Down Expand Up @@ -463,9 +463,9 @@ static int getPointerDepth(const Token *tok)
return n;
}

static bool isDeadTemporary(bool cpp, const Token* tok, const Token* expr, const Library* library)
static bool isDeadTemporary(const Token* tok, const Token* expr, const Library* library)
{
if (!isTemporary(cpp, tok, library))
if (!isTemporary(tok, library))
return false;
if (expr) {
if (!precedes(nextAfterAstRightmostLeaf(tok->astTop()), nextAfterAstRightmostLeaf(expr->astTop())))
Expand Down Expand Up @@ -495,7 +495,7 @@ static bool isEscapedReference(const Variable* var)
if (!Token::simpleMatch(varDeclEndToken, "="))
return false;
const Token* vartok = varDeclEndToken->astOperand2();
return !isTemporary(true, vartok, nullptr, false);
return !isTemporary(vartok, nullptr, false);
}

static bool isDanglingSubFunction(const Token* tokvalue, const Token* tok)
Expand Down Expand Up @@ -562,7 +562,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
errorReturnReference(tok, lt.errorPath, lt.inconclusive);
break;
}
if (isDeadTemporary(mTokenizer->isCPP(), lt.token, nullptr, &mSettings->library)) {
if (isDeadTemporary(lt.token, nullptr, &mSettings->library)) {
errorReturnTempReference(tok, lt.errorPath, lt.inconclusive);
break;
}
Expand All @@ -584,7 +584,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
if (!printInconclusive && lt.inconclusive)
continue;
const Token * tokvalue = lt.token;
if (isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) {
if (isDeadTemporary(tokvalue, tok, &mSettings->library)) {
errorDanglingTempReference(tok, lt.errorPath, lt.inconclusive);
break;
}
Expand All @@ -598,7 +598,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
continue;
if (!printInconclusive && val.isInconclusive())
continue;
const Token* parent = getParentLifetime(mTokenizer->isCPP(), val.tokvalue, &mSettings->library);
const Token* parent = getParentLifetime(val.tokvalue, &mSettings->library);
if (!exprs.insert(parent).second)
continue;
for (const ValueFlow::LifetimeToken& lt : ValueFlow::getLifetimeTokens(parent, escape || isAssignedToNonLocal(tok))) {
Expand All @@ -614,15 +614,15 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
continue;
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
isInScope(tokvalue->variable()->nameToken(), scope)) ||
isDeadTemporary(mTokenizer->isCPP(), tokvalue, nullptr, &mSettings->library)) {
isDeadTemporary(tokvalue, nullptr, &mSettings->library)) {
errorReturnDanglingLifetime(tok, &val);
break;
}
} else if (tokvalue->variable() && isDeadScope(tokvalue->variable()->nameToken(), tok->scope())) {
errorInvalidLifetime(tok, &val);
break;
} else if (!tokvalue->variable() &&
isDeadTemporary(mTokenizer->isCPP(), tokvalue, tok, &mSettings->library)) {
isDeadTemporary(tokvalue, tok, &mSettings->library)) {
if (!diag(tokvalue))
errorDanglingTemporaryLifetime(tok, &val, tokvalue);
break;
Expand Down Expand Up @@ -650,8 +650,7 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
tok->scope()->bodyEnd,
var->declarationId(),
var->isGlobal(),
mSettings,
mTokenizer->isCPP())) {
mSettings)) {
errorDanglngLifetime(tok2, &val);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/checkbool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void CheckBool::checkBitwiseOnBoolean()
if (tok->str() == "|" && !isConvertedToBool(tok) && !(isBoolOp1 && isBoolOp2))
continue;
// first operand will always be evaluated
if (!isConstExpression(tok->astOperand2(), mSettings->library, mTokenizer->isCPP()))
if (!isConstExpression(tok->astOperand2(), mSettings->library))
continue;
if (tok->astOperand2()->variable() && tok->astOperand2()->variable()->nameToken() == tok->astOperand2())
continue;
Expand Down
6 changes: 3 additions & 3 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ void CheckBufferOverrun::arrayIndex()
const Token* changeTok = var->scope()->bodyStart;
bool isChanged = false;
while ((changeTok = findVariableChanged(changeTok->next(), var->scope()->bodyEnd, /*indirect*/ 0, var->declarationId(),
/*globalvar*/ false, mSettings, mTokenizer->isCPP()))) {
/*globalvar*/ false, mSettings))) {
if (!Token::simpleMatch(changeTok->astParent(), "[")) {
isChanged = true;
break;
Expand Down Expand Up @@ -793,7 +793,7 @@ void CheckBufferOverrun::stringNotZeroTerminated()
const Token *rhs = tok2->next()->astOperand2();
if (!rhs || !rhs->hasKnownIntValue() || rhs->getKnownIntValue() != 0)
continue;
if (isSameExpression(mTokenizer->isCPP(), false, args[0], tok2->link()->astOperand1(), mSettings->library, false, false))
if (isSameExpression(false, args[0], tok2->link()->astOperand1(), mSettings->library, false, false))
isZeroTerminated = true;
}
if (isZeroTerminated)
Expand Down Expand Up @@ -1090,7 +1090,7 @@ void CheckBufferOverrun::objectIndex()
if (var->valueType()->pointer > obj->valueType()->pointer)
continue;
}
if (obj->valueType() && var->valueType() && (obj->isCast() || (mTokenizer->isCPP() && isCPPCast(obj)) || obj->valueType()->pointer)) { // allow cast to a different type
if (obj->valueType() && var->valueType() && (obj->isCast() || (obj->isCpp() && isCPPCast(obj)) || obj->valueType()->pointer)) { // allow cast to a different type
const auto varSize = var->valueType()->typeSize(mSettings->platform);
if (varSize == 0)
continue;
Expand Down
14 changes: 7 additions & 7 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ void CheckClass::copyconstructors()
}
}
for (tok = func.functionScope->bodyStart; tok != func.functionScope->bodyEnd; tok = tok->next()) {
if ((mTokenizer->isCPP() && Token::Match(tok, "%var% = new")) ||
if ((tok->isCpp() && Token::Match(tok, "%var% = new")) ||
(Token::Match(tok, "%var% = %name% (") && (mSettings->library.getAllocFuncInfo(tok->tokAt(2)) || mSettings->library.getReallocFuncInfo(tok->tokAt(2))))) {
allocatedVars.erase(tok->varId());
} else if (Token::Match(tok, "%var% = %name% . %name% ;") && allocatedVars.find(tok->varId()) != allocatedVars.end()) {
Expand Down Expand Up @@ -817,7 +817,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
continue;

// Variable getting value from stream?
if (Token::Match(ftok, ">>|& %name%") && isLikelyStreamRead(true, ftok)) {
if (Token::Match(ftok, ">>|& %name%") && isLikelyStreamRead(ftok)) {
assignVar(usage, ftok->next()->varId());
}

Expand Down Expand Up @@ -1771,7 +1771,7 @@ bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const T
if (!end)
end = func->functionScope->bodyEnd;
for (const Token *tok = start; tok && (tok != end); tok = tok->next()) {
if (((mTokenizer->isCPP() && Token::Match(tok, "%var% = new")) ||
if (((tok->isCpp() && Token::Match(tok, "%var% = new")) ||
(Token::Match(tok, "%var% = %name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2)))) &&
isMemberVar(scope, tok))
return true;
Expand All @@ -1780,9 +1780,9 @@ bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const T
const Token *var;
if (Token::Match(tok, "%name% ( %var%") && mSettings->library.getDeallocFuncInfo(tok))
var = tok->tokAt(2);
else if (mTokenizer->isCPP() && Token::Match(tok, "delete [ ] %var%"))
else if (tok->isCpp() && Token::Match(tok, "delete [ ] %var%"))
var = tok->tokAt(3);
else if (mTokenizer->isCPP() && Token::Match(tok, "delete %var%"))
else if (tok->isCpp() && Token::Match(tok, "delete %var%"))
var = tok->next();
else
continue;
Expand Down Expand Up @@ -2552,7 +2552,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
// Streaming
else if (end->strAt(1) == "<<" && tok1->strAt(-1) != "<<")
return false;
else if (isLikelyStreamRead(true, tok1->previous()))
else if (isLikelyStreamRead(tok1->previous()))
return false;

// ++/--
Expand Down Expand Up @@ -2581,7 +2581,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
}

// streaming: >> *this
else if (Token::simpleMatch(tok1, ">> * this") && isLikelyStreamRead(true, tok1)) {
else if (Token::simpleMatch(tok1, ">> * this") && isLikelyStreamRead(tok1)) {
return false;
}

Expand Down
Loading
Loading