Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3480,11 +3480,14 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL

const bool hasInclude = isCpp17OrLater(dui) || isGnu(dui);
MacroMap macros;
bool strictAnsiDefined = false;
for (std::list<std::string>::const_iterator it = dui.defines.begin(); it != dui.defines.end(); ++it) {
const std::string &macrostr = *it;
const std::string::size_type eq = macrostr.find('=');
const std::string::size_type par = macrostr.find('(');
const std::string macroname = macrostr.substr(0, std::min(eq,par));
if (macroname == "__STRICT_ANSI__")
strictAnsiDefined = true;
if (dui.undefined.find(macroname) != dui.undefined.end())
continue;
const std::string lhs(macrostr.substr(0,eq));
Expand All @@ -3493,6 +3496,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
macros.insert(std::pair<TokenString,Macro>(macro.name(), macro));
}

const bool strictAnsiUndefined = dui.undefined.find("__STRICT_ANSI__") != dui.undefined.cend();
if (!isGnu(dui) && !strictAnsiDefined && !strictAnsiUndefined)
macros.insert(std::pair<TokenString, Macro>("__STRICT_ANSI__", Macro("__STRICT_ANSI__", "1", dummy)));

macros.insert(std::make_pair("__FILE__", Macro("__FILE__", "__FILE__", dummy)));
macros.insert(std::make_pair("__LINE__", Macro("__LINE__", "__LINE__", dummy)));
macros.insert(std::make_pair("__COUNTER__", Macro("__COUNTER__", "__COUNTER__", dummy)));
Expand Down
47 changes: 47 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,48 @@ static void has_include_6()
ASSERT_EQUALS("", preprocess(code));
}

static void strict_ansi_1()
{
const char code[] = "#if __STRICT_ANSI__\n"
" A\n"
"#endif";
simplecpp::DUI dui;
dui.std = "gnu99";
ASSERT_EQUALS("", preprocess(code, dui));
}

static void strict_ansi_2()
{
const char code[] = "#if __STRICT_ANSI__\n"
" A\n"
"#endif";
simplecpp::DUI dui;
dui.std = "c99";
ASSERT_EQUALS("\nA", preprocess(code, dui));
}

static void strict_ansi_3()
{
const char code[] = "#if __STRICT_ANSI__\n"
" A\n"
"#endif";
simplecpp::DUI dui;
dui.std = "c99";
dui.undefined.insert("__STRICT_ANSI__");
ASSERT_EQUALS("", preprocess(code, dui));
}

static void strict_ansi_4()
{
const char code[] = "#if __STRICT_ANSI__\n"
" A\n"
"#endif";
simplecpp::DUI dui;
dui.std = "gnu99";
dui.defines.push_back("__STRICT_ANSI__");
ASSERT_EQUALS("\nA", preprocess(code, dui));
}

static void ifdef1()
{
const char code[] = "#ifdef A\n"
Expand Down Expand Up @@ -3181,6 +3223,11 @@ int main(int argc, char **argv)
TEST_CASE(has_include_5);
TEST_CASE(has_include_6);

TEST_CASE(strict_ansi_1);
TEST_CASE(strict_ansi_2);
TEST_CASE(strict_ansi_3);
TEST_CASE(strict_ansi_4);

TEST_CASE(ifdef1);
TEST_CASE(ifdef2);
TEST_CASE(ifndef);
Expand Down
Loading