Skip to content

Commit f53d280

Browse files
Fix #13295 syntaxError with using (danmar#6997)
Something like `s s;` where `s` is a type is legal C++, so I don't think we lose anything there.
1 parent 1a52e3b commit f53d280

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

lib/tokenize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,6 +3080,9 @@ bool Tokenizer::simplifyUsing()
30803080
if (usingEnd)
30813081
tok = usingEnd;
30823082

3083+
if (Token::Match(start, "class|struct|union|enum"))
3084+
start = start->next();
3085+
30833086
// Unfortunately we have to start searching from the beginning
30843087
// of the token stream because templates are instantiated at
30853088
// the end of the token stream and it may be used before then.

test/testsimplifyusing.cpp

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class TestSimplifyUsing : public TestFixture {
7373
TEST_CASE(simplifyUsing31);
7474
TEST_CASE(simplifyUsing32);
7575
TEST_CASE(simplifyUsing33);
76+
TEST_CASE(simplifyUsing34);
7677

7778
TEST_CASE(simplifyUsing8970);
7879
TEST_CASE(simplifyUsing8971);
@@ -231,7 +232,7 @@ class TestSimplifyUsing : public TestFixture {
231232
const char expected[] =
232233
"void f ( ) "
233234
"{ "
234-
"struct yy_buffer_state * state ; "
235+
"yy_buffer_state * state ; "
235236
"}";
236237

237238
ASSERT_EQUALS(expected, tok(code));
@@ -311,13 +312,13 @@ class TestSimplifyUsing : public TestFixture {
311312
"struct t { int a ; } ; "
312313
"struct U { int a ; } ; "
313314
"struct Unnamed0 { int a ; } ; "
314-
"struct s s ; "
315-
"struct s * ps ; "
316-
"struct t t ; "
317-
"struct t * tp ; "
318-
"struct U u ; "
319-
"struct U * v ; "
320-
"struct Unnamed0 * w ;";
315+
"s s ; "
316+
"s * ps ; "
317+
"t t ; "
318+
"t * tp ; "
319+
"U u ; "
320+
"U * v ; "
321+
"Unnamed0 * w ;";
321322

322323
ASSERT_EQUALS(expected, tok(code));
323324
}
@@ -342,13 +343,13 @@ class TestSimplifyUsing : public TestFixture {
342343
"union t { int a ; float b ; } ; "
343344
"union U { int a ; float b ; } ; "
344345
"union Unnamed0 { int a ; float b ; } ; "
345-
"union s s ; "
346-
"union s * ps ; "
347-
"union t t ; "
348-
"union t * tp ; "
349-
"union U u ; "
350-
"union U * v ; "
351-
"union Unnamed0 * w ;";
346+
"s s ; "
347+
"s * ps ; "
348+
"t t ; "
349+
"t * tp ; "
350+
"U u ; "
351+
"U * v ; "
352+
"Unnamed0 * w ;";
352353

353354
ASSERT_EQUALS(expected, tok(code));
354355
}
@@ -361,8 +362,8 @@ class TestSimplifyUsing : public TestFixture {
361362

362363
const char expected[] = "enum abc { a = 0 , b = 1 , c = 2 } ; "
363364
"enum xyz { x = 0 , y = 1 , z = 2 } ; "
364-
"enum abc e1 ; "
365-
"enum xyz e2 ;";
365+
"abc e1 ; "
366+
"xyz e2 ;";
366367

367368
ASSERT_EQUALS(expected, tok(code));
368369
}
@@ -448,7 +449,7 @@ class TestSimplifyUsing : public TestFixture {
448449
const char expected[] = "struct STRFOO { "
449450
"char freem [ 4096 ] ; "
450451
"} ; "
451-
"struct STRFOO s ;";
452+
"STRFOO s ;";
452453

453454
ASSERT_EQUALS(expected, tok(code));
454455
ASSERT_EQUALS("", errout_str());
@@ -470,10 +471,10 @@ class TestSimplifyUsing : public TestFixture {
470471
"class S2 : public C1 { } ; "
471472
"class S3 { } ; "
472473
"class S4 : public C1 { } ; "
473-
"class S1 s1 ; "
474-
"class S2 s2 ; "
475-
"class S3 s3 ; "
476-
"class S4 s4 ;";
474+
"S1 s1 ; "
475+
"S2 s2 ; "
476+
"S3 s3 ; "
477+
"S4 s4 ;";
477478

478479
ASSERT_EQUALS(expected, tok(code));
479480
}
@@ -833,6 +834,26 @@ class TestSimplifyUsing : public TestFixture {
833834
ASSERT_EQUALS("", errout_str());
834835
}
835836

837+
void simplifyUsing34() { // #13295
838+
const char code[] = "struct A {\n"
839+
" static const int a = 1;\n"
840+
"};\n"
841+
"using B = struct A;\n"
842+
"void g(int);\n"
843+
"void f() {\n"
844+
" g({ B::a });\n"
845+
"}\n";
846+
const char expected[] = "struct A { "
847+
"static const int a = 1 ; "
848+
"} ; "
849+
"void g ( int ) ; "
850+
"void f ( ) { "
851+
"g ( { A :: a } ) ; "
852+
"}";
853+
ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true));
854+
ASSERT_EQUALS("", errout_str());
855+
}
856+
836857
void simplifyUsing8970() {
837858
const char code[] = "using V = std::vector<int>;\n"
838859
"struct A {\n"

0 commit comments

Comments
 (0)