Catch ios_base::failure exceptions in ReadContent

`filename` may be a directory (the latest Emacs lsp-mode sometimes sends a `textDocument/didOpen` message with an empty filename) or the file cannot be read.
This commit is contained in:
Fangrui Song 2017-11-21 21:23:17 -08:00 committed by Jacob Dufault
parent 7d06ff212f
commit dba27c3d34

View File

@ -229,11 +229,13 @@ std::istream& SafeGetline(std::istream& is, std::string& t) {
optional<std::string> ReadContent(const std::string& filename) {
std::ifstream cache;
cache.open(filename);
if (!cache.good())
return nullopt;
try {
return std::string(std::istreambuf_iterator<char>(cache),
std::istreambuf_iterator<char>());
} catch (std::ios_base::failure&) {
return nullopt;
}
}
std::vector<std::string> ReadLines(std::string filename) {