Skip to content

Commit faced1b

Browse files
committed
Possibly fix danmar#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
1 parent 18e7813 commit faced1b

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

lib/filelister.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ std::string FileLister::simplifyPath(const char *originalPath)
5959

6060
for (std::vector<std::string>::size_type i = 0; i < pathParts.size(); ++i)
6161
{
62-
if (pathParts[i] == ".." && i > 1)
62+
if (pathParts[i] == ".." && i > 1 && pathParts.size() > i + 1)
6363
{
64+
pathParts.erase(pathParts.begin() + i + 1);
6465
pathParts.erase(pathParts.begin() + i);
6566
pathParts.erase(pathParts.begin() + i - 1);
6667
pathParts.erase(pathParts.begin() + i - 2);

lib/preprocessor.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,28 +1262,16 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
12621262
if (headerType == 0)
12631263
continue;
12641264

1265-
std::string tempFile = filename;
1266-
std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper);
1267-
if (handledFiles.find(tempFile) != handledFiles.end())
1268-
{
1269-
// We have processed this file already once, skip
1270-
// it this time to avoid ethernal loop.
1271-
continue;
1272-
}
1273-
1274-
handledFiles.insert(tempFile);
1275-
12761265
// filename contains now a file name e.g. "menu.h"
12771266
std::string processedFile;
12781267
bool fileOpened = false;
1268+
std::ifstream fin;
12791269
for (std::list<std::string>::const_iterator iter = includePaths.begin(); iter != includePaths.end(); ++iter)
12801270
{
1281-
std::ifstream fin;
12821271
fin.open((*iter + filename).c_str());
12831272
if (fin.is_open())
12841273
{
12851274
filename = *iter + filename;
1286-
processedFile = Preprocessor::read(fin, filename, _settings);
12871275
fileOpened = true;
12881276
break;
12891277
}
@@ -1292,14 +1280,30 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
12921280
if (headerType == 1 && !fileOpened)
12931281
{
12941282
filename = paths.back() + filename;
1295-
std::ifstream fin(filename.c_str());
1283+
fin.open(filename.c_str());
12961284
if (fin.is_open())
12971285
{
1298-
processedFile = Preprocessor::read(fin, filename, _settings);
12991286
fileOpened = true;
13001287
}
13011288
}
13021289

1290+
if (fileOpened)
1291+
{
1292+
std::string tempFile = FileLister::simplifyPath(filename.c_str());
1293+
std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper);
1294+
if (handledFiles.find(tempFile) != handledFiles.end())
1295+
{
1296+
// We have processed this file already once, skip
1297+
// it this time to avoid ethernal loop.
1298+
continue;
1299+
}
1300+
1301+
handledFiles.insert(tempFile);
1302+
std::ifstream fin(filename.c_str());
1303+
processedFile = Preprocessor::read(fin, filename, _settings);
1304+
fin.close();
1305+
}
1306+
13031307
if (processedFile.length() > 0)
13041308
{
13051309
// Replace all tabs with spaces..

test/testfilelister.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class TestFileLister : public TestFixture
4545
ASSERT_EQUALS("/index.h", FileLister::simplifyPath("/path/../other/../index.h"));
4646
ASSERT_EQUALS("/index.h", FileLister::simplifyPath("/path/../other///././../index.h"));
4747
ASSERT_EQUALS("../path/index.h", FileLister::simplifyPath("../path/other/../index.h"));
48+
ASSERT_EQUALS("a/index.h", FileLister::simplifyPath("a/../a/index.h"));
49+
ASSERT_EQUALS("a/..", FileLister::simplifyPath("a/.."));
4850
}
4951

5052

0 commit comments

Comments
 (0)