Skip to content

Commit 99080da

Browse files
Fix #14151 syntaxError with explicit operator call in braced initializer (danmar#7850)
1 parent 99523e1 commit 99080da

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10356,7 +10356,7 @@ bool Tokenizer::operatorEnd(const Token * tok)
1035610356
return true;
1035710357

1035810358
tok = tok->next();
10359-
while (tok && !Token::Match(tok, "[=;{),]")) {
10359+
while (tok && !Token::Match(tok, "[=;{}),]")) {
1036010360
if (Token::Match(tok, "const|volatile|override")) {
1036110361
tok = tok->next();
1036210362
} else if (tok->str() == "noexcept") {

test/testtokenize.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ class TestTokenizer : public TestFixture {
350350
TEST_CASE(simplifyOperatorName27);
351351
TEST_CASE(simplifyOperatorName28);
352352
TEST_CASE(simplifyOperatorName29); // spaceship operator
353+
TEST_CASE(simplifyOperatorName30);
353354
TEST_CASE(simplifyOperatorName31); // #6342
354355
TEST_CASE(simplifyOperatorName32); // #10256
355356
TEST_CASE(simplifyOperatorName33); // #10138
@@ -5328,29 +5329,6 @@ class TestTokenizer : public TestFixture {
53285329
ASSERT_EQUALS(code, tokenizeAndStringify(code));
53295330
}
53305331

5331-
void simplifyOperatorName31() { // #6342
5332-
const char code[] = "template <typename T>\n"
5333-
"struct B {\n"
5334-
" typedef T A[3];\n"
5335-
" operator A& () { return x_; }\n"
5336-
" A x_;\n"
5337-
"};";
5338-
ASSERT_EQUALS("template < typename T >\nstruct B {\n\nT ( & operatorT ( ) ) [ 3 ] { return x_ ; }\nT x_ [ 3 ] ;\n} ;", tokenizeAndStringify(code));
5339-
ASSERT_EQUALS("", errout_str());
5340-
}
5341-
5342-
void simplifyOperatorName32() { // #10256
5343-
const char code[] = "void f(int* = nullptr) {}\n";
5344-
ASSERT_EQUALS("void f ( int * = nullptr ) { }", tokenizeAndStringify(code));
5345-
ASSERT_EQUALS("", errout_str());
5346-
}
5347-
5348-
void simplifyOperatorName33() { // #10138
5349-
const char code[] = "int (operator\"\" _ii)(unsigned long long v) { return v; }\n";
5350-
ASSERT_EQUALS("int operator\"\"_ii ( unsigned long long v ) { return v ; }", tokenizeAndStringify(code));
5351-
ASSERT_EQUALS("", errout_str());
5352-
}
5353-
53545332
void simplifyOperatorName10() { // #8746
53555333
const char code1[] = "using a::operator=;";
53565334
ASSERT_EQUALS("using a :: operator= ;", tokenizeAndStringify(code1));
@@ -5627,6 +5605,43 @@ class TestTokenizer : public TestFixture {
56275605
ASSERT_EQUALS("auto operator<=> ( ) ;", tokenizeAndStringify("auto operator<=>();", settings));
56285606
}
56295607

5608+
void simplifyOperatorName30() { // #14151
5609+
const char code[] = "struct S { int operator()() const { return 42; } };\n"
5610+
"int f() {\n"
5611+
" S s;\n"
5612+
" return int{ s.operator()() };\n"
5613+
"}\n";
5614+
ASSERT_EQUALS("struct S { int operator() ( ) const { return 42 ; } } ;\n"
5615+
"int f ( ) {\n"
5616+
"S s ;\n"
5617+
"return int { s . operator() ( ) } ;\n"
5618+
"}", tokenizeAndStringify(code));
5619+
ASSERT_EQUALS("", errout_str());
5620+
}
5621+
5622+
void simplifyOperatorName31() { // #6342
5623+
const char code[] = "template <typename T>\n"
5624+
"struct B {\n"
5625+
" typedef T A[3];\n"
5626+
" operator A& () { return x_; }\n"
5627+
" A x_;\n"
5628+
"};";
5629+
ASSERT_EQUALS("template < typename T >\nstruct B {\n\nT ( & operatorT ( ) ) [ 3 ] { return x_ ; }\nT x_ [ 3 ] ;\n} ;", tokenizeAndStringify(code));
5630+
ASSERT_EQUALS("", errout_str());
5631+
}
5632+
5633+
void simplifyOperatorName32() { // #10256
5634+
const char code[] = "void f(int* = nullptr) {}\n";
5635+
ASSERT_EQUALS("void f ( int * = nullptr ) { }", tokenizeAndStringify(code));
5636+
ASSERT_EQUALS("", errout_str());
5637+
}
5638+
5639+
void simplifyOperatorName33() { // #10138
5640+
const char code[] = "int (operator\"\" _ii)(unsigned long long v) { return v; }\n";
5641+
ASSERT_EQUALS("int operator\"\"_ii ( unsigned long long v ) { return v ; }", tokenizeAndStringify(code));
5642+
ASSERT_EQUALS("", errout_str());
5643+
}
5644+
56305645
void simplifyOverloadedOperators1() {
56315646
const char code[] = "struct S { void operator()(int); };\n"
56325647
"\n"

0 commit comments

Comments
 (0)