From 8f76aea6ed73d9313f670837d373f05e4a6847fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 21 May 2010 21:06:11 +0200 Subject: [PATCH] 2 pass checking: broke out Cppcheck::analyseFile. To be used for tests --- lib/cppcheck.cpp | 60 ++++++++++++++++++++++++++---------------------- lib/cppcheck.h | 6 +++++ 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 19bccbc97..1398d3cfe 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -639,34 +639,8 @@ unsigned int CppCheck::check() reportOut("Analysing " + fname + ".."); - // Preprocess file.. - Preprocessor preprocessor(&_settings, this); - std::list configurations; - std::string filedata = ""; - std::ifstream fin(fname.c_str()); - preprocessor.preprocess(fin, filedata, configurations, fname, _settings._includePaths); - const std::string code = Preprocessor::getcode(filedata, "", fname, &_errorLogger); - - // Tokenize.. - Tokenizer tokenizer(&_settings, this); - std::istringstream istr(code); - tokenizer.tokenize(istr, fname.c_str(), ""); - tokenizer.simplifyTokenList(); - - // Analyse the tokens.. - std::set data; - for (std::list::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) - { - (*it)->analyse(tokenizer.tokens(), data); - } - - // Save analysis results.. - // TODO: This loop should be protected by a mutex or something like that - // The saveAnalysisData must _not_ be called from many threads at the same time. - for (std::list::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) - { - (*it)->saveAnalysisData(data); - } + std::ifstream f(fname.c_str()); + analyseFile(f, fname); } } @@ -761,6 +735,36 @@ unsigned int CppCheck::check() return exitcode; } +void CppCheck::analyseFile(std::istream &fin, const std::string &filename) +{ + // Preprocess file.. + Preprocessor preprocessor(&_settings, this); + std::list configurations; + std::string filedata = ""; + preprocessor.preprocess(fin, filedata, configurations, filename, _settings._includePaths); + const std::string code = Preprocessor::getcode(filedata, "", filename, &_errorLogger); + + // Tokenize.. + Tokenizer tokenizer(&_settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, filename.c_str(), ""); + tokenizer.simplifyTokenList(); + + // Analyse the tokens.. + std::set data; + for (std::list::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) + { + (*it)->analyse(tokenizer.tokens(), data); + } + + // Save analysis results.. + // TODO: This loop should be protected by a mutex or something like that + // The saveAnalysisData must _not_ be called from many threads at the same time. + for (std::list::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) + { + (*it)->saveAnalysisData(data); + } +} //--------------------------------------------------------------------------- // CppCheck - A function that checks a specified file diff --git a/lib/cppcheck.h b/lib/cppcheck.h index e6416d407..e3b8959c2 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -130,7 +130,13 @@ public: */ void getErrorMessages(); + /** + * @brief Analyse file - It's public so unit tests can be written + */ + void analyseFile(std::istream &f, const std::string &filename); + private: + /** @brief Check file */ void checkFile(const std::string &code, const char FileName[]); /**