fix #9854 (Syntax error on valid C++ code) (#2756)

This commit is contained in:
IOBYTE 2020-08-26 01:00:58 -04:00 committed by GitHub
parent 46bf2d7d52
commit 7cf3909275
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 3 deletions

View File

@ -372,7 +372,7 @@ void TemplateSimplifier::checkComplicatedSyntaxErrorsInTemplates()
// @todo add better expression detection
if (!Token::Match(tok2->next(), "*| %type%|%num% ;"))
inclevel = true;
} else if (tok2->next() && tok2->next()->isStandardType())
} else if (tok2->next() && tok2->next()->isStandardType() && !Token::Match(tok2->tokAt(2), "(|{"))
inclevel = true;
else if (Token::simpleMatch(tok2, "< typename"))
inclevel = true;
@ -1121,7 +1121,7 @@ void TemplateSimplifier::useDefaultArgumentValues(TokenAndName &declaration)
(from->strAt(1) == ">" || (from->previous()->isName() &&
typeParameterNames.find(from->strAt(-1)) == typeParameterNames.end())))
++indentlevel;
else if (from->str() == ">")
else if (from->str() == ">" && (links.empty() || links.top()->str() == "<"))
--indentlevel;
auto entry = typeParameterNames.find(from->str());
if (entry != typeParameterNames.end() && entry->second < instantiationArgs.size()) {
@ -2609,7 +2609,8 @@ bool TemplateSimplifier::simplifyCalculations(Token* frontToken, Token *backToke
}
if (validTokenEnd(bounded, tok, backToken, 2) &&
Token::Match(tok, "char|short|int|long { }")) {
(Token::Match(tok, "char|short|int|long { }") ||
Token::Match(tok, "char|short|int|long ( )"))) {
tok->str("0"); // FIXME add type suffix
tok->isSigned(false);
tok->isUnsigned(false);

View File

@ -196,6 +196,7 @@ private:
TEST_CASE(template154); // #9495
TEST_CASE(template155); // #9539
TEST_CASE(template156);
TEST_CASE(template157); // #9854
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)
@ -3732,6 +3733,40 @@ private:
tok(code); // don't crash
}
void template157() { // #9854
const char code[] = "template <int a, bool c = a == int()> struct b1 { bool d = c; };\n"
"template <int a, bool c = a != int()> struct b2 { bool d = c; };\n"
"template <int a, bool c = a < int()> struct b3 { bool d = c; };\n"
"template <int a, bool c = a <= int()> struct b4 { bool d = c; };\n"
"template <int a, bool c = (a > int())> struct b5 { bool d = c; };\n"
"template <int a, bool c = a >= int()> struct b6 { bool d = c; };\n"
"b1<0> var1;\n"
"b2<0> var2;\n"
"b3<0> var3;\n"
"b4<0> var4;\n"
"b5<0> var5;\n"
"b6<0> var6;";
const char exp[] = "struct b1<0,true> ; "
"struct b2<0,false> ; "
"struct b3<0,false> ; "
"struct b4<0,true> ; "
"struct b5<0,false> ; "
"struct b6<0,true> ; "
"b1<0,true> var1 ; "
"b2<0,false> var2 ; "
"b3<0,false> var3 ; "
"b4<0,true> var4 ; "
"b5<0,false> var5 ; "
"b6<0,true> var6 ; "
"struct b6<0,true> { bool d ; d = true ; } ; "
"struct b5<0,false> { bool d ; d = false ; } ; "
"struct b4<0,true> { bool d ; d = true ; } ; "
"struct b3<0,false> { bool d ; d = false ; } ; "
"struct b2<0,false> { bool d ; d = false ; } ; "
"struct b1<0,true> { bool d ; d = true ; } ;";
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"