Simplify Tokens: Replace NULL with 0 and '(char *)0' with '0'

This commit is contained in:
Daniel Marjamäki 2008-11-15 16:27:09 +00:00
parent 8c9c6529b8
commit 1801f25bce
3 changed files with 83 additions and 1 deletions

View File

@ -1,6 +1,6 @@
SRCS=CheckBufferOverrun.cpp CheckClass.cpp CheckHeaders.cpp CheckMemoryLeak.cpp CheckOther.cpp CommonCheck.cpp FileLister.cpp preprocessor.cpp tokenize.cpp cppcheck.cpp settings.cpp
OBJS=$(SRCS:%.cpp=%.o)
TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testincompletestatement.o testmemleak.o testpreprocessor.o testtokenize.o testunusedprivfunc.o testunusedvar.o settings.o cppcheck.o
TESTS=testbufferoverrun.o testcharvar.o testconstructors.o testdivision.o testincompletestatement.o testmemleak.o testpreprocessor.o testsimplifytokens.o testtokenize.o testunusedprivfunc.o testunusedvar.o settings.o cppcheck.o
BIN = ${DESTDIR}/usr/bin
all: ${OBJS} main.o
@ -47,6 +47,8 @@ testrunner.o: testrunner.cpp testsuite.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testsuite.o: testsuite.cpp testsuite.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testsimplifytokens.o: testsimplifytokens.cpp testsuite.h tokenize.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testtokenize.o:testtokenize.cpp testsuite.h tokenize.h
g++ -Wall -pedantic -g -I. -o $@ -c $*.cpp
testunusedprivfunc.o: testunusedprivfunc.cpp tokenize.h CheckClass.h testsuite.h

63
testsimplifytokens.cpp Normal file
View File

@ -0,0 +1,63 @@
/*
* c++check - c/c++ syntax checking
* Copyright (C) 2007 Daniel Marjamäki
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#include "testsuite.h"
#include "tokenize.h"
#include <sstream>
class TestSimplifyTokens : public TestFixture
{
public:
TestSimplifyTokens() : TestFixture("TestSimplifyTokens")
{ }
private:
void run()
{
TEST_CASE( cast0 );
}
static std::string tok(const char code[])
{
std::istringstream istr(code);
Tokenizer tokenizer;
tokenizer.TokenizeCode( istr );
tokenizer.SimplifyTokenList();
std::string ret;
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
{
ret += std::string(tok->str) + " ";
}
return ret;
}
void cast0()
{
const char code1[] = " if ( p == (char *)0 ) ";
const char code2[] = " if ( p == 0 ) ";
ASSERT_EQUALS( tok(code1), tok(code2) );
}
};
REGISTER_TEST( TestSimplifyTokens )

View File

@ -1012,6 +1012,23 @@ void Tokenizer::SimplifyTokenList()
}
}
}
}
// Replace NULL with 0..
for ( TOKEN *tok = tokens; tok; tok = tok->next )
{
if ( Match(tok, "NULL") )
tok->setstr("0");
}
// Replace pointer casts of 0.. "(char *)0" => "0"
for ( TOKEN *tok = tokens; tok; tok = tok->next )
{
if ( Match(tok->next, "( %type% * ) 0") || Match(tok->next,"( %type% %type% * ) 0") )
{
while (!Match(tok->next,"0"))
DeleteNextToken(tok);
}
}
}
//---------------------------------------------------------------------------