Skip to content

Commit dfe551c

Browse files
committed
refs #583 - report CACHE_ERROR when simplecpp::FileDataCache::getFileId() fails
1 parent 5cd15b3 commit dfe551c

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ int main(int argc, char **argv)
239239
case simplecpp::Output::DUI_ERROR:
240240
std::cerr << "dui error: ";
241241
break;
242+
case simplecpp::Output::CACHE_ERROR:
243+
std::cerr << "cache error: ";
244+
break;
242245
}
243246
std::cerr << output.msg << std::endl;
244247
}

simplecpp.cpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <stack>
4646
#include <stdexcept>
4747
#include <string>
48+
#include <system_error>
4849
#ifdef SIMPLECPP_WINDOWS
4950
# include <mutex>
5051
#endif
@@ -3051,8 +3052,18 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat
30513052
const std::string &path = name_it->first;
30523053
FileID fileId;
30533054

3054-
if (!getFileId(path, fileId))
3055+
std::string errmsg;
3056+
if (!getFileId(path, fileId, errmsg)) {
3057+
if (outputList && !errmsg.empty()) {
3058+
simplecpp::Output err = {
3059+
Output::CACHE_ERROR,
3060+
Location{filenames},
3061+
"Failed to get ID for " + path + " (" + errmsg + ")"
3062+
};
3063+
outputList->push_back(std::move(err));
3064+
}
30553065
return {nullptr, false};
3066+
}
30563067

30573068
const auto id_it = mIdMap.find(fileId);
30583069
if (id_it != mIdMap.end()) {
@@ -3118,24 +3129,36 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::get(const std::
31183129
return {nullptr, false};
31193130
}
31203131

3121-
bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id)
3122-
{
3132+
bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id, std::string& errmsg) {
3133+
errmsg = "";
3134+
31233135
#ifdef _WIN32
31243136
HANDLE hFile = CreateFileA(path.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3125-
3126-
if (hFile == INVALID_HANDLE_VALUE)
3137+
if (hFile == INVALID_HANDLE_VALUE) {
3138+
const DWORD err = GetLastError();
3139+
if (err != ERROR_FILE_NOT_FOUND)
3140+
errmsg = "CreateFileA() failed with '" + std::system_category().message(err) + "'";
31273141
return false;
3142+
}
31283143

31293144
const BOOL ret = GetFileInformationByHandleEx(hFile, FileIdInfo, &id.fileIdInfo, sizeof(id.fileIdInfo));
3145+
if (!ret) {
3146+
const DWORD err = GetLastError();
3147+
errmsg = "GetFileInformationByHandleEx() failed with '" + std::system_category().message(err) + "'";
3148+
}
31303149

31313150
CloseHandle(hFile);
31323151

31333152
return ret == TRUE;
31343153
#else
31353154
struct stat statbuf;
31363155

3137-
if (stat(path.c_str(), &statbuf) != 0)
3156+
if (stat(path.c_str(), &statbuf) == -1) {
3157+
const int err = errno;
3158+
if (err != ENOENT)
3159+
errmsg = std::string("stat() failed with '") + strerror(err) + "'";
31383160
return false;
3161+
}
31393162

31403163
id.dev = statbuf.st_dev;
31413164
id.ino = statbuf.st_ino;

simplecpp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ namespace simplecpp {
211211
UNHANDLED_CHAR_ERROR,
212212
EXPLICIT_INCLUDE_NOT_FOUND,
213213
FILE_NOT_FOUND,
214-
DUI_ERROR
214+
DUI_ERROR,
215+
CACHE_ERROR
215216
} type;
216217
Output(Type type, const Location& loc, std::string msg) : type(type), location(loc), msg(std::move(msg)) {}
217218
Location location;
@@ -501,7 +502,7 @@ namespace simplecpp {
501502
using name_map_type = std::unordered_map<std::string, FileData *>;
502503
using id_map_type = std::unordered_map<FileID, FileData *, FileID::Hasher>;
503504

504-
static bool getFileId(const std::string &path, FileID &id);
505+
static bool getFileId(const std::string &path, FileID &id, std::string& errmsg);
505506

506507
std::pair<FileData *, bool> tryload(name_map_type::iterator &name_it, const DUI &dui, std::vector<std::string> &filenames, OutputList *outputList);
507508

test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ static std::string toString(const simplecpp::OutputList &outputList)
167167
case simplecpp::Output::Type::DUI_ERROR:
168168
ostr << "dui_error,";
169169
break;
170+
case simplecpp::Output::Type::CACHE_ERROR:
171+
ostr << "cache_error,";
172+
break;
170173
}
171174

172175
ostr << output.msg << '\n';

0 commit comments

Comments
 (0)