Skip to content

Commit 7eb95aa

Browse files
committed
Preprocessor::getConfigs: Handle #error in '#if !A' better
1 parent 87bc667 commit 7eb95aa

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/preprocessor.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,14 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
375375
configs.push_back(configs_ifndef.back());
376376
ret.insert(cfg(configs, userDefines));
377377
}
378+
if (!configs_if.empty() && !configs_if.back().empty()) {
379+
const std::string &last = configs_if.back();
380+
if (last.size() > 2U && last.compare(last.size()-2U,2,"=0") == 0) {
381+
std::vector<std::string> configs(configs_if);
382+
configs[configs.size() - 1U] = last.substr(0,last.size()-2U);
383+
ret.insert(cfg(configs, userDefines));
384+
}
385+
}
378386
} else if (cmdtok->str == "define" && sameline(tok, cmdtok->next) && cmdtok->next->name) {
379387
defined.insert(cmdtok->next->str);
380388
}

test/testpreprocessor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class TestPreprocessor : public TestFixture {
7373
TEST_CASE(Bug2190219);
7474

7575
TEST_CASE(error1); // #error => don't extract any code
76+
TEST_CASE(error2); // #error if symbol is not defined
7677
TEST_CASE(error3);
7778
TEST_CASE(error4); // #2919 - wrong filename is reported
7879
TEST_CASE(error5);
@@ -319,6 +320,18 @@ class TestPreprocessor : public TestFixture {
319320
ASSERT_EQUALS("\nA\n", getConfigsStr(filedata));
320321
}
321322

323+
void error2() {
324+
const char filedata1[] = "#ifndef A\n"
325+
"#error\n"
326+
"#endif\n";
327+
TODO_ASSERT_EQUALS("A\n", "\nA\n", getConfigsStr(filedata1));
328+
329+
const char filedata2[] = "#if !A\n"
330+
"#error\n"
331+
"#endif\n";
332+
TODO_ASSERT_EQUALS("A\n", "\nA\nA=0\n", getConfigsStr(filedata2));
333+
}
334+
322335
void error3() {
323336
errout.str("");
324337
Settings settings;

0 commit comments

Comments
 (0)