template simplifier: fix syntax error (#2218)

This commit is contained in:
IOBYTE 2019-09-26 04:31:19 -04:00 committed by Daniel Marjamäki
parent d7d22f51b7
commit 4475c4c7e2
3 changed files with 16 additions and 4 deletions

View File

@ -419,7 +419,7 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
if (Token::Match(tok->previous(), "%var% <"))
return 0;
tok = tok->next();
if (tok->str() == ">")
if (!tok || tok->str() == ">")
return 0;
unsigned int level = 0;
@ -3505,9 +3505,6 @@ void TemplateSimplifier::simplifyTemplates(
}
}
// split ">>" into "> >"
fixAngleBrackets();
// Remove "typename" unless used in template arguments or using type alias..
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "typename %name%") && !Token::Match(tok->tokAt(-3), "using %name% ="))

View File

@ -4260,6 +4260,10 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
// Remove __asm..
simplifyAsm();
// foo < bar < >> => foo < bar < > >
if (isCPP())
mTemplateSimplifier->fixAngleBrackets();
// Bail out if code is garbage
if (mTimerResults) {
Timer t("Tokenizer::tokenize::findGarbageCode", mSettings->showtime, mTimerResults);

View File

@ -185,6 +185,7 @@ private:
TEST_CASE(template145); // syntax error
TEST_CASE(template146); // syntax error
TEST_CASE(template147); // syntax error
TEST_CASE(template148); // 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)
@ -3532,6 +3533,16 @@ private:
ASSERT_EQUALS(exp, tok(code));
}
void template148() { // syntax error
const char code[] = "static_assert(var<S1<11, 100>> == var<S1<199, 23>> / 2\n"
" && var<S1<50, 120>> == var<S1<150, var<S1<10, 10>>>>\n"
" && var<S1<53, 23>> != 222, \"\");";
const char exp[] = "static_assert ( var < S1 < 11 , 100 > > == var < S1 < 199 , 23 > > / 2 "
"&& var < S1 < 50 , 120 > > == var < S1 < 150 , var < S1 < 10 , 10 > > > > "
"&& var < S1 < 53 , 23 > > != 222 , \"\" ) ;";
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"