diff --git a/lib/checkobsoletefunctions.h b/lib/checkobsoletefunctions.h index d9e7f6bb9..621db0a78 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. " + "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")); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 459a0f43e..66f4c8f09 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 */ @@ -1645,9 +1680,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) { @@ -4860,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; } 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() { {