Test for syntax error in nested templates (#2746)

This commit is contained in:
shaneasd 2020-08-25 13:14:44 +08:00 committed by GitHub
parent ec89c57a90
commit 45fc6a0eeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 6 deletions

View File

@ -243,6 +243,10 @@ void TemplateSimplifier::fixAngleBrackets()
if (endTok && endTok->str() == ">>") {
endTok->str(">");
endTok->insertToken(">");
} else if (endTok && endTok->str() == ">>=") {
endTok->str(">");
endTok->insertToken("=");
endTok->insertToken(">");
}
} else if (Token::Match(tok, "class|struct|union|=|:|public|protected|private %name% <")) {
Token *endTok = tok->tokAt(2)->findClosingBracket();
@ -432,7 +436,7 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
if (closing->str() == ">>")
return numberOfParameters;
tok = closing->next();
if (tok->str() == ">" || tok->str() == ">>")
if (Token::Match(tok, ">|>>|>>="))
return numberOfParameters;
else if (tok->str() == ",") {
++numberOfParameters;
@ -467,7 +471,7 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
if (level == 0)
return numberOfParameters;
--level;
} else if (tok->str() == ">>") {
} else if (tok->str() == ">>" || tok->str() == ">>=") {
if (level == 1)
return numberOfParameters;
level -= 2;
@ -493,7 +497,7 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
return 0;
if (tok->str() == ">" && level == 0)
return numberOfParameters;
else if (tok->str() == ">>" && level == 1)
else if ((tok->str() == ">>" || tok->str() == ">>=") && level == 1)
return numberOfParameters;
else if (tok->str() == ",") {
if (level == 0)
@ -550,11 +554,11 @@ unsigned int TemplateSimplifier::templateParameters(const Token *tok)
return 0;
// ,/>
while (Token::Match(tok, ">|>>")) {
while (Token::Match(tok, ">|>>|>>=")) {
if (level == 0)
return tok->str() == ">" && !Token::Match(tok->next(), "%num%") ? numberOfParameters : 0;
--level;
if (tok->str() == ">>") {
if (tok->str() == ">>" || tok->str() == ">>=") {
if (level == 0)
return !Token::Match(tok->next(), "%num%") ? numberOfParameters : 0;
--level;

View File

@ -885,7 +885,7 @@ const Token * Token::findClosingBracket() const
else if (closing->str() == ">") {
if (--depth == 0)
return closing;
} else if (closing->str() == ">>") {
} else if (closing->str() == ">>" || closing->str() == ">>=") {
if (depth <= 2)
return closing;
depth -= 2;

View File

@ -4119,6 +4119,19 @@ private:
" A a;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
//both of these should work but in cppcheck 2.1 only the first option will work (ticket #9843)
{
const std::string expected = "template < long Num > const bool foo < bar < Num > > = true ;";
ASSERT_EQUALS(expected,
tok("template <long Num>\n"
"constexpr bool foo<bar<Num> > = true;\n"));
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS(expected,
tok("template <long Num>\n"
"constexpr bool foo<bar<Num>> = true;\n"));
ASSERT_EQUALS("", errout.str());
}
}
void template_member_ptr() { // Ticket #5786