From 193cffdb0bdc592bb82b478fe8e89bf2ba4351e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 23 Jan 2009 17:14:42 +0000 Subject: [PATCH] preprocessor: fixed bug with mixed macros ABC and ABCD --- src/preprocessor.cpp | 19 +++++++++++++------ test/testpreprocessor.cpp | 9 +++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index 772fbf6f1..964ed70ca 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -679,15 +679,22 @@ std::string Preprocessor::expandMacros(std::string code) // #undef => break if (code[pos1] == '#') { - std::string substr; + // Are we at a #undef or #define? + if (code.substr(pos1, 7) == "#undef ") + pos1 += 7; + else if (code.substr(pos1, 8) == "#define ") + pos1 += 8; + else + continue; - substr = code.substr(pos1, 7 + macro.name().length()); - if (substr == "#undef " + macro.name()) + // Compare the macroname with the macroname we're currently parsing (macro.name()) + // If it's the same macroname.. break. + std::string::size_type pos = pos1 + macro.name().length(); + if (pos < code.length() + && code.substr(pos1, macro.name().length()) == macro.name() + && !isalnum(code[pos]) && code[pos] != '_') break; - substr = code.substr(pos1, 8 + macro.name().length()); - if (substr == "#define " + macro.name()) - break; continue; } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index f5a551271..e8e2b19b5 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -79,6 +79,7 @@ private: TEST_CASE(macro_simple5); TEST_CASE(macro_simple6); TEST_CASE(macro_simple7); + TEST_CASE(macro_simple8); TEST_CASE(macro_mismatch); TEST_CASE(string1); TEST_CASE(string2); @@ -515,6 +516,14 @@ private: ASSERT_EQUALS("\n\"(\"", Preprocessor::expandMacros(filedata)); } + void macro_simple8() + { + const char filedata[] = "#define ABC 123\n" + "#define ABCD 1234\n" + "ABC ABCD"; + ASSERT_EQUALS("\n\n123 1234", Preprocessor::expandMacros(filedata)); + } + void macro_mismatch() { const char filedata[] = "#define AAA(aa,bb) f(aa)\n"