Skip to content

Commit f9f3fe0

Browse files
Fix syntaxError with using namespace ::std (f'up to #8454) (#6100)
Why do `%type%` and `%name%` match keywords?
1 parent 866619c commit f9f3fe0

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/tokenize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,6 +2844,9 @@ bool Tokenizer::simplifyUsing()
28442844
for (Token* tok = list.front(); tok; tok = tok->next()) {
28452845
if (!Token::Match(tok, "using ::| %name% ::"))
28462846
continue;
2847+
const Token* ns = tok->tokAt(tok->strAt(1) == "::" ? 2 : 1);
2848+
if (ns->isKeyword())
2849+
continue;
28472850
Token* end = tok->tokAt(3);
28482851
while (end && !Token::Match(end, "[;,]")) {
28492852
if (end->str() == "<" && end->link()) // skip template args

test/testsimplifyusing.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,18 @@ class TestSimplifyUsing : public TestFixture {
724724
ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true));
725725
ASSERT_EQUALS("", errout.str());
726726
}
727+
{
728+
const char code[] = "using namespace ::std;\n"
729+
"void f(const char* c) {\n"
730+
" cout << std::string(c) << \"abc\";\n"
731+
"}\n";
732+
const char expected[] = "using namespace :: std ; " // TODO: simplify cout?
733+
"void f ( const char * c ) { "
734+
"cout << std :: string ( c ) << \"abc\" ; "
735+
"}";
736+
ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true));
737+
ASSERT_EQUALS("", errout.str());
738+
}
727739
}
728740

729741
void simplifyUsing8970() {

0 commit comments

Comments
 (0)