Fixed #7064 (Tokenizer: Wrong simplification of 'a.public:')

This commit is contained in:
Daniel Marjamäki 2015-10-19 10:01:57 +02:00
parent 06c20252fa
commit b856ac51dd
2 changed files with 37 additions and 11 deletions

View File

@ -1862,6 +1862,8 @@ void Tokenizer::fillTypeSizes()
void Tokenizer::combineOperators()
{
const bool cpp = isCPP();
// Combine tokens..
for (Token *tok = list.front();
tok && tok->next();
@ -1902,11 +1904,33 @@ void Tokenizer::combineOperators()
tok->str("<<=");
tok->deleteNext();
}
} else if ((c1 == 'p' || c1 == '_') && tok->next()->str() == ":" && tok->strAt(2) != ":") {
if (Token::Match(tok, "private|protected|public|__published")) {
} else if (cpp && (c1 == 'p' || c1 == '_') &&
Token::Match(tok, "private|protected|public|__published : !!:")) {
bool simplify = false;
unsigned int par = 0U;
for (const Token *prev = tok->tokAt(-1); prev; prev = prev->previous()) {
if (prev->str() == ")") {
++par;
} else if (prev->str() == "(") {
if (par == 0U)
break;
--par;
}
if (par != 0U || prev->str() == "(")
continue;
if (Token::Match(prev, "[;{}]")) {
simplify = true;
break;
}
if (prev->isName() && prev->isUpperCaseName())
continue;
if (prev->isName() && prev->str()[prev->str().size() - 1U] == ':')
simplify = true;
break;
}
if (simplify) {
tok->str(tok->str() + ":");
tok->deleteNext();
continue;
}
}
}

View File

@ -48,7 +48,6 @@ private:
TEST_CASE(tokenize7);
TEST_CASE(tokenize8);
TEST_CASE(tokenize9);
TEST_CASE(tokenize10);
TEST_CASE(tokenize11);
TEST_CASE(tokenize13); // bailout if the code contains "@" - that is not handled well.
TEST_CASE(tokenize14); // tokenize "0X10" => 16
@ -74,6 +73,8 @@ private:
TEST_CASE(foreach); // #3690
TEST_CASE(combineOperators);
TEST_CASE(concatenateNegativeNumber);
TEST_CASE(longtok);
@ -661,13 +662,6 @@ private:
ASSERT_EQUALS("", errout.str());
}
void tokenize10() {
ASSERT_EQUALS("private:", tokenizeAndStringify("private:", false));
ASSERT_EQUALS("protected:", tokenizeAndStringify("protected:", false));
ASSERT_EQUALS("public:", tokenizeAndStringify("public:", false));
ASSERT_EQUALS("__published:", tokenizeAndStringify("__published:", false));
}
void tokenize11() {
ASSERT_EQUALS("X * sizeof ( Y ( ) ) ;", tokenizeAndStringify("X * sizeof(Y());", false));
}
@ -865,6 +859,14 @@ private:
ASSERT_EQUALS("void f ( ) { asm ( \"char c in MyString\" ) { Console :: Write ( c ) ; } }" ,tokenizeAndStringify(code));
}
void combineOperators() {
ASSERT_EQUALS("; private:", tokenizeAndStringify(";private:", false));
ASSERT_EQUALS("; protected:", tokenizeAndStringify(";protected:", false));
ASSERT_EQUALS("; public:", tokenizeAndStringify(";public:", false));
ASSERT_EQUALS("; __published:", tokenizeAndStringify(";__published:", false));
ASSERT_EQUALS("a . public :", tokenizeAndStringify("a.public:", false));
}
void concatenateNegativeNumber() {
ASSERT_EQUALS("i = -12", tokenizeAndStringify("i = -12"));
ASSERT_EQUALS("1 - 2", tokenizeAndStringify("1-2"));