Skip to content

Commit

Permalink
bail out on unknown DUI::std value
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed May 6, 2024
1 parent e040047 commit eee3b41
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
3 changes: 3 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ int main(int argc, char **argv)
case simplecpp::Output::FILE_NOT_FOUND:
std::cerr << "file not found: ";
break;
case simplecpp::Output::DUI_ERROR:
std::cerr << "dui error: ";
break;
}
std::cerr << output.msg << std::endl;
}
Expand Down
13 changes: 11 additions & 2 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3315,8 +3315,17 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
macros.insert(std::make_pair("__STDC_VERSION__", Macro("__STDC_VERSION__", std_def, dummy)));
} else {
std_def = simplecpp::getCppStdString(dui.std);
if (!std_def.empty())
macros.insert(std::make_pair("__cplusplus", Macro("__cplusplus", std_def, dummy)));
if (std_def.empty()) {
if (outputList) {
simplecpp::Output err(files);
err.type = Output::DUI_ERROR;
err.msg = "unknown standard specified: '" + dui.std + "'";
outputList->push_back(err);
}
output.clear();
return;
}
macros.insert(std::make_pair("__cplusplus", Macro("__cplusplus", std_def, dummy)));
}
}

Expand Down
3 changes: 2 additions & 1 deletion simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ namespace simplecpp {
PORTABILITY_BACKSLASH,
UNHANDLED_CHAR_ERROR,
EXPLICIT_INCLUDE_NOT_FOUND,
FILE_NOT_FOUND
FILE_NOT_FOUND,
DUI_ERROR
} type;
explicit Output(const std::vector<std::string>& files, Type type, const std::string& msg) : type(type), location(files), msg(msg) {}
Location location;
Expand Down
46 changes: 46 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ static std::string toString(const simplecpp::OutputList &outputList)
case simplecpp::Output::Type::FILE_NOT_FOUND:
ostr << "file_not_found,";
break;
case simplecpp::Output::Type::DUI_ERROR:
ostr << "dui_error,";
break;
}

ostr << output.msg << '\n';
Expand Down Expand Up @@ -2656,6 +2659,48 @@ static void cpluscplusDefine()
ASSERT_EQUALS("\n201103L", preprocess(code, dui));
}

static void invalidStd()
{
const char code[] = "";
simplecpp::DUI dui;
simplecpp::OutputList outputList;

dui.std = "c88";
ASSERT_EQUALS("", preprocess(code, dui, &outputList));
ASSERT_EQUALS(1, outputList.size());
ASSERT_EQUALS(simplecpp::Output::Type::DUI_ERROR, outputList.cbegin()->type);
ASSERT_EQUALS("unknown standard specified: 'c88'", outputList.cbegin()->msg);
outputList.clear();

dui.std = "gnu88";
ASSERT_EQUALS("", preprocess(code, dui, &outputList));
ASSERT_EQUALS(1, outputList.size());
ASSERT_EQUALS(simplecpp::Output::Type::DUI_ERROR, outputList.cbegin()->type);
ASSERT_EQUALS("unknown standard specified: 'gnu88'", outputList.cbegin()->msg);
outputList.clear();

dui.std = "d99";
ASSERT_EQUALS("", preprocess(code, dui, &outputList));
ASSERT_EQUALS(1, outputList.size());
ASSERT_EQUALS(simplecpp::Output::Type::DUI_ERROR, outputList.cbegin()->type);
ASSERT_EQUALS("unknown standard specified: 'd99'", outputList.cbegin()->msg);
outputList.clear();

dui.std = "c++77";
ASSERT_EQUALS("", preprocess(code, dui, &outputList));
ASSERT_EQUALS(1, outputList.size());
ASSERT_EQUALS(simplecpp::Output::Type::DUI_ERROR, outputList.cbegin()->type);
ASSERT_EQUALS("unknown standard specified: 'c++77'", outputList.cbegin()->msg);
outputList.clear();

dui.std = "gnu++33";
ASSERT_EQUALS("", preprocess(code, dui, &outputList));
ASSERT_EQUALS(1, outputList.size());
ASSERT_EQUALS(simplecpp::Output::Type::DUI_ERROR, outputList.cbegin()->type);
ASSERT_EQUALS("unknown standard specified: 'gnu++33'", outputList.cbegin()->msg);
outputList.clear();
}

static void assertToken(const std::string& s, bool name, bool number, bool comment, char op, int line)
{
const std::vector<std::string> f;
Expand Down Expand Up @@ -2984,6 +3029,7 @@ int main(int argc, char **argv)

TEST_CASE(stdcVersionDefine);
TEST_CASE(cpluscplusDefine);
TEST_CASE(invalidStd);

TEST_CASE(token);

Expand Down

0 comments on commit eee3b41

Please sign in to comment.