From 1a48f869c84018d73fd63bf59263224b019a5997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 4 Oct 2009 07:25:30 +0200 Subject: [PATCH] Fixed #772 (Preprocessor: #if(A) is not seen equal to #if A) --- src/preprocessor.cpp | 34 +++++++++++++++++++++++++++++++++- src/preprocessor.h | 8 ++++++++ test/testpreprocessor.cpp | 7 ++++--- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index d0a782e4e..e6003e29c 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -324,7 +324,7 @@ std::string Preprocessor::read(std::istream &istr) } } - return removeComments(code.str()); + return removeParantheses(removeComments(code.str())); } @@ -442,6 +442,38 @@ std::string Preprocessor::removeComments(const std::string &str) } +std::string Preprocessor::removeParantheses(const std::string &str) +{ + if (str.find("\n#if") == std::string::npos) + return str; + + std::istringstream istr(str.c_str()); + std::ostringstream ret; + std::string line; + while (std::getline(istr, line)) + { + if (line.substr(0, 3) == "#if") + { + while (line.find(" (") != std::string::npos) + line.erase(line.find(" ("), 1); + while (line.find("( ") != std::string::npos) + line.erase(line.find("( ") + 1, 1); + while (line.find(" )") != std::string::npos) + line.erase(line.find(" )"), 1); + while (line.find(") ") != std::string::npos) + line.erase(line.find(") ") + 1, 1); + if (line.substr(0, 4) == "#if(" && line.find(")") == line.length() - 1) + { + line[3] = ' '; + line.erase(line.length() - 1); + } + } + ret << line << "\n"; + } + return ret.str(); +} + + static void _removeAsm(std::string &str, const std::string::size_type pos) { diff --git a/src/preprocessor.h b/src/preprocessor.h index 8cb724735..335413f19 100644 --- a/src/preprocessor.h +++ b/src/preprocessor.h @@ -99,6 +99,14 @@ protected: */ static std::string removeComments(const std::string &str); + /** + * Remove redundant parantheses from preprocessor commands. This should only be called from read(). + * @param str Code processed by read(). + * @return code with reduced parantheses + * @throws std::runtime_error when code contains unhandled characters + */ + static std::string removeParantheses(const std::string &str); + /** * Returns the string between double quote characters or \< \> characters. * @param str e.g. \code#include "menu.h"\endcode or \code#include \endcode diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index cc650c3fb..9ff59357c 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -342,14 +342,15 @@ private: void test6() { - const char filedata[] = "#if(AAA)\n" - "#if(AAA)\n"; + const char filedata[] = "#if(A)\n" + "#if ( A ) \n" + "#if A\n"; std::istringstream istr(filedata); const std::string actual(Preprocessor::read(istr)); // Compare results.. - ASSERT_EQUALS("#if (AAA)\n#if (AAA)\n", actual); + ASSERT_EQUALS("#if A\n#if A\n#if A\n", actual); } void test7()