parent
f56677a99d
commit
efd488b519
|
@ -60,6 +60,18 @@ static const int NEW = -1;
|
||||||
static const std::array<std::pair<std::string, std::string>, 4> alloc_failed_conds {{{"==", "0"}, {"<", "0"}, {"==", "-1"}, {"<=", "-1"}}};
|
static const std::array<std::pair<std::string, std::string>, 4> alloc_failed_conds {{{"==", "0"}, {"<", "0"}, {"==", "-1"}, {"<=", "-1"}}};
|
||||||
static const std::array<std::pair<std::string, std::string>, 4> alloc_success_conds {{{"!=", "0"}, {">", "0"}, {"!=", "-1"}, {">=", "0"}}};
|
static const std::array<std::pair<std::string, std::string>, 4> alloc_success_conds {{{"!=", "0"}, {">", "0"}, {"!=", "-1"}, {">=", "0"}}};
|
||||||
|
|
||||||
|
static bool isAutoDeallocType(const Type* type) {
|
||||||
|
if (!type)
|
||||||
|
return true;
|
||||||
|
if (type->classScope && type->classScope->numConstructors == 0 &&
|
||||||
|
(type->classScope->varlist.empty() || type->needInitialization == Type::NeedInitialization::True) &&
|
||||||
|
std::none_of(type->derivedFrom.cbegin(), type->derivedFrom.cend(), [](const Type::BaseInfo& bi) {
|
||||||
|
return isAutoDeallocType(bi.type);
|
||||||
|
}))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Is variable type some class with automatic deallocation?
|
* @brief Is variable type some class with automatic deallocation?
|
||||||
* @param var variable token
|
* @param var variable token
|
||||||
|
@ -72,14 +84,8 @@ static bool isAutoDealloc(const Variable *var)
|
||||||
|
|
||||||
// return false if the type is a simple record type without side effects
|
// return false if the type is a simple record type without side effects
|
||||||
// a type that has no side effects (no constructors and no members with constructors)
|
// a type that has no side effects (no constructors and no members with constructors)
|
||||||
/** @todo false negative: check base class for side effects */
|
|
||||||
/** @todo false negative: check constructors for side effects */
|
/** @todo false negative: check constructors for side effects */
|
||||||
if (var->typeScope() && var->typeScope()->numConstructors == 0 &&
|
return isAutoDeallocType(var->type());
|
||||||
(var->typeScope()->varlist.empty() || var->type()->needInitialization == Type::NeedInitialization::True) &&
|
|
||||||
var->type()->derivedFrom.empty())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t N>
|
template<std::size_t N>
|
||||||
|
|
|
@ -601,6 +601,14 @@ private:
|
||||||
" b = Bar(*new Foo(data));\n"
|
" b = Bar(*new Foo(data));\n"
|
||||||
"}", /*cpp*/ true);
|
"}", /*cpp*/ true);
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function Foo() should have <use>/<leak-ignore> configuration\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (information) --check-library: Function Foo() should have <use>/<leak-ignore> configuration\n", errout.str());
|
||||||
|
|
||||||
|
check("class B {};\n"
|
||||||
|
" class D : public B {};\n"
|
||||||
|
" void g() {\n"
|
||||||
|
" auto d = new D();\n"
|
||||||
|
" if (d) {}\n"
|
||||||
|
"}", /*cpp*/ true);
|
||||||
|
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: d\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void realloc1() {
|
void realloc1() {
|
||||||
|
|
Loading…
Reference in New Issue