Skip to content

Commit 0e4efea

Browse files
IOBYTEamai2012
authored andcommitted
fix #9539 (Syntax error for valid C++14 code) (#2446)
1 parent eb6203c commit 0e4efea

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

lib/templatesimplifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ void TemplateSimplifier::getTemplateInstantiations()
824824
Token *tok2 = Token::findsimplematch(tok->tokAt(2), ";");
825825
if (tok2)
826826
tok = tok2;
827-
} else if (Token::Match(tok->previous(), "(|{|}|;|=|>|<<|:|.|*|&|return|<|,|! %name% ::|<|(") ||
827+
} else if (Token::Match(tok->previous(), "(|{|}|;|=|>|<<|:|.|*|&|return|<|,|!|[ %name% ::|<|(") ||
828828
Token::Match(tok->previous(), "%type% %name% ::|<") ||
829829
Token::Match(tok->tokAt(-2), "[,:] private|protected|public %name% ::|<")) {
830830
std::string scopeName = tok->scopeInfo()->name;

lib/tokenize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9347,7 +9347,7 @@ void Tokenizer::findGarbageCode() const
93479347
prev = prev->previous();
93489348
if (Token::Match(prev, "%op%|%num%|%str%|%char%")) {
93499349
if (!Token::simpleMatch(tok->tokAt(-2), "operator \"\" if") &&
9350-
!Token::simpleMatch(tok->tokAt(-2), "extern \"C\"") )
9350+
!Token::simpleMatch(tok->tokAt(-2), "extern \"C\""))
93519351
syntaxError(tok, prev == tok->previous() ? (prev->str() + " " + tok->str()) : (prev->str() + " .. " + tok->str()));
93529352
}
93539353
}
@@ -9448,7 +9448,7 @@ void Tokenizer::findGarbageCode() const
94489448
syntaxError(tok);
94499449
if (Token::Match(tok, ";|(|[ %comp%"))
94509450
syntaxError(tok);
9451-
if (Token::Match(tok, "%cop%|= ]") && !(isCPP() && Token::Match(tok->previous(), "[|, &|= ]")))
9451+
if (Token::Match(tok, "%cop%|= ]") && !(isCPP() && Token::Match(tok->previous(), "[|,|%num% &|=|> ]")))
94529452
syntaxError(tok);
94539453
if (Token::Match(tok, "[+-] [;,)]}]") && !(isCPP() && Token::Match(tok->previous(), "operator [+-] ;")))
94549454
syntaxError(tok);

test/testsimplifytemplate.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class TestSimplifyTemplate : public TestFixture {
192192
TEST_CASE(template152); // #9467
193193
TEST_CASE(template153); // #9483
194194
TEST_CASE(template154); // #9495
195+
TEST_CASE(template155); // #9539
195196
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
196197
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
197198
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@@ -2799,12 +2800,6 @@ class TestSimplifyTemplate : public TestFixture {
27992800
ASSERT_EQUALS(exp, tok(code));
28002801
}
28012802

2802-
void template154() { // #9495
2803-
const char code[] = "template <typename S, enable_if_t<(is_compile_string<S>::value), int>> void i(S s);";
2804-
const char exp[] = "template < typename S , enable_if_t < ( is_compile_string < S > :: value ) , int > > void i ( S s ) ;";
2805-
ASSERT_EQUALS(exp, tok(code));
2806-
}
2807-
28082803
void template116() { // #9178
28092804
{
28102805
const char code[] = "template <class, class a> auto b() -> decltype(a{}.template b<void(int, int)>);\n"
@@ -3666,6 +3661,33 @@ class TestSimplifyTemplate : public TestFixture {
36663661
ASSERT_EQUALS(exp, tok(code));
36673662
}
36683663

3664+
void template154() { // #9495
3665+
const char code[] = "template <typename S, enable_if_t<(is_compile_string<S>::value), int>> void i(S s);";
3666+
const char exp[] = "template < typename S , enable_if_t < ( is_compile_string < S > :: value ) , int > > void i ( S s ) ;";
3667+
ASSERT_EQUALS(exp, tok(code));
3668+
}
3669+
3670+
void template155() { // #9539
3671+
const char code[] = "template <int> int a = 0;\n"
3672+
"struct b {\n"
3673+
" void operator[](int);\n"
3674+
"};\n"
3675+
"void c() {\n"
3676+
" b d;\n"
3677+
" d[a<0>];\n"
3678+
"}";
3679+
const char exp[] = "int a<0> ; "
3680+
"a<0> = 0 ; "
3681+
"struct b { "
3682+
"void operator[] ( int ) ; "
3683+
"} ; "
3684+
"void c ( ) { "
3685+
"b d ; "
3686+
"d [ a<0> ] ; "
3687+
"}";
3688+
ASSERT_EQUALS(exp, tok(code));
3689+
}
3690+
36693691
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
36703692
const char code[] = "template <typename T> struct C {};\n"
36713693
"template <typename T> struct S {a};\n"

0 commit comments

Comments
 (0)