From ee7ad272d668a50b7e4d8d510b60819517f034e7 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 16 Jun 2010 18:04:31 +0200 Subject: [PATCH] variable usage: fix false positives when __attribute__ is used. Ticket: #1792 --- lib/token.cpp | 1 + lib/tokenize.cpp | 4 ++++ test/testunusedvar.cpp | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/lib/token.cpp b/lib/token.cpp index cffbc99bd..d8b39f83c 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -104,6 +104,7 @@ void Token::deleteThis() _isName = _next->_isName; _isNumber = _next->_isNumber; _isBoolean = _next->_isBoolean; + _isUnused = _next->_isUnused; _varId = _next->_varId; _fileIndex = _next->_fileIndex; _linenr = _next->_linenr; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 19ac61473..e873082ea 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7688,6 +7688,10 @@ void Tokenizer::simplifyAttribute() if (Token::Match(tok->previous(), "%type%")) tok->previous()->isUnused(true); } + + // check if before variable name + else if (Token::Match(tok->next()->link()->next(), "%type%")) + tok->next()->link()->next()->isUnused(true); } Token::eraseTokens(tok, tok->next()->link()->next()); diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 0d76fead7..3d269e540 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -1935,6 +1935,18 @@ private: " bool test __attribute__((unused)) = true;\n" "}\n"); ASSERT_EQUALS(std::string(""), errout.str()); + + functionVariableUsage("int foo()\n" + "{\n" + " bool __attribute__((unused)) test;\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + + functionVariableUsage("int foo()\n" + "{\n" + " bool __attribute__((unused)) test = true;\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); } };