CheckIncompleteStatement: Fixed false positive generated by CheckIncompleteStatement (bug 2187837)

This commit is contained in:
Daniel Marjamäki 2008-10-23 17:45:24 +00:00
parent bacc402d04
commit 30e1d5c06e
3 changed files with 73 additions and 3 deletions

View File

@ -710,14 +710,14 @@ void CheckIncompleteStatement()
if ( parlevel != 0 ) if ( parlevel != 0 )
continue; continue;
if ( Match(tok,"; %str%") && !Match(gettok(tok,2), ",") ) if ( !Match(tok,"#") && Match(tok->next,"; %str%") && !Match(gettok(tok,3), ",") )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok->next) << ": Redundant code: Found a statement that begins with string constant"; errmsg << FileLine(tok->next) << ": Redundant code: Found a statement that begins with string constant";
ReportErr(errmsg.str()); ReportErr(errmsg.str());
} }
if ( Match(tok,"; %num%") && !Match(gettok(tok,2), ",") ) if ( !Match(tok,"#") && Match(tok->next,"; %num%") && !Match(gettok(tok,3), ",") )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << FileLine(tok->next) << ": Redundant code: Found a statement that begins with numeric constant"; errmsg << FileLine(tok->next) << ": Redundant code: Found a statement that begins with numeric constant";

View File

@ -1,6 +1,6 @@
SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckOther.cpp CommonCheck.cpp tokenize.cpp SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckOther.cpp CommonCheck.cpp tokenize.cpp
OBJS=$(SRCS:%.cpp=%.o) OBJS=$(SRCS:%.cpp=%.o)
TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testmemleak.o testunusedvar.o TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testmemleak.o testother.o testunusedvar.o
%.o: %.cpp %.o: %.cpp
g++ -Wall -pedantic -g -I. -o $@ -c $^ g++ -Wall -pedantic -g -I. -o $@ -c $^

70
testother.cpp Normal file
View File

@ -0,0 +1,70 @@
// Check for unused variables..
#include "testsuite.h"
#include "tokenize.h"
#include "CheckOther.h"
#include <sstream>
extern std::ostringstream errout;
class TestOther : public TestFixture
{
public:
TestOther() : TestFixture("TestOther")
{ }
private:
void check( const char code[] )
{
// Tokenize..
tokens = tokens_back = NULL;
std::istringstream istr(code);
TokenizeCode( istr );
SimplifyTokenList();
// Clear the error buffer..
errout.str("");
// Check for unused variables..
CheckIncompleteStatement();
}
void run()
{
TEST_CASE( test1 );
TEST_CASE( test2 );
}
void test1()
{
check( "void foo()\n"
"{\n"
" const char def[] =\n"
"#ifdef ABC\n"
" \"abc\";\n"
"#else\n"
" \"not abc\";\n"
"#endif\n"
"}\n" );
ASSERT_EQUALS( std::string(""), errout.str() );
}
void test2()
{
// Todo: remove the ';' before the string
check( "void foo()\n"
"{\n"
" ;\"abc\";\n"
"}\n" );
ASSERT_EQUALS( std::string("[test.cpp:3]: Redundant code: Found a statement that begins with string constant\n"), errout.str() );
}
};
REGISTER_TEST( TestOther )