diff --git a/Makefile b/Makefile index b9ab3a721..0784a5cc9 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/testsimplifytokens.cpp b/testsimplifytokens.cpp new file mode 100644 index 000000000..20e928395 --- /dev/null +++ b/testsimplifytokens.cpp @@ -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 + +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 ) diff --git a/tokenize.cpp b/tokenize.cpp index 415b5cadf..e3ee603ae 100644 --- a/tokenize.cpp +++ b/tokenize.cpp @@ -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); + } } } //---------------------------------------------------------------------------