Skip to content

Commit e72ec42

Browse files
committed
Fixed cppcheck-opensource#7842 (Preprocessor::getConfigs: #error in #ifndef not handled well)
1 parent 1039c71 commit e72ec42

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

lib/preprocessor.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
184184
return cond->str;
185185
}
186186

187+
if (len == 2 && cond->op == '!' && next1->name) {
188+
if (defined.find(next1->str) == defined.end())
189+
return next1->str + "=0";
190+
}
191+
187192
if (len == 3 && cond->op == '(' && next1->name && next2->op == ')') {
188193
if (defined.find(next1->str) == defined.end() && undefined.find(next1->str) == undefined.end())
189194
return next1->str;
@@ -375,17 +380,22 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
375380
ret.erase("");
376381
std::vector<std::string> configs(configs_if);
377382
configs.push_back(configs_ifndef.back());
378-
ret.insert(cfg(configs, userDefines));
383+
ret.erase(cfg(configs, userDefines));
384+
if (!elseError.empty())
385+
elseError += ';';
386+
elseError += cfg(configs_ifndef, userDefines);
379387
}
380388
if (!configs_if.empty() && !configs_if.back().empty()) {
381389
const std::string &last = configs_if.back();
382390
if (last.size() > 2U && last.compare(last.size()-2U,2,"=0") == 0) {
383391
std::vector<std::string> configs(configs_if);
384392
ret.erase(cfg(configs, userDefines));
385393
configs[configs.size() - 1U] = last.substr(0,last.size()-2U);
386-
ret.insert(cfg(configs, userDefines));
387394
if (configs.size() == 1U)
388395
ret.erase("");
396+
if (!elseError.empty())
397+
elseError += ';';
398+
elseError += cfg(configs, userDefines);
389399
}
390400
}
391401
} else if (cmdtok->str == "define" && sameline(tok, cmdtok->next) && cmdtok->next->name) {

test/testpreprocessor.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -379,15 +379,32 @@ class TestPreprocessor : public TestFixture {
379379
}
380380

381381
void error6() {
382-
const char filedata[] = "#ifdef A\n"
383-
"#else\n"
384-
"#error 1\n"
385-
"#endif\n"
386-
"#ifdef B\n"
387-
"#else\n"
388-
"#error 2\n"
389-
"#endif\n";
390-
ASSERT_EQUALS("\nA\nA;B\nB\n", getConfigsStr(filedata));
382+
const char filedata1[] = "#ifdef A\n"
383+
"#else\n"
384+
"#error 1\n"
385+
"#endif\n"
386+
"#ifdef B\n"
387+
"#else\n"
388+
"#error 2\n"
389+
"#endif\n";
390+
ASSERT_EQUALS("\nA\nA;B\nB\n", getConfigsStr(filedata1));
391+
392+
const char filedata2[] = "#ifndef A\n"
393+
"#error 1\n"
394+
"#endif\n"
395+
"#ifndef B\n"
396+
"#error 2\n"
397+
"#endif\n";
398+
ASSERT_EQUALS("A;B\n", getConfigsStr(filedata2));
399+
400+
const char filedata3[] = "#if !A\n"
401+
"#error 1\n"
402+
"#endif\n"
403+
"#if !B\n"
404+
"#error 2\n"
405+
"#endif\n";
406+
ASSERT_EQUALS("A;B\n", getConfigsStr(filedata3));
407+
391408
}
392409

393410
void setPlatformInfo() {
@@ -2105,7 +2122,7 @@ class TestPreprocessor : public TestFixture {
21052122
"#error \"!Y\"\n"
21062123
"#endif\n"
21072124
"#endif\n";
2108-
ASSERT_EQUALS("\nX\nX;Y\n", getConfigsStr(filedata2));
2125+
ASSERT_EQUALS("\nX\nY\n", getConfigsStr(filedata2));
21092126
}
21102127

21112128
void getConfigsD1() {

0 commit comments

Comments
 (0)