Refactoring: The preprocessor will report errors instead of throwing exceptions. Ticket: #1866

This commit is contained in:
Daniel Marjamäki 2010-07-23 13:54:52 +02:00
parent 37c959023a
commit 5e0a4a173a
3 changed files with 25 additions and 25 deletions

View File

@ -216,7 +216,7 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
errmsg << "The code contains characters that are unhandled. " errmsg << "The code contains characters that are unhandled. "
<< "Neither unicode nor extended ascii are supported. " << "Neither unicode nor extended ascii are supported. "
<< "(line=" << lineno << ", character code=" << std::hex << (int(ch) & 0xff) << ")"; << "(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) if (str.compare(i, 6, "#error") == 0 || str.compare(i, 8, "#warning") == 0)

View File

@ -85,13 +85,7 @@ public:
void preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list<std::string> &resultConfigurations, const std::string &filename, const std::list<std::string> &includePaths); void preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list<std::string> &resultConfigurations, const std::string &filename, const std::list<std::string> &includePaths);
/** Just read the code into a string. Perform simple cleanup of the code */ /** 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); 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);
}
/** /**
* Get preprocessed code for a given configuration * Get preprocessed code for a given configuration
@ -147,9 +141,8 @@ protected:
* @param filename filename * @param filename filename
* @param settings Settings. If there are inline suppressions these will be added to the settings * @param settings Settings. If there are inline suppressions these will be added to the settings
* @return code without comments * @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(). * Remove redundant parantheses from preprocessor commands. This should only be called from read().

View File

@ -188,9 +188,9 @@ private:
{ {
const char code[] = " \t a //\n" const char code[] = " \t a //\n"
" #aa\t /* remove this */\tb \r\n"; " #aa\t /* remove this */\tb \r\n";
Preprocessor p; Preprocessor preprocessor(0,this);
std::istringstream istr(code); 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); ASSERT_EQUALS("a \n#aa b \n", codestr);
} }
@ -374,7 +374,8 @@ private:
"#elif defined (A)\n"; "#elif defined (A)\n";
std::istringstream istr(filedata); 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.. // Compare results..
ASSERT_EQUALS("#if A\n#if A\n#if A\n#if defined(A)\n#elif defined(A)\n", actual); 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.. // Read string..
std::istringstream istr(filedata); 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 // Preprocess
std::istringstream istr(filedata); std::istringstream istr(filedata);
Preprocessor preprocessor; Preprocessor preprocessor(0, this);
ASSERT_EQUALS("\n\n\n", preprocessor.read(istr)); ASSERT_EQUALS("\n\n\n", preprocessor.read(istr, "test.c", 0));
} }
@ -1085,8 +1087,8 @@ private:
// Preprocess => actual result.. // Preprocess => actual result..
std::istringstream istr(filedata); std::istringstream istr(filedata);
Preprocessor preprocessor; Preprocessor preprocessor(0, this);
ASSERT_EQUALS("#define str \"abc\" \"def\" \n\nabcdef = str;\n", preprocessor.read(istr)); ASSERT_EQUALS("#define str \"abc\" \"def\" \n\nabcdef = str;\n", preprocessor.read(istr, "test.c", 0));
} }
void multiline2() void multiline2()
@ -1097,8 +1099,8 @@ private:
// Preprocess => actual result.. // Preprocess => actual result..
std::istringstream istr(filedata); std::istringstream istr(filedata);
Preprocessor preprocessor; Preprocessor preprocessor(0, this);
ASSERT_EQUALS("#define sqr(aa) aa * aa\n\nsqr(5);\n", preprocessor.read(istr)); ASSERT_EQUALS("#define sqr(aa) aa * aa\n\nsqr(5);\n", preprocessor.read(istr, "test.c", 0));
} }
void multiline3() void multiline3()
@ -1109,8 +1111,8 @@ private:
// Preprocess => actual result.. // Preprocess => actual result..
std::istringstream istr(filedata); std::istringstream istr(filedata);
Preprocessor preprocessor; Preprocessor preprocessor(0, this);
ASSERT_EQUALS("const char *str = \"abcdefghi\"\n\n\n", preprocessor.read(istr)); ASSERT_EQUALS("const char *str = \"abcdefghi\"\n\n\n", preprocessor.read(istr, "test.c", 0));
} }
void multiline4() void multiline4()
@ -1887,21 +1889,26 @@ private:
{ {
const std::string filedata("a\xC8"); const std::string filedata("a\xC8");
std::istringstream istr(filedata); 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() void unicodeInComment()
{ {
const std::string filedata("//\xC8"); const std::string filedata("//\xC8");
std::istringstream istr(filedata.c_str()); 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() void unicodeInString()
{ {
const std::string filedata("\"\xC8\""); const std::string filedata("\"\xC8\"");
std::istringstream istr(filedata.c_str()); 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));
} }