From 34fba9e1ea6e66bd5b59bc62dd9b9d9e6920887c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 13 Dec 2011 21:14:41 +0100 Subject: [PATCH] Fixed #3405 ((error) Invalid number of character ({) when these macros are def ined: 'WIN32'.) --- lib/preprocessor.cpp | 13 ++++++------ test/testpreprocessor.cpp | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 4d4b0dbbb..cebff7c14 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1779,7 +1779,7 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str if (line.compare(0,7,"#ifdef ") == 0) { if (indent == indentmatch) { - std::string tag = getdef(line,true); + const std::string tag = getdef(line,true); if (defs.find(tag) != defs.end()) { elseIsTrue = false; indentmatch++; @@ -1795,7 +1795,7 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str elseIsTrue = true; } else if (line.compare(0,8,"#ifndef ") == 0) { if (indent == indentmatch) { - std::string tag = getdef(line,false); + const std::string tag = getdef(line,false); if (defs.find(tag) == defs.end()) { elseIsTrue = false; indentmatch++; @@ -1804,11 +1804,12 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str indentmatch++; suppressCurrentCodePath = false; } - ++indent; - - if (indent == indentmatch + 1) - elseIsTrue = true; } + ++indent; + + if (indent == indentmatch + 1) + elseIsTrue = true; + } else if (!suppressCurrentCodePath && line.compare(0,4,"#if ") == 0) { if (indent == indentmatch && match_cfg_def(defs, line.substr(4))) { elseIsTrue = false; diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index a8079bfdb..3861f84d4 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -234,6 +234,7 @@ private: // Defines are given: test Preprocessor::handleIncludes TEST_CASE(def_handleIncludes); TEST_CASE(def_missingInclude); + TEST_CASE(def_handleIncludes_ifelse); // problems in handleIncludes for #else // Using -U to undefine symbols TEST_CASE(undef1); @@ -3043,6 +3044,48 @@ private: } } + void def_handleIncludes_ifelse() { + const std::string filePath("test.c"); + const std::list includePaths; + std::map defs; + Preprocessor preprocessor(NULL, this); + + // #3405 + { + defs.clear(); + defs["A"] = ""; + const std::string code("\n#ifndef PAL_UTIL_UTILS_H_\n" + "#define PAL_UTIL_UTILS_H_\n" + "1\n" + "#ifndef USE_BOOST\n" + "2\n" + "#else\n" + "3\n" + "#endif\n" + "4\n" + "#endif\n" + "\n" + "#ifndef PAL_UTIL_UTILS_H_\n" + "#define PAL_UTIL_UTILS_H_\n" + "5\n" + "#ifndef USE_BOOST\n" + "6\n" + "#else\n" + "7\n" + "#endif\n" + "8\n" + "#endif\n" + "\n"); + std::string actual(preprocessor.handleIncludes(code,filePath,includePaths,defs)); + + // the 1,2,4 should be in the result + actual.erase(0, actual.find("1")); + while (actual.find("\n") != std::string::npos) + actual.erase(actual.find("\n"),1); + ASSERT_EQUALS("124", actual); + } + } + void undef1() { Settings settings;