Skip to content

Commit 8321b48

Browse files
committed
Test-implementation of replace ifdef/if defined macros with itself and for only if with 1.
* simplecpp.cpp: find ifdef/if defined macros ("_~") and set as itself else 1. * preprocessor.cpp: Add "_~" in the end of the macro when ifdef or if defined.
1 parent a965059 commit 8321b48

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

externals/simplecpp/simplecpp.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,13 +3333,23 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33333333
const std::string &macrostr = *it;
33343334
const std::string::size_type eq = macrostr.find('=');
33353335
const std::string::size_type par = macrostr.find('(');
3336+
const std::string::size_type useMName = macrostr.find("_~");
33363337
const std::string macroname = macrostr.substr(0, std::min(eq,par));
33373338
if (macroname == "__STRICT_ANSI__")
33383339
strictAnsiDefined = true;
33393340
if (dui.undefined.find(macroname) != dui.undefined.end())
33403341
continue;
3341-
const std::string lhs(macrostr.substr(0,eq));
3342-
const std::string rhs(eq == std::string::npos ? lhs : macrostr.substr(eq + 1));
3342+
std::string lhs;
3343+
std::string rhs;
3344+
if (useMName != std::string::npos) {
3345+
//Define macro as itself or to specified value
3346+
lhs.assign(macrostr.substr(0, useMName));
3347+
rhs.assign(eq == std::string::npos ? lhs : macrostr.substr(eq + 1));
3348+
} else {
3349+
//Define macro as 1 or to specified value
3350+
lhs.assign(macrostr.substr(0, eq));
3351+
rhs.assign(eq == std::string::npos ? std::string("1") : macrostr.substr(eq + 1));
3352+
}
33433353
const Macro macro(lhs, rhs, dummy);
33443354
macros.insert(std::pair<TokenString,Macro>(macro.name(), macro));
33453355
}

lib/preprocessor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
445445
if (dtok->op == '(')
446446
dtok = dtok->next;
447447
if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end())
448-
configset.insert(dtok->str());
448+
configset.insert(dtok->str() + "_~");
449449
}
450450
std::string cfgStr;
451451
for (const std::string &s : configset) {
@@ -553,6 +553,8 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
553553
const simplecpp::Token *expr1 = cmdtok->next;
554554
if (sameline(tok,expr1) && expr1->name && !sameline(tok,expr1->next))
555555
config = expr1->str();
556+
if (cmdtok->str() == "ifdef")
557+
config.append("_~"); //Tag config as a ifdef or if defined macro
556558
if (defined.find(config) != defined.end())
557559
config.clear();
558560
} else if (cmdtok->str() == "if") {

0 commit comments

Comments
 (0)