From 3f97bd5477d6c13e4108234f60c5f6fab623d775 Mon Sep 17 00:00:00 2001 From: Zachary Blair Date: Tue, 4 Jan 2011 23:21:02 -0800 Subject: [PATCH 1/5] Fixed #2343 (The ftime obsolete function warning seems wrong) --- lib/checkobsoletefunctions.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/checkobsoletefunctions.h b/lib/checkobsoletefunctions.h index d9e7f6bb9..aebfbea51 100644 --- a/lib/checkobsoletefunctions.h +++ b/lib/checkobsoletefunctions.h @@ -82,7 +82,9 @@ private: _obsoleteFunctions.push_back(std::make_pair("fcvt","Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function")); _obsoleteFunctions.push_back(std::make_pair("gcvt","Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function")); - _obsoleteFunctions.push_back(std::make_pair("ftime","Found obsolete function 'ftime'. It is recommended that new applications use the 'ftime' function. Realtime applications should use ''clock_gettime'' to determine the current time")); + _obsoleteFunctions.push_back(std::make_pair("ftime","Found obsolete function 'ftime'.\n" + "It is recommended that new applications use time(), gettimeofday(), or clock_gettime() instead.\n" + "For high-resolution timing on Windows, QueryPerformanceCounter() and QueryPerformanceFrequency may be used.")); _obsoleteFunctions.push_back(std::make_pair("getcontext","Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads")); _obsoleteFunctions.push_back(std::make_pair("makecontext","Found obsolete function 'makecontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads")); From 188871c428f15c0ade1403bef7ec6ca30b2f28ae Mon Sep 17 00:00:00 2001 From: Zachary Blair Date: Wed, 5 Jan 2011 00:06:53 -0800 Subject: [PATCH 2/5] Removed an unnecessary newline in the message. Ticket #2343 --- lib/checkobsoletefunctions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkobsoletefunctions.h b/lib/checkobsoletefunctions.h index aebfbea51..621db0a78 100644 --- a/lib/checkobsoletefunctions.h +++ b/lib/checkobsoletefunctions.h @@ -83,7 +83,7 @@ private: _obsoleteFunctions.push_back(std::make_pair("gcvt","Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function")); _obsoleteFunctions.push_back(std::make_pair("ftime","Found obsolete function 'ftime'.\n" - "It is recommended that new applications use time(), gettimeofday(), or clock_gettime() instead.\n" + "It is recommended that new applications use time(), gettimeofday(), or clock_gettime() instead. " "For high-resolution timing on Windows, QueryPerformanceCounter() and QueryPerformanceFrequency may be used.")); _obsoleteFunctions.push_back(std::make_pair("getcontext","Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads")); From ed6f6835736f3b29637e4fe000918baf1b335a10 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 5 Jan 2011 17:39:53 +0100 Subject: [PATCH 3/5] typedef: better handling. ticket: #2414 --- lib/tokenize.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 459a0f43e..765522e5a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1645,9 +1645,12 @@ void Tokenizer::simplifyTypedef() tok2 = tok2->next(); } - // skip over variable name + // skip over variable name if there if (!inCast) - tok2 = tok2->next(); + { + if (tok2->next()->str() != ")") + tok2 = tok2->next(); + } if (tok4 && functionPtrRetFuncPtr) { From 157498e6945c31ce37ab0e52083053b7de229759 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 5 Jan 2011 17:42:55 +0100 Subject: [PATCH 4/5] Fixed #2412 (typedef: struct with inheritance) --- lib/tokenize.cpp | 135 +++++++++++++++++++++++------------- test/testsimplifytokens.cpp | 15 ++++ 2 files changed, 100 insertions(+), 50 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 765522e5a..0c630aa4c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -720,6 +720,78 @@ struct SpaceInfo const Token * classEnd; }; +static Token *splitDefinitionFromTypedef(Token *tok) +{ + Token *tok1; + std::string name; + bool isConst = false; + + if (tok->next()->str() == "const") + { + tok->next()->deleteThis(); + isConst = true; + } + + if (tok->tokAt(2)->str() == "{") // unnamed + { + tok1 = tok->tokAt(2)->link(); + + if (tok1 && tok1->next()) + { + // use typedef name if available + if (Token::Match(tok1->next(), "%type%")) + name = tok1->next()->str(); + else // create a unique name + { + static long count = 0; + name = "Unnamed" + MathLib::toString(count++); + } + tok->tokAt(1)->insertToken(name.c_str()); + } + else + return NULL; + } + else if (tok->strAt(3) == ":") + { + tok1 = tok->tokAt(4); + while (tok1 && tok1->str() != "{") + tok1 = tok1->next(); + if (!tok1) + return NULL; + + tok1 = tok1->link(); + + name = tok->tokAt(2)->str(); + } + else // has a name + { + tok1 = tok->tokAt(3)->link(); + + if (!tok1) + return NULL; + + name = tok->tokAt(2)->str(); + } + + tok1->insertToken(";"); + tok1 = tok1->next(); + tok1->insertToken("typedef"); + tok1 = tok1->next(); + Token * tok3 = tok1; + if (isConst) + { + tok1->insertToken("const"); + tok1 = tok1->next(); + } + tok1->insertToken(tok->next()->strAt(0)); // struct, union or enum + tok1 = tok1->next(); + tok1->insertToken(name.c_str()); + tok->deleteThis(); + tok = tok3; + + return tok; +} + void Tokenizer::simplifyTypedef() { std::vector spaceInfo; @@ -767,60 +839,23 @@ void Tokenizer::simplifyTypedef() if (Token::Match(tok->next(), "const| struct|enum|union|class %type% {") || Token::Match(tok->next(), "const| struct|enum|union|class {")) { - Token *tok1; - std::string name; - bool isConst = false; - - if (tok->next()->str() == "const") + Token *tok1 = splitDefinitionFromTypedef(tok); + if (!tok1) + continue; + tok = tok1; + } + else if (Token::Match(tok->next(), "const| struct|class %type% :")) + { + Token *tok1 = tok; + while (tok1 && tok1->str() != ";" && tok1->str() != "{") + tok1 = tok1->next(); + if (tok1 && tok1->str() == "{") { - tok->next()->deleteThis(); - isConst = true; - } - - if (tok->tokAt(2)->str() == "{") // unnamed - { - tok1 = tok->tokAt(2)->link(); - - if (tok1 && tok1->next()) - { - // use typedef name if available - if (Token::Match(tok1->next(), "%type%")) - name = tok1->next()->str(); - else // create a unique name - { - static long count = 0; - name = "Unnamed" + MathLib::toString(count++); - } - tok->tokAt(1)->insertToken(name.c_str()); - } - else - continue; - } - else // has a name - { - tok1 = tok->tokAt(3)->link(); - + tok1 = splitDefinitionFromTypedef(tok); if (!tok1) continue; - - name = tok->tokAt(2)->str(); + tok = tok1; } - - tok1->insertToken(";"); - tok1 = tok1->next(); - tok1->insertToken("typedef"); - tok1 = tok1->next(); - Token * tok3 = tok1; - if (isConst) - { - tok1->insertToken("const"); - tok1 = tok1->next(); - } - tok1->insertToken(tok->next()->strAt(0)); // struct, union or enum - tok1 = tok1->next(); - tok1->insertToken(name.c_str()); - tok->deleteThis(); - tok = tok3; } /** @todo add support for struct and union */ diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index f57bde0f4..cb4256620 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -230,6 +230,7 @@ private: TEST_CASE(simplifyTypedef70); // ticket #2348 TEST_CASE(simplifyTypedef71); // ticket #2348 TEST_CASE(simplifyTypedef72); // ticket #2375 + TEST_CASE(simplifyTypedef73); // ticket #2412 TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -4757,6 +4758,20 @@ private: } } + void simplifyTypedef73() // ticket #2412 + { + const char code[] = "struct B {};\n" + "typedef struct A : public B {\n" + " void f();\n" + "} a, *aPtr;\n"; + const std::string expected = "struct B { } ; " + "struct A : public B { " + "void f ( ) ; " + "} ;"; + ASSERT_EQUALS(expected, sizeof_(code)); + ASSERT_EQUALS("", errout.str()); + } + void simplifyTypedefFunction1() { { From a0d62e041f426e2c4ce14c5e2880f96dcfb2d4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 5 Jan 2011 19:38:22 +0100 Subject: [PATCH 5/5] Tokenizer: Fixed memory corruption --- lib/tokenize.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0c630aa4c..66f4c8f09 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4898,7 +4898,8 @@ bool Tokenizer::simplifyConditions() else if (Token::simpleMatch(tok, "|| true )") || Token::simpleMatch(tok, "&& false )")) { - Token::eraseTokens(tok->tokAt(2)->link(), tok->next()); + tok = tok->next(); + Token::eraseTokens(tok->next()->link(), tok); ret = true; }