Preprocessor: Reverted fix for #2131, it didn't work well so a better fix is needed

This commit is contained in:
Daniel Marjamäki 2011-01-06 20:01:09 +01:00
parent 6e0f6c5aec
commit a7835c4054
2 changed files with 30 additions and 34 deletions

View File

@ -699,8 +699,7 @@ void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processe
processedFile = ostr.str(); processedFile = ostr.str();
} }
std::set<std::string> systemIncludes; handleIncludes(processedFile, filename, includePaths);
handleIncludes(processedFile, filename, includePaths, systemIncludes);
processedFile = replaceIfDefined(processedFile); processedFile = replaceIfDefined(processedFile);
@ -1490,13 +1489,17 @@ static int tolowerWrapper(int c)
} }
void Preprocessor::handleIncludes(std::string &code, void Preprocessor::handleIncludes(std::string &code, const std::string &filePath, const std::list<std::string> &includePaths)
const std::string &filePath,
const std::list<std::string> &includePaths,
std::set<std::string> &systemIncludes,
std::set<std::string> handledFiles)
{ {
std::list<std::string> paths;
std::string path;
path = filePath;
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::set<std::string> 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
@ -1506,6 +1509,15 @@ void Preprocessor::handleIncludes(std::string &code,
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("\n#endfile", endfilePos)) != std::string::npos && endfilePos < pos)
{
paths.pop_back();
endfilePos += 9; // 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);
@ -1539,17 +1551,11 @@ void Preprocessor::handleIncludes(std::string &code,
if (headerType == UserHeader && !fileOpened) if (headerType == UserHeader && !fileOpened)
{ {
if (filePath.find_first_of("\\/") != std::string::npos) fin.open((paths.back() + filename).c_str());
if (fin.is_open())
{ {
std::string path(filePath); filename = paths.back() + filename;
path.erase(1 + path.find_last_of("\\/")); fileOpened = true;
fin.open((path + filename).c_str());
if (fin.is_open())
{
filename = path + filename;
fileOpened = true;
}
} }
} }
@ -1558,8 +1564,7 @@ void Preprocessor::handleIncludes(std::string &code,
filename = Path::simplifyPath(filename.c_str()); filename = Path::simplifyPath(filename.c_str());
std::string tempFile = filename; std::string tempFile = filename;
std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper); std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper);
if (handledFiles.find(tempFile) != handledFiles.end() || if (handledFiles.find(tempFile) != handledFiles.end())
(headerType == SystemHeader && systemIncludes.find(tempFile) != systemIncludes.end()))
{ {
// We have processed this file already once, skip // We have processed this file already once, skip
// it this time to avoid eternal loop. // it this time to avoid eternal loop.
@ -1567,10 +1572,7 @@ void Preprocessor::handleIncludes(std::string &code,
continue; continue;
} }
if (headerType == SystemHeader) handledFiles.insert(tempFile);
systemIncludes.insert(tempFile);
else
handledFiles.insert(tempFile);
processedFile = Preprocessor::read(fin, filename, _settings); processedFile = Preprocessor::read(fin, filename, _settings);
fin.close(); fin.close();
} }
@ -1586,10 +1588,12 @@ void Preprocessor::handleIncludes(std::string &code,
// Remove space characters that are after or before new line character // Remove space characters that are after or before new line character
processedFile = removeSpaceNearNL(processedFile); processedFile = removeSpaceNearNL(processedFile);
handleIncludes(processedFile, filename, includePaths, systemIncludes, handledFiles);
processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile"; processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile";
code.insert(pos, processedFile); code.insert(pos, processedFile);
pos += processedFile.size();
path = filename;
path.erase(1 + path.find_last_of("\\/"));
paths.push_back(path);
} }
else if (!fileOpened) else if (!fileOpened)
{ {

View File

@ -25,7 +25,6 @@
#include <istream> #include <istream>
#include <string> #include <string>
#include <list> #include <list>
#include <set>
class ErrorLogger; class ErrorLogger;
class Settings; class Settings;
@ -213,16 +212,9 @@ private:
* There must be a path separator at the end. Default parameter is empty list. * There must be a path separator at the end. Default parameter is empty list.
* Note that if path from given filename is also extracted and that is used as * Note that if path from given filename is also extracted and that is used as
* a last include path if include file was not found from earlier paths. * a last include path if include file was not found from earlier paths.
* @param systemIncludes System includes
* @param handledFiles used in the recursive handling. Should be empty unless
* a recursive call is made.
* @return modified source code * @return modified source code
*/ */
void handleIncludes(std::string &code, void handleIncludes(std::string &code, const std::string &filePath, const std::list<std::string> &includePaths);
const std::string &filePath,
const std::list<std::string> &includePaths,
std::set<std::string> &systemIncludes,
std::set<std::string> handledFiles = std::set<std::string>());
Settings *_settings; Settings *_settings;
ErrorLogger *_errorLogger; ErrorLogger *_errorLogger;