diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 72ecf02df..19e032b8e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1660,6 +1660,18 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::s return false; } + // check for simple syntax errors.. + for (Token *tok = _tokens; tok; tok = tok->next()) + { + if (Token::simpleMatch(tok, "> struct {") && + Token::simpleMatch(tok->tokAt(2)->link(), "} ;")) + { + syntaxError(tok); + deallocateTokens(); + return false; + } + } + // specify array size.. arraySize(); @@ -6899,7 +6911,26 @@ const Token * Tokenizer::findClassFunction(const Token *tok, const std::string & return NULL; } //--------------------------------------------------------------------------- -// Error message for bad iterator usage.. + +void Tokenizer::syntaxError(const Token *tok) +{ + if (tok) + { + std::list locationList; + ErrorLogger::ErrorMessage::FileLocation loc; + loc.line = tok->linenr(); + loc.setfile(file(tok)); + locationList.push_back(loc); + const ErrorLogger::ErrorMessage errmsg(locationList, + Severity::error, + "syntax error", + "syntaxError"); + if (_errorLogger) + _errorLogger->reportErr(errmsg); + else + Check::reportError(errmsg); + } +} void Tokenizer::syntaxError(const Token *tok, char c) { diff --git a/lib/tokenize.h b/lib/tokenize.h index 904ba0732..46439a790 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -411,6 +411,10 @@ public: */ bool createLinks(); + /** Syntax error */ + void syntaxError(const Token *tok); + + /** Syntax error. Example: invalid number of ')' */ void syntaxError(const Token *tok, char c); /** diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 51f0d677f..9e5b5eed1 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -248,7 +248,7 @@ private: std::string tokenizeAndStringify(const char code[], bool simplify = false) { // tokenize.. - Tokenizer tokenizer; + Tokenizer tokenizer(0, this); std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); if (simplify) @@ -348,10 +348,19 @@ private: void wrong_syntax() { - errout.str(""); - const std::string code("TR(kvmpio, PROTO(int rw), ARGS(rw), TP_(aa->rw;))"); - ASSERT_EQUALS("TR ( kvmpio , PROTO ( int rw ) , ARGS ( rw ) , TP_ ( aa . rw ; ) )", tokenizeAndStringify(code.c_str(), true)); - ASSERT_EQUALS("", errout.str()); + { + errout.str(""); + const std::string code("TR(kvmpio, PROTO(int rw), ARGS(rw), TP_(aa->rw;))"); + ASSERT_EQUALS("TR ( kvmpio , PROTO ( int rw ) , ARGS ( rw ) , TP_ ( aa . rw ; ) )", tokenizeAndStringify(code.c_str(), true)); + ASSERT_EQUALS("", errout.str()); + } + + { + errout.str(""); + const std::string code("struct A { template struct { }; };"); + ASSERT_EQUALS("", tokenizeAndStringify(code.c_str(), true)); + ASSERT_EQUALS("[test.cpp:1]: (error) syntax error\n", errout.str()); + } } void minus()