Simplify function pointers

This commit is contained in:
Daniel Marjamäki 2010-01-20 21:19:06 +01:00
parent e163f878ab
commit 96359b14ff
5 changed files with 62 additions and 3 deletions

View File

@ -579,6 +579,7 @@ void CheckClass::privateFunctions()
tok = tok->tokAt(2)->link();
else if (Token::Match(tok, "%var% (") &&
!Token::simpleMatch(tok->next()->link(), ") (") &&
!Token::Match(tok, classname.c_str()))
{
FuncList.push_back(tok);

View File

@ -941,6 +941,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
// remove exception specifications..
removeExceptionSpecifications(_tokens);
// simplify function pointers
simplifyFunctionPointers();
setVarId();
if (!validate())
return false;
@ -3291,6 +3294,36 @@ void Tokenizer::simplifyFunctionParameters()
}
void Tokenizer:: simplifyFunctionPointers()
{
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->previous() && !Token::Match(tok->previous(), "[{};]"))
continue;
if (Token::Match(tok, "%type% *| *| ( * %var% ) ("))
;
else if (Token::Match(tok, "%type% %type% *| *| ( * %var% ) ("))
tok = tok->next();
else
continue;
while (tok->next()->str() == "*")
tok = tok->next();
// check that the declaration ends with ;
if (!Token::simpleMatch(tok->tokAt(5)->link(), ") ;"))
continue;
// ok simplify this function pointer to an ordinary pointer
tok->deleteNext();
tok->tokAt(2)->deleteNext();
const Token *tok2 = tok->tokAt(3)->link();
Token::eraseTokens(tok->tokAt(2), tok2 ? tok2->next() : 0);
}
}
bool Tokenizer::simplifyFunctionReturn()
{
bool ret = false;

View File

@ -336,6 +336,9 @@ private:
*/
void simplifyStd();
/** Simplify function pointers */
void simplifyFunctionPointers();
/**
* Remove exception specifications. This function calls itself recursively.
* @param tok First token in scope to cleanup

View File

@ -135,7 +135,7 @@ private:
TEST_CASE(simplifyAtol)
TEST_CASE(simplifyHexInString)
TEST_CASE(simplifyTypedef)
TEST_CASE(simplifyTypedef1)
TEST_CASE(simplifyTypedef2)
TEST_CASE(simplifyTypedef3)
TEST_CASE(simplifyTypedef4)
@ -2158,7 +2158,7 @@ private:
ASSERT_EQUALS("\"a\"", tok("\"\\177\""));
}
void simplifyTypedef()
void simplifyTypedef1()
{
const char code[] = "class A\n"
"{\n"
@ -2652,7 +2652,7 @@ private:
"void ( * pf ) ( ) ; "
"void * ( * pfv ) ( void * ) ;";
ASSERT_EQUALS(expected, tok(code, false));
ASSERT_EQUALS(tok(expected), tok(code));
}
void simplifyTypedef22()

View File

@ -176,6 +176,8 @@ private:
TEST_CASE(simplifyString);
TEST_CASE(simplifyConst);
TEST_CASE(switchCase);
TEST_CASE(functionpointer);
}
@ -2808,6 +2810,26 @@ private:
ASSERT_EQUALS("void foo ( int i ) { switch ( i ) { case -1 : break ; } }",
tokenizeAndStringify("void foo (int i) { switch(i) { case -1: break; } }"));
}
std::string simplifyFunctionPointers(const char code[])
{
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyFunctionPointers();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << (tok->isName() ? " " : "") << tok->str();
return ostr.str();
}
void functionpointer()
{
ASSERT_EQUALS(" void* f;", simplifyFunctionPointers("void (*f)();"));
ASSERT_EQUALS(" void** f;", simplifyFunctionPointers("void *(*f)();"));
ASSERT_EQUALS(" unsigned int* f;", simplifyFunctionPointers("unsigned int (*f)();"));
ASSERT_EQUALS(" unsigned int** f;", simplifyFunctionPointers("unsigned int * (*f)();"));
}
};
REGISTER_TEST(TestTokenizer)