Skip to content

Commit dfd3e8a

Browse files
Partial fix for #11378: No implicit int in C++ mode (#4696)
1 parent e205550 commit dfd3e8a

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

lib/tokenlist.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,11 +1957,25 @@ void TokenList::simplifyPlatformTypes()
19571957

19581958
void TokenList::simplifyStdType()
19591959
{
1960+
auto isVarDeclC = [](const Token* tok) -> bool {
1961+
if (!Token::simpleMatch(tok, "}"))
1962+
return false;
1963+
tok = tok->link()->previous();
1964+
while (Token::Match(tok, "%name%")) {
1965+
if (Token::Match(tok, "struct|union|enum"))
1966+
return true;
1967+
tok = tok->previous();
1968+
}
1969+
return false;
1970+
};
1971+
19601972
for (Token *tok = front(); tok; tok = tok->next()) {
19611973

1962-
if (Token::Match(tok, "const|extern *|&|%name%") && (!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) {
1974+
if (isC() && Token::Match(tok, "const|extern *|&|%name%") && (!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) {
19631975
if (Token::Match(tok->next(), "%name% !!;"))
19641976
continue;
1977+
if (isVarDeclC(tok->previous()))
1978+
continue;
19651979

19661980
tok->insertToken("int");
19671981
tok->next()->isImplicitInt(true);

test/testtokenize.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class TestTokenizer : public TestFixture {
223223
TEST_CASE(vardecl27); // #7850 - crash on valid C code
224224
TEST_CASE(vardecl28);
225225
TEST_CASE(vardecl29); // #9282
226+
TEST_CASE(vardecl30);
226227
TEST_CASE(vardecl_stl_1);
227228
TEST_CASE(vardecl_stl_2);
228229
TEST_CASE(vardecl_stl_3);
@@ -2518,6 +2519,14 @@ class TestTokenizer : public TestFixture {
25182519
tokenizeAndStringify(code));
25192520
}
25202521

2522+
void vardecl30() {
2523+
const char code[] = "struct D {} const d;";
2524+
ASSERT_EQUALS("struct D { } ; struct D const d ;",
2525+
tokenizeAndStringify(code, true, Settings::Native, "test.cpp"));
2526+
ASSERT_EQUALS("struct D { } ; struct D const d ;",
2527+
tokenizeAndStringify(code, true, Settings::Native, "test.c"));
2528+
}
2529+
25212530
void volatile_variables() {
25222531
{
25232532
const char code[] = "volatile int a=0;\n"
@@ -2578,15 +2587,15 @@ class TestTokenizer : public TestFixture {
25782587
}
25792588

25802589
void implicitIntConst() {
2581-
ASSERT_EQUALS("const int x ;", tokenizeAndStringify("const x;"));
2582-
ASSERT_EQUALS("const int * x ;", tokenizeAndStringify("const *x;"));
2583-
ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();"));
2590+
ASSERT_EQUALS("const int x ;", tokenizeAndStringify("const x;", true, Settings::Native, "test.c"));
2591+
ASSERT_EQUALS("const int * x ;", tokenizeAndStringify("const *x;", true, Settings::Native, "test.c"));
2592+
ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();", true, Settings::Native, "test.c"));
25842593
}
25852594

25862595
void implicitIntExtern() {
2587-
ASSERT_EQUALS("extern int x ;", tokenizeAndStringify("extern x;"));
2588-
ASSERT_EQUALS("extern int * x ;", tokenizeAndStringify("extern *x;"));
2589-
ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();"));
2596+
ASSERT_EQUALS("extern int x ;", tokenizeAndStringify("extern x;", true, Settings::Native, "test.c"));
2597+
ASSERT_EQUALS("extern int * x ;", tokenizeAndStringify("extern *x;", true, Settings::Native, "test.c"));
2598+
ASSERT_EQUALS("const int * f ( ) ;", tokenizeAndStringify("const *f();", true, Settings::Native, "test.c"));
25902599
}
25912600

25922601
/**

0 commit comments

Comments
 (0)