From dc4497b2505027d684cf7ca397a57226cafd4309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 6 Jan 2009 08:49:54 +0000 Subject: [PATCH] Preprocessor: Improved handling of multiline macros --- preprocessor.cpp | 11 ++++++++++- testpreprocessor.cpp | 21 +++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/preprocessor.cpp b/preprocessor.cpp index f2abdeea3..44db33eab 100644 --- a/preprocessor.cpp +++ b/preprocessor.cpp @@ -428,9 +428,18 @@ std::string Preprocessor::expandMacros(std::string code) break; } - const std::string macro(code.substr(defpos + 8, endpos - defpos - 7)); + // Extract the whole macro into a separate variable "macro" and then erase it from "code" + std::string macro(code.substr(defpos + 8, endpos - defpos - 7)); code.erase(defpos, endpos - defpos); + // Remove "\\\n" from the macro + while (macro.find("\\\n") != std::string::npos) + { + macro.erase(macro.find("\\\n"), 2); + code.insert(defpos, "\n"); + ++defpos; + } + // Tokenize the macro to make it easier to handle Tokenizer tokenizer; std::istringstream istr(macro.c_str()); diff --git a/testpreprocessor.cpp b/testpreprocessor.cpp index ef64fb4ad..9d656c0bf 100644 --- a/testpreprocessor.cpp +++ b/testpreprocessor.cpp @@ -66,9 +66,10 @@ private: TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA" // Macros.. - TEST_CASE(macro1); - TEST_CASE(macro2); - TEST_CASE(macro3); + TEST_CASE(macro_simple1); + TEST_CASE(macro_simple2); + TEST_CASE(macro_mismatch); + TEST_CASE(macro_multiline); } @@ -463,27 +464,35 @@ private: } - void macro1() + void macro_simple1() { const char filedata[] = "#define AAA(aa) f(aa)\n" "AAA(5);\n"; ASSERT_EQUALS("\nf(5);\n", Preprocessor::expandMacros(filedata)); } - void macro2() + void macro_simple2() { const char filedata[] = "#define min(x,y) x