More robust template detection in clarifyCondition check based on Token::link. (#3818)

Create links between < and > only on non-C code.
AStyle fix
This commit is contained in:
PKEuS 2012-08-02 04:03:01 -07:00
parent 3b5dabdb14
commit 452f95cea0
3 changed files with 19 additions and 4 deletions

View File

@ -156,7 +156,7 @@ void CheckOther::clarifyCondition()
tok2 = tok2->link(); tok2 = tok2->link();
else if (tok2->type() == Token::eComparisonOp) { else if (tok2->type() == Token::eComparisonOp) {
// This might be a template // This might be a template
if (!isC && Token::Match(tok2->previous(), "%var% <")) if (!isC && tok2->link())
break; break;
clarifyConditionError(tok, tok->strAt(2) == "=", false); clarifyConditionError(tok, tok->strAt(2) == "=", false);
@ -170,6 +170,9 @@ void CheckOther::clarifyCondition()
// using boolean result in bitwise operation ! x [&|^] // using boolean result in bitwise operation ! x [&|^]
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "!|<|<=|==|!=|>|>=")) { if (Token::Match(tok, "!|<|<=|==|!=|>|>=")) {
if (tok->link()) // don't write false positives when templates are used
continue;
const Token *tok2 = tok->next(); const Token *tok2 = tok->next();
// Todo: There are false positives if '(' if encountered. It // Todo: There are false positives if '(' if encountered. It
@ -186,8 +189,7 @@ void CheckOther::clarifyCondition()
if (Token::Match(tok2, "[&|^]")) { if (Token::Match(tok2, "[&|^]")) {
// don't write false positives when templates are used // don't write false positives when templates are used
if (Token::Match(tok, "<|>") && (Token::Match(tok2, "& ,|>") || if (Token::Match(tok2, "&|* ,|>") || Token::simpleMatch(tok2->previous(), "const &"))
Token::simpleMatch(tok2->previous(), "const &")))
continue; continue;
// #3609 - CWinTraits<WS_CHILD|WS_VISIBLE>::.. // #3609 - CWinTraits<WS_CHILD|WS_VISIBLE>::..

View File

@ -2935,6 +2935,9 @@ bool Tokenizer::createLinks()
void Tokenizer::createLinks2() void Tokenizer::createLinks2()
{ {
if (isC())
return;
std::stack<const Token*> type; std::stack<const Token*> type;
std::stack<Token*> links; std::stack<Token*> links;
for (Token *token = list.front(); token; token = token->next()) { for (Token *token = list.front(); token; token = token->next()) {
@ -7836,7 +7839,7 @@ void Tokenizer::simplifyComma()
void Tokenizer::removeExceptionSpecifications() void Tokenizer::removeExceptionSpecifications()
{ {
for(Token* tok = list.front(); tok; tok = tok->next()) { for (Token* tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, ") const| throw (")) { if (Token::Match(tok, ") const| throw (")) {
if (tok->next()->str() == "const") { if (tok->next()->str() == "const") {
Token::eraseTokens(tok->next(), tok->linkAt(3)); Token::eraseTokens(tok->next(), tok->linkAt(3));

View File

@ -124,6 +124,7 @@ private:
TEST_CASE(clarifyCondition3); // if (! a & b) TEST_CASE(clarifyCondition3); // if (! a & b)
TEST_CASE(clarifyCondition4); // ticket #3110 TEST_CASE(clarifyCondition4); // ticket #3110
TEST_CASE(clarifyCondition5); // #3609 CWinTraits<WS_CHILD|WS_VISIBLE>.. TEST_CASE(clarifyCondition5); // #3609 CWinTraits<WS_CHILD|WS_VISIBLE>..
TEST_CASE(clarifyCondition6); // #3818
TEST_CASE(bitwiseOnBoolean); // if (bool & bool) TEST_CASE(bitwiseOnBoolean); // if (bool & bool)
TEST_CASE(comparisonOfBoolExpressionWithInt1); TEST_CASE(comparisonOfBoolExpressionWithInt1);
@ -3657,6 +3658,15 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void clarifyCondition6() {
check("template<class Y>\n"
"SharedPtr& operator=( SharedPtr<Y> const & r ) {\n"
" px = r.px;\n"
" return *this;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void bitwiseOnBoolean() { // 3062 void bitwiseOnBoolean() { // 3062
check("void f(_Bool a, _Bool b) {\n" check("void f(_Bool a, _Bool b) {\n"
" if(a & b) {}\n" " if(a & b) {}\n"