Quote file path if file not found to make trailing ws apparent.

For an include directive with a path with trailing whitespace, e.g.

```
INCLUDE
  '/home/model/../include/file_trailing_ws.ext ' /
```
the current error message
```
Error: No such file: /home/model/../include/file_trailing_ws.ext
```
is changed to
```
File '/home/model/../include/file_trailing_ws.ext ' included via
INCLUDE directive does not exist. Note that the file name contains trailing whitespace.
```

This should at least make it easier to spot this error.

An alternative would be to trim the string, but people using quotes
should actually know what they are doing.
This commit is contained in:
Markus Blatt
2023-06-27 14:02:22 +02:00
parent c6d4a635e3
commit 340b3dc450

View File

@@ -66,6 +66,7 @@
#include <stack>
#include <stdexcept>
#include <string>
#include <regex>
#include <utility>
#include <vector>
@@ -688,7 +689,19 @@ std::optional<std::filesystem::path> ParserState::getIncludeFilePath( std::strin
try {
includeFilePath = std::filesystem::canonical(includeFilePath);
} catch (const std::filesystem::filesystem_error& fs_error) {
parseContext.handleError( ParseContext::PARSE_MISSING_INCLUDE , fmt::format("No such file: {}", includeFilePath.string()), {}, errors);
const auto& str_rep = includeFilePath.string();
std::regex trim_regex("^\\s+|\\s+$");
const auto& trimmed_rep = std::regex_replace(str_rep, trim_regex, "");
std::string extra_info;
if (str_rep.size() != trimmed_rep.size()) {
extra_info = " Note that the file name contains trailing whitespace.";
}
parseContext.handleError( ParseContext::PARSE_MISSING_INCLUDE ,
fmt::format("File '{}' included via INCLUDE"
" directive does not exist.{}",
str_rep,
extra_info),
{}, errors);
return {};
}