Refactor and improve Tokenizer::sizeofAddParentheses

This commit is contained in:
Daniel Marjamäki 2015-02-22 13:38:06 +01:00
parent bedc935ab0
commit d4fa656e58
4 changed files with 23 additions and 27 deletions

View File

@ -2964,33 +2964,21 @@ void Tokenizer::createLinks2()
void Tokenizer::sizeofAddParentheses()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "sizeof %name%")) {
Token *tempToken = tok->next();
while (Token::Match(tempToken, "%name%")) {
while (tempToken && tempToken->next() && tempToken->next()->str() == "[")
tempToken = tempToken->next()->link();
if (!tempToken || !tempToken->next())
break;
if (Token::Match(tempToken->next(), ". %name%")) {
// We are checking a class or struct, search next varname
tempToken = tempToken->tokAt(2);
continue;
} else if (tempToken->next()->type() == Token::eIncDecOp) {
// We have variable++ or variable--, the sizeof argument
// ends after the op
tempToken = tempToken->next();
} else if (Token::Match(tempToken->next(), "[),;}]")) {
;
} else {
break;
if (Token::Match(tok, "sizeof %name%|%num%|%str%")) {
Token *endToken = tok->next();
while (Token::Match(endToken, "%name%|%num%|%str%|[|(|.|::|++|--|!|~")) {
if (endToken->str() == "[" || endToken->str() == "(")
endToken = endToken->link();
endToken = endToken->next();
if (Token::simpleMatch(endToken, "- >"))
endToken = endToken->tokAt(2);
}
// Ok. Add ( after sizeof and ) after tempToken
if (endToken) {
// Ok. Add ( after sizeof and ) before endToken
tok->insertToken("(");
tempToken->insertToken(")");
Token::createMutualLinks(tok->next(), tempToken->next());
break;
endToken->previous()->insertToken(")");
Token::createMutualLinks(tok->next(), endToken->previous());
}
}
}

View File

@ -1441,7 +1441,7 @@ private:
"{\n"
" sizeof sizeof 1;\n"
"}\n";
ASSERT_EQUALS("void f ( ) { sizeof sizeof ( 1 ) ; }", tok(code));
ASSERT_EQUALS("void f ( ) { sizeof ( sizeof ( 1 ) ) ; }", tok(code));
ASSERT_EQUALS("", errout.str());
}

View File

@ -472,6 +472,8 @@ private:
TEST_CASE(startOfExecutableScope);
TEST_CASE(removeMacroInClassDef); // #6058
TEST_CASE(sizeofAddParentheses);
}
std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Unspecified, const char* filename = "test.cpp", bool cpp11 = true) {
@ -8739,6 +8741,12 @@ private:
ASSERT_EQUALS("class Fred : Base { } ;", tokenizeAndStringify("class Fred FINAL : Base { } ;"));
}
void sizeofAddParentheses() {
ASSERT_EQUALS("sizeof ( sizeof ( 1 ) ) ;", tokenizeAndStringify("sizeof sizeof 1;"));
ASSERT_EQUALS("sizeof ( a . b ) + 3 ;", tokenizeAndStringify("sizeof a.b+3;"));
ASSERT_EQUALS("sizeof ( a [ 2 ] . b ) + 3 ;", tokenizeAndStringify("sizeof a[2].b+3;"));
ASSERT_EQUALS("f ( 0 , sizeof ( ptr . bar ) ) ;", tokenizeAndStringify("f(0, sizeof ptr->bar );"));
}
};
REGISTER_TEST(TestTokenizer)

View File

@ -1829,7 +1829,7 @@ private:
ASSERT_EQUALS("\n\n##file 0\n"
"1: void which_test ( ) {\n"
"2: const char * argv@1 [ 2 ] = { \"./test_runner\" , \"TestClass\" } ;\n"
"3: options args@2 ( sizeof argv@1 / sizeof ( argv@1 [ 0 ] ) , argv@1 ) ;\n"
"3: options args@2 ( sizeof ( argv@1 ) / sizeof ( argv@1 [ 0 ] ) , argv@1 ) ;\n"
"4: args@2 . which_test ( ) ;\n"
"5: }\n",
tokenize("void which_test() {\n"