template simplifier: fix syntax error false positive (an expression can't be a template) (#2115)

https://stackoverflow.com/questions/57590411/cppcheck-syntax-error-with-using-typedef
This commit is contained in:
IOBYTE 2019-08-27 00:37:58 -04:00 committed by Daniel Marjamäki
parent a209c5b37a
commit 28a95802a2
3 changed files with 101 additions and 9 deletions

View File

@ -332,7 +332,7 @@ void TemplateSimplifier::checkComplicatedSyntaxErrorsInTemplates()
continue;
// skip starting tokens.. ;;; typedef typename foo::bar::..
while (Token::simpleMatch(tok, ";"))
while (Token::Match(tok, ";|{"))
tok = tok->next();
while (Token::Match(tok, "typedef|typename"))
tok = tok->next();
@ -357,9 +357,11 @@ void TemplateSimplifier::checkComplicatedSyntaxErrorsInTemplates()
bool inclevel = false;
if (Token::simpleMatch(tok2->previous(), "operator <"))
;
else if (level == 0)
inclevel = true;
else if (tok2->next() && tok2->next()->isStandardType())
else if (level == 0 && Token::Match(tok2->previous(), "%type%")) {
// @todo add better expression detection
if (!Token::Match(tok2->next(), "%type%|%num% ;"))
inclevel = true;
} else if (tok2->next() && tok2->next()->isStandardType())
inclevel = true;
else if (Token::simpleMatch(tok2, "< typename"))
inclevel = true;

View File

@ -154,7 +154,7 @@ private:
TEST_CASE(garbageCode110);
TEST_CASE(garbageCode111);
TEST_CASE(garbageCode112);
TEST_CASE(garbageCode114);
TEST_CASE(garbageCode114); // #2118
TEST_CASE(garbageCode115); // #5506
TEST_CASE(garbageCode116); // #5356
TEST_CASE(garbageCode117); // #6121
@ -909,10 +909,10 @@ private:
}
void garbageCode114() { // #2118
ASSERT_THROW(checkCode("Q_GLOBAL_STATIC_WITH_INITIALIZER(Qt4NodeStaticData, qt4NodeStaticData, {\n"
" for (unsigned i = 0 ; i < count; i++) {\n"
" }\n"
"});"), InternalError);
checkCode("Q_GLOBAL_STATIC_WITH_INITIALIZER(Qt4NodeStaticData, qt4NodeStaticData, {\n"
" for (unsigned i = 0 ; i < count; i++) {\n"
" }\n"
"});");
}
void garbageCode115() { // #5506

View File

@ -175,6 +175,7 @@ private:
TEST_CASE(template135);
TEST_CASE(template136); // #9287
TEST_CASE(template137); // #9288
TEST_CASE(template138);
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)
@ -3274,6 +3275,95 @@ private:
ASSERT_EQUALS(exp, tok(code));
}
void template138() {
{
const char code[] = "struct inferior {\n"
" using visitor = int;\n"
" template <typename T>\n"
" bool operator()(const T &a, const T &b) const {\n"
" return 1 < b;\n"
" }\n"
"};\n"
"int main() {\n"
" return 0;\n"
"}";
const char exp[] = "struct inferior { "
"template < typename T > "
"bool operator() ( const T & a , const T & b ) const { "
"return 1 < b ; "
"} "
"} ; "
"int main ( ) { "
"return 0 ; "
"}";
ASSERT_EQUALS(exp, tok(code));
}
{
const char code[] = "struct inferior {\n"
" template <typename T>\n"
" bool operator()(const T &a, const T &b) const {\n"
" return 1 < b;\n"
" }\n"
"};\n"
"int main() {\n"
" return 0;\n"
"}";
const char exp[] = "struct inferior { "
"template < typename T > "
"bool operator() ( const T & a , const T & b ) const { "
"return 1 < b ; "
"} "
"} ; "
"int main ( ) { "
"return 0 ; "
"}";
ASSERT_EQUALS(exp, tok(code));
}
{
const char code[] = "struct inferior {\n"
" using visitor = int;\n"
" template <typename T>\n"
" bool operator()(const T &a, const T &b) const {\n"
" return a < b;\n"
" }\n"
"};\n"
"int main() {\n"
" return 0;\n"
"}";
const char exp[] = "struct inferior { "
"template < typename T > "
"bool operator() ( const T & a , const T & b ) const { "
"return a < b ; "
"} "
"} ; "
"int main ( ) { "
"return 0 ; "
"}";
ASSERT_EQUALS(exp, tok(code));
}
{
const char code[] = "struct inferior {\n"
" template <typename T>\n"
" bool operator()(const T &a, const T &b) const {\n"
" return a < b;\n"
" }\n"
"};\n"
"int main() {\n"
" return 0;\n"
"}";
const char exp[] = "struct inferior { "
"template < typename T > "
"bool operator() ( const T & a , const T & b ) const { "
"return a < b ; "
"} "
"} ; "
"int main ( ) { "
"return 0 ; "
"}";
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"