Skip to content

Commit

Permalink
Fix #8454 Function configuration not found with using std:: (#6052)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Mar 3, 2024
1 parent cba2739 commit 4daa1c3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2840,6 +2840,21 @@ bool Tokenizer::simplifyUsing()
if (!isCPP() || mSettings.standards.cpp < Standards::CPP11)
return false;

// simplify using N::x; to using x = N::x;
for (Token* tok = list.front(); tok; tok = tok->next()) {
if (!Token::Match(tok, "using ::| %name% ::"))
continue;
Token* end = tok->tokAt(3);
while (end && end->str() != ";")
end = end->next();
if (!end)
continue;
if (!end->tokAt(-1)->isNameOnly()) // e.g. operator=
continue;
tok->insertToken(end->strAt(-1))->insertToken("=");
tok = end;
}

const unsigned int maxReplacementTokens = 1000; // limit the number of tokens we replace

bool substitute = false;
Expand Down
4 changes: 2 additions & 2 deletions test/testsimplifytemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2880,7 +2880,7 @@ class TestSimplifyTemplate : public TestFixture {
"struct f<b::B<0>> ; "
"} "
"} "
"b :: f<b::B<0>> g1 ; struct b :: B<0> { using B<0> :: d ; } ; "
"b :: f<b::B<0>> g1 ; struct b :: B<0> { } ; "
"struct b :: f<b::B<0>> { } ;";
ASSERT_EQUALS(exp, tok(code));
}
Expand Down Expand Up @@ -3573,7 +3573,7 @@ class TestSimplifyTemplate : public TestFixture {
"};";
const char exp[] = "template < typename ... > struct a ; "
"template < typename b , typename c , typename ... d > struct a < b c :: * , d ... > { "
"using b :: e ; "
"using e = b :: e ; "
"static_assert ( e :: f ? sizeof... ( d ) : sizeof... ( d ) , \"\" ) ; "
"} ;";
ASSERT_EQUALS(exp, tok(code));
Expand Down
11 changes: 11 additions & 0 deletions test/testsimplifyusing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class TestSimplifyUsing : public TestFixture {
TEST_CASE(simplifyUsing27);
TEST_CASE(simplifyUsing28);
TEST_CASE(simplifyUsing29);
TEST_CASE(simplifyUsing30);

TEST_CASE(simplifyUsing8970);
TEST_CASE(simplifyUsing8971);
Expand Down Expand Up @@ -682,6 +683,16 @@ class TestSimplifyUsing : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void simplifyUsing30() { // #8454
const char code[] = "using std::to_string;\n"
"void f() {\n"
" std::string str = to_string(1);\n"
"}\n";
const char expected[] = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }";
ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true));
ASSERT_EQUALS("", errout.str());
}

void simplifyUsing8970() {
const char code[] = "using V = std::vector<int>;\n"
"struct A {\n"
Expand Down

0 comments on commit 4daa1c3

Please sign in to comment.