From 5e0a4a173a5856754081c28d852e16e783b4d43d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 23 Jul 2010 13:54:52 +0200 Subject: [PATCH] Refactoring: The preprocessor will report errors instead of throwing exceptions. Ticket: #1866 --- lib/preprocessor.cpp | 2 +- lib/preprocessor.h | 11 ++--------- test/testpreprocessor.cpp | 37 ++++++++++++++++++++++--------------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index f465aa198..9e1979436 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -216,7 +216,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri errmsg << "The code contains characters that are unhandled. " << "Neither unicode nor extended ascii are supported. " << "(line=" << lineno << ", character code=" << std::hex << (int(ch) & 0xff) << ")"; - throw std::runtime_error(errmsg.str()); + writeError(filename, lineno, _errorLogger, "syntaxError", errmsg.str()); } if (str.compare(i, 6, "#error") == 0 || str.compare(i, 8, "#warning") == 0) diff --git a/lib/preprocessor.h b/lib/preprocessor.h index da5215bfc..e502794de 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -85,13 +85,7 @@ public: void preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list &resultConfigurations, const std::string &filename, const std::list &includePaths); /** Just read the code into a string. Perform simple cleanup of the code */ - static std::string read(std::istream &istr, const std::string &filename, Settings *settings); - - /** Just read the code into a string. Perform simple cleanup of the code */ - static std::string read(std::istream &istr) - { - return read(istr, "", 0); - } + std::string read(std::istream &istr, const std::string &filename, Settings *settings); /** * Get preprocessed code for a given configuration @@ -147,9 +141,8 @@ protected: * @param filename filename * @param settings Settings. If there are inline suppressions these will be added to the settings * @return code without comments - * @throws std::runtime_error when code contains unhandled characters */ - static std::string removeComments(const std::string &str, const std::string &filename, Settings *settings); + std::string removeComments(const std::string &str, const std::string &filename, Settings *settings); /** * Remove redundant parantheses from preprocessor commands. This should only be called from read(). diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index bd707091a..cd20f4719 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -188,9 +188,9 @@ private: { const char code[] = " \t a //\n" " #aa\t /* remove this */\tb \r\n"; - Preprocessor p; + Preprocessor preprocessor(0,this); std::istringstream istr(code); - std::string codestr(p.read(istr)); + std::string codestr(preprocessor.read(istr,"test.c",0)); ASSERT_EQUALS("a \n#aa b \n", codestr); } @@ -374,7 +374,8 @@ private: "#elif defined (A)\n"; std::istringstream istr(filedata); - const std::string actual(Preprocessor::read(istr)); + Preprocessor preprocessor(0,this); + const std::string actual(preprocessor.read(istr, "test.c", 0)); // Compare results.. ASSERT_EQUALS("#if A\n#if A\n#if A\n#if defined(A)\n#elif defined(A)\n", actual); @@ -438,7 +439,8 @@ private: // Read string.. std::istringstream istr(filedata); - ASSERT_EQUALS("#error\n\n123", Preprocessor::read(istr)); + Preprocessor preprocessor(0,this); + ASSERT_EQUALS("#error\n\n123", preprocessor.read(istr,"test.c",0)); } @@ -520,8 +522,8 @@ private: // Preprocess std::istringstream istr(filedata); - Preprocessor preprocessor; - ASSERT_EQUALS("\n\n\n", preprocessor.read(istr)); + Preprocessor preprocessor(0, this); + ASSERT_EQUALS("\n\n\n", preprocessor.read(istr, "test.c", 0)); } @@ -1085,8 +1087,8 @@ private: // Preprocess => actual result.. std::istringstream istr(filedata); - Preprocessor preprocessor; - ASSERT_EQUALS("#define str \"abc\" \"def\" \n\nabcdef = str;\n", preprocessor.read(istr)); + Preprocessor preprocessor(0, this); + ASSERT_EQUALS("#define str \"abc\" \"def\" \n\nabcdef = str;\n", preprocessor.read(istr, "test.c", 0)); } void multiline2() @@ -1097,8 +1099,8 @@ private: // Preprocess => actual result.. std::istringstream istr(filedata); - Preprocessor preprocessor; - ASSERT_EQUALS("#define sqr(aa) aa * aa\n\nsqr(5);\n", preprocessor.read(istr)); + Preprocessor preprocessor(0, this); + ASSERT_EQUALS("#define sqr(aa) aa * aa\n\nsqr(5);\n", preprocessor.read(istr, "test.c", 0)); } void multiline3() @@ -1109,8 +1111,8 @@ private: // Preprocess => actual result.. std::istringstream istr(filedata); - Preprocessor preprocessor; - ASSERT_EQUALS("const char *str = \"abcdefghi\"\n\n\n", preprocessor.read(istr)); + Preprocessor preprocessor(0, this); + ASSERT_EQUALS("const char *str = \"abcdefghi\"\n\n\n", preprocessor.read(istr, "test.c", 0)); } void multiline4() @@ -1887,21 +1889,26 @@ private: { const std::string filedata("a\xC8"); std::istringstream istr(filedata); - ASSERT_THROW(Preprocessor::read(istr), std::runtime_error); + errout.str(""); + Preprocessor preprocessor(0, this); + preprocessor.read(istr, "test.cpp", 0); + ASSERT_EQUALS("[test.cpp:1]: (error) The code contains characters that are unhandled. Neither unicode nor extended ascii are supported. (line=1, character code=c8)\n", errout.str()); } void unicodeInComment() { const std::string filedata("//\xC8"); std::istringstream istr(filedata.c_str()); - ASSERT_EQUALS("", Preprocessor::read(istr)); + Preprocessor preprocessor(0, this); + ASSERT_EQUALS("", preprocessor.read(istr, "test.cpp", 0)); } void unicodeInString() { const std::string filedata("\"\xC8\""); std::istringstream istr(filedata.c_str()); - ASSERT_EQUALS(filedata, Preprocessor::read(istr)); + Preprocessor preprocessor(0, this); + ASSERT_EQUALS(filedata, preprocessor.read(istr, "test.cpp", 0)); }