From dc12a73987ec0a69c137d548e5bbd34f890a8ab3 Mon Sep 17 00:00:00 2001 From: Simon Martin Date: Sun, 8 Jun 2014 14:59:58 +0200 Subject: [PATCH] Ticket #5907: Properly handle extern declarations in Tokenizer::simplifyVarDecl. --- lib/tokenize.cpp | 4 ++-- test/testtokenize.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 83a5e8a29..5968b73f2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5257,7 +5257,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ continue; Token *type0 = tok; - if (!Token::Match(type0, "::| %type%")) + if (!Token::Match(type0, "::|extern| %type%")) continue; if (Token::Match(type0, "else|return|public:|protected:|private:")) continue; @@ -5267,7 +5267,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ Token *tok2 = type0; unsigned int typelen = 1; - if (tok2->str() == "::") { + if (Token::Match(tok2, "::|extern")) { tok2 = tok2->next(); typelen++; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index cf8f87ca9..8a0b6240c 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -412,6 +412,7 @@ private: TEST_CASE(vardecl23); // #4276 - segmentation fault TEST_CASE(vardecl24); // #4187 - variable declaration within lambda function TEST_CASE(vardecl25); // #4799 - segmentation fault + TEST_CASE(vardecl26); // #5907 - incorrect handling of extern declarations TEST_CASE(vardecl_stl_1); TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_template_1); @@ -6355,6 +6356,13 @@ private: "}"); } + void vardecl26() { // #5907 + const char code[] = "extern int *new, obj, player;"; + const char expected[] = "extern int * new ; extern int obj ; extern int player ;"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, false, true, Settings::Unspecified, "test.c")); + ASSERT_EQUALS(expected, tokenizeAndStringify(code)); + } + void volatile_variables() { const char code[] = "volatile int a=0;\n" "volatile int b=0;\n"