Memory Leaks: Convert "do-while" blocks to "while" blocks to make the handling for that the same.
This commit is contained in:
parent
cdf9ee4d8a
commit
34f44e3c04
|
@ -201,6 +201,16 @@ static void instoken(TOKEN *tok, const char str[])
|
|||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
static bool notvar(const TOKEN *tok, const char *varnames[])
|
||||
{
|
||||
return bool( Match(tok, "! %var1%", varnames) ||
|
||||
Match(tok, "unlikely ( ! %var1% )", varnames) ||
|
||||
Match(tok, "unlikely ( %var1% == NULL )", varnames) ||
|
||||
Match(tok, "%var1% == NULL", varnames) ||
|
||||
Match(tok, "NULL == %var1%", varnames) ||
|
||||
Match(tok, "%var1% == 0", varnames) );
|
||||
}
|
||||
|
||||
|
||||
extern bool ShowAll;
|
||||
|
||||
|
@ -305,12 +315,7 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
|
|||
addtoken("if(var)");
|
||||
tok = gettok(tok, 3); // Make sure the "use" will not be added
|
||||
}
|
||||
else if ( Match(tok, "if ( ! %var1% )", varnames) ||
|
||||
Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) ||
|
||||
Match(tok, "if ( unlikely ( %var1% == NULL ) )", varnames) ||
|
||||
Match(tok, "if ( %var1% == NULL )", varnames) ||
|
||||
Match(tok, "if ( NULL == %var1% )", varnames) ||
|
||||
Match(tok, "if ( %var1% == 0 )", varnames) )
|
||||
else if ( Match(tok, "if (") && notvar(gettok(tok,2), varnames) )
|
||||
{
|
||||
addtoken("if(!var)");
|
||||
}
|
||||
|
@ -334,12 +339,16 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[])
|
|||
}
|
||||
|
||||
// Loops..
|
||||
if (Match(tok, "for") || Match(tok, "while") || Match(tok, "do") )
|
||||
if (Match(tok, "for") || Match(tok, "while") )
|
||||
{
|
||||
addtoken("loop");
|
||||
isloop = Match(tok, "%var% (");
|
||||
isloop = true;
|
||||
}
|
||||
if ( isloop && Match(tok,"%var1%",varnames) )
|
||||
if ( Match(tok, "do") )
|
||||
{
|
||||
addtoken("do");
|
||||
}
|
||||
if ( isloop && notvar(tok,varnames) )
|
||||
addtoken("!var");
|
||||
|
||||
// continue / break..
|
||||
|
@ -407,6 +416,58 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
|
|||
}
|
||||
|
||||
|
||||
// Remove "do"...
|
||||
// do { x } while (y);
|
||||
// =>
|
||||
// { x } while(y) { x }"
|
||||
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
|
||||
{
|
||||
if ( ! Match(tok2->next, "do") )
|
||||
continue;
|
||||
|
||||
// Remove the next token "do"
|
||||
erase( tok2, gettok(tok2, 2) );
|
||||
tok2 = tok2->next;
|
||||
|
||||
// Find the end of the "do" block..
|
||||
TOKEN *tok2_;
|
||||
int indentlevel = 0;
|
||||
for ( tok2_ = tok2; tok2_ && indentlevel>=0; tok2_ = tok2_->next )
|
||||
{
|
||||
if ( Match(tok2_, "{") )
|
||||
++indentlevel;
|
||||
|
||||
else if ( Match(tok2_, "}") )
|
||||
--indentlevel;
|
||||
|
||||
else if ( indentlevel == 0 && Match(tok2_->next, ";") )
|
||||
break;
|
||||
}
|
||||
|
||||
// End not found?
|
||||
if ( ! tok2_ )
|
||||
continue;
|
||||
|
||||
// Copy code..
|
||||
indentlevel = 0;
|
||||
do
|
||||
{
|
||||
if ( Match( tok2, "{" ) )
|
||||
++indentlevel;
|
||||
else if ( Match(tok2, "}") )
|
||||
--indentlevel;
|
||||
|
||||
// Copy token..
|
||||
instoken( tok2_, tok2->str );
|
||||
|
||||
// Next token..
|
||||
tok2 = tok2->next;
|
||||
tok2_ = tok2_->next;
|
||||
}
|
||||
while ( tok2 && indentlevel > 0 );
|
||||
}
|
||||
|
||||
|
||||
// reduce the code..
|
||||
bool done = false;
|
||||
while ( ! done )
|
||||
|
@ -624,6 +685,11 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
|
|||
MemoryLeak(gettok(findmatch(tok,"alloc ; return ;"),2), varname);
|
||||
}
|
||||
|
||||
else if ( findmatch(tok, "alloc ; alloc") )
|
||||
{
|
||||
MemoryLeak(gettok(findmatch(tok,"alloc ; alloc"),2), varname);
|
||||
}
|
||||
|
||||
else if ( ! findmatch(tok,"dealloc") &&
|
||||
! findmatch(tok,"use") &&
|
||||
! findmatch(tok,"return use ;") )
|
||||
|
|
|
@ -77,6 +77,8 @@ private:
|
|||
TEST_CASE( forwhile5 );
|
||||
TEST_CASE( forwhile6 );
|
||||
|
||||
TEST_CASE( dowhile1 );
|
||||
|
||||
TEST_CASE( switch1 );
|
||||
TEST_CASE( switch2 );
|
||||
|
||||
|
@ -407,6 +409,23 @@ private:
|
|||
|
||||
|
||||
|
||||
void dowhile1()
|
||||
{
|
||||
check( "void f()\n"
|
||||
"{\n"
|
||||
" char *str = strdup(\"abc\");\n"
|
||||
" do\n"
|
||||
" {\n"
|
||||
" str = strdup(\"def\");\n"
|
||||
" }\n"
|
||||
" while (!str);\n"
|
||||
" return str;\n"
|
||||
"}\n" );
|
||||
ASSERT_EQUALS( std::string("[test.cpp:5]: Memory leak: str\n"), errout.str() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
void switch1()
|
||||
{
|
||||
check("void f()\n"
|
||||
|
|
|
@ -18,15 +18,15 @@
|
|||
<CfgParent>Base</CfgParent>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Base)'!=''">
|
||||
<BCC_OptimizeForSpeed>true</BCC_OptimizeForSpeed>
|
||||
<OutputExt>exe</OutputExt>
|
||||
<Defines>NO_STRICT</Defines>
|
||||
<BCC_OptimizeForSpeed>true</BCC_OptimizeForSpeed>
|
||||
<DCC_CBuilderOutput>JPHNE</DCC_CBuilderOutput>
|
||||
<Defines>NO_STRICT</Defines>
|
||||
<DynamicRTL>true</DynamicRTL>
|
||||
<UsePackages>true</UsePackages>
|
||||
<ILINK_ObjectSearchPath>C:\cppcheck</ILINK_ObjectSearchPath>
|
||||
<ProjectType>CppConsoleApplication</ProjectType>
|
||||
<UsePackages>true</UsePackages>
|
||||
<NoVCL>true</NoVCL>
|
||||
<ProjectType>CppConsoleApplication</ProjectType>
|
||||
<FinalOutputDir>.</FinalOutputDir>
|
||||
<PackageImports>vclx.bpi;vcl.bpi;rtl.bpi;vclactnband.bpi</PackageImports>
|
||||
<BCC_wpar>false</BCC_wpar>
|
||||
|
@ -34,10 +34,10 @@
|
|||
<ILINK_LibraryPath>$(BDS)\lib;$(BDS)\lib\obj;$(BDS)\lib\psdk;C:\cppcheck</ILINK_LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Cfg_1)'!=''">
|
||||
<DCC_Optimize>false</DCC_Optimize>
|
||||
<BCC_OptimizeForSpeed>false</BCC_OptimizeForSpeed>
|
||||
<Defines>_DEBUG;$(Defines)</Defines>
|
||||
<DCC_Optimize>false</DCC_Optimize>
|
||||
<DCC_DebugInfoInExe>true</DCC_DebugInfoInExe>
|
||||
<Defines>_DEBUG;$(Defines)</Defines>
|
||||
<ILINK_FullDebugInfo>true</ILINK_FullDebugInfo>
|
||||
<BCC_InlineFunctionExpansion>false</BCC_InlineFunctionExpansion>
|
||||
<ILINK_DisableIncrementalLinking>true</ILINK_DisableIncrementalLinking>
|
||||
|
@ -48,8 +48,8 @@
|
|||
<IntermediateOutputDir>bcb_Debug</IntermediateOutputDir>
|
||||
<TASM_DisplaySourceLines>true</TASM_DisplaySourceLines>
|
||||
<BCC_StackFrames>true</BCC_StackFrames>
|
||||
<ILINK_LibraryPath>$(BDS)\lib\debug;$(ILINK_LibraryPath)</ILINK_LibraryPath>
|
||||
<BCC_DisableOptimizations>true</BCC_DisableOptimizations>
|
||||
<ILINK_LibraryPath>$(BDS)\lib\debug;$(ILINK_LibraryPath)</ILINK_LibraryPath>
|
||||
<TASM_Debugging>Full</TASM_Debugging>
|
||||
<BCC_SourceDebuggingOn>true</BCC_SourceDebuggingOn>
|
||||
</PropertyGroup>
|
||||
|
@ -125,6 +125,9 @@
|
|||
<CppCompile Include="testtokenize.cpp">
|
||||
<BuildOrder>15</BuildOrder>
|
||||
</CppCompile>
|
||||
<CppCompile Include="testunusedvar.cpp">
|
||||
<BuildOrder>17</BuildOrder>
|
||||
</CppCompile>
|
||||
<CppCompile Include="tokenize.cpp">
|
||||
<DependentOn>tokenize.h</DependentOn>
|
||||
<BuildOrder>0</BuildOrder>
|
||||
|
|
Loading…
Reference in New Issue