Tokenizer: replace ') const| {' pattern to detect function start

This commit is contained in:
Robert Reif 2014-04-26 18:30:09 +02:00 committed by Daniel Marjamäki
parent b5c580a59e
commit cd6332d960
2 changed files with 45 additions and 2 deletions

View File

@ -10025,6 +10025,22 @@ void Tokenizer::deleteSymbolDatabase()
_symbolDatabase = 0;
}
static bool operatorEnd(const Token * tok)
{
if (Token::Match(tok, ") const|volatile| noexcept| [=;{),]"))
return true;
if (Token::Match(tok, ") const|volatile| noexcept|throw (")) {
int offset = 2;
if (tok->strAt(1) == "const" || tok->strAt(1) == "volatile")
++offset;
if (Token::Match(tok->linkAt(offset), ") [=;{),]"))
return true;
}
return false;
}
void Tokenizer::simplifyOperatorName()
{
if (isC())
@ -10060,7 +10076,7 @@ void Tokenizer::simplifyOperatorName()
}
if (Token::Match(par, "( *| )")) {
// break out and simplify..
if (Token::Match(par, "( ) const| [=;{),]"))
if (operatorEnd(par->next()))
break;
while (par->str() != ")") {
@ -10073,7 +10089,7 @@ void Tokenizer::simplifyOperatorName()
}
}
if (par && Token::Match(par->link(), ") const| [=;{),]")) {
if (par && operatorEnd(par->link())) {
tok->str("operator" + op);
Token::eraseTokens(tok, par);
}

View File

@ -504,6 +504,7 @@ private:
TEST_CASE(simplifyOperatorName5);
TEST_CASE(simplifyOperatorName6); // ticket #3194
TEST_CASE(simplifyOperatorName7); // ticket #4619
TEST_CASE(simplifyOperatorName8); // ticket #5706
TEST_CASE(simplifyNull);
@ -8128,6 +8129,32 @@ private:
ASSERT_EQUALS(result1, tokenizeAndStringify(code1,false));
}
void simplifyOperatorName8() { // ticket #5706
const char code1[] = "value_type * operator += (int) noexcept ;";
const char result1[] = "value_type * operator+= ( int ) noexcept ;";
ASSERT_EQUALS(result1, tokenizeAndStringify(code1,false));
const char code2[] = "value_type * operator += (int) noexcept ( true ) ;";
const char result2[] = "value_type * operator+= ( int ) noexcept ( true ) ;";
ASSERT_EQUALS(result2, tokenizeAndStringify(code2,false));
const char code3[] = "value_type * operator += (int) throw ( ) ;";
const char result3[] = "value_type * operator+= ( int ) throw ( ) ;";
ASSERT_EQUALS(result3, tokenizeAndStringify(code3,false));
const char code4[] = "value_type * operator += (int) const noexcept ;";
const char result4[] = "value_type * operator+= ( int ) const noexcept ;";
ASSERT_EQUALS(result4, tokenizeAndStringify(code4,false));
const char code5[] = "value_type * operator += (int) const noexcept ( true ) ;";
const char result5[] = "value_type * operator+= ( int ) const noexcept ( true ) ;";
ASSERT_EQUALS(result5, tokenizeAndStringify(code5,false));
const char code6[] = "value_type * operator += (int) const throw ( ) ;";
const char result6[] = "value_type * operator+= ( int ) const throw ( ) ;";
ASSERT_EQUALS(result6, tokenizeAndStringify(code6,false));
}
void simplifyNull() {
ASSERT_EQUALS("if ( ! p )", tokenizeAndStringify("if (p==NULL)"));
ASSERT_EQUALS("f ( NULL ) ;", tokenizeAndStringify("f(NULL);"));