Tokenizer: simplify bitfields. Ticket: #1956

This commit is contained in:
Robert Reif 2010-08-21 16:34:41 +02:00 committed by Daniel Marjamäki
parent 8432e0048f
commit ac9078857e
2 changed files with 67 additions and 5 deletions

View File

@ -7981,19 +7981,47 @@ void Tokenizer::simplifyAsm()
}
}
// Simplify bitfields
static bool isAllUpper(const Token *type)
{
if (type->isName())
{
bool allupper = true;
const std::string s(type->str());
for (std::string::size_type pos = 0; pos < s.size(); ++pos)
{
const char ch = s[pos];
if (!(ch == '_' || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')))
{
allupper = false;
break;
}
}
return allupper;
}
return false;
}
static bool isBitfieldType(const Token *type)
{
if (Token::Match(type, "signed|unsigned|int|long|bool|char|short") || isAllUpper(type))
return true;
return false;
}
void Tokenizer::simplifyBitfields()
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, ";|{|public:|protected:|private: signed|unsigned|int|long|bool|char|short %var% : %num% ;"))
if (Token::Match(tok, ";|{|public:|protected:|private: %type% %var% : %num% ;") && isBitfieldType(tok->next()))
Token::eraseTokens(tok->tokAt(2), tok->tokAt(5));
else if (Token::Match(tok, ";|{|public:|protected:|private: signed|unsigned int|long|char|short %var% : %num% ;"))
else if (Token::Match(tok, ";|{|public:|protected:|private: signed|unsigned %type% %var% : %num% ;") && isBitfieldType(tok->tokAt(2)))
Token::eraseTokens(tok->tokAt(3), tok->tokAt(6));
else if (Token::Match(tok, ";|{|public:|protected:|private: const signed|unsigned|int|long|bool|char|short %var% : %num% ;"))
else if (Token::Match(tok, ";|{|public:|protected:|private: const %type% %var% : %num% ;") && isBitfieldType(tok->tokAt(2)))
Token::eraseTokens(tok->tokAt(3), tok->tokAt(6));
else if (Token::Match(tok, ";|{|public:|protected:|private: const signed|unsigned int|long|char|short %var% : %num% ;"))
else if (Token::Match(tok, ";|{|public:|protected:|private: const signed|unsigned %type% %var% : %num% ;") && isBitfieldType(tok->tokAt(3)))
Token::eraseTokens(tok->tokAt(4), tok->tokAt(7));
}
}

View File

@ -246,6 +246,7 @@ private:
TEST_CASE(bitfields1);
TEST_CASE(bitfields2);
TEST_CASE(bitfields3);
TEST_CASE(bitfields4); // ticket #1956
TEST_CASE(microsoftMFC);
}
@ -4372,6 +4373,39 @@ private:
ASSERT_EQUALS("struct A { public: const unsigned long x ; } ;", tokenizeAndStringify(code4,false));
}
void bitfields4() // ticket #1956
{
const char code1[] = "struct A { CHAR x : 3; };";
ASSERT_EQUALS("struct A { CHAR x ; } ;", tokenizeAndStringify(code1,false));
const char code2[] = "struct A { UCHAR x : 3; };";
ASSERT_EQUALS("struct A { UCHAR x ; } ;", tokenizeAndStringify(code2,false));
const char code3[] = "struct A { BYTE x : 3; };";
ASSERT_EQUALS("struct A { BYTE x ; } ;", tokenizeAndStringify(code3,false));
const char code4[] = "struct A { WORD x : 3; };";
ASSERT_EQUALS("struct A { WORD x ; } ;", tokenizeAndStringify(code4,false));
const char code5[] = "struct A { DWORD x : 3; };";
ASSERT_EQUALS("struct A { DWORD x ; } ;", tokenizeAndStringify(code5,false));
const char code6[] = "struct A { LONG x : 3; };";
ASSERT_EQUALS("struct A { LONG x ; } ;", tokenizeAndStringify(code6,false));
const char code7[] = "struct A { UINT8 x : 3; };";
ASSERT_EQUALS("struct A { UINT8 x ; } ;", tokenizeAndStringify(code7,false));
const char code8[] = "struct A { UINT16 x : 3; };";
ASSERT_EQUALS("struct A { UINT16 x ; } ;", tokenizeAndStringify(code8,false));
const char code9[] = "struct A { UINT32 x : 3; };";
ASSERT_EQUALS("struct A { UINT32 x ; } ;", tokenizeAndStringify(code9,false));
const char code10[] = "struct A { UINT64 x : 3; };";
ASSERT_EQUALS("struct A { UINT64 x ; } ;", tokenizeAndStringify(code10,false));
}
void microsoftMFC()
{
const char code1[] = "class MyDialog : public CDialog { DECLARE_MESSAGE_MAP() private: CString text; };";