Fix #10936 FP constStatement with extern declaration (#3960)

This commit is contained in:
chrchr-github 2022-03-31 21:08:04 +02:00 committed by GitHub
parent c85e7e7d2f
commit 183969cd4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 2 deletions

View File

@ -3482,8 +3482,8 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
hasstruct = true;
typeCount = 0;
singleNameCount = 0;
} else if (tok2->str() == "const") {
// just skip "const"
} else if (Token::Match(tok2, "const|extern")) {
// just skip "const", "extern"
} else if (!hasstruct && variableId.find(tok2->str()) != variableId.end() && tok2->previous()->str() != "::") {
++typeCount;
tok2 = tok2->next();
@ -3521,6 +3521,8 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
bracket = true; // Skip: Seems to be valid pointer to array or function pointer
} else if (singleNameCount >= 1 && Token::Match(tok2, "( * %name% [") && Token::Match(tok2->linkAt(3), "] ) [;,]")) {
bracket = true;
} else if (singleNameCount >= 1 && tok2->previous() && tok2->previous()->isStandardType() && Token::Match(tok2, "( *|&| %name% ) ;")) {
bracket = true;
} else if (tok2->str() == "::") {
singleNameCount = 0;
} else if (tok2->str() != "*" && tok2->str() != "::" && tok2->str() != "...") {

View File

@ -614,6 +614,12 @@ private:
" return [](int i) { return i > 0; }(s.i);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("extern int (*p);\n" // #10936
"void f() {\n"
" for (int i = 0; ;) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void vardecl() {

View File

@ -96,6 +96,7 @@ private:
TEST_CASE(varid62);
TEST_CASE(varid63);
TEST_CASE(varid64); // #9928 - extern const char (*x[256])
TEST_CASE(varid65); // #10936
TEST_CASE(varid_for_1);
TEST_CASE(varid_for_2);
TEST_CASE(varid_cpp_keywords_in_c_code);
@ -1174,6 +1175,29 @@ private:
ASSERT_EQUALS(expected, tokenize(code));
}
void varid65() { // #10936
{
const char code[] = "extern int (*p);";
const char expected[] = "1: extern int ( * p@1 ) ;\n";
ASSERT_EQUALS(expected, tokenize(code));
}
{
const char code[] = "extern int (i);";
const char expected[] = "1: extern int ( i@1 ) ;\n";
ASSERT_EQUALS(expected, tokenize(code));
}
{
const char code[] = "int (*p);";
const char expected[] = "1: int ( * p@1 ) ;\n";
ASSERT_EQUALS(expected, tokenize(code));
}
{
const char code[] = "int (i);";
const char expected[] = "1: int ( i@1 ) ;\n";
ASSERT_EQUALS(expected, tokenize(code));
}
}
void varid_for_1() {
const char code[] = "void foo(int a, int b) {\n"
" for (int a=1,b=2;;) {}\n"