Fix #12165 (simplifyTypedef: the "enum" token is not inserted) (#5641)

This commit is contained in:
Daniel Marjamäki 2023-11-09 07:48:33 +01:00 committed by GitHub
parent 30bf5cac0a
commit 780b742568
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 11 deletions

View File

@ -630,7 +630,7 @@ namespace {
start = start->next(); start = start->next();
// TODO handle unnamed structs etc // TODO handle unnamed structs etc
if (Token::Match(start, "const| enum|struct|union|class %name% {")) { if (Token::Match(start, "const| enum|struct|union|class %name%| {")) {
const std::pair<Token*, Token*> rangeBefore(start, Token::findsimplematch(start, "{")); const std::pair<Token*, Token*> rangeBefore(start, Token::findsimplematch(start, "{"));
// find typedef name token // find typedef name token
@ -640,6 +640,8 @@ namespace {
const std::pair<Token*, Token*> rangeQualifiers(rangeBefore.second->link()->next(), nameToken); const std::pair<Token*, Token*> rangeQualifiers(rangeBefore.second->link()->next(), nameToken);
if (Token::Match(nameToken, "%name% ;")) { if (Token::Match(nameToken, "%name% ;")) {
if (Token::Match(rangeBefore.second->previous(), "enum|struct|union|class {"))
rangeBefore.second->previous()->insertToken(nameToken->str());
mRangeType = rangeBefore; mRangeType = rangeBefore;
mRangeTypeQualifiers = rangeQualifiers; mRangeTypeQualifiers = rangeQualifiers;
Token* typeName = rangeBefore.second->previous(); Token* typeName = rangeBefore.second->previous();
@ -774,7 +776,7 @@ namespace {
} }
// Inherited type => skip "struct" / "class" // Inherited type => skip "struct" / "class"
if (Token::Match(mRangeType.first, "const| struct|class %name% {") && Token::Match(tok->previous(), "public|protected|private")) { if (Token::Match(mRangeType.first, "const| struct|class %name% {") && Token::Match(tok->previous(), "public|protected|private|<")) {
tok->originalName(tok->str()); tok->originalName(tok->str());
tok->str(mRangeType.second->previous()->str()); tok->str(mRangeType.second->previous()->str());
return; return;

View File

@ -5025,7 +5025,7 @@ private:
} }
// #9353 // #9353
check("typedef struct { std::string s; } X;\n" check("struct X { std::string s; };\n"
"void f(const std::vector<X>&v) {\n" "void f(const std::vector<X>&v) {\n"
" for (std::vector<X>::const_iterator it = v.begin(); it != v.end(); ++it)\n" " for (std::vector<X>::const_iterator it = v.begin(); it != v.end(); ++it)\n"
" if (!it->s.empty()) {\n" " if (!it->s.empty()) {\n"
@ -5034,6 +5034,15 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style) Condition '!it->s.empty()' is always true\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style) Condition '!it->s.empty()' is always true\n", errout.str());
check("struct X { std::string s; };\n"
"void f(const std::vector<struct X>&v) {\n"
" for (std::vector<struct X>::const_iterator it = v.begin(); it != v.end(); ++it)\n"
" if (!it->s.empty()) {\n"
" if (!it->s.empty()) {}\n"
" }\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style) Condition '!it->s.empty()' is always true\n", "", errout.str());
// #10508 // #10508
check("bool f(const std::string& a, const std::string& b) {\n" check("bool f(const std::string& a, const std::string& b) {\n"
" return a.empty() || (b.empty() && a.empty());\n" " return a.empty() || (b.empty() && a.empty());\n"

View File

@ -52,6 +52,7 @@ private:
TEST_CASE(cstruct3); TEST_CASE(cstruct3);
TEST_CASE(cstruct3); TEST_CASE(cstruct3);
TEST_CASE(cstruct4); TEST_CASE(cstruct4);
TEST_CASE(cenum1);
TEST_CASE(cfunction1); TEST_CASE(cfunction1);
TEST_CASE(cfunction2); TEST_CASE(cfunction2);
TEST_CASE(cfunction3); TEST_CASE(cfunction3);
@ -372,8 +373,8 @@ private:
void cstruct2() { void cstruct2() {
const char code[] = "typedef enum { A, B } t;\n" const char code[] = "typedef enum { A, B } t;\n"
"t x;"; "t x;";
ASSERT_EQUALS("enum t { A , B } ; t x ;", simplifyTypedef(code)); ASSERT_EQUALS("enum t { A , B } ; enum t x ;", simplifyTypedef(code));
ASSERT_EQUALS("enum t { A , B } ; t x ;", simplifyTypedefC(code)); ASSERT_EQUALS("enum t { A , B } ; enum t x ;", simplifyTypedefC(code));
} }
void cstruct3() { void cstruct3() {
@ -388,6 +389,12 @@ private:
ASSERT_EQUALS("struct s { int a ; int b ; } ; struct s x { } ;", simplifyTypedefC(code)); ASSERT_EQUALS("struct s { int a ; int b ; } ; struct s x { } ;", simplifyTypedefC(code));
} }
void cenum1() {
const char code[] = "typedef enum { a, b } E;\n"
"E e;";
ASSERT_EQUALS("enum E { a , b } ; enum E e ;", simplifyTypedefC(code));
}
void cfunction1() { void cfunction1() {
const char code[] = "typedef int callback(int);\n" const char code[] = "typedef int callback(int);\n"
"callback* cb;"; "callback* cb;";
@ -694,13 +701,13 @@ private:
const char expected[] = const char expected[] =
"struct t { int a ; } ; " "struct t { int a ; } ; "
"struct U { int a ; } ; " "struct U { int a ; } ; "
"struct Unnamed0 { int a ; } ; " "struct V { int a ; } ; "
"struct s s ; " "struct s s ; "
"struct s * ps ; " "struct s * ps ; "
"struct t t ; " "struct t t ; "
"struct t * tp ; " "struct t * tp ; "
"struct U u ; " "struct U u ; "
"struct Unnamed0 * v ;"; "struct V * v ;";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
@ -720,13 +727,13 @@ private:
const char expected[] = const char expected[] =
"union t { int a ; float b ; } ; " "union t { int a ; float b ; } ; "
"union U { int a ; float b ; } ; " "union U { int a ; float b ; } ; "
"union Unnamed0 { int a ; float b ; } ; " "union V { int a ; float b ; } ; "
"union s s ; " "union s s ; "
"union s * ps ; " "union s * ps ; "
"union t t ; " "union t t ; "
"union t * tp ; " "union t * tp ; "
"union U u ; " "union U u ; "
"union Unnamed0 * v ;"; "union V * v ;";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
} }
@ -739,7 +746,7 @@ private:
const char expected[] = "enum abc { a = 0 , b = 1 , c = 2 } ; " const char expected[] = "enum abc { a = 0 , b = 1 , c = 2 } ; "
"enum xyz { x = 0 , y = 1 , z = 2 } ; " "enum xyz { x = 0 , y = 1 , z = 2 } ; "
"abc e1 ; " "enum abc e1 ; "
"enum xyz e2 ;"; "enum xyz e2 ;";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
@ -1837,7 +1844,7 @@ private:
" localEntitiyAddFunc_t f;\n" " localEntitiyAddFunc_t f;\n"
"}"; "}";
// The expected result.. // The expected result..
const char expected[] = "enum qboolean { qfalse , qtrue } ; void f ( ) { qboolean b ; qboolean ( * f ) ( struct le_s * , entity_t * ) ; }"; const char expected[] = "enum qboolean { qfalse , qtrue } ; void f ( ) { enum qboolean b ; enum qboolean ( * f ) ( struct le_s * , entity_t * ) ; }";
ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS(expected, tok(code, false));
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }