Skip to content

Commit

Permalink
Merge branch 'chr_Fix12540_42' of https://github.com/chrchr-github/cp…
Browse files Browse the repository at this point in the history
…pcheck into chr_Fix12540_42
  • Loading branch information
chrchr-github committed Mar 24, 2024
2 parents ebc0d9f + 5ea8330 commit 54de32a
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cfg/opencv2.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<noreturn>false</noreturn>
<returnValue type="void"/>
<arg nr="1">
<not-uninit indirect="1"/>
<not-uninit/>
</arg>
</function>
<!-- void * cv::fastMalloc (size_t bufSize) -->
Expand Down
31 changes: 23 additions & 8 deletions cfg/wxwidgets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@
<def format="2">
<type-checks>
<unusedvar>
<check>wxRect</check>
<check>wxSize</check>
<check>wxAcceleratorEntry</check>
<check>wxArrayDouble</check>
<check>wxArrayInt</check>
<check>wxArrayPtrVoid</check>
<check>wxArrayShort</check>
<check>wxArrayString</check>
<check>wxBrush</check>
<check>wxColour</check>
<check>wxDateSpan</check>
<check>wxFileType</check>
<check>wxFont</check>
<check>wxFontInfo</check>
<check>wxFontList</check>
<check>wxPen</check>
<check>wxPenList</check>
<check>wxPoint</check>
<check>wxPoint2DInt</check>
<check>wxPoint2DDouble</check>
<check>wxPoint2DInt</check>
<check>wxPosition</check>
<check>wxRealPoint</check>
<check>wxVersionInfo</check>
<check>wxRegion</check>
<check>wxRegEx</check>
<check>wxPosition</check>
<check>wxFileType</check>
<check>wxRect</check>
<check>wxSize</check>
<check>wxSystemOptions</check>
<check>wxSystemSettings</check>
<check>wxTimeSpan</check>
<check>wxDateSpan</check>
<check>wxAcceleratorEntry</check>
<check>wxVector</check>
<check>wxVersionInfo</check>
</unusedvar>
<operatorEqVarError>
</operatorEqVarError>
Expand Down
6 changes: 6 additions & 0 deletions lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,20 +1075,26 @@ bool Library::isuninitargbad(const Token *ftok, int argnr, int indirect, bool *h
/** get allocation info for function */
const Library::AllocFunc* Library::getAllocFuncInfo(const Token *tok) const
{
while (Token::simpleMatch(tok, "::"))
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
const std::string funcname = getFunctionName(tok);
return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mAlloc, funcname);
}

/** get deallocation info for function */
const Library::AllocFunc* Library::getDeallocFuncInfo(const Token *tok) const
{
while (Token::simpleMatch(tok, "::"))
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
const std::string funcname = getFunctionName(tok);
return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mDealloc, funcname);
}

/** get reallocation info for function */
const Library::AllocFunc* Library::getReallocFuncInfo(const Token *tok) const
{
while (Token::simpleMatch(tok, "::"))
tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
const std::string funcname = getFunctionName(tok);
return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mRealloc, funcname);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2558,11 +2558,11 @@ Function::Function(const Token *tok,
}

// class constructor/destructor
else if (((tokenDef->str() == scope->className) ||
else if (scope->isClassOrStructOrUnion() &&
((tokenDef->str() == scope->className) ||
(tokenDef->str().substr(0, scope->className.size()) == scope->className &&
tokenDef->str().size() > scope->className.size() + 1 &&
tokenDef->str()[scope->className.size() + 1] == '<')) &&
scope->type != Scope::ScopeType::eNamespace) {
tokenDef->str()[scope->className.size() + 1] == '<'))) {
// destructor
if (tokenDef->previous()->str() == "~")
type = Function::eDestructor;
Expand Down
7 changes: 1 addition & 6 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2200,15 +2200,11 @@ static void valueFlowReverse(const TokenList& tokenlist,
Token* tok,
const Token* const varToken,
ValueFlow::Value val,
const ValueFlow::Value& val2,
ErrorLogger* errorLogger,
const Settings& settings,
SourceLocation loc = SourceLocation::current())
{
std::list<ValueFlow::Value> values = {std::move(val)};
if (val2.varId != 0)
values.push_back(val2);
valueFlowReverse(tok, nullptr, varToken, std::move(values), tokenlist, errorLogger, settings, loc);
valueFlowReverse(tok, nullptr, varToken, {std::move(val)}, tokenlist, errorLogger, settings, loc);
}

static bool isConditionKnown(const Token* tok, bool then)
Expand Down Expand Up @@ -7639,7 +7635,6 @@ static void valueFlowSwitchVariable(const TokenList &tokenlist, const SymbolData
const_cast<Token*>(scope.classDef),
vartok,
*val,
ValueFlow::Value(),
errorLogger,
settings);
}
Expand Down
9 changes: 6 additions & 3 deletions test/cfg/opencv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ void validCode(const char* argStr)
cvStr += " World";
std::cout << cvStr;

char * pBuf = (char *)cv::fastMalloc(20); // cppcheck-suppress cstyleCast
// cppcheck-suppress [cstyleCast, unusedAllocatedMemory]
char * pBuf = (char *)cv::fastMalloc(20);
cv::fastFree(pBuf);
}

Expand All @@ -42,7 +43,9 @@ void ignoredReturnValue()

void memleak()
{
const char * pBuf = (char *)cv::fastMalloc(1000); // cppcheck-suppress cstyleCast
std::cout << pBuf; // cppcheck-suppress valueFlowBailoutIncompleteVar
// cppcheck-suppress cstyleCast
const char * pBuf = (char *)cv::fastMalloc(1000);
// cppcheck-suppress [uninitdata, valueFlowBailoutIncompleteVar]
std::cout << pBuf;
// cppcheck-suppress memleak
}
6 changes: 3 additions & 3 deletions test/cfg/std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,7 @@ void uninitvar_longjmp(void)
void uninitvar_malloc(void)
{
size_t size;
// cppcheck-suppress [uninitvar, cstyleCast]
// cppcheck-suppress [uninitvar, cstyleCast, unusedAllocatedMemory]
int *p = (int*)std::malloc(size);
free(p);
}
Expand Down Expand Up @@ -4957,7 +4957,7 @@ void std_vector_data_arithmetic()

void memleak_std_malloc() // #12332
{
//cppcheck-suppress [unreadVariable, constVariablePointer]
//cppcheck-suppress [unreadVariable, constVariablePointer, unusedAllocatedMemory]
void* p = std::malloc(1);
//cppcheck-suppress memleak
}
Expand All @@ -4971,7 +4971,7 @@ void memleak_std_realloc(void* block, size_t newsize)

void unusedAllocatedMemory_std_free()
{
// TODO cppcheck-suppress unusedAllocatedMemory
// cppcheck-suppress unusedAllocatedMemory
void* p = std::malloc(1);
std::free(p);
}
Expand Down
168 changes: 156 additions & 12 deletions test/cfg/wxwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,177 @@
#include <wx/wx.h>
#include <wx/accel.h>
#include <wx/app.h>
#include <wx/dc.h>
#include <wx/log.h>
#include <wx/filefn.h>
#include <wx/geometry.h>
#include <wx/spinctrl.h>
#include <wx/artprov.h>
#include <wx/bitmap.h>
#include <wx/brush.h>
#include <wx/calctrl.h>
#include <wx/colour.h>
#include <wx/combo.h>
#include <wx/icon.h>
#include <wx/bitmap.h>
#include <wx/dc.h>
#include <wx/dataview.h>
#include <wx/datetime.h>
#include <wx/memory.h>
#include <wx/dc.h>
#include <wx/dynarray.h>
#include <wx/filefn.h>
#include <wx/font.h>
#include <wx/frame.h>
#include <wx/gdicmn.h>
#include <wx/geometry.h>
#include <wx/icon.h>
#include <wx/log.h>
#include <wx/menu.h>
#include <wx/memory.h>
#include <wx/mimetype.h>
#include <wx/pen.h>
#include <wx/position.h>
#include <wx/propgrid/property.h>
#include <wx/regex.h>
#include <wx/region.h>
#include <wx/stattext.h>
#include <wx/settings.h>
#include <wx/spinctrl.h>
#include <wx/sizer.h>
#include <wx/string.h>
#include <wx/sysopt.h>
#include <wx/textctrl.h>
#include <wx/gdicmn.h>
#include <wx/propgrid/property.h>
#include <wx/position.h>
#include <wx/vector.h>
#include <wx/versioninfo.h>

void unusedVariable_wxSystemOptions()
{
// cppcheck-suppress unusedVariable
wxSystemOptions a;
}

void unusedVariable_wxSystemSettings()
{
// cppcheck-suppress unusedVariable
wxSystemSettings a;
}

void unusedVariable_wxPenList()
{
// cppcheck-suppress unusedVariable
wxPenList a;
}

void unusedVariable_wxPen(const wxColour &colour, int width, const wxPenStyle style, const wxPen &pen)
{
// cppcheck-suppress unusedVariable
wxPen a;
// cppcheck-suppress unreadVariable
wxPen b(colour, width);
// cppcheck-suppress unreadVariable
wxPen c(colour, width, style);
// cppcheck-suppress unreadVariable
wxPen d(pen);
}

void unusedVariable_wxBrush(const wxColour &color, const wxBrushStyle style, const wxBitmap &bmp, const wxBrush &brush)
{
// cppcheck-suppress unusedVariable
wxBrush a;
// cppcheck-suppress unreadVariable
wxBrush b(color, style);
// cppcheck-suppress unreadVariable
wxBrush c(bmp);
// cppcheck-suppress unreadVariable
wxBrush d(brush);
}

void unusedVariable_wxFontList()
{
// cppcheck-suppress unusedVariable
wxFontList a;
}

void unusedVariable_wxFontInfo(const double pointSize, const wxSize &sz)
{
// cppcheck-suppress unusedVariable
wxFontInfo a;
// cppcheck-suppress unreadVariable
wxFontInfo b(pointSize);
// cppcheck-suppress unreadVariable
wxFontInfo c(sz);
}

void unusedVariable_wxFont(const wxFont &font,
const wxFontInfo &fontInfo,
const int pointSize,
const wxFontFamily family,
const wxFontStyle style,
const wxFontWeight weight,
const bool underline,
const wxString &faceName,
const wxFontEncoding encoding)
{
// cppcheck-suppress unusedVariable
wxFont a;
// cppcheck-suppress unreadVariable
wxFont b(font);
// cppcheck-suppress unreadVariable
wxFont c(fontInfo);
// cppcheck-suppress unreadVariable
wxFont d(pointSize, family, style, weight);
// cppcheck-suppress unreadVariable
wxFont e(pointSize, family, style, weight, underline);
// cppcheck-suppress unreadVariable
wxFont f(pointSize, family, style, weight, underline, faceName);
// cppcheck-suppress unreadVariable
wxFont g(pointSize, family, style, weight, underline, faceName, encoding);
}

void unusedVariable_wxVector()
{
// cppcheck-suppress unusedVariable
wxVector<int> a;
}

void unusedVariable_wxArrayInt()
{
// cppcheck-suppress unusedVariable
wxArrayInt a;
}

void unusedVariable_wxArrayDouble()
{
// cppcheck-suppress unusedVariable
wxArrayDouble a;
}

void unusedVariable_wxArrayShort()
{
// cppcheck-suppress unusedVariable
wxArrayShort a;
}

void unusedVariable_wxArrayString()
{
// cppcheck-suppress unusedVariable
wxArrayString a;
}

void unusedVariable_wxArrayPtrVoid()
{
// cppcheck-suppress unusedVariable
wxArrayPtrVoid a;
}

void unreadVariable_wxColour(const unsigned char uc, const wxString &name, const unsigned long colRGB, const wxColour &colour)
{
// cppcheck-suppress unusedVariable
wxColour a;
// cppcheck-suppress unreadVariable
wxColour b(uc, uc, uc);
// cppcheck-suppress unreadVariable
wxColour c(uc, uc, uc, uc);
// cppcheck-suppress unreadVariable
wxColour d(name);
// cppcheck-suppress unreadVariable
wxColour e(colRGB);
// cppcheck-suppress unreadVariable
wxColour f(colour);
}

void unreadVariable_wxPoint2DInt(const wxInt32 x, const wxPoint2DInt& pti, const wxPoint &pt)
{
// cppcheck-suppress unusedVariable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
o oo(){extern oo();}
6 changes: 6 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,12 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(db && ctor && ctor->type == Function::eMoveConstructor);
ASSERT(ctor && ctor->retDef == nullptr);
}
{
GET_SYMBOL_DB("void f() { extern void f(); }");
ASSERT(db && db->scopeList.size() == 2);
const Function* f = findFunctionByName("f", &db->scopeList.back());
ASSERT(f && f->type == Function::eFunction);
}
}

void functionDeclarationTemplate() {
Expand Down

0 comments on commit 54de32a

Please sign in to comment.