Skip to content

Commit

Permalink
Fix operator simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Feb 24, 2024
1 parent 1e2fe97 commit 9d680cb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
13 changes: 10 additions & 3 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10025,12 +10025,19 @@ void Tokenizer::simplifyOperatorName()
}
}

const bool returnsRef = Token::simpleMatch(par, "( & (") && tok->next()->isName();
if (par && !op.empty()) {
tok->str("operator" + op);
Token::eraseTokens(tok, par);
if (returnsRef) {
par->next()->insertToken("operator" + op)->isOperatorKeyword(true);
tok->deleteThis();
}
else {
tok->str("operator" + op);
Token::eraseTokens(tok, par);
}
}

if (!op.empty())
if (!op.empty() && !returnsRef)
tok->isOperatorKeyword(true);
}

Expand Down
4 changes: 2 additions & 2 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2909,7 +2909,7 @@ class TestSimplifyTypedef : public TestFixture {
" constexpr operator foo &() const noexcept { return _a; }\n"
"};";

const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char actual[] = "class c { char _a [ 4 ] ; constexpr char ( & operatorchar ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const operator char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
TODO_ASSERT_EQUALS(exp, actual, tok(code, false));
}
Expand All @@ -2921,7 +2921,7 @@ class TestSimplifyTypedef : public TestFixture {
" constexpr operator const foo &() const noexcept { return _a; }\n"
"};";

const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorconstchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char actual[] = "class c { char _a [ 4 ] ; constexpr const char ( & operatorconstchar ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const operator const char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
TODO_ASSERT_EQUALS(exp, actual, tok(code, false));
}
Expand Down
2 changes: 1 addition & 1 deletion test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5101,7 +5101,7 @@ class TestTokenizer : public TestFixture {
" operator A& () { return x_; }\n"
" A x_;\n"
"};";
ASSERT_EQUALS("template < typename T >\nstruct B {\n\noperatorT ( & ( ) ) [ 3 ] { return x_ ; }\nT x_ [ 3 ] ;\n} ;", tokenizeAndStringify(code));
ASSERT_EQUALS("template < typename T >\nstruct B {\n\nT ( & operatorT ( ) ) [ 3 ] { return x_ ; }\nT x_ [ 3 ] ;\n} ;", tokenizeAndStringify(code));
ASSERT_EQUALS("", errout.str());
}

Expand Down

0 comments on commit 9d680cb

Please sign in to comment.