diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 63edd51f9..4346b51b8 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1306,7 +1306,21 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listtokAt(-2), "; %var% = %varid%", varid)) + { + const unsigned int varid2(tok->previous()->varId()); + if (Token::Match(tok->tokAt(-6), "const %type% * %varid% ;", varid2) || + Token::Match(tok->tokAt(-7), "const struct %type% * %varid% ;", varid2)) + { + // address is taken by pointer constant + used = false; + } + } + + if (used) + addtoken("use"); } else if (Token::Match(tok->previous(), "[;{}=(,+-*/] %varid% [", varid)) { @@ -1823,6 +1837,14 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) done = false; } + // Replace "loop callfunc ;" with ";" + if (Token::simpleMatch(tok2, "loop callfunc ;")) + { + tok2->deleteThis(); + tok2->deleteThis(); + done = false; + } + // Replace "loop if return ;" with "if return ;" if (Token::simpleMatch(tok2->next(), "loop if return")) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 50607e766..a940ceaaa 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -518,6 +518,7 @@ private: ASSERT_EQUALS(";;use;", getcode("char *s; s2 = x + s;", "s")); ASSERT_EQUALS(";;use;if{;}", getcode("char *s; if (foo(s)) ;", "s")); ASSERT_EQUALS(";;use;", getcode("char *s; map1[s] = 0;", "s")); + ASSERT_EQUALS(";;;;", getcode("char *p; const char *q; q = p;", "p")); // return.. ASSERT_EQUALS(";;return;", getcode("char *s; return;", "s")); @@ -712,6 +713,7 @@ private: ASSERT_EQUALS("; callfunc ;", simplifycode(";callfunc;")); ASSERT_EQUALS(";", simplifycode(";callfunc;;")); ASSERT_EQUALS("dealloc ; alloc ; return ; }", simplifycode("while1 { dealloc ; alloc ; } callfunc ; return ; }")); + ASSERT_EQUALS(";", simplifycode("loop callfunc ;")); // exit.. ASSERT_EQUALS("; exit ;", simplifycode("; alloc; exit;"));