Skip to content

Commit e040047

Browse files
authored
improved prerequisite checks for __has_include handling (#286)
1 parent 3678cd1 commit e040047

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

simplecpp.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ namespace simplecpp {
19801980
if (tok->next->str() == "(")
19811981
++paren;
19821982
else if (tok->next->str() == ")")
1983-
--paren;
1983+
--paren;
19841984
if (paren == 0)
19851985
return tok->next->next;
19861986
tok = tok->next;
@@ -2614,11 +2614,20 @@ static void simplifySizeof(simplecpp::TokenList &expr, const std::map<std::strin
26142614
}
26152615

26162616
/** Evaluate __has_include(file) */
2617+
static bool isCpp17OrLater(const simplecpp::DUI &dui)
2618+
{
2619+
const std::string std_ver = simplecpp::getCppStdString(dui.std);
2620+
return !std_ver.empty() && (std_ver >= "201703L");
2621+
}
2622+
26172623
static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
26182624
static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI &dui)
26192625
{
2626+
if (!isCpp17OrLater(dui))
2627+
return;
2628+
26202629
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
2621-
if (tok->str() != "__has_include")
2630+
if (tok->str() != HAS_INCLUDE)
26222631
continue;
26232632
simplecpp::Token *tok1 = tok->next;
26242633
if (!tok1) {
@@ -3277,7 +3286,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
32773286
// 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
32783287
std::vector<std::string> dummy;
32793288

3280-
const bool hasInclude = (dui.std.size() == 5 && dui.std.compare(0,3,"c++") == 0 && dui.std >= "c++17");
3289+
const bool hasInclude = isCpp17OrLater(dui);
32813290
MacroMap macros;
32823291
for (std::list<std::string>::const_iterator it = dui.defines.begin(); it != dui.defines.end(); ++it) {
32833292
const std::string &macrostr = *it;

0 commit comments

Comments
 (0)