Possibly fix #1369 (Internal error - double-declared enum followed by another enum)

http://sourceforge.net/apps/trac/cppcheck/ticket/1369
Don't include same file twice if one is a/a.h and other is a/../a/a.h
This commit is contained in:
Reijo Tomperi 2010-02-09 22:26:15 +02:00
parent 18e7813e04
commit faced1b483
3 changed files with 23 additions and 16 deletions

View File

@ -59,8 +59,9 @@ std::string FileLister::simplifyPath(const char *originalPath)
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i)
{
if (pathParts[i] == ".." && i > 1)
if (pathParts[i] == ".." && i > 1 && pathParts.size() > i + 1)
{
pathParts.erase(pathParts.begin() + i + 1);
pathParts.erase(pathParts.begin() + i);
pathParts.erase(pathParts.begin() + i - 1);
pathParts.erase(pathParts.begin() + i - 2);

View File

@ -1262,7 +1262,34 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
if (headerType == 0)
continue;
std::string tempFile = filename;
// filename contains now a file name e.g. "menu.h"
std::string processedFile;
bool fileOpened = false;
std::ifstream fin;
for (std::list<std::string>::const_iterator iter = includePaths.begin(); iter != includePaths.end(); ++iter)
{
fin.open((*iter + filename).c_str());
if (fin.is_open())
{
filename = *iter + filename;
fileOpened = true;
break;
}
}
if (headerType == 1 && !fileOpened)
{
filename = paths.back() + filename;
fin.open(filename.c_str());
if (fin.is_open())
{
fileOpened = true;
}
}
if (fileOpened)
{
std::string tempFile = FileLister::simplifyPath(filename.c_str());
std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper);
if (handledFiles.find(tempFile) != handledFiles.end())
{
@ -1272,32 +1299,9 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
}
handledFiles.insert(tempFile);
// filename contains now a file name e.g. "menu.h"
std::string processedFile;
bool fileOpened = false;
for (std::list<std::string>::const_iterator iter = includePaths.begin(); iter != includePaths.end(); ++iter)
{
std::ifstream fin;
fin.open((*iter + filename).c_str());
if (fin.is_open())
{
filename = *iter + filename;
processedFile = Preprocessor::read(fin, filename, _settings);
fileOpened = true;
break;
}
}
if (headerType == 1 && !fileOpened)
{
filename = paths.back() + filename;
std::ifstream fin(filename.c_str());
if (fin.is_open())
{
processedFile = Preprocessor::read(fin, filename, _settings);
fileOpened = true;
}
fin.close();
}
if (processedFile.length() > 0)

View File

@ -45,6 +45,8 @@ private:
ASSERT_EQUALS("/index.h", FileLister::simplifyPath("/path/../other/../index.h"));
ASSERT_EQUALS("/index.h", FileLister::simplifyPath("/path/../other///././../index.h"));
ASSERT_EQUALS("../path/index.h", FileLister::simplifyPath("../path/other/../index.h"));
ASSERT_EQUALS("a/index.h", FileLister::simplifyPath("a/../a/index.h"));
ASSERT_EQUALS("a/..", FileLister::simplifyPath("a/.."));
}