Skip to content

Commit

Permalink
replaced global emptyString used for const reference results with l…
Browse files Browse the repository at this point in the history
…ocal instances (#7250)
  • Loading branch information
firewave authored Jan 28, 2025
1 parent e7fb594 commit 1ffb72f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
14 changes: 8 additions & 6 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ struct Library::LibraryData
std::unordered_set<std::string> mEntrypoints;
};

const std::string Library::mEmptyString;

Library::Library()
: mData(new LibraryData())
{}
Expand Down Expand Up @@ -1588,9 +1590,9 @@ Library::UseRetValType Library::getUseRetValType(const Token *ftok) const
const std::string& Library::returnValue(const Token *ftok) const
{
if (isNotLibraryFunction(ftok))
return emptyString;
return mEmptyString;
const auto it = utils::as_const(mData->mReturnValue).find(getFunctionName(ftok));
return it != mData->mReturnValue.cend() ? it->second : emptyString;
return it != mData->mReturnValue.cend() ? it->second : mEmptyString;
}

const std::string& Library::returnValueType(const Token *ftok) const
Expand All @@ -1601,10 +1603,10 @@ const std::string& Library::returnValueType(const Token *ftok) const
if (contTok->valueType() && contTok->valueType()->container)
return contTok->valueType()->container->getReturnType(ftok->str());
}
return emptyString;
return mEmptyString;
}
const auto it = utils::as_const(mData->mReturnValueType).find(getFunctionName(ftok));
return it != mData->mReturnValueType.cend() ? it->second : emptyString;
return it != mData->mReturnValueType.cend() ? it->second : mEmptyString;
}

int Library::returnValueContainer(const Token *ftok) const
Expand Down Expand Up @@ -1794,7 +1796,7 @@ const std::string& Library::blockstart(const std::string &file) const
if (map_it != mData->mExecutableBlocks.end()) {
return map_it->second.start();
}
return emptyString;
return mEmptyString;
}

const std::string& Library::blockend(const std::string &file) const
Expand All @@ -1805,7 +1807,7 @@ const std::string& Library::blockend(const std::string &file) const
if (map_it != mData->mExecutableBlocks.end()) {
return map_it->second.end();
}
return emptyString;
return mEmptyString;
}

bool Library::iskeyword(const std::string &file, const std::string &keyword) const
Expand Down
6 changes: 4 additions & 2 deletions lib/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ namespace tinyxml2 {
class CPPCHECKLIB Library {
friend struct LibraryHelper; // for testing

static const std::string mEmptyString;

public:
Library();
~Library();
Expand Down Expand Up @@ -252,7 +254,7 @@ class CPPCHECKLIB Library {

const std::string& getReturnType(const std::string& function) const {
const auto i = utils::as_const(functions).find(function);
return (i != functions.end()) ? i->second.returnType : emptyString;
return (i != functions.end()) ? i->second.returnType : mEmptyString;
}

static Yield yieldFrom(const std::string& yieldName);
Expand Down Expand Up @@ -348,7 +350,7 @@ class CPPCHECKLIB Library {

const std::string& validarg(const Token *ftok, int argnr) const {
const ArgumentChecks *arg = getarg(ftok, argnr);
return arg ? arg->valid : emptyString;
return arg ? arg->valid : mEmptyString;
}

const ArgumentChecks::IteratorInfo *getArgIteratorInfo(const Token *ftok, int argnr) const {
Expand Down
6 changes: 4 additions & 2 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class CPPCHECKLIB Type {
std::string name() const;

const std::string& type() const {
return classDef ? classDef->str() : emptyString;
static const std::string s_empty_string;
return classDef ? classDef->str() : s_empty_string;
}

bool isClassType() const;
Expand Down Expand Up @@ -294,7 +295,8 @@ class CPPCHECKLIB Variable {
if (mNameToken)
return mNameToken->str();

return emptyString;
static const std::string s_empty_string;
return s_empty_string;
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace {
}

const std::list<ValueFlow::Value> TokenImpl::mEmptyValueList;
const std::string Token::mEmptyString;

Token::Token(TokensFrontBack &tokensFrontBack)
: mTokensFrontBack(tokensFrontBack)
Expand Down
6 changes: 4 additions & 2 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ class CPPCHECKLIB Token {
private:
TokensFrontBack& mTokensFrontBack;

static const std::string mEmptyString;

public:
Token(const Token &) = delete;
Token& operator=(const Token &) = delete;
Expand Down Expand Up @@ -256,7 +258,7 @@ class CPPCHECKLIB Token {
const std::string &strAt(int index) const
{
const Token *tok = this->tokAt(index);
return tok ? tok->mStr : emptyString;
return tok ? tok->mStr : mEmptyString;
}

/**
Expand Down Expand Up @@ -1272,7 +1274,7 @@ class CPPCHECKLIB Token {
* @return the original name.
*/
const std::string & originalName() const {
return mImpl->mOriginalName ? *mImpl->mOriginalName : emptyString;
return mImpl->mOriginalName ? *mImpl->mOriginalName : mEmptyString;
}

const std::list<ValueFlow::Value>& values() const {
Expand Down
3 changes: 2 additions & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ TokenList::~TokenList()
const std::string& TokenList::getSourceFilePath() const
{
if (getFiles().empty()) {
return emptyString;
static const std::string s_empty_string;
return s_empty_string;
}
return getFiles()[0];
}
Expand Down
3 changes: 2 additions & 1 deletion lib/vf_analyzers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ struct ValueFlowAnalyzer : Analyzer {
{"^=", "^="}};
auto it = lookup.find(assign);
if (it == lookup.end()) {
return emptyString;
static const std::string s_empty_string;
return s_empty_string;
}
return it->second;
}
Expand Down

0 comments on commit 1ffb72f

Please sign in to comment.