Fix #11740 (FP syntaxError from switch in C code) (#5153)

This commit is contained in:
Daniel Marjamäki 2023-06-14 10:41:37 +02:00 committed by GitHub
parent 6f56a9563e
commit 9339802be3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -4085,6 +4085,7 @@ void VariableMap::addVariable(const std::string& varname, bool globalNamespace)
static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap, bool executableScope, bool cpp, bool c)
{
const Token* const tok1 = *tok;
Token* tok2 = *tok;
if (!tok2->isName())
return false;
@ -4209,13 +4210,15 @@ static bool setVarIdParseDeclaration(Token** tok, const VariableMap& variableMap
// Check if array declaration is valid (#2638)
// invalid declaration: AAA a[4] = 0;
if (typeCount >= 2 && executableScope && tok2 && tok2->str() == "[") {
const Token *tok3 = tok2->link()->next();
if (typeCount >= 2 && executableScope && Token::Match(tok2, ")| [")) {
const Token *tok3 = tok2->str() == ")" ? tok2->next() : tok2;
while (tok3 && tok3->str() == "[") {
tok3 = tok3->link()->next();
}
if (Token::Match(tok3, "= %num%"))
return false;
if (bracket && Token::Match(tok1->previous(), "[(,]") && Token::Match(tok3, "[,)]"))
return false;
}
return (typeCount >= 2 && tok2 && Token::Match(tok2->tokAt(-2), "!!:: %type%"));

View File

@ -99,6 +99,7 @@ private:
TEST_CASE(varid65); // #10936
TEST_CASE(varid66);
TEST_CASE(varid67); // #11711 - NOT function pointer
TEST_CASE(varid68); // #11740 - switch (str_chars(&strOut)[0])
TEST_CASE(varid_for_1);
TEST_CASE(varid_for_2);
TEST_CASE(varid_cpp_keywords_in_c_code);
@ -1225,6 +1226,21 @@ private:
ASSERT_EQUALS(expected1, tokenize(code1, "test.c"));
}
void varid68() { // #11740
const char code1[] = "struct S {};\n"
"char* str_chars(struct S* s);\n"
"void f(struct S strOut) {\n"
" switch (str_chars(&strOut)[0]) {}\n"
"}";
const char expected1[] = "1: struct S { } ;\n"
"2: char * str_chars ( struct S * s@1 ) ;\n"
"3: void f ( struct S strOut@2 ) {\n"
"4: switch ( str_chars ( & strOut@2 ) [ 0 ] ) { }\n"
"5: }\n";
ASSERT_EQUALS(expected1, tokenize(code1, "test.c"));
ASSERT_EQUALS(expected1, tokenize(code1, "test.cpp"));
}
void varid_for_1() {
const char code[] = "void foo(int a, int b) {\n"
" for (int a=1,b=2;;) {}\n"