preprocessor: Remove comments

This commit is contained in:
Daniel Marjamäki 2008-10-26 16:29:12 +00:00
parent 6390873a09
commit 96bea619bf
2 changed files with 91 additions and 6 deletions

View File

@ -41,19 +41,85 @@ static std::string getcode(const std::string &filedata, std::string cfg);
*/ */
void preprocess(std::istream &istr, std::map<std::string, std::string> &result) void preprocess(std::istream &istr, std::map<std::string, std::string> &result)
{ {
std::ostringstream ostr; // Get filedata from stream.. remove all comments
std::string line; std::ostringstream code;
while ( getline(istr, line) ) for (char ch = (char)istr.get(); !istr.eof(); ch = (char)istr.get())
ostr << line << "\n"; {
if ( ch == '/' )
{
char chNext = (char)istr.get();
if ( chNext == '/' )
{
while (!istr.eof() && ch!='\n')
ch = (char)istr.get();
code << "\n";
}
else if ( chNext == '*' )
{
char chPrev = 0;
while (!istr.eof() && (chPrev!='*' || ch!='/'))
{
chPrev = ch;
ch = (char)istr.get();
if (ch == '\n')
code << "\n";
}
}
else
{
code << std::string(1,ch) << std::string(1,chNext);
}
}
else if ( ch == '\"' )
{
do
{
code << std::string(1,ch);
ch = (char)istr.get();
if ( ch == '\\' )
{
code << "\\";
ch = (char)istr.get();
}
} while ( !istr.eof() && ch != '\"' );
code << std::string(1, ch);
}
else if ( ch == '\'' )
{
do
{
code << std::string(1, ch);
ch = (char)istr.get();
if ( ch == '\\' )
{
code << "\\";
ch = (char)istr.get();
}
} while ( !istr.eof() && ch != '\'' );
code << std::string(1, ch);
}
else
{
code << std::string(1, ch);
}
}
std::string codestr( code.str() );
// Get all possible configurations.. // Get all possible configurations..
std::list<std::string> cfgs = getcfgs( ostr.str() ); std::list<std::string> cfgs = getcfgs( codestr );
// Extract the code for each possible configuration.. // Extract the code for each possible configuration..
result.clear(); result.clear();
for ( std::list<std::string>::const_iterator it = cfgs.begin(); it != cfgs.end(); ++it ) for ( std::list<std::string>::const_iterator it = cfgs.begin(); it != cfgs.end(); ++it )
{ {
result[ *it ] = getcode( ostr.str(), *it ); result[ *it ] = getcode( codestr, *it );
} }
} }

View File

@ -39,6 +39,8 @@ private:
{ {
TEST_CASE( test1 ); TEST_CASE( test1 );
TEST_CASE( test2 ); TEST_CASE( test2 );
TEST_CASE( comments1 );
} }
void check(const char filedata[], const std::map<std::string,std::string> &expected) void check(const char filedata[], const std::map<std::string,std::string> &expected)
@ -54,7 +56,11 @@ private:
if ( it2 == expected.end() ) if ( it2 == expected.end() )
assertFail(__FILE__, __LINE__); assertFail(__FILE__, __LINE__);
else else
{
std::string s1 = it->second;
std::string s2 = it2->second;
ASSERT_EQUALS( it->second, it2->second ); ASSERT_EQUALS( it->second, it2->second );
}
} }
} }
@ -87,6 +93,19 @@ private:
check( filedata, expected ); check( filedata, expected );
} }
void comments1()
{
const char filedata[] = "/*\n"
"#ifdef WIN32\n"
"#endif\n"
"*/\n";
std::map<std::string, std::string> expected;
expected[""] = "\n\n\n\n";
check( filedata, expected );
}
}; };
REGISTER_TEST( TestPreprocessor ) REGISTER_TEST( TestPreprocessor )