Skip to content

Commit 6b2bab5

Browse files
committed
CLI,LIB: Added --std setting for GTK
1 parent ba23b65 commit 6b2bab5

4 files changed

Lines changed: 56 additions & 48 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
478478
// --std
479479
else if (std::strcmp(argv[i], "--std=posix") == 0) {
480480
_settings->standards.posix = true;
481+
} else if (std::strcmp(argv[i], "--std=gtk") == 0) {
482+
_settings->standards.gtk = true;
481483
} else if (std::strcmp(argv[i], "--std=c89") == 0) {
482484
_settings->standards.c = Standards::C89;
483485
} else if (std::strcmp(argv[i], "--std=c99") == 0) {
@@ -863,6 +865,8 @@ void CmdLineParser::PrintHelp()
863865
#endif
864866
" --std=<id> Set standard.\n"
865867
" The available options are:\n"
868+
" * gtk\n"
869+
" GTK code\n"
866870
" * posix\n"
867871
" POSIX compatible code\n"
868872
" * c89\n"

lib/checkmemoryleak.cpp

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -149,22 +149,24 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
149149
return Malloc;
150150

151151
// Does tok2 point on "g_malloc", "g_strdup", ..
152-
static const char * const gmallocfunc[] = {
153-
"g_new",
154-
"g_new0",
155-
"g_try_new",
156-
"g_try_new0",
157-
"g_malloc",
158-
"g_malloc0",
159-
"g_try_malloc",
160-
"g_try_malloc0",
161-
"g_strdup",
162-
"g_strndup",
163-
"g_strdup_printf"
164-
};
165-
for (unsigned int i = 0; i < sizeof(gmallocfunc)/sizeof(*gmallocfunc); i++) {
166-
if (tok2->str() == gmallocfunc[i])
167-
return gMalloc;
152+
if (standards.gtk) {
153+
static const char * const gmallocfunc[] = {
154+
"g_new",
155+
"g_new0",
156+
"g_try_new",
157+
"g_try_new0",
158+
"g_malloc",
159+
"g_malloc0",
160+
"g_try_malloc",
161+
"g_try_malloc0",
162+
"g_strdup",
163+
"g_strndup",
164+
"g_strdup_printf"
165+
};
166+
for (unsigned int i = 0; i < sizeof(gmallocfunc)/sizeof(*gmallocfunc); i++) {
167+
if (tok2->str() == gmallocfunc[i])
168+
return gMalloc;
169+
}
168170
}
169171

170172
if (Token::Match(tok2, "new struct| %type% [;()]") ||
@@ -180,17 +182,19 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
180182
if (Token::Match(tok2, "fopen|tmpfile|g_fopen ("))
181183
return File;
182184

183-
if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp (")) {
184-
// simple sanity check of function parameters..
185-
// TODO: Make such check for all these functions
186-
unsigned int num = countParameters(tok2);
187-
if (tok2->str() == "open" && num != 2 && num != 3)
188-
return No;
185+
if (standards.posix) {
186+
if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp (")) {
187+
// simple sanity check of function parameters..
188+
// TODO: Make such check for all these functions
189+
unsigned int num = countParameters(tok2);
190+
if (tok2->str() == "open" && num != 2 && num != 3)
191+
return No;
189192

190-
// is there a user function with this name?
191-
if (tokenizer && Token::findmatch(tokenizer->tokens(), ("%type% *|&| " + tok2->str()).c_str()))
192-
return No;
193-
return Fd;
193+
// is there a user function with this name?
194+
if (tokenizer && Token::findmatch(tokenizer->tokens(), ("%type% *|&| " + tok2->str()).c_str()))
195+
return No;
196+
return Fd;
197+
}
194198
}
195199

196200
if (Token::simpleMatch(tok2, "popen ("))
@@ -220,11 +224,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
220224
return functionReturnType(func, callstack);
221225
}
222226

223-
static bool isPosixAllocationType(const CheckMemoryLeak::AllocType allocType)
224-
{
225-
return (allocType == CheckMemoryLeak::Fd || allocType == CheckMemoryLeak::Pipe || allocType == CheckMemoryLeak::Dir);
226-
}
227-
228227

229228
CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok2, unsigned int varid) const
230229
{
@@ -273,22 +272,26 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
273272
Token::Match(tok, "realloc ( %varid% , 0 ) ;", varid))
274273
return Malloc;
275274

276-
if (Token::Match(tok, "g_free ( %varid% ) ;", varid) ||
277-
Token::Match(tok, "g_free ( %varid% -", varid))
278-
return gMalloc;
275+
if (standards.gtk) {
276+
if (Token::Match(tok, "g_free ( %varid% ) ;", varid) ||
277+
Token::Match(tok, "g_free ( %varid% -", varid))
278+
return gMalloc;
279+
}
279280

280281
if (Token::Match(tok, "fclose ( %varid% )", varid) ||
281282
Token::simpleMatch(tok, "fcloseall ( )"))
282283
return File;
283284

284-
if (Token::Match(tok, "close ( %varid% )", varid))
285-
return Fd;
285+
if (standards.posix) {
286+
if (Token::Match(tok, "close ( %varid% )", varid))
287+
return Fd;
286288

287-
if (Token::Match(tok, "pclose ( %varid% )", varid))
288-
return Pipe;
289+
if (Token::Match(tok, "pclose ( %varid% )", varid))
290+
return Pipe;
289291

290-
if (Token::Match(tok, "closedir ( %varid% )", varid))
291-
return Dir;
292+
if (Token::Match(tok, "closedir ( %varid% )", varid))
293+
return Dir;
294+
}
292295

293296
return No;
294297
}
@@ -887,9 +890,6 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
887890
}
888891
}
889892

890-
if (isPosixAllocationType(alloc) && !_settings->standards.posix)
891-
alloc = CheckMemoryLeak::No;
892-
893893
// don't check classes..
894894
if (alloc == CheckMemoryLeak::New) {
895895
if (Token::Match(tok->tokAt(2), "new struct| %type% [(;]")) {
@@ -979,9 +979,6 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
979979

980980
AllocType dealloc = getDeallocationType(tok, varid);
981981

982-
if (isPosixAllocationType(dealloc) && !_settings->standards.posix)
983-
dealloc = CheckMemoryLeak::No;
984-
985982
if (dealloc != No && tok->str() == "fcloseall" && alloctype != dealloc)
986983
//TODO: this assignment is redundant, should be fixed
987984
/*dealloc = No*/;

lib/standards.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ struct Standards {
3333
/** C++ code standard */
3434
enum cppstd_t { CPP03, CPP11 } cpp;
3535

36+
/** Code is gtk */
37+
bool gtk;
38+
3639
/** Code is posix */
3740
bool posix;
3841

3942
/** This constructor clear all the variables **/
40-
Standards() : c(C11), cpp(CPP11), posix(false) {}
43+
Standards() : c(C11), cpp(CPP11), gtk(false), posix(false) {}
4144
};
4245

4346
/// @}

test/testmemleak.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ class TestMemleak : private TestFixture {
4444
errout.str("");
4545

4646
Settings settings;
47+
settings.standards.gtk = true;
4748

4849
// Tokenize..
4950
Tokenizer tokenizer(&settings, this);
5051
std::istringstream istr(code);
5152
tokenizer.tokenize(istr, "test.cpp");
5253

53-
return ((const CheckMemoryLeak *)0)->functionReturnType(&tokenizer.getSymbolDatabase()->scopeList.front().functionList.front());
54+
CheckMemoryLeak c(&tokenizer, this, settings.standards);
55+
56+
return c.functionReturnType(&tokenizer.getSymbolDatabase()->scopeList.front().functionList.front());
5457
}
5558

5659
void testFunctionReturnType() {
@@ -368,6 +371,7 @@ class TestMemleakInFunction : public TestFixture {
368371
errout.str("");
369372

370373
Settings settings;
374+
settings.standards.gtk = true;
371375
settings.standards.posix = true;
372376

373377
// Tokenize..

0 commit comments

Comments
 (0)