From 0eb4e3032ab21bb50bc59f62c53f8086f6bd94e8 Mon Sep 17 00:00:00 2001 From: Daniel Marjamaki Date: Mon, 24 Oct 2011 19:51:00 +0200 Subject: [PATCH] Preprocessor: handle '#undef' better. Ticket: #2131 --- lib/preprocessor.cpp | 4 ++++ test/testpreprocessor.cpp | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 24fba4648..660ae270b 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1798,6 +1798,10 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str } } + else if (line.compare(0,7,"#undef ") == 0) { + defs.erase(line.substr(7)); + } + else { ostr << line; } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 37c192c9f..d580af178 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -2912,6 +2912,22 @@ private: ASSERT_EQUALS("\n\n\n\n\n\n\n4\n\n", actual); } } + + // #undef + { + const std::string code("#ifndef X\n" + "#define X\n" + "123\n" + "#endif\n"); + + defs.clear(); + const std::string actual1(preprocessor.handleIncludes(code,filePath,includePaths,defs)); + + defs.clear(); + const std::string actual(preprocessor.handleIncludes(code + "#undef X\n" + code, filePath, includePaths, defs)); + + ASSERT_EQUALS(actual1 + "\n" + actual1, actual); + } } };