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

Fix #12335 FP checkLibraryFunction for incorrect scope #5868

Merged
merged 5 commits into from
Jan 12, 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
8 changes: 6 additions & 2 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,12 @@ std::string Library::getFunctionName(const Token *ftok) const
if (ftok->astParent()) {
bool error = false;
const Token * tok = ftok->astParent()->isUnaryOp("&") ? ftok->astParent()->astOperand1() : ftok->next()->astOperand1();
const std::string ret = getFunctionName(tok, error);
return error ? std::string() : ret;
std::string ret = getFunctionName(tok, error);
if (error)
return {};
if (startsWith(ret, "::"))
ret.erase(0, 2);
return ret;
}

// Lookup function name without using AST..
Expand Down
16 changes: 14 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9647,6 +9647,18 @@ void Tokenizer::simplifyBitfields()
}
}

static bool isStdContainerOrIterator(const Token* tok, const Settings* settings)
{
const Library::Container* ctr = settings->library.detectContainerOrIterator(tok, nullptr, /*withoutStd*/ true);
return ctr && startsWith(ctr->startPattern, "std ::");
}

static bool isStdSmartPointer(const Token* tok, const Settings* settings)
{
const Library::SmartPointer* ptr = settings->library.detectSmartPointer(tok, /*withoutStd*/ true);
return ptr && startsWith(ptr->name, "std::");
}

// Add std:: in front of std classes, when using namespace std; was given
void Tokenizer::simplifyNamespaceStd()
{
Expand Down Expand Up @@ -9677,11 +9689,11 @@ void Tokenizer::simplifyNamespaceStd()
if (userFunctions.find(tok->str()) == userFunctions.end() && mSettings->library.matchArguments(tok, "std::" + tok->str()))
insert = true;
} else if (Token::simpleMatch(tok->next(), "<") &&
(mSettings->library.detectContainerOrIterator(tok, nullptr, /*withoutStd*/ true) || mSettings->library.detectSmartPointer(tok, /*withoutStd*/ true)))
(isStdContainerOrIterator(tok, mSettings) || isStdSmartPointer(tok, mSettings)))
insert = true;
else if (mSettings->library.hasAnyTypeCheck("std::" + tok->str()) ||
mSettings->library.podtype("std::" + tok->str()) ||
mSettings->library.detectContainerOrIterator(tok, nullptr, /*withoutStd*/ true))
isStdContainerOrIterator(tok, mSettings))
insert = true;

if (insert) {
Expand Down
10 changes: 10 additions & 0 deletions test/cfg/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ bool QString7(QString s, const QString& l) {
return l.startsWith(s);
}

namespace NTestStd // #12355
{
using namespace std;
QString QString_std(QString s)
{
s.replace("abc", "def");
return s;
}
}

void QByteArray1(QByteArray byteArrayArg)
{
for (int i = 0; i <= byteArrayArg.size(); ++i) {
Expand Down
6 changes: 6 additions & 0 deletions test/cfg/std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4860,3 +4860,9 @@ void std_vector_data_arithmetic()
buf.resize(1);
memcpy(buf.data() + 0, "", 1);
}

std::string global_scope_std() // #12355
{
::std::stringstream ss;
return ss.str();
}
Loading