From 7112f69d7be888acabfbb9997f1f5d05cb157e99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 29 Nov 2020 12:56:13 +0100 Subject: [PATCH] Fixed bug in fixAngleBrackets --- lib/token.cpp | 12 ++++++++++++ test/testtokenize.cpp | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/token.cpp b/lib/token.cpp index f9c7f7f63..8ce9feb0e 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -865,6 +865,16 @@ const Token * Token::findClosingBracket() const const bool templateParameter(strAt(-1) == "template"); std::set templateParameters; + bool isDecl = true; + for (const Token *prev = previous(); prev; prev = prev->previous()) { + if (prev->str() == "=") + isDecl = false; + if (Token::simpleMatch(prev, "template <")) + isDecl = true; + if (Token::Match(prev, "[;{}]")) + break; + } + unsigned int depth = 0; for (closing = this; closing != nullptr; closing = closing->next()) { if (Token::Match(closing, "{|[|(")) { @@ -882,6 +892,8 @@ const Token * Token::findClosingBracket() const if (--depth == 0) return closing; } else if (closing->str() == ">>" || closing->str() == ">>=") { + if (!isDecl && depth == 1) + continue; if (depth <= 2) return closing; depth -= 2; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d810b25d5..b24e1ba9e 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -348,6 +348,8 @@ private: TEST_CASE(functionAttributeBefore); TEST_CASE(functionAttributeAfter); + TEST_CASE(fixAngleBrackets); + TEST_CASE(cpp03template1); TEST_CASE(cpp0xtemplate1); TEST_CASE(cpp0xtemplate2); @@ -5270,6 +5272,13 @@ private: ASSERT(func5 && func5->isAttributeNoreturn()); } + void fixAngleBrackets() { + { + const char *code = "; z = x < 0 ? x >> y : x >> y;"; + ASSERT_EQUALS("; z = x < 0 ? x >> y : x >> y ;", tokenizeAndStringify(code)); + } + } + void cpp03template1() { { const char *code = "template struct extent {};";