Fixed #9046 syntaxError (#2180)

* Fixed #9046 syntaxError

* fix another syntax error

* fix some more syntax errors
This commit is contained in:
IOBYTE 2019-09-20 06:35:01 -04:00 committed by amai2012
parent 8fba2af267
commit 8c5cf8c029
4 changed files with 59 additions and 6 deletions

View File

@ -384,7 +384,8 @@ void TemplateSimplifier::checkComplicatedSyntaxErrorsInTemplates()
tok3 = tok3->tokAt(2);
if (Token::Match(tok3, "%type% <"))
inclevel = true;
}
} else if (tok2->strAt(-1) == ">")
syntaxError(tok);
if (inclevel) {
++level;

View File

@ -869,9 +869,8 @@ const Token * Token::findClosingBracket() const
} else if (Token::Match(closing, "}|]|)|;"))
return nullptr;
// we can make some guesses for template parameters
else if (closing->str() == "<" &&
(!templateParameter || (closing->previous() && closing->previous()->isName() &&
templateParameters.find(closing->strAt(-1)) == templateParameters.end())))
else if (closing->str() == "<" && closing->previous() && closing->previous()->isName() &&
(templateParameter ? templateParameters.find(closing->strAt(-1)) == templateParameters.end() : true))
++depth;
else if (closing->str() == ">") {
if (--depth == 0)

View File

@ -9408,8 +9408,9 @@ void Tokenizer::findGarbageCode() const
tok = tok->next()->findClosingBracket();
if (!tok)
syntaxError(tok1);
if (!Token::Match(tok, ">|>> ::| %name%") &&
!Token::Match(tok, ">|>> [ [ %name%"))
if (!Token::Match(tok, ">|>> ::|...| %name%") &&
!Token::Match(tok, ">|>> [ [ %name%") &&
!Token::Match(tok, "> >|*"))
syntaxError(tok->next() ? tok->next() : tok1);
}
}

View File

@ -181,6 +181,10 @@ private:
TEST_CASE(template141); // #9337
TEST_CASE(template142); // #9338
TEST_CASE(template143);
TEST_CASE(template144); // #9046
TEST_CASE(template145); // syntax error
TEST_CASE(template146); // syntax error
TEST_CASE(template147); // syntax error
TEST_CASE(template_specialization_1); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_specialization_2); // #7868 - template specialization template <typename T> struct S<C<T>> {..};
TEST_CASE(template_enum); // #6299 Syntax error in complex enum declaration (including template)
@ -3479,6 +3483,54 @@ private:
ASSERT_EQUALS(exp, tok(code));
}
void template144() { // #9046
const char code[] = "namespace a {\n"
"template <typename T, typename enable = void>\n"
"struct promote {\n"
" using type = T;\n"
"};\n"
"template <typename T>\n"
"struct promote <T, typename std::enable_if< std::is_integral<T>::value && sizeof(T) < sizeof(int) >::type>{\n"
"};\n"
"}";
const char exp[] = "namespace a { "
"template < typename T , typename enable = void > "
"struct promote { "
"using type = T ; "
"} ; "
"template < typename T > "
"struct promote < T , std :: enable_if < std :: is_integral < T > :: value && sizeof ( T ) < sizeof ( int ) > :: type > { "
"} ; "
"}";
ASSERT_EQUALS(exp, tok(code));
}
void template145() { // syntax error
const char code[] = "template<template<typename, Ts = 0> class ...Cs, Cs<Ts> ...Vs> struct B { };";
const char exp[] = "template < template < typename , Ts = 0 > class ... Cs , Cs < Ts > ... Vs > struct B { } ;";
ASSERT_EQUALS(exp, tok(code));
}
void template146() { // syntax error
const char code[] = "template<class T> struct C { };\n"
"template<class T, template<class TT_T0, template<class TT_T1> class TT_TT> class TT, class U = TT<int, C> >\n"
"struct S {\n"
" void foo(TT<T, C>);\n"
"};";
const char exp[] = "template < class T > struct C { } ; "
"template < class T , template < class TT_T0 , template < class TT_T1 > class TT_TT > class TT , class U = TT < int , C > > "
"struct S { "
"void foo ( TT < T , C > ) ; "
"} ;";
ASSERT_EQUALS(exp, tok(code));
}
void template147() { // syntax error
const char code[] = "template <template <typename> class C, typename X, C<X>*> struct b { };";
const char exp[] = "template < template < typename > class C , typename X , C < X > * > struct b { } ;";
ASSERT_EQUALS(exp, tok(code));
}
void template_specialization_1() { // #7868 - template specialization template <typename T> struct S<C<T>> {..};
const char code[] = "template <typename T> struct C {};\n"
"template <typename T> struct S {a};\n"