From a6ff3681bbd1fc753fe55d0636178e75808322e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 18 Sep 2010 22:20:01 +0200 Subject: [PATCH] Fixed #1802 (Preprocessor: macros are expanded wrong) --- lib/preprocessor.cpp | 13 ++++++++++++- test/testpreprocessor.cpp | 10 +++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index d20e43572..c26475fdb 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1894,7 +1894,7 @@ public: * @param macrocode output string * @return true if the expanding was successful */ - bool code(const std::vector ¶ms2, const std::map macros, std::string ¯ocode) const + bool code(const std::vector ¶ms2, const std::map ¯os, std::string ¯ocode) const { if (_nopar || (_params.empty() && _variadic)) { @@ -2008,6 +2008,17 @@ public: break; } } + + // expand nopar macro + const std::map::const_iterator it = macros.find(str); + if (it != macros.end() && it->second->_macro.find("(") == std::string::npos) + { + str = it->second->_macro; + if (str.find(" ") != std::string::npos) + str.erase(0, str.find(" ")); + else + str = ""; + } } if (_variadic && tok->str() == "," && tok->next() && tok->next()->str() == "##") { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 1941363d8..49a54f1db 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -1411,7 +1411,7 @@ private: const char filedata[] = "#define A 4\n" "#define B(a) a,A\n" "B(2);\n"; - ASSERT_EQUALS("\n\n2,4;\n", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\n\n2, 4;\n", OurPreprocessor::expandMacros(filedata)); } { @@ -1642,6 +1642,14 @@ private: "#define ab(A,B) AB(A,B)\n" "ab(a,AB(b,c))\n"; ASSERT_EQUALS("\n\nabc\n", OurPreprocessor::expandMacros(filedata5)); + + // Ticket #1802 + const char filedata6[] = "#define AB_(A,B) A ## B\n" + "#define AB(A,B) AB_(A,B)\n" + "#define ab(suf) AB(X, AB_(_, suf))\n" + "#define X x\n" + "ab(y)\n"; + ASSERT_EQUALS("\n\n\n\nx_y\n", OurPreprocessor::expandMacros(filedata6)); }