diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index b14c57459..e70174724 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3188,10 +3188,10 @@ static void linkBrackets(const Tokenizer * const tokenizer, std::stackstr()[0] == close) { if (links.empty()) { // Error, { and } don't match. - tokenizer->syntaxError(token, open); + tokenizer->unmatchedToken(token); } if (type.top()->str()[0] != open) { - tokenizer->syntaxError(type.top(), type.top()->str()[0]); + tokenizer->unmatchedToken(type.top()); } type.pop(); @@ -3220,17 +3220,17 @@ void Tokenizer::createLinks() if (!links1.empty()) { // Error, { and } don't match. - syntaxError(links1.top(), '{'); + unmatchedToken(links1.top()); } if (!links2.empty()) { // Error, ( and ) don't match. - syntaxError(links2.top(), '('); + unmatchedToken(links2.top()); } if (!links3.empty()) { // Error, [ and ] don't match. - syntaxError(links3.top(), '['); + unmatchedToken(links3.top()); } } @@ -7930,17 +7930,12 @@ void Tokenizer::syntaxError(const Token *tok) const throw InternalError(tok, "syntax error", InternalError::SYNTAX); } -void Tokenizer::syntaxError(const Token *tok, char c) const +void Tokenizer::unmatchedToken(const Token *tok) const { printDebugOutput(0); - if (mConfiguration.empty()) - throw InternalError(tok, - std::string("Invalid number of character '") + c + "' when no macros are defined.", - InternalError::SYNTAX); - else - throw InternalError(tok, - std::string("Invalid number of character '") + c + "' when these macros are defined: '" + mConfiguration + "'.", - InternalError::SYNTAX); + throw InternalError(tok, + "Unmatched '" + tok->str() + "'. Configuration: '" + mConfiguration + "'.", + InternalError::SYNTAX); } void Tokenizer::syntaxErrorC(const Token *tok, const std::string &what) const diff --git a/lib/tokenize.h b/lib/tokenize.h index 3aa14a88a..f7bf3a306 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -594,8 +594,8 @@ public: /** Syntax error */ void syntaxError(const Token *tok) const; - /** Syntax error. Example: invalid number of ')' */ - void syntaxError(const Token *tok, char c) const; + /** Syntax error. Unmatched character. */ + void unmatchedToken(const Token *tok) const; /** Syntax error. C++ code in C file. */ void syntaxErrorC(const Token *tok, const std::string &what) const; diff --git a/samples/syntaxError/out.txt b/samples/syntaxError/out.txt index 8f0626044..2617f34bf 100644 --- a/samples/syntaxError/out.txt +++ b/samples/syntaxError/out.txt @@ -1 +1 @@ -[samples\syntaxError\bad.c:2]: (error) Invalid number of character '{' when no macros are defined. +[samples\syntaxError\bad.c:2]: (error) Unmatched '{'. Configuration: ''. diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 869730285..8441e5331 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -284,6 +284,19 @@ private: return tokenizer.tokens()->stringifyList(false, false, false, true, false, 0, 0); } + std::string getSyntaxError(const char code[]) { + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + try { + tokenizer.tokenize(istr, "test.cpp"); + } catch (InternalError& e) { + if (e.id != "syntaxError") + return ""; + return "[test.cpp:" + MathLib::toString(e.token->linenr()) + "] " + e.errorMessage; + } + return ""; + } + void wrong_syntax1() { { const char code[] ="TR(kvmpio, PROTO(int rw), ARGS(rw), TP_(aa->rw;))"; @@ -1007,23 +1020,34 @@ private: "}\n"), InternalError); { - errout.str(""); const char code[] = "{\n" - " a(\n" + " a(\n" // <- error "}\n" "{\n" " b());\n" "}\n"; - Tokenizer tokenizer(&settings, this); - std::istringstream istr(code); - try { - tokenizer.tokenize(istr, "test.cpp"); - assertThrowFail(__FILE__, __LINE__); - } catch (InternalError& e) { - ASSERT_EQUALS("Invalid number of character '(' when no macros are defined.", e.errorMessage); - ASSERT_EQUALS("syntaxError", e.id); - ASSERT_EQUALS(2, e.token->linenr()); - } + ASSERT_EQUALS("[test.cpp:2] Unmatched '('. Configuration: ''.", getSyntaxError(code)); + } + + { + const char code[] = "void f() {\n" + " int x = 3) + 0;\n" // <- error: unmatched ) + "}\n"; + ASSERT_EQUALS("[test.cpp:2] Unmatched ')'. Configuration: ''.", getSyntaxError(code)); + } + + { + const char code[] = "void f() {\n" + " int x = (3] + 0;\n" // <- error: unmatched ] + "}\n"; + ASSERT_EQUALS("[test.cpp:2] Unmatched ']'. Configuration: ''.", getSyntaxError(code)); + } + + { + const char code[] = "void f() {\n" // <- error: unmatched { + " {\n" + "}\n"; + ASSERT_EQUALS("[test.cpp:1] Unmatched '{'. Configuration: ''.", getSyntaxError(code)); } }