Skip to content

Commit

Permalink
fixed #365 - do not treat standards with empty define string as unkno…
Browse files Browse the repository at this point in the history
…wn (#367)
  • Loading branch information
firewave authored Sep 9, 2024
1 parent 7b88c13 commit 2c66d08
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
16 changes: 10 additions & 6 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3360,12 +3360,14 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
macros.insert(std::make_pair("__TIME__", Macro("__TIME__", getTimeDefine(&ltime), dummy)));

if (!dui.std.empty()) {
std::string std_def = simplecpp::getCStdString(dui.std);
if (!std_def.empty()) {
macros.insert(std::make_pair("__STDC_VERSION__", Macro("__STDC_VERSION__", std_def, dummy)));
const cstd_t c_std = simplecpp::getCStd(dui.std);
if (c_std != CUnknown) {
const std::string std_def = simplecpp::getCStdString(c_std);
if (!std_def.empty())
macros.insert(std::make_pair("__STDC_VERSION__", Macro("__STDC_VERSION__", std_def, dummy)));
} else {
std_def = simplecpp::getCppStdString(dui.std);
if (std_def.empty()) {
const cppstd_t cpp_std = simplecpp::getCppStd(dui.std);
if (cpp_std == CPPUnknown) {
if (outputList) {
simplecpp::Output err(files);
err.type = Output::DUI_ERROR;
Expand All @@ -3375,7 +3377,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
output.clear();
return;
}
macros.insert(std::make_pair("__cplusplus", Macro("__cplusplus", std_def, dummy)));
const std::string std_def = simplecpp::getCppStdString(cpp_std);
if (!std_def.empty())
macros.insert(std::make_pair("__cplusplus", Macro("__cplusplus", std_def, dummy)));
}
}

Expand Down
28 changes: 28 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,33 @@ static void stdEnum()
ASSERT_EQUALS(simplecpp::cppstd_t::CPPUnknown, simplecpp::getCppStd("c11"));
}

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

dui.std = "c89";
ASSERT_EQUALS("", preprocess(code, dui, &outputList));
ASSERT_EQUALS(0, outputList.size());
outputList.clear();

dui.std = "gnu23";
ASSERT_EQUALS("", preprocess(code, dui, &outputList));
ASSERT_EQUALS(0, outputList.size());
outputList.clear();

dui.std = "c++03";
ASSERT_EQUALS("", preprocess(code, dui, &outputList));
ASSERT_EQUALS(0, outputList.size());
outputList.clear();

dui.std = "gnu++26";
ASSERT_EQUALS("", preprocess(code, dui, &outputList));
ASSERT_EQUALS(0, outputList.size());
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 @@ -3048,6 +3075,7 @@ int main(int argc, char **argv)
TEST_CASE(cpluscplusDefine);
TEST_CASE(invalidStd);
TEST_CASE(stdEnum);
TEST_CASE(stdValid);

TEST_CASE(token);

Expand Down

0 comments on commit 2c66d08

Please sign in to comment.