Fix ticket #304 (#include <file.h> should be parsed like #include "file.h" is being parsed)

http://apps.sourceforge.net/trac/cppcheck/ticket/304
This commit is contained in:
Reijo Tomperi 2009-05-22 23:59:07 +03:00
parent 0f9b2efa43
commit 93d9400f63
3 changed files with 24 additions and 10 deletions

View File

@ -719,13 +719,17 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
std::string Preprocessor::getHeaderFileName(const std::string &str)
{
std::string result;
std::string::size_type i = str.find("\"");
std::string::size_type i = str.find_first_of("<\"");
if (i == std::string::npos)
return result;
char c = str[i];
if (c == '<')
c = '>';
for (i = i + 1; i < str.length(); ++i)
{
if (str[i] == '"')
if (str[i] == c)
break;
result.append(1, str[i]);

View File

@ -95,6 +95,13 @@ protected:
*/
static std::string removeComments(const std::string &str);
/**
* Returns the string between double quote characters or < > characters.
* @param str e.g. '#include "menu.h"' or '#include <menu.h>'
* @return e.g. 'menu.h' or empty string if double quotes
* or < > were not found.
*/
static std::string getHeaderFileName(const std::string &str);
private:
/**
@ -128,13 +135,6 @@ private:
*/
static void handleIncludes(std::string &code, const std::string &filename, const std::list<std::string> &includePaths);
/**
* Returns the string between double quote characters.
* @param str e.g. '#include "menu.h"'
* @return e.g. 'menu.h' or empty string if double quotes were
* not found.
*/
static std::string getHeaderFileName(const std::string &str);
};
//---------------------------------------------------------------------------

View File

@ -50,6 +50,11 @@ public:
{
return Preprocessor::expandMacros(code, "file.cpp", errorLogger);
}
static std::string getHeaderFileName(const std::string &str)
{
return Preprocessor::getHeaderFileName(str);
}
};
private:
@ -126,6 +131,7 @@ private:
TEST_CASE(multiline_comment);
TEST_CASE(macro_parameters);
TEST_CASE(newline_in_macro);
TEST_CASE(includes);
}
@ -1068,7 +1074,11 @@ private:
ASSERT_EQUALS("", errout.str());
}
void includes()
{
ASSERT_EQUALS("a.h", OurPreprocessor::getHeaderFileName("#include \"a.h\""));
ASSERT_EQUALS("a.h", OurPreprocessor::getHeaderFileName("#include <a.h>"));
}
};
REGISTER_TEST(TestPreprocessor)