Fixed #2615 (Segmentation fault in cppcheck 1.47)

This commit is contained in:
Daniel Marjamäki 2011-02-28 20:29:34 +01:00
parent 81318bb4a1
commit 893b84a87c
2 changed files with 91 additions and 38 deletions

View File

@ -9349,26 +9349,56 @@ const SymbolDatabase *Tokenizer::getSymbolDatabase() const
void Tokenizer::simplifyOperatorName()
{
for (const Token *tok = _tokens; tok; tok = tok->next())
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, ") const| {|;|="))
if (tok->str() == "operator")
{
Token *tok1 = tok->link();
Token *tok2 = tok1;
tok1 = tok1->previous();
while (tok1 && !Token::Match(tok1, "operator|{|}|;|public:|private|protected:"))
// operator op
std::string op;
Token *par = tok->next();
bool done = false;
while (!done && par)
{
tok1 = tok1->previous();
done = true;
if (par && par->isName())
{
op += par->str();
par = par->next();
done = false;
}
if (Token::Match(par, "[<>+-*&/=.]") || Token::Match(par, "==|!=|<=|>="))
{
op += par->str();
par = par->next();
done = false;
}
if (Token::simpleMatch(par, "[ ]"))
{
op += "[]";
par = par->next()->next();
done = false;
}
if (Token::Match(par, "( *| )"))
{
// break out and simplify..
if (Token::Match(par, "( ) const| [=;{),]"))
break;
while (par->str() != ")")
{
op += par->str();
par = par->next();
}
op += ")";
par = par->next();
done = false;
}
}
if (tok1 && tok1->str() == "operator")
if (par && Token::Match(par->link(), ") const| [=;{),]"))
{
while (tok1->next() != tok2)
{
tok1->str(tok1->str() + tok1->next()->str());
tok1->deleteNext();
}
tok->str("operator" + op);
Token::eraseTokens(tok,par);
}
}
}

View File

@ -305,7 +305,10 @@ private:
// Tokenize JAVA
TEST_CASE(java);
TEST_CASE(simplifyOperatorName);
TEST_CASE(simplifyOperatorName1);
TEST_CASE(simplifyOperatorName2);
TEST_CASE(simplifyOperatorName3);
TEST_CASE(simplifyOperatorName4);
// Some simple cleanups of unhandled macros in the global scope
TEST_CASE(removeMacrosInGlobalScope);
@ -5312,36 +5315,56 @@ private:
ASSERT_EQUALS("void f ( ) { }", javatest("void f() throws Exception { }"));
}
void simplifyOperatorName()
void simplifyOperatorName1()
{
// make sure C code doesn't get changed
const char code1[] = "void operator () {}"
"int main()"
"{"
" operator();"
"}";
const char code[] = "void operator () {}"
"int main()"
"{"
" operator();"
"}";
const char result1 [] = "void operator ( ) { } "
"int main ( ) "
"{ "
"operator ( ) ; "
"}";
const char result [] = "void operator ( ) { } "
"int main ( ) "
"{ "
"operator ( ) ; "
"}";
ASSERT_EQUALS(result1, tokenizeAndStringify(code1,false));
ASSERT_EQUALS(result, tokenizeAndStringify(code,false));
}
const char code2[] = "class Fred"
"{"
" Fred(const Fred & f) { operator = (f); }"
" operator = ();"
"}";
void simplifyOperatorName2()
{
const char code[] = "class Fred"
"{"
" Fred(const Fred & f) { operator = (f); }"
" operator = ();"
"}";
const char result2 [] = "class Fred "
"{ "
"Fred ( const Fred & f ) { operator= ( f ) ; } "
"operator= ( ) ; "
"}";
const char result [] = "class Fred "
"{ "
"Fred ( const Fred & f ) { operator= ( f ) ; } "
"operator= ( ) ; "
"}";
ASSERT_EQUALS(result2, tokenizeAndStringify(code2,false));
ASSERT_EQUALS(result, tokenizeAndStringify(code,false));
}
void simplifyOperatorName3()
{
// #2615
const char code[] = "void f() {"
"static_cast<ScToken*>(xResult.operator->())->GetMatrix();"
"}";
const char result[] = "void f ( ) { static_cast < ScToken * > ( xResult . operator. ( ) ) . GetMatrix ( ) ; }";
ASSERT_EQUALS(result, tokenizeAndStringify(code,false));
}
void simplifyOperatorName4()
{
const char code[] = "void operator==() { }";
const char result[] = "void operator== ( ) { }";
ASSERT_EQUALS(result, tokenizeAndStringify(code,false));
}
void removeMacrosInGlobalScope()