Fix ticket #304 (#include <file.h> 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.
This commit is contained in:
Reijo Tomperi 2009-05-23 00:18:48 +03:00
parent 93d9400f63
commit bbf63b7970
3 changed files with 36 additions and 12 deletions

View File

@ -716,12 +716,15 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
return expandMacros(ret.str(), filename, errorLogger); 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 result;
std::string::size_type i = str.find_first_of("<\""); std::string::size_type i = str.find_first_of("<\"");
if (i == std::string::npos) if (i == std::string::npos)
return result; {
str = "";
return 0;
}
char c = str[i]; char c = str[i];
if (c == '<') if (c == '<')
@ -735,7 +738,11 @@ std::string Preprocessor::getHeaderFileName(const std::string &str)
result.append(1, str[i]); 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<std::string> &includePaths) void Preprocessor::handleIncludes(std::string &code, const std::string &filename, const std::list<std::string> &includePaths)
@ -774,8 +781,8 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
// Remove #include clause // Remove #include clause
code.erase(pos, end - pos); code.erase(pos, end - pos);
filename = getHeaderFileName(filename); int headerType = getHeaderFileName(filename);
if (filename.length() == 0) if (headerType == 0)
continue; continue;
std::string tempFile = filename; 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; filename = paths.back() + filename;
std::ifstream fin(filename.c_str()); std::ifstream fin(filename.c_str());

View File

@ -98,10 +98,12 @@ protected:
/** /**
* Returns the string between double quote characters or < > characters. * Returns the string between double quote characters or < > characters.
* @param str e.g. '#include "menu.h"' or '#include <menu.h>' * @param str e.g. '#include "menu.h"' or '#include <menu.h>'
* @return e.g. 'menu.h' or empty string if double quotes * After function call it will contain e.g. "menu.h" without double quotes.
* or < > were not found. * @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: private:
/** /**

View File

@ -51,7 +51,7 @@ public:
return Preprocessor::expandMacros(code, "file.cpp", errorLogger); 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); return Preprocessor::getHeaderFileName(str);
} }
@ -1076,8 +1076,23 @@ private:
void includes() void includes()
{ {
ASSERT_EQUALS("a.h", OurPreprocessor::getHeaderFileName("#include \"a.h\"")); {
ASSERT_EQUALS("a.h", OurPreprocessor::getHeaderFileName("#include <a.h>")); 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 <c.h>";
ASSERT_EQUALS(2, OurPreprocessor::getHeaderFileName(src));
ASSERT_EQUALS("c.h", src);
}
} }
}; };