diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index b315766f8e6..0ba8f2d2800 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -659,6 +659,19 @@ std::set Preprocessor::getConfigs() const std::set defined = { "__cplusplus" }; + // Insert library defines + for (const auto &define : mSettings.library.defines()) { + + const std::string::size_type paren = define.find("("); + const std::string::size_type space = define.find(" "); + std::string::size_type end = space; + + if (paren != std::string::npos && paren < space) + end = paren; + + defined.insert(define.substr(0, end)); + } + ::getConfigs(mTokens, defined, mSettings.userDefines, mSettings.userUndefs, ret); for (const auto &filedata : mFileCache) { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index ea0863eff99..b0b51e9d02c 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -311,6 +311,8 @@ class TestPreprocessor : public TestFixture { TEST_CASE(getConfigs8); // #if A==1 => cfg: A=1 TEST_CASE(getConfigs10); // #5139 TEST_CASE(getConfigs11); // #9832 - include guards + TEST_CASE(getConfigs12); // #14222 + TEST_CASE(getConfigs13); // #14222 TEST_CASE(getConfigsError); TEST_CASE(getConfigsD1); @@ -357,12 +359,14 @@ class TestPreprocessor : public TestFixture { } template - std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr) { + std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr, const char *library = nullptr) { Settings settings; if (arg && std::strncmp(arg,"-D",2)==0) settings.userDefines = arg + 2; if (arg && std::strncmp(arg,"-U",2)==0) settings.userUndefs.insert(arg+2); + if (library) + ASSERT(settings.library.load("", library, false).errorcode == Library::ErrorCode::OK); std::vector files; // TODO: this adds an empty filename simplecpp::TokenList tokens(code,files); @@ -2264,6 +2268,20 @@ class TestPreprocessor : public TestFixture { ASSERT_EQUALS("\n", getConfigsStr(filedata)); } + void getConfigs12() { // #14222 + const char filedata[] = "#ifdef INT8_MAX\n" + "INT8_MAX\n" + "#endif\n"; + ASSERT_EQUALS("\n", getConfigsStr(filedata, nullptr, "std.cfg")); + } + + void getConfigs13() { // #14222 + const char filedata[] = "#ifdef __builtin_bswap16\n" + "__builtin_bswap16(x);\n" + "#endif\n"; + ASSERT_EQUALS("\n", getConfigsStr(filedata, nullptr, "gnu.cfg")); + } + void getConfigsError() { const char filedata1[] = "#ifndef X\n" "#error \"!X\"\n"