Skip to content

Commit 6fa5b6b

Browse files
Refs #4412: using namespace std; not simplified (constructor) (#8477)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent a374143 commit 6fa5b6b

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

lib/tokenize.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10344,6 +10344,13 @@ static bool isStdSmartPointer(const Token* tok, const Settings& settings)
1034410344
return ptr && startsWith(ptr->name, "std::");
1034510345
}
1034610346

10347+
static bool isLibraryType(const Token* tok, const Settings& settings)
10348+
{
10349+
return settings.library.hasAnyTypeCheck("std::" + tok->str()) ||
10350+
settings.library.podtype("std::" + tok->str()) ||
10351+
isStdContainerOrIterator(tok, settings);
10352+
}
10353+
1034710354
// Add std:: in front of std classes, when using namespace std; was given
1034810355
void Tokenizer::simplifyNamespaceStd()
1034910356
{
@@ -10371,14 +10378,13 @@ void Tokenizer::simplifyNamespaceStd()
1037110378
if (start != tok && start->isName() && !start->isKeyword() && (!start->previous() || Token::Match(start->previous(), "[;{}]")))
1037210379
userFunctions.insert(tok->str());
1037310380
}
10374-
if (userFunctions.find(tok->str()) == userFunctions.end() && mSettings.library.matchArguments(tok, "std::" + tok->str()))
10381+
if ((userFunctions.find(tok->str()) == userFunctions.end() && mSettings.library.matchArguments(tok, "std::" + tok->str())) ||
10382+
(tok->tokAt(-1)->isKeyword() && isLibraryType(tok, mSettings)))
1037510383
insert = true;
1037610384
} else if (Token::simpleMatch(tok->next(), "<") &&
1037710385
(isStdContainerOrIterator(tok, mSettings) || isStdSmartPointer(tok, mSettings)))
1037810386
insert = true;
10379-
else if (mSettings.library.hasAnyTypeCheck("std::" + tok->str()) ||
10380-
mSettings.library.podtype("std::" + tok->str()) ||
10381-
isStdContainerOrIterator(tok, mSettings))
10387+
else if (isLibraryType(tok, mSettings))
1038210388
insert = true;
1038310389
else if (Token::simpleMatch(tok, "aligned_storage"))
1038410390
insert = true;

test/testtokenize.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5288,12 +5288,7 @@ class TestTokenizer : public TestFixture {
52885288
"if ( ! p ) {\n"
52895289
"throw std :: runtime_error ( \"abc\" ) ; }\n"
52905290
"}";
5291-
TODO_ASSERT_EQUALS(expected,
5292-
"void f ( const std :: unique_ptr < int > & p ) {\n"
5293-
"if ( ! p ) {\n"
5294-
"throw runtime_error ( \"abc\" ) ; }\n"
5295-
"}",
5296-
tokenizeAndStringify(code));
5291+
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
52975292
}
52985293

52995294
{
@@ -5315,6 +5310,17 @@ class TestTokenizer : public TestFixture {
53155310
"}";
53165311
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
53175312
}
5313+
5314+
{
5315+
const char code[] = "using namespace std;\n"
5316+
"string_view f() { return string(); }\n"
5317+
"void move() {}\n"
5318+
"void string() {}\n";
5319+
expected = "std :: string_view f ( ) { return std :: string ( ) ; }\n"
5320+
"void move ( ) { }\n"
5321+
"void string ( ) { }";
5322+
ASSERT_EQUALS(expected, tokenizeAndStringify(code));
5323+
}
53185324
}
53195325

53205326
void microsoftMemory() {

0 commit comments

Comments
 (0)