Preprocessor: Began work on the macro handling

This commit is contained in:
Daniel Marjamäki 2009-01-05 09:26:00 +00:00
parent 4d0cd99e97
commit 7495190312
3 changed files with 52 additions and 0 deletions

View File

@ -407,3 +407,37 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg)
return ret.str();
}
static std::string Preprocessor::expandMacros( std::string code )
{
std::list<std::string> macros;
// Get macros..
std::string::size_type defpos = 0;
while ((defpos = code.find("#define", defpos)) != std::string::npos)
{
// Get macro..
std::string::size_type endpos = code.find("\n", defpos + 6);
while (endpos != std::string::npos && code[endpos-1] == '\\')
endpos = code.find("\n", endpos + 1);
if (endpos == std::string::npos)
{
code.erase( defpos );
break;
}
macros.push_back( code.substr( defpos, endpos - defpos ) );
code.erase( defpos, endpos + 1 - defpos );
}
// Expand macros..
for (std::list<std::string>::const_iterator it = macros.begin(); it != macros.end(); ++it)
{
const std::string &macro(*it);
}
return code;
}

View File

@ -88,6 +88,8 @@ private:
static std::string getdef(std::string line, bool def);
static bool match_cfg_def( std::string cfg, const std::string &def );
static std::string expandMacros( const std::string &code );
};
//---------------------------------------------------------------------------

View File

@ -64,6 +64,9 @@ private:
TEST_CASE( multiline );
TEST_CASE( if_defined ); // "#if defined(AAA)" => "#ifdef AAA"
// Macros..
TEST_CASE( macro1 );
}
@ -458,6 +461,19 @@ private:
}
void macro1()
{
const char filedata[] = "#define AAA(aa) f(aa)\n"
"AAA(5);\n";
// Expected result..
std::string expected( "\nf(5);\n" );
// Compare result..
//ASSERT_EQUALS( expected, Preprocessor::expandMacros(filedata) );
}
};
REGISTER_TEST( TestPreprocessor )