Partial fix for #11378: No implicit int in C++ mode (#4696)

This commit is contained in:
chrchr-github 2023-01-18 20:37:00 +01:00 committed by GitHub
parent e205550bdb
commit dfd3e8ac55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View File

@ -1957,11 +1957,25 @@ void TokenList::simplifyPlatformTypes()
void TokenList::simplifyStdType()
{
auto isVarDeclC = [](const Token* tok) -> bool {
if (!Token::simpleMatch(tok, "}"))
return false;
tok = tok->link()->previous();
while (Token::Match(tok, "%name%")) {
if (Token::Match(tok, "struct|union|enum"))
return true;
tok = tok->previous();
}
return false;
};
for (Token *tok = front(); tok; tok = tok->next()) {
if (Token::Match(tok, "const|extern *|&|%name%") && (!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) {
if (isC() && Token::Match(tok, "const|extern *|&|%name%") && (!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) {
if (Token::Match(tok->next(), "%name% !!;"))
continue;
if (isVarDeclC(tok->previous()))
continue;
tok->insertToken("int");
tok->next()->isImplicitInt(true);

View File

@ -223,6 +223,7 @@ private:
TEST_CASE(vardecl27); // #7850 - crash on valid C code
TEST_CASE(vardecl28);
TEST_CASE(vardecl29); // #9282
TEST_CASE(vardecl30);
TEST_CASE(vardecl_stl_1);
TEST_CASE(vardecl_stl_2);
TEST_CASE(vardecl_stl_3);
@ -2518,6 +2519,14 @@ private:
tokenizeAndStringify(code));
}
void vardecl30() {
const char code[] = "struct D {} const d;";
ASSERT_EQUALS("struct D { } ; struct D const d ;",
tokenizeAndStringify(code, true, Settings::Native, "test.cpp"));
ASSERT_EQUALS("struct D { } ; struct D const d ;",
tokenizeAndStringify(code, true, Settings::Native, "test.c"));
}
void volatile_variables() {
{
const char code[] = "volatile int a=0;\n"
@ -2578,15 +2587,15 @@ private:
}
void implicitIntConst() {
ASSERT_EQUALS("const int x ;", tokenizeAndStringify("const x;"));
ASSERT_EQUALS("const int * x ;", tokenizeAndStringify("const *x;"));
ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();"));
ASSERT_EQUALS("const int x ;", tokenizeAndStringify("const x;", true, Settings::Native, "test.c"));
ASSERT_EQUALS("const int * x ;", tokenizeAndStringify("const *x;", true, Settings::Native, "test.c"));
ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();", true, Settings::Native, "test.c"));
}
void implicitIntExtern() {
ASSERT_EQUALS("extern int x ;", tokenizeAndStringify("extern x;"));
ASSERT_EQUALS("extern int * x ;", tokenizeAndStringify("extern *x;"));
ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();"));
ASSERT_EQUALS("extern int x ;", tokenizeAndStringify("extern x;", true, Settings::Native, "test.c"));
ASSERT_EQUALS("extern int * x ;", tokenizeAndStringify("extern *x;", true, Settings::Native, "test.c"));
ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();", true, Settings::Native, "test.c"));
}
/**