Skip to content

fixed #11342 - cache Library::getFunctionName() calls in Token #7573

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2209,7 +2209,7 @@ static bool hasNoreturnFunction(const Token* tok, const Library& library, const
} else if (Token::Match(ftok, "exit|abort")) {
return true;
}
if (unknownFunc && !function && library.functions().count(library.getFunctionName(ftok)) == 0)
if (unknownFunc && !function && library.functions().count(ftok->funcname(library)) == 0)
*unknownFunc = ftok;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/checkassert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void CheckAssert::assertWithSideEffects()
f->containerYield == Library::Container::Yield::END_ITERATOR ||
f->containerYield == Library::Container::Yield::ITERATOR)
continue;
sideEffectInAssertError(tmp, mSettings->library.getFunctionName(tmp));
sideEffectInAssertError(tmp, tmp->funcname(mSettings->library));
}
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ void CheckFunctions::checkLibraryMatchFunctions()
if (!mSettings->library.isNotLibraryFunction(tok))
continue;

const std::string &functionName = mSettings->library.getFunctionName(tok);
const std::string &functionName = tok->funcname(mSettings->library);
if (functionName.empty())
continue;

Expand Down
6 changes: 3 additions & 3 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void CheckLeakAutoVar::configurationInfo(const Token* tok, const std::pair<const
{
if (mSettings->checkLibrary && functionUsage.second == VarInfo::USED &&
(!functionUsage.first || !functionUsage.first->function() || !functionUsage.first->function()->hasBody())) {
std::string funcStr = functionUsage.first ? mSettings->library.getFunctionName(functionUsage.first) : "f";
std::string funcStr = functionUsage.first ? functionUsage.first->funcname(mSettings->library) : "f";
if (funcStr.empty())
funcStr = "unknown::" + functionUsage.first->str();
reportError(tok,
Expand Down Expand Up @@ -748,7 +748,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
if (ftok->function() && !ftok->function()->isAttributeNoreturn() &&
!(ftok->function()->functionScope && mTokenizer->isScopeNoReturn(ftok->function()->functionScope->bodyEnd))) // check function scope
continue;
const std::string functionName(mSettings->library.getFunctionName(ftok));
const std::string& functionName(ftok->funcname(mSettings->library));
if (!mSettings->library.isLeakIgnore(functionName) && !mSettings->library.isUse(functionName)) {
const VarInfo::Usage usage = Token::simpleMatch(openingPar, "( )") ? VarInfo::NORET : VarInfo::USED; // TODO: check parameters passed to function
varInfo.possibleUsageAll({ ftok, usage });
Expand Down Expand Up @@ -989,7 +989,7 @@ void CheckLeakAutoVar::changeAllocStatus(VarInfo &varInfo, const VarInfo::AllocI
void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpeningPar, VarInfo &varInfo, const VarInfo::AllocInfo& allocation, const Library::AllocFunc* af)
{
// Ignore function call?
const bool isLeakIgnore = mSettings->library.isLeakIgnore(mSettings->library.getFunctionName(tokName));
const bool isLeakIgnore = mSettings->library.isLeakIgnore(tokName->funcname(mSettings->library));
if (mSettings->library.getReallocFuncInfo(tokName))
return;
if (tokName->next()->valueType() && tokName->next()->valueType()->container && tokName->next()->valueType()->container->stdStringLike)
Expand Down
46 changes: 23 additions & 23 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ bool Library::isnullargbad(const Token *ftok, int argnr) const
const ArgumentChecks *arg = getarg(ftok, argnr);
if (!arg) {
// scan format string argument should not be null
const std::string funcname = getFunctionName(ftok);
const std::string& funcname = ftok->funcname(*this);
const auto it = utils::as_const(mData->mFunctions).find(funcname);
if (it != mData->mFunctions.cend() && it->second.formatstr && it->second.formatstr_scan)
return true;
Expand All @@ -1196,7 +1196,7 @@ bool Library::isuninitargbad(const Token *ftok, int argnr, int indirect, bool *h
const ArgumentChecks *arg = getarg(ftok, argnr);
if (!arg) {
// non-scan format string argument should not be uninitialized
const std::string funcname = getFunctionName(ftok);
const std::string& funcname = ftok->funcname(*this);
const auto it = utils::as_const(mData->mFunctions).find(funcname);
if (it != mData->mFunctions.cend() && it->second.formatstr && !it->second.formatstr_scan)
return true;
Expand All @@ -1214,7 +1214,7 @@ const Library::AllocFunc* Library::getAllocFuncInfo(const Token *tok) const
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
if (!tok)
return nullptr;
const std::string funcname = getFunctionName(tok);
const std::string& funcname = tok->funcname(*this);
return isNotLibraryFunction(tok) && mData->mFunctions.find(funcname) != mData->mFunctions.end() ? nullptr : getAllocDealloc(mData->mAlloc, funcname);
}

Expand All @@ -1225,7 +1225,7 @@ const Library::AllocFunc* Library::getDeallocFuncInfo(const Token *tok) const
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
if (!tok)
return nullptr;
const std::string funcname = getFunctionName(tok);
const std::string& funcname = tok->funcname(*this);
return isNotLibraryFunction(tok) && mData->mFunctions.find(funcname) != mData->mFunctions.end() ? nullptr : getAllocDealloc(mData->mDealloc, funcname);
}

Expand All @@ -1236,7 +1236,7 @@ const Library::AllocFunc* Library::getReallocFuncInfo(const Token *tok) const
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
if (!tok)
return nullptr;
const std::string funcname = getFunctionName(tok);
const std::string& funcname = tok->funcname(*this);
return isNotLibraryFunction(tok) && mData->mFunctions.find(funcname) != mData->mFunctions.end() ? nullptr : getAllocDealloc(mData->mRealloc, funcname);
}

Expand Down Expand Up @@ -1443,7 +1443,7 @@ bool Library::isNotLibraryFunction(const Token *ftok, const Function **func) con
if (ftok->varId())
return true;

return !matchArguments(ftok, getFunctionName(ftok), func);
return !matchArguments(ftok, ftok->funcname(*this), func);
}

bool Library::matchArguments(const Token *ftok, const std::string &functionName, const Function **func) const
Expand Down Expand Up @@ -1483,7 +1483,7 @@ const Library::WarnInfo* Library::getWarnInfo(const Token* ftok) const
{
if (isNotLibraryFunction(ftok))
return nullptr;
const auto i = utils::as_const(mData->mFunctionwarn).find(getFunctionName(ftok));
const auto i = utils::as_const(mData->mFunctionwarn).find(ftok->funcname(*this));
if (i == mData->mFunctionwarn.cend())
return nullptr;
return &i->second;
Expand Down Expand Up @@ -1539,15 +1539,15 @@ bool Library::formatstr_function(const Token* ftok) const
if (isNotLibraryFunction(ftok))
return false;

const auto it = utils::as_const(mData->mFunctions).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mFunctions).find(ftok->funcname(*this));
if (it != mData->mFunctions.cend())
return it->second.formatstr;
return false;
}

int Library::formatstr_argno(const Token* ftok) const
{
const std::map<int, Library::ArgumentChecks>& argumentChecksFunc = mData->mFunctions.at(getFunctionName(ftok)).argumentChecks;
const std::map<int, Library::ArgumentChecks>& argumentChecksFunc = mData->mFunctions.at(ftok->funcname(*this)).argumentChecks;
auto it = std::find_if(argumentChecksFunc.cbegin(), argumentChecksFunc.cend(), [](const std::pair<const int, Library::ArgumentChecks>& a) {
return a.second.formatstr;
});
Expand All @@ -1556,19 +1556,19 @@ int Library::formatstr_argno(const Token* ftok) const

bool Library::formatstr_scan(const Token* ftok) const
{
return mData->mFunctions.at(getFunctionName(ftok)).formatstr_scan;
return mData->mFunctions.at(ftok->funcname(*this)).formatstr_scan;
}

bool Library::formatstr_secure(const Token* ftok) const
{
return mData->mFunctions.at(getFunctionName(ftok)).formatstr_secure;
return mData->mFunctions.at(ftok->funcname(*this)).formatstr_secure;
}

const Library::NonOverlappingData* Library::getNonOverlappingData(const Token *ftok) const
{
if (isNotLibraryFunction(ftok))
return nullptr;
const auto it = utils::as_const(mData->mNonOverlappingData).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mNonOverlappingData).find(ftok->funcname(*this));
return (it != mData->mNonOverlappingData.cend()) ? &it->second : nullptr;
}

Expand All @@ -1586,7 +1586,7 @@ Library::UseRetValType Library::getUseRetValType(const Token *ftok) const
}
return Library::UseRetValType::NONE;
}
const auto it = utils::as_const(mData->mFunctions).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mFunctions).find(ftok->funcname(*this));
if (it != mData->mFunctions.cend())
return it->second.useretval;
return Library::UseRetValType::NONE;
Expand All @@ -1596,7 +1596,7 @@ const std::string& Library::returnValue(const Token *ftok) const
{
if (isNotLibraryFunction(ftok))
return mEmptyString;
const auto it = utils::as_const(mData->mReturnValue).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mReturnValue).find(ftok->funcname(*this));
return it != mData->mReturnValue.cend() ? it->second : mEmptyString;
}

Expand All @@ -1614,31 +1614,31 @@ const std::string& Library::returnValueType(const Token *ftok) const
}
return mEmptyString;
}
const auto it = utils::as_const(mData->mReturnValueType).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mReturnValueType).find(ftok->funcname(*this));
return it != mData->mReturnValueType.cend() ? it->second : mEmptyString;
}

int Library::returnValueContainer(const Token *ftok) const
{
if (isNotLibraryFunction(ftok))
return -1;
const auto it = utils::as_const(mData->mReturnValueContainer).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mReturnValueContainer).find(ftok->funcname(*this));
return it != mData->mReturnValueContainer.cend() ? it->second : -1;
}

std::vector<MathLib::bigint> Library::unknownReturnValues(const Token *ftok) const
{
if (isNotLibraryFunction(ftok))
return std::vector<MathLib::bigint>();
const auto it = utils::as_const(mData->mUnknownReturnValues).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mUnknownReturnValues).find(ftok->funcname(*this));
return (it == mData->mUnknownReturnValues.cend()) ? std::vector<MathLib::bigint>() : it->second;
}

const Library::Function *Library::getFunction(const Token *ftok) const
{
if (isNotLibraryFunction(ftok))
return nullptr;
const auto it1 = utils::as_const(mData->mFunctions).find(getFunctionName(ftok));
const auto it1 = utils::as_const(mData->mFunctions).find(ftok->funcname(*this));
if (it1 == mData->mFunctions.cend())
return nullptr;
return &it1->second;
Expand All @@ -1649,7 +1649,7 @@ bool Library::hasminsize(const Token *ftok) const
{
if (isNotLibraryFunction(ftok))
return false;
const auto it = utils::as_const(mData->mFunctions).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mFunctions).find(ftok->funcname(*this));
if (it == mData->mFunctions.cend())
return false;
return std::any_of(it->second.argumentChecks.cbegin(), it->second.argumentChecks.cend(), [](const std::pair<const int, Library::ArgumentChecks>& a) {
Expand Down Expand Up @@ -1721,7 +1721,7 @@ bool Library::isFunctionConst(const Token *ftok) const
}
return false;
}
const auto it = utils::as_const(mData->mFunctions).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mFunctions).find(ftok->funcname(*this));
return (it != mData->mFunctions.cend() && it->second.isconst);
}

Expand All @@ -1740,7 +1740,7 @@ bool Library::isnoreturn(const Token *ftok) const
}
return false;
}
const auto it = utils::as_const(mData->mNoReturn).find(getFunctionName(ftok));
const auto it = utils::as_const(mData->mNoReturn).find(ftok->funcname(*this));
if (it == mData->mNoReturn.end())
return false;
if (it->second == LibraryData::FalseTrueMaybe::Maybe)
Expand All @@ -1753,8 +1753,8 @@ bool Library::isnotnoreturn(const Token *ftok) const
if (ftok->function() && ftok->function()->isAttributeNoreturn())
return false;
if (isNotLibraryFunction(ftok))
return hasAnyTypeCheck(getFunctionName(ftok));
const auto it = utils::as_const(mData->mNoReturn).find(getFunctionName(ftok));
return hasAnyTypeCheck(ftok->funcname(*this));
const auto it = utils::as_const(mData->mNoReturn).find(ftok->funcname(*this));
if (it == mData->mNoReturn.end())
return false;
if (it->second == LibraryData::FalseTrueMaybe::Maybe)
Expand Down
10 changes: 10 additions & 0 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,7 @@ const ValueFlow::Value* Token::getContainerSizeValue(const MathLib::bigint val)
TokenImpl::~TokenImpl()
{
delete mMacroName;
delete mFuncName;
delete mOriginalName;
delete mValueType;
delete mValues;
Expand Down Expand Up @@ -2721,3 +2722,12 @@ Token* findLambdaEndScope(Token* tok)
const Token* findLambdaEndScope(const Token* tok) {
return findLambdaEndScope(const_cast<Token*>(tok));
}

const std::string& Token::funcname(const Library& library) const {
if (!mImpl->mFuncName)
mImpl->mFuncName = new std::string(library.getFunctionName(this));
const std::string fname = library.getFunctionName(this);
std::cout << this << " astParent: " << astParent() << " previous: " << previous() << " " << stringify(stringifyOptions::forDebug()) << " " << fname << std::endl;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that this is debug code that will be removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still WIP - I forgot to mark it a draft.

assert(*mImpl->mFuncName == fname);
return *mImpl->mFuncName;
}
5 changes: 5 additions & 0 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ConstTokenRange;
class Token;
struct TokensFrontBack;
class TokenList;
class Library;

struct ScopeInfo2 {
ScopeInfo2(std::string name_, const Token *bodyEnd_, std::set<std::string> usingNamespaces_ = std::set<std::string>()) : name(std::move(name_)), bodyEnd(bodyEnd_), usingNamespaces(std::move(usingNamespaces_)) {}
Expand Down Expand Up @@ -104,6 +105,8 @@ struct TokenImpl {
// If this token came from a macro replacement list, this is the name of that macro
std::string* mMacroName{};

std::string* mFuncName{};

// ValueType
ValueType* mValueType{};

Expand Down Expand Up @@ -1589,6 +1592,8 @@ class CPPCHECKLIB Token {
{
return mIsC;
}

const std::string& funcname(const Library& library) const;
};

Token* findTypeEnd(Token* tok);
Expand Down