diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 39f435556..5a82af5d6 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -569,6 +569,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[]) // Simplify the operator "?:" simplifyConditionOperator(); + // remove exception specifications.. + removeExceptionSpecifications(_tokens); + // change array to pointer.. for (Token *tok = _tokens; tok; tok = tok->next()) { @@ -4165,6 +4168,39 @@ void Tokenizer::simplifyComma() } +void Tokenizer::removeExceptionSpecifications(Token *tok) const +{ + while (tok) + { + if (tok->str() == "{") + tok = tok->link(); + + else if (tok->str() == "}") + break; + + else if (Token::Match(tok, ") throw (")) + { + while (tok->next() && !Token::Match(tok->next(), "[;{]")) + tok->deleteNext(); + } + + else if (Token::Match(tok, "class %type%")) + { + while (tok && !Token::Match(tok, "[;{]")) + tok = tok->next(); + if (tok && tok->str() == "{") + { + removeExceptionSpecifications(tok->next()); + tok = tok->link(); + } + } + + tok = tok ? tok->next() : 0; + } +} + + + bool Tokenizer::validate() const { std::stack linktok; diff --git a/src/tokenize.h b/src/tokenize.h index 5c02cafbc..94f53541b 100644 --- a/src/tokenize.h +++ b/src/tokenize.h @@ -304,6 +304,12 @@ private: */ std::string simplifyString(const std::string &source); + /** + * Remove exception specifications. This function calls itself recursively. + * @param tok First token in scope to cleanup + */ + void removeExceptionSpecifications(Token *tok) const; + void insertTokens(Token *dest, const Token *src, unsigned int n); /** diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 74dd7e354..33e22aaae 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -155,6 +155,8 @@ private: TEST_CASE(testUpdateClassList); TEST_CASE(createLinks); TEST_CASE(signed1); + + TEST_CASE(removeExceptionSpecification); } @@ -2570,6 +2572,28 @@ private: ASSERT_EQUALS(true, tok->tokAt(9)->link() == tok->tokAt(8)); } } + + void removeExceptionSpecification() + { + const char code[] = "class A\n" + "{\n" + "private:\n" + " void f() throw (std::runtime_error);\n" + "};\n" + "void A::f() throw (std::runtime_error)\n" + "{ }"; + + const char expected[] = "class A\n" + "{\n" + "private:\n" + "void f ( ) ;\n" + "} ;\n" + "void A :: f ( )\n" + "{ }"; + + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + } + }; REGISTER_TEST(TestTokenizer)