From 31f8a71f84a4e08866edf25cc1924e3462580af8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 15 Apr 2012 17:39:22 +0200 Subject: [PATCH] Tokenizer::setVarIdNew: Better handling for catch-exception-by-reference --- lib/tokenize.cpp | 7 +++++-- test/testtokenize.cpp | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 021240b35..f2912141f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2854,8 +2854,11 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::mapstr() != "=") + // Catching by reference is an exception + if (executableScope && ref && tok2->str() != "=" && + (tok2->str() != ")" || !Token::simpleMatch(tok2->link()->previous(), "catch"))) { return false; + } } return bool(typeCount >= 2 && tok2 && Token::Match(tok2->tokAt(-2), "!!:: %type%")); @@ -2903,7 +2906,7 @@ void Tokenizer::setVarIdNew() if (tok == _tokens || Token::Match(tok, "[;{},]") || (tok->str()=="(" && (!executableScope.top() || Token::simpleMatch(tok->link(), ") {")))) { // locate the variable name.. - const Token *tok2 = (tok == _tokens) ? tok : tok->next(); + const Token *tok2 = (tok->isName()) ? tok : tok->next(); if (!tok2) break; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 077c3baa3..d49c00a4f 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -225,6 +225,7 @@ private: TEST_CASE(varid_throw); TEST_CASE(varid_unknown_macro); // #2638 - unknown macro is not type TEST_CASE(varid_using); // ticket #3648 + TEST_CASE(varid_catch); TEST_CASE(varidclass1); TEST_CASE(varidclass2); @@ -3645,6 +3646,19 @@ private: ASSERT_EQUALS(expected, tokenizeDebugListing(code)); } + void varid_catch() { + const char code[] = "void f() {\n" + " try { dostuff(); }\n" + " catch (exception &e) { }\n" + "}"; + const char expected[] = "\n\n##file 0\n" + "1: void f ( ) {\n" + "2: try { dostuff ( ) ; }\n" + "3: catch ( exception & e@1 ) { }\n" + "4: }\n"; + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); + } + void varidclass1() { const std::string actual = tokenizeDebugListing( "class Fred\n"