Skip to content

Commit 06c7bf0

Browse files
authored
Fix #688: #ifdef and #ifndef directives missing from ifCond (#689)
1 parent f6947eb commit 06c7bf0

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

simplecpp.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,11 +3740,23 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
37403740
conditionIsTrue = false;
37413741
}
37423742
else if (rawtok->str() == IFDEF) {
3743-
conditionIsTrue = (macros.find(rawtok->next->str()) != macros.end() || (hasInclude && rawtok->next->str() == HAS_INCLUDE));
3744-
maybeUsedMacros[rawtok->next->str()].emplace_back(rawtok->next->location);
3743+
const std::string &name = rawtok->next->str();
3744+
conditionIsTrue = (macros.find(name) != macros.end() || (hasInclude && name == HAS_INCLUDE));
3745+
maybeUsedMacros[name].emplace_back(rawtok->next->location);
3746+
if (ifCond) {
3747+
const std::string E = conditionIsTrue ? "1" : "0";
3748+
const long long result = conditionIsTrue ? 1 : 0;
3749+
ifCond->emplace_back(rawtok->location, E, result);
3750+
}
37453751
} else if (rawtok->str() == IFNDEF) {
3746-
conditionIsTrue = (macros.find(rawtok->next->str()) == macros.end() && !(hasInclude && rawtok->next->str() == HAS_INCLUDE));
3747-
maybeUsedMacros[rawtok->next->str()].emplace_back(rawtok->next->location);
3752+
const std::string &name = rawtok->next->str();
3753+
conditionIsTrue = (macros.find(name) == macros.end() && !(hasInclude && name == HAS_INCLUDE));
3754+
maybeUsedMacros[name].emplace_back(rawtok->next->location);
3755+
if (ifCond) {
3756+
const std::string E = conditionIsTrue ? "1" : "0";
3757+
const long long result = conditionIsTrue ? 1 : 0;
3758+
ifCond->emplace_back(rawtok->location, E, result);
3759+
}
37483760
} else { /*if (rawtok->str() == IF || rawtok->str() == ELIF)*/
37493761
TokenList expr(files);
37503762
for (const Token *tok = rawtok->next; tok && tok->location.sameline(rawtok->location); tok = tok->next) {

test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4026,6 +4026,27 @@ static void ifCond()
40264026
ASSERT_EQUALS("0", it->E);
40274027
ASSERT_EQUALS(0, it->result);
40284028
}
4029+
{
4030+
const char code[] = "#ifdef NOTDEFINED\n"
4031+
"#endif\n"
4032+
"#ifndef NOTDEFINED\n"
4033+
"#endif\n";
4034+
std::list<simplecpp::IfCond> ifCond;
4035+
ASSERT_EQUALS("", preprocess(code, &ifCond));
4036+
ASSERT_EQUALS(2, ifCond.size());
4037+
auto it = ifCond.cbegin();
4038+
ASSERT_EQUALS(0, it->location.fileIndex);
4039+
ASSERT_EQUALS(1, it->location.line);
4040+
ASSERT_EQUALS(2, it->location.col);
4041+
ASSERT_EQUALS("0", it->E);
4042+
ASSERT_EQUALS(0, it->result);
4043+
++it;
4044+
ASSERT_EQUALS(0, it->location.fileIndex);
4045+
ASSERT_EQUALS(3, it->location.line);
4046+
ASSERT_EQUALS(2, it->location.col);
4047+
ASSERT_EQUALS("1", it->E);
4048+
ASSERT_EQUALS(1, it->result);
4049+
}
40294050
}
40304051

40314052
static void macroUsage()

0 commit comments

Comments
 (0)