Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1955,11 +1955,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);
Expand Down
21 changes: 15 additions & 6 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class TestTokenizer : public TestFixture {
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);
Expand Down Expand Up @@ -2518,6 +2519,14 @@ class TestTokenizer : public TestFixture {
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"
Expand Down Expand Up @@ -2576,15 +2585,15 @@ class TestTokenizer : public TestFixture {
}

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"));
}

/**
Expand Down