Skip to content

Commit

Permalink
Fix #334 Do not assert when source file is missing (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
olabetskyi authored Feb 2, 2024
1 parent e096d6b commit ea78f9a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 13 additions & 3 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,10 @@ class FileStream : public simplecpp::TokenList::Stream {
, lastCh(0)
, lastStatus(0)
{
assert(file != nullptr);
if (!file) {
const std::vector<std::string> location;
throw simplecpp::Output(location, simplecpp::Output::FILE_NOT_FOUND, "File is missing: " + filename);
}
init();
}

Expand Down Expand Up @@ -442,8 +445,15 @@ simplecpp::TokenList::TokenList(std::istream &istr, std::vector<std::string> &fi
simplecpp::TokenList::TokenList(const std::string &filename, std::vector<std::string> &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)
Expand Down
4 changes: 3 additions & 1 deletion simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> &files, Output::Type id, const std::string & errMsg ) : type(id), location(files), msg(errMsg) {}
Location location;
std::string msg;
};
Expand Down
13 changes: 13 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<std::string> 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"
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit ea78f9a

Please sign in to comment.