preprocessor: Fixed bug when expanding macros without parameters
This commit is contained in:
parent
ecdfee850d
commit
da881fdd0a
@ -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"
|
// 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);
|
code.erase(defpos, endpos - defpos);
|
||||||
|
|
||||||
// Tokenize the macro to make it easier to handle
|
// Tokenize the macro to make it easier to handle
|
||||||
@ -484,7 +484,7 @@ std::string Preprocessor::expandMacros(std::string code)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// The char after the macroname must not be alphanumeric or '_'
|
// 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();
|
std::string::size_type pos2 = pos1 + macroname.length();
|
||||||
if (isalnum(code[pos2]) || code[pos2] == '_')
|
if (isalnum(code[pos2]) || code[pos2] == '_')
|
||||||
@ -538,33 +538,45 @@ std::string Preprocessor::expandMacros(std::string code)
|
|||||||
|
|
||||||
// Create macro code..
|
// Create macro code..
|
||||||
std::string macrocode;
|
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() != ")")
|
while (tok && tok->str() != ")")
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
}
|
while ((tok = tok->next()) != NULL)
|
||||||
while ((tok = tok->next()) != NULL)
|
|
||||||
{
|
|
||||||
std::string str = tok->str();
|
|
||||||
if (tok->isName())
|
|
||||||
{
|
{
|
||||||
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];
|
if (str == macroparams[i])
|
||||||
break;
|
{
|
||||||
|
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..
|
// Insert macro code..
|
||||||
if ( !macroparams.empty() )
|
if (!macroparams.empty())
|
||||||
++pos2;
|
++pos2;
|
||||||
code.erase(pos1, pos2 - pos1);
|
code.erase(pos1, pos2 - pos1);
|
||||||
code.insert(pos1, macrocode);
|
code.insert(pos1, macrocode);
|
||||||
|
@ -71,6 +71,7 @@ private:
|
|||||||
TEST_CASE(macro_simple1);
|
TEST_CASE(macro_simple1);
|
||||||
TEST_CASE(macro_simple2);
|
TEST_CASE(macro_simple2);
|
||||||
TEST_CASE(macro_simple3);
|
TEST_CASE(macro_simple3);
|
||||||
|
TEST_CASE(macro_simple4);
|
||||||
TEST_CASE(macro_mismatch);
|
TEST_CASE(macro_mismatch);
|
||||||
TEST_CASE(preprocessor_inside_string);
|
TEST_CASE(preprocessor_inside_string);
|
||||||
}
|
}
|
||||||
@ -450,6 +451,13 @@ private:
|
|||||||
ASSERT_EQUALS("\n4 AA\n", Preprocessor::expandMacros(filedata));
|
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()
|
void macro_mismatch()
|
||||||
{
|
{
|
||||||
const char filedata[] = "#define AAA(aa,bb) f(aa)\n"
|
const char filedata[] = "#define AAA(aa,bb) f(aa)\n"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user