Fix bug in Tokenizer::simplifyVarDecl

This commit is contained in:
Daniel Marjamäki 2021-06-06 08:13:40 +02:00
parent 3c458d8b7b
commit 3c3435dd10
2 changed files with 14 additions and 1 deletions

View File

@ -7438,7 +7438,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co
else if (std::strchr(";,", tok2->str()[0])) {
// "type var =" => "type var; var ="
const Token *varTok = type0->tokAt(typelen);
while (Token::Match(varTok, "*|&|const|volatile"))
while (Token::Match(varTok, "%name%|*|& %name%|*|&"))
varTok = varTok->next();
if (!varTok)
syntaxError(tok2); // invalid code

View File

@ -215,6 +215,7 @@ private:
TEST_CASE(vardecl25); // #4799 - segmentation fault
TEST_CASE(vardecl26); // #5907 - incorrect handling of extern declarations
TEST_CASE(vardecl27); // #7850 - crash on valid C code
TEST_CASE(vardecl28);
TEST_CASE(vardecl_stl_1);
TEST_CASE(vardecl_stl_2);
TEST_CASE(vardecl_template_1);
@ -2473,6 +2474,18 @@ private:
tokenizeAndStringify(code, /*expand=*/true, Settings::Native, "test.c");
}
void vardecl28() {
const char code[] = "unsigned short f(void) {\n"
" unsigned short const int x = 1;\n"
" return x;\n"
"}";
ASSERT_EQUALS("unsigned short f ( void ) {\n"
"const unsigned short x ; x = 1 ;\n"
"return x ;\n"
"}",
tokenizeAndStringify(code, /*expand=*/true, Settings::Native, "test.c"));
}
void volatile_variables() {
const char code[] = "volatile int a=0;\n"
"volatile int b=0;\n"