#6690 override and final qualifiers plus less than operator results in a syntax error

Tokenizer::simplifyKeyword() is now able to handle all combinations out
of const|final|override
This commit is contained in:
amai2012 2015-05-17 13:02:13 +02:00
parent 3c97736d44
commit 20842fb1fc
2 changed files with 59 additions and 19 deletions

View File

@ -3157,7 +3157,7 @@ bool Tokenizer::simplifySizeof()
for (unsigned int i = 0; i < derefs; i++) for (unsigned int i = 0; i < derefs; i++)
tok2 = tok2->linkAt(1); // Skip all dimensions that are derefenced before the sizeof call tok2 = tok2->linkAt(1); // Skip all dimensions that are derefenced before the sizeof call
while (Token::Match(tok2, "] [ %num% ]")) { while (Token::Match(tok2, "] [ %num% ]")) {
sz = sz * MathLib::toLongNumber(tok2->strAt(2)); sz *= static_cast<size_t>(MathLib::toULongNumber(tok2->strAt(2)));
tok2 = tok2->linkAt(1); tok2 = tok2->linkAt(1);
} }
if (Token::simpleMatch(tok2, "] [")) if (Token::simpleMatch(tok2, "] ["))
@ -3169,7 +3169,7 @@ bool Tokenizer::simplifySizeof()
continue; continue;
const Token *tok2 = nametok->next(); const Token *tok2 = nametok->next();
while (Token::Match(tok2, "[ %num% ]")) { while (Token::Match(tok2, "[ %num% ]")) {
sz *= static_cast<unsigned long>(MathLib::toLongNumber(tok2->strAt(1))); sz *= static_cast<size_t>(MathLib::toLongNumber(tok2->strAt(1)));
tok2 = tok2->link()->next(); tok2 = tok2->link()->next();
} }
if (!tok2 || tok2->str() != ")") if (!tok2 || tok2->str() != ")")
@ -9315,17 +9315,26 @@ void Tokenizer::simplifyKeyword()
} }
// final: // final:
// void f() final; <- function is final // 1) struct name final { }; <- struct is final
// struct name final { }; <- struct is final if (Token::Match(tok, "%type% final [:{]")) {
if (Token::Match(tok, ") final [{;]") || Token::Match(tok, "%type% final [:{]"))
tok->deleteNext(); tok->deleteNext();
continue;
// override }
// final:
// 2) void f() final; <- function is final
// override:
// void f() override; // void f() override;
else if (Token::Match(tok, ") override [{;]")) //if (Token::Match(tok, ") override [{;]"))
tok->deleteNext(); if (Token::Match(tok, ") const|override|final")) {
else if (Token::Match(tok, ") const override [{;]")) Token* specifier = tok->tokAt(2);
tok->next()->deleteNext(); while(specifier && Token::Match(specifier, "const|override|final"))
specifier=specifier->next();
if (specifier && Token::Match(specifier, "[{;]")) {
specifier=tok->next();
while (specifier->str()=="override" || specifier->str()=="final")
specifier->deleteThis();
}
}
} }
} }
} }

View File

@ -4443,18 +4443,49 @@ private:
void removeKeywords() { void removeKeywords() {
const char code[] = "if (__builtin_expect(!!(x), 1));"; const char code[] = "if (__builtin_expect(!!(x), 1));";
ASSERT_EQUALS("if ( ! ! x ) { ; }", tokenizeAndStringify(code, true));
const std::string actual(tokenizeAndStringify(code, true));
ASSERT_EQUALS("if ( ! ! x ) { ; }", actual);
} }
void simplifyKeyword() { void simplifyKeyword() {
{
const char code[] = "void f (int a [ static 5] );"; const char code[] = "void f (int a [ static 5] );";
ASSERT_EQUALS("void f ( int a [ 5 ] ) ;", tokenizeAndStringify(code));
}
{
const char in1[] = "class Base {\n"
" virtual int test() = 0;\n"
"};\n"
"class Derived : public Base {\n"
" virtual int test() override final {\n"
" for( int Index ( 0 ); Index < 16; ++ Index) { int stub = 0; }\n"
" }\n"
"};";
const char out1[] = "class Base {\n"
"virtual int test ( ) = 0 ;\n"
"} ;\n"
"class Derived : public Base {\n"
"virtual int test ( ) {\n"
"for ( int Index ( 0 ) ; Index < 16 ; ++ Index ) { int stub ; stub = 0 ; }\n"
"}\n"
"} ;";
ASSERT_EQUALS(out1, tokenizeAndStringify(in1));
const char in2[] = "class Derived{\n"
" virtual int test() final override;"
"};";
const char out2[] = "class Derived {\n"
"virtual int test ( ) ; } ;";
ASSERT_EQUALS(out2, tokenizeAndStringify(in2));
const char in3[] = "class Derived{\n"
" virtual int test() final override const;"
"};";
const char out3[] = "class Derived {\n"
"virtual int test ( ) const ; } ;";
ASSERT_EQUALS(out3, tokenizeAndStringify(in3));
const std::string actual(tokenizeAndStringify(code, true)); const char in4 [] = "struct B final : A { void foo(); };";
const char out4 [] = "struct B : A { void foo ( ) ; } ;";
ASSERT_EQUALS("void f ( int a [ 5 ] ) ;", actual); ASSERT_EQUALS(out4, tokenizeAndStringify(in4));
}
} }
/** /**