diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index 5803bda4e..d911e565e 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -1356,12 +1356,12 @@ public: std::string Preprocessor::expandMacros(std::string code, const std::string &filename, ErrorLogger *errorLogger) { // Search for macros and expand them.. - std::string::size_type defpos = 0; - while ((defpos = code.find("#define ", defpos)) != std::string::npos) + std::string::size_type defpos = std::string::npos; + while ((defpos > 0) && ((defpos = code.rfind("#define ", defpos)) != std::string::npos)) { if (defpos > 0 && code[defpos-1] != '\n') { - defpos += 6; + defpos--; continue; } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 6687f2453..a9edb4137 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -114,6 +114,7 @@ private: TEST_CASE(macro_simple8); TEST_CASE(macro_simple9); TEST_CASE(macro_simple10); + TEST_CASE(macroInMacro); TEST_CASE(macro_mismatch); TEST_CASE(macro_linenumbers); TEST_CASE(macro_nopar); @@ -840,6 +841,14 @@ private: ASSERT_EQUALS("\nunsigned long x;", OurPreprocessor::expandMacros(filedata)); } + void macroInMacro() + { + const char filedata[] = "#define A(m) long n = m; n++;\n" + "#define B(n) A(n)\n" + "B(0)"; + ASSERT_EQUALS("\n\nlong n=0;n++;", OurPreprocessor::expandMacros(filedata)); + } + void macro_mismatch() { const char filedata[] = "#define AAA(aa,bb) f(aa)\n"