From 76cb2310af6be6e53d4778595ea963e923023a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 21 Jan 2009 17:11:24 +0000 Subject: [PATCH] preprocessor: handled problem with parsing strings when expanding macros --- src/preprocessor.cpp | 20 ++++++++++++++++++++ test/testpreprocessor.cpp | 12 +++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index ec88cf421..54303fe71 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -729,6 +729,26 @@ std::string Preprocessor::expandMacros(std::string code) break; } } + else if (code[pos2] == '\"' || code[pos2] == '\'') + { + par += code[pos2]; + char ch = code[pos2]; + ++pos2; + while (pos2 < code.length() && code[pos2] != ch) + { + par += code[pos2]; + if (code[pos2] == '\\') + { + par += code[pos2]; + ++pos2; + } + ++pos2; + } + if (pos2 == code.length()) + break; + par += code[pos2]; + continue; + } if (parlevel == 1 && code[pos2] == ',') { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index ed3002fe7..39a37d3eb 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -74,6 +74,7 @@ private: TEST_CASE(macro_simple4); TEST_CASE(macro_simple5); TEST_CASE(macro_simple6); + TEST_CASE(macro_simple7); TEST_CASE(macro_mismatch); TEST_CASE(string1); TEST_CASE(string2); @@ -479,6 +480,13 @@ private: ASSERT_EQUALS("\n(a+b+c)", Preprocessor::expandMacros(filedata)); } + void macro_simple7() + { + const char filedata[] = "#define ABC(str) str\n" + "ABC(\"(\")"; + ASSERT_EQUALS("\n\"(\"", Preprocessor::expandMacros(filedata)); + } + void macro_mismatch() { const char filedata[] = "#define AAA(aa,bb) f(aa)\n" @@ -564,11 +572,13 @@ private: void fmt() { - const char filedata[] = "#define DBG(fmt...) printf(fmt);\n" + const char filedata[] = "#define DBG(fmt...) printf(fmt)\n" "DBG(\"[0x%lx-0x%lx)\", pstart, pend);"; // Preprocess.. std::string actual = Preprocessor::expandMacros(filedata); + + ASSERT_EQUALS("\nprintf(\"[0x%lx-0x%lx)\", pstart, pend);", actual); } };