Skip to content

Commit c1f3688

Browse files
authored
Fix danmar#454: Accept __has_include for GNU C standards (danmar#456)
1 parent 0ff0149 commit c1f3688

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

simplecpp.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,13 +2673,16 @@ static void simplifySizeof(simplecpp::TokenList &expr, const std::map<std::strin
26732673
}
26742674
}
26752675

2676-
/** Evaluate __has_include(file) */
26772676
static bool isCpp17OrLater(const simplecpp::DUI &dui)
26782677
{
26792678
const std::string std_ver = simplecpp::getCppStdString(dui.std);
26802679
return !std_ver.empty() && (std_ver >= "201703L");
26812680
}
26822681

2682+
static bool isGnu(const simplecpp::DUI &dui)
2683+
{
2684+
return dui.std.rfind("gnu", 0) != std::string::npos;
2685+
}
26832686

26842687
static std::string currentDirectoryOSCalc() {
26852688
const std::size_t size = 4096;
@@ -2752,7 +2755,7 @@ static std::string extractRelativePathFromAbsolute(const std::string& absoluteSi
27522755
static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
27532756
static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI &dui)
27542757
{
2755-
if (!isCpp17OrLater(dui))
2758+
if (!isCpp17OrLater(dui) && !isGnu(dui))
27562759
return;
27572760

27582761
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
@@ -3475,7 +3478,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34753478
// use a dummy vector for the macros because as this is not part of the file and would add an empty entry - e.g. /usr/include/poll.h
34763479
std::vector<std::string> dummy;
34773480

3478-
const bool hasInclude = isCpp17OrLater(dui);
3481+
const bool hasInclude = isCpp17OrLater(dui) || isGnu(dui);
34793482
MacroMap macros;
34803483
for (std::list<std::string>::const_iterator it = dui.defines.begin(); it != dui.defines.end(); ++it) {
34813484
const std::string &macrostr = *it;

test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,21 @@ static void has_include_5()
16021602
ASSERT_EQUALS("", preprocess(code));
16031603
}
16041604

1605+
static void has_include_6()
1606+
{
1607+
const char code[] = "#if defined( __has_include)\n"
1608+
" #if !__has_include(<testsuite/unrealFileName2.abcdef>)\n"
1609+
" A\n"
1610+
" #else\n"
1611+
" B\n"
1612+
" #endif\n"
1613+
"#endif";
1614+
simplecpp::DUI dui;
1615+
dui.std = "gnu99";
1616+
ASSERT_EQUALS("\n\nA", preprocess(code, dui));
1617+
ASSERT_EQUALS("", preprocess(code));
1618+
}
1619+
16051620
static void ifdef1()
16061621
{
16071622
const char code[] = "#ifdef A\n"
@@ -3164,6 +3179,7 @@ int main(int argc, char **argv)
31643179
TEST_CASE(has_include_3);
31653180
TEST_CASE(has_include_4);
31663181
TEST_CASE(has_include_5);
3182+
TEST_CASE(has_include_6);
31673183

31683184
TEST_CASE(ifdef1);
31693185
TEST_CASE(ifdef2);

0 commit comments

Comments
 (0)