Skip to content

Commit c7e9974

Browse files
authored
fix danmar#459: Set __STRICT_ANSI__=1 for non-gnu standards (danmar#460)
1 parent f566848 commit c7e9974

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

simplecpp.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3482,11 +3482,14 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34823482

34833483
const bool hasInclude = isCpp17OrLater(dui) || isGnu(dui);
34843484
MacroMap macros;
3485+
bool strictAnsiDefined = false;
34853486
for (std::list<std::string>::const_iterator it = dui.defines.begin(); it != dui.defines.end(); ++it) {
34863487
const std::string &macrostr = *it;
34873488
const std::string::size_type eq = macrostr.find('=');
34883489
const std::string::size_type par = macrostr.find('(');
34893490
const std::string macroname = macrostr.substr(0, std::min(eq,par));
3491+
if (macroname == "__STRICT_ANSI__")
3492+
strictAnsiDefined = true;
34903493
if (dui.undefined.find(macroname) != dui.undefined.end())
34913494
continue;
34923495
const std::string lhs(macrostr.substr(0,eq));
@@ -3495,6 +3498,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34953498
macros.insert(std::pair<TokenString,Macro>(macro.name(), macro));
34963499
}
34973500

3501+
const bool strictAnsiUndefined = dui.undefined.find("__STRICT_ANSI__") != dui.undefined.cend();
3502+
if (!isGnu(dui) && !strictAnsiDefined && !strictAnsiUndefined)
3503+
macros.insert(std::pair<TokenString, Macro>("__STRICT_ANSI__", Macro("__STRICT_ANSI__", "1", dummy)));
3504+
34983505
macros.insert(std::make_pair("__FILE__", Macro("__FILE__", "__FILE__", dummy)));
34993506
macros.insert(std::make_pair("__LINE__", Macro("__LINE__", "__LINE__", dummy)));
35003507
macros.insert(std::make_pair("__COUNTER__", Macro("__COUNTER__", "__COUNTER__", dummy)));

test.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,48 @@ static void has_include_6()
16171617
ASSERT_EQUALS("", preprocess(code));
16181618
}
16191619

1620+
static void strict_ansi_1()
1621+
{
1622+
const char code[] = "#if __STRICT_ANSI__\n"
1623+
" A\n"
1624+
"#endif";
1625+
simplecpp::DUI dui;
1626+
dui.std = "gnu99";
1627+
ASSERT_EQUALS("", preprocess(code, dui));
1628+
}
1629+
1630+
static void strict_ansi_2()
1631+
{
1632+
const char code[] = "#if __STRICT_ANSI__\n"
1633+
" A\n"
1634+
"#endif";
1635+
simplecpp::DUI dui;
1636+
dui.std = "c99";
1637+
ASSERT_EQUALS("\nA", preprocess(code, dui));
1638+
}
1639+
1640+
static void strict_ansi_3()
1641+
{
1642+
const char code[] = "#if __STRICT_ANSI__\n"
1643+
" A\n"
1644+
"#endif";
1645+
simplecpp::DUI dui;
1646+
dui.std = "c99";
1647+
dui.undefined.insert("__STRICT_ANSI__");
1648+
ASSERT_EQUALS("", preprocess(code, dui));
1649+
}
1650+
1651+
static void strict_ansi_4()
1652+
{
1653+
const char code[] = "#if __STRICT_ANSI__\n"
1654+
" A\n"
1655+
"#endif";
1656+
simplecpp::DUI dui;
1657+
dui.std = "gnu99";
1658+
dui.defines.push_back("__STRICT_ANSI__");
1659+
ASSERT_EQUALS("\nA", preprocess(code, dui));
1660+
}
1661+
16201662
static void ifdef1()
16211663
{
16221664
const char code[] = "#ifdef A\n"
@@ -3190,6 +3232,11 @@ int main(int argc, char **argv)
31903232
TEST_CASE(has_include_5);
31913233
TEST_CASE(has_include_6);
31923234

3235+
TEST_CASE(strict_ansi_1);
3236+
TEST_CASE(strict_ansi_2);
3237+
TEST_CASE(strict_ansi_3);
3238+
TEST_CASE(strict_ansi_4);
3239+
31933240
TEST_CASE(ifdef1);
31943241
TEST_CASE(ifdef2);
31953242
TEST_CASE(ifndef);

0 commit comments

Comments
 (0)