diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ebccfc90687..01f94588e6d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -10328,6 +10328,13 @@ static bool isStdSmartPointer(const Token* tok, const Settings& settings) return ptr && startsWith(ptr->name, "std::"); } +static bool isLibraryType(const Token* tok, const Settings& settings) +{ + return settings.library.hasAnyTypeCheck("std::" + tok->str()) || + settings.library.podtype("std::" + tok->str()) || + isStdContainerOrIterator(tok, settings); +} + // Add std:: in front of std classes, when using namespace std; was given void Tokenizer::simplifyNamespaceStd() { @@ -10355,14 +10362,13 @@ void Tokenizer::simplifyNamespaceStd() if (start != tok && start->isName() && !start->isKeyword() && (!start->previous() || Token::Match(start->previous(), "[;{}]"))) userFunctions.insert(tok->str()); } - if (userFunctions.find(tok->str()) == userFunctions.end() && mSettings.library.matchArguments(tok, "std::" + tok->str())) + if ((userFunctions.find(tok->str()) == userFunctions.end() && mSettings.library.matchArguments(tok, "std::" + tok->str())) || + (tok->tokAt(-1)->isKeyword() && isLibraryType(tok, mSettings))) insert = true; } else if (Token::simpleMatch(tok->next(), "<") && (isStdContainerOrIterator(tok, mSettings) || isStdSmartPointer(tok, mSettings))) insert = true; - else if (mSettings.library.hasAnyTypeCheck("std::" + tok->str()) || - mSettings.library.podtype("std::" + tok->str()) || - isStdContainerOrIterator(tok, mSettings)) + else if (isLibraryType(tok, mSettings)) insert = true; else if (Token::simpleMatch(tok, "aligned_storage")) insert = true; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 0798983c079..fd0a0d6aeb6 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5288,12 +5288,7 @@ class TestTokenizer : public TestFixture { "if ( ! p ) {\n" "throw std :: runtime_error ( \"abc\" ) ; }\n" "}"; - TODO_ASSERT_EQUALS(expected, - "void f ( const std :: unique_ptr < int > & p ) {\n" - "if ( ! p ) {\n" - "throw runtime_error ( \"abc\" ) ; }\n" - "}", - tokenizeAndStringify(code)); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } { @@ -5315,6 +5310,17 @@ class TestTokenizer : public TestFixture { "}"; ASSERT_EQUALS(expected, tokenizeAndStringify(code)); } + + { + const char code[] = "using namespace std;\n" + "void move() {}\n" + "void string() {}\n" + "string_view f() { return string(); }\n"; + expected = "void move ( ) { }\n" + "void string ( ) { }\n" + "std :: string_view f ( ) { return std :: string ( ) ; }"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + } } void microsoftMemory() {