From 661f13e0facb9195061ba6bce18e8f9603a8585a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 1 Jul 2025 15:43:04 +0200 Subject: [PATCH 1/2] fix #454 --- main.cpp | 1 + simplecpp.cpp | 4 ++-- simplecpp.h | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 424ef6fa..baf006ee 100644 --- a/main.cpp +++ b/main.cpp @@ -60,6 +60,7 @@ int main(int argc, char **argv) case 's': if (std::strncmp(arg, "-std=",5)==0) { dui.std = arg + 5; + dui.gnu = dui.std.rfind("gnu", 0) != std::string::npos; found = true; } break; diff --git a/simplecpp.cpp b/simplecpp.cpp index 599ffdfe..bb076c10 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -2760,7 +2760,7 @@ static std::string extractRelativePathFromAbsolute(const std::string& absoluteSi static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader); static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI &dui) { - if (!isCpp17OrLater(dui)) + if (!isCpp17OrLater(dui) && !dui.gnu) return; for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) { @@ -3483,7 +3483,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL // 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 std::vector dummy; - const bool hasInclude = isCpp17OrLater(dui); + const bool hasInclude = isCpp17OrLater(dui) || dui.gnu; MacroMap macros; for (std::list::const_iterator it = dui.defines.begin(); it != dui.defines.end(); ++it) { const std::string ¯ostr = *it; diff --git a/simplecpp.h b/simplecpp.h index f5c69593..0d199977 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -337,12 +337,13 @@ namespace simplecpp { * On the command line these are configured by -D, -U, -I, --include, -std */ struct SIMPLECPP_LIB DUI { - DUI() : clearIncludeCache(false), removeComments(false) {} + DUI() : clearIncludeCache(false), removeComments(false), gnu(false) {} std::list defines; std::set undefined; std::list includePaths; std::list includes; std::string std; + bool gnu; bool clearIncludeCache; bool removeComments; /** remove comment tokens from included files */ }; From 332ce916180e72d1a0b1edcf1b8cc0fe19ace331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Wed, 2 Jul 2025 09:23:35 +0200 Subject: [PATCH 2/2] add test --- test.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test.cpp b/test.cpp index cec253b8..81d1e46f 100644 --- a/test.cpp +++ b/test.cpp @@ -1563,6 +1563,22 @@ static void has_include_5() ASSERT_EQUALS("", preprocess(code)); } +static void has_include_6() +{ + const char code[] = "#if defined( __has_include)\n" + " #if !__has_include()\n" + " A\n" + " #else\n" + " B\n" + " #endif\n" + "#endif"; + simplecpp::DUI dui; + dui.std = "gnu99"; + dui.gnu = true; + ASSERT_EQUALS("\n\nA", preprocess(code, dui)); + ASSERT_EQUALS("", preprocess(code)); +} + static void ifdef1() { const char code[] = "#ifdef A\n" @@ -3123,6 +3139,7 @@ int main(int argc, char **argv) TEST_CASE(has_include_3); TEST_CASE(has_include_4); TEST_CASE(has_include_5); + TEST_CASE(has_include_6); TEST_CASE(ifdef1); TEST_CASE(ifdef2);