preprocessor: expand macro without parameters

This commit is contained in:
Daniel Marjamäki 2009-01-11 15:07:13 +00:00
parent da46e4bd96
commit ecdfee850d
2 changed files with 19 additions and 1 deletions

View File

@ -483,6 +483,14 @@ std::string Preprocessor::expandMacros(std::string code)
if (pos1 != 0 && (isalnum(code[pos1-1]) || code[pos1-1] == '_')) if (pos1 != 0 && (isalnum(code[pos1-1]) || code[pos1-1] == '_'))
continue; continue;
// The char after the macroname must not be alphanumeric or '_'
if ( pos1 + macroname.length() < code.length() )
{
std::string::size_type pos2 = pos1 + macroname.length();
if (isalnum(code[pos2]) || code[pos2] == '_')
continue;
}
std::vector<std::string> params; std::vector<std::string> params;
std::string::size_type pos2 = pos1 + macroname.length(); std::string::size_type pos2 = pos1 + macroname.length();
if (pos2 >= macro.length()) if (pos2 >= macro.length())
@ -556,7 +564,9 @@ std::string Preprocessor::expandMacros(std::string code)
} }
// Insert macro code.. // Insert macro code..
code.erase(pos1, pos2 + 1 - pos1); if ( !macroparams.empty() )
++pos2;
code.erase(pos1, pos2 - pos1);
code.insert(pos1, macrocode); code.insert(pos1, macrocode);
pos1 += macrocode.length(); pos1 += macrocode.length();
} }

View File

@ -70,6 +70,7 @@ private:
// Macros.. // Macros..
TEST_CASE(macro_simple1); TEST_CASE(macro_simple1);
TEST_CASE(macro_simple2); TEST_CASE(macro_simple2);
TEST_CASE(macro_simple3);
TEST_CASE(macro_mismatch); TEST_CASE(macro_mismatch);
TEST_CASE(preprocessor_inside_string); TEST_CASE(preprocessor_inside_string);
} }
@ -442,6 +443,13 @@ private:
ASSERT_EQUALS("\na()<b()?a():b();\n", Preprocessor::expandMacros(filedata)); ASSERT_EQUALS("\na()<b()?a():b();\n", Preprocessor::expandMacros(filedata));
} }
void macro_simple3()
{
const char filedata[] = "#define A 4\n"
"A AA\n";
ASSERT_EQUALS("\n4 AA\n", Preprocessor::expandMacros(filedata));
}
void macro_mismatch() void macro_mismatch()
{ {
const char filedata[] = "#define AAA(aa,bb) f(aa)\n" const char filedata[] = "#define AAA(aa,bb) f(aa)\n"