From 5ce5c7ab63bae0cc4bf1dc9b84748895df0053aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 4 Dec 2008 07:34:31 +0000 Subject: [PATCH] Preprocessor: Minor refactoring and improved testing --- preprocessor.cpp | 30 ++++++++++++++++++------------ preprocessor.h | 4 ++++ testpreprocessor.cpp | 16 +++++++++++++++- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/preprocessor.cpp b/preprocessor.cpp index 1d91b861a..b87030dcf 100644 --- a/preprocessor.cpp +++ b/preprocessor.cpp @@ -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 &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::mapreportErr( oss.str() ); - result.clear(); - return; + return ""; } if ( ch == '\n' ) @@ -142,9 +138,19 @@ void Preprocessor::preprocess(std::istream &istr, std::map &result, const std::string &filename) +{ + std::string codestr( read(istr, filename) ); // Replace all tabs with spaces.. std::string::size_type loc = 0; diff --git a/preprocessor.h b/preprocessor.h index 3c74ee499..a631c58fa 100644 --- a/preprocessor.h +++ b/preprocessor.h @@ -32,6 +32,10 @@ class Preprocessor public: Preprocessor( ErrorLogger *errorLogger ); void preprocess(std::istream &istr, std::map &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 diff --git a/testpreprocessor.cpp b/testpreprocessor.cpp index 3a7e5243c..f12360770 100644 --- a/testpreprocessor.cpp +++ b/testpreprocessor.cpp @@ -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 &m1, const std::map &m2) {