From 29b2be19ab5de01da7842991a23758462fae31b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 25 Jul 2010 15:19:25 +0200 Subject: [PATCH] Fixed #1383 (Preprocessor: define - ifndef problem) --- lib/preprocessor.cpp | 10 ++++++---- test/testpreprocessor.cpp | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 6aab5bc6a..e7dfdeb2b 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -712,7 +712,7 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const continue; } - if (line.compare(0, 8, "#define ") == 0 && line.find("(", 8) == std::string::npos) + if (line.compare(0, 8, "#define ") == 0) { if (line.find(" ", 8) == std::string::npos) defines.insert(line.substr(8)); @@ -1219,13 +1219,15 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg, std::string def = getdef(line, true); std::string ndef = getdef(line, false); - if (line.compare(0, 8, "#define ") == 0 && line.find("(", 8) == std::string::npos) + if (line.compare(0, 8, "#define ") == 0) { - std::string::size_type pos = line.find(" ", 8); + std::string::size_type pos = line.find_first_of(" (", 8); if (pos == std::string::npos) cfgmap[line.substr(8)] = ""; - else + else if (line[pos] == ' ') cfgmap[line.substr(8, pos - 8)] = line.substr(pos + 1); + else + cfgmap[line.substr(8, pos - 8)] = ""; } else if (line.find("#elif ") == 0) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index cd20f4719..3d10dde98 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -178,6 +178,7 @@ private: // define and then ifdef TEST_CASE(define_ifdef); + TEST_CASE(define_ifndef); TEST_CASE(endfile); TEST_CASE(redundant_config); @@ -2145,6 +2146,25 @@ private: } } + void define_ifndef() + { + const char filedata[] = "#define A(x) (x)\n" + "#ifndef A\n" + ";\n" + "#endif\n"; + + // Preprocess => actual result.. + std::istringstream istr(filedata); + std::map actual; + Preprocessor preprocessor; + preprocessor.preprocess(istr, actual, "file.c"); + + // Compare results.. + ASSERT_EQUALS("\n\n\n\n", actual[""]); + TODO_ASSERT_EQUALS(1, actual.size()); + ASSERT_EQUALS(2, actual.size()); + } + void redundant_config() { const char filedata[] = "int main() {\n"