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 <iostream>
#include <cstdlib> #include <cstdlib>
#include <cctype> #include <cctype>
#include <vector>
Preprocessor::Preprocessor() 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) void Preprocessor::handleIncludes(std::string &code, const std::string &filename, const std::list<std::string> &includePaths)
{ {
// std::string line; std::list<std::string> paths;
std::string path = filename; std::string path;
path = filename;
path.erase(1 + path.find_last_of("\\/")); path.erase(1 + path.find_last_of("\\/"));
paths.push_back(path);
std::string::size_type pos = 0; std::string::size_type pos = 0;
std::string::size_type endfilePos = 0;
std::map<std::string, bool> handledFiles; std::map<std::string, bool> handledFiles;
endfilePos = pos;
while ((pos = code.find("#include", pos)) != std::string::npos) while ((pos = code.find("#include", pos)) != std::string::npos)
{ {
// Accept only includes that are at the start of a line // Accept only includes that are at the start of a line
if (pos > 0 && code[pos-1] != '\n') if (pos > 0 && code[pos-1] != '\n')
{ {
@ -504,6 +510,15 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename
continue; 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::size_type end = code.find("\n", pos);
std::string filename = code.substr(pos, end - 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) if (processedFile.length() == 0)
{ {
filename = path + filename; filename = paths.back() + filename;
std::ifstream fin(filename.c_str()); std::ifstream fin(filename.c_str());
if (fin.is_open())
processedFile = Preprocessor::read(fin); 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"; processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile";
code.insert(pos, processedFile); code.insert(pos, processedFile);
path = filename;
path.erase(1 + path.find_last_of("\\/"));
paths.push_back(path);
} }
} }
} }