Preprocessor: run paths of some error messages through Path::simplifyPath()

teach Path::simplifyPath() to recursively remove "./././" from beginning of paths.
This commit is contained in:
Matthias Krüger 2015-11-30 19:27:15 +01:00
parent 9c3f25603e
commit c7bbc27130
3 changed files with 28 additions and 5 deletions

View File

@ -63,8 +63,7 @@ std::string Path::simplifyPath(std::string originalPath)
const bool isUnc = originalPath.size() > 2 && originalPath[0] == '/' && originalPath[1] == '/'; const bool isUnc = originalPath.size() > 2 && originalPath[0] == '/' && originalPath[1] == '/';
// Remove ./, .//, ./// etc. at the beginning // Remove ./, .//, ./// etc. at the beginning
if (originalPath.size() > 2 && originalPath[0] == '.' && while (originalPath.size() > 2 && originalPath[0] == '.' && originalPath[1] == '/') { // remove "./././"
originalPath[1] == '/') {
size_t toErase = 2; size_t toErase = 2;
for (std::size_t i = 2; i < originalPath.size(); i++) { for (std::size_t i = 2; i < originalPath.size(); i++) {
if (originalPath[i] == '/') if (originalPath[i] == '/')

View File

@ -485,7 +485,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
errmsg.str(""); errmsg.str("");
errmsg << "The code contains unhandled characters " << info << ". Checking continues, but do not expect valid results.\n" errmsg << "The code contains unhandled characters " << info << ". Checking continues, but do not expect valid results.\n"
<< "The code contains characters that are unhandled " << info << ". Neither unicode nor extended ASCII are supported. Checking continues, but do not expect valid results."; << "The code contains characters that are unhandled " << info << ". Neither unicode nor extended ASCII are supported. Checking continues, but do not expect valid results.";
writeError(filename, lineno, _errorLogger, "unhandledCharacters", errmsg.str()); writeError(Path::simplifyPath(filename), lineno, _errorLogger, "unhandledCharacters", errmsg.str());
} }
if (_settings.terminated()) if (_settings.terminated())
@ -1914,7 +1914,7 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string
// #error => return "" // #error => return ""
if (match && line.compare(0, 6, "#error") == 0) { if (match && line.compare(0, 6, "#error") == 0) {
if (!_settings.userDefines.empty() && !_settings._force) { if (!_settings.userDefines.empty() && !_settings._force) {
error(filenames.top(), lineno, line); error(Path::simplifyPath(filenames.top()), lineno, line);
} }
return ""; return "";
} }
@ -3044,7 +3044,7 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
++pos; ++pos;
if (pos >= line.size()) { if (pos >= line.size()) {
writeError(filename, writeError(Path::simplifyPath(filename),
linenr + tmpLinenr, linenr + tmpLinenr,
errorLogger, errorLogger,
"noQuoteCharPair", "noQuoteCharPair",

View File

@ -300,6 +300,9 @@ private:
TEST_CASE(invalid_ifs); // #5909 TEST_CASE(invalid_ifs); // #5909
TEST_CASE(garbage); TEST_CASE(garbage);
TEST_CASE(wrongPathOnUnicodeError); // see #6773
TEST_CASE(wrongPathOnErrorDirective);
} }
std::string preprocessorRead(const char* code) { std::string preprocessorRead(const char* code) {
@ -3711,6 +3714,27 @@ private:
std::map<std::string, std::string> actual; std::map<std::string, std::string> actual;
preprocess(filedata, actual); preprocess(filedata, actual);
} }
void wrongPathOnUnicodeError() {
const char filedata[] = "#file ././test.c\n"
"extern int 🌷;\n";
preprocessorRead(filedata);
ASSERT_EQUALS("[test.c:2]: (error) The code contains unhandled characters (character code = 0xf0). Checking continues, but do not expect valid results.\n"
"[test.c:2]: (error) The code contains unhandled characters (character code = 0x9f). Checking continues, but do not expect valid results.\n"
"[test.c:2]: (error) The code contains unhandled characters (character code = 0x8c). Checking continues, but do not expect valid results.\n"
"[test.c:2]: (error) The code contains unhandled characters (character code = 0xb7). Checking continues, but do not expect valid results.\n", errout.str());
}
void wrongPathOnErrorDirective() {
errout.str("");
Settings settings;
settings.userDefines = "foo";
Preprocessor preprocessor(settings, this);
const std::string code("#error hello world!\n");
preprocessor.getcode(code, "X", "./././test.c");
ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str());
}
}; };
REGISTER_TEST(TestPreprocessor) REGISTER_TEST(TestPreprocessor)