From 6a523f2730012f845e9a5056ff8fa1a9f9db35df Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Fri, 23 Jan 2009 18:27:04 +0000 Subject: [PATCH] Fixed Ticket #52, Cppcheck hangs when checking VLC's source code --- src/cppcheck.cpp | 1 + src/preprocessor.cpp | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index dd222a3c7..9562fff08 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -408,4 +408,5 @@ void CppCheck::reportOut(const std::string &outmsg) { // This is currently never called. It is here just to comply with // the interface. + std::string foo = outmsg; // This is just to get rid of compiler warning } diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index 964ed70ca..1e50f0b97 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -25,7 +25,8 @@ #include #include -#include +#include +#include #ifdef __BORLANDC__ #include @@ -476,7 +477,8 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename // std::string line; std::string path = filename; path.erase(1 + path.find_last_of("\\/")); - std::string::size_type pos = 0; + std::string::size_type pos = 0; + std::map handledFiles; while ((pos = code.find("#include", pos)) != std::string::npos) { // Accept only includes that are at the start of a line @@ -495,7 +497,16 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename filename = getHeaderFileName(filename); if (filename.length() == 0) continue; - + + if( handledFiles.find( filename ) != handledFiles.end() ) + { + // We have processed this file already once, skip + // it this time to avoid ethernal loop. + continue; + } + + handledFiles[ filename ] = true; + // filename contains now a file name e.g. "menu.h" std::string processedFile; for (std::list::const_iterator iter = includePaths.begin(); iter != includePaths.end(); ++iter) @@ -503,7 +514,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename std::ifstream fin; fin.open((*iter + filename).c_str()); if (fin.is_open()) - { + { filename = *iter + filename; processedFile = Preprocessor::read(fin); break; @@ -516,7 +527,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename std::ifstream fin(filename.c_str()); processedFile = Preprocessor::read(fin); } - + if (processedFile.length() > 0) { // Replace all tabs with spaces.. @@ -529,9 +540,10 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename // Remove space characters that are after or before new line character processedFile = removeSpaceNearNL(processedFile); processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile"; - code.insert(pos, processedFile); + code.insert(pos, processedFile); + } - } + } } class Macro @@ -637,9 +649,6 @@ public: } }; - -#include - std::string Preprocessor::expandMacros(std::string code) { // Search for macros and expand them..