Simplify function pointers
This commit is contained in:
parent
e163f878ab
commit
96359b14ff
|
@ -579,6 +579,7 @@ void CheckClass::privateFunctions()
|
||||||
tok = tok->tokAt(2)->link();
|
tok = tok->tokAt(2)->link();
|
||||||
|
|
||||||
else if (Token::Match(tok, "%var% (") &&
|
else if (Token::Match(tok, "%var% (") &&
|
||||||
|
!Token::simpleMatch(tok->next()->link(), ") (") &&
|
||||||
!Token::Match(tok, classname.c_str()))
|
!Token::Match(tok, classname.c_str()))
|
||||||
{
|
{
|
||||||
FuncList.push_back(tok);
|
FuncList.push_back(tok);
|
||||||
|
|
|
@ -941,6 +941,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[])
|
||||||
// remove exception specifications..
|
// remove exception specifications..
|
||||||
removeExceptionSpecifications(_tokens);
|
removeExceptionSpecifications(_tokens);
|
||||||
|
|
||||||
|
// simplify function pointers
|
||||||
|
simplifyFunctionPointers();
|
||||||
|
|
||||||
setVarId();
|
setVarId();
|
||||||
if (!validate())
|
if (!validate())
|
||||||
return false;
|
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 Tokenizer::simplifyFunctionReturn()
|
||||||
{
|
{
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
|
@ -336,6 +336,9 @@ private:
|
||||||
*/
|
*/
|
||||||
void simplifyStd();
|
void simplifyStd();
|
||||||
|
|
||||||
|
/** Simplify function pointers */
|
||||||
|
void simplifyFunctionPointers();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove exception specifications. This function calls itself recursively.
|
* Remove exception specifications. This function calls itself recursively.
|
||||||
* @param tok First token in scope to cleanup
|
* @param tok First token in scope to cleanup
|
||||||
|
|
|
@ -135,7 +135,7 @@ private:
|
||||||
|
|
||||||
TEST_CASE(simplifyAtol)
|
TEST_CASE(simplifyAtol)
|
||||||
TEST_CASE(simplifyHexInString)
|
TEST_CASE(simplifyHexInString)
|
||||||
TEST_CASE(simplifyTypedef)
|
TEST_CASE(simplifyTypedef1)
|
||||||
TEST_CASE(simplifyTypedef2)
|
TEST_CASE(simplifyTypedef2)
|
||||||
TEST_CASE(simplifyTypedef3)
|
TEST_CASE(simplifyTypedef3)
|
||||||
TEST_CASE(simplifyTypedef4)
|
TEST_CASE(simplifyTypedef4)
|
||||||
|
@ -2158,7 +2158,7 @@ private:
|
||||||
ASSERT_EQUALS("\"a\"", tok("\"\\177\""));
|
ASSERT_EQUALS("\"a\"", tok("\"\\177\""));
|
||||||
}
|
}
|
||||||
|
|
||||||
void simplifyTypedef()
|
void simplifyTypedef1()
|
||||||
{
|
{
|
||||||
const char code[] = "class A\n"
|
const char code[] = "class A\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -2652,7 +2652,7 @@ private:
|
||||||
"void ( * pf ) ( ) ; "
|
"void ( * pf ) ( ) ; "
|
||||||
"void * ( * pfv ) ( void * ) ;";
|
"void * ( * pfv ) ( void * ) ;";
|
||||||
|
|
||||||
ASSERT_EQUALS(expected, tok(code, false));
|
ASSERT_EQUALS(tok(expected), tok(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
void simplifyTypedef22()
|
void simplifyTypedef22()
|
||||||
|
|
|
@ -176,6 +176,8 @@ private:
|
||||||
TEST_CASE(simplifyString);
|
TEST_CASE(simplifyString);
|
||||||
TEST_CASE(simplifyConst);
|
TEST_CASE(simplifyConst);
|
||||||
TEST_CASE(switchCase);
|
TEST_CASE(switchCase);
|
||||||
|
|
||||||
|
TEST_CASE(functionpointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -2808,6 +2810,26 @@ private:
|
||||||
ASSERT_EQUALS("void foo ( int i ) { switch ( i ) { case -1 : break ; } }",
|
ASSERT_EQUALS("void foo ( int i ) { switch ( i ) { case -1 : break ; } }",
|
||||||
tokenizeAndStringify("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)
|
REGISTER_TEST(TestTokenizer)
|
||||||
|
|
Loading…
Reference in New Issue