Improve path name handling in ErrorLogger.

This commit adds setfile() method to FileLocation class. The setfile
method converts in Windows path separators to internally used Unix
separators. And getfile() converts path separators back to Windows
separators. This fixes bugs that error reports had mixed path
separators in paths.
This commit is contained in:
Kimmo Varis 2010-07-17 01:27:40 +03:00
parent 6362e50e73
commit 6db365e6f7
8 changed files with 56 additions and 23 deletions

View File

@ -146,7 +146,7 @@ protected:
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = (*it)->linenr(); loc.line = (*it)->linenr();
loc.file = _tokenizer->file(*it); loc.setfile(_tokenizer->file(*it));
locationList.push_back(loc); locationList.push_back(loc);
} }

View File

@ -320,7 +320,7 @@ void CheckMemoryLeak::reportErr(const std::list<const Token *> &callstack, Sever
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr(); loc.line = tok->linenr();
loc.file = tokenizer->file(tok); loc.setfile(tokenizer->file(tok));
locations.push_back(loc); locations.push_back(loc);
} }

View File

@ -187,7 +187,7 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
if (!filename.empty()) if (!filename.empty())
{ {
ErrorLogger::ErrorMessage::FileLocation fileLoc; ErrorLogger::ErrorMessage::FileLocation fileLoc;
fileLoc.file = filename; fileLoc.setfile(filename);
fileLoc.line = 1; fileLoc.line = 1;
locationList.push_back(fileLoc); locationList.push_back(fileLoc);
} }

View File

@ -100,7 +100,7 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
} }
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.file = temp.substr(temp.find(':') + 1); loc.setfile(temp.substr(temp.find(':') + 1));
temp = temp.substr(0, temp.find(':')); temp = temp.substr(0, temp.find(':'));
std::istringstream fiss(temp); std::istringstream fiss(temp);
fiss >> loc.line; fiss >> loc.line;
@ -217,7 +217,7 @@ std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMes
std::string ErrorLogger::ErrorMessage::FileLocation::getfile() const std::string ErrorLogger::ErrorMessage::FileLocation::getfile() const
{ {
std::string f(file); std::string f(_file);
// replace "/ab/../" with "/".. // replace "/ab/../" with "/"..
std::string::size_type pos = 0; std::string::size_type pos = 0;
@ -228,15 +228,15 @@ std::string ErrorLogger::ErrorMessage::FileLocation::getfile() const
continue; continue;
// Previous char must be a separator.. // Previous char must be a separator..
if (f[pos-1] != '/' && f[pos-2] != '\\') if (f[pos-1] != '/')
continue; continue;
// Next char must be a separator.. // Next char must be a separator..
if (f[pos+2] != '/' && f[pos+2] != '\\') if (f[pos+2] != '/')
continue; continue;
// Locate previous separator.. // Locate previous separator..
std::string::size_type sep = f.find_last_of("/\\", pos - 2); std::string::size_type sep = f.find_last_of("/", pos - 2);
if (sep == std::string::npos) if (sep == std::string::npos)
continue; continue;
@ -245,6 +245,36 @@ std::string ErrorLogger::ErrorMessage::FileLocation::getfile() const
pos = sep; pos = sep;
} }
#if defined(_WIN32)
{
std::string::iterator iter = f.begin();
std::string::iterator end = f.end();
while (iter != end)
{
if (*iter == '/')
*iter = '\\';
++iter;
}
}
#endif
return f; return f;
} }
void ErrorLogger::ErrorMessage::FileLocation::setfile(const std::string &file)
{
_file = file;
std::cout << "Setting file: " << file << std::endl;
#if defined(_WIN32)
{
std::string::iterator iter = _file.begin();
std::string::iterator end = _file.end();
while (iter != end)
{
if (*iter == '\\')
*iter = '/';
++iter;
}
}
#endif
}

View File

@ -91,8 +91,11 @@ public:
} }
std::string getfile() const; std::string getfile() const;
std::string file; void setfile(const std::string &file);
unsigned int line; unsigned int line;
private:
std::string _file;
}; };
ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id); ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id);

View File

@ -46,7 +46,7 @@ void Preprocessor::writeError(const std::string &fileName, const int linenr, Err
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = linenr; loc.line = linenr;
loc.file = fileName; loc.setfile(fileName);
locationList.push_back(loc); locationList.push_back(loc);
errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList, errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList,
Severity::error, Severity::error,
@ -754,7 +754,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
ErrorLogger::ErrorMessage errmsg; ErrorLogger::ErrorMessage errmsg;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.file = filename; loc.setfile(filename);
loc.line = linenr; loc.line = linenr;
errmsg._callStack.push_back(loc); errmsg._callStack.push_back(loc);
errmsg._severity = Severity::fromString("error"); errmsg._severity = Severity::fromString("error");
@ -899,7 +899,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
ErrorLogger::ErrorMessage errmsg; ErrorLogger::ErrorMessage errmsg;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.file = filename; loc.setfile(filename);
loc.line = 1; loc.line = 1;
errmsg._callStack.push_back(loc); errmsg._callStack.push_back(loc);
errmsg._severity = Severity::fromString("error"); errmsg._severity = Severity::fromString("error");

View File

@ -408,10 +408,10 @@ void Tokenizer::duplicateTypedefError(const Token *tok1, const Token *tok2, cons
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok1->linenr(); loc.line = tok1->linenr();
loc.file = file(tok1); loc.setfile(file(tok1));
locationList.push_back(loc); locationList.push_back(loc);
loc.line = tok2->linenr(); loc.line = tok2->linenr();
loc.file = file(tok2); loc.setfile(file(tok2));
locationList.push_back(loc); locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList, const ErrorLogger::ErrorMessage errmsg(locationList,
@ -434,10 +434,10 @@ void Tokenizer::duplicateDeclarationError(const Token *tok1, const Token *tok2,
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok1->linenr(); loc.line = tok1->linenr();
loc.file = file(tok1); loc.setfile(file(tok1));
locationList.push_back(loc); locationList.push_back(loc);
loc.line = tok2->linenr(); loc.line = tok2->linenr();
loc.file = file(tok2); loc.setfile(file(tok2));
locationList.push_back(loc); locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList, const ErrorLogger::ErrorMessage errmsg(locationList,
@ -2284,7 +2284,7 @@ void Tokenizer::simplifyTemplates()
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok2->linenr(); loc.line = tok2->linenr();
loc.file = file(tok2); loc.setfile(file(tok2));
locationList.push_back(loc); locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList, const ErrorLogger::ErrorMessage errmsg(locationList,
@ -6177,10 +6177,10 @@ void Tokenizer::duplicateEnumError(const Token * tok1, const Token * tok2, const
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok1->linenr(); loc.line = tok1->linenr();
loc.file = file(tok1); loc.setfile(file(tok1));
locationList.push_back(loc); locationList.push_back(loc);
loc.line = tok2->linenr(); loc.line = tok2->linenr();
loc.file = file(tok2); loc.setfile(file(tok2));
locationList.push_back(loc); locationList.push_back(loc);
const ErrorLogger::ErrorMessage errmsg(locationList, const ErrorLogger::ErrorMessage errmsg(locationList,
@ -6926,7 +6926,7 @@ void Tokenizer::syntaxError(const Token *tok, char c)
{ {
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr(); loc.line = tok->linenr();
loc.file = file(tok); loc.setfile(file(tok));
locationList.push_back(loc); locationList.push_back(loc);
} }
@ -6969,7 +6969,7 @@ void Tokenizer::cppcheckError(const Token *tok) const
{ {
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr(); loc.line = tok->linenr();
loc.file = file(tok); loc.setfile(file(tok));
locationList.push_back(loc); locationList.push_back(loc);
} }

View File

@ -382,7 +382,7 @@ private:
{ {
ErrorLogger::ErrorMessage errorMessage; ErrorLogger::ErrorMessage errorMessage;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.file = "ab/cd/../ef.h"; loc.setfile("ab/cd/../ef.h");
errorMessage._callStack.push_back(loc); errorMessage._callStack.push_back(loc);
ASSERT_EQUALS("<error file=\"ab/ef.h\" line=\"0\" id=\"\" severity=\"\" msg=\"\"/>", errorMessage.toXML()); ASSERT_EQUALS("<error file=\"ab/ef.h\" line=\"0\" id=\"\" severity=\"\" msg=\"\"/>", errorMessage.toXML());
ASSERT_EQUALS("[ab/ef.h:0]: ", errorMessage.toString()); ASSERT_EQUALS("[ab/ef.h:0]: ", errorMessage.toString());
@ -392,7 +392,7 @@ private:
{ {
ErrorLogger::ErrorMessage errorMessage; ErrorLogger::ErrorMessage errorMessage;
ErrorLogger::ErrorMessage::FileLocation loc; ErrorLogger::ErrorMessage::FileLocation loc;
loc.file = "some/{file}file.cpp"; loc.setfile("some/{file}file.cpp");
loc.line = 10; loc.line = 10;
errorMessage._callStack.push_back(loc); errorMessage._callStack.push_back(loc);
errorMessage._id = "testId"; errorMessage._id = "testId";