diff --git a/main.cpp b/main.cpp index 9e9c4b1d..ecbe062f 100644 --- a/main.cpp +++ b/main.cpp @@ -166,6 +166,9 @@ int main(int argc, char **argv) case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND: std::cerr << "explicit include not found: "; break; + case simplecpp::Output::FILE_NOT_FOUND: + std::cerr << "file not found: "; + break; } std::cerr << output.msg << std::endl; } diff --git a/simplecpp.cpp b/simplecpp.cpp index e8636aaa..d9fd0087 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -385,7 +385,10 @@ class FileStream : public simplecpp::TokenList::Stream { , lastCh(0) , lastStatus(0) { - assert(file != nullptr); + if (!file) { + const std::vector location; + throw simplecpp::Output(location, simplecpp::Output::FILE_NOT_FOUND, "File is missing: " + filename); + } init(); } @@ -442,8 +445,15 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector &fi simplecpp::TokenList::TokenList(const std::string &filename, std::vector &filenames, OutputList *outputList) : frontToken(nullptr), backToken(nullptr), files(filenames) { - FileStream stream(filename); - readfile(stream,filename,outputList); + try + { + FileStream stream(filename); + readfile(stream,filename,outputList); + } + catch(const simplecpp::Output & e) // TODO handle extra type of errors + { + outputList->push_back(e); + } } simplecpp::TokenList::TokenList(const TokenList &other) : frontToken(nullptr), backToken(nullptr), files(other.files) diff --git a/simplecpp.h b/simplecpp.h index e5745a1a..5ad44a09 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -180,8 +180,10 @@ namespace simplecpp { SYNTAX_ERROR, PORTABILITY_BACKSLASH, UNHANDLED_CHAR_ERROR, - EXPLICIT_INCLUDE_NOT_FOUND + EXPLICIT_INCLUDE_NOT_FOUND, + FILE_NOT_FOUND } type; + explicit Output(const std::vector &files, Output::Type id, const std::string & errMsg ) : type(id), location(files), msg(errMsg) {} Location location; std::string msg; }; diff --git a/test.cpp b/test.cpp index 2bbf861d..9588441c 100644 --- a/test.cpp +++ b/test.cpp @@ -150,6 +150,10 @@ static std::string toString(const simplecpp::OutputList &outputList) break; case simplecpp::Output::Type::EXPLICIT_INCLUDE_NOT_FOUND: ostr << "explicit_include_not_found,"; + break; + case simplecpp::Output::Type::FILE_NOT_FOUND: + ostr << "file_not_found,"; + break; } ostr << output.msg << '\n'; @@ -2266,6 +2270,14 @@ static void readfile_error() "X",readfile("#if !A\n#error\n#endif\nX\n")); } +static void readfile_file_not_found() +{ + simplecpp::OutputList outputList; + std::vector files; + (void)simplecpp::TokenList("NotAFile", files, &outputList); + ASSERT_EQUALS("file0,1,file_not_found,File is missing: NotAFile\n", toString(outputList)); +} + static void stringify1() { const char code_c[] = "#include \"A.h\"\n" @@ -2891,6 +2903,7 @@ int main(int argc, char **argv) TEST_CASE(readfile_cpp14_number); TEST_CASE(readfile_unhandled_chars); TEST_CASE(readfile_error); + TEST_CASE(readfile_file_not_found); TEST_CASE(stringify1);