diff --git a/lib/library.cpp b/lib/library.cpp index f1058c7883a..d815b3ac615 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -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.. diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 03b31da4bcc..70aa0ee17e8 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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() { @@ -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) { diff --git a/test/cfg/qt.cpp b/test/cfg/qt.cpp index 2bef064a983..2c45ba8340c 100644 --- a/test/cfg/qt.cpp +++ b/test/cfg/qt.cpp @@ -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) { diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index 1d2c17e8de6..07f427e6fc1 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -4892,3 +4892,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(); +} \ No newline at end of file