Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 31 additions & 24 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2249,33 +2249,40 @@ namespace simplecpp {
}

if (tok->str() == DEFINED) {
const Token * const tok2 = tok->next;
const Token * const tok3 = tok2 ? tok2->next : nullptr;
const Token * const tok4 = tok3 ? tok3->next : nullptr;
const Token *defToken = nullptr;
const Token *lastToken = nullptr;
if (sameline(tok, tok4) && tok2->op == '(' && tok3->name && tok4->op == ')') {
defToken = tok3;
lastToken = tok4;
} else if (sameline(tok,tok2) && tok2->name) {
defToken = lastToken = tok2;
std::string macroName;
TokenList temp(files);
const Token *argTok = tok->next;
bool expectParen = false;
if (argTok->op == '(') {
argTok = argTok->next;
expectParen = true;
}
if (defToken) {
std::string macroName = defToken->str();
if (defToken->next && defToken->next->op == '#' && defToken->next->next && defToken->next->next->op == '#' && defToken->next->next->next && defToken->next->next->next->name && sameline(defToken,defToken->next->next->next)) {
TokenList temp(files);
if (expandArg(temp, defToken, parametertokens))
macroName = temp.cback()->str();
if (expandArg(temp, defToken->next->next->next, parametertokens))
macroName += temp.cback() ? temp.cback()->str() : "";
else
macroName += defToken->next->next->next->str();
lastToken = defToken->next->next->next;
while (argTok->name) {
if (expandArg(temp, argTok, parametertokens))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that we should expand macros unless they are operands. will this work:

#define x
#if defined(x)

the macroName will be empty string here now? and we will think that the condition is false?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works, it takes a different execution path.

macroName += temp.cback() ? temp.cback()->str() : "";
else
macroName += argTok->str();
argTok = argTok->next;
if (!sameline(tok, argTok))
break;
if (argTok->str() == "#") {
if (!argTok->next || argTok->next->str() != "#")
throw Error(argTok->location, "Expected '#'");
argTok = argTok->next->next;
Comment thread
ludviggunne marked this conversation as resolved.
if (!sameline(tok, argTok) || !argTok->name)
throw Error(argTok->location, "Expected name");
} else {
break;
}
const bool def = (macros.find(macroName) != macros.end());
output.push_back(newMacroToken(def ? "1" : "0", loc, true));
return lastToken->next;
}
if (expectParen) {
if (!sameline(tok, argTok) && argTok->op != ')')
throw Error(argTok->location, "Expected ')'");
argTok = argTok->next;
}
const bool def = (macros.find(macroName) != macros.end());
output.push_back(newMacroToken(def ? "1" : "0", loc, true));
return argTok;
}

output.push_back(newMacroToken(tok->str(), loc, true, tok));
Expand Down
12 changes: 12 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,17 @@ static void ifDefinedHashHash2()
ASSERT_EQUALS("\n0", preprocess(code, &outputList));
}

static void ifDefinedHashHash3()
{
const char code[] = "#define ENABLED(x) (defined(x ## _ENABLED) && (x ## _ENABLED))\n"
"#define FOO_ENABLED 1\n"
"#if ENABLED(FOO)\n"
"int x;\n"
"#endif\n";
simplecpp::OutputList outputList;
ASSERT_EQUALS("\n\n\nint x ;", preprocess(code, &outputList));
}

static void ifLogical()
{
const char code[] = "#if defined(A) || defined(B)\n"
Expand Down Expand Up @@ -4701,6 +4712,7 @@ static void runTests(int argc, char **argv, Input input)
TEST_CASE(ifDefinedInvalid2);
TEST_CASE(ifDefinedHashHash);
TEST_CASE(ifDefinedHashHash2);
TEST_CASE(ifDefinedHashHash3);
TEST_CASE(ifLogical);
TEST_CASE(ifSizeof);
TEST_CASE(elif);
Expand Down
Loading