From 74951903126b913bd5fe05fc3502aeb4d69c166d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 5 Jan 2009 09:26:00 +0000 Subject: [PATCH] Preprocessor: Began work on the macro handling --- preprocessor.cpp | 34 ++++++++++++++++++++++++++++++++++ preprocessor.h | 2 ++ testpreprocessor.cpp | 16 ++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/preprocessor.cpp b/preprocessor.cpp index 878d82b04..3aa6de181 100644 --- a/preprocessor.cpp +++ b/preprocessor.cpp @@ -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 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::const_iterator it = macros.begin(); it != macros.end(); ++it) + { + const std::string ¯o(*it); + + } + + return code; +} + diff --git a/preprocessor.h b/preprocessor.h index 70735c7a2..895f279dd 100644 --- a/preprocessor.h +++ b/preprocessor.h @@ -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 ); }; //--------------------------------------------------------------------------- diff --git a/testpreprocessor.cpp b/testpreprocessor.cpp index 7961feb44..d74e20afa 100644 --- a/testpreprocessor.cpp +++ b/testpreprocessor.cpp @@ -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 )