Fix ticket #244 (Headers from included file are searched from wrong path)

http://apps.sourceforge.net/trac/cppcheck/ticket/244
This commit is contained in:
Reijo Tomperi 2009-04-05 21:14:02 +03:00
parent 2185a95cde
commit 051f2929b5
1 changed files with 23 additions and 4 deletions

View File

@ -28,6 +28,7 @@
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <vector>
Preprocessor::Preprocessor()
{
@ -490,13 +491,18 @@ std::string Preprocessor::getHeaderFileName(const std::string &str)
void Preprocessor::handleIncludes(std::string &code, const std::string &filename, const std::list<std::string> &includePaths)
{
// std::string line;
std::string path = filename;
std::list<std::string> paths;
std::string path;
path = filename;
path.erase(1 + path.find_last_of("\\/"));
paths.push_back(path);
std::string::size_type pos = 0;
std::string::size_type endfilePos = 0;
std::map<std::string, bool> handledFiles;
endfilePos = pos;
while ((pos = code.find("#include", pos)) != std::string::npos)
{
// Accept only includes that are at the start of a line
if (pos > 0 && code[pos-1] != '\n')
{
@ -504,6 +510,15 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
continue;
}
// If endfile is encountered, we have moved to a next file in our stack,
// so remove last path in our list.
while ((endfilePos = code.find("#endfile", endfilePos)) != std::string::npos && endfilePos < pos)
{
paths.pop_back();
endfilePos += 8; // size of #endfile
}
endfilePos = pos;
std::string::size_type end = code.find("\n", pos);
std::string filename = code.substr(pos, end - pos);
@ -541,8 +556,9 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
if (processedFile.length() == 0)
{
filename = path + filename;
filename = paths.back() + filename;
std::ifstream fin(filename.c_str());
if (fin.is_open())
processedFile = Preprocessor::read(fin);
}
@ -560,6 +576,9 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile";
code.insert(pos, processedFile);
path = filename;
path.erase(1 + path.find_last_of("\\/"));
paths.push_back(path);
}
}
}