Preprocessor: Minor refactoring and improved testing

This commit is contained in:
Daniel Marjamäki 2008-12-04 07:34:31 +00:00
parent 5eb653911e
commit 5ce5c7ab63
3 changed files with 37 additions and 13 deletions

View File

@ -32,17 +32,14 @@ Preprocessor::Preprocessor( ErrorLogger *errorLogger )
{
_errorLogger = errorLogger;
}
/**
* Extract the code for each configuration
* \param istr The (file/string) stream to read from.
* \param result The map that will get the results
*/
void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename)
{
/** Just read the code into a string. Perform simple cleanup of the code */
std::string Preprocessor::read(std::istream &istr, const std::string &filename)
{
// Get filedata from stream..
bool ignoreSpace = true;
// For the error report
int lineno = 1;
std::ostringstream code;
@ -54,8 +51,7 @@ void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::str
std::ostringstream oss;
oss << "[" << filename << ":" << lineno << "] Bad character found: " << int((unsigned char)ch) << std::endl;
_errorLogger->reportErr( oss.str() );
result.clear();
return;
return "";
}
if ( ch == '\n' )
@ -142,9 +138,19 @@ void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::str
{
code << std::string(1, ch);
}
}
}
return code.str();
}
std::string codestr( code.str() );
/**
* Extract the code for each configuration
* \param istr The (file/string) stream to read from.
* \param result The map that will get the results
*/
void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename)
{
std::string codestr( read(istr, filename) );
// Replace all tabs with spaces..
std::string::size_type loc = 0;

View File

@ -32,6 +32,10 @@ class Preprocessor
public:
Preprocessor( ErrorLogger *errorLogger );
void preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename);
/** Just read the code into a string. Perform simple cleanup of the code */
std::string read(std::istream &istr, const std::string &filename);
private:
/**
* Get preprocessed code for a given configuration

View File

@ -37,10 +37,12 @@ private:
void run()
{
// Just read the code into a string. Perform simple cleanup of the code
TEST_CASE(readCode);
// The bug that started the whole work with the new preprocessor
TEST_CASE( Bug2190219 );
TEST_CASE( test1 );
TEST_CASE( test2 );
TEST_CASE( test3 );
@ -60,6 +62,18 @@ private:
TEST_CASE( multiline );
}
void readCode()
{
const char code[] = " \t a //\n"
" #aa\t /* remove this */\tb \r\n";
Preprocessor p(NULL);
std::istringstream istr(code);
std::string codestr( p.read(istr,"") );
ASSERT_EQUALS( "a \n#aa b \n", codestr );
}
bool cmpmaps(const std::map<std::string, std::string> &m1, const std::map<std::string, std::string> &m2)
{