Skip to content

Commit

Permalink
Macro expansion is passing argument values along correctly now.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 11, 2017
1 parent 20bf034 commit 2dabb9b
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions libs/parser/CodeTokeniser.h
Expand Up @@ -679,11 +679,6 @@ class CodeTokeniser :

if (found != _macros.end())
{
if (found->second.name == "MM_TITLE_CAMPAIGN")
{
int i = 6;
}

// Expand this macro, new tokens are acquired from the currently active tokeniser
StringList expanded = expandMacro(found->second, [this]() { return (*_curNode)->tokeniser.nextToken(); });

Expand Down Expand Up @@ -741,13 +736,16 @@ class CodeTokeniser :
// Insert the macro contents into the buffer, expanding sub-macros along the way
for (StringList::const_iterator t = macro.tokens.begin(); t != macro.tokens.end(); ++t)
{
// Replace macro variable with its value
std::string token = getMacroToken(*t, macro, argumentValues);

// check if this is matching a preprocessor definition
Macros::const_iterator found = _macros.find(*t);
Macros::const_iterator found = _macros.find(token);

if (found == _macros.end())
{
// Not a macro
expandedTokens.push_back(*t);
expandedTokens.push_back(token);
continue;
}

Expand All @@ -756,11 +754,12 @@ class CodeTokeniser :
{
if (t == macro.tokens.end())
{
throw ParseException(fmt::format("Running out of tokens expanding sub-macro {0}", *t));
throw ParseException(fmt::format("Running out of tokens expanding sub-macro {0}", token));
}

// Advance iterator and dereference
return *(++t);
// When delivering tokens to sub-macro expansions we still need to replace any argument values
return getMacroToken(*(++t), macro, argumentValues);
});

if (!subMacro.empty())
Expand All @@ -778,6 +777,21 @@ class CodeTokeniser :
return expandedTokens;
}

std::string getMacroToken(std::string token, const Macro& macro, const StringList& argumentValues)
{
// Check if the current token is referring to a macro argument and replace it with its value
for (StringList::const_iterator arg = macro.arguments.begin(), val = argumentValues.begin();
arg != macro.arguments.end() && val != argumentValues.end(); ++arg, ++val)
{
if (token == *arg)
{
return *val;
}
}

return token; // leave token unchanged
}

void handlePreprocessorToken(const std::string& token)
{
if (token == "#include")
Expand Down

0 comments on commit 2dabb9b

Please sign in to comment.