From bbf63b7970d1c134f10221562ae1e04c17794d4d Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sat, 23 May 2009 00:18:48 +0300 Subject: [PATCH] Fix ticket #304 (#include should be searched from paths given with -I parameter.) http://apps.sourceforge.net/trac/cppcheck/ticket/304 Note that the ticket is same as with previous commit, but task description was changed a little. --- src/preprocessor.cpp | 19 +++++++++++++------ src/preprocessor.h | 8 +++++--- test/testpreprocessor.cpp | 21 ++++++++++++++++++--- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index b6c70cded..f4a310d16 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -716,12 +716,15 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg, return expandMacros(ret.str(), filename, errorLogger); } -std::string Preprocessor::getHeaderFileName(const std::string &str) +int Preprocessor::getHeaderFileName(std::string &str) { std::string result; std::string::size_type i = str.find_first_of("<\""); if (i == std::string::npos) - return result; + { + str = ""; + return 0; + } char c = str[i]; if (c == '<') @@ -735,7 +738,11 @@ std::string Preprocessor::getHeaderFileName(const std::string &str) result.append(1, str[i]); } - return result; + str = result; + if (c == '"') + return 1; + else + return 2; } void Preprocessor::handleIncludes(std::string &code, const std::string &filename, const std::list &includePaths) @@ -774,8 +781,8 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename // Remove #include clause code.erase(pos, end - pos); - filename = getHeaderFileName(filename); - if (filename.length() == 0) + int headerType = getHeaderFileName(filename); + if (headerType == 0) continue; std::string tempFile = filename; @@ -803,7 +810,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename } } - if (processedFile.length() == 0) + if (headerType == 1 && processedFile.length() == 0) { filename = paths.back() + filename; std::ifstream fin(filename.c_str()); diff --git a/src/preprocessor.h b/src/preprocessor.h index 699a02bd4..07063b3ad 100644 --- a/src/preprocessor.h +++ b/src/preprocessor.h @@ -98,10 +98,12 @@ protected: /** * Returns the string between double quote characters or < > characters. * @param str e.g. '#include "menu.h"' or '#include ' - * @return e.g. 'menu.h' or empty string if double quotes - * or < > were not found. + * After function call it will contain e.g. "menu.h" without double quotes. + * @return 0 empty string if double quotes or < > were not found. + * 1 if file surrounded with "" was found + * 2 if file surrounded with <> was found */ - static std::string getHeaderFileName(const std::string &str); + static int getHeaderFileName(std::string &str); private: /** diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index aae70f4ab..26e0045ff 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -51,7 +51,7 @@ public: return Preprocessor::expandMacros(code, "file.cpp", errorLogger); } - static std::string getHeaderFileName(const std::string &str) + static int getHeaderFileName(std::string &str) { return Preprocessor::getHeaderFileName(str); } @@ -1076,8 +1076,23 @@ private: void includes() { - ASSERT_EQUALS("a.h", OurPreprocessor::getHeaderFileName("#include \"a.h\"")); - ASSERT_EQUALS("a.h", OurPreprocessor::getHeaderFileName("#include ")); + { + std::string src = "#include a.h"; + ASSERT_EQUALS(0, OurPreprocessor::getHeaderFileName(src)); + ASSERT_EQUALS("", src); + } + + { + std::string src = "#include \"b.h\""; + ASSERT_EQUALS(1, OurPreprocessor::getHeaderFileName(src)); + ASSERT_EQUALS("b.h", src); + } + + { + std::string src = "#include "; + ASSERT_EQUALS(2, OurPreprocessor::getHeaderFileName(src)); + ASSERT_EQUALS("c.h", src); + } } };