From 489a3a6e53b2ce1b55f84c66f131c6ecb9a24143 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 6 Sep 2013 05:36:33 +0200 Subject: [PATCH] Fixed #5015 (strings not being concatinated properly) --- lib/token.cpp | 2 +- lib/tokenize.cpp | 15 +++++++++++++++ test/testtokenize.cpp | 28 +++++++++++++++++++++------- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 615dfcfd7..eaea508b7 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -993,7 +993,7 @@ void Token::stringify(std::ostream& os, bool varid, bool attributes) const else if (isSigned()) os << "signed "; if (isLong()) { - if (_type == eString) + if (_type == eString || _type == eChar) os << "L"; else os << "long "; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index cc4d964f0..9d46147d0 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9358,6 +9358,13 @@ void Tokenizer::simplifyMicrosoftStringFunctions() tok->deleteNext(); tok->deleteThis(); tok->deleteNext(); + while (tok->next() && Token::Match(tok->next(), "_T ( %char%|%str% )")) { + tok->next()->deleteNext(); + tok->next()->deleteThis(); + tok->next()->deleteNext(); + tok->concatStr(tok->next()->str()); + tok->deleteNext(); + } } } } else if (_settings->platformType == Settings::Win32W || @@ -9401,6 +9408,14 @@ void Tokenizer::simplifyMicrosoftStringFunctions() tok->deleteNext(); tok->deleteThis(); tok->deleteNext(); + tok->isLong(true); + while (tok->next() && Token::Match(tok->next(), "_T ( %char%|%str% )")) { + tok->next()->deleteNext(); + tok->next()->deleteThis(); + tok->next()->deleteNext(); + tok->concatStr(tok->next()->str()); + tok->deleteNext(); + } } } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 695b12972..22ec7f348 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -493,6 +493,8 @@ private: TEST_CASE(platformWin64); TEST_CASE(platformUnix32); TEST_CASE(platformUnix64); + TEST_CASE(platformWin32AStringCat); // ticket #5015 + TEST_CASE(platformWin32WStringCat); // ticket #5015 TEST_CASE(simplifyMathExpressions); //ticket #1620 @@ -8084,18 +8086,18 @@ private: "const wchar_t * lpctstr ; " "unsigned char tbyte ; " "void foo ( ) { " - "wchar_t tc ; tc = \'c\' ; " - "wchar_t src [ 10 ] = \"123456789\" ; " + "wchar_t tc ; tc = L\'c\' ; " + "wchar_t src [ 10 ] = L\"123456789\" ; " "wchar_t dst [ 10 ] ; " "wcscpy ( dst , src ) ; " "dst [ 0 ] = 0 ; " "wcscat ( dst , src ) ; " "wchar_t * d ; d = wcsdup ( str ) ; " - "wprintf ( \"Hello world!\n\" ) ; " - "swprintf ( dst , \"Hello!\n\" ) ; " - "snwprintf ( dst , sizeof ( dst ) / sizeof ( wchar_t ) , \"Hello world!\n\" ) ; " - "wscanf ( \"%s\" , dst ) ; " - "swscanf ( dst , \"%s\" , dst ) ; " + "wprintf ( L\"Hello world!\n\" ) ; " + "swprintf ( dst , L\"Hello!\n\" ) ; " + "snwprintf ( dst , sizeof ( dst ) / sizeof ( wchar_t ) , L\"Hello world!\n\" ) ; " + "wscanf ( L\"%s\" , dst ) ; " + "swscanf ( dst , L\"%s\" , dst ) ; " "}"; ASSERT_EQUALS(expected, tokenizeAndStringify(code, false, true, Settings::Win32W)); } @@ -8262,6 +8264,18 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unix64)); } + void platformWin32AStringCat() { //#5150 + const char code[] = "TCHAR text[] = _T(\"123\") _T(\"456\") _T(\"789\");"; + const char expected[] = "char text [ 10 ] = \"123456789\" ;"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Win32A)); + } + + void platformWin32WStringCat() { //#5150 + const char code[] = "TCHAR text[] = _T(\"123\") _T(\"456\") _T(\"789\");"; + const char expected[] = "wchar_t text [ 10 ] = L\"123456789\" ;"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Win32W)); + } + void simplifyMathExpressions() {//#1620 const char code1[] = "void foo() {\n" " std::cout<