Refactor and improve Tokenizer::sizeofAddParentheses
This commit is contained in:
parent
bedc935ab0
commit
d4fa656e58
|
@ -2964,33 +2964,21 @@ void Tokenizer::createLinks2()
|
||||||
void Tokenizer::sizeofAddParentheses()
|
void Tokenizer::sizeofAddParentheses()
|
||||||
{
|
{
|
||||||
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
||||||
if (Token::Match(tok, "sizeof %name%")) {
|
if (Token::Match(tok, "sizeof %name%|%num%|%str%")) {
|
||||||
Token *tempToken = tok->next();
|
Token *endToken = tok->next();
|
||||||
while (Token::Match(tempToken, "%name%")) {
|
while (Token::Match(endToken, "%name%|%num%|%str%|[|(|.|::|++|--|!|~")) {
|
||||||
while (tempToken && tempToken->next() && tempToken->next()->str() == "[")
|
if (endToken->str() == "[" || endToken->str() == "(")
|
||||||
tempToken = tempToken->next()->link();
|
endToken = endToken->link();
|
||||||
if (!tempToken || !tempToken->next())
|
endToken = endToken->next();
|
||||||
break;
|
if (Token::simpleMatch(endToken, "- >"))
|
||||||
|
endToken = endToken->tokAt(2);
|
||||||
|
}
|
||||||
|
|
||||||
if (Token::Match(tempToken->next(), ". %name%")) {
|
if (endToken) {
|
||||||
// We are checking a class or struct, search next varname
|
// Ok. Add ( after sizeof and ) before endToken
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ok. Add ( after sizeof and ) after tempToken
|
|
||||||
tok->insertToken("(");
|
tok->insertToken("(");
|
||||||
tempToken->insertToken(")");
|
endToken->previous()->insertToken(")");
|
||||||
Token::createMutualLinks(tok->next(), tempToken->next());
|
Token::createMutualLinks(tok->next(), endToken->previous());
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1441,7 +1441,7 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" sizeof sizeof 1;\n"
|
" sizeof sizeof 1;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
ASSERT_EQUALS("void f ( ) { sizeof sizeof ( 1 ) ; }", tok(code));
|
ASSERT_EQUALS("void f ( ) { sizeof ( sizeof ( 1 ) ) ; }", tok(code));
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -472,6 +472,8 @@ private:
|
||||||
TEST_CASE(startOfExecutableScope);
|
TEST_CASE(startOfExecutableScope);
|
||||||
|
|
||||||
TEST_CASE(removeMacroInClassDef); // #6058
|
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) {
|
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 { } ;"));
|
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)
|
REGISTER_TEST(TestTokenizer)
|
||||||
|
|
|
@ -1829,7 +1829,7 @@ private:
|
||||||
ASSERT_EQUALS("\n\n##file 0\n"
|
ASSERT_EQUALS("\n\n##file 0\n"
|
||||||
"1: void which_test ( ) {\n"
|
"1: void which_test ( ) {\n"
|
||||||
"2: const char * argv@1 [ 2 ] = { \"./test_runner\" , \"TestClass\" } ;\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"
|
"4: args@2 . which_test ( ) ;\n"
|
||||||
"5: }\n",
|
"5: }\n",
|
||||||
tokenize("void which_test() {\n"
|
tokenize("void which_test() {\n"
|
||||||
|
|
Loading…
Reference in New Issue