Skip to content

Commit

Permalink
refs #11342 - Library: avoid redundant lookups in getarg() (#6791)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Sep 10, 2024
1 parent e2efe8e commit cf7f74d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
33 changes: 19 additions & 14 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,16 +1249,14 @@ int Library::getReallocId(const Token *tok, int arg) const

const Library::ArgumentChecks * Library::getarg(const Token *ftok, int argnr) const
{
if (isNotLibraryFunction(ftok))
const Function* func = nullptr;
if (isNotLibraryFunction(ftok, &func))
return nullptr;
const std::unordered_map<std::string, Function>::const_iterator it1 = mData->mFunctions.find(getFunctionName(ftok));
if (it1 == mData->mFunctions.cend())
return nullptr;
const std::map<int,ArgumentChecks>::const_iterator it2 = it1->second.argumentChecks.find(argnr);
if (it2 != it1->second.argumentChecks.cend())
const std::map<int,ArgumentChecks>::const_iterator it2 = func->argumentChecks.find(argnr);
if (it2 != func->argumentChecks.cend())
return &it2->second;
const std::map<int,ArgumentChecks>::const_iterator it3 = it1->second.argumentChecks.find(-1);
if (it3 != it1->second.argumentChecks.cend())
const std::map<int,ArgumentChecks>::const_iterator it3 = func->argumentChecks.find(-1);
if (it3 != func->argumentChecks.cend())
return &it3->second;
return nullptr;
}
Expand Down Expand Up @@ -1414,7 +1412,7 @@ Library::Container::Yield Library::getContainerYield(const Token* const cond)
}

// returns true if ftok is not a library function
bool Library::isNotLibraryFunction(const Token *ftok) const
bool Library::isNotLibraryFunction(const Token *ftok, const Function **func) const
{
if (ftok->isKeyword() || ftok->isStandardType())
return true;
Expand All @@ -1426,10 +1424,10 @@ bool Library::isNotLibraryFunction(const Token *ftok) const
if (ftok->varId())
return true;

return !matchArguments(ftok, getFunctionName(ftok));
return !matchArguments(ftok, getFunctionName(ftok), func);
}

bool Library::matchArguments(const Token *ftok, const std::string &functionName) const
bool Library::matchArguments(const Token *ftok, const std::string &functionName, const Function **func) const
{
if (functionName.empty())
return false;
Expand All @@ -1444,10 +1442,17 @@ bool Library::matchArguments(const Token *ftok, const std::string &functionName)
if (argCheck.second.optional && (firstOptionalArg == -1 || firstOptionalArg > argCheck.first))
firstOptionalArg = argCheck.first;

if (argCheck.second.formatstr || argCheck.second.variadic)
return args <= callargs;
if (argCheck.second.formatstr || argCheck.second.variadic) {
const bool b = args <= callargs;
if (b && func)
*func = &it->second;
return b;
}
}
return (firstOptionalArg < 0) ? args == callargs : (callargs >= firstOptionalArg-1 && callargs <= args);
const bool b = (firstOptionalArg < 0) ? args == callargs : (callargs >= firstOptionalArg-1 && callargs <= args);
if (b && func)
*func = &it->second;
return b;
}

const std::map<std::string, Library::WarnInfo>& Library::functionwarn() const
Expand Down
6 changes: 4 additions & 2 deletions lib/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ class CPPCHECKLIB Library {

const WarnInfo* getWarnInfo(const Token* ftok) const;

struct Function;

// returns true if ftok is not a library function
bool isNotLibraryFunction(const Token *ftok) const;
bool matchArguments(const Token *ftok, const std::string &functionName) const;
bool isNotLibraryFunction(const Token *ftok, const Function **func = nullptr) const;
bool matchArguments(const Token *ftok, const std::string &functionName, const Function **func = nullptr) const;

enum class UseRetValType : std::uint8_t { NONE, DEFAULT, ERROR_CODE };
UseRetValType getUseRetValType(const Token* ftok) const;
Expand Down
8 changes: 6 additions & 2 deletions test/testlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ class TestLibrary : public TestFixture {
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
tokenList.createAst();

ASSERT(!library.isNotLibraryFunction(tokenList.front()));
const Library::Function* func = nullptr;
ASSERT(!library.isNotLibraryFunction(tokenList.front(), &func));
ASSERT(func);
}
{
TokenList tokenList(&settings);
Expand All @@ -200,7 +202,9 @@ class TestLibrary : public TestFixture {
Token::createMutualLinks(tokenList.front()->next(), tokenList.back()->previous());
tokenList.createAst();

ASSERT(!library.isNotLibraryFunction(tokenList.front()));
const Library::Function* func = nullptr;
ASSERT(!library.isNotLibraryFunction(tokenList.front(), &func));
ASSERT(func);
}
{
TokenList tokenList(&settings);
Expand Down

0 comments on commit cf7f74d

Please sign in to comment.