preprocessor: Fixed bug when expanding macros without parameters

This commit is contained in:
Daniel Marjamäki 2009-01-11 15:51:46 +00:00
parent ecdfee850d
commit da881fdd0a
2 changed files with 37 additions and 17 deletions

View File

@ -453,7 +453,7 @@ std::string Preprocessor::expandMacros(std::string code)
}
// 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));
const std::string macro(code.substr(defpos + 8, endpos - defpos - 7));
code.erase(defpos, endpos - defpos);
// Tokenize the macro to make it easier to handle
@ -484,7 +484,7 @@ std::string Preprocessor::expandMacros(std::string code)
continue;
// The char after the macroname must not be alphanumeric or '_'
if ( pos1 + macroname.length() < code.length() )
if (pos1 + macroname.length() < code.length())
{
std::string::size_type pos2 = pos1 + macroname.length();
if (isalnum(code[pos2]) || code[pos2] == '_')
@ -538,33 +538,45 @@ std::string Preprocessor::expandMacros(std::string code)
// Create macro code..
std::string macrocode;
const Token *tok = tokenizer.tokens();
if (! macroparams.empty())
if (macroparams.empty())
{
std::string::size_type pos = macro.find(" ");
if (pos == std::string::npos)
macrocode = "";
else
{
macrocode = macro.substr(pos + 1);
if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos)
macrocode.erase(pos);
}
}
else
{
const Token *tok = tokenizer.tokens();
while (tok && tok->str() != ")")
tok = tok->next();
}
while ((tok = tok->next()) != NULL)
{
std::string str = tok->str();
if (tok->isName())
while ((tok = tok->next()) != NULL)
{
for (unsigned int i = 0; i < macroparams.size(); ++i)
std::string str = tok->str();
if (tok->isName())
{
if (str == macroparams[i])
for (unsigned int i = 0; i < macroparams.size(); ++i)
{
str = params[i];
break;
if (str == macroparams[i])
{
str = params[i];
break;
}
}
}
macrocode += str;
if (Token::Match(tok, "%type% %var%"))
macrocode += " ";
}
macrocode += str;
if (Token::Match(tok, "%type% %var%"))
macrocode += " ";
}
// Insert macro code..
if ( !macroparams.empty() )
if (!macroparams.empty())
++pos2;
code.erase(pos1, pos2 - pos1);
code.insert(pos1, macrocode);

View File

@ -71,6 +71,7 @@ private:
TEST_CASE(macro_simple1);
TEST_CASE(macro_simple2);
TEST_CASE(macro_simple3);
TEST_CASE(macro_simple4);
TEST_CASE(macro_mismatch);
TEST_CASE(preprocessor_inside_string);
}
@ -450,6 +451,13 @@ private:
ASSERT_EQUALS("\n4 AA\n", Preprocessor::expandMacros(filedata));
}
void macro_simple4()
{
const char filedata[] = "#define TEMP_1 if( temp > 0 ) return 1;\n"
"TEMP_1\n";
ASSERT_EQUALS("\nif( temp > 0 ) return 1;\n", Preprocessor::expandMacros(filedata));
}
void macro_mismatch()
{
const char filedata[] = "#define AAA(aa,bb) f(aa)\n"