From 98ae6602604a2e46b0f10b15947c7984731e76b5 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Mon, 14 Jun 2010 15:46:57 +0200 Subject: [PATCH] Fixed #1792 (false positive: Variable 'test' is assigned a value that is never used) --- lib/checkother.cpp | 3 +++ lib/token.cpp | 1 + lib/token.h | 9 +++++++++ lib/tokenize.cpp | 11 +++++++++++ test/testunusedvar.cpp | 16 ++++++++++++++++ 5 files changed, 40 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d58bedeeb..87c8badd6 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1378,6 +1378,9 @@ void CheckOther::functionVariableUsage() const Variables::VariableUsage &usage = it->second; const std::string &varname = usage._name->str(); + if (usage._name->isUnused()) + continue; + if (usage.unused() && !usage._modified) unusedVariableError(usage._name, varname); diff --git a/lib/token.cpp b/lib/token.cpp index b5de09c72..cffbc99bd 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -35,6 +35,7 @@ Token::Token(Token **t) : _isUnsigned(false), _isSigned(false), _isLong(false), + _isUnused(false), _varId(0), _next(0), _previous(0), diff --git a/lib/token.h b/lib/token.h index ac7ba93c8..89993ec58 100644 --- a/lib/token.h +++ b/lib/token.h @@ -172,6 +172,14 @@ public: { _isLong = size; } + bool isUnused() const + { + return _isUnused; + } + void isUnused(bool used) + { + _isUnused = used; + } bool isStandardType() const; bool isIntegerType() const; @@ -368,6 +376,7 @@ private: bool _isUnsigned; bool _isSigned; bool _isLong; + bool _isUnused; unsigned int _varId; Token *_next; Token *_previous; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index b334c5b9d..20e78d688 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -172,6 +172,7 @@ void Tokenizer::addtoken(const Token * tok, const unsigned int lineno, const uns _tokensBack->isUnsigned(tok->isUnsigned()); _tokensBack->isSigned(tok->isSigned()); _tokensBack->isLong(tok->isLong()); + _tokensBack->isUnused(tok->isUnused()); } //--------------------------------------------------------------------------- @@ -7678,6 +7679,16 @@ void Tokenizer::simplifyAttribute() { if (Token::simpleMatch(tok, "__attribute__ (") && tok->next()->link() && tok->next()->link()->next()) { + if (Token::simpleMatch(tok->tokAt(2), "( unused )")) + { + // check if after variable name + if (Token::Match(tok->next()->link()->next(), ";|=")) + { + if (Token::Match(tok->previous(), "%type%")) + tok->previous()->isUnused(true); + } + } + Token::eraseTokens(tok, tok->next()->link()->next()); tok->deleteThis(); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index d79581f28..007c10d31 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -105,6 +105,7 @@ private: TEST_CASE(localvarShift); // 1 >> var TEST_CASE(localvarCast); TEST_CASE(localvarClass); + TEST_CASE(localvarUnused); } void structmember1() @@ -1893,6 +1894,21 @@ private: "}\n"); ASSERT_EQUALS(std::string(""), errout.str()); } + + void localvarUnused() + { + functionVariableUsage("int foo()\n" + "{\n" + " bool test __attribute__((unused));\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + + functionVariableUsage("int foo()\n" + "{\n" + " bool test __attribute__((unused)) = true;\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + } }; REGISTER_TEST(TestUnusedVar)