Skip to content

Commit 2c47ced

Browse files
authored
fix #14222: Preprocessor: should use library macros when extracting macros (danmar#7992)
1 parent 843ce7c commit 2c47ced

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/preprocessor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,19 @@ std::set<std::string> Preprocessor::getConfigs() const
660660

661661
std::set<std::string> defined = { "__cplusplus" };
662662

663+
// Insert library defines
664+
for (const auto &define : mSettings.library.defines()) {
665+
666+
const std::string::size_type paren = define.find("(");
667+
const std::string::size_type space = define.find(" ");
668+
std::string::size_type end = space;
669+
670+
if (paren != std::string::npos && paren < space)
671+
end = paren;
672+
673+
defined.insert(define.substr(0, end));
674+
}
675+
663676
::getConfigs(mTokens, defined, mSettings.userDefines, mSettings.userUndefs, ret);
664677

665678
for (const auto &filedata : mFileCache) {

test/testpreprocessor.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ class TestPreprocessor : public TestFixture {
311311
TEST_CASE(getConfigs8); // #if A==1 => cfg: A=1
312312
TEST_CASE(getConfigs10); // #5139
313313
TEST_CASE(getConfigs11); // #9832 - include guards
314+
TEST_CASE(getConfigs12); // #14222
315+
TEST_CASE(getConfigs13); // #14222
314316
TEST_CASE(getConfigsError);
315317

316318
TEST_CASE(getConfigsD1);
@@ -357,12 +359,14 @@ class TestPreprocessor : public TestFixture {
357359
}
358360

359361
template<size_t size>
360-
std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr) {
362+
std::string getConfigsStr(const char (&code)[size], const char *arg = nullptr, const char *library = nullptr) {
361363
Settings settings;
362364
if (arg && std::strncmp(arg,"-D",2)==0)
363365
settings.userDefines = arg + 2;
364366
if (arg && std::strncmp(arg,"-U",2)==0)
365367
settings.userUndefs.insert(arg+2);
368+
if (library)
369+
ASSERT(settings.library.load("", library, false).errorcode == Library::ErrorCode::OK);
366370
std::vector<std::string> files;
367371
// TODO: this adds an empty filename
368372
simplecpp::TokenList tokens(code,files);
@@ -2264,6 +2268,20 @@ class TestPreprocessor : public TestFixture {
22642268
ASSERT_EQUALS("\n", getConfigsStr(filedata));
22652269
}
22662270

2271+
void getConfigs12() { // #14222
2272+
const char filedata[] = "#ifdef INT8_MAX\n"
2273+
"INT8_MAX\n"
2274+
"#endif\n";
2275+
ASSERT_EQUALS("\n", getConfigsStr(filedata, nullptr, "std.cfg"));
2276+
}
2277+
2278+
void getConfigs13() { // #14222
2279+
const char filedata[] = "#ifdef __builtin_bswap16\n"
2280+
"__builtin_bswap16(x);\n"
2281+
"#endif\n";
2282+
ASSERT_EQUALS("\n", getConfigsStr(filedata, nullptr, "gnu.cfg"));
2283+
}
2284+
22672285
void getConfigsError() {
22682286
const char filedata1[] = "#ifndef X\n"
22692287
"#error \"!X\"\n"

0 commit comments

Comments
 (0)