diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a3e677df1..2ed03445c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2160,6 +2160,8 @@ bool Tokenizer::simplifyTokenList() tok->deleteNext(); } + simplifyStd(); + simplifyNamespaces(); simplifyGoto(); @@ -4543,6 +4545,32 @@ void Tokenizer::simplifyEnum() } } + +void Tokenizer::simplifyStd() +{ + std::set f; + f.insert("strcat"); + f.insert("strcpy"); + f.insert("strncat"); + f.insert("strncpy"); + f.insert("free"); + f.insert("malloc"); + f.insert("strdup"); + + for (Token *tok = _tokens; tok; tok = tok->next()) + { + if (tok->str() != "std") + continue; + + if (Token::Match(tok->previous(), "[(,{};] std :: %var% (") && + f.find(tok->strAt(2)) != f.end()) + { + tok->deleteNext(); + tok->deleteThis(); + } + } +} + //--------------------------------------------------------------------------- // Helper functions for handling the tokens list //--------------------------------------------------------------------------- diff --git a/lib/tokenize.h b/lib/tokenize.h index 303b52b09..183617ca4 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -331,6 +331,11 @@ private: */ void simplifyEnum(); + /** + * Remove "std::" before some function names + */ + void simplifyStd(); + /** * Remove exception specifications. This function calls itself recursively. * @param tok First token in scope to cleanup diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 04952394a..368592c08 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -164,6 +164,9 @@ private: TEST_CASE(enum1); TEST_CASE(enum2); + + // remove "std::" on some standard functions + TEST_CASE(removestd); } std::string tok(const char code[], bool simplify = true) @@ -2719,6 +2722,16 @@ private: ASSERT_EQUALS(expected, tok(code, false)); } + void removestd() + { + ASSERT_EQUALS("; strcpy ( a , b ) ;", tok("; std::strcpy(a,b);")); + ASSERT_EQUALS("; strcat ( a , b ) ;", tok("; std::strcat(a,b);")); + ASSERT_EQUALS("; strncpy ( a , b , 10 ) ;", tok("; std::strncpy(a,b,10);")); + ASSERT_EQUALS("; strncat ( a , b , 10 ) ;", tok("; std::strncat(a,b,10);")); + ASSERT_EQUALS("; free ( p ) ;", tok("; std::free(p);")); + ASSERT_EQUALS("; malloc ( 10 ) ;", tok("; std::malloc(10);")); + } + }; REGISTER_TEST(TestSimplifyTokens)