Skip to content

Commit

Permalink
Fix #12081 (Tokenizer::simplifyTypedef: Handle volatile structs bette…
Browse files Browse the repository at this point in the history
…r) (#5577)
  • Loading branch information
danmar committed Oct 19, 2023
1 parent 04ece4f commit 17ea101
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,15 @@ namespace {
static Token *splitDefinitionFromTypedef(Token *tok, nonneg int *unnamedCount)
{
std::string name;
bool isConst = false;
Token *tok1 = tok->next();
std::set<std::string> qualifiers;

// skip const if present
if (tok1->str() == "const") {
tok1->deleteThis();
isConst = true;
while (Token::Match(tok->next(), "const|volatile")) {
qualifiers.insert(tok->next()->str());
tok->deleteNext();
}

// skip "class|struct|union|enum"
tok1 = tok1->next();
Token *tok1 = tok->tokAt(2);

const bool hasName = Token::Match(tok1, "%name%");

Expand Down Expand Up @@ -465,8 +463,8 @@ static Token *splitDefinitionFromTypedef(Token *tok, nonneg int *unnamedCount)
tok1->insertToken("typedef");
tok1 = tok1->next();
Token * tok3 = tok1;
if (isConst) {
tok1->insertToken("const");
for (const std::string &qualifier : qualifiers) {
tok1->insertToken(qualifier);
tok1 = tok1->next();
}
tok1->insertToken(tok->next()->str()); // struct, union or enum
Expand Down Expand Up @@ -1209,7 +1207,10 @@ void Tokenizer::simplifyTypedefCpp()

// pull struct, union, enum or class definition out of typedef
// use typedef name for unnamed struct, union, enum or class
if (Token::Match(tok->next(), "const| struct|enum|union|class %type%| {|:")) {
const Token* tokClass = tok->next();
while (Token::Match(tokClass, "const|volatile"))
tokClass = tokClass->next();
if (Token::Match(tokClass, "struct|enum|union|class %type%| {|:")) {
Token *tok1 = splitDefinitionFromTypedef(tok, &mUnnamedCount);
if (!tok1)
continue;
Expand Down
16 changes: 16 additions & 0 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedefFunction9);
TEST_CASE(simplifyTypedefFunction10); // #5191

TEST_CASE(simplifyTypedefStruct); // #12081 - volatile struct

TEST_CASE(simplifyTypedefShadow); // #4445 - shadow variable

TEST_CASE(simplifyTypedefMacro);
Expand Down Expand Up @@ -4002,6 +4004,20 @@ class TestSimplifyTypedef : public TestFixture {
tok(code,false));
}

void simplifyTypedefStruct() {
const char code1[] = "typedef struct S { int x; } xyz;\n"
"xyz var;";
ASSERT_EQUALS("struct S { int x ; } ; struct S var ;", tok(code1,false));

const char code2[] = "typedef const struct S { int x; } xyz;\n"
"xyz var;";
ASSERT_EQUALS("struct S { int x ; } ; const struct S var ;", tok(code2,false));

const char code3[] = "typedef volatile struct S { int x; } xyz;\n"
"xyz var;";
ASSERT_EQUALS("struct S { int x ; } ; volatile struct S var ;", tok(code3,false));
}

void simplifyTypedefShadow() { // shadow variable (#4445)
const char code[] = "typedef struct { int x; } xyz;;\n"
"void f(){\n"
Expand Down

0 comments on commit 17ea101

Please sign in to comment.