Fix issue 8732: Syntax error when using enable_if (#1453)

* Fix issue 8732: Syntax error when using enable_if

* Fix FPs

* Use simpleMatch
This commit is contained in:
Paul Fultz II 2018-10-28 11:16:31 -05:00 committed by Daniel Marjamäki
parent 06ede9c239
commit f5811c6818
2 changed files with 36 additions and 1 deletions

View File

@ -169,7 +169,9 @@ void TemplateSimplifier::checkComplicatedSyntaxErrorsInTemplates()
// parse this statement and see if the '<' and '>' are matching
unsigned int level = 0;
for (const Token *tok2 = tok; tok2 && !Token::Match(tok2, "[;{}]"); tok2 = tok2->next()) {
for (const Token *tok2 = tok; tok2 && !Token::simpleMatch(tok2, ";"); tok2 = tok2->next()) {
if(Token::simpleMatch(tok2, "{") && (!Token::Match(tok2->previous(), ">|%type%") || Token::simpleMatch(tok2->link(), "} ;")))
break;
if (tok2->str() == "(")
tok2 = tok2->link();
else if (tok2->str() == "<") {

View File

@ -476,6 +476,7 @@ private:
// Make sure the Tokenizer::findGarbageCode() does not have false positives
// The TestGarbage ensures that there are true positives
TEST_CASE(findGarbageCode);
TEST_CASE(checkEnableIf);
// --check-config
TEST_CASE(checkConfiguration);
@ -8648,6 +8649,38 @@ private:
}
void checkEnableIf() {
ASSERT_NO_THROW(tokenizeAndStringify(
"template<\n"
" typename U,\n"
" typename std::enable_if<\n"
" std::is_convertible<U, T>{}>::type* = nullptr>\n"
"void foo(U x);\n"))
ASSERT_NO_THROW(tokenizeAndStringify(
"template<class t>\n"
"T f(const T a, const T b) {\n"
" return a < b ? b : a;\n"
"}\n"))
ASSERT_NO_THROW(tokenizeAndStringify(
"template<class T>\n"
"struct A {\n"
" T f(const T a, const T b) {\n"
" return a < b ? b : a;\n"
" }\n"
"};\n"))
ASSERT_NO_THROW(tokenizeAndStringify(
"const int a = 1;\n"
"const int b = 2;\n"
"template<class T>\n"
"struct A {\n"
" int x = a < b ? b : a;"
"};\n"))
}
void checkConfig(const char code[]) {
errout.str("");