From 5dc45bd4ac178104eaf96f9ceb225728583dde0f Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 30 Dec 2014 19:23:01 +0100 Subject: [PATCH] Fixed #5497: Support designated initializers in array size detection --- lib/tokenize.cpp | 12 ++++++++++-- test/testtokenize.cpp | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 30582062c..dfaa1df21 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2069,9 +2069,17 @@ void Tokenizer::arraySize() tok = tok->next(); Token *end = tok->linkAt(3); for (Token *tok2 = tok->tokAt(4); tok2 && tok2 != end; tok2 = tok2->next()) { - if (Token::Match(tok2, "{|(|[")) + if (tok2->link() && Token::Match(tok2, "{|(|[|<")) { + if (tok2->str() == "[" && tok2->link()->strAt(1) == "=") { // designated initializer + if (Token::Match(tok2, "[ %num% ]")) + sz = std::max(sz, (unsigned int)MathLib::toULongNumber(tok2->strAt(1)) + 1U); + else { + sz = 0; + break; + } + } tok2 = tok2->link(); - else if (tok2->str() == "<") { // Bailout. TODO: When link() supports <>, this bailout becomes unnecessary + } else if (tok2->str() == "<") { // Bailout. TODO: When link() supports <>, this bailout becomes unnecessary sz = 0; break; } else if (tok2->str() == ",") { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 3dc00bf40..3e88c45fd 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -5251,6 +5251,9 @@ private: ASSERT_EQUALS("; int a[2]={ b> c?1:2,3};", arraySize_(";int a[]={ b>c?1:2,3};")); TODO_ASSERT_EQUALS("int main(){ int a[2]={ b< c?1:2,3}}", "int main(){ int a[]={ b< c?1:2,3}}", arraySize_("int main(){int a[]={b