diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index b3e5f3865..6b504ed09 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -411,6 +411,37 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set if (isUndefined(config, undefined)) config.clear(); + bool ifndef = false; + if (cmdtok->str() == "ifndef") + ifndef = true; + else { + const std::vector match{"if", "!", "defined", "(", config, ")"}; + int i = 0; + ifndef = true; + for (const simplecpp::Token *t = cmdtok; i < match.size(); t = t->next) { + if (!t || t->str() != match[i++]) { + ifndef = false; + break; + } + } + } + + // include guard.. + if (ifndef && tok->location.fileIndex > 0) { + bool includeGuard = true; + for (const simplecpp::Token *t = tok->previous; t; t = t->previous) { + if (t->location.fileIndex == tok->location.fileIndex) { + includeGuard = false; + break; + } + } + if (includeGuard) { + configs_if.push_back(std::string()); + configs_ifndef.push_back(std::string()); + continue; + } + } + configs_if.push_back((cmdtok->str() == "ifndef") ? std::string() : config); configs_ifndef.push_back((cmdtok->str() == "ifndef") ? config : std::string()); ret.insert(cfg(configs_if,userDefines)); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index a90f85a3b..bdc7fdb41 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -221,6 +221,7 @@ private: TEST_CASE(getConfigs7e); TEST_CASE(getConfigs8); // #if A==1 => cfg: A=1 TEST_CASE(getConfigs10); // #5139 + TEST_CASE(getConfigs11); // #9832 - include guards TEST_CASE(getConfigsError); TEST_CASE(getConfigsD1); @@ -2134,6 +2135,16 @@ private: ASSERT_EQUALS("\n", getConfigsStr(filedata)); } + void getConfigs11() { // #9832 - include guards + const char filedata[] = "#file \"test.h\"\n" + "#if !defined(test_h)\n" + "#define test_h\n" + "123\n" + "#endif\n" + "#endfile\n"; + ASSERT_EQUALS("\n", getConfigsStr(filedata)); + } + void getConfigsError() { const char filedata1[] = "#ifndef X\n" "#error \"!X\"\n"