diff --git a/verify/Makefile b/verify/Makefile new file mode 100644 index 000000000..a853e5f6a --- /dev/null +++ b/verify/Makefile @@ -0,0 +1,15 @@ +FILES = ../lib/errorlogger.cpp \ + ../lib/filelister.cpp \ + ../lib/filelister_win32.cpp \ + ../lib/filelister_unix.cpp \ + ../lib/mathlib.cpp \ + ../lib/path.cpp \ + ../lib/preprocessor.cpp \ + ../lib/settings.cpp \ + ../lib/token.cpp \ + ../lib/tokenize.cpp + +HDRS = $(FILES:%.cpp=%.h) + +cppcheck-verify: main.cpp $(FILES) $(HDRS) + g++ -Wall -I../lib -o cppcheck-verify main.cpp $(FILES) diff --git a/verify/main.cpp b/verify/main.cpp new file mode 100644 index 000000000..478df8225 --- /dev/null +++ b/verify/main.cpp @@ -0,0 +1,82 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team. + * + * 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 . + */ + +#include "preprocessor.h" +#include "tokenize.h" + +#include +#include +#include +#include + +// Check that array indexes are within bounds +static void arrayIndex(const Tokenizer &tokenizer, std::ostream &errout) +{ + // Check that all array indexes are within bounds.. + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + { + if (tok->str() == "[") + { + // TODO: try to determine if the array index is within bounds + ; + + // Write error message: + errout << tokenizer.fileLine(tok) + << " failed to determine if given array index is within bounds" + << std::endl; + } + } +} + + +int main(int argc, const char *argv[]) +{ + if (argc != 2) + { + std::cerr << "syntax: cppcheck-verify file.cpp" << std::endl; + return 0; + } + + const std::string fileName = argv[1]; + + Tokenizer tokenizer; + + { + // Preprocess the file.. + Preprocessor preprocessor; + std::ifstream fin(fileName.c_str()); + std::string filedata; + std::list configurations; + std::list includePaths; + preprocessor.preprocess(fin, + filedata, + configurations, + fileName, + includePaths); + filedata = Preprocessor::getcode(filedata, "", fileName, NULL); + + // Tokenize the preprocessed code.. + std::istringstream istr(filedata); + tokenizer.tokenize(istr, fileName.c_str(), ""); + } + + // Check the tokens.. + arrayIndex(tokenizer, std::cerr); + + return 0; +} diff --git a/verify/readme.txt b/verify/readme.txt new file mode 100644 index 000000000..fc9e726ae --- /dev/null +++ b/verify/readme.txt @@ -0,0 +1,11 @@ + +cppcheck-verify +=============== + +Experimental subproject for Cppcheck. + +The goal is no false negatives. + +Just use 'make' to build. + +