Preprocessor: Improved handling of multiline macros

This commit is contained in:
Daniel Marjamäki 2009-01-06 08:49:54 +00:00
parent f0f42563e6
commit dc4497b250
2 changed files with 25 additions and 7 deletions

View File

@ -428,9 +428,18 @@ std::string Preprocessor::expandMacros(std::string code)
break;
}
const std::string macro(code.substr(defpos + 8, endpos - defpos - 7));
// Extract the whole macro into a separate variable "macro" and then erase it from "code"
std::string macro(code.substr(defpos + 8, endpos - defpos - 7));
code.erase(defpos, endpos - defpos);
// Remove "\\\n" from the macro
while (macro.find("\\\n") != std::string::npos)
{
macro.erase(macro.find("\\\n"), 2);
code.insert(defpos, "\n");
++defpos;
}
// Tokenize the macro to make it easier to handle
Tokenizer tokenizer;
std::istringstream istr(macro.c_str());

View File

@ -66,9 +66,10 @@ private:
TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA"
// Macros..
TEST_CASE(macro1);
TEST_CASE(macro2);
TEST_CASE(macro3);
TEST_CASE(macro_simple1);
TEST_CASE(macro_simple2);
TEST_CASE(macro_mismatch);
TEST_CASE(macro_multiline);
}
@ -463,27 +464,35 @@ private:
}
void macro1()
void macro_simple1()
{
const char filedata[] = "#define AAA(aa) f(aa)\n"
"AAA(5);\n";
ASSERT_EQUALS("\nf(5);\n", Preprocessor::expandMacros(filedata));
}
void macro2()
void macro_simple2()
{
const char filedata[] = "#define min(x,y) x<y?x:y\n"
"min(a(),b());\n";
ASSERT_EQUALS("\na()<b()?a():b();\n", Preprocessor::expandMacros(filedata));
}
void macro3()
void macro_mismatch()
{
const char filedata[] = "#define AAA(aa,bb) f(aa)\n"
"AAA(5);\n";
ASSERT_EQUALS("\nAAA(5);\n", Preprocessor::expandMacros(filedata));
}
void macro_multiline()
{
const char filedata[] = "#define sqr(aa) aa * \\\n"
" aa\n"
"sqr(5);\n";
ASSERT_EQUALS("\n\n5*5;\n", Preprocessor::expandMacros(filedata));
}
};