From 96bea619bfb46d38bb0977f9010863118933a81f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 26 Oct 2008 16:29:12 +0000 Subject: [PATCH] preprocessor: Remove comments --- preprocessor.cpp | 78 ++++++++++++++++++++++++++++++++++++++++---- testpreprocessor.cpp | 19 +++++++++++ 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/preprocessor.cpp b/preprocessor.cpp index 93becaf13..5440f11f3 100644 --- a/preprocessor.cpp +++ b/preprocessor.cpp @@ -41,19 +41,85 @@ static std::string getcode(const std::string &filedata, std::string cfg); */ void preprocess(std::istream &istr, std::map &result) { - std::ostringstream ostr; - std::string line; - while ( getline(istr, line) ) - ostr << line << "\n"; + // Get filedata from stream.. remove all comments + std::ostringstream code; + for (char ch = (char)istr.get(); !istr.eof(); ch = (char)istr.get()) + { + 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.. - std::list cfgs = getcfgs( ostr.str() ); + std::list cfgs = getcfgs( codestr ); // Extract the code for each possible configuration.. result.clear(); for ( std::list::const_iterator it = cfgs.begin(); it != cfgs.end(); ++it ) { - result[ *it ] = getcode( ostr.str(), *it ); + result[ *it ] = getcode( codestr, *it ); } } diff --git a/testpreprocessor.cpp b/testpreprocessor.cpp index 61f33f012..40d1a0916 100644 --- a/testpreprocessor.cpp +++ b/testpreprocessor.cpp @@ -39,6 +39,8 @@ private: { TEST_CASE( test1 ); TEST_CASE( test2 ); + + TEST_CASE( comments1 ); } void check(const char filedata[], const std::map &expected) @@ -54,7 +56,11 @@ private: if ( it2 == expected.end() ) assertFail(__FILE__, __LINE__); else + { + std::string s1 = it->second; + std::string s2 = it2->second; ASSERT_EQUALS( it->second, it2->second ); + } } } @@ -87,6 +93,19 @@ private: check( filedata, expected ); } + + void comments1() + { + const char filedata[] = "/*\n" + "#ifdef WIN32\n" + "#endif\n" + "*/\n"; + + std::map expected; + expected[""] = "\n\n\n\n"; + check( filedata, expected ); + } + }; REGISTER_TEST( TestPreprocessor )