Skip to content

Commit ea78f9a

Browse files
authored
Fix danmar#334 Do not assert when source file is missing (danmar#333)
1 parent e096d6b commit ea78f9a

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ int main(int argc, char **argv)
166166
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
167167
std::cerr << "explicit include not found: ";
168168
break;
169+
case simplecpp::Output::FILE_NOT_FOUND:
170+
std::cerr << "file not found: ";
171+
break;
169172
}
170173
std::cerr << output.msg << std::endl;
171174
}

simplecpp.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,10 @@ class FileStream : public simplecpp::TokenList::Stream {
385385
, lastCh(0)
386386
, lastStatus(0)
387387
{
388-
assert(file != nullptr);
388+
if (!file) {
389+
const std::vector<std::string> location;
390+
throw simplecpp::Output(location, simplecpp::Output::FILE_NOT_FOUND, "File is missing: " + filename);
391+
}
389392
init();
390393
}
391394

@@ -442,8 +445,15 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
442445
simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList)
443446
: frontToken(nullptr), backToken(nullptr), files(filenames)
444447
{
445-
FileStream stream(filename);
446-
readfile(stream,filename,outputList);
448+
try
449+
{
450+
FileStream stream(filename);
451+
readfile(stream,filename,outputList);
452+
}
453+
catch(const simplecpp::Output & e) // TODO handle extra type of errors
454+
{
455+
outputList->push_back(e);
456+
}
447457
}
448458

449459
simplecpp::TokenList::TokenList(const TokenList &other) : frontToken(nullptr), backToken(nullptr), files(other.files)

simplecpp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,10 @@ namespace simplecpp {
180180
SYNTAX_ERROR,
181181
PORTABILITY_BACKSLASH,
182182
UNHANDLED_CHAR_ERROR,
183-
EXPLICIT_INCLUDE_NOT_FOUND
183+
EXPLICIT_INCLUDE_NOT_FOUND,
184+
FILE_NOT_FOUND
184185
} type;
186+
explicit Output(const std::vector<std::string> &files, Output::Type id, const std::string & errMsg ) : type(id), location(files), msg(errMsg) {}
185187
Location location;
186188
std::string msg;
187189
};

test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ static std::string toString(const simplecpp::OutputList &outputList)
150150
break;
151151
case simplecpp::Output::Type::EXPLICIT_INCLUDE_NOT_FOUND:
152152
ostr << "explicit_include_not_found,";
153+
break;
154+
case simplecpp::Output::Type::FILE_NOT_FOUND:
155+
ostr << "file_not_found,";
156+
break;
153157
}
154158

155159
ostr << output.msg << '\n';
@@ -2266,6 +2270,14 @@ static void readfile_error()
22662270
"X",readfile("#if !A\n#error\n#endif\nX\n"));
22672271
}
22682272

2273+
static void readfile_file_not_found()
2274+
{
2275+
simplecpp::OutputList outputList;
2276+
std::vector<std::string> files;
2277+
(void)simplecpp::TokenList("NotAFile", files, &outputList);
2278+
ASSERT_EQUALS("file0,1,file_not_found,File is missing: NotAFile\n", toString(outputList));
2279+
}
2280+
22692281
static void stringify1()
22702282
{
22712283
const char code_c[] = "#include \"A.h\"\n"
@@ -2891,6 +2903,7 @@ int main(int argc, char **argv)
28912903
TEST_CASE(readfile_cpp14_number);
28922904
TEST_CASE(readfile_unhandled_chars);
28932905
TEST_CASE(readfile_error);
2906+
TEST_CASE(readfile_file_not_found);
28942907

28952908
TEST_CASE(stringify1);
28962909

0 commit comments

Comments
 (0)